diff --git a/packages/crypto-lib/CHANGELOG.md b/packages/crypto-lib/CHANGELOG.md index c5eec4a..0594e8e 100644 --- a/packages/crypto-lib/CHANGELOG.md +++ b/packages/crypto-lib/CHANGELOG.md @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file. +# [1.0.6](https://github.com/okx/js-wallet-sdk) (2024-10-22) + +### Bug Fixes + +- **crypto-lib:** fix bip32 publicKey check + # [1.0.4](https://github.com/okx/js-wallet-sdk) (2024-08-20) ### New Feature diff --git a/packages/crypto-lib/package.json b/packages/crypto-lib/package.json index b7d8939..90ec517 100644 --- a/packages/crypto-lib/package.json +++ b/packages/crypto-lib/package.json @@ -1,6 +1,6 @@ { "name": "@okxweb3/crypto-lib", - "version": "1.0.5", + "version": "1.0.6", "description": "", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/packages/crypto-lib/src/bip32/api.ts b/packages/crypto-lib/src/bip32/api.ts index 0feb37a..a9b63dd 100644 --- a/packages/crypto-lib/src/bip32/api.ts +++ b/packages/crypto-lib/src/bip32/api.ts @@ -21,6 +21,10 @@ function loadCompressedPublicKey (first: number, xbuf: Uint8Array) { let y = x.redSqr().redIMul(x).redIAdd(curve.b).redSqrt() if ((first === 0x03) !== y.isOdd()) y = y.redNeg() + // x*x*x + b = y*y + const x3 = x.redSqr().redIMul(x) + if (!y.redSqr().redISub(x3.redIAdd(curve.b)).isZero()) return null + return secp256k1.keyPair({ pub: { x: x, y: y } }) } diff --git a/packages/crypto-lib/src/signutil/secp256k1.ts b/packages/crypto-lib/src/signutil/secp256k1.ts index cd30d5c..559ffdf 100644 --- a/packages/crypto-lib/src/signutil/secp256k1.ts +++ b/packages/crypto-lib/src/signutil/secp256k1.ts @@ -118,6 +118,10 @@ export function loadCompressedPublicKey (first: number, xbuf: Buffer | Uint8Arra let y = xx.redSqr().redIMul(xx).redIAdd(ec.curve.b).redSqrt() if ((first === 0x03) !== y.isOdd()) y = y.redNeg() + // x*x*x + b = y*y + const x3 = xx.redSqr().redIMul(xx) + if (!y.redSqr().redISub(x3.redIAdd(ec.curve.b)).isZero()) return null + return {x: xx, y: y} } diff --git a/packages/crypto-lib/tests/crypto.test.ts b/packages/crypto-lib/tests/crypto.test.ts index 20a98e6..397815b 100644 --- a/packages/crypto-lib/tests/crypto.test.ts +++ b/packages/crypto-lib/tests/crypto.test.ts @@ -2,6 +2,7 @@ import {sha256} from "@noble/hashes/sha256"; import {Buffer} from "buffer"; import {base, bip32, bip39, signUtil} from "../src"; import {randomBytes} from '../src/base'; +import {secp256k1} from "../src/signutil"; describe("crypto", () => { @@ -145,6 +146,16 @@ describe("crypto", () => { console.info(bb); }); + test("publicKeyVerify test", async ()=> { + const zeroUncompressed = Buffer.concat([Buffer.from([0x04]), Buffer.alloc(64)]) + expect(secp256k1.publicKeyVerify(zeroUncompressed)).toBe(false); + + const zeroCompressed = Buffer.concat([Buffer.from([0x02]), Buffer.alloc(32)]) + expect(secp256k1.publicKeyVerify(zeroCompressed)).toBe(false); + + // bip32.fromPublicKey(zeroCompressed,Buffer.alloc(32)) + }) + test("bip32", async () => { let node: bip32.BIP32Interface = bip32.fromSeed(base.fromHex("000102030405060708090a0b0c0d0e0f")); console.info("node1-publicKey: ", base.toHex(node.publicKey));