Repository: 0xs34n/starknet.js
Pull request #356 (2022-10-13 10:05:52)

Fix: Failing tests with declare-0.10 PR


Patch:
diff --git a/src/utils/merkle.ts b/src/utils/merkle.ts
index 2353453a..03879147 100644
--- a/src/utils/merkle.ts
+++ b/src/utils/merkle.ts
@@ -23,7 +23,7 @@ export class MerkleTree {
     const newLeaves = [];
     for (let i = 0; i < leaves.length; i += 2) {
       if (i + 1 === leaves.length) {
-        newLeaves.push(MerkleTree.hash(leaves[i], '0x0'));
+        newLeaves.push(leaves[i]);
       } else {
         newLeaves.push(MerkleTree.hash(leaves[i], leaves[i + 1]));
       }
@@ -53,7 +53,9 @@ export class MerkleTree {
         : this.branches.findIndex((b) => b.length === branch.length);
     const nextBranch = this.branches[currentBranchLevelIndex + 1] ?? [this.root];
     return this.getProof(
-      MerkleTree.hash(isLeft ? leaf : neededBranch, isLeft ? neededBranch : leaf),
+      neededBranch === '0x0'
+        ? leaf
+        : MerkleTree.hash(isLeft ? leaf : neededBranch, isLeft ? neededBranch : leaf),
       nextBranch,
       newHashPath
     );


Test patch:
diff --git a/__tests__/defaultProvider.test.ts b/__tests__/defaultProvider.test.ts
index 76fcad05..9d00fb4d 100644
--- a/__tests__/defaultProvider.test.ts
+++ b/__tests__/defaultProvider.test.ts
@@ -1,13 +1,6 @@
-import {
-  BlockNumber,
-  DeclareContractResponse,
-  DeployContractResponse,
-  GetBlockResponse,
-  stark,
-} from '../src';
+import { BlockNumber, DeployContractResponse, GetBlockResponse, stark } from '../src';
 import { toBN } from '../src/utils/number';
 import {
-  compiledErc20,
   compiledOpenZeppelinAccount,
   describeIfNotDevnet,
   getERC20DeployPayload,
@@ -62,7 +55,7 @@ describe('defaultProvider', () => {
     });
 
     test('getBlock() -> { blockNumber }', async () => {
-      const block = await testProvider.getBlock();
+      const block = await testProvider.getBlock('latest');
       return expect(block).toHaveProperty('block_number');
     });
 
@@ -142,8 +135,8 @@ describe('defaultProvider', () => {
     let latestBlock;
     describe(`Provider methods if not devnet`, () => {
       describe('getBlock', () => {
-        test('getBlock by tag pending', async () => {
-          latestBlock = await provider.getBlock();
+        test('getBlock by tag latest', async () => {
+          latestBlock = await provider.getBlock('latest');
           expect(latestBlock).toHaveProperty('block_hash');
           expect(latestBlock).toHaveProperty('parent_hash');
           expect(latestBlock).toHaveProperty('block_number');
@@ -249,16 +242,13 @@ describe('defaultProvider', () => {
       describe('Contract methods', () => {
         let contractAddress: string;
         let deployResponse: DeployContractResponse;
-        let declareResponse: DeclareContractResponse;
         let blockNumber: BlockNumber;
 
         beforeAll(async () => {
-          deployResponse = await provider.deployContract({ contract: compiledErc20 });
+          const erc20DeployPayload = getERC20DeployPayload(wallet);
+          deployResponse = await provider.deployContract(erc20DeployPayload);
           contractAddress = deployResponse.contract_address;
-          await Promise.all([
-            provider.waitForTransaction(deployResponse.transaction_hash),
-            provider.waitForTransaction(declareResponse.transaction_hash),
-          ]);
+          await provider.waitForTransaction(deployResponse.transaction_hash);
           ({ block_number: blockNumber } = await provider.getBlock('latest'));
         });
 
@@ -284,9 +274,9 @@ describe('defaultProvider', () => {
               provider
                 .callContract({
                   contractAddress: deployResponse.contract_address,
-                  entrypoint: 'balance_of',
+                  entrypoint: 'balanceOf',
                   calldata: compileCalldata({
-                    user: '0x9ff64f4ab0e1fe88df4465ade98d1ea99d5732761c39279b8e1374fa943e9b',
+                    user: wallet,
                   }),
                 })
                 .then((res) => {
diff --git a/__tests__/utils/merkle.test.ts b/__tests__/utils/merkle.test.ts
index 85a350fd..a47cc6b6 100644
--- a/__tests__/utils/merkle.test.ts
+++ b/__tests__/utils/merkle.test.ts
@@ -53,7 +53,7 @@ describe('MerkleTree class', () => {
           MerkleTree.hash(leaves[0], leaves[1]),
           MerkleTree.hash(leaves[2], leaves[3])
         ),
-        MerkleTree.hash(MerkleTree.hash(leaves[4], leaves[5]), '0x0')
+        MerkleTree.hash(leaves[4], leaves[5])
       );
 
       expect(tree.root).toBe(manualMerkle);
@@ -67,7 +67,7 @@ describe('MerkleTree class', () => {
           MerkleTree.hash(leaves[0], leaves[1]),
           MerkleTree.hash(leaves[2], leaves[3])
         ),
-        MerkleTree.hash(MerkleTree.hash(leaves[4], leaves[5]), MerkleTree.hash(leaves[6], '0x0'))
+        MerkleTree.hash(MerkleTree.hash(leaves[4], leaves[5]), leaves[6])
       );
 
       expect(tree.root).toBe(manualMerkle);
@@ -85,7 +85,7 @@ describe('MerkleTree class', () => {
       const manualProof = [
         '0x4',
         MerkleTree.hash('0x1', '0x2'),
-        MerkleTree.hash(MerkleTree.hash('0x5', '0x6'), MerkleTree.hash('0x7', '0x0')),
+        MerkleTree.hash(MerkleTree.hash('0x5', '0x6'), '0x7'),
       ];
 
       expect(proof).toEqual(manualProof);
@@ -101,17 +101,6 @@ describe('MerkleTree class', () => {
 
       expect(proof).toEqual(manualProof);
     });
-    test('should return proof path for valid child', async () => {
-      const proof = tree.getProof('0x5');
-
-      const manualProof = [
-        '0x6',
-        MerkleTree.hash('0x7', '0x0'), // tree should be padded with 0x0 so that all proofs are equals in size
-        MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4')),
-      ];
-
-      expect(proof).toEqual(manualProof);
-    });
     test('should throw for invalid child', () => {
       expect(() => tree.getProof('0x8')).toThrow('leaf not found');
     });
@@ -125,7 +114,6 @@ describe('MerkleTree class', () => {
 
     test('should return true for valid manual proof', async () => {
       const manualProof = [
-        '0x0', // tree should be padded with 0x0 so that all proofs are equals in size
         MerkleTree.hash('0x5', '0x6'),
         MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4')),
       ];
@@ -134,17 +122,6 @@ describe('MerkleTree class', () => {
 
       expect(proofMerklePath(root, leaf, manualProof)).toBe(true);
     });
-    test('should return true for valid manual proof', async () => {
-      const manualProof = [
-        '0x6',
-        MerkleTree.hash('0x7', '0x0'), // tree should be padded with 0x0 so that all proofs are equals in size
-        MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4')),
-      ];
-      const leaf = '0x5';
-      const { root } = tree;
-
-      expect(proofMerklePath(root, leaf, manualProof)).toBe(true);
-    });
     test('should return true for valid proof', async () => {
       const proof = tree.getProof('0x3');
       const leaf = '0x3';
@@ -181,76 +158,4 @@ describe('MerkleTree class', () => {
       expect(proofMerklePath(root, leaf, proof)).toBe(false);
     });
   });
-  describe('verify 2-deep tree with empty data on the right', () => {
-    let tree: MerkleTree;
-    beforeAll(() => {
-      const leaves = ['0x1', '0x2', '0x3'];
-      tree = new MerkleTree(leaves);
-    });
-    test('should return 1-length proof in a 2-length tree', async () => {
-      const proof = tree.getProof('0x3');
-      const manualProof = ['0x0', MerkleTree.hash('0x1', '0x2')];
-      expect(proof).toEqual(manualProof);
-    });
-    test('should check the previous proof works fine', async () => {
-      const manualMerkle = MerkleTree.hash(
-        MerkleTree.hash('0x3', '0x0'),
-        MerkleTree.hash('0x1', '0x2')
-      );
-      expect(tree.root).toBe(manualMerkle);
-    });
-  });
-  describe('verify 3-deep tree with empty data on the right', () => {
-    let tree: MerkleTree;
-    beforeAll(() => {
-      const leaves = ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6'];
-      tree = new MerkleTree(leaves);
-    });
-    test('should return 2-length proof with the 2nd place skipped', async () => {
-      const proof = tree.getProof('0x5');
-      const manualProof = [
-        '0x6',
-        '0x0',
-        MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4')),
-      ];
-      expect(proof).toEqual(manualProof);
-    });
-    test('should check the previous proof works fine', async () => {
-      const manualMerkle = MerkleTree.hash(
-        MerkleTree.hash(MerkleTree.hash('0x5', '0x6'), '0x0'),
-        MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4'))
-      );
-      expect(tree.root).toBe(manualMerkle);
-    });
-  });
-  describe('verify 4-deep tree with empty data on the right', () => {
-    let tree: MerkleTree;
-    beforeAll(() => {
-      const leaves = ['0x1', '0x2', '0x3', '0x4', '0x5', '0x6', '0x7', '0x8', '0x9'];
-      tree = new MerkleTree(leaves);
-    });
-    test('should return 2-length proof with the 2nd place skipped', async () => {
-      const proof = tree.getProof('0x9');
-      const manualProof = [
-        '0x0',
-        '0x0',
-        '0x0',
-        MerkleTree.hash(
-          MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4')),
-          MerkleTree.hash(MerkleTree.hash('0x5', '0x6'), MerkleTree.hash('0x7', '0x8'))
-        ),
-      ];
-      expect(proof).toEqual(manualProof);
-    });
-    test('should check the previous proof works fine', async () => {
-      const manualMerkle = MerkleTree.hash(
-        MerkleTree.hash(MerkleTree.hash(MerkleTree.hash('0x9', '0x0'), '0x0'), '0x0'),
-        MerkleTree.hash(
-          MerkleTree.hash(MerkleTree.hash('0x1', '0x2'), MerkleTree.hash('0x3', '0x4')),
-          MerkleTree.hash(MerkleTree.hash('0x5', '0x6'), MerkleTree.hash('0x7', '0x8'))
-        )
-      );
-      expect(tree.root).toBe(manualMerkle);
-    });
-  });
 });
diff --git a/__tests__/utils/typedData.test.ts b/__tests__/utils/typedData.test.ts
index ea723070..78d82b78 100644
--- a/__tests__/utils/typedData.test.ts
+++ b/__tests__/utils/typedData.test.ts
@@ -72,7 +72,7 @@ describe('typedData', () => {
     const [, merkleTreeHash] = encodeValue({}, 'merkletree', tree.leaves);
     expect(merkleTreeHash).toBe(tree.root);
     expect(merkleTreeHash).toMatchInlineSnapshot(
-      `"0x15ac9e457789ef0c56e5d559809e7336a909c14ee2511503fa7af69be1ba639"`
+      `"0x551b4adb6c35d49c686a00b9192da9332b18c9b262507cad0ece37f3b6918d2"`
     );
   });
 
@@ -119,7 +119,7 @@ describe('typedData', () => {
     );
     expect(merkleTreeHash).toBe(tree.root);
     expect(merkleTreeHash).toMatchInlineSnapshot(
-      `"0x12354b159e3799dc0ebe86d62dde4ce7b300538d471e5a7fef23dcbac076011"`
+      `"0x75c4f467f4527a5348f3e302007228a6b0057fc4c015f981ffb5b3ace475727"`
     );
   });
 
@@ -217,7 +217,7 @@ describe('typedData', () => {
       '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826'
     );
     expect(hash).toMatchInlineSnapshot(
-      `"0x751fb7d98545f7649d0d0eadc80d770fcd88d8cfaa55590b284f4e1b701ef0a"`
+      `"0x1ad0330a62a4a94eae5ea1a7ad96388179d2e4d33e6f909d17421d315110653"`
     );
   });
 });