Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public addSignatureParameters(
signatureParams: SignatureTypes.ISignatureParameters,
): IdentityTypes.IIdentity {
if (!this.supportedMethods.includes(signatureParams.method)) {
throw Error(`Signing method not supported ${signatureParams.method}`);
}
// compute the address from private key
// toLowerCase to avoid mismatch because of case
const address = Utils.crypto.EcUtils.getAddressFromPrivateKey(
signatureParams.privateKey,
).toLowerCase();
this.signatureParametersDictionary.set(address, signatureParams);
return {
type: IdentityTypes.TYPE.ETHEREUM_ADDRESS,
value: address,
};
}
function calculate(requestId: string, salt: string, address: string): string {
if (!requestId || !salt || !address) {
throw new Error('RequestId, salt and address are mandatory to calculate the payment reference');
}
// "The value is the last 8 bytes of a salted hash of the requestId: `last8Bytes(hash(requestId + salt + address))`"
// tslint:disable:no-magic-numbers
return Utils.crypto.keccak256Hash((requestId + salt + address).toLowerCase()).slice(-16);
}
public async persistTransaction(
transactionData: TransactionTypes.ITransactionData,
channelId: string,
topics: string[] = [],
encryptionParams: EncryptionTypes.IEncryptionParameters[] = [],
): Promise {
let transaction: TransactionTypes.IPersistedTransaction = {};
let encryptionMethod: string | undefined;
// compute hash to add it to the topics
const hash = Utils.crypto.normalizeKeccak256Hash(JSON.parse(transactionData));
// Need to create a new channel (only the first transaction can have the hash equals to the channel id)
if (channelId === hash) {
if (encryptionParams.length === 0) {
// create a clear channel
transaction = await TransactionsFactory.createClearTransaction(transactionData);
} else {
// create an encrypted channel
transaction = await TransactionsFactory.createEncryptedTransactionInNewChannel(
transactionData,
encryptionParams,
);
encryptionMethod = transaction.encryptionMethod;
}
// Add the transaction to an existing channel
): 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');
}
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));
}
// format encryption method property
const encryptionMethod = `${EncryptionTypes.METHOD.ECIES}-${EncryptionTypes.METHOD.AES256_CBC}`;
// Generate a key for the AES encryption
const symmetricKey: string = await Utils.crypto.generate32BufferKey();
// Encrypt the data with the key and the AES256-CBC algorithm
const encryptedData: EncryptionTypes.IEncryptedData = await Utils.encryption.encrypt(data, {
key: symmetricKey,
method: EncryptionTypes.METHOD.AES256_CBC,
});
// 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');
}
// Check that all the encryption parameters given are ECIES (the only encryption method supported for now)
if (
!encryptionParams.every(
(encryptionParam: EncryptionTypes.IEncryptionParameters) =>
encryptionParam.method === EncryptionTypes.METHOD.ECIES,
)
) {
throw new Error(`encryptionParams method must be all: ${EncryptionTypes.METHOD.ECIES}`);
}
// Compute key encryption and identity hash for every encryption parameters given
const encryptedKeyAndIdentityHashesPromises = encryptionParams.map(
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;
}
public async append(content: string): Promise {
if (!content) {
throw Error('Error: no content provided');
}
const hash = Utils.crypto.normalizeKeccak256Hash(content);
const nowTimestampInSec = Utils.getCurrentTimestampInSecond();
this.data[hash] = { content, timestamp: nowTimestampInSec };
return {
meta: {
storageType: StorageTypes.StorageSystemType.IN_MEMORY_MOCK,
timestamp: nowTimestampInSec,
},
result: {
dataId: hash,
},
};
}
public async append(content: string): Promise {
if (!content) {
throw Error('Error: no content provided');
}
const hash = Utils.crypto.normalizeKeccak256Hash(content);
const timestamp = Utils.getCurrentTimestampInSecond();
this.data[hash] = { content, timestamp };
return {
meta: {
storageType: StorageTypes.StorageSystemType.IN_MEMORY_MOCK,
timestamp,
},
result: {
dataId: hash,
},
};
}
public async sign(
data: any,
signer: IdentityTypes.IIdentity,
): Promise {
if (!this.supportedIdentityTypes.includes(signer.type)) {
throw Error(`Identity type not supported ${signer.type}`);
}
const normalizedData = Utils.crypto.normalize(data);
const signatureValue = await this.eth.personal.sign(normalizedData, signer.value);
return {
data,
signature: {
method: SignatureTypes.METHOD.ECDSA_ETHEREUM,
value: signatureValue,
},
};
}
}