Demo for migrating to noble-curves

This commit is contained in:
Richard Moore 2023-04-22 21:03:20 +09:00
parent 8fcbd9cac4
commit 5e5e5322c4
16 changed files with 4581 additions and 2539 deletions

3452
dist/ethers.js vendored

File diff suppressed because it is too large Load Diff

2
dist/ethers.js.map vendored

File diff suppressed because one or more lines are too long

2
dist/ethers.min.js vendored

File diff suppressed because one or more lines are too long

3452
dist/ethers.umd.js vendored

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -42,9 +42,9 @@ const assert$1 = {
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
const u32 = (arr) => new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4)); const u32 = (arr) => new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
// big-endian hardware is rare. Just in case someone still decides to run hashes:
// early-throw an error because we don't support BE yet.
const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44; const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
// There is almost no big endian hardware, but js typed arrays uses platform specific endianness.
// So, just to be sure not to corrupt anything.
if (!isLE) if (!isLE)
throw new Error('Non little-endian hardware is not supported'); throw new Error('Non little-endian hardware is not supported');
Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, '0')); Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, '0'));

File diff suppressed because one or more lines are too long

@ -4,40 +4,17 @@
* *
* @_subsection: api/crypto:Signing [about-signing] * @_subsection: api/crypto:Signing [about-signing]
*/ */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.SigningKey = void 0; exports.SigningKey = void 0;
const secp256k1 = __importStar(require("@noble/secp256k1")); const secp256k1_1 = require("@noble/curves/secp256k1");
const index_js_1 = require("../utils/index.js"); const index_js_1 = require("../utils/index.js");
const hmac_js_1 = require("./hmac.js"); //import { computeHmac } from "./hmac.js";
const signature_js_1 = require("./signature.js"); const signature_js_1 = require("./signature.js");
//const N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"); //const N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
// Make noble-secp256k1 sync // Make noble-secp256k1 sync
secp256k1.utils.hmacSha256Sync = function (key, ...messages) { //secp256k1.utils.hmacSha256Sync = function(key: Uint8Array, ...messages: Array<Uint8Array>): Uint8Array {
return (0, index_js_1.getBytes)((0, hmac_js_1.computeHmac)("sha256", key, (0, index_js_1.concat)(messages))); // return getBytes(computeHmac("sha256", key, concat(messages)));
}; //}
/** /**
* A **SigningKey** provides high-level access to the elliptic curve * A **SigningKey** provides high-level access to the elliptic curve
* cryptography (ECC) operations and key management. * cryptography (ECC) operations and key management.
@ -75,15 +52,13 @@ class SigningKey {
*/ */
sign(digest) { sign(digest) {
(0, index_js_1.assertArgument)((0, index_js_1.dataLength)(digest) === 32, "invalid digest length", "digest", digest); (0, index_js_1.assertArgument)((0, index_js_1.dataLength)(digest) === 32, "invalid digest length", "digest", digest);
const [sigDer, recid] = secp256k1.signSync((0, index_js_1.getBytesCopy)(digest), (0, index_js_1.getBytesCopy)(this.#privateKey), { const sig = secp256k1_1.secp256k1.sign((0, index_js_1.getBytesCopy)(digest), (0, index_js_1.getBytesCopy)(this.#privateKey), {
recovered: true, lowS: true
canonical: true
}); });
const sig = secp256k1.Signature.fromHex(sigDer);
return signature_js_1.Signature.from({ return signature_js_1.Signature.from({
r: (0, index_js_1.toBeHex)("0x" + sig.r.toString(16), 32), r: (0, index_js_1.toBeHex)(sig.r, 32),
s: (0, index_js_1.toBeHex)("0x" + sig.s.toString(16), 32), s: (0, index_js_1.toBeHex)(sig.s, 32),
v: (recid ? 0x1c : 0x1b) v: (sig.recovery ? 0x1c : 0x1b)
}); });
} }
/** /**
@ -111,7 +86,7 @@ class SigningKey {
computeSharedSecret(other) { computeSharedSecret(other) {
const pubKey = SigningKey.computePublicKey(other); const pubKey = SigningKey.computePublicKey(other);
console.log(pubKey); console.log(pubKey);
return (0, index_js_1.hexlify)(secp256k1.getSharedSecret((0, index_js_1.getBytesCopy)(this.#privateKey), (0, index_js_1.getBytes)(pubKey))); return (0, index_js_1.hexlify)(secp256k1_1.secp256k1.getSharedSecret((0, index_js_1.getBytesCopy)(this.#privateKey), (0, index_js_1.getBytes)(pubKey), false));
} }
/** /**
* Compute the public key for %%key%%, optionally %%compressed%%. * Compute the public key for %%key%%, optionally %%compressed%%.
@ -142,7 +117,7 @@ class SigningKey {
let bytes = (0, index_js_1.getBytes)(key, "key"); let bytes = (0, index_js_1.getBytes)(key, "key");
// private key // private key
if (bytes.length === 32) { if (bytes.length === 32) {
const pubKey = secp256k1.getPublicKey(bytes, !!compressed); const pubKey = secp256k1_1.secp256k1.getPublicKey(bytes, !!compressed);
return (0, index_js_1.hexlify)(pubKey); return (0, index_js_1.hexlify)(pubKey);
} }
// raw public key; use uncompressed key with 0x04 prefix // raw public key; use uncompressed key with 0x04 prefix
@ -152,7 +127,7 @@ class SigningKey {
pub.set(bytes, 1); pub.set(bytes, 1);
bytes = pub; bytes = pub;
} }
const point = secp256k1.Point.fromHex(bytes); const point = secp256k1_1.secp256k1.ProjectivePoint.fromHex(bytes);
return (0, index_js_1.hexlify)(point.toRawBytes(compressed)); return (0, index_js_1.hexlify)(point.toRawBytes(compressed));
} }
/** /**
@ -176,10 +151,12 @@ class SigningKey {
static recoverPublicKey(digest, signature) { static recoverPublicKey(digest, signature) {
(0, index_js_1.assertArgument)((0, index_js_1.dataLength)(digest) === 32, "invalid digest length", "digest", digest); (0, index_js_1.assertArgument)((0, index_js_1.dataLength)(digest) === 32, "invalid digest length", "digest", digest);
const sig = signature_js_1.Signature.from(signature); const sig = signature_js_1.Signature.from(signature);
const der = secp256k1.Signature.fromCompact((0, index_js_1.getBytesCopy)((0, index_js_1.concat)([sig.r, sig.s]))).toDERRawBytes(); const secpSig = secp256k1_1.secp256k1.Signature.fromCompact((0, index_js_1.getBytesCopy)((0, index_js_1.concat)([sig.r, sig.s])));
const pubKey = secp256k1.recoverPublicKey((0, index_js_1.getBytesCopy)(digest), der, sig.yParity); secpSig.addRecoveryBit(sig.yParity);
const pubKey = secpSig.recoverPublicKey((0, index_js_1.getBytesCopy)(digest));
(0, index_js_1.assertArgument)(pubKey != null, "invalid signautre for digest", "signature", signature); (0, index_js_1.assertArgument)(pubKey != null, "invalid signautre for digest", "signature", signature);
return (0, index_js_1.hexlify)(pubKey); console.log("SS2", pubKey);
return (0, index_js_1.hexlify)(pubKey.toHex(false));
} }
/** /**
* Returns the point resulting from adding the ellipic curve points * Returns the point resulting from adding the ellipic curve points
@ -192,8 +169,8 @@ class SigningKey {
* addresses from parent public keys and chain codes. * addresses from parent public keys and chain codes.
*/ */
static addPoints(p0, p1, compressed) { static addPoints(p0, p1, compressed) {
const pub0 = secp256k1.Point.fromHex(SigningKey.computePublicKey(p0).substring(2)); const pub0 = secp256k1_1.secp256k1.ProjectivePoint.fromHex(SigningKey.computePublicKey(p0).substring(2));
const pub1 = secp256k1.Point.fromHex(SigningKey.computePublicKey(p1).substring(2)); const pub1 = secp256k1_1.secp256k1.ProjectivePoint.fromHex(SigningKey.computePublicKey(p1).substring(2));
return "0x" + pub0.add(pub1).toHex(!!compressed); return "0x" + pub0.add(pub1).toHex(!!compressed);
} }
} }

