From 1592675f8fe20dabd13319beb54b79331102aba8 Mon Sep 17 00:00:00 2001 From: Jayden Date: Fri, 20 Oct 2023 01:18:48 +0800 Subject: [PATCH 1/2] Fix a bug in P256 caused by reference type --- .changeset/violet-garlics-compete.md | 5 ++ .../soulwallet-sdk/__tests__/main.test.ts | 46 +++++++++++++++++++ packages/soulwallet-sdk/src/tools/p256lib.ts | 3 +- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 .changeset/violet-garlics-compete.md diff --git a/.changeset/violet-garlics-compete.md b/.changeset/violet-garlics-compete.md new file mode 100644 index 0000000..003da1e --- /dev/null +++ b/.changeset/violet-garlics-compete.md @@ -0,0 +1,5 @@ +--- +"@soulwallet/sdk": patch +--- + +Fix a bug in P256Lib caused by reference type diff --git a/packages/soulwallet-sdk/__tests__/main.test.ts b/packages/soulwallet-sdk/__tests__/main.test.ts index 5bf5fe4..3a0afa8 100644 --- a/packages/soulwallet-sdk/__tests__/main.test.ts +++ b/packages/soulwallet-sdk/__tests__/main.test.ts @@ -64,4 +64,50 @@ describe('SDK', () => { const expectSignature = '0x00022ae3ddfe4cc414dc0fad7ff3a5c960d1cee1211722d3099ade76e5ac1826731a87e5d654f357e4cd6cb52512b2da4d91eae0ae48e9d892ce532b9352f63a55d61c0025000049960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000000222c226f726967696e223a22687474703a2f2f6c6f63616c686f73743a35353030222c2263726f73734f726967696e223a66616c73657d'; expect(signature.OK).toBe(expectSignature); }); + + test('packUserOpP256Signature-1', async () => { + const soulwallet = new SoulWallet('https://localhost/', 'https://localhost/', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000'); + const signature = await soulwallet.packUserOpP256Signature({ + messageHash: '0xd45f9c36f42a0a149e3b77dec8597563235ff5463bf2c9af2f3e75cbd6eb6935', + publicKey: { + x: '0x6af4a0dbda88d45e4c6d0c97784671e44df2896a06b1200bf5ab9c2f54c7aca3', + y: '0xa439bdd51a1af33dbd97cc917ba103ce0694e46c4ad56d079991a0307364f956' + }, + r: '0x8da3e1aa957bbefb34926bf9ee3892e4a27ee96cd54309deb23ba0151fb255c7', + s: '0xb2587548067b70aee24b25424aa515bd5b452424e3ab0451834da7d43928ee3e', + authenticatorData: '0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000000', + clientDataSuffix: '","origin":"http://localhost:8000","crossOrigin":false}' + }, '0x653120f7000065312f070000000000000000000000000000000000000000', undefined); + expect(signature.isOk()).toBe(true); + }); + test('packUserOpP256Signature-2', async () => { + const soulwallet = new SoulWallet('https://localhost/', 'https://localhost/', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000'); + const signature = await soulwallet.packUserOpP256Signature({ + messageHash: '0xb494f6738df235dea972d9f88139ff6c1bec48a97cbf12a44e5f39562b20c34c', + publicKey: { + x: '0x6af4a0dbda88d45e4c6d0c97784671e44df2896a06b1200bf5ab9c2f54c7aca3', + y: '0xa439bdd51a1af33dbd97cc917ba103ce0694e46c4ad56d079991a0307364f956' + }, + r: '0xd2d8837118f0063d3552b02734a749e52484d397ed1b4e35f0e01668b9942bb3', + s: '0x9a31d197eb2a3f05e616639efd116daa4cdd68d7124062cc22f83852e6422d6e', + authenticatorData: '0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000000', + clientDataSuffix: '","origin":"http://localhost:8000","crossOrigin":false}' + }, '0x653120f7000065312f070000000000000000000000000000000000000000', undefined); + expect(signature.isOk()).toBe(true); + }); + test('packUserOpP256Signature-3', async () => { + const soulwallet = new SoulWallet('https://localhost/', 'https://localhost/', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000000'); + const signature = await soulwallet.packUserOpP256Signature({ + messageHash: '0x371406a3e3929737d36f8dfe36befd83f090d4975a520320a6c471dd61b9810b', + publicKey: { + x: '0x6af4a0dbda88d45e4c6d0c97784671e44df2896a06b1200bf5ab9c2f54c7aca3', + y: '0xa439bdd51a1af33dbd97cc917ba103ce0694e46c4ad56d079991a0307364f956' + }, + r: '0x0d9263333c04157e474092d1700414e2fe5fe118948dddd17fd1f7c8f0f648f3', + s: '0x774875e034be9cb380630b9311d85ff4837d424a32dd6800772ab022cbaae627', + authenticatorData: '0x49960de5880e8c687434170f6476605b8fe4aeb9a28632c7995cf3ba831d97630500000000', + clientDataSuffix: '","origin":"http://localhost:8000","crossOrigin":false}' + }, '0x653120f7000065312f070000000000000000000000000000000000000000', undefined); + expect(signature.isOk()).toBe(true); + }); }); \ No newline at end of file diff --git a/packages/soulwallet-sdk/src/tools/p256lib.ts b/packages/soulwallet-sdk/src/tools/p256lib.ts index 0013d15..0995955 100644 --- a/packages/soulwallet-sdk/src/tools/p256lib.ts +++ b/packages/soulwallet-sdk/src/tools/p256lib.ts @@ -197,7 +197,8 @@ export class P256Lib { _point.x = point.x; _point.y = point.y; } else if (T4 == BigInt(3)) { - _point = H; + _point.x = H.x; + _point.y = H.y; } } index--; From 16fbc616ecdf3a2ef0cb8b0ac26351ffe811a0e2 Mon Sep 17 00:00:00 2001 From: Jayden Date: Fri, 20 Oct 2023 01:21:28 +0800 Subject: [PATCH 2/2] Fixed the bug in P256Lib caused by reference type --- packages/soulwallet-sdk/src/tools/p256lib.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/soulwallet-sdk/src/tools/p256lib.ts b/packages/soulwallet-sdk/src/tools/p256lib.ts index 0995955..0791efb 100644 --- a/packages/soulwallet-sdk/src/tools/p256lib.ts +++ b/packages/soulwallet-sdk/src/tools/p256lib.ts @@ -182,7 +182,7 @@ export class P256Lib { } const H = P256Lib.ecAff_add(new Point(gx, gy), point); //will not work if Q=P, obvious forbidden private key let index: bigint = BigInt(255); - let _point: Point = new Point(BigInt(0), BigInt(0)); + const _point: Point = new Point(BigInt(0), BigInt(0)); { let T4 = (((scalar_v >> index) & BigInt(1)) << BigInt(1)) + ((scalar_u >> index) & BigInt(1)); while (T4 === BigInt(0)) {