@ocap/mcrypto 1.29.23 → 1.29.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/signer/ethereum.mjs +11 -7
- package/esm/signer/secp256k1.mjs +15 -8
- package/esm/webauthn.mjs +7 -6
- package/lib/signer/ethereum.cjs +11 -7
- package/lib/signer/secp256k1.cjs +15 -8
- package/lib/webauthn.cjs +11 -10
- package/package.json +4 -4
package/esm/signer/ethereum.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { Secp256k1Signer } from "./secp256k1.mjs";
|
|
|
2
2
|
import { hexToBytes, isHexStrict, utf8ToHex } from "@ocap/util";
|
|
3
3
|
import { keccak_256 } from "@noble/hashes/sha3.js";
|
|
4
4
|
import { bytesToHex } from "@noble/hashes/utils.js";
|
|
5
|
-
import { secp256k1 } from "@noble/curves/secp256k1";
|
|
5
|
+
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
6
6
|
|
|
7
7
|
//#region src/signer/ethereum.ts
|
|
8
8
|
function toChecksumAddress(address) {
|
|
@@ -29,18 +29,22 @@ var EthereumSigner = class extends Secp256k1Signer {
|
|
|
29
29
|
return `0x${bytesToHex(keccak_256(new Uint8Array(ethMessage)))}`;
|
|
30
30
|
}
|
|
31
31
|
ethSign(data, privateKey) {
|
|
32
|
-
const msgHash = Buffer.from(data.slice(2), "hex");
|
|
33
|
-
const sk = Buffer.from(privateKey.slice(2), "hex");
|
|
34
|
-
const
|
|
32
|
+
const msgHash = new Uint8Array(Buffer.from(data.slice(2), "hex"));
|
|
33
|
+
const sk = new Uint8Array(Buffer.from(privateKey.slice(2), "hex"));
|
|
34
|
+
const recoveredBytes = secp256k1.sign(msgHash, sk, {
|
|
35
|
+
prehash: false,
|
|
36
|
+
format: "recovered"
|
|
37
|
+
});
|
|
38
|
+
const sig = secp256k1.Signature.fromBytes(recoveredBytes, "recovered");
|
|
35
39
|
const v = (27 + sig.recovery).toString(16).padStart(2, "0");
|
|
36
|
-
return `0x${sig.
|
|
40
|
+
return `0x${sig.toHex("compact")}${v}`;
|
|
37
41
|
}
|
|
38
42
|
ethRecover(data, signature) {
|
|
39
43
|
const sigHex = signature.slice(2);
|
|
40
44
|
const v = Number.parseInt(sigHex.slice(128, 130), 16);
|
|
41
45
|
const recovery = v < 2 ? v : 1 - v % 2;
|
|
42
|
-
const sig = secp256k1.Signature.
|
|
43
|
-
const msgHash = Buffer.from(data.slice(2), "hex");
|
|
46
|
+
const sig = secp256k1.Signature.fromHex(sigHex.slice(0, 128), "compact").addRecoveryBit(recovery);
|
|
47
|
+
const msgHash = new Uint8Array(Buffer.from(data.slice(2), "hex"));
|
|
44
48
|
const pubKeyHex = sig.recoverPublicKey(msgHash).toHex(false).slice(2);
|
|
45
49
|
return toChecksumAddress(`0x${bytesToHex(keccak_256(Buffer.from(pubKeyHex, "hex"))).slice(-40)}`);
|
|
46
50
|
}
|
package/esm/signer/secp256k1.mjs
CHANGED
|
@@ -2,8 +2,8 @@ import { encode } from "../encode.mjs";
|
|
|
2
2
|
import signer_default from "../protocols/signer.mjs";
|
|
3
3
|
import { toHex, toUint8Array } from "@ocap/util";
|
|
4
4
|
import randomBytes from "randombytes";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { bytesToHex } from "@noble/hashes/utils.js";
|
|
6
|
+
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
7
7
|
|
|
8
8
|
//#region src/signer/secp256k1.ts
|
|
9
9
|
/**
|
|
@@ -19,7 +19,7 @@ var Secp256k1Signer = class extends signer_default {
|
|
|
19
19
|
}
|
|
20
20
|
isValidSK(sk) {
|
|
21
21
|
if (sk.byteLength !== 32) return false;
|
|
22
|
-
return secp256k1.utils.
|
|
22
|
+
return secp256k1.utils.isValidSecretKey(sk);
|
|
23
23
|
}
|
|
24
24
|
/**
|
|
25
25
|
* @public
|
|
@@ -47,7 +47,8 @@ var Secp256k1Signer = class extends signer_default {
|
|
|
47
47
|
*/
|
|
48
48
|
getPublicKey(sk, encoding = "hex") {
|
|
49
49
|
if (!this.isValidSK(toUint8Array(sk))) throw new Error("Invalid secret key");
|
|
50
|
-
|
|
50
|
+
const pkBytes = secp256k1.getPublicKey(toUint8Array(sk), this.pkCompressed);
|
|
51
|
+
let pk = bytesToHex(new Uint8Array(pkBytes));
|
|
51
52
|
if (this.pkHasFormatPrefix === false) pk = pk.slice(2);
|
|
52
53
|
return encode(`0x${pk}`, encoding);
|
|
53
54
|
}
|
|
@@ -56,7 +57,10 @@ var Secp256k1Signer = class extends signer_default {
|
|
|
56
57
|
*/
|
|
57
58
|
sign(message, sk, encoding = "hex") {
|
|
58
59
|
const msg = toUint8Array(message);
|
|
59
|
-
return encode(`0x${secp256k1.sign(msg, toUint8Array(sk)
|
|
60
|
+
return encode(`0x${bytesToHex(secp256k1.sign(msg, toUint8Array(sk), {
|
|
61
|
+
prehash: false,
|
|
62
|
+
format: "der"
|
|
63
|
+
}))}`, encoding);
|
|
60
64
|
}
|
|
61
65
|
/**
|
|
62
66
|
* Verify if a signature is valid
|
|
@@ -72,9 +76,12 @@ var Secp256k1Signer = class extends signer_default {
|
|
|
72
76
|
pkBytes = prefixed;
|
|
73
77
|
}
|
|
74
78
|
try {
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
79
|
+
const sigBytes = secp256k1.Signature.fromHex(sigHex, "der").toBytes("der");
|
|
80
|
+
return secp256k1.verify(sigBytes, msg, pkBytes, {
|
|
81
|
+
prehash: false,
|
|
82
|
+
format: "der",
|
|
83
|
+
lowS: true
|
|
84
|
+
});
|
|
78
85
|
} catch {
|
|
79
86
|
return false;
|
|
80
87
|
}
|
package/esm/webauthn.mjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { __exportAll } from "./_virtual/rolldown_runtime.mjs";
|
|
2
2
|
import { sha256, sha384, sha512 } from "@noble/hashes/sha2.js";
|
|
3
|
-
import { secp256k1 } from "@noble/curves/secp256k1";
|
|
4
|
-
import { p256 } from "@noble/curves/
|
|
5
|
-
import { p384 } from "@noble/curves/p384";
|
|
6
|
-
import { p521 } from "@noble/curves/p521";
|
|
3
|
+
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
4
|
+
import { p256, p384, p521 } from "@noble/curves/nist.js";
|
|
7
5
|
|
|
8
6
|
//#region src/webauthn.ts
|
|
9
7
|
/**
|
|
@@ -332,8 +330,11 @@ async function verifySignature(opts) {
|
|
|
332
330
|
pubKeyBytes.set(y.length <= componentLen ? y : y.slice(y.length - componentLen), 1 + componentLen + (componentLen - y.length));
|
|
333
331
|
const msgHash = hash(data);
|
|
334
332
|
try {
|
|
335
|
-
|
|
336
|
-
|
|
333
|
+
return curve.verify(signature, msgHash, pubKeyBytes, {
|
|
334
|
+
prehash: false,
|
|
335
|
+
format: "der",
|
|
336
|
+
lowS: false
|
|
337
|
+
});
|
|
337
338
|
} catch {
|
|
338
339
|
return false;
|
|
339
340
|
}
|
package/lib/signer/ethereum.cjs
CHANGED
|
@@ -4,7 +4,7 @@ const require_signer_secp256k1 = require('./secp256k1.cjs');
|
|
|
4
4
|
let _ocap_util = require("@ocap/util");
|
|
5
5
|
let _noble_hashes_sha3_js = require("@noble/hashes/sha3.js");
|
|
6
6
|
let _noble_hashes_utils_js = require("@noble/hashes/utils.js");
|
|
7
|
-
let
|
|
7
|
+
let _noble_curves_secp256k1_js = require("@noble/curves/secp256k1.js");
|
|
8
8
|
|
|
9
9
|
//#region src/signer/ethereum.ts
|
|
10
10
|
function toChecksumAddress(address) {
|
|
@@ -31,18 +31,22 @@ var EthereumSigner = class extends require_signer_secp256k1.Secp256k1Signer {
|
|
|
31
31
|
return `0x${(0, _noble_hashes_utils_js.bytesToHex)((0, _noble_hashes_sha3_js.keccak_256)(new Uint8Array(ethMessage)))}`;
|
|
32
32
|
}
|
|
33
33
|
ethSign(data, privateKey) {
|
|
34
|
-
const msgHash = Buffer.from(data.slice(2), "hex");
|
|
35
|
-
const sk = Buffer.from(privateKey.slice(2), "hex");
|
|
36
|
-
const
|
|
34
|
+
const msgHash = new Uint8Array(Buffer.from(data.slice(2), "hex"));
|
|
35
|
+
const sk = new Uint8Array(Buffer.from(privateKey.slice(2), "hex"));
|
|
36
|
+
const recoveredBytes = _noble_curves_secp256k1_js.secp256k1.sign(msgHash, sk, {
|
|
37
|
+
prehash: false,
|
|
38
|
+
format: "recovered"
|
|
39
|
+
});
|
|
40
|
+
const sig = _noble_curves_secp256k1_js.secp256k1.Signature.fromBytes(recoveredBytes, "recovered");
|
|
37
41
|
const v = (27 + sig.recovery).toString(16).padStart(2, "0");
|
|
38
|
-
return `0x${sig.
|
|
42
|
+
return `0x${sig.toHex("compact")}${v}`;
|
|
39
43
|
}
|
|
40
44
|
ethRecover(data, signature) {
|
|
41
45
|
const sigHex = signature.slice(2);
|
|
42
46
|
const v = Number.parseInt(sigHex.slice(128, 130), 16);
|
|
43
47
|
const recovery = v < 2 ? v : 1 - v % 2;
|
|
44
|
-
const sig =
|
|
45
|
-
const msgHash = Buffer.from(data.slice(2), "hex");
|
|
48
|
+
const sig = _noble_curves_secp256k1_js.secp256k1.Signature.fromHex(sigHex.slice(0, 128), "compact").addRecoveryBit(recovery);
|
|
49
|
+
const msgHash = new Uint8Array(Buffer.from(data.slice(2), "hex"));
|
|
46
50
|
const pubKeyHex = sig.recoverPublicKey(msgHash).toHex(false).slice(2);
|
|
47
51
|
return toChecksumAddress(`0x${(0, _noble_hashes_utils_js.bytesToHex)((0, _noble_hashes_sha3_js.keccak_256)(Buffer.from(pubKeyHex, "hex"))).slice(-40)}`);
|
|
48
52
|
}
|
package/lib/signer/secp256k1.cjs
CHANGED
|
@@ -5,8 +5,8 @@ const require_protocols_signer = require('../protocols/signer.cjs');
|
|
|
5
5
|
let _ocap_util = require("@ocap/util");
|
|
6
6
|
let randombytes = require("randombytes");
|
|
7
7
|
randombytes = require_rolldown_runtime.__toESM(randombytes);
|
|
8
|
-
let
|
|
9
|
-
let
|
|
8
|
+
let _noble_hashes_utils_js = require("@noble/hashes/utils.js");
|
|
9
|
+
let _noble_curves_secp256k1_js = require("@noble/curves/secp256k1.js");
|
|
10
10
|
|
|
11
11
|
//#region src/signer/secp256k1.ts
|
|
12
12
|
/**
|
|
@@ -22,7 +22,7 @@ var Secp256k1Signer = class extends require_protocols_signer.default {
|
|
|
22
22
|
}
|
|
23
23
|
isValidSK(sk) {
|
|
24
24
|
if (sk.byteLength !== 32) return false;
|
|
25
|
-
return
|
|
25
|
+
return _noble_curves_secp256k1_js.secp256k1.utils.isValidSecretKey(sk);
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
28
|
* @public
|
|
@@ -50,7 +50,8 @@ var Secp256k1Signer = class extends require_protocols_signer.default {
|
|
|
50
50
|
*/
|
|
51
51
|
getPublicKey(sk, encoding = "hex") {
|
|
52
52
|
if (!this.isValidSK((0, _ocap_util.toUint8Array)(sk))) throw new Error("Invalid secret key");
|
|
53
|
-
|
|
53
|
+
const pkBytes = _noble_curves_secp256k1_js.secp256k1.getPublicKey((0, _ocap_util.toUint8Array)(sk), this.pkCompressed);
|
|
54
|
+
let pk = (0, _noble_hashes_utils_js.bytesToHex)(new Uint8Array(pkBytes));
|
|
54
55
|
if (this.pkHasFormatPrefix === false) pk = pk.slice(2);
|
|
55
56
|
return require_encode.encode(`0x${pk}`, encoding);
|
|
56
57
|
}
|
|
@@ -59,7 +60,10 @@ var Secp256k1Signer = class extends require_protocols_signer.default {
|
|
|
59
60
|
*/
|
|
60
61
|
sign(message, sk, encoding = "hex") {
|
|
61
62
|
const msg = (0, _ocap_util.toUint8Array)(message);
|
|
62
|
-
return require_encode.encode(`0x${
|
|
63
|
+
return require_encode.encode(`0x${(0, _noble_hashes_utils_js.bytesToHex)(_noble_curves_secp256k1_js.secp256k1.sign(msg, (0, _ocap_util.toUint8Array)(sk), {
|
|
64
|
+
prehash: false,
|
|
65
|
+
format: "der"
|
|
66
|
+
}))}`, encoding);
|
|
63
67
|
}
|
|
64
68
|
/**
|
|
65
69
|
* Verify if a signature is valid
|
|
@@ -75,9 +79,12 @@ var Secp256k1Signer = class extends require_protocols_signer.default {
|
|
|
75
79
|
pkBytes = prefixed;
|
|
76
80
|
}
|
|
77
81
|
try {
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
82
|
+
const sigBytes = _noble_curves_secp256k1_js.secp256k1.Signature.fromHex(sigHex, "der").toBytes("der");
|
|
83
|
+
return _noble_curves_secp256k1_js.secp256k1.verify(sigBytes, msg, pkBytes, {
|
|
84
|
+
prehash: false,
|
|
85
|
+
format: "der",
|
|
86
|
+
lowS: true
|
|
87
|
+
});
|
|
81
88
|
} catch {
|
|
82
89
|
return false;
|
|
83
90
|
}
|
package/lib/webauthn.cjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
2
|
let _noble_hashes_sha2_js = require("@noble/hashes/sha2.js");
|
|
3
|
-
let
|
|
4
|
-
let
|
|
5
|
-
let _noble_curves_p384 = require("@noble/curves/p384");
|
|
6
|
-
let _noble_curves_p521 = require("@noble/curves/p521");
|
|
3
|
+
let _noble_curves_secp256k1_js = require("@noble/curves/secp256k1.js");
|
|
4
|
+
let _noble_curves_nist_js = require("@noble/curves/nist.js");
|
|
7
5
|
|
|
8
6
|
//#region src/webauthn.ts
|
|
9
7
|
/**
|
|
@@ -294,22 +292,22 @@ function parseAuthenticatorData(authData) {
|
|
|
294
292
|
function getCurveForCrv(crv) {
|
|
295
293
|
switch (crv) {
|
|
296
294
|
case COSECRV.P256: return {
|
|
297
|
-
curve:
|
|
295
|
+
curve: _noble_curves_nist_js.p256,
|
|
298
296
|
hash: _noble_hashes_sha2_js.sha256,
|
|
299
297
|
componentLen: 32
|
|
300
298
|
};
|
|
301
299
|
case COSECRV.P384: return {
|
|
302
|
-
curve:
|
|
300
|
+
curve: _noble_curves_nist_js.p384,
|
|
303
301
|
hash: _noble_hashes_sha2_js.sha384,
|
|
304
302
|
componentLen: 48
|
|
305
303
|
};
|
|
306
304
|
case COSECRV.P521: return {
|
|
307
|
-
curve:
|
|
305
|
+
curve: _noble_curves_nist_js.p521,
|
|
308
306
|
hash: _noble_hashes_sha2_js.sha512,
|
|
309
307
|
componentLen: 66
|
|
310
308
|
};
|
|
311
309
|
case COSECRV.SECP256K1: return {
|
|
312
|
-
curve:
|
|
310
|
+
curve: _noble_curves_secp256k1_js.secp256k1,
|
|
313
311
|
hash: _noble_hashes_sha2_js.sha256,
|
|
314
312
|
componentLen: 32
|
|
315
313
|
};
|
|
@@ -332,8 +330,11 @@ async function verifySignature(opts) {
|
|
|
332
330
|
pubKeyBytes.set(y.length <= componentLen ? y : y.slice(y.length - componentLen), 1 + componentLen + (componentLen - y.length));
|
|
333
331
|
const msgHash = hash(data);
|
|
334
332
|
try {
|
|
335
|
-
|
|
336
|
-
|
|
333
|
+
return curve.verify(signature, msgHash, pubKeyBytes, {
|
|
334
|
+
prehash: false,
|
|
335
|
+
format: "der",
|
|
336
|
+
lowS: false
|
|
337
|
+
});
|
|
337
338
|
} catch {
|
|
338
339
|
return false;
|
|
339
340
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/mcrypto",
|
|
3
|
-
"version": "1.29.
|
|
3
|
+
"version": "1.29.25",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Crypto lib that provides signer,crypter,hasher interface",
|
|
6
6
|
"keywords": [
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"esm"
|
|
49
49
|
],
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@ocap/e2e-test": "1.29.
|
|
51
|
+
"@ocap/e2e-test": "1.29.25",
|
|
52
52
|
"@types/crypto-js": "^4.2.2",
|
|
53
53
|
"@types/node": "^22.7.5",
|
|
54
54
|
"@types/randombytes": "^2.0.3",
|
|
@@ -73,10 +73,10 @@
|
|
|
73
73
|
"url": "https://github.com/ArcBlock/blockchain/issues"
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
|
-
"@noble/curves": "^
|
|
76
|
+
"@noble/curves": "^2.0.1",
|
|
77
77
|
"@noble/ed25519": "^3.0.0",
|
|
78
78
|
"@noble/hashes": "^2.0.1",
|
|
79
|
-
"@ocap/util": "1.29.
|
|
79
|
+
"@ocap/util": "1.29.25",
|
|
80
80
|
"bn.js": "5.2.3",
|
|
81
81
|
"crypto-js": "^4.2.0",
|
|
82
82
|
"interface": "^1.2.1",
|