Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
await this.db.task(task => {
const transactions = [];
for (const tx of chunk) {
const transaction = Transactions.TransactionFactory.fromBytesUnsafe(tx.serialized, tx.id);
if (transaction.data.asset) {
let transactionId = transaction.id;
// If the transaction is a broken v1 transaction use the broken id for the query.
if (transactionIdFixTable && transactionIdFixTable[transactionId]) {
transactionId = transactionIdFixTable[transactionId];
}
const query =
this.pgp.helpers.update(
{
asset: transaction.data.asset,
},
["asset"],
"transactions",
) + ` WHERE id = '${transactionId}'`;
export const transformTransaction = (model, transform) => {
const blockchain = app.resolvePlugin("blockchain");
const databaseService = app.resolvePlugin("database");
const transaction: Interfaces.ITransaction = Transactions.TransactionFactory.fromBytesUnsafe(
model.serialized,
model.id,
);
if (!transform) {
return transaction.toJson();
}
const { data } = transaction;
const sender: string = databaseService.walletManager.findByPublicKey(data.senderPublicKey).address;
const lastBlock: Interfaces.IBlock = blockchain.getLastBlock();
return {
id: data.id,
`Failed to verify signature: ${JSON.stringify(data)}`,
);
}
return signatureVerify;
}
return true;
}
if (context === "transactions") {
if (!verifySignatures) {
return true;
}
return Transactions.TransactionFactory.fromBytes(data.serialized).verified;
}
if (context === "rounds") {
return true;
}
return false;
};
transactions = block.transactions.map(transaction => {
const { data } = Transactions.TransactionFactory.fromBytesUnsafe(Buffer.from(transaction, "hex"));
data.blockId = block.id;
return data;
});
} catch {
const result: IPendingTransactionJobResult = {
ticketId: job.ticketId,
validTransactions: [],
invalid: {},
excess: {},
errors: {},
};
for (const transactionData of transactions) {
try {
if (!this.performBasicTransactionChecks(result, transactionData)) {
continue;
}
const transaction: Interfaces.ITransaction = Transactions.TransactionFactory.fromData(transactionData);
const handler: Handlers.TransactionHandler = Handlers.Registry.get(
transaction.type,
transaction.typeGroup,
);
const walletData: State.IWallet = senderWallets[transactionData.senderPublicKey];
const senderWallet: State.IWallet = Object.assign(new Wallets.Wallet(walletData.address), {
...walletData,
});
if (!(await handler.verify(transaction, senderWallet))) {
pushError(result, transactionData.id, {
type: "ERR_BAD_DATA",
message: "Failed to verify transaction signature.",
});
const transactions = dbTransactions.map(tx => {
const { data } = Transactions.TransactionFactory.fromBytesUnsafe(tx.serialized, tx.id);
data.blockId = tx.blockId;
return data;
});
const exists: boolean = this.pool.has(transaction.id);
if (exists) {
this.pushError(transaction, "ERR_DUPLICATE", `Duplicate transaction ${transaction.id}`);
} else if (JSON.stringify(transaction).length > maxTransactionBytes) {
this.pushError(
transaction,
"ERR_TOO_LARGE",
`Transaction ${transaction.id} is larger than ${maxTransactionBytes} bytes.`,
);
} else if (this.pool.hasExceededMaxTransactions(transaction.senderPublicKey)) {
this.excess.push(transaction.id);
} else if (this.validateTransaction(transaction)) {
try {
const receivedId: string = transaction.id;
const trx: Interfaces.ITransaction = Transactions.TransactionFactory.fromData(transaction);
const handler = Handlers.Registry.get(trx.type);
if (handler.verify(trx, this.pool.walletManager)) {
try {
this.walletManager.throwIfApplyingFails(trx);
const dynamicFee: IDynamicFeeMatch = dynamicFeeMatcher(trx);
if (!dynamicFee.enterPool && !dynamicFee.broadcast) {
this.pushError(
transaction,
"ERR_LOW_FEE",
"The fee is too low to broadcast and accept the transaction",
);
} else {
if (dynamicFee.enterPool) {
this.accept.set(trx.data.id, trx);
}
async method(params: { id: string }) {
const transaction: Interfaces.ITransactionData = await database.get(params.id);
if (!transaction) {
return Boom.notFound(`Transaction ${params.id} could not be found.`);
}
const { data } = Transactions.TransactionFactory.fromData(transaction);
if (!Transactions.Verifier.verifyHash(data)) {
return Boom.badData();
}
await network.sendPOST({
path: "transactions",
body: { transactions: [transaction] },
});
return transaction;
},
schema: {
(hex: string) => Transactions.TransactionFactory.fromBytesUnsafe(Buffer.from(hex, "hex")).data,
);
private async validateTransactions(transactions: Interfaces.ITransaction[]): Promise {
const validTransactions: string[] = [];
const forgedIds: string[] = await this.removeForgedTransactions(transactions);
const unforgedTransactions = transactions.filter(
(transaction: Interfaces.ITransaction) => !forgedIds.includes(transaction.id),
);
const databaseWalletManager: State.IWalletManager = this.databaseService.walletManager;
const localWalletManager: Wallets.WalletManager = new Wallets.WalletManager();
for (const transaction of unforgedTransactions) {
try {
const deserialized: Interfaces.ITransaction = Transactions.TransactionFactory.fromBytes(
transaction.serialized,
);
strictEqual(transaction.id, deserialized.id);
const { sender, recipient } = this.getSenderAndRecipient(transaction, localWalletManager);
const handler: Handlers.TransactionHandler = Handlers.Registry.get(transaction.type);
handler.canBeApplied(transaction, sender, databaseWalletManager);
handler.applyToSenderInPool(transaction, localWalletManager);
if (recipient && sender.address !== recipient.address) {
handler.applyToRecipientInPool(transaction, localWalletManager);
}