Skip to content

Commit

Permalink
Merge pull request #134 from lucas7788/main_fix_bip32
Browse files Browse the repository at this point in the history
fix bip32 publicKey check bug
  • Loading branch information
luyishisi authored Oct 22, 2024
2 parents 773f313 + e44feb1 commit e1ddb6d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/crypto-lib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/crypto-lib/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 4 additions & 0 deletions packages/crypto-lib/src/bip32/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } })
}

Expand Down
4 changes: 4 additions & 0 deletions packages/crypto-lib/src/signutil/secp256k1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}

Expand Down
11 changes: 11 additions & 0 deletions packages/crypto-lib/tests/crypto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit e1ddb6d

Please sign in to comment.