Skip to content

Commit b27faa0

Browse files
committed
Update to latest noble crypto libraries (#3975).
1 parent 9541f2f commit b27faa0

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"./lib.esm/wordlists/wordlists.js": "./lib.esm/wordlists/wordlists-browser.js"
1010
},
1111
"dependencies": {
12-
"@adraffy/ens-normalize": "1.9.2",
13-
"@noble/hashes": "1.1.2",
14-
"@noble/secp256k1": "1.7.1",
12+
"@adraffy/ens-normalize": "1.9.4",
13+
"@noble/curves": "1.2.0",
14+
"@noble/hashes": "1.3.2",
1515
"@types/node": "18.15.13",
1616
"aes-js": "4.0.0-beta.5",
1717
"tslib": "2.4.0",
@@ -93,7 +93,7 @@
9393
"url": "https://www.buymeacoffee.com/ricmoo"
9494
}
9595
],
96-
"gitHead": "7d4173049edc3b4ff2de1971c3ecca3b08588651",
96+
"gitHead": "9541f2f70cd7f5c6f3caf93f5a3d5e34eae5281a",
9797
"homepage": "https://ethers.org",
9898
"keywords": [
9999
"ethereum",
@@ -131,5 +131,5 @@
131131
"test-esm": "mocha --reporter ./reporter.cjs ./lib.esm/_tests/test-*.js"
132132
},
133133
"sideEffects": false,
134-
"version": "6.7.1"
134+
"version": "6.8.0"
135135
}

src.ts/crypto/signing-key.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,20 @@
44
* @_subsection: api/crypto:Signing [about-signing]
55
*/
66

7-
import * as secp256k1 from "@noble/secp256k1";
7+
import { secp256k1 } from "@noble/curves/secp256k1";
88

99
import {
1010
concat, dataLength, getBytes, getBytesCopy, hexlify, toBeHex,
1111
assertArgument
1212
} from "../utils/index.js";
1313

14-
import { computeHmac } from "./hmac.js";
1514
import { Signature } from "./signature.js";
1615

1716
import type { BytesLike } from "../utils/index.js";
1817

1918
import type { SignatureLike } from "./index.js";
2019

2120

22-
//const N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
23-
24-
// Make noble-secp256k1 sync
25-
secp256k1.utils.hmacSha256Sync = function(key: Uint8Array, ...messages: Array<Uint8Array>): Uint8Array {
26-
return getBytes(computeHmac("sha256", key, concat(messages)));
27-
}
28-
2921
/**
3022
* A **SigningKey** provides high-level access to the elliptic curve
3123
* cryptography (ECC) operations and key management.
@@ -69,16 +61,14 @@ export class SigningKey {
6961
sign(digest: BytesLike): Signature {
7062
assertArgument(dataLength(digest) === 32, "invalid digest length", "digest", digest);
7163

72-
const [ sigDer, recid ] = secp256k1.signSync(getBytesCopy(digest), getBytesCopy(this.#privateKey), {
73-
recovered: true,
74-
canonical: true
64+
const sig = secp256k1.sign(getBytesCopy(digest), getBytesCopy(this.#privateKey), {
65+
lowS: true
7566
});
7667

77-
const sig = secp256k1.Signature.fromHex(sigDer);
7868
return Signature.from({
79-
r: toBeHex("0x" + sig.r.toString(16), 32),
80-
s: toBeHex("0x" + sig.s.toString(16), 32),
81-
v: (recid ? 0x1c: 0x1b)
69+
r: toBeHex(sig.r, 32),
70+
s: toBeHex(sig.s, 32),
71+
v: (sig.recovery ? 0x1c: 0x1b)
8272
});
8373
}
8474

@@ -106,7 +96,7 @@ export class SigningKey {
10696
*/
10797
computeSharedSecret(other: BytesLike): string {
10898
const pubKey = SigningKey.computePublicKey(other);
109-
return hexlify(secp256k1.getSharedSecret(getBytesCopy(this.#privateKey), getBytes(pubKey)));
99+
return hexlify(secp256k1.getSharedSecret(getBytesCopy(this.#privateKey), getBytes(pubKey), false));
110100
}
111101

112102
/**
@@ -151,7 +141,7 @@ export class SigningKey {
151141
bytes = pub;
152142
}
153143

154-
const point = secp256k1.Point.fromHex(bytes);
144+
const point = secp256k1.ProjectivePoint.fromHex(bytes);
155145
return hexlify(point.toRawBytes(compressed));
156146
}
157147

@@ -177,12 +167,14 @@ export class SigningKey {
177167
assertArgument(dataLength(digest) === 32, "invalid digest length", "digest", digest);
178168

179169
const sig = Signature.from(signature);
180-
const der = secp256k1.Signature.fromCompact(getBytesCopy(concat([ sig.r, sig.s ]))).toDERRawBytes();
181170

182-
const pubKey = secp256k1.recoverPublicKey(getBytesCopy(digest), der, sig.yParity);
183-
assertArgument(pubKey != null, "invalid signature for digest", "signature", signature);
171+
let secpSig = secp256k1.Signature.fromCompact(getBytesCopy(concat([ sig.r, sig.s ])));
172+
secpSig = secpSig.addRecoveryBit(sig.yParity);
173+
174+
const pubKey = secpSig.recoverPublicKey(getBytesCopy(digest));
175+
assertArgument(pubKey != null, "invalid signautre for digest", "signature", signature);
184176

185-
return hexlify(pubKey);
177+
return "0x" + pubKey.toHex(false);
186178
}
187179

188180
/**
@@ -196,8 +188,8 @@ export class SigningKey {
196188
* addresses from parent public keys and chain codes.
197189
*/
198190
static addPoints(p0: BytesLike, p1: BytesLike, compressed?: boolean): string {
199-
const pub0 = secp256k1.Point.fromHex(SigningKey.computePublicKey(p0).substring(2));
200-
const pub1 = secp256k1.Point.fromHex(SigningKey.computePublicKey(p1).substring(2));
191+
const pub0 = secp256k1.ProjectivePoint.fromHex(SigningKey.computePublicKey(p0).substring(2));
192+
const pub1 = secp256k1.ProjectivePoint.fromHex(SigningKey.computePublicKey(p1).substring(2));
201193
return "0x" + pub0.add(pub1).toHex(!!compressed)
202194
}
203195
}

0 commit comments

Comments
 (0)