From 5eeedd8608545da8b6c58cce742da53b95b90944 Mon Sep 17 00:00:00 2001 From: danielailie Date: Wed, 29 Jan 2025 11:46:33 +0200 Subject: [PATCH 1/2] Remove testTransaction and TestMessage and update tests --- src/testutils/message.ts | 21 ------ src/testutils/transaction.ts | 43 ------------- src/wallet/users.spec.ts | 120 +++++++++++++++++------------------ 3 files changed, 60 insertions(+), 124 deletions(-) delete mode 100644 src/testutils/message.ts delete mode 100644 src/testutils/transaction.ts diff --git a/src/testutils/message.ts b/src/testutils/message.ts deleted file mode 100644 index c115d20dc..000000000 --- a/src/testutils/message.ts +++ /dev/null @@ -1,21 +0,0 @@ -/** - * A dummy message used in tests. - */ -export class TestMessage { - foo: string = ""; - bar: string = ""; - - constructor(init?: Partial) { - Object.assign(this, init); - } - - serializeForSigning(): Buffer { - let plainObject = { - foo: this.foo, - bar: this.bar - }; - - let serialized = JSON.stringify(plainObject); - return Buffer.from(serialized); - } -} diff --git a/src/testutils/transaction.ts b/src/testutils/transaction.ts deleted file mode 100644 index 318b9a8b6..000000000 --- a/src/testutils/transaction.ts +++ /dev/null @@ -1,43 +0,0 @@ -/** - * A dummy transaction used in tests. - */ -export class TestTransaction { - nonce: number = 0; - value: string = ""; - receiver: string = ""; - sender: string = ""; - guardian: string = ""; - gasPrice: number = 0; - gasLimit: number = 0; - data: string = ""; - chainID: string = ""; - version: number = 1; - options: number = 0; - - constructor(init?: Partial) { - Object.assign(this, init); - } - - serializeForSigning(): Buffer { - const dataEncoded = this.data ? Buffer.from(this.data).toString("base64") : undefined; - const guardian = this.guardian ? this.guardian : undefined; - const options = this.options ? this.options : undefined; - - const plainObject = { - nonce: this.nonce, - value: this.value, - receiver: this.receiver, - sender: this.sender, - guardian: guardian, - gasPrice: this.gasPrice, - gasLimit: this.gasLimit, - data: dataEncoded, - chainID: this.chainID, - options: options, - version: this.version - }; - - const serialized = JSON.stringify(plainObject); - return Buffer.from(serialized); - } -} diff --git a/src/wallet/users.spec.ts b/src/wallet/users.spec.ts index 0cc2408f0..2e329f905 100644 --- a/src/wallet/users.spec.ts +++ b/src/wallet/users.spec.ts @@ -1,8 +1,7 @@ import { assert } from "chai"; import path from "path"; +import { Address, Message, Transaction } from ".."; import { ErrBadMnemonicEntropy, ErrInvariantFailed } from "../errors"; -import { TestMessage } from "./../testutils/message"; -import { TestTransaction } from "./../testutils/transaction"; import { DummyMnemonicOf12Words, loadMnemonic, @@ -302,13 +301,14 @@ describe("test user wallets", () => { ); // With data field - let transaction = new TestTransaction({ - nonce: 0, - value: "0", - receiver: "erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r", - gasPrice: 1000000000, - gasLimit: 50000, - data: "foo", + let transaction = new Transaction({ + nonce: 0n, + value: 0n, + sender: Address.newFromBech32("erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz"), + receiver: Address.newFromBech32("erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r"), + gasPrice: 1000000000n, + gasLimit: 50000n, + data: new TextEncoder().encode("foo"), chainID: "1", }); @@ -316,23 +316,24 @@ describe("test user wallets", () => { let signature = await signer.sign(serialized); assert.deepEqual(await signer.sign(serialized), await signer.sign(Uint8Array.from(serialized))); - assert.equal( + assert.deepEqual( serialized.toString(), - `{"nonce":0,"value":"0","receiver":"erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r","sender":"","gasPrice":1000000000,"gasLimit":50000,"data":"Zm9v","chainID":"1","version":1}`, + `{"nonce":0,"value":"0","receiver":"erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r","sender":"erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz","gasPrice":1000000000,"gasLimit":50000,"data":"Zm9v","chainID":"1","version":2}`, ); assert.equal( Buffer.from(signature).toString("hex"), - "a3b61a2fe461f3393c42e6cb0477a6b52ffd92168f10c111f6aa8d0a310ee0c314fae0670f8313f1ad992933ac637c61a8ff20cc20b6a8b2260a4af1a120a70d", + "a5db62c6186612d44094f83576aa6a664299315fb6e42d0c17a40e9cd33efa9a9df8b76943aeac7dceaff3d78a16a7414c914f03f7a88e786c2cf939eb111c06", ); assert.isTrue(verifier.verify(serialized, signature)); // Without data field - transaction = new TestTransaction({ - nonce: 8, - value: "10000000000000000000", - receiver: "erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r", - gasPrice: 1000000000, - gasLimit: 50000, + transaction = new Transaction({ + nonce: 8n, + value: 10000000000000000000n, + sender: Address.newFromBech32("erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz"), + receiver: Address.newFromBech32("erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r"), + gasPrice: 1000000000n, + gasLimit: 50000n, chainID: "1", }); @@ -342,11 +343,11 @@ describe("test user wallets", () => { assert.deepEqual(await signer.sign(serialized), await signer.sign(Uint8Array.from(serialized))); assert.equal( serialized.toString(), - `{"nonce":8,"value":"10000000000000000000","receiver":"erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r","sender":"","gasPrice":1000000000,"gasLimit":50000,"chainID":"1","version":1}`, + `{"nonce":8,"value":"10000000000000000000","receiver":"erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r","sender":"erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz","gasPrice":1000000000,"gasLimit":50000,"chainID":"1","version":2}`, ); assert.equal( Buffer.from(signature).toString("hex"), - "f136c901d37349a7da8cfe3ab5ec8ef333b0bc351517c0e9bef9eb9704aed3077bf222769cade5ff29dffe5f42e4f0c5e0b068bdba90cd2cb41da51fd45d5a03", + "024f007f7eae87141b34708e33afd66c85a49ea8c8422e55292832ee870f879cdc033d2511c174d0f2ed62799b9f597c4a8399309578a258f558131d74374f0d", ); }); @@ -363,16 +364,16 @@ describe("test user wallets", () => { let guardianSigner = new UserSigner(UserSecretKey.fromPem(bob.pemFileText)); // With data field - let transaction = new TestTransaction({ - nonce: 0, - value: "0", - receiver: "erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r", - sender: "erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz", - gasPrice: 1000000000, - gasLimit: 50000, - data: "foo", + let transaction = new Transaction({ + nonce: 0n, + value: 0n, + receiver: Address.newFromBech32("erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r"), + sender: Address.newFromBech32("erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz"), + gasPrice: 1000000000n, + gasLimit: 50000n, + data: new TextEncoder().encode("foo"), chainID: "1", - guardian: "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx", + guardian: Address.newFromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"), options: 2, version: 2, }); @@ -381,30 +382,30 @@ describe("test user wallets", () => { let signature = await signer.sign(serialized); let guardianSignature = await guardianSigner.sign(serialized); - assert.equal( + assert.deepEqual( serialized.toString(), - `{"nonce":0,"value":"0","receiver":"erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r","sender":"erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz","guardian":"erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx","gasPrice":1000000000,"gasLimit":50000,"data":"Zm9v","chainID":"1","options":2,"version":2}`, + `{"nonce":0,"value":"0","receiver":"erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r","sender":"erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz","gasPrice":1000000000,"gasLimit":50000,"data":"Zm9v","chainID":"1","version":2,"options":2,"guardian":"erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"}`, ); assert.equal( Buffer.from(signature).toString("hex"), - "00b867ae749616954711ef227c0a3f5c6556246f26dbde12ad929a099094065341a0fae7c5ced98e6bdd100ce922c975667444ea859dce9597b46e63cade2a03", + "fa067dc9508ec9df04896665fc9c9e3e7e9cbdc6577c10d56128e3c891ea502572be637bd7cdfb466779cee3e208a2be1f32b0267af1710a6532848e5e5e6f0d", ); assert.equal( Buffer.from(guardianSignature).toString("hex"), - "1326e44941ef7bfbad3edf346e72abe23704ee32b4b6a6a6a9b793bd7c62b6d4a69d3c6ea2dddf7eabc8df8fe291cd24822409ab9194b6a0f3bbbf1c59b0a10f", + "5695fde5d9c77a94bb320438fbebe3bbd60b7cc4d633fb38e42bb65f83d253cbb82cc5ae40d701a7f0b839a5231320ca356018ced949885baae473e469ec770e", ); assert.isTrue(verifier.verify(serialized, signature)); // Without data field - transaction = new TestTransaction({ - nonce: 8, - value: "10000000000000000000", - receiver: "erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r", - sender: "erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz", - gasPrice: 1000000000, - gasLimit: 50000, + transaction = new Transaction({ + nonce: 8n, + value: 10000000000000000000n, + receiver: Address.newFromBech32("erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r"), + sender: Address.newFromBech32("erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz"), + gasPrice: 1000000000n, + gasLimit: 50000n, chainID: "1", - guardian: "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx", + guardian: Address.newFromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"), options: 2, version: 2, }); @@ -415,15 +416,15 @@ describe("test user wallets", () => { assert.equal( serialized.toString(), - `{"nonce":8,"value":"10000000000000000000","receiver":"erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r","sender":"erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz","guardian":"erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx","gasPrice":1000000000,"gasLimit":50000,"chainID":"1","options":2,"version":2}`, + `{"nonce":8,"value":"10000000000000000000","receiver":"erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r","sender":"erd1l453hd0gt5gzdp7czpuall8ggt2dcv5zwmfdf3sd3lguxseux2fsmsgldz","gasPrice":1000000000,"gasLimit":50000,"chainID":"1","version":2,"options":2,"guardian":"erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"}`, ); assert.equal( Buffer.from(signature).toString("hex"), - "49a63fa0e3cfb81a2b6d926c741328fb270ea4f58fa32585fe8aa3cde191245e5a13c5c059d5576f4c05fc24d2534a2124ff79c98d067ce8412c806779066b03", + "50d61a408cf032b3e70b15ecc313dbea43e35a1b33ea89aadb42b25a672d3427147bcda0d911be539629fcd3183c22b30f8ac30023abb230b13abf2cd1befd04", ); assert.equal( Buffer.from(guardianSignature).toString("hex"), - "4c25a54381bf66576d05f32659d30672b5b0bfbfb6b6aee52290d28cfbc87860637f095f83663a1893d12d0d5a27b2ab3325829ff1f1215b81a7ced8ee5d7203", + "ea3b83adcc468b0c7d3613fca5f429a9764d5710137c34c27e15d06e625326724ccfa758968507acadb14345d19389ba6004a4f0a6c527799c01713e10cf650b", ); assert.isTrue(verifier.verify(serialized, signature)); }); @@ -431,13 +432,14 @@ describe("test user wallets", () => { it("should sign transactions using PEM files", async () => { const signer = UserSigner.fromPem(alice.pemFileText); - const transaction = new TestTransaction({ - nonce: 0, - value: "0", - receiver: "erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r", - gasPrice: 1000000000, - gasLimit: 50000, - data: "foo", + const transaction = new Transaction({ + nonce: 0n, + value: 0n, + sender: signer.getAddress(), + receiver: Address.newFromBech32("erd1cux02zersde0l7hhklzhywcxk4u9n4py5tdxyx7vrvhnza2r4gmq4vw35r"), + gasPrice: 1000000000n, + gasLimit: 50000n, + data: new TextEncoder().encode("foo"), chainID: "1", }); @@ -447,7 +449,7 @@ describe("test user wallets", () => { assert.deepEqual(await signer.sign(serialized), await signer.sign(Uint8Array.from(serialized))); assert.equal( Buffer.from(signature).toString("hex"), - "ba4fa95fea1402e4876abf1d5a510615aab374ee48bb76f5230798a7d3f2fcae6ba91ba56c6d62e6e7003ce531ff02f219cb7218dd00dd2ca650ba747f19640a", + "b6feb8b50711cc8436040de561355e94585b2cf9e33e9e887125ad9c6877829dbc75afaf878c690e249455b738e89f63067930bc8c46fcf0779ac0bd3590a206", ); }); @@ -461,17 +463,15 @@ describe("test user wallets", () => { ).generatePublicKey(), ); - const message = new TestMessage({ - foo: "hello", - bar: "world", + const message = new Message({ + data: new TextEncoder().encode(JSON.stringify({ foo: "hello", bar: "world" })), }); - const data = message.serializeForSigning(); - const signature = await signer.sign(data); + const signature = await signer.sign(message.data); - assert.deepEqual(await signer.sign(data), await signer.sign(Uint8Array.from(data))); - assert.isTrue(verifier.verify(data, signature)); - assert.isTrue(verifier.verify(Uint8Array.from(data), Uint8Array.from(signature))); + assert.deepEqual(await signer.sign(message.data), await signer.sign(Uint8Array.from(message.data))); + assert.isTrue(verifier.verify(message.data, signature)); + assert.isTrue(verifier.verify(Uint8Array.from(message.data), Uint8Array.from(signature))); assert.isFalse(verifier.verify(Buffer.from("hello"), signature)); assert.isFalse(verifier.verify(new TextEncoder().encode("hello"), signature)); }); From efc0fd9dd0be059a845bcf68f24f48e4f9409c02 Mon Sep 17 00:00:00 2001 From: danielailie Date: Wed, 29 Jan 2025 12:25:02 +0200 Subject: [PATCH 2/2] Remove dummy query --- src/testutils/dummyQuery.ts | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 src/testutils/dummyQuery.ts diff --git a/src/testutils/dummyQuery.ts b/src/testutils/dummyQuery.ts deleted file mode 100644 index 5be41fe69..000000000 --- a/src/testutils/dummyQuery.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Address } from "../address"; -import { IContractQuery } from "../networkProviders/interface"; - -export class MockQuery implements IContractQuery { - caller = Address.empty(); - address = Address.empty(); - func: string = ""; - args: string[] = []; - value: string = ""; - - constructor(init?: Partial) { - Object.assign(this, init); - } - - getEncodedArguments(): string[] { - return this.args; - } -}