Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Encrypt the data with the key and the AES256-CBC algorithm
const encryptedData: EncryptionTypes.IEncryptedData = await Utils.encryption.encrypt(
data,
channelKey,
);
// Compute the hash of the data
let hash: MultiFormatTypes.HashTypes.IHash;
try {
hash = Utils.crypto.normalizeKeccak256Hash(JSON.parse(data));
} catch (error) {
throw new Error('Data not parsable');
}
const encryptedDataSerialized: string = MultiFormat.serialize(encryptedData);
const hashSerialized: string = MultiFormat.serialize(hash);
return { encryptedData: encryptedDataSerialized, hash: hashSerialized };
}
}
function getActionHash(action: RequestLogicTypes.IAction): string {
// Before the version 2.0.0, the hash was computed without the signature
if (Semver.lte(action.data.version, '2.0.0')) {
return MultiFormat.serialize(Utils.crypto.normalizeKeccak256Hash(action.data));
}
return MultiFormat.serialize(Utils.crypto.normalizeKeccak256Hash(action));
}
public async getRequestsByTopic(
topic: string,
updatedBetween?: RequestLogicTypes.ITimestampBoundaries,
): Promise {
// hash all the topics
const hashedTopic = MultiFormat.serialize(Utils.crypto.normalizeKeccak256Hash(topic));
const getChannelsResult = await this.transactionManager.getChannelsByTopic(
hashedTopic,
updatedBetween,
);
return this.computeMultipleRequestFromChannels(getChannelsResult);
}
public async getHash(): Promise {
if (this.dataHashSerialized === '') {
const data = await this.getData();
try {
const dataHash = await Utils.crypto.normalizeKeccak256Hash(JSON.parse(data));
this.dataHashSerialized = MultiFormat.serialize(dataHash);
} catch (e) {
throw new Error('Impossible to JSON parse the decrypted transaction data');
}
}
return this.dataHashSerialized;
}
(
allKeys: TransactionTypes.IKeysDictionary,
keyAndHash: {
encryptedKey: EncryptionTypes.IEncryptedData;
multiFormattedIdentity: string;
},
): TransactionTypes.IKeysDictionary => {
const encryptedKeySerialized: string = MultiFormat.serialize(keyAndHash.encryptedKey);
allKeys[keyAndHash.multiFormattedIdentity] = encryptedKeySerialized;
return allKeys;
},
{},
allKeys: TransactionTypes.IKeysDictionary,
keyAndHash: {
encryptedKey: EncryptionTypes.IEncryptedData;
multiFormattedIdentity: string;
},
): TransactionTypes.IKeysDictionary => {
const encryptedKeySerialized: string = MultiFormat.serialize(keyAndHash.encryptedKey);
allKeys[keyAndHash.multiFormattedIdentity] = encryptedKeySerialized;
return allKeys;
},
{},
);
const encryptedDataSerialized: string = MultiFormat.serialize(encryptedData);
const hashSerialized: string = MultiFormat.serialize(hash);
return { encryptedData: encryptedDataSerialized, keys, hash: hashSerialized, encryptionMethod };
}
(
allKeys: TransactionTypes.IKeysDictionary,
keyAndHash: {
encryptedKey: EncryptionTypes.IEncryptedData;
multiFormattedIdentity: string;
},
): TransactionTypes.IKeysDictionary => {
const encryptedKeySerialized: string = MultiFormat.serialize(keyAndHash.encryptedKey);
allKeys[keyAndHash.multiFormattedIdentity] = encryptedKeySerialized;
return allKeys;
},
{},
);
const encryptedDataSerialized: string = MultiFormat.serialize(encryptedData);
const hashSerialized: string = MultiFormat.serialize(hash);
return { encryptedData: encryptedDataSerialized, keys, hash: hashSerialized, encryptionMethod };
}
public async getHash(): Promise {
return MultiFormat.serialize(Utils.crypto.normalizeKeccak256Hash(JSON.parse(this.data)));
}
async (
encryptionParam: EncryptionTypes.IEncryptionParameters,
): Promise<{
encryptedKey: EncryptionTypes.IEncryptedData;
multiFormattedIdentity: string;
}> => {
const encryptedKey: EncryptionTypes.IEncryptedData = await Utils.encryption.encrypt(
symmetricKey,
encryptionParam,
);
const identityEncryption = Utils.encryption.getIdentityFromEncryptionParams(
encryptionParam,
);
const multiFormattedIdentity: string = MultiFormat.serialize(identityEncryption);
return { encryptedKey, multiFormattedIdentity };
},
);
public async getData(): Promise {
if (this.data === '') {
try {
const encryptedData = MultiFormat.deserialize(this.persistedData);
this.data = await Utils.encryption.decrypt(encryptedData, this.channelKey);
} catch {
throw new Error('Impossible to decrypt the transaction');
}
}
return this.data;
}