@ -1 +1 @@
{"version":3,"file":"signing-key.js","sourceRoot":"","sources":["../../src.ts/crypto/signing-key.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,4DAA8C;AAE9C,gDAG2B;AAE3B,uCAAwC;AACxC,iDAA2C;AAO3C,yFAAyF;AAEzF,4BAA4B;AAC5B,SAAS,CAAC,KAAK,CAAC,cAAc,GAAG,UAAS,GAAe,EAAE,GAAG,QAA2B;IACrF,OAAO,IAAA,mBAAQ,EAAC,IAAA,qBAAW,EAAC,QAAQ,EAAE,GAAG,EAAE,IAAA,iBAAM,EAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAClE,CAAC,CAAA;AAED;;;GAGG;AACH,MAAa,UAAU;IACnB,WAAW,CAAS;IAEpB;;OAEG;IACH,YAAY,UAAqB;QAC7B,IAAA,yBAAc,EAAC,IAAA,qBAAU,EAAC,UAAU,CAAC,KAAK,EAAE,EAAE,qBAAqB,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC;QACjG,IAAI,CAAC,WAAW,GAAG,IAAA,kBAAO,EAAC,UAAU,CAAC,CAAC;IAC3C,CAAC;IAED;;OAEG;IACH,IAAI,UAAU,KAAa,OAAO,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC;IAErD;;;;;OAKG;IACH,IAAI,SAAS,KAAa,OAAO,UAAU,CAAC,gBAAgB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAEjF;;;;;;OAMG;IACH,IAAI,mBAAmB,KAAa,OAAO,UAAU,CAAC,gBAAgB,CAAC,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC,CAA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1"; import { secp256k1 } from "@noble/curves/secp256k1";
import { concat, dataLength, getBytes, getBytesCopy, hexlify, toBeHex, assertArgument } from "../utils/index.js"; import { concat, dataLength, getBytes, getBytesCopy, hexlify, toBeHex, assertArgument } from "../utils/index.js";
import { computeHmac } from "./hmac.js"; //import { computeHmac } from "./hmac.js";
import { Signature } from "./signature.js"; import { Signature } from "./signature.js";
//const N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"); //const N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
// Make noble-secp256k1 sync // Make noble-secp256k1 sync
secp256k1.utils.hmacSha256Sync = function (key, ...messages) { //secp256k1.utils.hmacSha256Sync = function(key: Uint8Array, ...messages: Array<Uint8Array>): Uint8Array {
return getBytes(computeHmac("sha256", key, concat(messages))); // return getBytes(computeHmac("sha256", key, concat(messages)));
}; //}
/** /**
* A **SigningKey** provides high-level access to the elliptic curve * A **SigningKey** provides high-level access to the elliptic curve
* cryptography (ECC) operations and key management. * cryptography (ECC) operations and key management.
@ -49,15 +49,13 @@ export class SigningKey {
*/ */
sign(digest) { sign(digest) {
assertArgument(dataLength(digest) === 32, "invalid digest length", "digest", digest); assertArgument(dataLength(digest) === 32, "invalid digest length", "digest", digest);
const [sigDer, recid] = secp256k1.signSync(getBytesCopy(digest), getBytesCopy(this.#privateKey), { const sig = secp256k1.sign(getBytesCopy(digest), getBytesCopy(this.#privateKey), {
recovered: true, lowS: true
canonical: true
}); });
const sig = secp256k1.Signature.fromHex(sigDer);
return Signature.from({ return Signature.from({
r: toBeHex("0x" + sig.r.toString(16), 32), r: toBeHex(sig.r, 32),
s: toBeHex("0x" + sig.s.toString(16), 32), s: toBeHex(sig.s, 32),
v: (recid ? 0x1c : 0x1b) v: (sig.recovery ? 0x1c : 0x1b)
}); });
} }
/** /**
@ -85,7 +83,7 @@ export class SigningKey {
computeSharedSecret(other) { computeSharedSecret(other) {
const pubKey = SigningKey.computePublicKey(other); const pubKey = SigningKey.computePublicKey(other);
console.log(pubKey); console.log(pubKey);
return hexlify(secp256k1.getSharedSecret(getBytesCopy(this.#privateKey), getBytes(pubKey))); return hexlify(secp256k1.getSharedSecret(getBytesCopy(this.#privateKey), getBytes(pubKey), false));
} }
/** /**
* Compute the public key for %%key%%, optionally %%compressed%%. * Compute the public key for %%key%%, optionally %%compressed%%.
@ -126,7 +124,7 @@ export class SigningKey {
pub.set(bytes, 1); pub.set(bytes, 1);
bytes = pub; bytes = pub;
} }
const point = secp256k1.Point.fromHex(bytes); const point = secp256k1.ProjectivePoint.fromHex(bytes);
return hexlify(point.toRawBytes(compressed)); return hexlify(point.toRawBytes(compressed));
} }
/** /**
@ -150,10 +148,12 @@ export class SigningKey {
static recoverPublicKey(digest, signature) { static recoverPublicKey(digest, signature) {
assertArgument(dataLength(digest) === 32, "invalid digest length", "digest", digest); assertArgument(dataLength(digest) === 32, "invalid digest length", "digest", digest);
const sig = Signature.from(signature); const sig = Signature.from(signature);
const der = secp256k1.Signature.fromCompact(getBytesCopy(concat([sig.r, sig.s]))).toDERRawBytes(); const secpSig = secp256k1.Signature.fromCompact(getBytesCopy(concat([sig.r, sig.s])));
const pubKey = secp256k1.recoverPublicKey(getBytesCopy(digest), der, sig.yParity); secpSig.addRecoveryBit(sig.yParity);
const pubKey = secpSig.recoverPublicKey(getBytesCopy(digest));
assertArgument(pubKey != null, "invalid signautre for digest", "signature", signature); assertArgument(pubKey != null, "invalid signautre for digest", "signature", signature);
return hexlify(pubKey); console.log("SS2", pubKey);
return hexlify(pubKey.toHex(false));
} }
/** /**
* Returns the point resulting from adding the ellipic curve points * Returns the point resulting from adding the ellipic curve points
@ -166,8 +166,8 @@ export class SigningKey {
* addresses from parent public keys and chain codes. * addresses from parent public keys and chain codes.
*/ */
static addPoints(p0, p1, compressed) { static addPoints(p0, p1, compressed) {
const pub0 = secp256k1.Point.fromHex(SigningKey.computePublicKey(p0).substring(2)); const pub0 = secp256k1.ProjectivePoint.fromHex(SigningKey.computePublicKey(p0).substring(2));
const pub1 = secp256k1.Point.fromHex(SigningKey.computePublicKey(p1).substring(2)); const pub1 = secp256k1.ProjectivePoint.fromHex(SigningKey.computePublicKey(p1).substring(2));
return "0x" + pub0.add(pub1).toHex(!!compressed); return "0x" + pub0.add(pub1).toHex(!!compressed);
} }
} }

