diff --git a/docs/sbom/coreguard-0.1.0.cdx.json b/docs/sbom/coreguard-0.1.0.cdx.json index 898f2085..d9342a65 100644 --- a/docs/sbom/coreguard-0.1.0.cdx.json +++ b/docs/sbom/coreguard-0.1.0.cdx.json @@ -1,7 +1,7 @@ { "bomFormat": "CycloneDX", "specVersion": "1.5", - "serialNumber": "urn:uuid:5d55c919-b9c6-86a7-8b7a-cb0439db44ee", + "serialNumber": "urn:uuid:76b71079-7f06-1499-62d4-0e1e40e6d928", "version": 1, "metadata": { "tools": [ @@ -365,12 +365,12 @@ { "type": "library", "name": "@noble/curves", - "version": "1.9.7", - "purl": "pkg:npm/%40noble/curves@1.9.7", + "version": "2.4.0", + "purl": "pkg:npm/%40noble/curves@2.4.0", "hashes": [ { "alg": "SHA-512", - "content": "81b286711518223037ff308235a5837224cc148d1d0a4be8bb74cbf199b2e4d73bb09e3b6b48ed39e6684da33192ea91a3d71186339d6495de831604e4537fc7" + "content": "3f8ffadb3ae07c7df70a7784dc39f85a1255036d98514d1e479d702886a7e0d551bf0b00d189cf4f0586a4d6e9b9a712ba399214bbf2ce9cae477d3e7dbab613" } ], "licenses": [ @@ -403,12 +403,12 @@ { "type": "library", "name": "@noble/hashes", - "version": "1.8.0", - "purl": "pkg:npm/%40noble/hashes@1.8.0", + "version": "2.4.0", + "purl": "pkg:npm/%40noble/hashes@2.4.0", "hashes": [ { "alg": "SHA-512", - "content": "8c2b3d95d77b370ce98170c87fa3f7f8dac787dfec0fa0907711f28d023e87feab0cda3cf32a41c71cf8e540ee647cfdaf7b4dcfb37f5489d256855db57108e0" + "content": "5f95da5566480424fb1c76469b923b6505c3c0b1be6c65ee4ab310016fbb66f97cee1d6499cd5907555244972951fa14ac6429e05928c66e6467e32cf9a5be78" } ], "licenses": [ @@ -438,25 +438,6 @@ } ] }, - { - "type": "library", - "name": "@stellar/stellar-sdk/node_modules/@noble/hashes", - "version": "2.4.0", - "purl": "pkg:npm/%40stellar/stellar-sdk/node_modules/@noble/hashes@2.4.0", - "hashes": [ - { - "alg": "SHA-512", - "content": "5f95da5566480424fb1c76469b923b6505c3c0b1be6c65ee4ab310016fbb66f97cee1d6499cd5907555244972951fa14ac6429e05928c66e6467e32cf9a5be78" - } - ], - "licenses": [ - { - "license": { - "id": "MIT" - } - } - ] - }, { "type": "library", "name": "@stellar/stellar-sdk", diff --git a/package-lock.json b/package-lock.json index 988be6a9..d0051197 100644 --- a/package-lock.json +++ b/package-lock.json @@ -140,15 +140,15 @@ } }, "node_modules/@noble/curves": { - "version": "1.9.7", - "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.9.7.tgz", - "integrity": "sha512-gbKGcRUYIjA3/zCCNaWDciTMFI0dCkvou3TL8Zmy5Nc7sJ47a0jtOeZoTaMxkuqRo9cRhjOdZJXegxYE5FN/xw==", + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-2.4.0.tgz", + "integrity": "sha512-P4/62zrgfH33CneE3Dn4WhJVA22YUU0eR51wKIan4NVRvwsA0YnPTwWGpNbpuacSujmSFLvyzpyuR30+fbq2Ew==", "license": "MIT", "dependencies": { - "@noble/hashes": "1.8.0" + "@noble/hashes": "2.4.0" }, "engines": { - "node": "^14.21.3 || >=16" + "node": ">= 20.19.0" }, "funding": { "url": "https://paulmillr.com/funding/" @@ -164,12 +164,12 @@ } }, "node_modules/@noble/hashes": { - "version": "1.8.0", - "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.8.0.tgz", - "integrity": "sha512-jCs9ldd7NwzpgXDIf6P3+NrHh9/sD6CQdxHyjQI+h/6rDNo88ypBxxz45UDuZHz9r3tNz7N/VInSVoVdtXEI4A==", + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-2.4.0.tgz", + "integrity": "sha512-X5XaVWZIBCT7HHZGm5I7ZQXDwLG+bGXuSrMQAW+7Zvl87h1kmc1ZB1VSRJcpUfoUrGQp4Fkoxm5kZ+Ms+aW+eA==", "license": "MIT", "engines": { - "node": "^14.21.3 || >=16" + "node": ">= 20.19.0" }, "funding": { "url": "https://paulmillr.com/funding/" @@ -213,18 +213,6 @@ "node": ">=22.12.0" } }, - "node_modules/@stellar/stellar-sdk/node_modules/@noble/hashes": { - "version": "2.4.0", - "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-2.4.0.tgz", - "integrity": "sha512-X5XaVWZIBCT7HHZGm5I7ZQXDwLG+bGXuSrMQAW+7Zvl87h1kmc1ZB1VSRJcpUfoUrGQp4Fkoxm5kZ+Ms+aW+eA==", - "license": "MIT", - "engines": { - "node": ">= 20.19.0" - }, - "funding": { - "url": "https://paulmillr.com/funding/" - } - }, "node_modules/@types/json-schema": { "version": "7.0.15", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz", @@ -1135,8 +1123,8 @@ "version": "0.1.0", "license": "MIT", "dependencies": { - "@noble/curves": "^1.9.0", - "@noble/hashes": "^1.8.0" + "@noble/curves": "^2.4.0", + "@noble/hashes": "^2.4.0" }, "engines": { "node": ">=18" diff --git a/packages/evm/keccak256.js b/packages/evm/keccak256.js index a2ca13e2..c95a8f48 100644 --- a/packages/evm/keccak256.js +++ b/packages/evm/keccak256.js @@ -5,7 +5,7 @@ * Everything else in @coreguard/evm imports keccak from here (or bytes.js). */ -import { keccak_256 } from "@noble/hashes/sha3"; +import { keccak_256 } from "@noble/hashes/sha3.js"; import { asBytes } from "./bytes.js"; /** diff --git a/packages/evm/package.json b/packages/evm/package.json index 4c9c313d..bf98e5a7 100644 --- a/packages/evm/package.json +++ b/packages/evm/package.json @@ -12,8 +12,8 @@ "node": ">=18" }, "dependencies": { - "@noble/curves": "^1.9.0", - "@noble/hashes": "^1.8.0" + "@noble/curves": "^2.4.0", + "@noble/hashes": "^2.4.0" }, "publishConfig": { "access": "public" diff --git a/packages/evm/signer/secp256k1.js b/packages/evm/signer/secp256k1.js index 933e0e00..286647f3 100644 --- a/packages/evm/signer/secp256k1.js +++ b/packages/evm/signer/secp256k1.js @@ -6,11 +6,14 @@ * and public-key recovery (the EIP-712 verifier primitive). */ -import { secp256k1 } from "@noble/curves/secp256k1"; +import { secp256k1 } from "@noble/curves/secp256k1.js"; import { bytesToHex, hexToBytes, asBytes, concat } from "../bytes.js"; import { keccak } from "../keccak256.js"; import { addressFromPublicKey } from "./address.js"; +/** secp256k1 group order (n) — the scalar field order. */ +const CURVE_ORDER = secp256k1.Point.CURVE().n; + /** 32-byte big-endian encoding of a 256-bit value. */ export function to32(value) { const bytes = hexToBytes(Buffer.from(BigInt(value).toString(16).padStart(64, "0"), "hex")); @@ -35,15 +38,15 @@ export function recoverSignerAddress(digest, sig) { const { r, s, v } = sig || {}; const rBig = BigInt(typeof r === "bigint" ? r : "0x" + String(r || "")); const sBig = BigInt(typeof s === "bigint" ? s : "0x" + String(s || "")); - if (rBig <= 0n || rBig >= secp256k1.CURVE.n) throw new Error("recover: r out of range"); - if (sBig <= 0n || sBig >= secp256k1.CURVE.n) throw new Error("recover: s out of range"); + if (rBig <= 0n || rBig >= CURVE_ORDER) throw new Error("recover: r out of range"); + if (sBig <= 0n || sBig >= CURVE_ORDER) throw new Error("recover: s out of range"); const recid = Number(v) % 27; if (recid > 1) throw new Error("recover: v must be 27 or 28"); const compact = concat(to32(rBig), to32(sBig)); - const sigObj = secp256k1.Signature.fromBytes(compact).addRecoveryBit(recid); + const sigObj = secp256k1.Signature.fromBytes(compact, "compact").addRecoveryBit(recid); const pub = sigObj.recoverPublicKey(asBytes(digest)); - return addressFromPublicKey(pub.toRawBytes(false)); + return addressFromPublicKey(pub.toBytes(false)); } /** @@ -55,11 +58,14 @@ export async function signDigest(digestBytes, privateKeyHex) { const priv = typeof privateKeyHex === "string" ? hexToBytes(privateKeyHex) : privateKeyHex; - const sig = secp256k1.sign(asBytes(digestBytes), priv, { prehash: false }); - const recid = sig.recovery; - const compact = sig.toCompactRawBytes(); - const r = bytesToHex(compact.slice(0, 32)); - const s = bytesToHex(compact.slice(32)); + // noble/curves v2 returns encoded bytes; "recovered" is 65 bytes: + // recovery byte ‖ r(32) ‖ s(32). + const recovered = secp256k1.sign(asBytes(digestBytes), priv, { prehash: false, format: "recovered" }); + const recid = recovered[0]; + if (recid > 1) throw new Error("sign: recovery id outside the Ethereum v=27/28 range"); + const compact = recovered.subarray(1); + const r = bytesToHex(compact.subarray(0, 32)); + const s = bytesToHex(compact.subarray(32, 64)); return { r, s, v: 27 + recid }; }