diff --git a/changelog/js-sdk.md b/changelog/js-sdk.md index 6963576cc5..eb1745cea1 100644 --- a/changelog/js-sdk.md +++ b/changelog/js-sdk.md @@ -6,7 +6,7 @@ All notable changes to `zksync.js` will be documented in this file. ### Added -- `getHashOfTx` function and a test for it. +- Method for calculation of transaction hash. ### Changed diff --git a/core/tests/ts-tests/tests/main.test.ts b/core/tests/ts-tests/tests/main.test.ts index 03d2df7a25..886e6e624d 100644 --- a/core/tests/ts-tests/tests/main.test.ts +++ b/core/tests/ts-tests/tests/main.test.ts @@ -254,10 +254,6 @@ describe(`ZkSync integration tests (token: ${token}, transport: ${transport})`, await tester.testCreate2BatchFail(hilda, david, token, TX_AMOUNT); }); }); - - it('should test GetHashOfTx function', async () => { - await tester.testGetHashOfTx(alice, bob, token, TX_AMOUNT); - }); }); // wBTC is chosen because it has decimals different from ETH (8 instead of 18). diff --git a/core/tests/ts-tests/tests/misc.ts b/core/tests/ts-tests/tests/misc.ts index 0376c51391..0e930ebc1b 100644 --- a/core/tests/ts-tests/tests/misc.ts +++ b/core/tests/ts-tests/tests/misc.ts @@ -1,6 +1,6 @@ import { Tester } from './tester'; import { expect } from 'chai'; -import { Wallet, types, utils } from 'zksync'; +import { Wallet, types } from 'zksync'; import { BigNumber, ethers } from 'ethers'; import { SignedTransaction, TxEthSignature } from 'zksync/build/types'; import { submitSignedTransactionsBatch } from 'zksync/build/wallet'; @@ -14,7 +14,6 @@ declare module './tester' { testMultipleBatchSigners(wallets: Wallet[], token: TokenLike, amount: BigNumber): Promise; testMultipleWalletsWrongSignature(from: Wallet, to: Wallet, token: TokenLike, amount: BigNumber): Promise; testBackwardCompatibleEthMessages(from: Wallet, to: Wallet, token: TokenLike, amount: BigNumber): Promise; - testGetHashOfTx(from: Wallet, to: Wallet, token: TokenLike, amount: BigNumber): Promise; } } @@ -237,17 +236,3 @@ Tester.prototype.testBackwardCompatibleEthMessages = async function ( await Promise.all(handles.map((handle) => handle.awaitReceipt())); this.runningFee = this.runningFee.add(totalFee); }; - -Tester.prototype.testGetHashOfTx = async function (from: Wallet, to: Wallet, token: TokenLike, amount: BigNumber) { - const signedTransfer = await from.signSyncTransfer({ - to: to.address(), - token: token, - amount, - fee: amount.div(2), - nonce: await from.getNonce() - }); - let hashFromSubmitTx = await from.provider.submitTx(signedTransfer.tx, signedTransfer.ethereumSignature); - let hashFromGetHash = utils.getHashOfTx(signedTransfer.tx); - expect(hashFromSubmitTx === hashFromGetHash, 'Hashes from submitTx and getHashOfTx functions should be equal').to.be - .true; -}; diff --git a/sdk/zksync.js/src/utils.ts b/sdk/zksync.js/src/utils.ts index 0a9ff6daed..f7f9fb7e03 100644 --- a/sdk/zksync.js/src/utils.ts +++ b/sdk/zksync.js/src/utils.ts @@ -728,7 +728,7 @@ export async function getPendingBalance( return zksyncContract.getPendingBalance(address, tokenAddress); } -export function getHashOfTx(tx: Transfer | Withdraw | ChangePubKey | ForcedExit | CloseAccount): string { +export function getTxHash(tx: Transfer | Withdraw | ChangePubKey | ForcedExit | CloseAccount): string { if (tx.type == 'Close') { throw new Error('Close operation is disabled'); } diff --git a/sdk/zksync.js/tests/util.test.ts b/sdk/zksync.js/tests/util.test.ts index cfcaba1566..e65c8e271b 100644 --- a/sdk/zksync.js/tests/util.test.ts +++ b/sdk/zksync.js/tests/util.test.ts @@ -6,8 +6,10 @@ import { closestPackableTransactionFee, isTransactionAmountPackable, isTransactionFeePackable, - TokenSet + TokenSet, + getTxHash } from '../src/utils'; +import { Transfer, ChangePubKey, Withdraw, ForcedExit } from '../src/types'; import { BigNumber } from 'ethers'; describe('Packing and unpacking', function () { @@ -66,3 +68,79 @@ describe('Token cache resolve', function () { expect(() => tokenCache.resolveTokenId('ERC20-2')).to.throw(); }); }); + +describe('Test getTxHash', function () { + it('Test Transfer', async function () { + const transfer = { + type: 'Transfer', + accountId: 123, + from: '0xdddddddddddddddddddddddddddddddddddddddd', + to: '0xeddddddddddddddddddddddddddddddddddddddd', + token: 0, + amount: 23, + fee: 88, + nonce: 123, + validFrom: 12, + validUntil: 1232321 + }; + const transferHash = getTxHash(transfer as Transfer); + expect( + 'sync-tx:9aa2460771722dfc15fc371e11d8412b63acdd0a483b888336234fc4b825b00b' === transferHash, + 'Incorrect transfer hash' + ).to.be.true; + }); + it('Test Withdraw', async function () { + const withdraw = { + type: 'Withdraw', + accountId: 1, + from: '0xddddddddddddddddddddddddddddddddddddddde', + to: '0xadddddddddddddddddddddddddddddddddddddde', + token: 12, + amount: '123', + fee: '897', + nonce: 1, + validFrom: 90809, + validUntil: 873712938 + }; + const withdrawHash = getTxHash(withdraw as Withdraw); + expect( + 'sync-tx:84365ebb70259b8f6d6d9729e660f1ea9ecb2dbeeefd449bed54ac144d80a315' === withdrawHash, + 'Incorrect withdrawal hash' + ).to.be.true; + }); + it('Test ChangePubKey', async function () { + const changePubKey = { + type: 'ChangePubKey', + accountId: 2, + account: '0xaddddddddddddddddddddddddddddddddddddd0e', + newPkHash: '0xadddddddd1234ddddddddddddddddddddddddd0e', + feeToken: 20, + fee: 98, + nonce: 32, + validFrom: 177, + validUntil: 52443 + }; + const changePubKeyHash = getTxHash(changePubKey as ChangePubKey); + expect( + 'sync-tx:486629437f43e9d9383431e2d075ba194d9e549c08b03db234ca4edaebb2200f' === changePubKeyHash, + 'Incorrect changePubKey hash' + ).to.be.true; + }); + it('Test ForcedExit', async function () { + const forcedExit = { + type: 'ForcedExit', + initiatorAccountId: 776, + target: '0xadddddddd1234ddddd777ddddddddddddddddd0e', + token: 5, + fee: 123, + nonce: 5, + validFrom: 8978, + validUntil: 57382678 + }; + const forcedExitHash = getTxHash(forcedExit as ForcedExit); + expect( + 'sync-tx:0f5cba03550d1ab984d6f478c79aeb6f6961873df7a5876c9af4502364163d03' === forcedExitHash, + 'Incorrect forcedExit hash' + ).to.be.true; + }); +});