@ocap/wallet-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.
- package/README.md +6 -2
- package/esm/broadcaster.d.mts +88 -0
- package/esm/broadcaster.mjs +260 -0
- package/esm/http-cosigner-transport.d.mts +28 -0
- package/esm/http-cosigner-transport.mjs +214 -0
- package/esm/index.d.mts +5 -0
- package/esm/index.mjs +6 -0
- package/esm/logical-hsm-wallet.d.mts +29 -0
- package/esm/logical-hsm-wallet.mjs +34 -0
- package/esm/personal-hsm-wallet.d.mts +88 -0
- package/esm/personal-hsm-wallet.mjs +383 -0
- package/lib/_virtual/rolldown_runtime.cjs +29 -0
- package/lib/broadcaster.cjs +263 -0
- package/lib/broadcaster.d.cts +88 -0
- package/lib/http-cosigner-transport.cjs +217 -0
- package/lib/http-cosigner-transport.d.cts +28 -0
- package/lib/index.cjs +13 -0
- package/lib/index.d.cts +5 -0
- package/lib/logical-hsm-wallet.cjs +35 -0
- package/lib/logical-hsm-wallet.d.cts +29 -0
- package/lib/personal-hsm-wallet.cjs +385 -0
- package/lib/personal-hsm-wallet.d.cts +88 -0
- package/package.json +68 -4
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ExternalWalletSignContext, WalletObject } from "@ocap/wallet";
|
|
2
|
+
import { CryptoMethod, DIDTypeArg } from "@arcblock/did";
|
|
3
|
+
import { BytesType } from "@ocap/util";
|
|
4
|
+
|
|
5
|
+
//#region src/logical-hsm-wallet.d.ts
|
|
6
|
+
|
|
7
|
+
interface LogicalHsmSigningRequest {
|
|
8
|
+
readonly keyAlias?: string;
|
|
9
|
+
readonly kind: ExternalWalletSignContext['kind'];
|
|
10
|
+
readonly rawPayloadBytes: Uint8Array;
|
|
11
|
+
readonly signingDigestHex: string;
|
|
12
|
+
readonly hashBeforeSign: boolean;
|
|
13
|
+
readonly publicKeyHex: string;
|
|
14
|
+
readonly address: string;
|
|
15
|
+
readonly extra?: string;
|
|
16
|
+
}
|
|
17
|
+
interface CreateLogicalHsmWalletOptions {
|
|
18
|
+
readonly publicKey: BytesType;
|
|
19
|
+
readonly address?: string;
|
|
20
|
+
readonly didType?: DIDTypeArg;
|
|
21
|
+
readonly method?: CryptoMethod;
|
|
22
|
+
readonly keyAlias?: string;
|
|
23
|
+
/** Default `false`: rejects raw 32B digests & `hashBeforeSign === false` outside structured JWTs */
|
|
24
|
+
readonly allowRawDigestSigning?: boolean;
|
|
25
|
+
readonly signWithBox: (request: LogicalHsmSigningRequest) => Promise<BytesType>;
|
|
26
|
+
}
|
|
27
|
+
declare function createLogicalHsmWallet(options: CreateLogicalHsmWalletOptions): WalletObject<string>;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { CreateLogicalHsmWalletOptions, LogicalHsmSigningRequest, createLogicalHsmWallet };
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let _ocap_mpc_hsm_personal = require("@ocap/mpc-hsm/personal");
|
|
3
|
+
let _arcblock_jwt = require("@arcblock/jwt");
|
|
4
|
+
_arcblock_jwt = require_rolldown_runtime.__toESM(_arcblock_jwt);
|
|
5
|
+
let _noble_curves_secp256k1_js = require("@noble/curves/secp256k1.js");
|
|
6
|
+
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
7
|
+
let _ocap_util = require("@ocap/util");
|
|
8
|
+
let _ocap_wallet = require("@ocap/wallet");
|
|
9
|
+
|
|
10
|
+
//#region src/personal-hsm-wallet.ts
|
|
11
|
+
/**
|
|
12
|
+
* Dual-Surface Keyless Personal & Agent MPC Wallet (`@ocap/wallet-hsm`, #2333).
|
|
13
|
+
*
|
|
14
|
+
* Bridges `@ocap/mpc-hsm/personal` (`Slot 1` client share + `Slot 2` Stateless Sealed HSM cosigner)
|
|
15
|
+
* across BOTH:
|
|
16
|
+
* 1. EVM Clear-Signing (`signEip1559Transaction`, `sendEip1559Transaction`, `toEip1193Provider()`, `toViemLocalAccount()`)
|
|
17
|
+
* 2. OCAP Ecosystem `WalletObject` (`toOcapWallet()`, `signOcapTransaction`, `signArcblockJwt`, `signArcblockDelegationToken`)
|
|
18
|
+
*
|
|
19
|
+
* Non-negotiable invariants:
|
|
20
|
+
* - Zero `node:*` imports and zero `Buffer` references.
|
|
21
|
+
* - Full secret key `x_child` is NEVER reconstructed; `s_actor` is copied & zeroized in `finally`;
|
|
22
|
+
* `WalletObject.secretKey` is `''` and `toJSON().sk === ''`.
|
|
23
|
+
* - Raw 32-byte blind digests, `hashBeforeSign === false`, `eth_sign`, `personal_sign`, and EIP-7702 are hard-banned.
|
|
24
|
+
*/
|
|
25
|
+
function aggregatedSigToCanonicalDerHex(sig) {
|
|
26
|
+
return `0x${(0, _ocap_mpc_hsm_personal.bytesToHex)(new _noble_curves_secp256k1_js.secp256k1.Signature(sig.r, sig.s).toBytes("der"))}`;
|
|
27
|
+
}
|
|
28
|
+
function createPersonalHsmWallet(params) {
|
|
29
|
+
const pRootComp33 = (0, _ocap_mpc_hsm_personal.encodePointCompressed)((0, _ocap_mpc_hsm_personal.decodePointCompressed)((0, _ocap_mpc_hsm_personal.normalizeHexBytes)(params.rootPublicKeyComp33, 33), false));
|
|
30
|
+
const derivedChild = (0, _ocap_mpc_hsm_personal.derivePersonalVaultChildKey)({
|
|
31
|
+
vaultId: params.vaultId,
|
|
32
|
+
rootPublicKeyComp33: pRootComp33,
|
|
33
|
+
derivation: params.childDerivation
|
|
34
|
+
});
|
|
35
|
+
const identities = (0, _ocap_mpc_hsm_personal.deriveOcapIdentitiesFromChildPoint)(derivedChild.childPublicKeyComp33);
|
|
36
|
+
const nowSecFn = params.nowSec ?? (() => BigInt(Math.floor(Date.now() / 1e3)));
|
|
37
|
+
let cachedCluster = params.xActorComp33 !== void 0 ? (0, _ocap_mpc_hsm_personal.buildPersonalVaultClusterInfo)({
|
|
38
|
+
vaultId: params.vaultId,
|
|
39
|
+
slotId: params.slotId,
|
|
40
|
+
policyEpoch: params.policyEpoch,
|
|
41
|
+
rootPublicKeyComp33: pRootComp33,
|
|
42
|
+
xActorComp33: params.xActorComp33
|
|
43
|
+
}) : null;
|
|
44
|
+
const transportWithBroadcaster = {
|
|
45
|
+
initPresign: (r) => params.transport.initPresign(r),
|
|
46
|
+
stepPresign: (r) => params.transport.stepPresign(r),
|
|
47
|
+
signOnline: (r) => params.transport.signOnline(r),
|
|
48
|
+
broadcastRawTransaction: params.transport.broadcastRawTransaction ?? (params.broadcaster ? async (rawSignedTxHex) => {
|
|
49
|
+
const decoded = (0, _ocap_mpc_hsm_personal.decodeAndRecoverSignedEip1559Tx)(rawSignedTxHex);
|
|
50
|
+
return (await params.broadcaster.broadcastEip1559({
|
|
51
|
+
chainId: decoded.chainId,
|
|
52
|
+
rawSignedTxHex,
|
|
53
|
+
tier: "pro-shield"
|
|
54
|
+
})).txHashHex;
|
|
55
|
+
} : void 0)
|
|
56
|
+
};
|
|
57
|
+
const eoaSigner = (0, _ocap_mpc_hsm_personal.createPersonalMpcEoaSigner)({
|
|
58
|
+
vaultId: params.vaultId,
|
|
59
|
+
slotId: params.slotId,
|
|
60
|
+
policyEpoch: params.policyEpoch,
|
|
61
|
+
rootPublicKeyComp33: pRootComp33,
|
|
62
|
+
xActorComp33: params.xActorComp33,
|
|
63
|
+
getActorShareBytes32: params.getActorShareBytes32,
|
|
64
|
+
policy: params.policy,
|
|
65
|
+
childDerivation: params.childDerivation,
|
|
66
|
+
transport: transportWithBroadcaster,
|
|
67
|
+
rng: params.rng
|
|
68
|
+
});
|
|
69
|
+
const executeMpcCoSign = async (coSignInput) => {
|
|
70
|
+
const callerSActor = await params.getActorShareBytes32();
|
|
71
|
+
const sActorCopy32 = new Uint8Array(callerSActor);
|
|
72
|
+
const localPool = new _ocap_mpc_hsm_personal.VolatilePresignaturePool();
|
|
73
|
+
let presigPub = null;
|
|
74
|
+
try {
|
|
75
|
+
const xActorComp33 = (0, _ocap_mpc_hsm_personal.encodePointCompressed)((0, _ocap_mpc_hsm_personal.mulBaseG)((0, _ocap_mpc_hsm_personal.bytes32ToScalarNonZero)(sActorCopy32)));
|
|
76
|
+
if (!cachedCluster) cachedCluster = (0, _ocap_mpc_hsm_personal.buildPersonalVaultClusterInfo)({
|
|
77
|
+
vaultId: params.vaultId,
|
|
78
|
+
slotId: params.slotId,
|
|
79
|
+
policyEpoch: params.policyEpoch,
|
|
80
|
+
rootPublicKeyComp33: pRootComp33,
|
|
81
|
+
xActorComp33
|
|
82
|
+
});
|
|
83
|
+
const clientPresigRecord = await (0, _ocap_mpc_hsm_personal.runPersonalClientPresignWithTransport)({
|
|
84
|
+
cluster: cachedCluster,
|
|
85
|
+
sActorBytes32: sActorCopy32,
|
|
86
|
+
transport: params.transport,
|
|
87
|
+
rng: params.rng
|
|
88
|
+
});
|
|
89
|
+
presigPub = clientPresigRecord.publicVerification;
|
|
90
|
+
localPool.store(clientPresigRecord);
|
|
91
|
+
const onlineReq = {
|
|
92
|
+
rootId: params.vaultId,
|
|
93
|
+
presigId: presigPub.presigId32,
|
|
94
|
+
subset: [1, 2],
|
|
95
|
+
msgDigest32: coSignInput.signingDigest32,
|
|
96
|
+
childDerivation: derivedChild.derivationSpec
|
|
97
|
+
};
|
|
98
|
+
const localShareRes = (0, _ocap_mpc_hsm_personal.createOnlineSignShare)({
|
|
99
|
+
pool: localPool,
|
|
100
|
+
request: onlineReq
|
|
101
|
+
});
|
|
102
|
+
const cosignerRes = await params.transport.signOnline({
|
|
103
|
+
vaultId: params.vaultId,
|
|
104
|
+
slotId: params.slotId,
|
|
105
|
+
presigIdHex: presigPub.presigIdHex,
|
|
106
|
+
payloadKind: coSignInput.payloadKind,
|
|
107
|
+
unsignedPayloadBytes: coSignInput.unsignedPayloadBytes,
|
|
108
|
+
signingDigest32: coSignInput.signingDigest32,
|
|
109
|
+
clearSigningPreview: coSignInput.clearSigningPreview,
|
|
110
|
+
clearSigningPreviewHash32: coSignInput.clearSigningPreviewHash32,
|
|
111
|
+
childDerivation: derivedChild.derivationSpec,
|
|
112
|
+
policyProof: coSignInput.options?.policyProof,
|
|
113
|
+
entitlementTicket: coSignInput.options?.entitlementTicket,
|
|
114
|
+
firewallAttestation: coSignInput.options?.firewallAttestation,
|
|
115
|
+
attestedProxyImplementationAddress: coSignInput.options?.attestedProxyImplementationAddress,
|
|
116
|
+
simulatedDelta: coSignInput.options?.simulatedDelta,
|
|
117
|
+
postConditions: coSignInput.options?.postConditions
|
|
118
|
+
});
|
|
119
|
+
if (!cosignerRes?.share || cosignerRes.share.slot !== 2) throw new _ocap_mpc_hsm_personal.MpcProtocolError("SIGN_SHARE_VERIFICATION_FAILED", "Expected cosigner partial signature from slot 2", 2);
|
|
120
|
+
return (0, _ocap_mpc_hsm_personal.aggregateOnlineSignature)({
|
|
121
|
+
shares: [localShareRes.share, cosignerRes.share],
|
|
122
|
+
publicVerification: presigPub,
|
|
123
|
+
request: onlineReq
|
|
124
|
+
});
|
|
125
|
+
} finally {
|
|
126
|
+
(0, _ocap_mpc_hsm_personal.zeroizeBytes)(sActorCopy32);
|
|
127
|
+
localPool.wipeAll();
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
const signEip1559Transaction = (tx, options) => eoaSigner.signTransaction(tx, options);
|
|
131
|
+
const sendEip1559Transaction = async (tx, options) => {
|
|
132
|
+
if (!params.broadcaster) throw new _ocap_mpc_hsm_personal.MpcProtocolError("INVALID_PARAMETER", "TxBroadcaster is required for sendEip1559Transaction");
|
|
133
|
+
let preparedAttestation = options?.firewallAttestation;
|
|
134
|
+
let preparedDelta = options?.simulatedDelta;
|
|
135
|
+
let preparedProxyImpl = options?.attestedProxyImplementationAddress;
|
|
136
|
+
const preVerified = (0, _ocap_mpc_hsm_personal.decodeAndVerifyPersonalEip1559Tx)({
|
|
137
|
+
txOrRawBytes: tx,
|
|
138
|
+
policy: params.policy,
|
|
139
|
+
hasL2OrL3QuorumProof: options?.policyProof?.tier === "L2" || options?.policyProof?.tier === "L3"
|
|
140
|
+
});
|
|
141
|
+
const prePreviewHashHex = `0x${(0, _ocap_mpc_hsm_personal.bytesToHex)((0, _ocap_mpc_hsm_personal.computePersonalClearSigningPreviewHash32)((0, _ocap_mpc_hsm_personal.buildPersonalClearSigningPreview)({
|
|
142
|
+
fromAddress: identities.ethChecksumAddress,
|
|
143
|
+
verifiedTx: preVerified
|
|
144
|
+
})))}`;
|
|
145
|
+
if (!preparedAttestation && params.broadcaster.prepareEip1559) {
|
|
146
|
+
const prep = await params.broadcaster.prepareEip1559({
|
|
147
|
+
vaultId: params.vaultId,
|
|
148
|
+
policyEpoch: params.policyEpoch,
|
|
149
|
+
tier: options?.tier ?? (params.policy?.requireFirewallAttestation ? "vault-enforced" : "pro-shield"),
|
|
150
|
+
fromAddress: identities.ethChecksumAddress,
|
|
151
|
+
tx,
|
|
152
|
+
signingDigestHex: preVerified.signingDigestHex,
|
|
153
|
+
clearSigningPreviewHashHex: prePreviewHashHex,
|
|
154
|
+
knownCounterparties: options?.knownCounterparties,
|
|
155
|
+
postConditions: options?.postConditions,
|
|
156
|
+
authProof: options?.authProof
|
|
157
|
+
});
|
|
158
|
+
preparedAttestation = prep.firewallAttestation;
|
|
159
|
+
preparedDelta = prep.simulatedDelta;
|
|
160
|
+
preparedProxyImpl = prep.attestedProxyImplementationAddress;
|
|
161
|
+
}
|
|
162
|
+
if (params.policy?.requireFirewallAttestation && (!preparedAttestation || !preparedDelta)) throw new _ocap_mpc_hsm_personal.MpcProtocolError("PERSONAL_FIREWALL_ATTESTATION_REQUIRED", "Vault policy requires SignedFirewallAttestation + simulatedDelta, and broadcaster did not supply Stage 1 prepare output");
|
|
163
|
+
const signed = await signEip1559Transaction(tx, {
|
|
164
|
+
...options,
|
|
165
|
+
simulatedDelta: preparedDelta,
|
|
166
|
+
firewallAttestation: preparedAttestation,
|
|
167
|
+
attestedProxyImplementationAddress: preparedProxyImpl
|
|
168
|
+
});
|
|
169
|
+
const broadcasted = await params.broadcaster.broadcastEip1559({
|
|
170
|
+
chainId: tx.chainId,
|
|
171
|
+
rawSignedTxHex: signed.rawSignedTxHex,
|
|
172
|
+
tier: options?.tier ?? (preparedAttestation ? preparedAttestation.tier : "pro-shield"),
|
|
173
|
+
firewallAttestation: preparedAttestation,
|
|
174
|
+
stage1SimulatedDelta: preparedDelta,
|
|
175
|
+
attestedProxyImplementationAddress: preparedProxyImpl,
|
|
176
|
+
postConditions: options?.postConditions,
|
|
177
|
+
preferPrivateMevRpc: options?.preferPrivateMevRpc
|
|
178
|
+
});
|
|
179
|
+
return {
|
|
180
|
+
txHashHex: broadcasted.txHashHex,
|
|
181
|
+
via: broadcasted.via,
|
|
182
|
+
signed,
|
|
183
|
+
...preparedAttestation ? { firewallAttestation: preparedAttestation } : {}
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
const signOcapTransaction = async (rawUnsignedCborBytes, options) => {
|
|
187
|
+
const hasL2OrL3 = options?.policyProof?.tier === "L2" || options?.policyProof?.tier === "L3";
|
|
188
|
+
const hasL3Guardian = options?.policyProof?.tier === "L3" && options.policyProof.quorumProof?.kind === "guardian-quorum";
|
|
189
|
+
const decoded = (0, _ocap_mpc_hsm_personal.decodeAndVerifyPersonalOcapTx)({
|
|
190
|
+
rawTxBytes: rawUnsignedCborBytes,
|
|
191
|
+
childPublicKeyComp33: derivedChild.childPublicKeyComp33,
|
|
192
|
+
policy: params.policy,
|
|
193
|
+
nowSec: options?.nowSec ?? nowSecFn(),
|
|
194
|
+
hasL2OrL3QuorumProof: hasL2OrL3,
|
|
195
|
+
hasL3GuardianQuorumProof: hasL3Guardian
|
|
196
|
+
});
|
|
197
|
+
const aggregated = await executeMpcCoSign({
|
|
198
|
+
payloadKind: "ocap-tx",
|
|
199
|
+
unsignedPayloadBytes: decoded.verifiedOcapTx.unsignedPayloadBytes,
|
|
200
|
+
signingDigest32: decoded.verifiedOcapTx.signingDigest32,
|
|
201
|
+
clearSigningPreview: decoded.clearSigningPreview,
|
|
202
|
+
clearSigningPreviewHash32: decoded.clearSigningPreviewHash32,
|
|
203
|
+
options
|
|
204
|
+
});
|
|
205
|
+
return {
|
|
206
|
+
derSignatureHex: aggregatedSigToCanonicalDerHex(aggregated),
|
|
207
|
+
signature: aggregated,
|
|
208
|
+
signingDigest32: decoded.verifiedOcapTx.signingDigest32,
|
|
209
|
+
clearSigningPreview: decoded.clearSigningPreview,
|
|
210
|
+
clearSigningPreviewHash32: decoded.clearSigningPreviewHash32
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
const signArcblockJwt = async (payload = {}, options) => {
|
|
214
|
+
const signerDid = (options?.didType ?? "secp256k1") === "ethereum" ? `did:abt:${identities.ethChecksumAddress}` : identities.ocapSecp256k1Did;
|
|
215
|
+
return _arcblock_jwt.signWithSigner(signerDid, async (_signingDigestHex, ctx) => {
|
|
216
|
+
const hasL2OrL3 = options?.policyProof?.tier === "L2" || options?.policyProof?.tier === "L3";
|
|
217
|
+
const verified = (0, _ocap_mpc_hsm_personal.decodeAndVerifyPersonalArcblockJwt)({
|
|
218
|
+
unsignedTokenOrBytes: ctx.unsignedToken,
|
|
219
|
+
childPublicKeyComp33: derivedChild.childPublicKeyComp33,
|
|
220
|
+
nowSec: options?.nowSec ?? nowSecFn(),
|
|
221
|
+
policy: params.policy,
|
|
222
|
+
hasL2OrL3QuorumProof: hasL2OrL3
|
|
223
|
+
});
|
|
224
|
+
return aggregatedSigToCanonicalDerHex(await executeMpcCoSign({
|
|
225
|
+
payloadKind: "arcblock-jwt",
|
|
226
|
+
unsignedPayloadBytes: verified.verifiedJwt.unsignedPayloadBytes,
|
|
227
|
+
signingDigest32: verified.verifiedJwt.signingDigest32,
|
|
228
|
+
clearSigningPreview: verified.clearSigningPreview,
|
|
229
|
+
clearSigningPreviewHash32: verified.clearSigningPreviewHash32,
|
|
230
|
+
options
|
|
231
|
+
}));
|
|
232
|
+
}, payload, "1.1.0");
|
|
233
|
+
};
|
|
234
|
+
const signArcblockDelegationToken = async (payload, options) => {
|
|
235
|
+
const scheme = options?.didType ?? "secp256k1";
|
|
236
|
+
const signerDid = scheme === "ethereum" ? `did:abt:${identities.ethChecksumAddress}` : identities.ocapSecp256k1Did;
|
|
237
|
+
const pkHex = scheme === "ethereum" ? identities.ethUncompressedPkHex : identities.secp256k1UncompressedPkHex;
|
|
238
|
+
return _arcblock_jwt.signDelegationTokenWithSigner(signerDid, pkHex, async (_signingDigestHex, ctx) => {
|
|
239
|
+
const hasL2OrL3 = options?.policyProof?.tier === "L2" || options?.policyProof?.tier === "L3";
|
|
240
|
+
const verified = (0, _ocap_mpc_hsm_personal.decodeAndVerifyPersonalArcblockJwt)({
|
|
241
|
+
unsignedTokenOrBytes: ctx.unsignedToken,
|
|
242
|
+
childPublicKeyComp33: derivedChild.childPublicKeyComp33,
|
|
243
|
+
nowSec: options?.nowSec ?? nowSecFn(),
|
|
244
|
+
policy: params.policy,
|
|
245
|
+
hasL2OrL3QuorumProof: hasL2OrL3
|
|
246
|
+
});
|
|
247
|
+
return aggregatedSigToCanonicalDerHex(await executeMpcCoSign({
|
|
248
|
+
payloadKind: "arcblock-jwt",
|
|
249
|
+
unsignedPayloadBytes: verified.verifiedJwt.unsignedPayloadBytes,
|
|
250
|
+
signingDigest32: verified.verifiedJwt.signingDigest32,
|
|
251
|
+
clearSigningPreview: verified.clearSigningPreview,
|
|
252
|
+
clearSigningPreviewHash32: verified.clearSigningPreviewHash32,
|
|
253
|
+
options
|
|
254
|
+
}));
|
|
255
|
+
}, payload);
|
|
256
|
+
};
|
|
257
|
+
const toOcapWallet = (opts) => {
|
|
258
|
+
const scheme = opts?.didType ?? "secp256k1";
|
|
259
|
+
const walletType = scheme === "ethereum" ? "ethereum" : {
|
|
260
|
+
role: _ocap_mcrypto.types.RoleType.ROLE_ACCOUNT,
|
|
261
|
+
pk: _ocap_mcrypto.types.KeyType.SECP256K1,
|
|
262
|
+
hash: _ocap_mcrypto.types.HashType.SHA3,
|
|
263
|
+
address: _ocap_mcrypto.types.EncodingType.BASE58
|
|
264
|
+
};
|
|
265
|
+
return (0, _ocap_wallet.fromExternalSigner)({
|
|
266
|
+
publicKey: scheme === "ethereum" ? identities.ethUncompressedPkHex : identities.secp256k1UncompressedPkHex,
|
|
267
|
+
address: scheme === "ethereum" ? identities.ethChecksumAddress : identities.ocapSecp256k1Address,
|
|
268
|
+
async sign(_signingInput, context) {
|
|
269
|
+
if (context.kind === "jwt") {
|
|
270
|
+
if (!context.hashBeforeSign || typeof context.rawPayload !== "string") throw new _ocap_mpc_hsm_personal.MpcProtocolError("PERSONAL_BLIND_DIGEST_FORBIDDEN", "Personal HSM OCAP wallet requires v1.1.0 prehashed structured JWT");
|
|
271
|
+
const signOpts = opts?.defaultSignOptions?.();
|
|
272
|
+
const hasL2OrL3 = signOpts?.policyProof?.tier === "L2" || signOpts?.policyProof?.tier === "L3";
|
|
273
|
+
const verified = (0, _ocap_mpc_hsm_personal.decodeAndVerifyPersonalArcblockJwt)({
|
|
274
|
+
unsignedTokenOrBytes: context.rawPayload,
|
|
275
|
+
childPublicKeyComp33: derivedChild.childPublicKeyComp33,
|
|
276
|
+
nowSec: nowSecFn(),
|
|
277
|
+
policy: params.policy,
|
|
278
|
+
hasL2OrL3QuorumProof: hasL2OrL3
|
|
279
|
+
});
|
|
280
|
+
return aggregatedSigToCanonicalDerHex(await executeMpcCoSign({
|
|
281
|
+
payloadKind: "arcblock-jwt",
|
|
282
|
+
unsignedPayloadBytes: verified.verifiedJwt.unsignedPayloadBytes,
|
|
283
|
+
signingDigest32: verified.verifiedJwt.signingDigest32,
|
|
284
|
+
clearSigningPreview: verified.clearSigningPreview,
|
|
285
|
+
clearSigningPreviewHash32: verified.clearSigningPreviewHash32,
|
|
286
|
+
options: signOpts
|
|
287
|
+
}));
|
|
288
|
+
}
|
|
289
|
+
if (!context.hashBeforeSign) throw new _ocap_mpc_hsm_personal.MpcProtocolError("PERSONAL_BLIND_DIGEST_FORBIDDEN", "Pre-hashed blind digest signing (hashBeforeSign=false) is forbidden on Personal HSM wallet");
|
|
290
|
+
const rawBytes = (0, _ocap_util.toUint8Array)(context.rawPayload);
|
|
291
|
+
if (rawBytes.length >= 4 && rawBytes[0] === 217 && rawBytes[1] === 217 && rawBytes[2] === 247) return (await signOcapTransaction(rawBytes, opts?.defaultSignOptions?.())).derSignatureHex;
|
|
292
|
+
throw new _ocap_mpc_hsm_personal.MpcProtocolError("PERSONAL_BLIND_DIGEST_FORBIDDEN", "Personal HSM OCAP wallet only signs canonical CBOR OCAP transactions (0xd9d9f7) or v1.1.0 ArcBlock JWTs");
|
|
293
|
+
},
|
|
294
|
+
async signEnvelope() {
|
|
295
|
+
throw new _ocap_mpc_hsm_personal.MpcProtocolError("PERSONAL_BLIND_DIGEST_FORBIDDEN", "Arbitrary message envelope signing is forbidden on Personal HSM wallet");
|
|
296
|
+
},
|
|
297
|
+
async signETH() {
|
|
298
|
+
throw new _ocap_mpc_hsm_personal.MpcProtocolError("PERSONAL_BLIND_DIGEST_FORBIDDEN", "Blind ethSign is forbidden on Personal HSM wallet");
|
|
299
|
+
}
|
|
300
|
+
}, walletType);
|
|
301
|
+
};
|
|
302
|
+
const parseEip1193Uint = (raw, label, fallback) => {
|
|
303
|
+
if (raw === void 0 || raw === null) {
|
|
304
|
+
if (fallback !== void 0) return fallback;
|
|
305
|
+
throw new _ocap_mpc_hsm_personal.MpcProtocolError("RLP_INVALID_TRANSACTION", `Missing ${label} in EIP-1193 transaction`);
|
|
306
|
+
}
|
|
307
|
+
if (typeof raw === "bigint" && raw >= 0n) return raw;
|
|
308
|
+
if (typeof raw === "number" && Number.isSafeInteger(raw) && raw >= 0) return BigInt(raw);
|
|
309
|
+
if (typeof raw === "string") {
|
|
310
|
+
const s = raw.trim();
|
|
311
|
+
if (/^0x[0-9a-fA-F]+$/.test(s)) return BigInt(s);
|
|
312
|
+
if (/^(?:0|[1-9][0-9]*)$/.test(s)) return BigInt(s);
|
|
313
|
+
}
|
|
314
|
+
throw new _ocap_mpc_hsm_personal.MpcProtocolError("RLP_INVALID_TRANSACTION", `Invalid ${label} in EIP-1193 transaction`);
|
|
315
|
+
};
|
|
316
|
+
const parseEip1193TxObject = (rawTx) => {
|
|
317
|
+
if (!rawTx || typeof rawTx !== "object" || Array.isArray(rawTx)) throw new _ocap_mpc_hsm_personal.MpcProtocolError("RLP_INVALID_TRANSACTION", "EIP-1193 eth_sendTransaction requires a tx object");
|
|
318
|
+
const obj = rawTx;
|
|
319
|
+
if (obj.type === 4 || obj.type === "0x4" || obj.type === "0x04" || obj.type === "eip7702" || obj.authorizationList !== void 0) throw new _ocap_mpc_hsm_personal.MpcProtocolError("PERSONAL_EIP7702_FORBIDDEN", "EIP-7702 transactions are forbidden");
|
|
320
|
+
if (obj.type !== void 0 && obj.type !== 2 && obj.type !== "0x2" && obj.type !== "0x02" && obj.type !== "eip1559") throw new _ocap_mpc_hsm_personal.MpcProtocolError("RLP_INVALID_TRANSACTION", `Unsupported EIP-1193 transaction type "${String(obj.type)}"; EIP-1559 (0x02) required`);
|
|
321
|
+
if (obj.from !== void 0 && String(obj.from).toLowerCase() !== identities.ethChecksumAddress.toLowerCase()) throw new _ocap_mpc_hsm_personal.MpcProtocolError("PERSONAL_POLICY_VIOLATION", "EIP-1193 tx.from does not match Personal HSM EOA");
|
|
322
|
+
if (typeof obj.to !== "string" || obj.to.length === 0) throw new _ocap_mpc_hsm_personal.MpcProtocolError("RLP_INVALID_TRANSACTION", "Contract creation (missing to) is forbidden on Personal HSM");
|
|
323
|
+
let parsedAccessList;
|
|
324
|
+
if (obj.accessList !== void 0) {
|
|
325
|
+
if (!Array.isArray(obj.accessList)) throw new _ocap_mpc_hsm_personal.MpcProtocolError("RLP_INVALID_TRANSACTION", "EIP-1193 accessList must be an array");
|
|
326
|
+
parsedAccessList = obj.accessList.map((item, idx) => {
|
|
327
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) throw new _ocap_mpc_hsm_personal.MpcProtocolError("RLP_INVALID_TRANSACTION", `Invalid accessList[${idx}] entry`);
|
|
328
|
+
const rec = item;
|
|
329
|
+
if (typeof rec.address !== "string" || !Array.isArray(rec.storageKeys)) throw new _ocap_mpc_hsm_personal.MpcProtocolError("RLP_INVALID_TRANSACTION", `Malformed accessList[${idx}] address or storageKeys`);
|
|
330
|
+
return {
|
|
331
|
+
address: rec.address,
|
|
332
|
+
storageKeys: rec.storageKeys.map((sk) => sk instanceof Uint8Array ? sk : String(sk))
|
|
333
|
+
};
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
return {
|
|
337
|
+
chainId: parseEip1193Uint(obj.chainId, "chainId"),
|
|
338
|
+
nonce: parseEip1193Uint(obj.nonce, "nonce", 0n),
|
|
339
|
+
maxPriorityFeePerGasWei: parseEip1193Uint(obj.maxPriorityFeePerGas ?? obj.maxPriorityFeePerGasWei, "maxPriorityFeePerGas"),
|
|
340
|
+
maxFeePerGasWei: parseEip1193Uint(obj.maxFeePerGas ?? obj.maxFeePerGasWei, "maxFeePerGas"),
|
|
341
|
+
gasLimit: parseEip1193Uint(obj.gas ?? obj.gasLimit, "gas"),
|
|
342
|
+
to: obj.to,
|
|
343
|
+
valueWei: parseEip1193Uint(obj.value ?? obj.valueWei, "value", 0n),
|
|
344
|
+
data: obj.data instanceof Uint8Array ? obj.data : typeof obj.data === "string" && obj.data.length > 0 ? obj.data : "0x",
|
|
345
|
+
...parsedAccessList ? { accessList: parsedAccessList } : {}
|
|
346
|
+
};
|
|
347
|
+
};
|
|
348
|
+
const toEip1193Provider = (providerOpts) => {
|
|
349
|
+
const resolveMergedOpts = () => {
|
|
350
|
+
const baseOpts = params.defaultSendOptions?.();
|
|
351
|
+
const overrideOpts = typeof providerOpts?.defaultSendOptions === "function" ? providerOpts.defaultSendOptions() : providerOpts?.defaultSendOptions;
|
|
352
|
+
return baseOpts || overrideOpts ? {
|
|
353
|
+
...baseOpts,
|
|
354
|
+
...overrideOpts
|
|
355
|
+
} : void 0;
|
|
356
|
+
};
|
|
357
|
+
const request = async (args) => {
|
|
358
|
+
if (args.method === "eth_sendTransaction" && params.broadcaster) return (await sendEip1559Transaction(parseEip1193TxObject(Array.isArray(args.params) ? args.params[0] : void 0), resolveMergedOpts())).txHashHex;
|
|
359
|
+
if (args.method === "eth_signTransaction") return (await signEip1559Transaction(parseEip1193TxObject(Array.isArray(args.params) ? args.params[0] : void 0), resolveMergedOpts())).rawSignedTxHex;
|
|
360
|
+
return eoaSigner.request(args);
|
|
361
|
+
};
|
|
362
|
+
return { request };
|
|
363
|
+
};
|
|
364
|
+
return {
|
|
365
|
+
address: identities.ethChecksumAddress,
|
|
366
|
+
ocapSecp256k1Address: identities.ocapSecp256k1Address,
|
|
367
|
+
ocapSecp256k1Did: identities.ocapSecp256k1Did,
|
|
368
|
+
publicKeyComp33: derivedChild.childPublicKeyComp33,
|
|
369
|
+
publicKeyCompHex: `0x${(0, _ocap_mpc_hsm_personal.bytesToHex)(derivedChild.childPublicKeyComp33)}`,
|
|
370
|
+
ethUncompressedPublicKeyHex: identities.ethUncompressedPkHex,
|
|
371
|
+
secp256k1UncompressedPublicKeyHex: identities.secp256k1UncompressedPkHex,
|
|
372
|
+
childDerivation: derivedChild.derivationSpec,
|
|
373
|
+
signEip1559Transaction,
|
|
374
|
+
sendEip1559Transaction,
|
|
375
|
+
signOcapTransaction,
|
|
376
|
+
signArcblockJwt,
|
|
377
|
+
signArcblockDelegationToken,
|
|
378
|
+
toEip1193Provider,
|
|
379
|
+
toViemLocalAccount: () => eoaSigner.toViemLocalAccount(),
|
|
380
|
+
toOcapWallet
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
//#endregion
|
|
385
|
+
exports.createPersonalHsmWallet = createPersonalHsmWallet;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { GatewayPrepareAuthProof, TxBroadcaster } from "./broadcaster.cjs";
|
|
2
|
+
import { Eip1559UnsignedTxFields, FirewallCommercialTier, MpcAggregatedSignature, MpcChildDerivationSpec, PersonalChildDerivationInput, PersonalClearSigningPreview, PersonalCosignerTransport, PersonalMpcEoaSigner, PersonalPolicyProofBundle, PersonalPostConditions, PersonalSignedEip1559Result, PersonalVaultPolicy, RandomBytesFn, SignedFirewallAttestation, SimulatedBalanceDelta, VaultEntitlementTicket } from "@ocap/mpc-hsm/personal";
|
|
3
|
+
import { WalletObject } from "@ocap/wallet";
|
|
4
|
+
|
|
5
|
+
//#region src/personal-hsm-wallet.d.ts
|
|
6
|
+
|
|
7
|
+
interface PersonalHsmSignOptions {
|
|
8
|
+
readonly policyProof?: PersonalPolicyProofBundle;
|
|
9
|
+
readonly entitlementTicket?: VaultEntitlementTicket;
|
|
10
|
+
readonly postConditions?: PersonalPostConditions;
|
|
11
|
+
readonly simulatedDelta?: SimulatedBalanceDelta;
|
|
12
|
+
readonly firewallAttestation?: SignedFirewallAttestation;
|
|
13
|
+
readonly attestedProxyImplementationAddress?: string;
|
|
14
|
+
}
|
|
15
|
+
interface PersonalHsmSendEip1559Options extends PersonalHsmSignOptions {
|
|
16
|
+
readonly tier?: FirewallCommercialTier;
|
|
17
|
+
readonly knownCounterparties?: ReadonlyArray<string>;
|
|
18
|
+
readonly authProof?: GatewayPrepareAuthProof;
|
|
19
|
+
readonly preferPrivateMevRpc?: boolean;
|
|
20
|
+
}
|
|
21
|
+
interface PersonalSignedOcapResult {
|
|
22
|
+
readonly derSignatureHex: string;
|
|
23
|
+
readonly signature: MpcAggregatedSignature;
|
|
24
|
+
readonly signingDigest32: Uint8Array;
|
|
25
|
+
readonly clearSigningPreview: PersonalClearSigningPreview;
|
|
26
|
+
readonly clearSigningPreviewHash32: Uint8Array;
|
|
27
|
+
}
|
|
28
|
+
interface PersonalHsmWallet {
|
|
29
|
+
readonly address: string;
|
|
30
|
+
readonly ocapSecp256k1Address: string;
|
|
31
|
+
readonly ocapSecp256k1Did: string;
|
|
32
|
+
readonly publicKeyComp33: Uint8Array;
|
|
33
|
+
readonly publicKeyCompHex: string;
|
|
34
|
+
readonly ethUncompressedPublicKeyHex: string;
|
|
35
|
+
readonly secp256k1UncompressedPublicKeyHex: string;
|
|
36
|
+
readonly childDerivation: MpcChildDerivationSpec;
|
|
37
|
+
signEip1559Transaction(tx: Eip1559UnsignedTxFields, options?: PersonalHsmSignOptions): Promise<PersonalSignedEip1559Result>;
|
|
38
|
+
sendEip1559Transaction(tx: Eip1559UnsignedTxFields, options?: PersonalHsmSendEip1559Options): Promise<{
|
|
39
|
+
readonly txHashHex: string;
|
|
40
|
+
readonly via: string;
|
|
41
|
+
readonly signed: PersonalSignedEip1559Result;
|
|
42
|
+
readonly firewallAttestation?: SignedFirewallAttestation;
|
|
43
|
+
}>;
|
|
44
|
+
readonly signOcapTransaction: (rawUnsignedCborBytes: Uint8Array | string, options?: PersonalHsmSignOptions & {
|
|
45
|
+
readonly nowSec?: bigint;
|
|
46
|
+
}) => Promise<PersonalSignedOcapResult>;
|
|
47
|
+
signArcblockJwt(payload?: Record<string, unknown>, options?: PersonalHsmSignOptions & {
|
|
48
|
+
readonly didType?: 'ethereum' | 'secp256k1';
|
|
49
|
+
readonly nowSec?: bigint;
|
|
50
|
+
}): Promise<string>;
|
|
51
|
+
signArcblockDelegationToken(payload: {
|
|
52
|
+
sub: string;
|
|
53
|
+
ops: string[];
|
|
54
|
+
deny?: string[];
|
|
55
|
+
delegation?: string;
|
|
56
|
+
iat?: string;
|
|
57
|
+
nbf?: string;
|
|
58
|
+
exp?: string;
|
|
59
|
+
}, options: PersonalHsmSignOptions & {
|
|
60
|
+
readonly didType?: 'ethereum' | 'secp256k1';
|
|
61
|
+
readonly nowSec?: bigint;
|
|
62
|
+
}): Promise<string>;
|
|
63
|
+
toEip1193Provider(options?: {
|
|
64
|
+
readonly defaultSendOptions?: PersonalHsmSendEip1559Options | (() => PersonalHsmSendEip1559Options | undefined);
|
|
65
|
+
}): Pick<PersonalMpcEoaSigner, 'request'>;
|
|
66
|
+
toViemLocalAccount(): ReturnType<PersonalMpcEoaSigner['toViemLocalAccount']>;
|
|
67
|
+
toOcapWallet(options?: {
|
|
68
|
+
readonly didType?: 'ethereum' | 'secp256k1';
|
|
69
|
+
readonly defaultSignOptions?: () => PersonalHsmSignOptions | undefined;
|
|
70
|
+
}): WalletObject<string>;
|
|
71
|
+
}
|
|
72
|
+
declare function createPersonalHsmWallet(params: {
|
|
73
|
+
readonly vaultId: string;
|
|
74
|
+
readonly slotId: string;
|
|
75
|
+
readonly policyEpoch: bigint;
|
|
76
|
+
readonly rootPublicKeyComp33: Uint8Array | string;
|
|
77
|
+
readonly xActorComp33?: Uint8Array | string;
|
|
78
|
+
readonly getActorShareBytes32: () => Promise<Uint8Array> | Uint8Array;
|
|
79
|
+
readonly policy?: PersonalVaultPolicy;
|
|
80
|
+
readonly childDerivation?: PersonalChildDerivationInput;
|
|
81
|
+
readonly transport: PersonalCosignerTransport;
|
|
82
|
+
readonly broadcaster?: TxBroadcaster;
|
|
83
|
+
readonly defaultSendOptions?: () => PersonalHsmSendEip1559Options | undefined;
|
|
84
|
+
readonly nowSec?: () => bigint;
|
|
85
|
+
readonly rng?: RandomBytesFn;
|
|
86
|
+
}): PersonalHsmWallet;
|
|
87
|
+
//#endregion
|
|
88
|
+
export { PersonalHsmSendEip1559Options, PersonalHsmSignOptions, PersonalHsmWallet, PersonalSignedOcapResult, createPersonalHsmWallet };
|
package/package.json
CHANGED
|
@@ -1,6 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/wallet-hsm",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "1.33.7",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Isomorphic keyless HSM & 2-of-2 Passkey/Agent MPC wallet SDK bridging @ocap/mpc-hsm/personal, Logical-HSM SigningBox, Multi-Chain Firewall Relay, EIP-1193/viem, and @ocap/wallet",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"arcblock",
|
|
8
|
+
"blockchain",
|
|
9
|
+
"wallet",
|
|
10
|
+
"hsm",
|
|
11
|
+
"mpc",
|
|
12
|
+
"passkey",
|
|
13
|
+
"clear-signing"
|
|
14
|
+
],
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"author": {
|
|
19
|
+
"name": "ArcBlock",
|
|
20
|
+
"email": "engineering@arcblock.io",
|
|
21
|
+
"url": "https://www.arcblock.io"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/ArcBlock/blockchain/tree/master/core/wallet-hsm",
|
|
24
|
+
"license": "Apache-2.0",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"main": "./lib/index.cjs",
|
|
27
|
+
"module": "./esm/index.mjs",
|
|
28
|
+
"types": "./esm/index.d.mts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./esm/index.d.mts",
|
|
32
|
+
"import": "./esm/index.mjs",
|
|
33
|
+
"default": "./lib/index.cjs"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"lib",
|
|
38
|
+
"esm"
|
|
39
|
+
],
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@arcblock/did": "1.33.7",
|
|
42
|
+
"@arcblock/jwt": "1.33.7",
|
|
43
|
+
"@noble/curves": "2.2.0",
|
|
44
|
+
"@noble/hashes": "2.2.0",
|
|
45
|
+
"@ocap/mcrypto": "1.33.7",
|
|
46
|
+
"@ocap/mpc-hsm": "1.33.7",
|
|
47
|
+
"@ocap/util": "1.33.7",
|
|
48
|
+
"@ocap/wallet": "1.33.7"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/node": "^22.7.5",
|
|
52
|
+
"tsdown": "^0.18.4"
|
|
53
|
+
},
|
|
54
|
+
"repository": {
|
|
55
|
+
"type": "git",
|
|
56
|
+
"url": "git+https://github.com/ArcBlock/blockchain.git"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"lint": "biome check",
|
|
60
|
+
"lint:fix": "biome check --write",
|
|
61
|
+
"test": "bun test",
|
|
62
|
+
"coverage": "npm run test -- --coverage",
|
|
63
|
+
"build": "tsdown",
|
|
64
|
+
"build:watch": "tsdown -w",
|
|
65
|
+
"clean": "rm -rf lib esm"
|
|
66
|
+
},
|
|
67
|
+
"bugs": {
|
|
68
|
+
"url": "https://github.com/ArcBlock/blockchain/issues"
|
|
69
|
+
}
|
|
70
|
+
}
|