@ocap/mcrypto 1.30.23 → 1.30.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/crypter/aes-legacy.d.mts +11 -0
- package/esm/crypter/aes-legacy.mjs +67 -15
- package/esm/crypter/aes.d.mts +14 -2
- package/esm/crypter/aes.mjs +69 -3
- package/esm/encode.mjs +1 -1
- package/esm/hasher/keccak.mjs +7 -2
- package/esm/hasher/sha2.d.mts +1 -1
- package/esm/hasher/sha2.mjs +26 -11
- package/esm/hasher/sha3.d.mts +1 -1
- package/esm/hasher/sha3.mjs +21 -6
- package/esm/index.d.mts +1 -1
- package/esm/signer/ed25519.d.mts +1 -1
- package/esm/signer/ed25519.mjs +143 -16
- package/esm/signer/ethereum.mjs +7 -3
- package/esm/signer/secp256k1.mjs +3 -2
- package/esm/webauthn.d.mts +68 -2
- package/esm/webauthn.mjs +64 -16
- package/lib/_vendor/@noble/curves/LICENSE +21 -0
- package/lib/_vendor/@noble/curves/abstract/curve.cjs +373 -0
- package/lib/_vendor/@noble/curves/abstract/modular.cjs +616 -0
- package/lib/_vendor/@noble/curves/abstract/weierstrass.cjs +1020 -0
- package/lib/_vendor/@noble/curves/nist.cjs +91 -0
- package/lib/_vendor/@noble/curves/secp256k1.cjs +73 -0
- package/lib/_vendor/@noble/curves/utils.cjs +463 -0
- package/lib/_vendor/@noble/ed25519/LICENSE +21 -0
- package/lib/_vendor/@noble/ed25519/index.cjs +512 -0
- package/lib/_vendor/@noble/hashes/LICENSE +21 -0
- package/lib/_vendor/@noble/hashes/_md.cjs +230 -0
- package/lib/_vendor/@noble/hashes/_u64.cjs +67 -0
- package/lib/_vendor/@noble/hashes/hmac.cjs +86 -0
- package/lib/_vendor/@noble/hashes/sha2.cjs +477 -0
- package/lib/_vendor/@noble/hashes/sha3.cjs +329 -0
- package/lib/_vendor/@noble/hashes/utils.cjs +410 -0
- package/lib/crypter/aes-legacy.cjs +68 -21
- package/lib/crypter/aes-legacy.d.cts +11 -0
- package/lib/crypter/aes.cjs +69 -2
- package/lib/crypter/aes.d.cts +14 -2
- package/lib/encode.cjs +1 -1
- package/lib/hasher/keccak.cjs +13 -8
- package/lib/hasher/sha2.cjs +32 -17
- package/lib/hasher/sha2.d.cts +1 -1
- package/lib/hasher/sha3.cjs +27 -12
- package/lib/hasher/sha3.d.cts +1 -1
- package/lib/index.d.cts +1 -1
- package/lib/signer/ed25519.cjs +146 -20
- package/lib/signer/ed25519.d.cts +1 -1
- package/lib/signer/ethereum.cjs +16 -12
- package/lib/signer/secp256k1.cjs +11 -10
- package/lib/webauthn.cjs +80 -31
- package/lib/webauthn.d.cts +68 -2
- package/package.json +6 -7
package/lib/hasher/keccak.cjs
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
3
|
const require_encode = require('../encode.cjs');
|
|
4
|
+
const require_utils = require('../_vendor/@noble/hashes/utils.cjs');
|
|
5
|
+
const require_sha3 = require('../_vendor/@noble/hashes/sha3.cjs');
|
|
4
6
|
let _ocap_util = require("@ocap/util");
|
|
5
|
-
let _noble_hashes_sha3_js = require("@noble/hashes/sha3.js");
|
|
6
|
-
let _noble_hashes_utils_js = require("@noble/hashes/utils.js");
|
|
7
7
|
|
|
8
8
|
//#region src/hasher/keccak.ts
|
|
9
9
|
const hashFns = {
|
|
10
|
-
keccak_224:
|
|
11
|
-
keccak_256:
|
|
12
|
-
keccak_384:
|
|
13
|
-
keccak_512:
|
|
10
|
+
keccak_224: require_sha3.keccak_224,
|
|
11
|
+
keccak_256: require_sha3.keccak_256,
|
|
12
|
+
keccak_384: require_sha3.keccak_384,
|
|
13
|
+
keccak_512: require_sha3.keccak_512
|
|
14
14
|
};
|
|
15
15
|
/**
|
|
16
16
|
* Keccak support with different hash length
|
|
@@ -34,12 +34,17 @@ var KeccakHasher = class {
|
|
|
34
34
|
};
|
|
35
35
|
this[name] = (data, round = 1, encoding = "hex") => {
|
|
36
36
|
let input;
|
|
37
|
-
|
|
37
|
+
if (data instanceof Uint8Array) input = data;
|
|
38
|
+
else try {
|
|
38
39
|
input = (0, _ocap_util.toUint8Array)(data);
|
|
39
40
|
} catch (_err) {
|
|
40
41
|
input = data;
|
|
41
42
|
}
|
|
42
|
-
|
|
43
|
+
const res = hashFn(input, round);
|
|
44
|
+
if (encoding === "hex" || encoding === "base16") return `0x${require_utils.bytesToHex(res)}`;
|
|
45
|
+
if (encoding === "buffer" && typeof Buffer !== "undefined") return Buffer.from(res.buffer, res.byteOffset, res.byteLength);
|
|
46
|
+
if (encoding === "Uint8Array") return res;
|
|
47
|
+
return require_encode.encode(res, encoding);
|
|
43
48
|
};
|
|
44
49
|
});
|
|
45
50
|
}
|
package/lib/hasher/sha2.cjs
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
3
|
const require_encode = require('../encode.cjs');
|
|
4
|
+
const require_utils = require('../_vendor/@noble/hashes/utils.cjs');
|
|
5
|
+
const require_sha2 = require('../_vendor/@noble/hashes/sha2.cjs');
|
|
4
6
|
let _ocap_util = require("@ocap/util");
|
|
5
|
-
let _noble_hashes_utils_js = require("@noble/hashes/utils.js");
|
|
6
|
-
let _noble_hashes_sha2_js = require("@noble/hashes/sha2.js");
|
|
7
7
|
|
|
8
8
|
//#region src/hasher/sha2.ts
|
|
9
9
|
const hashFns = {
|
|
10
|
-
sha224:
|
|
11
|
-
sha256:
|
|
12
|
-
sha384:
|
|
13
|
-
sha512:
|
|
10
|
+
sha224: require_sha2.sha224,
|
|
11
|
+
sha256: require_sha2.sha256,
|
|
12
|
+
sha384: require_sha2.sha384,
|
|
13
|
+
sha512: require_sha2.sha512
|
|
14
14
|
};
|
|
15
|
+
const createHash = (typeof process !== "undefined" ? process.getBuiltinModule?.("node:crypto") : void 0)?.createHash;
|
|
15
16
|
/**
|
|
16
17
|
* Sha2 support with different hash length
|
|
17
|
-
* Uses @noble/hashes
|
|
18
|
+
* Uses native node:crypto when available with @noble/hashes fallback
|
|
18
19
|
*
|
|
19
20
|
* @class
|
|
20
21
|
*/
|
|
@@ -27,19 +28,33 @@ var Sha2Hasher = class {
|
|
|
27
28
|
512
|
|
28
29
|
].forEach((x) => {
|
|
29
30
|
const name = `hash${x}`;
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
const alg = `sha${x}`;
|
|
32
|
+
const hasher = hashFns[alg];
|
|
33
|
+
const hashBytes = (input, round) => {
|
|
34
|
+
const rounds = Math.max(1, round | 0);
|
|
35
|
+
if (createHash) {
|
|
36
|
+
let res$1 = input;
|
|
37
|
+
for (let i = 0; i < rounds; i++) res$1 = createHash(alg).update(res$1).digest();
|
|
38
|
+
return res$1;
|
|
37
39
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
let res = input;
|
|
41
|
+
for (let i = 0; i < rounds; i++) res = hasher(res);
|
|
42
|
+
return res;
|
|
40
43
|
};
|
|
41
44
|
this[name] = (data, round = 2, encoding = "hex") => {
|
|
42
|
-
|
|
45
|
+
let inputBytes;
|
|
46
|
+
if (data instanceof Uint8Array) inputBytes = data;
|
|
47
|
+
else try {
|
|
48
|
+
inputBytes = (0, _ocap_util.toUint8Array)(data);
|
|
49
|
+
} catch (_err) {
|
|
50
|
+
inputBytes = data;
|
|
51
|
+
}
|
|
52
|
+
const res = hashBytes(inputBytes, round);
|
|
53
|
+
const isBuf = typeof Buffer !== "undefined" && Buffer.isBuffer(res);
|
|
54
|
+
if (encoding === "hex" || encoding === "base16") return isBuf ? `0x${res.toString("hex")}` : `0x${require_utils.bytesToHex(res)}`;
|
|
55
|
+
if (encoding === "buffer" && typeof Buffer !== "undefined") return isBuf ? res : Buffer.from(res.buffer, res.byteOffset, res.byteLength);
|
|
56
|
+
if (encoding === "Uint8Array") return isBuf ? new Uint8Array(res) : res;
|
|
57
|
+
return require_encode.encode(res, encoding);
|
|
43
58
|
};
|
|
44
59
|
});
|
|
45
60
|
}
|
package/lib/hasher/sha2.d.cts
CHANGED
package/lib/hasher/sha3.cjs
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
3
|
const require_encode = require('../encode.cjs');
|
|
4
|
+
const require_utils = require('../_vendor/@noble/hashes/utils.cjs');
|
|
5
|
+
const require_sha3 = require('../_vendor/@noble/hashes/sha3.cjs');
|
|
4
6
|
let _ocap_util = require("@ocap/util");
|
|
5
|
-
let _noble_hashes_sha3_js = require("@noble/hashes/sha3.js");
|
|
6
|
-
let _noble_hashes_utils_js = require("@noble/hashes/utils.js");
|
|
7
7
|
|
|
8
8
|
//#region src/hasher/sha3.ts
|
|
9
9
|
const hashFns = {
|
|
10
|
-
sha3_224:
|
|
11
|
-
sha3_256:
|
|
12
|
-
sha3_384:
|
|
13
|
-
sha3_512:
|
|
10
|
+
sha3_224: require_sha3.sha3_224,
|
|
11
|
+
sha3_256: require_sha3.sha3_256,
|
|
12
|
+
sha3_384: require_sha3.sha3_384,
|
|
13
|
+
sha3_512: require_sha3.sha3_512
|
|
14
14
|
};
|
|
15
|
+
const createHash = (typeof process !== "undefined" ? process.getBuiltinModule?.("node:crypto") : void 0)?.createHash;
|
|
15
16
|
/**
|
|
16
17
|
* Sha3 support with different hash length
|
|
17
|
-
* Uses @noble/hashes
|
|
18
|
+
* Uses native node:crypto when available with @noble/hashes fallback
|
|
18
19
|
*
|
|
19
20
|
* @class Sha3Hasher
|
|
20
21
|
*/
|
|
@@ -27,19 +28,33 @@ var Sha3Hasher = class {
|
|
|
27
28
|
512
|
|
28
29
|
].forEach((x) => {
|
|
29
30
|
const name = `hash${x}`;
|
|
31
|
+
const alg = `sha3-${x}`;
|
|
30
32
|
const hasher = hashFns[`sha3_${x}`];
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
const hashBytes = (input, round) => {
|
|
34
|
+
const rounds = Math.max(1, round | 0);
|
|
35
|
+
if (createHash) {
|
|
36
|
+
let res$1 = input;
|
|
37
|
+
for (let i = 0; i < rounds; i++) res$1 = createHash(alg).update(res$1).digest();
|
|
38
|
+
return res$1;
|
|
39
|
+
}
|
|
40
|
+
let res = input;
|
|
41
|
+
for (let i = 0; i < rounds; i++) res = hasher(res);
|
|
42
|
+
return res;
|
|
34
43
|
};
|
|
35
44
|
this[name] = (data, round = 1, encoding = "hex") => {
|
|
36
45
|
let input;
|
|
37
|
-
|
|
46
|
+
if (data instanceof Uint8Array) input = data;
|
|
47
|
+
else try {
|
|
38
48
|
input = (0, _ocap_util.toUint8Array)(data);
|
|
39
49
|
} catch (_err) {
|
|
40
50
|
input = data;
|
|
41
51
|
}
|
|
42
|
-
|
|
52
|
+
const res = hashBytes(input, round);
|
|
53
|
+
const isBuf = typeof Buffer !== "undefined" && Buffer.isBuffer(res);
|
|
54
|
+
if (encoding === "hex" || encoding === "base16") return isBuf ? `0x${res.toString("hex")}` : `0x${require_utils.bytesToHex(res)}`;
|
|
55
|
+
if (encoding === "buffer" && typeof Buffer !== "undefined") return isBuf ? res : Buffer.from(res.buffer, res.byteOffset, res.byteLength);
|
|
56
|
+
if (encoding === "Uint8Array") return isBuf ? new Uint8Array(res) : res;
|
|
57
|
+
return require_encode.encode(res, encoding);
|
|
43
58
|
};
|
|
44
59
|
});
|
|
45
60
|
}
|
package/lib/hasher/sha3.d.cts
CHANGED
package/lib/index.d.cts
CHANGED
|
@@ -10,7 +10,7 @@ import { BytesType, EncodingType, KeyPairType } from "@ocap/util";
|
|
|
10
10
|
import { LiteralUnion } from "type-fest";
|
|
11
11
|
|
|
12
12
|
//#region src/index.d.ts
|
|
13
|
-
type KeyType = LiteralUnion<'ED25519' | 'SECP256K1' | 'ETHEREUM', string>;
|
|
13
|
+
type KeyType = LiteralUnion<'ED25519' | 'SECP256K1' | 'ETHEREUM' | 'PASSKEY', string>;
|
|
14
14
|
type HashType = LiteralUnion<'KECCAK' | 'SHA3' | 'KECCAK_384' | 'SHA3_384' | 'KECCAK_512' | 'SHA3_512' | 'SHA2', string>;
|
|
15
15
|
type RoleType = LiteralUnion<'ROLE_ACCOUNT' | 'ROLE_NODE' | 'ROLE_DEVICE' | 'ROLE_APPLICATION' | 'ROLE_SMART_CONTRACT' | 'ROLE_BOT' | 'ROLE_ASSET' | 'ROLE_STAKE' | 'ROLE_VALIDATOR' | 'ROLE_GROUP' | 'ROLE_TX' | 'ROLE_TETHER' | 'ROLE_SWAP' | 'ROLE_DELEGATION' | 'ROLE_VC' | 'ROLE_BLOCKLET' | 'ROLE_STORE' | 'ROLE_TOKEN' | 'ROLE_FACTORY' | 'ROLE_ROLLUP' | 'ROLE_STORAGE' | 'ROLE_PROFILE' | 'ROLE_PASSKEY' | 'ROLE_TOKEN_FACTORY' | 'ROLE_PROVIDER' | 'ROLE_ANY', string>;
|
|
16
16
|
type AddressType = LiteralUnion<'BASE16' | 'BASE58', string>;
|
package/lib/signer/ed25519.cjs
CHANGED
|
@@ -1,18 +1,124 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
3
|
const require_encode = require('../encode.cjs');
|
|
4
|
+
const require_utils = require('../_vendor/@noble/hashes/utils.cjs');
|
|
5
|
+
const require_sha2 = require('../_vendor/@noble/hashes/sha2.cjs');
|
|
6
|
+
const require_index = require('../_vendor/@noble/ed25519/index.cjs');
|
|
4
7
|
const require_protocols_signer = require('../protocols/signer.cjs');
|
|
5
8
|
let _ocap_util = require("@ocap/util");
|
|
6
9
|
let randombytes = require("randombytes");
|
|
7
10
|
randombytes = require_rolldown_runtime.__toESM(randombytes);
|
|
8
|
-
let _noble_hashes_sha2_js = require("@noble/hashes/sha2.js");
|
|
9
|
-
let _noble_ed25519 = require("@noble/ed25519");
|
|
10
|
-
_noble_ed25519 = require_rolldown_runtime.__toESM(_noble_ed25519);
|
|
11
11
|
|
|
12
12
|
//#region src/signer/ed25519.ts
|
|
13
|
-
|
|
13
|
+
const nodeCrypto = typeof process !== "undefined" ? process.getBuiltinModule?.("node:crypto") : void 0;
|
|
14
|
+
require_index.hashes.sha512 = nodeCrypto?.createHash ? (msg) => new Uint8Array(nodeCrypto.createHash("sha512").update(msg).digest()) : (msg) => require_sha2.sha512(msg);
|
|
15
|
+
const ED25519_PKCS8_PREFIX = new Uint8Array([
|
|
16
|
+
48,
|
|
17
|
+
46,
|
|
18
|
+
2,
|
|
19
|
+
1,
|
|
20
|
+
0,
|
|
21
|
+
48,
|
|
22
|
+
5,
|
|
23
|
+
6,
|
|
24
|
+
3,
|
|
25
|
+
43,
|
|
26
|
+
101,
|
|
27
|
+
112,
|
|
28
|
+
4,
|
|
29
|
+
34,
|
|
30
|
+
4,
|
|
31
|
+
32
|
|
32
|
+
]);
|
|
33
|
+
const ED25519_SPKI_PREFIX = new Uint8Array([
|
|
34
|
+
48,
|
|
35
|
+
42,
|
|
36
|
+
48,
|
|
37
|
+
5,
|
|
38
|
+
6,
|
|
39
|
+
3,
|
|
40
|
+
43,
|
|
41
|
+
101,
|
|
42
|
+
112,
|
|
43
|
+
3,
|
|
44
|
+
33,
|
|
45
|
+
0
|
|
46
|
+
]);
|
|
47
|
+
const MAX_KEY_CACHE_SIZE = 512;
|
|
48
|
+
const privateKeyCache = /* @__PURE__ */ new Map();
|
|
49
|
+
const publicKeyCache = /* @__PURE__ */ new Map();
|
|
50
|
+
function getNativePrivateKeyEntry(seed) {
|
|
51
|
+
if (typeof Buffer === "undefined" || !nodeCrypto?.createPrivateKey || !nodeCrypto?.createPublicKey || seed.byteLength !== 32) return null;
|
|
52
|
+
const cacheKey = Buffer.isBuffer(seed) ? seed.toString("hex") : require_utils.bytesToHex(seed);
|
|
53
|
+
const cached = privateKeyCache.get(cacheKey);
|
|
54
|
+
if (cached) {
|
|
55
|
+
privateKeyCache.delete(cacheKey);
|
|
56
|
+
privateKeyCache.set(cacheKey, cached);
|
|
57
|
+
return cached;
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
const der = Buffer.allocUnsafe(48);
|
|
61
|
+
der.set(ED25519_PKCS8_PREFIX, 0);
|
|
62
|
+
der.set(seed, 16);
|
|
63
|
+
const keyObject = nodeCrypto.createPrivateKey({
|
|
64
|
+
key: der,
|
|
65
|
+
format: "der",
|
|
66
|
+
type: "pkcs8"
|
|
67
|
+
});
|
|
68
|
+
const spki = nodeCrypto.createPublicKey(keyObject).export({
|
|
69
|
+
format: "der",
|
|
70
|
+
type: "spki"
|
|
71
|
+
});
|
|
72
|
+
const entry = {
|
|
73
|
+
keyObject,
|
|
74
|
+
publicKey: new Uint8Array(spki.buffer, spki.byteOffset + spki.byteLength - 32, 32).slice()
|
|
75
|
+
};
|
|
76
|
+
if (privateKeyCache.size >= MAX_KEY_CACHE_SIZE) {
|
|
77
|
+
const oldestKey = privateKeyCache.keys().next().value;
|
|
78
|
+
if (oldestKey !== void 0) privateKeyCache.delete(oldestKey);
|
|
79
|
+
}
|
|
80
|
+
privateKeyCache.set(cacheKey, entry);
|
|
81
|
+
return entry;
|
|
82
|
+
} catch {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function getNativePublicKeyObject(pkBytes) {
|
|
87
|
+
if (typeof Buffer === "undefined" || !nodeCrypto?.createPublicKey || pkBytes.byteLength !== 32) return null;
|
|
88
|
+
const cacheKey = Buffer.isBuffer(pkBytes) ? pkBytes.toString("hex") : require_utils.bytesToHex(pkBytes);
|
|
89
|
+
const cached = publicKeyCache.get(cacheKey);
|
|
90
|
+
if (cached) {
|
|
91
|
+
publicKeyCache.delete(cacheKey);
|
|
92
|
+
publicKeyCache.set(cacheKey, cached);
|
|
93
|
+
return cached;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
const der = Buffer.allocUnsafe(44);
|
|
97
|
+
der.set(ED25519_SPKI_PREFIX, 0);
|
|
98
|
+
der.set(pkBytes, 12);
|
|
99
|
+
const keyObject = nodeCrypto.createPublicKey({
|
|
100
|
+
key: der,
|
|
101
|
+
format: "der",
|
|
102
|
+
type: "spki"
|
|
103
|
+
});
|
|
104
|
+
if (publicKeyCache.size >= MAX_KEY_CACHE_SIZE) {
|
|
105
|
+
const oldestKey = publicKeyCache.keys().next().value;
|
|
106
|
+
if (oldestKey !== void 0) publicKeyCache.delete(oldestKey);
|
|
107
|
+
}
|
|
108
|
+
publicKeyCache.set(cacheKey, keyObject);
|
|
109
|
+
return keyObject;
|
|
110
|
+
} catch {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const toBytes = (data) => data instanceof Uint8Array ? data : (0, _ocap_util.toUint8Array)(data);
|
|
115
|
+
function derivePublicKeyBytes(seed) {
|
|
116
|
+
const nativeEntry = getNativePrivateKeyEntry(seed);
|
|
117
|
+
if (nativeEntry) return nativeEntry.publicKey;
|
|
118
|
+
return require_index.getPublicKey(seed);
|
|
119
|
+
}
|
|
14
120
|
/**
|
|
15
|
-
* Signer implementation for ed25519,
|
|
121
|
+
* Signer implementation for ed25519, using native `node:crypto` with `@noble/ed25519` fallback
|
|
16
122
|
*
|
|
17
123
|
* @class Ed25519Signer
|
|
18
124
|
*/
|
|
@@ -33,9 +139,9 @@ var Ed25519Signer = class extends require_protocols_signer.default {
|
|
|
33
139
|
* @memberof Ed25519Signer
|
|
34
140
|
*/
|
|
35
141
|
genKeyPair(encoding = "hex", userSeed) {
|
|
36
|
-
const seed = userSeed ? (
|
|
142
|
+
const seed = userSeed ? toBytes(userSeed) : new Uint8Array((0, randombytes.default)(32));
|
|
37
143
|
if (seed.byteLength !== 32) throw new Error("Invalid seed to generate key pair");
|
|
38
|
-
const publicKey =
|
|
144
|
+
const publicKey = derivePublicKeyBytes(seed);
|
|
39
145
|
const secretKey = new Uint8Array(64);
|
|
40
146
|
secretKey.set(seed);
|
|
41
147
|
secretKey.set(publicKey, 32);
|
|
@@ -54,11 +160,10 @@ var Ed25519Signer = class extends require_protocols_signer.default {
|
|
|
54
160
|
* @throws {Error} if the key is not 64 bytes
|
|
55
161
|
*/
|
|
56
162
|
validateKeyPair(sk) {
|
|
57
|
-
const skBytes = (
|
|
163
|
+
const skBytes = toBytes(sk);
|
|
58
164
|
if (skBytes.byteLength !== 64) throw new Error("validateKeyPair requires a 64-byte key");
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
const embeddedPk = skBytes.slice(32, 64);
|
|
165
|
+
const derivedPk = derivePublicKeyBytes(skBytes.subarray(0, 32));
|
|
166
|
+
const embeddedPk = skBytes.subarray(32, 64);
|
|
62
167
|
return derivedPk.length === embeddedPk.length && derivedPk.every((b, i) => b === embeddedPk[i]);
|
|
63
168
|
}
|
|
64
169
|
/**
|
|
@@ -68,9 +173,9 @@ var Ed25519Signer = class extends require_protocols_signer.default {
|
|
|
68
173
|
* @returns {string} hex encoded publicKey
|
|
69
174
|
*/
|
|
70
175
|
getPublicKey(sk, encoding = "hex") {
|
|
71
|
-
const skBytes = (
|
|
176
|
+
const skBytes = toBytes(sk);
|
|
72
177
|
if (skBytes.byteLength === 64) return require_encode.encode(skBytes.slice(32, 64), encoding);
|
|
73
|
-
return require_encode.encode(
|
|
178
|
+
return require_encode.encode(derivePublicKeyBytes(skBytes), encoding);
|
|
74
179
|
}
|
|
75
180
|
/**
|
|
76
181
|
* Sign a message and get the signature hex
|
|
@@ -80,9 +185,19 @@ var Ed25519Signer = class extends require_protocols_signer.default {
|
|
|
80
185
|
* @returns {string} hex encoded signature
|
|
81
186
|
*/
|
|
82
187
|
sign(message, sk, encoding = "hex") {
|
|
83
|
-
const seed = (
|
|
84
|
-
const messageBytes = (
|
|
85
|
-
|
|
188
|
+
const seed = toBytes(sk).subarray(0, 32);
|
|
189
|
+
const messageBytes = toBytes(message);
|
|
190
|
+
if (nodeCrypto?.sign && seed.byteLength === 32) {
|
|
191
|
+
const entry = getNativePrivateKeyEntry(seed);
|
|
192
|
+
if (entry) {
|
|
193
|
+
const sigBuf = nodeCrypto.sign(null, messageBytes, entry.keyObject);
|
|
194
|
+
if (encoding === "hex" || encoding === "base16") return `0x${sigBuf.toString("hex")}`;
|
|
195
|
+
if (encoding === "buffer") return sigBuf;
|
|
196
|
+
if (encoding === "Uint8Array") return new Uint8Array(sigBuf);
|
|
197
|
+
return require_encode.encode(sigBuf, encoding);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return require_encode.encode(require_index.sign(messageBytes, seed), encoding);
|
|
86
201
|
}
|
|
87
202
|
/**
|
|
88
203
|
* Verify if a signature is valid
|
|
@@ -93,10 +208,21 @@ var Ed25519Signer = class extends require_protocols_signer.default {
|
|
|
93
208
|
* @returns {bool}
|
|
94
209
|
*/
|
|
95
210
|
verify(message, signature, pk) {
|
|
96
|
-
const pkBytes = (
|
|
97
|
-
const messageBytes = (
|
|
98
|
-
const signatureBytes = (
|
|
99
|
-
|
|
211
|
+
const pkBytes = toBytes(pk);
|
|
212
|
+
const messageBytes = toBytes(message);
|
|
213
|
+
const signatureBytes = toBytes(signature);
|
|
214
|
+
if (pkBytes.byteLength !== 32 || signatureBytes.byteLength !== 64) return false;
|
|
215
|
+
if (nodeCrypto?.verify) {
|
|
216
|
+
const pubKeyObject = getNativePublicKeyObject(pkBytes);
|
|
217
|
+
if (pubKeyObject) try {
|
|
218
|
+
if (nodeCrypto.verify(null, messageBytes, pubKeyObject, signatureBytes)) return true;
|
|
219
|
+
} catch {}
|
|
220
|
+
}
|
|
221
|
+
try {
|
|
222
|
+
return require_index.verify(signatureBytes, messageBytes, pkBytes);
|
|
223
|
+
} catch {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
100
226
|
}
|
|
101
227
|
};
|
|
102
228
|
var ed25519_default = new Ed25519Signer();
|
package/lib/signer/ed25519.d.cts
CHANGED
|
@@ -4,7 +4,7 @@ import { BytesType, EncodingType, KeyPairType } from "@ocap/util";
|
|
|
4
4
|
//#region src/signer/ed25519.d.ts
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Signer implementation for ed25519,
|
|
7
|
+
* Signer implementation for ed25519, using native `node:crypto` with `@noble/ed25519` fallback
|
|
8
8
|
*
|
|
9
9
|
* @class Ed25519Signer
|
|
10
10
|
*/
|
package/lib/signer/ethereum.cjs
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
|
+
const require_utils = require('../_vendor/@noble/hashes/utils.cjs');
|
|
4
|
+
const require_sha3 = require('../_vendor/@noble/hashes/sha3.cjs');
|
|
5
|
+
const require_secp256k1 = require('../_vendor/@noble/curves/secp256k1.cjs');
|
|
3
6
|
const require_signer_secp256k1 = require('./secp256k1.cjs');
|
|
4
7
|
let _ocap_util = require("@ocap/util");
|
|
5
|
-
let _noble_hashes_sha3_js = require("@noble/hashes/sha3.js");
|
|
6
|
-
let _noble_hashes_utils_js = require("@noble/hashes/utils.js");
|
|
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) {
|
|
11
11
|
const addr = address.slice(2).toLowerCase();
|
|
12
|
-
const hash =
|
|
12
|
+
const hash = require_utils.bytesToHex(require_sha3.keccak_256(Buffer.from(addr)));
|
|
13
13
|
let result = "0x";
|
|
14
14
|
for (let i = 0; i < 40; i++) result += Number.parseInt(hash[i], 16) > 7 ? addr[i].toUpperCase() : addr[i];
|
|
15
15
|
return result;
|
|
@@ -28,27 +28,31 @@ var EthereumSigner = class extends require_signer_secp256k1.Secp256k1Signer {
|
|
|
28
28
|
const messageBytes = (0, _ocap_util.hexToBytes)((0, _ocap_util.isHexStrict)(data) ? data : (0, _ocap_util.utf8ToHex)(data));
|
|
29
29
|
const preamble = `\x19Ethereum Signed Message:\n${messageBytes.length}`;
|
|
30
30
|
const ethMessage = Buffer.concat([Buffer.from(preamble), Buffer.from(messageBytes)]);
|
|
31
|
-
return `0x${
|
|
31
|
+
return `0x${require_utils.bytesToHex(require_sha3.keccak_256(new Uint8Array(ethMessage)))}`;
|
|
32
32
|
}
|
|
33
33
|
ethSign(data, privateKey) {
|
|
34
34
|
const msgHash = new Uint8Array(Buffer.from(data.slice(2), "hex"));
|
|
35
35
|
const sk = new Uint8Array(Buffer.from(privateKey.slice(2), "hex"));
|
|
36
|
-
const recoveredBytes =
|
|
36
|
+
const recoveredBytes = require_secp256k1.secp256k1.sign(msgHash, sk, {
|
|
37
37
|
prehash: false,
|
|
38
38
|
format: "recovered"
|
|
39
39
|
});
|
|
40
|
-
const sig =
|
|
40
|
+
const sig = require_secp256k1.secp256k1.Signature.fromBytes(recoveredBytes, "recovered");
|
|
41
41
|
const v = (27 + sig.recovery).toString(16).padStart(2, "0");
|
|
42
42
|
return `0x${sig.toHex("compact")}${v}`;
|
|
43
43
|
}
|
|
44
44
|
ethRecover(data, signature) {
|
|
45
|
-
|
|
45
|
+
if (typeof data !== "string" || typeof signature !== "string") throw new Error("Invalid ethereum signature or data");
|
|
46
|
+
const sigHex = signature.replace(/^0x/i, "");
|
|
47
|
+
if (sigHex.length !== 130) throw new Error("Invalid ethereum signature length");
|
|
46
48
|
const v = Number.parseInt(sigHex.slice(128, 130), 16);
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
const
|
|
49
|
+
if (v !== 0 && v !== 1 && v !== 27 && v !== 28) throw new Error("Invalid ethereum signature recovery parameter");
|
|
50
|
+
const recovery = v < 2 ? v : v - 27;
|
|
51
|
+
const sig = require_secp256k1.secp256k1.Signature.fromHex(sigHex.slice(0, 128), "compact").addRecoveryBit(recovery);
|
|
52
|
+
if (sig.hasHighS()) throw new Error("Invalid ethereum high-S signature");
|
|
53
|
+
const msgHash = new Uint8Array(Buffer.from(data.replace(/^0x/i, ""), "hex"));
|
|
50
54
|
const pubKeyHex = sig.recoverPublicKey(msgHash).toHex(false).slice(2);
|
|
51
|
-
return toChecksumAddress(`0x${
|
|
55
|
+
return toChecksumAddress(`0x${require_utils.bytesToHex(require_sha3.keccak_256(Buffer.from(pubKeyHex, "hex"))).slice(-40)}`);
|
|
52
56
|
}
|
|
53
57
|
};
|
|
54
58
|
var ethereum_default = new EthereumSigner();
|
package/lib/signer/secp256k1.cjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
3
|
const require_encode = require('../encode.cjs');
|
|
4
|
+
const require_utils = require('../_vendor/@noble/hashes/utils.cjs');
|
|
4
5
|
const require_protocols_signer = require('../protocols/signer.cjs');
|
|
6
|
+
const require_secp256k1 = require('../_vendor/@noble/curves/secp256k1.cjs');
|
|
5
7
|
let _ocap_util = require("@ocap/util");
|
|
6
8
|
let randombytes = require("randombytes");
|
|
7
9
|
randombytes = require_rolldown_runtime.__toESM(randombytes);
|
|
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 require_secp256k1.secp256k1.utils.isValidSecretKey(sk);
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
28
|
* @public
|
|
@@ -50,8 +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
|
-
const pkBytes =
|
|
54
|
-
let pk =
|
|
53
|
+
const pkBytes = require_secp256k1.secp256k1.getPublicKey((0, _ocap_util.toUint8Array)(sk), this.pkCompressed);
|
|
54
|
+
let pk = require_utils.bytesToHex(new Uint8Array(pkBytes));
|
|
55
55
|
if (this.pkHasFormatPrefix === false) pk = pk.slice(2);
|
|
56
56
|
return require_encode.encode(`0x${pk}`, encoding);
|
|
57
57
|
}
|
|
@@ -60,7 +60,7 @@ var Secp256k1Signer = class extends require_protocols_signer.default {
|
|
|
60
60
|
*/
|
|
61
61
|
sign(message, sk, encoding = "hex") {
|
|
62
62
|
const msg = (0, _ocap_util.toUint8Array)(message);
|
|
63
|
-
return require_encode.encode(`0x${
|
|
63
|
+
return require_encode.encode(`0x${require_utils.bytesToHex(require_secp256k1.secp256k1.sign(msg, (0, _ocap_util.toUint8Array)(sk), {
|
|
64
64
|
prehash: false,
|
|
65
65
|
format: "der"
|
|
66
66
|
}))}`, encoding);
|
|
@@ -70,17 +70,18 @@ var Secp256k1Signer = class extends require_protocols_signer.default {
|
|
|
70
70
|
*/
|
|
71
71
|
verify(message, signature, pk) {
|
|
72
72
|
const msg = (0, _ocap_util.toUint8Array)(message);
|
|
73
|
-
const sigHex = (0, _ocap_util.toHex)(signature).replace(/^0x/i, "");
|
|
73
|
+
const sigHex = (0, _ocap_util.toHex)((0, _ocap_util.toUint8Array)(signature)).replace(/^0x/i, "");
|
|
74
74
|
let pkBytes = (0, _ocap_util.toUint8Array)(pk);
|
|
75
|
-
if (this.pkHasFormatPrefix === false && pkBytes
|
|
75
|
+
if (this.pkHasFormatPrefix === false && pkBytes.byteLength === 64) {
|
|
76
76
|
const prefixed = new Uint8Array(65);
|
|
77
77
|
prefixed[0] = 4;
|
|
78
78
|
prefixed.set(pkBytes, 1);
|
|
79
79
|
pkBytes = prefixed;
|
|
80
80
|
}
|
|
81
81
|
try {
|
|
82
|
-
const sigBytes =
|
|
83
|
-
|
|
82
|
+
const sigBytes = require_secp256k1.secp256k1.Signature.fromHex(sigHex, "der").toBytes("der");
|
|
83
|
+
if (require_utils.bytesToHex(sigBytes) !== sigHex.toLowerCase()) return false;
|
|
84
|
+
return require_secp256k1.secp256k1.verify(sigBytes, msg, pkBytes, {
|
|
84
85
|
prehash: false,
|
|
85
86
|
format: "der",
|
|
86
87
|
lowS: true
|