Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
// Add the transaction to an existing channel
} else {
const resultGetTx = await this.dataAccess.getTransactionsByChannelId(channelId);
const { channelKey, channelType } = await this.channelParser.getChannelTypeAndChannelKey(
channelId,
resultGetTx.result.transactions,
);
if (channelType === TransactionTypes.ChannelType.UNKNOWN) {
throw new Error(`Impossible to retrieve the channel: ${channelId}`);
}
if (channelType === TransactionTypes.ChannelType.CLEAR) {
// add the transaction to a clear channel
transaction = await TransactionsFactory.createClearTransaction(transactionData);
}
if (channelType === TransactionTypes.ChannelType.ENCRYPTED) {
// we cannot add new stakeholders to an existing channel
if (encryptionParams.length !== 0) {
throw new Error('Impossible to add new stakeholder to an existing channel');
}
if (!channelKey) {
throw new Error(`Impossible to decrypt the channel key of: ${channelId}`);
}
transaction = await TransactionsFactory.createEncryptedTransaction(
transactionData,
if (error !== '') {
// Error in the transaction, we just ignore it
return result;
}
// check if the data hash matches the channel id
const hash = await transaction.getHash();
if (hash !== channelId) {
// we just ignored it
return result;
}
// We can deduce the type of the channel
result.channelType = !!parsedData.channelKey
? TransactionTypes.ChannelType.ENCRYPTED
: TransactionTypes.ChannelType.CLEAR;
// we keep the channelKey for this channel
result.channelKey = parsedData.channelKey;
// we keep the encryption method for this channel
result.encryptionMethod = parsedData.encryptionMethod;
return result;
},
Promise.resolve({
throw new Error('Clear transactions are not allowed in encrypted channel');
}
if (
persistedTransaction.encryptedData ||
persistedTransaction.encryptionMethod ||
persistedTransaction.hash ||
persistedTransaction.keys
) {
throw new Error('only the property "data" is allowed for clear transaction');
}
return { transaction: new ClearTransaction(persistedTransaction.data) };
}
// looks like an encrypted transaction
if (persistedTransaction.encryptedData) {
if (channelType === TransactionTypes.ChannelType.CLEAR) {
throw new Error('Encrypted transactions are not allowed in clear channel');
}
if (!persistedTransaction.hash) {
throw new Error('the property "hash" is missing for the encrypted transaction');
}
// if we don't have the channel key we need to decrypt it
if (!channelKey) {
if (!persistedTransaction.encryptionMethod || !persistedTransaction.keys) {
throw new Error(
'the properties "encryptionMethod" and "keys" are needed to compute the channel key',
);
}
channelKey = await this.decryptChannelKey(
persistedTransaction.keys,
persistedTransaction.encryptionMethod,
transactionData,
encryptionParams,
);
encryptionMethod = transaction.encryptionMethod;
}
// Add the transaction to an existing channel
} else {
const resultGetTx = await this.dataAccess.getTransactionsByChannelId(channelId);
const { channelKey, channelType } = await this.channelParser.getChannelTypeAndChannelKey(
channelId,
resultGetTx.result.transactions,
);
if (channelType === TransactionTypes.ChannelType.UNKNOWN) {
throw new Error(`Impossible to retrieve the channel: ${channelId}`);
}
if (channelType === TransactionTypes.ChannelType.CLEAR) {
// add the transaction to a clear channel
transaction = await TransactionsFactory.createClearTransaction(transactionData);
}
if (channelType === TransactionTypes.ChannelType.ENCRYPTED) {
// we cannot add new stakeholders to an existing channel
if (encryptionParams.length !== 0) {
throw new Error('Impossible to add new stakeholder to an existing channel');
}
if (!channelKey) {
throw new Error(`Impossible to decrypt the channel key of: ${channelId}`);
// We can deduce the type of the channel
result.channelType = !!parsedData.channelKey
? TransactionTypes.ChannelType.ENCRYPTED
: TransactionTypes.ChannelType.CLEAR;
// we keep the channelKey for this channel
result.channelKey = parsedData.channelKey;
// we keep the encryption method for this channel
result.encryptionMethod = parsedData.encryptionMethod;
return result;
},
Promise.resolve({
channelKey: undefined,
channelType: TransactionTypes.ChannelType.UNKNOWN,
encryptionMethod: undefined,
}),
);
return channelTypeAndKey;
}
}