Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const createSignatureObject = (txBuffer, account) => ({
// publicKey: account.publicKey,
signature: signData(
hash(Buffer.concat([hexToBuffer(networkIdentifier), txBuffer])),
account.passphrase,
),
});
public sign(passphrase: string): void {
this._signature = undefined;
this._signSignature = undefined;
const networkIdentifierBytes = hexToBuffer(this._networkIdentifier);
const transactionWithNetworkIdentifierBytes = Buffer.concat([
networkIdentifierBytes,
this.getBytes(),
]);
this._signature = signData(
hash(transactionWithNetworkIdentifierBytes),
passphrase,
);
this._id = getId(this.getBytes());
}
}
export const validateNetworkIdentifier = (networkIdentifier: string) => {
if (!networkIdentifier) {
throw new Error(`Network identifier can not be empty.`);
}
const networkIdentifierBuffer = cryptography.hexToBuffer(networkIdentifier);
if (networkIdentifierBuffer.length !== NETWORK_IDENTIFIER_LENGTH) {
throw new Error(`Invalid network identifier length: ${networkIdentifier}`);
}
return true;
};
const serializeBasicProperties = tx => {
const transactionTimestamp = Buffer.alloc(4);
transactionTimestamp.writeIntBE(tx.timestamp, 0, 4);
return Buffer.concat([
Buffer.alloc(1, tx.type),
transactionTimestamp,
hexToBuffer(tx.senderPublicKey),
intToBuffer(tx.asset.numberOfSignatures, 1),
assetToBytes(tx),
]);
};
const totalAmountBuffer = new BigNum(this.totalAmount).toBuffer({
endian: 'little',
size: SIZE_INT64,
});
const totalFeeBuffer = new BigNum(this.totalFee).toBuffer({
endian: 'little',
size: SIZE_INT64,
});
const rewardBuffer = new BigNum(this.reward).toBuffer({
endian: 'little',
size: SIZE_INT64,
});
const payloadLengthBuffer = Buffer.alloc(SIZE_INT32);
payloadLengthBuffer.writeInt32LE(this.payloadLength, 0);
const payloadHashBuffer = hexToBuffer(this.payloadHash);
const generatorPublicKeyBuffer = hexToBuffer(this.generatorPublicKey);
return Buffer.concat([
versionBuffer,
timestampBuffer,
prevBlockBuffer,
numTxBuffer,
totalAmountBuffer,
totalFeeBuffer,
rewardBuffer,
payloadLengthBuffer,
payloadHashBuffer,
generatorPublicKeyBuffer,
]);
}
}
LITTLE_ENDIAN,
);
const rewardBuffer = intToBuffer(
block.reward.toString(),
SIZE_INT64,
LITTLE_ENDIAN,
);
const payloadLengthBuffer = intToBuffer(
block.payloadLength,
SIZE_INT32,
LITTLE_ENDIAN,
);
const payloadHashBuffer = hexToBuffer(block.payloadHash);
const generatorPublicKeyBuffer = hexToBuffer(block.generatorPublicKey);
const blockSignatureBuffer = block.blockSignature
? hexToBuffer(block.blockSignature)
: Buffer.alloc(0);
return Buffer.concat([
blockVersionBuffer,
timestampBuffer,
previousBlockBuffer,
numTransactionsBuffer,
totalAmountBuffer,
totalFeeBuffer,
rewardBuffer,
payloadLengthBuffer,
if (amountBigNum.gt(new BigNum(MAX_TRANSACTION_AMOUNT))) {
throw new Error('Transaction amount is too large.');
}
const transactionAmount = amountBigNum.toBuffer({
endian: 'little',
size: BYTESIZES.AMOUNT,
});
const transactionAssetData = getAssetBytes(transaction);
const transactionSignature = signature
? cryptography.hexToBuffer(signature)
: Buffer.alloc(0);
const transactionSecondSignature = signSignature
? cryptography.hexToBuffer(signSignature)
: Buffer.alloc(0);
return Buffer.concat([
transactionType,
transactionTimestamp,
transactionSenderPublicKey,
transactionRecipientID,
transactionAmount,
transactionAssetData,
transactionSignature,
transactionSecondSignature,
]);
};
tx.signatures.forEach(aSignature => {
const signatureBuffer = Buffer.concat([
Buffer.alloc(1, aSignature.index),
hexToBuffer(aSignature.signature),
]);
txBufferCopy = Buffer.concat([txBufferCopy, signatureBuffer]);
});
return txBufferCopy;
LITTLE_ENDIAN,
);
const rewardBuffer = intToBuffer(
block.reward.toString(),
SIZE_INT64,
LITTLE_ENDIAN,
);
const payloadLengthBuffer = intToBuffer(
block.payloadLength,
SIZE_INT32,
LITTLE_ENDIAN,
);
const payloadHashBuffer = hexToBuffer(block.payloadHash);
const generatorPublicKeyBuffer = hexToBuffer(block.generatorPublicKey);
const blockSignatureBuffer = block.blockSignature
? hexToBuffer(block.blockSignature)
: Buffer.alloc(0);
return Buffer.concat([
blockVersionBuffer,
timestampBuffer,
previousBlockBuffer,
heightBuffer,
maxHeightPreviouslyForgedBuffer,
maxHeightPrevotedBuffer,
numTransactionsBuffer,
totalAmountBuffer,
}
}
byteBuffer.writeInt(block.numberOfTransactions);
byteBuffer.writeLong(block.totalAmount.toString());
byteBuffer.writeLong(block.totalFee.toString());
byteBuffer.writeLong(block.reward.toString());
byteBuffer.writeInt(block.payloadLength);
const payloadHashBuffer = hexToBuffer(block.payloadHash);
for (let i = 0; i < payloadHashBuffer.length; i++) {
byteBuffer.writeByte(payloadHashBuffer[i]);
}
const generatorPublicKeyBuffer = hexToBuffer(block.generatorPublicKey);
for (let i = 0; i < generatorPublicKeyBuffer.length; i++) {
byteBuffer.writeByte(generatorPublicKeyBuffer[i]);
}
if (block.blockSignature) {
const blockSignatureBuffer = hexToBuffer(block.blockSignature);
for (let i = 0; i < blockSignatureBuffer.length; i++) {
byteBuffer.writeByte(blockSignatureBuffer[i]);
}
}
byteBuffer.flip();
return byteBuffer.toBuffer();
};