@ocap/mpc-hsm 0.0.0-stage → 1.33.7

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.
Files changed (74) hide show
  1. package/esm/bip39-english-wordlist.d.mts +11 -0
  2. package/esm/bip39-english-wordlist.mjs +31 -0
  3. package/esm/constants.d.mts +140 -0
  4. package/esm/constants.mjs +142 -0
  5. package/esm/dkg.d.mts +279 -0
  6. package/esm/dkg.mjs +903 -0
  7. package/esm/index.d.mts +17 -0
  8. package/esm/index.mjs +18 -0
  9. package/esm/mta.d.mts +63 -0
  10. package/esm/mta.mjs +275 -0
  11. package/esm/ocap-clear-signing.d.mts +125 -0
  12. package/esm/ocap-clear-signing.mjs +1170 -0
  13. package/esm/offline-ceremony.d.mts +127 -0
  14. package/esm/offline-ceremony.mjs +481 -0
  15. package/esm/ot.d.mts +193 -0
  16. package/esm/ot.mjs +726 -0
  17. package/esm/personal-vault.d.mts +1292 -0
  18. package/esm/personal-vault.mjs +3615 -0
  19. package/esm/personal.d.mts +10 -0
  20. package/esm/personal.mjs +11 -0
  21. package/esm/presign.d.mts +242 -0
  22. package/esm/presign.mjs +951 -0
  23. package/esm/rlp-eip1559.d.mts +75 -0
  24. package/esm/rlp-eip1559.mjs +293 -0
  25. package/esm/secp256k1-scalar.d.mts +141 -0
  26. package/esm/secp256k1-scalar.mjs +474 -0
  27. package/esm/sign.d.mts +113 -0
  28. package/esm/sign.mjs +225 -0
  29. package/esm/steel-mnemonic.d.mts +55 -0
  30. package/esm/steel-mnemonic.mjs +247 -0
  31. package/esm/tweak.d.mts +100 -0
  32. package/esm/tweak.mjs +168 -0
  33. package/esm/ves-backup.d.mts +296 -0
  34. package/esm/ves-backup.mjs +1025 -0
  35. package/esm/x402-agent-guard.d.mts +111 -0
  36. package/esm/x402-agent-guard.mjs +780 -0
  37. package/lib/bip39-english-wordlist.cjs +33 -0
  38. package/lib/bip39-english-wordlist.d.cts +11 -0
  39. package/lib/constants.cjs +264 -0
  40. package/lib/constants.d.cts +140 -0
  41. package/lib/dkg.cjs +910 -0
  42. package/lib/dkg.d.cts +279 -0
  43. package/lib/index.cjs +420 -0
  44. package/lib/index.d.cts +17 -0
  45. package/lib/mta.cjs +280 -0
  46. package/lib/mta.d.cts +63 -0
  47. package/lib/ocap-clear-signing.cjs +1176 -0
  48. package/lib/ocap-clear-signing.d.cts +125 -0
  49. package/lib/offline-ceremony.cjs +487 -0
  50. package/lib/offline-ceremony.d.cts +127 -0
  51. package/lib/ot.cjs +744 -0
  52. package/lib/ot.d.cts +193 -0
  53. package/lib/personal-vault.cjs +3725 -0
  54. package/lib/personal-vault.d.cts +1292 -0
  55. package/lib/personal.cjs +338 -0
  56. package/lib/personal.d.cts +10 -0
  57. package/lib/presign.cjs +954 -0
  58. package/lib/presign.d.cts +242 -0
  59. package/lib/rlp-eip1559.cjs +303 -0
  60. package/lib/rlp-eip1559.d.cts +75 -0
  61. package/lib/secp256k1-scalar.cjs +527 -0
  62. package/lib/secp256k1-scalar.d.cts +141 -0
  63. package/lib/sign.cjs +233 -0
  64. package/lib/sign.d.cts +113 -0
  65. package/lib/steel-mnemonic.cjs +253 -0
  66. package/lib/steel-mnemonic.d.cts +55 -0
  67. package/lib/tweak.cjs +178 -0
  68. package/lib/tweak.d.cts +100 -0
  69. package/lib/ves-backup.cjs +1053 -0
  70. package/lib/ves-backup.d.cts +296 -0
  71. package/lib/x402-agent-guard.cjs +788 -0
  72. package/lib/x402-agent-guard.d.cts +111 -0
  73. package/package.json +80 -4
  74. package/README.md +0 -3
