Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private async decryptChannelKey(
keys: TransactionTypes.IKeysDictionary,
encryptionMethod: string,
): Promise {
// Check if the decryption provider is given
if (!this.decryptionProvider) {
throw new Error(`No decryption provider given`);
}
// Check the encryption method
if (
encryptionMethod !== `${EncryptionTypes.METHOD.ECIES}-${EncryptionTypes.METHOD.AES256_CBC}`
) {
throw new Error(`Encryption method not supported: ${encryptionMethod}`);
}
let errorReason = '';
// Try to decrypt the channelKey
const channelKey = await Object.keys(keys || {}).reduce(
async (decryptedChannelKeyPromise, identityMultiFormatted: string) => {
let decryptedChannelKey = await decryptedChannelKeyPromise;
if (keys && decryptedChannelKey === '') {
// TODO: PROT-745 - need a library to serialize/de-serialize multi-format
// Ignore what is not an identity Ethereum address
if (Utils.multiFormat.isIdentityEthereumAddress(identityMultiFormatted)) {
const identity: IdentityTypes.IIdentity = {
type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
value: `0x${Utils.multiFormat.removePadding(identityMultiFormatted)}`,
public static async createEncryptedTransaction(
data: TransactionTypes.ITransactionData,
channelKey: EncryptionTypes.IEncryptionParameters,
): Promise {
// check if the encryption method is the good one
if (channelKey.method !== EncryptionTypes.METHOD.AES256_CBC) {
throw new Error(`encryption method not supported for the channel key: ${channelKey.method}`);
}
// 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');
}
expectedAmount: '170000000000',
payee: {
type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
value: '0xAf083f77F1fFd54218d91491AFD06c9296EaC3ce',
},
payer: {
type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
value: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
},
timestamp: 1544426030,
};
const payeeEncryptionParameters: EncryptionTypes.IEncryptionParameters = {
key:
'9008306d319755055226827c22f4b95552c799bae7af0e99780cf1b5500d9d1ecbdbcf6f27cdecc72c97fef3703c54b717bca613894212e0b2525cbb2d1161b9',
method: EncryptionTypes.METHOD.ECIES,
};
const payeeDecryptionParameters: EncryptionTypes.IDecryptionParameters = {
key: '0x0906ff14227cead2b25811514302d57706e7d5013fcc40eca5985b216baeb998',
method: EncryptionTypes.METHOD.ECIES,
};
const payerEncryptionParameters: EncryptionTypes.IEncryptionParameters = {
key:
'cf4a1d0bbef8bf0e3fa479a9def565af1b22ea6266294061bfb430701b54a83699e3d47bf52e9f0224dcc29a02721810f1f624f1f70ea3cc5f1fb752cfed379d',
method: EncryptionTypes.METHOD.ECIES,
};
// A signature provider, for example @requestnetwork/epk-signature
const signatureProvider: SignatureProviderTypes.ISignatureProvider = new EthereumPrivateKeySignatureProvider(
{
method: SignatureTypes.METHOD.ECDSA,
public async decrypt(
encryptedData: EncryptionTypes.IEncryptedData,
identity: IdentityTypes.IIdentity,
): Promise {
if (encryptedData.type !== EncryptionTypes.METHOD.ECIES) {
throw Error(`The data must be encrypted with ${EncryptionTypes.METHOD.ECIES}`);
}
if (!this.supportedIdentityTypes.includes(identity.type)) {
throw Error(`Identity type not supported ${identity.type}`);
}
// toLowerCase to avoid mismatch because of case
const decryptionParameters:
| EncryptionTypes.IDecryptionParameters
| undefined = this.decryptionParametersDictionary.get(identity.value.toLowerCase());
if (!decryptionParameters) {
throw Error(`private key unknown for the identity: ${identity.value}`);
}
return Utils.encryption.decrypt(encryptedData, decryptionParameters);
},
payer: {
type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
value: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
},
timestamp: 1544426030,
};
const payeeEncryptionParameters: EncryptionTypes.IEncryptionParameters = {
key:
'9008306d319755055226827c22f4b95552c799bae7af0e99780cf1b5500d9d1ecbdbcf6f27cdecc72c97fef3703c54b717bca613894212e0b2525cbb2d1161b9',
method: EncryptionTypes.METHOD.ECIES,
};
const payeeDecryptionParameters: EncryptionTypes.IDecryptionParameters = {
key: '0x0906ff14227cead2b25811514302d57706e7d5013fcc40eca5985b216baeb998',
method: EncryptionTypes.METHOD.ECIES,
};
const payerEncryptionParameters: EncryptionTypes.IEncryptionParameters = {
key:
'cf4a1d0bbef8bf0e3fa479a9def565af1b22ea6266294061bfb430701b54a83699e3d47bf52e9f0224dcc29a02721810f1f624f1f70ea3cc5f1fb752cfed379d',
method: EncryptionTypes.METHOD.ECIES,
};
// A signature provider, for example @requestnetwork/epk-signature
const signatureProvider: SignatureProviderTypes.ISignatureProvider = new EthereumPrivateKeySignatureProvider(
{
method: SignatureTypes.METHOD.ECDSA,
privateKey: '0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3',
},
);
async function encrypt(
data: string,
encryptionParams: EncryptionTypes.IEncryptionParameters,
): Promise {
if (encryptionParams.method === EncryptionTypes.METHOD.ECIES) {
const encryptedData = await Crypto.EcUtils.encrypt(encryptionParams.key, data);
return {
type: EncryptionTypes.METHOD.ECIES,
value: encryptedData,
};
}
if (encryptionParams.method === EncryptionTypes.METHOD.AES256_CBC) {
const encryptedDataBuffer = await Crypto.CryptoWrapper.encryptWithAes256cbc(
Buffer.from(data, 'utf-8'),
Buffer.from(encryptionParams.key, 'base64'),
);
return {
type: EncryptionTypes.METHOD.AES256_CBC,
value: encryptedDataBuffer.toString('Base64'),
};
import { DecryptionProviderTypes, EncryptionTypes, IdentityTypes } from '@requestnetwork/types';
import Utils from '@requestnetwork/utils';
/** Type of the dictionary of decryptionParameters (private keys) indexed by ethereum address */
type IDecryptionParametersDictionary = Map;
/**
* Implementation of the decryption provider from private key
* Allows to decrypt() with "ethereumAddress" identities thanks to their private key given in constructor() or addDecryptionParameters()
*/
export default class EthereumPrivateKeyDecryptionProvider
implements DecryptionProviderTypes.IDecryptionProvider {
/** list of supported encryption method */
public supportedMethods: EncryptionTypes.METHOD[] = [EncryptionTypes.METHOD.ECIES];
/** list of supported identity types */
public supportedIdentityTypes: IdentityTypes.TYPE[] = [IdentityTypes.TYPE.ETHEREUM_ADDRESS];
/** Dictionary containing all the private keys indexed by address */
private decryptionParametersDictionary: IDecryptionParametersDictionary;
constructor(decryptionParameters?: EncryptionTypes.IDecryptionParameters) {
this.decryptionParametersDictionary = new Map();
if (decryptionParameters) {
this.addDecryptionParameters(decryptionParameters);
}
}
/**
* Decrypts data
*
async function encrypt(
data: string,
encryptionParams: EncryptionTypes.IEncryptionParameters,
): Promise {
if (encryptionParams.method === EncryptionTypes.METHOD.ECIES) {
const encryptedData = await Crypto.EcUtils.encrypt(encryptionParams.key, data);
return {
type: EncryptionTypes.METHOD.ECIES,
value: encryptedData,
};
}
if (encryptionParams.method === EncryptionTypes.METHOD.AES256_CBC) {
const encryptedDataBuffer = await Crypto.CryptoWrapper.encryptWithAes256cbc(
Buffer.from(data, 'utf-8'),
Buffer.from(encryptionParams.key, 'base64'),
);
return {
type: EncryptionTypes.METHOD.AES256_CBC,
value: encryptedDataBuffer.toString('Base64'),
};
}
throw new Error('encryptionParams.method not supported');
}
(encryptionParam: EncryptionTypes.IEncryptionParameters) =>
encryptionParam.method === EncryptionTypes.METHOD.ECIES,
)