getL2TxId function
Generates the Transaction Id based on the spec
TxID (33 bytes) for L2Tx is:
bytes: | 1 byte | 32 bytes |
SHA256( 6 bytes | 4 bytes | 2 bytes| 5 bytes | 1 byte )
content: | type | SHA256(FromIdx | TokenID | Amount | Nonce | Fee
)
where type for L2Tx is '2'
@param {Number} fromIdx - The account index that sends the transaction
@param {Number} tokenId - The tokenId being transacted
@param {Number} amount - The amount being transacted
@param {Number} nonce - Nonce of the transaction
@param {Number} fee - The fee of the transaction
@returns {String} Transaction Id
Implementation
String getL2TxId(int fromIdx, int tokenId, double amount, int nonce, int fee) {
final fromIdxBytes = Uint8List(8);
final fromIdxView = ByteData.view(fromIdxBytes.buffer);
fromIdxView.setUint64(0, fromIdx);
final tokenIdBytes = Uint8List(8);
final tokenIdView = ByteData.view(tokenIdBytes.buffer);
tokenIdView.setUint64(0, tokenId);
final amountF40 = HermezCompressedAmount.compressAmount(amount).value;
final amountBytes = Uint8List(8);
final amountView = ByteData.view(amountBytes.buffer);
amountView.setUint64(0, BigInt.from(amountF40).toInt());
final nonceBytes = Uint8List(8);
final nonceView = ByteData.view(nonceBytes.buffer);
nonceView.setUint64(0, nonce);
final fromIdxHex = bytesToHex(fromIdxView.buffer.asUint8List().sublist(2, 8));
final tokenIdHex = bytesToHex(tokenIdView.buffer.asUint8List().sublist(4, 8));
final amountHex = bytesToHex(
amountView.buffer.asUint8List().sublist(3, 8)); // float40: 5 bytes
final nonceHex = bytesToHex(nonceView.buffer.asUint8List().sublist(3, 8));
String feeHex = fee.toRadixString(16);
if (feeHex.length == 1) {
feeHex = '0' + feeHex;
}
final v = fromIdxHex + tokenIdHex + amountHex + nonceHex + feeHex;
final h = bytesToHex(keccak256(hexToBytes(v)));
return '0x02' + h;
}