package/lib/tweak.cjs ADDED
@@ -0,0 +1,178 @@
1
+ const require_constants = require('./constants.cjs');
2
+ const require_secp256k1_scalar = require('./secp256k1-scalar.cjs');
3
+ let _noble_hashes_sha3_js = require("@noble/hashes/sha3.js");
4
+ let _noble_hashes_utils_js = require("@noble/hashes/utils.js");
5
+ let _noble_curves_secp256k1_js = require("@noble/curves/secp256k1.js");
6
+
7
+ //#region src/tweak.ts
8
+ const ETH_ADDRESS_RE = /^0x[0-9a-fA-F]{40}$/;
9
+ function toEip55ChecksumAddress(lowerHexWithoutPrefix) {
10
+ if (!/^[0-9a-f]{40}$/.test(lowerHexWithoutPrefix)) throw new require_constants.MpcProtocolError("INVALID_ETH_ADDRESS", "Expected 40 lowercase hex chars");
11
+ const digestHex = (0, _noble_hashes_utils_js.bytesToHex)((0, _noble_hashes_sha3_js.keccak_256)(require_secp256k1_scalar.utf8Bytes(lowerHexWithoutPrefix)));
12
+ let out = "0x";
13
+ for (let i = 0; i < 40; i++) {
14
+ const ch = lowerHexWithoutPrefix[i];
15
+ out += Number.parseInt(digestHex[i], 16) >= 8 ? ch.toUpperCase() : ch;
16
+ }
17
+ return out;
18
+ }
19
+ /**
20
+ * Validates an Ethereum `0x` address (rejects non-hex, all-uppercase, bad EIP-55 mixed-case
21
+ * checksums, and the zero address `0x0000...0000`) and returns canonical EIP-55 + lowercase forms.
22
+ */
23
+ function parseStrictEthAddress(address) {
24
+ if (typeof address !== "string" || !ETH_ADDRESS_RE.test(address)) throw new require_constants.MpcProtocolError("INVALID_ETH_ADDRESS", `Invalid Ethereum address format: ${String(address)}`);
25
+ const body = address.slice(2);
26
+ const lower = body.toLowerCase();
27
+ if (/^0+$/.test(lower)) throw new require_constants.MpcProtocolError("INVALID_ETH_ADDRESS", "Zero Ethereum address is forbidden");
28
+ const hasLower = /[a-f]/.test(body);
29
+ const hasUpper = /[A-F]/.test(body);
30
+ if (hasUpper && !hasLower) throw new require_constants.MpcProtocolError("INVALID_ETH_ADDRESS", "All-uppercase Ethereum address is invalid");
31
+ const checksumAddress = toEip55ChecksumAddress(lower);
32
+ if (hasUpper && hasLower && checksumAddress !== address) throw new require_constants.MpcProtocolError("INVALID_ETH_ADDRESS", "EIP-55 checksum mismatch");
33
+ return {
34
+ checksumAddress,
35
+ lowercaseAddress: `0x${lower}`,
36
+ addressBytes20: (0, _noble_hashes_utils_js.hexToBytes)(lower)
37
+ };
38
+ }
39
+ function pointToEthAddress(pointOrComp33) {
40
+ const pt = pointOrComp33 instanceof Uint8Array ? require_secp256k1_scalar.decodePointCompressed(pointOrComp33, false) : pointOrComp33;
41
+ if (pt.is0()) throw new require_constants.MpcProtocolError("INVALID_POINT", "Cannot derive Ethereum address from infinity");
42
+ pt.assertValidity();
43
+ const addrLower = (0, _noble_hashes_utils_js.bytesToHex)((0, _noble_hashes_sha3_js.keccak_256)(pt.toBytes(false).subarray(1, 65)).subarray(12, 32));
44
+ if (/^0+$/.test(addrLower)) throw new require_constants.MpcProtocolError("INVALID_ETH_ADDRESS", "Derived Ethereum address is zero");
45
+ return {
46
+ checksumAddress: toEip55ChecksumAddress(addrLower),
47
+ lowercaseAddress: `0x${addrLower}`
48
+ };
49
+ }
50
+ function assertAllowedOnlineRole(role) {
51
+ if (typeof role !== "string" || role.trim().length === 0) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "Role must be a non-empty string");
52
+ if (role === require_constants.FORBIDDEN_ONLINE_ROLE_ESCAPE_CANARY) throw new require_constants.MpcProtocolError("FORBIDDEN_ONLINE_ROLE", `Role "${require_constants.FORBIDDEN_ONLINE_ROLE_ESCAPE_CANARY}" is hard-banned in online MPC derivation and signing`);
53
+ }
54
+ /**
55
+ * Applies a non-hardened additive scalar tweak `P_child = P_root + t_tweak * G`,
56
+ * strictly rejecting `t_tweak == 0`, `t_tweak >= n`, or `P_child == O`.
57
+ */
58
+ function applyAdditiveTweak(rootPublicKey, tweakScalar, tweakMode, derivationMeta) {
59
+ const pRoot = rootPublicKey instanceof Uint8Array ? require_secp256k1_scalar.decodePointCompressed(rootPublicKey, false) : rootPublicKey;
60
+ if (pRoot.is0()) throw new require_constants.MpcProtocolError("INVALID_POINT", "Root public key cannot be O");
61
+ if (typeof tweakScalar !== "bigint" || tweakScalar <= 0n || tweakScalar >= require_constants.SECP256K1_N) throw new require_constants.MpcProtocolError("DEGENERATE_TWEAK", "Additive tweak scalar must satisfy 1 <= t_tweak < n");
62
+ const pChild = pRoot.add(require_secp256k1_scalar.mulBaseG(tweakScalar));
63
+ if (pChild.is0()) throw new require_constants.MpcProtocolError("DEGENERATE_TWEAK", "Derived child public key collapsed to point at infinity O");
64
+ const childPublicKeyComp33 = require_secp256k1_scalar.encodePointCompressed(pChild);
65
+ const tweakBytes32 = require_secp256k1_scalar.scalarToBytes32(tweakScalar);
66
+ const { checksumAddress, lowercaseAddress } = pointToEthAddress(pChild);
67
+ const derivationSpec = derivationMeta?.kind === "role-bound" ? {
68
+ tweakMode: "role-bound",
69
+ role: derivationMeta.role,
70
+ ethChainId: derivationMeta.ethChainId,
71
+ tweakLabel: derivationMeta.tweakLabel,
72
+ childPublicKeyComp33
73
+ } : derivationMeta?.kind === "rule-bound" ? {
74
+ tweakMode: "rule-bound",
75
+ controllerDid: derivationMeta.controllerDid,
76
+ ethChainId: derivationMeta.ethChainId,
77
+ ruleHash: derivationMeta.ruleHashBytes32,
78
+ childPublicKeyComp33
79
+ } : {
80
+ tweakMode: "untweaked",
81
+ childPublicKeyComp33
82
+ };
83
+ return {
84
+ tweakMode,
85
+ tweakScalar,
86
+ tweakBytes32,
87
+ tweakHex: `0x${(0, _noble_hashes_utils_js.bytesToHex)(tweakBytes32)}`,
88
+ childPoint: pChild,
89
+ childPublicKeyComp33,
90
+ ethAddress: checksumAddress,
91
+ ethAddressLower: lowercaseAddress,
92
+ derivationSpec
93
+ };
94
+ }
95
+ function deriveUntweakedChild(rootPublicKey) {
96
+ const pRoot = rootPublicKey instanceof Uint8Array ? require_secp256k1_scalar.decodePointCompressed(rootPublicKey, false) : rootPublicKey;
97
+ if (pRoot.is0()) throw new require_constants.MpcProtocolError("INVALID_POINT", "Root public key cannot be O");
98
+ const childPublicKeyComp33 = require_secp256k1_scalar.encodePointCompressed(pRoot);
99
+ const tweakBytes32 = new Uint8Array(32);
100
+ const { checksumAddress, lowercaseAddress } = pointToEthAddress(pRoot);
101
+ return {
102
+ tweakMode: "untweaked",
103
+ tweakScalar: 0n,
104
+ tweakBytes32,
105
+ tweakHex: `0x${(0, _noble_hashes_utils_js.bytesToHex)(tweakBytes32)}`,
106
+ childPoint: pRoot,
107
+ childPublicKeyComp33,
108
+ ethAddress: checksumAddress,
109
+ ethAddressLower: lowercaseAddress,
110
+ derivationSpec: {
111
+ tweakMode: "untweaked",
112
+ childPublicKeyComp33
113
+ }
114
+ };
115
+ }
116
+ /**
117
+ * `role-bound` EOA child derivation (`design.md` §4.2):
118
+ * `t_tweak = H_scalar("ARC-MPC-CHILD-TWEAK-V1", P_root^comp33, utf8(rootId), utf8(role), u64be(ethChainId), utf8(tweakLabel))`.
119
+ */
120
+ function deriveRoleBoundChild(params) {
121
+ const { rootPublicKey, rootId, role, ethChainId, tweakLabel } = params;
122
+ if (typeof rootId !== "string" || rootId.length === 0) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "rootId must be non-empty");
123
+ assertAllowedOnlineRole(role);
124
+ if (typeof tweakLabel !== "string" || tweakLabel.length === 0) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "tweakLabel must be non-empty");
125
+ const pRoot = rootPublicKey instanceof Uint8Array ? require_secp256k1_scalar.decodePointCompressed(rootPublicKey, false) : rootPublicKey;
126
+ return applyAdditiveTweak(pRoot, require_secp256k1_scalar.H_scalar(require_constants.DST_CHILD_TWEAK_V1, require_secp256k1_scalar.encodePointCompressed(pRoot), require_secp256k1_scalar.utf8Bytes(rootId), require_secp256k1_scalar.utf8Bytes(role), require_secp256k1_scalar.u64be(ethChainId), require_secp256k1_scalar.utf8Bytes(tweakLabel)), "role-bound", {
127
+ kind: "role-bound",
128
+ role,
129
+ ethChainId,
130
+ tweakLabel
131
+ });
132
+ }
133
+ /**
134
+ * `rule-bound` EOA child derivation (`design.md` §4.2):
135
+ * `t_tweak = H_scalar("ARC-MPC-RULE-TWEAK-V1", P_root^comp33, utf8(rootId), utf8(controllerDid), u64be(ethChainId), bytes32(ruleHash))`.
136
+ */
137
+ function deriveRuleBoundChild(params) {
138
+ const { rootPublicKey, rootId, controllerDid, ethChainId, ruleHash } = params;
139
+ if (typeof rootId !== "string" || rootId.length === 0) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "rootId must be non-empty");
140
+ if (typeof controllerDid !== "string" || controllerDid.length === 0) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "controllerDid must be non-empty");
141
+ const ruleHashBytes32 = require_secp256k1_scalar.normalizeHexBytes(ruleHash, 32);
142
+ const pRoot = rootPublicKey instanceof Uint8Array ? require_secp256k1_scalar.decodePointCompressed(rootPublicKey, false) : rootPublicKey;
143
+ return applyAdditiveTweak(pRoot, require_secp256k1_scalar.H_scalar(require_constants.DST_RULE_TWEAK_V1, require_secp256k1_scalar.encodePointCompressed(pRoot), require_secp256k1_scalar.utf8Bytes(rootId), require_secp256k1_scalar.utf8Bytes(controllerDid), require_secp256k1_scalar.u64be(ethChainId), ruleHashBytes32), "rule-bound", {
144
+ kind: "rule-bound",
145
+ controllerDid,
146
+ ethChainId,
147
+ ruleHashBytes32
148
+ });
149
+ }
150
+ /**
151
+ * Recovers the EIP-55 Ethereum address from a 32-byte digest and EIP-2 low-S `(r, s, yParity)`
152
+ * signature, matching Solidity `ecrecover` and Type-2 EIP-1559 transaction recovery.
153
+ */
154
+ function ecrecoverEthAddress(params) {
155
+ const { msgDigest32, r, s, yParity } = params;
156
+ if (!(msgDigest32 instanceof Uint8Array) || msgDigest32.length !== 32) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "msgDigest32 must be 32 bytes");
157
+ if (yParity !== 0 && yParity !== 1) throw new require_constants.MpcProtocolError("INVALID_PARAMETER", "yParity must be 0 or 1");
158
+ const sig = new _noble_curves_secp256k1_js.secp256k1.Signature(r, s, yParity);
159
+ if (sig.hasHighS()) throw new require_constants.MpcProtocolError("SIGN_AGGREGATION_FAILED", "High-S signature rejected by EIP-2");
160
+ const recoveredPoint = sig.recoverPublicKey(msgDigest32);
161
+ const { checksumAddress, lowercaseAddress } = pointToEthAddress(recoveredPoint);
162
+ return {
163
+ recoveredPoint,
164
+ checksumAddress,
165
+ lowercaseAddress
166
+ };
167
+ }
168
+
169
+ //#endregion
170
+ exports.applyAdditiveTweak = applyAdditiveTweak;
171
+ exports.assertAllowedOnlineRole = assertAllowedOnlineRole;
172
+ exports.deriveRoleBoundChild = deriveRoleBoundChild;
173
+ exports.deriveRuleBoundChild = deriveRuleBoundChild;
174
+ exports.deriveUntweakedChild = deriveUntweakedChild;
175
+ exports.ecrecoverEthAddress = ecrecoverEthAddress;
176
+ exports.parseStrictEthAddress = parseStrictEthAddress;
177
+ exports.pointToEthAddress = pointToEthAddress;
178
+ exports.toEip55ChecksumAddress = toEip55ChecksumAddress;
@@ -0,0 +1,100 @@
1
+ import { Secp256k1Point } from "./secp256k1-scalar.cjs";
2
+
3
+ //#region src/tweak.d.ts
4
+ type MpcTweakMode = 'untweaked' | 'role-bound' | 'rule-bound';
5
+ type MpcChildDerivationSpec = {
6
+ readonly tweakMode: 'untweaked';
7
+ readonly childPublicKeyComp33: Uint8Array;
8
+ } | {
9
+ readonly tweakMode: 'role-bound';
10
+ readonly role: string;
11
+ readonly ethChainId: bigint;
12
+ readonly tweakLabel: string;
13
+ readonly childPublicKeyComp33: Uint8Array;
14
+ } | {
15
+ readonly tweakMode: 'rule-bound';
16
+ readonly controllerDid: string;
17
+ readonly ethChainId: bigint;
18
+ readonly ruleHash: Uint8Array | string;
19
+ readonly childPublicKeyComp33: Uint8Array;
20
+ };
21
+ interface DerivedChildKey {
22
+ readonly tweakMode: MpcTweakMode;
23
+ readonly tweakScalar: bigint;
24
+ readonly tweakBytes32: Uint8Array;
25
+ readonly tweakHex: string;
26
+ readonly childPoint: Secp256k1Point;
27
+ readonly childPublicKeyComp33: Uint8Array;
28
+ readonly ethAddress: string;
29
+ readonly ethAddressLower: string;
30
+ readonly derivationSpec: MpcChildDerivationSpec;
31
+ }
32
+ declare function toEip55ChecksumAddress(lowerHexWithoutPrefix: string): string;
33
+ /**
34
+ * Validates an Ethereum `0x` address (rejects non-hex, all-uppercase, bad EIP-55 mixed-case
35
+ * checksums, and the zero address `0x0000...0000`) and returns canonical EIP-55 + lowercase forms.
36
+ */
37
+ declare function parseStrictEthAddress(address: string): {
38
+ checksumAddress: string;
39
+ lowercaseAddress: string;
40
+ addressBytes20: Uint8Array;
41
+ };
42
+ declare function pointToEthAddress(pointOrComp33: Secp256k1Point | Uint8Array): {
43
+ checksumAddress: string;
44
+ lowercaseAddress: string;
45
+ };
46
+ declare function assertAllowedOnlineRole(role: string): void;
47
+ /**
48
+ * Applies a non-hardened additive scalar tweak `P_child = P_root + t_tweak * G`,
49
+ * strictly rejecting `t_tweak == 0`, `t_tweak >= n`, or `P_child == O`.
50
+ */
51
+ declare function applyAdditiveTweak(rootPublicKey: Secp256k1Point | Uint8Array, tweakScalar: bigint, tweakMode: Exclude<MpcTweakMode, 'untweaked'>, derivationMeta?: {
52
+ readonly kind: 'role-bound';
53
+ readonly role: string;
54
+ readonly ethChainId: bigint;
55
+ readonly tweakLabel: string;
56
+ } | {
57
+ readonly kind: 'rule-bound';
58
+ readonly controllerDid: string;
59
+ readonly ethChainId: bigint;
60
+ readonly ruleHashBytes32: Uint8Array;
61
+ }): DerivedChildKey;
62
+ declare function deriveUntweakedChild(rootPublicKey: Secp256k1Point | Uint8Array): DerivedChildKey;
63
+ /**
64
+ * `role-bound` EOA child derivation (`design.md` §4.2):
65
+ * `t_tweak = H_scalar("ARC-MPC-CHILD-TWEAK-V1", P_root^comp33, utf8(rootId), utf8(role), u64be(ethChainId), utf8(tweakLabel))`.
66
+ */
67
+ declare function deriveRoleBoundChild(params: {
68
+ rootPublicKey: Secp256k1Point | Uint8Array;
69
+ rootId: string;
70
+ role: string;
71
+ ethChainId: bigint;
72
+ tweakLabel: string;
73
+ }): DerivedChildKey;
74
+ /**
75
+ * `rule-bound` EOA child derivation (`design.md` §4.2):
76
+ * `t_tweak = H_scalar("ARC-MPC-RULE-TWEAK-V1", P_root^comp33, utf8(rootId), utf8(controllerDid), u64be(ethChainId), bytes32(ruleHash))`.
77
+ */
78
+ declare function deriveRuleBoundChild(params: {
79
+ rootPublicKey: Secp256k1Point | Uint8Array;
80
+ rootId: string;
81
+ controllerDid: string;
82
+ ethChainId: bigint;
83
+ ruleHash: Uint8Array | string;
84
+ }): DerivedChildKey;
85
+ /**
86
+ * Recovers the EIP-55 Ethereum address from a 32-byte digest and EIP-2 low-S `(r, s, yParity)`
87
+ * signature, matching Solidity `ecrecover` and Type-2 EIP-1559 transaction recovery.
88
+ */
89
+ declare function ecrecoverEthAddress(params: {
90
+ msgDigest32: Uint8Array;
91
+ r: bigint;
92
+ s: bigint;
93
+ yParity: 0 | 1;
94
+ }): {
95
+ recoveredPoint: Secp256k1Point;
96
+ checksumAddress: string;
97
+ lowercaseAddress: string;
98
+ };
99
+ //#endregion
100
+ export { DerivedChildKey, MpcChildDerivationSpec, MpcTweakMode, applyAdditiveTweak, assertAllowedOnlineRole, deriveRoleBoundChild, deriveRuleBoundChild, deriveUntweakedChild, ecrecoverEthAddress, parseStrictEthAddress, pointToEthAddress, toEip55ChecksumAddress };