@ -1 +1 @@
{"version":3,"file":"signing-key.js","sourceRoot":"","sources":["../../src.ts/crypto/signing-key.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,SAAS,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EACH,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAC5D,cAAc,EACjB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAO3C,yFAAyF;AAEzF,4BAA4B;AAC5B,SAAS,CAAC,KAAK,CAAC,cAAc,GAAG,UAAS,GAAe,EAAE,GAAG,QAA2B;IACrF,OAAO,QAAQ,CAAC,WAAW,CAAC,QAAQ,EAAE,GAAG,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAClE,CAAC,CAAA;AAED;;;GAGG;AACH,MAAM,OAAO,UAAU;IACnB,WAAW,CAAS;IAEpB;;OAEG;IACH,YAAY,UAAqB;QAC7B,cAAc,CAAC,UAAU,CAAC,UAAU,CAAC,KAAK,EAAE,EAAE,qBAAqB,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC;QACjG,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAC3C,CAAC;IAED;;OAEG;IACH,IAAI,UAAU,KAAa,OAAO,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC;IAErD;;;;;OAKG;IACH,IAAI,SAAS,KAAa,OAAO,UAAU,CAAC,gBAAgB,CAAC,IAA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ockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "ethers", "name": "ethers",
"version": "6.3.0", "version": "6.3.1",
"funding": [ "funding": [
{ {
"type": "individual", "type": "individual",
@ -20,8 +20,9 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@adraffy/ens-normalize": "1.9.0", "@adraffy/ens-normalize": "1.9.0",
"@noble/hashes": "1.1.2", "@noble/curves": "1.0.0",
"@noble/secp256k1": "1.7.1", "@noble/hashes": "1.3.0",
"@types/node": "18.15.13",
"aes-js": "4.0.0-beta.3", "aes-js": "4.0.0-beta.3",
"tslib": "2.4.0", "tslib": "2.4.0",
"ws": "8.5.0" "ws": "8.5.0"
@ -77,30 +78,33 @@
"dev": true "dev": true
}, },
"node_modules/@jridgewell/trace-mapping": { "node_modules/@jridgewell/trace-mapping": {
"version": "0.3.17", "version": "0.3.18",
"resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.17.tgz", "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.18.tgz",
"integrity": "sha512-MCNzAp77qzKca9+W/+I0+sEpaUnZoeasnghNeVc41VZCEKaCH73Vq3BZZ/SzWIgrqE4H4ceI+p+b6C0mHf9T4g==", "integrity": "sha512-w+niJYzMHdd7USdiH2U6869nqhD2nbfZXND5Yp93qIbEmnDNk7PD48o+YchRVpzMU7M6jVCbenTR7PA1FLQ9pA==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"@jridgewell/resolve-uri": "3.1.0", "@jridgewell/resolve-uri": "3.1.0",
"@jridgewell/sourcemap-codec": "1.4.14" "@jridgewell/sourcemap-codec": "1.4.14"
} }
}, },
"node_modules/@noble/hashes": { "node_modules/@noble/curves": {
"version": "1.1.2", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.1.2.tgz", "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.0.0.tgz",
"integrity": "sha512-KYRCASVTv6aeUi1tsF8/vpyR7zpfs3FUzy2Jqm+MU+LmUKhQ0y2FpfwqkCcxSg2ua4GALJd8k2R76WxwZGbQpA==", "integrity": "sha512-2upgEu0iLiDVDZkNLeFV2+ht0BAVgQnEmCk6JsOch9Rp8xfkMCbvbAZlA2pBHQc73dbl+vFOXfqkf4uemdn0bw==",
"funding": [ "funding": [
{ {
"type": "individual", "type": "individual",
"url": "https://paulmillr.com/funding/" "url": "https://paulmillr.com/funding/"
} }
] ],
"dependencies": {
"@noble/hashes": "1.3.0"
}
}, },
"node_modules/@noble/secp256k1": { "node_modules/@noble/hashes": {
"version": "1.7.1", "version": "1.3.0",
"resolved": "https://registry.npmjs.org/@noble/secp256k1/-/secp256k1-1.7.1.tgz", "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.0.tgz",
"integrity": "sha512-hOUk6AyBFmqVrv7k5WAw/LpszxVbj9gGN4JRkIX52fdFAj1UA61KXmZDvqVEm+pOyec3+fIeZB02LYa/pWOArw==", "integrity": "sha512-ilHEACi9DwqJB0pw7kv+Apvh50jiiSyR/cQ3y4W7lOR5mhvn/50FLUfsnfJz0BDZtl/RR16kXvptiv6q1msYZg==",
"funding": [ "funding": [
{ {
"type": "individual", "type": "individual",
@ -164,10 +168,9 @@
"dev": true "dev": true
}, },
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "18.15.11", "version": "18.15.13",
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.11.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.13.tgz",
"integrity": "sha512-E5Kwq2n4SbMzQOn6wnmBjuK9ouqlURrcZDVfbo9ftDDTFt3nk7ZKK4GMOzoYgnpQJKcxwQw+lGaBvvlMo0qN/Q==", "integrity": "sha512-N+0kuo9KgrUQ1Sn/ifDXsvg0TTleP7rIy4zOBGECxAljqvqfqpTfzx0Q1NUedOixRMBfe2Whhb056a42cWs26Q=="
"dev": true
}, },
"node_modules/@types/resolve": { "node_modules/@types/resolve": {
"version": "1.17.1", "version": "1.17.1",
@ -742,9 +745,9 @@
} }
}, },
"node_modules/is-core-module": { "node_modules/is-core-module": {
"version": "2.11.0", "version": "2.12.0",
"resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.11.0.tgz", "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.12.0.tgz",
"integrity": "sha512-RRjxlvLDkD1YJwDbroBHMb+cukurkDWNyHx7D3oNB5x9rb5ogcksMC5wHCadcXoo67gVr/+3GFySh3134zi6rw==", "integrity": "sha512-RECHCBCd/viahWmwj6enj19sKbHfJrddi/6cBDsNTKbNq0f7VeaUkBo60BqzvPqo/W54ChS62Z5qyun7cfOMqQ==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"has": "^1.0.3" "has": "^1.0.3"

@ -10,8 +10,8 @@
}, },
"dependencies": { "dependencies": {
"@adraffy/ens-normalize": "1.9.0", "@adraffy/ens-normalize": "1.9.0",
"@noble/hashes": "1.1.2", "@noble/curves": "1.0.0",
"@noble/secp256k1": "1.7.1", "@noble/hashes": "1.3.0",
"@types/node": "18.15.13", "@types/node": "18.15.13",
"aes-js": "4.0.0-beta.3", "aes-js": "4.0.0-beta.3",
"tslib": "2.4.0", "tslib": "2.4.0",

@ -4,14 +4,14 @@
* @_subsection: api/crypto:Signing [about-signing] * @_subsection: api/crypto:Signing [about-signing]
*/ */
import * as secp256k1 from "@noble/secp256k1"; import { secp256k1 } from "@noble/curves/secp256k1";
import { import {
concat, dataLength, getBytes, getBytesCopy, hexlify, toBeHex, concat, dataLength, getBytes, getBytesCopy, hexlify, toBeHex,
assertArgument assertArgument
} from "../utils/index.js"; } from "../utils/index.js";
import { computeHmac } from "./hmac.js"; //import { computeHmac } from "./hmac.js";
import { Signature } from "./signature.js"; import { Signature } from "./signature.js";
import type { BytesLike } from "../utils/index.js"; import type { BytesLike } from "../utils/index.js";
@ -22,9 +22,9 @@ import type { SignatureLike } from "./index.js";
//const N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"); //const N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
// Make noble-secp256k1 sync // Make noble-secp256k1 sync
secp256k1.utils.hmacSha256Sync = function(key: Uint8Array, ...messages: Array<Uint8Array>): Uint8Array { //secp256k1.utils.hmacSha256Sync = function(key: Uint8Array, ...messages: Array<Uint8Array>): Uint8Array {
return getBytes(computeHmac("sha256", key, concat(messages))); // return getBytes(computeHmac("sha256", key, concat(messages)));
} //}
/** /**
* A **SigningKey** provides high-level access to the elliptic curve * A **SigningKey** provides high-level access to the elliptic curve
@ -69,16 +69,14 @@ export class SigningKey {
sign(digest: BytesLike): Signature { sign(digest: BytesLike): Signature {
assertArgument(dataLength(digest) === 32, "invalid digest length", "digest", digest); assertArgument(dataLength(digest) === 32, "invalid digest length", "digest", digest);
const [ sigDer, recid ] = secp256k1.signSync(getBytesCopy(digest), getBytesCopy(this.#privateKey), { const sig = secp256k1.sign(getBytesCopy(digest), getBytesCopy(this.#privateKey), {
recovered: true, lowS: true
canonical: true
}); });
const sig = secp256k1.Signature.fromHex(sigDer);
return Signature.from({ return Signature.from({
r: toBeHex("0x" + sig.r.toString(16), 32), r: toBeHex(sig.r, 32),
s: toBeHex("0x" + sig.s.toString(16), 32), s: toBeHex(sig.s, 32),
v: (recid ? 0x1c: 0x1b) v: (sig.recovery ? 0x1c: 0x1b)
}); });
} }
@ -106,8 +104,8 @@ export class SigningKey {
*/ */
computeSharedSecret(other: BytesLike): string { computeSharedSecret(other: BytesLike): string {
const pubKey = SigningKey.computePublicKey(other); const pubKey = SigningKey.computePublicKey(other);
console.log(pubKey); console.log(pubKey);
return hexlify(secp256k1.getSharedSecret(getBytesCopy(this.#privateKey), getBytes(pubKey))); return hexlify(secp256k1.getSharedSecret(getBytesCopy(this.#privateKey), getBytes(pubKey), false));
} }
/** /**
@ -152,7 +150,7 @@ export class SigningKey {
bytes = pub; bytes = pub;
} }
const point = secp256k1.Point.fromHex(bytes); const point = secp256k1.ProjectivePoint.fromHex(bytes);
return hexlify(point.toRawBytes(compressed)); return hexlify(point.toRawBytes(compressed));
} }
@ -178,12 +176,16 @@ export class SigningKey {
assertArgument(dataLength(digest) === 32, "invalid digest length", "digest", digest); assertArgument(dataLength(digest) === 32, "invalid digest length", "digest", digest);
const sig = Signature.from(signature); const sig = Signature.from(signature);
const der = secp256k1.Signature.fromCompact(getBytesCopy(concat([ sig.r, sig.s ]))).toDERRawBytes();
const pubKey = secp256k1.recoverPublicKey(getBytesCopy(digest), der, sig.yParity); const secpSig = secp256k1.Signature.fromCompact(getBytesCopy(concat([ sig.r, sig.s ])));
secpSig.addRecoveryBit(sig.yParity);
const pubKey = secpSig.recoverPublicKey(getBytesCopy(digest));
assertArgument(pubKey != null, "invalid signautre for digest", "signature", signature); assertArgument(pubKey != null, "invalid signautre for digest", "signature", signature);
return hexlify(pubKey); console.log("SS2", pubKey);
return hexlify(pubKey.toHex(false));
} }
/** /**
@ -197,8 +199,8 @@ export class SigningKey {
* addresses from parent public keys and chain codes. * addresses from parent public keys and chain codes.
*/ */
static addPoints(p0: BytesLike, p1: BytesLike, compressed?: boolean): string { static addPoints(p0: BytesLike, p1: BytesLike, compressed?: boolean): string {
const pub0 = secp256k1.Point.fromHex(SigningKey.computePublicKey(p0).substring(2)); const pub0 = secp256k1.ProjectivePoint.fromHex(SigningKey.computePublicKey(p0).substring(2));
const pub1 = secp256k1.Point.fromHex(SigningKey.computePublicKey(p1).substring(2)); const pub1 = secp256k1.ProjectivePoint.fromHex(SigningKey.computePublicKey(p1).substring(2));
return "0x" + pub0.add(pub1).toHex(!!compressed) return "0x" + pub0.add(pub1).toHex(!!compressed)
} }
} }

@ -1 +1 @@
{"version":3,"file":"signing-key.d.ts","sourceRoot":"","sources":["../../src.ts/crypto/signing-key.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAUH,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,OA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