@haven_ai/sdk 0.6.0-alpha.0 → 0.8.0-alpha.0
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 +33 -16
- package/dist/{base64-cD9kR4JL.d.cts → base64-znjJJQqu.d.cts} +457 -18
- package/dist/{base64-cD9kR4JL.d.ts → base64-znjJJQqu.d.ts} +457 -18
- package/dist/{chunk-63GPTEEY.js → chunk-H4APV67M.js} +90 -9
- package/dist/chunk-H4APV67M.js.map +1 -0
- package/dist/{chunk-S42QSWJY.cjs → chunk-HX34HOW4.cjs} +93 -8
- package/dist/chunk-HX34HOW4.cjs.map +1 -0
- package/dist/{chunk-3WSEX3GW.js → chunk-UXULZ3ZY.js} +186 -17
- package/dist/chunk-UXULZ3ZY.js.map +1 -0
- package/dist/{chunk-CK4NLLMO.cjs → chunk-WJFDXCYP.cjs} +223 -40
- package/dist/chunk-WJFDXCYP.cjs.map +1 -0
- package/dist/edge.cjs +317 -106
- package/dist/edge.cjs.map +1 -1
- package/dist/edge.d.cts +268 -2
- package/dist/edge.d.ts +268 -2
- package/dist/edge.js +196 -45
- package/dist/edge.js.map +1 -1
- package/dist/index.cjs +957 -391
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +181 -40
- package/dist/index.d.ts +181 -40
- package/dist/index.js +658 -113
- package/dist/index.js.map +1 -1
- package/dist/test-support.cjs +18 -18
- package/dist/test-support.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-3WSEX3GW.js.map +0 -1
- package/dist/chunk-63GPTEEY.js.map +0 -1
- package/dist/chunk-CK4NLLMO.cjs.map +0 -1
- package/dist/chunk-S42QSWJY.cjs.map +0 -1
package/dist/edge.d.ts
CHANGED
|
@@ -1,7 +1,177 @@
|
|
|
1
|
-
|
|
1
|
+
import { av as HavenSigningError, ck as DelegationForHashing } from './base64-znjJJQqu.js';
|
|
2
|
+
export { Z as AgentPaymentFailureCode, a0 as AgentPaymentNextAction, cl as CAVEAT_ENFORCERS, cm as CAVEAT_TYPEHASH, cn as DELEGATION_MANAGER, co as DELEGATION_TYPEHASH, cp as DIRECT_PAYMENT_CHAIN_IDS, ag as DIRECT_SIGN_CONTEXT_VERSION, ah as ENTRY_POINT_V07, cq as EXECUTE_ABI, aj as HAVEN_CLIENT_HEADER, ak as HAVEN_CONNECTOR_CHANNEL, al as HAVEN_MINIMUM_NODE_VERSION, cr as HYBRID_DELEGATOR_IMPLEMENTATION, ar as HavenApiError, a as HavenClientUpdate, M as HavenError, ax as HavenTypedDataRefusedError, ay as HavenUnsupportedSignerVersionError, az as HavenUserOpBindingError, cs as MAX_SETTLEMENT_WINDOW_SECONDS, aB as MAX_TASK_BUDGET_TTL_SECONDS, aG as NextStep, aI as NextStepHandoff, aK as NextStepTarget, aS as ROOT_AUTHORITY, aV as SIGNER_UPDATE_FALLBACK, ct as SIMPLE_FACTORY_ADDRESS, a_ as SettlementChildExpectation, cu as SettlementChildTypedData, a$ as SignerRefusalCode, j as SweepAuthorization, b3 as SweepExpectedAuth, b7 as TASK_SIGN_CONTEXT_VERSION, b9 as TYPED_DATA_NOT_ALLOWED, ba as TaskBudgetCloseExpectation, bb as TaskChildExpectation, bc as TaskChildTypedData, bg as X402ExpectedAuth, bk as X402PaymentOption, X as X402PaymentRequired, bw as assertBoundDirectPaymentUserOp, cv as assertFundingLegPaysDelegate, cw as assertOwnSettlementChild, bx as assertOwnTaskBudgetCloseUserOp, by as assertOwnTaskChild, bz as assertUserOpTypedDataBinding, bA as buildSweepAuthorizationMessage, bB as buildSweepTypedData, bC as buildX402ExpectedMessage, cx as chainIdForNetwork, bE as connectorRerunCommand, bG as connectorUpgradeCommand, bH as createNextStepBuilder, bI as decodeBase64Json, bL as deriveDelegateAccountAddress, bM as encodeBase64Json, bP as hashDelegation, bQ as havenClientIdentity, bT as isPackedUserOperationTypedData, cy as isSettlementChildTypedData, bU as isSupportedNodeVersion, bW as isTaskChildTypedData, bY as packedUserOperationHash, c0 as readClientUpdate, c7 as selectStandardPaymentOption, cc as toStandardPaymentRequirements, cd as unsupportedNodeVersionMessage, cf as verifySettlementChild, ch as x402AuthorizationAmount, cj as x402V2PaymentEnvelope } from './base64-znjJJQqu.js';
|
|
2
3
|
import { Hex, Address } from 'viem';
|
|
3
4
|
import 'x402/types';
|
|
4
5
|
|
|
6
|
+
/**
|
|
7
|
+
* The x402 buyer-side tax declaration (#3427, wg-tax #5 §2.2
|
|
8
|
+
* `buyer-side-tax-declaration.md`).
|
|
9
|
+
*
|
|
10
|
+
* The agent's own payment key — the same delegate EOA that signs the
|
|
11
|
+
* EIP-3009 payment authorization — signs the §2.1 declaration content, and
|
|
12
|
+
* the signed declaration travels to the SELLER ONLY, as
|
|
13
|
+
* `X-Tax-Declaration: <base64url(JSON)>` on the paid merchant retry
|
|
14
|
+
* (`MerchantCompletion.retryRequest`). It never reaches Haven or any
|
|
15
|
+
* facilitator, and it is never part of the payment payload.
|
|
16
|
+
*
|
|
17
|
+
* Why this lives on the edge (`docs/architecture/07-edge-signer.md`, #3283's
|
|
18
|
+
* shared-guard rule): the declaration MUST be signed by the key that signs
|
|
19
|
+
* the payment authorization (§2.2), and on the EIP-3009 bridge that key is
|
|
20
|
+
* held by this local SDK process. The builder therefore lives in
|
|
21
|
+
* `@haven_ai/sdk/edge` beside the other signing guards, and every property
|
|
22
|
+
* the threat model requires is enforced HERE, not by callers:
|
|
23
|
+
*
|
|
24
|
+
* - The EIP-712 typed data is built LOCALLY from the §2.1 fields. Typed data
|
|
25
|
+
* is never accepted from Haven — the wire content is validated to exactly
|
|
26
|
+
* the five §2.1 fields, and anything else (including a `principalId` or
|
|
27
|
+
* `principalAttributionHash` arriving pre-set) is REFUSED, never stripped
|
|
28
|
+
* and continued. A polluted declaration aborts the payment loudly rather
|
|
29
|
+
* than being silently laundered into a signature.
|
|
30
|
+
* - `principalId = did:pkh:<CAIP-2 network>:<delegate EOA>` — the chain
|
|
31
|
+
* comes from the ACCEPTED payment option's `network` (§6.6.5 Equality),
|
|
32
|
+
* never from configuration. An alias (`base`) is resolved through the same
|
|
33
|
+
* `chainIdForNetwork` map the settlement child uses; a network with no
|
|
34
|
+
* chain id refuses, so an unknown chain cannot mint a mis-scoped
|
|
35
|
+
* principal.
|
|
36
|
+
* - `principalAttributionHash = "sha-256:<hex>"` over the JCS-canonical bytes
|
|
37
|
+
* of `{ principalId }` (§6.6.2, §6.6.5 Effect).
|
|
38
|
+
*
|
|
39
|
+
* Residual accepted by the owner (issue threat model, 2026-09-28): Haven
|
|
40
|
+
* supplies `jurisdiction`, `taxableStatus` and `taxId`, so a compromised
|
|
41
|
+
* Haven could produce a declaration with a wrong VAT number. Nothing moves
|
|
42
|
+
* funds, the seller checks the VAT number in VIES, and the values are the
|
|
43
|
+
* owner's own onboarding data. The SDK logs the `taxId` it signs (stderr, so
|
|
44
|
+
* an MCP stdio session's JSON-RPC framing on stdout is untouched).
|
|
45
|
+
*/
|
|
46
|
+
/**
|
|
47
|
+
* PROVISIONAL EIP-712 domain (wg-tax PR #5 §2.2: the domain and types
|
|
48
|
+
* "remain to be defined"). No chainId and no salt: the declaration is
|
|
49
|
+
* portable across the settlement networks a seller may accept, and the
|
|
50
|
+
* principal is already chain-scoped by `principalId`. Recorded in the PR
|
|
51
|
+
* body for the working group; changing these constants changes every
|
|
52
|
+
* signature produced, so they are pinned by the fixed-vector tests.
|
|
53
|
+
*/
|
|
54
|
+
declare const TAX_DECLARATION_DOMAIN: {
|
|
55
|
+
readonly name: "x402 Tax Declaration";
|
|
56
|
+
readonly version: "1";
|
|
57
|
+
};
|
|
58
|
+
/** The declaration's EIP-712 primary type name. */
|
|
59
|
+
declare const TAX_DECLARATION_PRIMARY_TYPE = "TaxDeclaration";
|
|
60
|
+
/**
|
|
61
|
+
* PROVISIONAL typed-data types for the signed §2.1 declaration plus the two
|
|
62
|
+
* locally computed principal fields. Field order here is the ABI encoding
|
|
63
|
+
* order of every signature produced by this module.
|
|
64
|
+
*/
|
|
65
|
+
declare const TAX_DECLARATION_TYPES: {
|
|
66
|
+
readonly TaxDeclaration: readonly [{
|
|
67
|
+
readonly name: "version";
|
|
68
|
+
readonly type: "string";
|
|
69
|
+
}, {
|
|
70
|
+
readonly name: "jurisdiction";
|
|
71
|
+
readonly type: "string";
|
|
72
|
+
}, {
|
|
73
|
+
readonly name: "taxableStatus";
|
|
74
|
+
readonly type: "string";
|
|
75
|
+
}, {
|
|
76
|
+
readonly name: "taxId";
|
|
77
|
+
readonly type: "string";
|
|
78
|
+
}, {
|
|
79
|
+
readonly name: "validUntil";
|
|
80
|
+
readonly type: "uint256";
|
|
81
|
+
}, {
|
|
82
|
+
readonly name: "principalId";
|
|
83
|
+
readonly type: "string";
|
|
84
|
+
}, {
|
|
85
|
+
readonly name: "principalAttributionHash";
|
|
86
|
+
readonly type: "string";
|
|
87
|
+
}];
|
|
88
|
+
};
|
|
89
|
+
/** The x402 header this module's output is serialised into. */
|
|
90
|
+
declare const X_TAX_DECLARATION_HEADER = "X-Tax-Declaration";
|
|
91
|
+
/**
|
|
92
|
+
* The §2.1 unsigned content exactly as `#3426`'s content endpoint emits it
|
|
93
|
+
* (`GET /agents/:id/tax-declaration`, `{ available: true, declaration }`).
|
|
94
|
+
*/
|
|
95
|
+
interface TaxDeclarationContent {
|
|
96
|
+
/** §2.1 version discriminator, e.g. `x402-tax-1`. */
|
|
97
|
+
version: string;
|
|
98
|
+
/** ISO 3166-1 alpha-2 country code. */
|
|
99
|
+
jurisdiction: string;
|
|
100
|
+
/** §2.1's closed enumeration, e.g. `TAXABLE_PERSON`. */
|
|
101
|
+
taxableStatus: string;
|
|
102
|
+
/** The owner's VAT number, normalised by the backend. */
|
|
103
|
+
taxId: string;
|
|
104
|
+
/** Integer milliseconds — the declaration's validity bound. */
|
|
105
|
+
validUntil: number;
|
|
106
|
+
}
|
|
107
|
+
/** The signed declaration object carried (base64url-encoded) in the header. */
|
|
108
|
+
interface SignedTaxDeclaration extends TaxDeclarationContent {
|
|
109
|
+
/** `did:pkh:<CAIP-2 network>:<delegate EOA>` — computed locally. */
|
|
110
|
+
principalId: string;
|
|
111
|
+
/** `sha-256:<hex>` over the JCS bytes of `{ principalId }`. */
|
|
112
|
+
principalAttributionHash: string;
|
|
113
|
+
}
|
|
114
|
+
interface SignedTaxDeclarationResult {
|
|
115
|
+
declaration: SignedTaxDeclaration;
|
|
116
|
+
/** EIP-712 signature (65-byte `r‖s‖v`, 27/28) over the typed declaration. */
|
|
117
|
+
signature: string;
|
|
118
|
+
}
|
|
119
|
+
/** `sha-256:<hex>` over the UTF-8 JCS bytes of `{ principalId }` (§6.6.2). */
|
|
120
|
+
declare function principalAttributionHash(principalId: string): string;
|
|
121
|
+
/**
|
|
122
|
+
* `did:pkh:<CAIP-2 network>:<address>` from the accepted option's `network`.
|
|
123
|
+
*
|
|
124
|
+
* The chain comes from the settlement's network — a `eip155:<n>` value passes
|
|
125
|
+
* through verbatim, the x402 aliases (`base`, `base-sepolia`) resolve through
|
|
126
|
+
* the same `chainIdForNetwork` map the settlement child uses, and anything
|
|
127
|
+
* else refuses: a network this SDK cannot name cannot produce a principal
|
|
128
|
+
* that names its own chain.
|
|
129
|
+
*/
|
|
130
|
+
declare function taxPrincipalId(network: string, delegateAddress: string): string;
|
|
131
|
+
/**
|
|
132
|
+
* Build the EIP-712 typed data for the §2.1 content LOCALLY, set the two
|
|
133
|
+
* principal fields, and sign with the delegate key.
|
|
134
|
+
*
|
|
135
|
+
* The caller passes the raw content from `#3426`'s endpoint (parsed JSON) —
|
|
136
|
+
* `validateContent` refuses anything beyond the five §2.1 fields before any
|
|
137
|
+
* principal is computed or any byte is signed. The signature recovers to the
|
|
138
|
+
* delegate EOA (pinned by the sign+recover test), which is the §2.2
|
|
139
|
+
* requirement: the declaration signer is the payment authorizer.
|
|
140
|
+
*/
|
|
141
|
+
declare function buildSignedTaxDeclaration(input: {
|
|
142
|
+
/** The UNSIGNED §2.1 content from #3426's endpoint — `declaration` field of the response. */
|
|
143
|
+
content: unknown;
|
|
144
|
+
/** The ACCEPTED payment option's network — the only chain source (§6.6.5). */
|
|
145
|
+
network: string;
|
|
146
|
+
/** The agent's delegate key — the same key that signs the EIP-3009 authorization. */
|
|
147
|
+
delegateKey: string;
|
|
148
|
+
}): SignedTaxDeclarationResult;
|
|
149
|
+
/**
|
|
150
|
+
* Recompute the EIP-712 digest for a signed declaration and check the
|
|
151
|
+
* signature recovers to `expectedAddress` — the seller-side verification
|
|
152
|
+
* this SDK runs on itself in tests, kept next to the builder so the digest
|
|
153
|
+
* rule cannot drift from the signing one.
|
|
154
|
+
*/
|
|
155
|
+
declare function verifyTaxDeclarationSignature(declaration: SignedTaxDeclaration, signature: string, expectedAddress: string): boolean;
|
|
156
|
+
/**
|
|
157
|
+
* The base64url(JSON) header value for the paid merchant retry. The JSON is
|
|
158
|
+
* the signed declaration object with the `signature` field alongside — one
|
|
159
|
+
* object, so the seller decodes, re-derives the digest from the known
|
|
160
|
+
* provisional domain/types, and recovers the signer.
|
|
161
|
+
*/
|
|
162
|
+
declare function encodeTaxDeclarationHeader(result: SignedTaxDeclarationResult): string;
|
|
163
|
+
/**
|
|
164
|
+
* A refused declaration: the content was not exactly the locally built shape
|
|
165
|
+
* (a pre-set principal field, an unknown field, a malformed value) or the
|
|
166
|
+
* settlement network could not be named. Extends `HavenSigningError` so the
|
|
167
|
+
* existing signing-error handling catches it; the `code` lets callers tell
|
|
168
|
+
* the refusal apart from a key problem.
|
|
169
|
+
*/
|
|
170
|
+
declare class TaxDeclarationRefusedError extends HavenSigningError {
|
|
171
|
+
readonly code = "TAX_DECLARATION_REFUSED";
|
|
172
|
+
constructor(message: string);
|
|
173
|
+
}
|
|
174
|
+
|
|
5
175
|
declare function addressFromKey(privateKey: string): string;
|
|
6
176
|
declare function signHash(privateKey: string, hash: string): string;
|
|
7
177
|
declare function verifySignature(hash: string, signature: string, expectedAddress: string): boolean;
|
|
@@ -54,6 +224,19 @@ declare function verifySignature(hash: string, signature: string, expectedAddres
|
|
|
54
224
|
* documents). Any OTHER two-link chain — a leaf delegated by a third party, a
|
|
55
225
|
* leaf whose delegate is not this signer's own account, a chain longer than
|
|
56
226
|
* two — is refused exactly as before.
|
|
227
|
+
*
|
|
228
|
+
* SUB-BUDGETS (#3330) widen it once more, to exactly ONE, TWO or THREE
|
|
229
|
+
* links: `[sub grant, sub parent-child, budget]` is the third shape. The
|
|
230
|
+
* signer here is agent B's signer (B redeems its own grant), so the leaf's
|
|
231
|
+
* `delegate` is this signer's own account and its `delegator` is agent A's
|
|
232
|
+
* account — a REAL grant from another account, not a self-delegation. The
|
|
233
|
+
* middle link is A's parent-child (delegate == A's account, delegator == A's
|
|
234
|
+
* account: A's SELF-narrowing, the task-child shape class), and the root
|
|
235
|
+
* keeps the one-link shape (delegate == A's account, delegator != A). The
|
|
236
|
+
* AND-only property again makes the three-link chain AS OR MORE restrictive
|
|
237
|
+
* than the two-hop task chain; a chain longer than three, a leaf whose
|
|
238
|
+
* delegate is not this signer, or a middle link that is not a self-delegation
|
|
239
|
+
* is refused exactly as before.
|
|
57
240
|
*/
|
|
58
241
|
|
|
59
242
|
/** `Delegation` — `(address delegate, address delegator, bytes32 authority, Caveat[] caveats, uint256 salt, bytes signature)`. */
|
|
@@ -122,4 +305,87 @@ declare const SINGLE_DEFAULT_MODE: Hex;
|
|
|
122
305
|
*/
|
|
123
306
|
declare function assertRedeemsOwnBudgetDelegation(redeemCalldata: Hex, ownAccount: Address): void;
|
|
124
307
|
|
|
125
|
-
|
|
308
|
+
/**
|
|
309
|
+
* Sub-budget child guards (#3330) — the SDK-side local verification the
|
|
310
|
+
* signer runs before its delegate key signs a sub-budget child, mirroring
|
|
311
|
+
* `task-budget-guards.ts`'s `assertOwnTaskChild` for the two-row sub-budget
|
|
312
|
+
* tree. ONE verifier covers BOTH rows, because the same signer (the
|
|
313
|
+
* delegating agent A's own key) signs both:
|
|
314
|
+
*
|
|
315
|
+
* - A's PARENT-CHILD row: `delegate === delegator === A's own account`
|
|
316
|
+
* (self-delegated, the task-child shape class).
|
|
317
|
+
* - B's GRANT row: `delegator === A's own account`, `delegate === B's
|
|
318
|
+
* delegate account` (a real grant to another account).
|
|
319
|
+
*
|
|
320
|
+
* What makes a sub-budget child a sub-budget child (vs a task budget's
|
|
321
|
+
* hard-cap child) is the AMOUNT caveat: an `ERC20PeriodTransferEnforcer`
|
|
322
|
+
* caveat whose terms are `abi.encode(address token, uint256 periodAmount,
|
|
323
|
+
* uint256 periodDuration, uint256 startDate)` — the period-scoped slice of
|
|
324
|
+
* the parent's own period meter, with the parent's periodDuration and
|
|
325
|
+
* startDate reused verbatim. A hard-cap `ERC20TransferAmountEnforcer` caveat
|
|
326
|
+
* is refused: that would be a task budget, which this channel must never
|
|
327
|
+
* sign (two different products, two different sign-context endpoints).
|
|
328
|
+
*
|
|
329
|
+
* Authority, timestamp and recipient-pin checks are byte-for-byte the
|
|
330
|
+
* task-child discipline: authority must equal the declared parent hash and
|
|
331
|
+
* never be ROOT; expiry must be in the future and equal the declared one; a
|
|
332
|
+
* pinned recipient must match exactly, and an unpinned budget must not grow
|
|
333
|
+
* one. Enforcers are the pinned constants from `settlement-child.ts` (no kit
|
|
334
|
+
* dependency) — same reasoning as that file's header documents.
|
|
335
|
+
*/
|
|
336
|
+
|
|
337
|
+
/** The pinned `ERC20PeriodTransferEnforcer` (bytecode-identical on Base + Base Sepolia). */
|
|
338
|
+
declare const ERC20_PERIOD_TRANSFER_ENFORCER = "0x474e3Ae7E169e940607cC624Da8A15Eb120139aB";
|
|
339
|
+
interface SubBudgetChildTypedData {
|
|
340
|
+
domain: {
|
|
341
|
+
name?: string;
|
|
342
|
+
version?: string;
|
|
343
|
+
chainId?: number | string;
|
|
344
|
+
verifyingContract?: string;
|
|
345
|
+
};
|
|
346
|
+
types: Record<string, unknown>;
|
|
347
|
+
primaryType: string;
|
|
348
|
+
message: Record<string, unknown>;
|
|
349
|
+
}
|
|
350
|
+
/** The struct hash of a sub-budget child — the redemption chain's link value. */
|
|
351
|
+
declare function hashSubBudgetDelegation(delegation: DelegationForHashing): Hex;
|
|
352
|
+
|
|
353
|
+
interface SubBudgetChildExpectation {
|
|
354
|
+
chainId: number;
|
|
355
|
+
tokenAddress: string;
|
|
356
|
+
/** Per-period amount in atomic units — the child's periodAmount. */
|
|
357
|
+
periodAmountAtomic: string;
|
|
358
|
+
/** The PARENT's period duration (seconds), reused verbatim in the caveat. */
|
|
359
|
+
periodDurationSeconds: number;
|
|
360
|
+
/** The PARENT's period anchor, reused verbatim in the caveat. */
|
|
361
|
+
startDate: number;
|
|
362
|
+
recipientAddress: string | null;
|
|
363
|
+
/** unix seconds — the `TimestampEnforcer`'s `beforeThreshold`. */
|
|
364
|
+
expiresAt: number;
|
|
365
|
+
/** The hash of the row's parent delegation (`parent_delegation_hash`). */
|
|
366
|
+
parentDelegationHash: string;
|
|
367
|
+
/**
|
|
368
|
+
* A's own derived delegate account — the child's `delegator` on BOTH rows.
|
|
369
|
+
*/
|
|
370
|
+
delegateAccount: string;
|
|
371
|
+
/**
|
|
372
|
+
* The row's own `delegate`: A's account again for the parent-child row,
|
|
373
|
+
* B's delegate account for the grant row. Served in the sign-context's
|
|
374
|
+
* `expected` (read from the stored child JSON, not trusted caller input).
|
|
375
|
+
*/
|
|
376
|
+
childDelegateAccount: string;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Verify a sub-budget child delegation is EXACTLY what
|
|
380
|
+
* `GET /sub-budgets/:id/sign-context` (purpose `open`) declared, that its
|
|
381
|
+
* `delegator` is this signer's own account, and that its `delegate` is the
|
|
382
|
+
* declared child delegate. Throws `HavenTypedDataRefusedError` (never
|
|
383
|
+
* returns) on any disagreement.
|
|
384
|
+
*/
|
|
385
|
+
declare function assertOwnSubBudgetChild(typedData: unknown, expected: SubBudgetChildExpectation, delegateAddress: string, now?: number): void;
|
|
386
|
+
/** Close UserOp verification — byte-for-byte the task-budget discipline. */
|
|
387
|
+
declare function assertOwnSubBudgetCloseUserOp(typedData: Record<string, unknown>, expected: {
|
|
388
|
+
delegationHash: string;
|
|
389
|
+
}, delegateAddress: string): void;
|
|
390
|
+
|
|
391
|
+
export { DELEGATION_TUPLE_COMPONENTS, DelegationForHashing, ERC20_PERIOD_TRANSFER_ENFORCER, HavenSigningError, REDEEM_DELEGATIONS_ABI, SINGLE_DEFAULT_MODE, type SignedTaxDeclaration, type SignedTaxDeclarationResult, type SubBudgetChildExpectation, type SubBudgetChildTypedData, TAX_DECLARATION_DOMAIN, TAX_DECLARATION_PRIMARY_TYPE, TAX_DECLARATION_TYPES, type TaxDeclarationContent, TaxDeclarationRefusedError, X_TAX_DECLARATION_HEADER, addressFromKey, assertOwnSubBudgetChild, assertOwnSubBudgetCloseUserOp, assertRedeemsOwnBudgetDelegation, buildSignedTaxDeclaration, encodeTaxDeclarationHeader, hashSubBudgetDelegation, principalAttributionHash, signHash, taxPrincipalId, verifySignature, verifyTaxDeclarationSignature };
|
package/dist/edge.js
CHANGED
|
@@ -1,56 +1,207 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import { privateKeyToAddress, publicKeyToAddress } from 'viem/accounts';
|
|
6
|
-
import { serializeSignature } from 'viem';
|
|
1
|
+
import { hashDelegation, DIRECT_PAYMENT_CHAIN_IDS, assertOwnTaskBudgetCloseUserOp, HavenTypedDataRefusedError } from './chunk-UXULZ3ZY.js';
|
|
2
|
+
export { CAVEAT_TYPEHASH, DELEGATION_TYPEHASH, DIRECT_PAYMENT_CHAIN_IDS, EXECUTE_ABI, HAVEN_CLIENT_HEADER, HAVEN_MINIMUM_NODE_VERSION, HavenTypedDataRefusedError, MAX_TASK_BUDGET_TTL_SECONDS, TAX_DECLARATION_DOMAIN, TAX_DECLARATION_PRIMARY_TYPE, TAX_DECLARATION_TYPES, TYPED_DATA_NOT_ALLOWED, TaxDeclarationRefusedError, X_TAX_DECLARATION_HEADER, addressFromKey, assertBoundDirectPaymentUserOp, assertFundingLegPaysDelegate, assertOwnSettlementChild, assertOwnTaskBudgetCloseUserOp, assertOwnTaskChild, buildSignedTaxDeclaration, buildSweepAuthorizationMessage, buildSweepTypedData, buildX402ExpectedMessage, createNextStepBuilder, decodeBase64Json, encodeBase64Json, encodeTaxDeclarationHeader, hashDelegation, havenClientIdentity, isSupportedNodeVersion, isTaskChildTypedData, principalAttributionHash, readClientUpdate, selectStandardPaymentOption, signHash, taxPrincipalId, toStandardPaymentRequirements, unsupportedNodeVersionMessage, verifySignature, verifyTaxDeclarationSignature, x402AuthorizationAmount, x402V2PaymentEnvelope } from './chunk-UXULZ3ZY.js';
|
|
3
|
+
import { DELEGATION_MANAGER, deriveDelegateAccountAddress, ROOT_AUTHORITY, CAVEAT_ENFORCERS } from './chunk-H4APV67M.js';
|
|
4
|
+
export { AgentPaymentFailureCode, AgentPaymentNextAction, CAVEAT_ENFORCERS, DELEGATION_MANAGER, DELEGATION_TUPLE_COMPONENTS, DIRECT_SIGN_CONTEXT_VERSION, ENTRY_POINT_V07, HAVEN_CONNECTOR_CHANNEL, HYBRID_DELEGATOR_IMPLEMENTATION, HavenApiError, HavenError, HavenSigningError, HavenUnsupportedSignerVersionError, HavenUserOpBindingError, MAX_SETTLEMENT_WINDOW_SECONDS, REDEEM_DELEGATIONS_ABI, ROOT_AUTHORITY, SIGNER_UPDATE_FALLBACK, SIMPLE_FACTORY_ADDRESS, SINGLE_DEFAULT_MODE, SignerRefusalCode, TASK_SIGN_CONTEXT_VERSION, assertRedeemsOwnBudgetDelegation, assertUserOpTypedDataBinding, chainIdForNetwork, connectorRerunCommand, connectorUpgradeCommand, deriveDelegateAccountAddress, isPackedUserOperationTypedData, isSettlementChildTypedData, packedUserOperationHash, verifySettlementChild } from './chunk-H4APV67M.js';
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
// src/sub-budget-guards.ts
|
|
7
|
+
var ERC20_PERIOD_TRANSFER_ENFORCER = "0x474e3Ae7E169e940607cC624Da8A15Eb120139aB";
|
|
8
|
+
var DELEGATION_DOMAIN_NAME = "DelegationManager";
|
|
9
|
+
var DELEGATION_DOMAIN_VERSION = "1";
|
|
10
|
+
var EXPECTED_DELEGATION_TYPES = {
|
|
11
|
+
Caveat: [
|
|
12
|
+
{ name: "enforcer", type: "address" },
|
|
13
|
+
{ name: "terms", type: "bytes" }
|
|
14
|
+
],
|
|
15
|
+
Delegation: [
|
|
16
|
+
{ name: "delegate", type: "address" },
|
|
17
|
+
{ name: "delegator", type: "address" },
|
|
18
|
+
{ name: "authority", type: "bytes32" },
|
|
19
|
+
{ name: "caveats", type: "Caveat[]" },
|
|
20
|
+
{ name: "salt", type: "uint256" }
|
|
21
|
+
]
|
|
22
|
+
};
|
|
23
|
+
function same(a, b) {
|
|
24
|
+
return !!a && !!b && a.toLowerCase() === b.toLowerCase();
|
|
25
|
+
}
|
|
26
|
+
function sliceTerms(terms, start, end) {
|
|
27
|
+
const body = terms.startsWith("0x") ? terms.slice(2).toLowerCase() : terms.toLowerCase();
|
|
28
|
+
return end === void 0 ? body.slice(start * 2) : body.slice(start * 2, end * 2);
|
|
29
|
+
}
|
|
30
|
+
function findCaveat(caveats, enforcer) {
|
|
31
|
+
return caveats.find((c) => same(c.enforcer, enforcer));
|
|
14
32
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
33
|
+
function hashSubBudgetDelegation(delegation) {
|
|
34
|
+
return hashDelegation(delegation);
|
|
35
|
+
}
|
|
36
|
+
function refuseSubBudgetChild(what, detail) {
|
|
37
|
+
throw new HavenTypedDataRefusedError(
|
|
38
|
+
`Refusing to sign the sub-budget child: ${what}. ${detail} This signature opens a slice of the delegating agent budget for another agent, so it is verified locally against the expectation Haven declared, rather than trusted.`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
function assertOwnSubBudgetChild(typedData, expected, delegateAddress, now = Date.now()) {
|
|
42
|
+
const td = typedData;
|
|
43
|
+
if (td?.primaryType !== "Delegation") {
|
|
44
|
+
refuseSubBudgetChild("it is not a Delegation payload", `primaryType was '${String(td?.primaryType)}'.`);
|
|
45
|
+
}
|
|
46
|
+
if (!same(td.domain?.verifyingContract, DELEGATION_MANAGER)) {
|
|
47
|
+
refuseSubBudgetChild(
|
|
48
|
+
"the EIP-712 domain names an unknown DelegationManager",
|
|
49
|
+
`Expected ${DELEGATION_MANAGER}, got ${td.domain?.verifyingContract}.`
|
|
21
50
|
);
|
|
22
51
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return serializeSignature({
|
|
28
|
-
r: `0x${r.toString(16).padStart(64, "0")}`,
|
|
29
|
-
s: `0x${s.toString(16).padStart(64, "0")}`,
|
|
30
|
-
yParity: recovery
|
|
31
|
-
});
|
|
32
|
-
} catch (err) {
|
|
33
|
-
throw new HavenSigningError(
|
|
34
|
-
`Failed to sign hash: ${err instanceof Error ? err.message : String(err)}`
|
|
52
|
+
if (td.domain?.name !== DELEGATION_DOMAIN_NAME || td.domain?.version !== DELEGATION_DOMAIN_VERSION) {
|
|
53
|
+
refuseSubBudgetChild(
|
|
54
|
+
"the EIP-712 domain name/version does not match the pinned DelegationManager domain",
|
|
55
|
+
`Expected name '${DELEGATION_DOMAIN_NAME}' version '${DELEGATION_DOMAIN_VERSION}'; got name '${String(td.domain?.name)}' version '${String(td.domain?.version)}'.`
|
|
35
56
|
);
|
|
36
57
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
58
|
+
if (JSON.stringify(td.types?.Caveat) !== JSON.stringify(EXPECTED_DELEGATION_TYPES.Caveat) || JSON.stringify(td.types?.Delegation) !== JSON.stringify(EXPECTED_DELEGATION_TYPES.Delegation)) {
|
|
59
|
+
refuseSubBudgetChild(
|
|
60
|
+
"its EIP-712 type definitions do not match the pinned Delegation/Caveat shape",
|
|
61
|
+
"A renamed, reordered, or additional field on Delegation or Caveat could change what the caveats below mean without changing this check's field-by-field comparisons."
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
const domainChain = Number(td.domain?.chainId);
|
|
65
|
+
if (!Number.isFinite(domainChain) || !DIRECT_PAYMENT_CHAIN_IDS.has(domainChain)) {
|
|
66
|
+
refuseSubBudgetChild(
|
|
67
|
+
"it is scoped to a chain the delegation rail has no pinned contracts for",
|
|
68
|
+
`Chains with pinned contracts: ${[...DIRECT_PAYMENT_CHAIN_IDS].join(", ")}; the child says ${String(td.domain?.chainId)}.`
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
if (domainChain !== expected.chainId) {
|
|
72
|
+
refuseSubBudgetChild(
|
|
73
|
+
"it is scoped to the wrong chain",
|
|
74
|
+
`Expected chain ${expected.chainId}; the child says ${td.domain?.chainId}.`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
const ownAccount = deriveDelegateAccountAddress(delegateAddress);
|
|
78
|
+
const delegator = td.message?.delegator;
|
|
79
|
+
if (typeof delegator !== "string" || !same(delegator, ownAccount)) {
|
|
80
|
+
refuseSubBudgetChild(
|
|
81
|
+
"it is not delegated by this agent's own account",
|
|
82
|
+
`Expected delegator ${ownAccount}; the child names ${String(delegator)}.`
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
if (!same(delegator, expected.delegateAccount)) {
|
|
86
|
+
refuseSubBudgetChild(
|
|
87
|
+
"its delegator does not match the account Haven declared",
|
|
88
|
+
`Expected ${expected.delegateAccount}; the child names ${String(delegator)}.`
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
const delegate = td.message?.delegate;
|
|
92
|
+
if (typeof delegate !== "string" || !same(delegate, expected.childDelegateAccount)) {
|
|
93
|
+
refuseSubBudgetChild(
|
|
94
|
+
"its delegate does not match the declared child delegate",
|
|
95
|
+
`Expected ${expected.childDelegateAccount}; the child names ${String(delegate)}.`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
const authority = td.message?.authority;
|
|
99
|
+
if (typeof authority !== "string" || !/^0x[0-9a-fA-F]{64}$/.test(authority)) {
|
|
100
|
+
refuseSubBudgetChild("its authority is not a 32-byte delegation hash", `Got ${String(authority)}.`);
|
|
101
|
+
}
|
|
102
|
+
if (authority.toLowerCase() === ROOT_AUTHORITY) {
|
|
103
|
+
refuseSubBudgetChild(
|
|
104
|
+
"it is a ROOT delegation, not a re-delegation of the delegating agent budget",
|
|
105
|
+
"A sub-budget always chains under the delegating agent budget delegation."
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
if (!same(authority, expected.parentDelegationHash)) {
|
|
109
|
+
refuseSubBudgetChild(
|
|
110
|
+
"it chains under a different parent delegation",
|
|
111
|
+
`Expected authority ${expected.parentDelegationHash}; the child says ${authority}.`
|
|
112
|
+
);
|
|
51
113
|
}
|
|
114
|
+
const caveats = td.message?.caveats ?? [];
|
|
115
|
+
if (findCaveat(caveats, CAVEAT_ENFORCERS.erc20TransferAmount)) {
|
|
116
|
+
refuseSubBudgetChild(
|
|
117
|
+
"it carries a hard-cap transfer-amount caveat (the task-budget shape)",
|
|
118
|
+
"A sub-budget is period-scoped: its amount caveat must be an ERC20PeriodTransferEnforcer caveat slicing the parent period meter, never a lifetime cap."
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
const period = findCaveat(caveats, ERC20_PERIOD_TRANSFER_ENFORCER);
|
|
122
|
+
if (!period) {
|
|
123
|
+
refuseSubBudgetChild(
|
|
124
|
+
"it has no ERC-20 period-transfer caveat",
|
|
125
|
+
"Without it the child is not bounded to a per-period amount."
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
const token = `0x${sliceTerms(period.terms, 0, 20)}`;
|
|
129
|
+
if (!same(token, expected.tokenAddress)) {
|
|
130
|
+
refuseSubBudgetChild("it spends a different token", `Expected ${expected.tokenAddress}, child pins ${token}.`);
|
|
131
|
+
}
|
|
132
|
+
const periodAmount = BigInt(`0x${sliceTerms(period.terms, 20, 52)}`);
|
|
133
|
+
if (periodAmount !== BigInt(expected.periodAmountAtomic)) {
|
|
134
|
+
refuseSubBudgetChild(
|
|
135
|
+
"the period amount does not match",
|
|
136
|
+
`Expected ${expected.periodAmountAtomic}; the child allows ${periodAmount.toString()}.`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
const periodDuration = Number(BigInt(`0x${sliceTerms(period.terms, 52, 84)}`));
|
|
140
|
+
if (periodDuration !== expected.periodDurationSeconds) {
|
|
141
|
+
refuseSubBudgetChild(
|
|
142
|
+
"its period duration does not match the parent window Haven declared",
|
|
143
|
+
`Expected ${expected.periodDurationSeconds}s; the child says ${periodDuration}s. A sub-budget is a slice of the parent period meter \u2014 same window, smaller amount \u2014 never a different clock.`
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
const startDate = Number(BigInt(`0x${sliceTerms(period.terms, 84, 116)}`));
|
|
147
|
+
if (startDate !== expected.startDate) {
|
|
148
|
+
refuseSubBudgetChild(
|
|
149
|
+
"its period start does not match the parent anchor Haven declared",
|
|
150
|
+
`Expected ${expected.startDate}; the child says ${startDate}.`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
const timestamp = findCaveat(caveats, CAVEAT_ENFORCERS.timestamp);
|
|
154
|
+
if (!timestamp) {
|
|
155
|
+
refuseSubBudgetChild("it never expires", "A sub-budget child without a timestamp caveat is open-ended.");
|
|
156
|
+
}
|
|
157
|
+
const beforeThreshold = Number(BigInt(`0x${sliceTerms(timestamp.terms, 16, 32)}`));
|
|
158
|
+
const nowSec = Math.floor(now / 1e3);
|
|
159
|
+
if (beforeThreshold <= nowSec) {
|
|
160
|
+
refuseSubBudgetChild("it has already expired", `Expiry ${beforeThreshold} is not in the future.`);
|
|
161
|
+
}
|
|
162
|
+
if (beforeThreshold !== expected.expiresAt) {
|
|
163
|
+
refuseSubBudgetChild(
|
|
164
|
+
"its expiry does not match what Haven declared",
|
|
165
|
+
`Expected ${expected.expiresAt}; the child says ${beforeThreshold}.`
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
const calldata = findCaveat(caveats, CAVEAT_ENFORCERS.allowedCalldata);
|
|
169
|
+
if (expected.recipientAddress) {
|
|
170
|
+
if (!calldata) {
|
|
171
|
+
refuseSubBudgetChild(
|
|
172
|
+
"it has no recipient pin",
|
|
173
|
+
"The sub-budget was issued with a pinned recipient, so the child must carry one."
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
const startIndex = BigInt(`0x${sliceTerms(calldata.terms, 0, 32)}`);
|
|
177
|
+
if (startIndex !== 4n) {
|
|
178
|
+
refuseSubBudgetChild(
|
|
179
|
+
"the recipient pin points at the wrong calldata offset",
|
|
180
|
+
`Expected offset 4, got ${startIndex.toString()}.`
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
const paddedRecipient = sliceTerms(calldata.terms, 32, 64);
|
|
184
|
+
if (paddedRecipient.slice(0, 24) !== "0".repeat(24)) {
|
|
185
|
+
refuseSubBudgetChild("the pinned recipient is not a plain address", "Its 32-byte word is not a padded address.");
|
|
186
|
+
}
|
|
187
|
+
const recipient = `0x${paddedRecipient.slice(24)}`;
|
|
188
|
+
if (!same(recipient, expected.recipientAddress)) {
|
|
189
|
+
refuseSubBudgetChild(
|
|
190
|
+
"it pins a different recipient than Haven declared",
|
|
191
|
+
`Expected ${expected.recipientAddress}; the child pins ${recipient}.`
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
} else if (calldata) {
|
|
195
|
+
refuseSubBudgetChild(
|
|
196
|
+
"it pins a recipient the sub-budget was not issued with",
|
|
197
|
+
"This sub-budget is open (no recipient pin); the child must not add one."
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
function assertOwnSubBudgetCloseUserOp(typedData, expected, delegateAddress) {
|
|
202
|
+
assertOwnTaskBudgetCloseUserOp(typedData, expected, delegateAddress);
|
|
52
203
|
}
|
|
53
204
|
|
|
54
|
-
export {
|
|
205
|
+
export { ERC20_PERIOD_TRANSFER_ENFORCER, assertOwnSubBudgetChild, assertOwnSubBudgetCloseUserOp, hashSubBudgetDelegation };
|
|
55
206
|
//# sourceMappingURL=edge.js.map
|
|
56
207
|
//# sourceMappingURL=edge.js.map
|
package/dist/edge.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/edge-signing.ts"],"names":[],"mappings":";;;;;;;AAsBA,IAAM,MAAA,GAAS,OAAO,oEAAoE,CAAA;AAW1F,SAAS,QAAA,CAAS,KAAA,EAAe,KAAA,EAAe,IAAA,EAAsB;AACpE,EAAA,IAAI,CAAC,mBAAmB,IAAA,CAAK,KAAK,KAAK,KAAA,CAAM,MAAA,KAAW,CAAA,GAAI,KAAA,GAAQ,CAAA,EAAG;AACrE,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,EAAG,IAAI,CAAA,uBAAA,EAA0B,KAAK,CAAA,gBAAA,CAAkB,CAAA;AAAA,EAC1E;AACA,EAAA,OAAO,KAAA,CAAM,MAAM,CAAC,CAAA;AACtB;AAEO,SAAS,eAAe,UAAA,EAA4B;AACzD,EAAA,IAAI;AAEF,IAAA,OAAO,oBAAoB,CAAA,EAAA,EAAK,QAAA,CAAS,YAAY,EAAA,EAAI,aAAa,CAAC,CAAA,CAAE,CAAA;AAAA,EAC3E,SAAS,GAAA,EAAK;AACZ,IAAA,MAAM,IAAI,iBAAA;AAAA,MACR,wBAAwB,GAAA,YAAe,KAAA,GAAQ,IAAI,OAAA,GAAU,MAAA,CAAO,GAAG,CAAC,CAAA;AAAA,KAC1E;AAAA,EACF;AACF;AAEO,SAAS,QAAA,CAAS,YAAoB,IAAA,EAAsB;AACjE,EAAA,IAAI;AACF,IAAA,MAAM,EAAE,GAAG,CAAA,EAAG,QAAA,KAAa,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AAe,EAAA,GAAK,CAAA,GAAI,CAAA,CAAA;AAC5G,IAAA,IAAI,WAAA,GAAc,GAAG,OAAO,KAAA;AAC5B,IAAA,MAAM,YAAY,SAAA,CAAU,SAAA,CAAU,WAAA,CAAY,GAAA,CAAI,MAAM,CAAA,EAAG,GAAG,CAAC,CAAA,CAChE,eAAe,WAAW,CAAA,CAC1B,iBAAiB,OAAO,CAAA,CACxB,MAAM,KAAK,CAAA;AACd,IAAA,MAAM,SAAA,GAAY,kBAAA,CAAmB,CAAA,EAAA,EAAK,SAAS,CAAA,CAAE,CAAA;AACrD,IAAA,OAAO,SAAA,CAAU,WAAA,EAAY,KAAM,eAAA,CAAgB,WAAA,EAAY;AAAA,EACjE,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,KAAA;AAAA,EACT;AACF","file":"edge.js","sourcesContent":["import { secp256k1 } from '@noble/curves/secp256k1'\nimport { privateKeyToAddress, publicKeyToAddress } from 'viem/accounts'\nimport { serializeSignature, type Hex } from 'viem'\nimport { HavenSigningError } from './types.js'\n\n/**\n * #3173: the three key helpers the edge signer needs, without ethers.\n *\n * `./signer.ts` implements the same three on ethers, and the SDK barrel pulls\n * ethers in for them (and for `provider.ts` / `receipt.ts`). The edge signer\n * already ships viem, and viem signs with `@noble/curves` underneath — these\n * use the same curve library directly, synchronously (viem's own `sign` is\n * async), so `@haven_ai/sdk/edge` stays ethers-free and a signer session stops\n * paying for module init it never uses. Byte-equivalence with the ethers forms\n * is pinned in `edge-signing.test.ts`: same address, same 65-byte `r‖s‖v`\n * serialisation with low-s and v = 27/28, same recovery verdicts, and the same\n * refusals (high-s twin, wrong lengths) — with three stated strict-direction\n * exceptions, see `hexBytes`. Raw ECDSA over the hash — never the EIP-191\n * prefixed digest.\n */\n\n/** secp256k1 order / 2 — a signature with s above this is the malleable twin ethers rejects. */\nconst HALF_N = BigInt('0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0')\n\n/**\n * Stricter than ethers, never looser: the lowercase `0x` prefix and the exact\n * byte length are required. Ethers accepts an uppercase `0X` prefix on a\n * signature and the 64-byte EIP-2098 compact form, and is inconsistent on a\n * private key (`Wallet` accepts unprefixed and refuses `0X`; `SigningKey` does\n * the reverse); every such form is refused here — the divergences are pinned\n * in `edge-signing.test.ts` with ethers control assertions, and every\n * well-formed input is byte-equivalent.\n */\nfunction hexBytes(value: string, bytes: number, what: string): string {\n if (!/^0x[0-9a-fA-F]+$/.test(value) || value.length !== 2 + bytes * 2) {\n throw new Error(`${what} must be a 0x-prefixed ${bytes}-byte hex string`)\n }\n return value.slice(2)\n}\n\nexport function addressFromKey(privateKey: string): string {\n try {\n // Same gate as signHash: a key that derives an address must also sign.\n return privateKeyToAddress(`0x${hexBytes(privateKey, 32, 'private key')}`)\n } catch (err) {\n throw new HavenSigningError(\n `Invalid private key: ${err instanceof Error ? err.message : String(err)}`,\n )\n }\n}\n\nexport function signHash(privateKey: string, hash: string): string {\n try {\n const { r, s, recovery } = secp256k1.sign(hexBytes(hash, 32, 'hash'), hexBytes(privateKey, 32, 'private key'), { lowS: true })\n // viem serialises the trailing byte from yParity (0/1 → 27/28), the same\n // `v` ethers' `Signature.serialized` writes.\n return serializeSignature({\n r: `0x${r.toString(16).padStart(64, '0')}`,\n s: `0x${s.toString(16).padStart(64, '0')}`,\n yParity: recovery,\n })\n } catch (err) {\n throw new HavenSigningError(\n `Failed to sign hash: ${err instanceof Error ? err.message : String(err)}`,\n )\n }\n}\n\nexport function verifySignature(hash: string, signature: string, expectedAddress: string): boolean {\n try {\n const sig = hexBytes(signature, 65, 'signature')\n const hashHex = hexBytes(hash, 32, 'hash')\n // Reject the high-s malleable twin, as ethers does — one signature per message.\n if (BigInt(`0x${sig.slice(64, 128)}`) > HALF_N) return false\n const yParityOrV = Number.parseInt(sig.slice(128), 16)\n const recoveryBit = yParityOrV === 0 || yParityOrV === 27 ? 0 : yParityOrV === 1 || yParityOrV === 28 ? 1 : -1\n if (recoveryBit < 0) return false\n const publicKey = secp256k1.Signature.fromCompact(sig.slice(0, 128))\n .addRecoveryBit(recoveryBit)\n .recoverPublicKey(hashHex)\n .toHex(false)\n const recovered = publicKeyToAddress(`0x${publicKey}`)\n return recovered.toLowerCase() === expectedAddress.toLowerCase()\n } catch {\n return false\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/sub-budget-guards.ts"],"names":[],"mappings":";;;;;;AAyCO,IAAM,8BAAA,GAAiC;AAG9C,IAAM,sBAAA,GAAyB,mBAAA;AAC/B,IAAM,yBAAA,GAA4B,GAAA;AAGlC,IAAM,yBAAA,GAA4B;AAAA,EAChC,MAAA,EAAQ;AAAA,IACN,EAAE,IAAA,EAAM,UAAA,EAAY,IAAA,EAAM,SAAA,EAAU;AAAA,IACpC,EAAE,IAAA,EAAM,OAAA,EAAS,IAAA,EAAM,OAAA;AAAQ,GACjC;AAAA,EACA,UAAA,EAAY;AAAA,IACV,EAAE,IAAA,EAAM,UAAA,EAAY,IAAA,EAAM,SAAA,EAAU;AAAA,IACpC,EAAE,IAAA,EAAM,WAAA,EAAa,IAAA,EAAM,SAAA,EAAU;AAAA,IACrC,EAAE,IAAA,EAAM,WAAA,EAAa,IAAA,EAAM,SAAA,EAAU;AAAA,IACrC,EAAE,IAAA,EAAM,SAAA,EAAW,IAAA,EAAM,UAAA,EAAW;AAAA,IACpC,EAAE,IAAA,EAAM,MAAA,EAAQ,IAAA,EAAM,SAAA;AAAU;AAEpC,CAAA;AASA,SAAS,IAAA,CAAK,GAAuB,CAAA,EAAgC;AACnE,EAAA,OAAO,CAAC,CAAC,CAAA,IAAK,CAAC,CAAC,KAAK,CAAA,CAAE,WAAA,EAAY,KAAM,CAAA,CAAE,WAAA,EAAY;AACzD;AAGA,SAAS,UAAA,CAAW,KAAA,EAAe,KAAA,EAAe,GAAA,EAAsB;AACtE,EAAA,MAAM,IAAA,GAAO,KAAA,CAAM,UAAA,CAAW,IAAI,CAAA,GAAI,KAAA,CAAM,KAA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CAAA;AAChD,IAAA,IAAI,CAAC,IAAA,CAAK,SAAA,EAAW,QAAA,CAAS,gBAAgB,CAAA,EAAG;AAC/C,MAAA,oBAAA;AAAA,QACE,mDAAA;AAAA,QACA,CAAA,SAAA,EAAY,QAAA,CAAS,gBAAgB,CAAA,iBAAA,EAAoB,SAAS,CAAA,CAAA;AAAA,OACpE;AAAA,IACF;AAAA,EACF,WAAW,QAAA,EAAU;AACnB,IAAA,oBAAA;AAAA,MACE,wDAAA;AAAA,MACA;AAAA,KACF;AAAA,EACF;AACF;AAGO,SAAS,6BAAA,CACd,SAAA,EACA,QAAA,EACA,eAAA,EACM;AAMN,EAAA,8BAAA,CAA+B,SAAA,EAAW,UAAU,eAAe,CAAA;AACrE","file":"edge.js","sourcesContent":["/**\n * Sub-budget child guards (#3330) — the SDK-side local verification the\n * signer runs before its delegate key signs a sub-budget child, mirroring\n * `task-budget-guards.ts`'s `assertOwnTaskChild` for the two-row sub-budget\n * tree. ONE verifier covers BOTH rows, because the same signer (the\n * delegating agent A's own key) signs both:\n *\n * - A's PARENT-CHILD row: `delegate === delegator === A's own account`\n * (self-delegated, the task-child shape class).\n * - B's GRANT row: `delegator === A's own account`, `delegate === B's\n * delegate account` (a real grant to another account).\n *\n * What makes a sub-budget child a sub-budget child (vs a task budget's\n * hard-cap child) is the AMOUNT caveat: an `ERC20PeriodTransferEnforcer`\n * caveat whose terms are `abi.encode(address token, uint256 periodAmount,\n * uint256 periodDuration, uint256 startDate)` — the period-scoped slice of\n * the parent's own period meter, with the parent's periodDuration and\n * startDate reused verbatim. A hard-cap `ERC20TransferAmountEnforcer` caveat\n * is refused: that would be a task budget, which this channel must never\n * sign (two different products, two different sign-context endpoints).\n *\n * Authority, timestamp and recipient-pin checks are byte-for-byte the\n * task-child discipline: authority must equal the declared parent hash and\n * never be ROOT; expiry must be in the future and equal the declared one; a\n * pinned recipient must match exactly, and an unpinned budget must not grow\n * one. Enforcers are the pinned constants from `settlement-child.ts` (no kit\n * dependency) — same reasoning as that file's header documents.\n */\n\nimport { type Address, type Hex } from 'viem'\nimport { HavenTypedDataRefusedError, DIRECT_PAYMENT_CHAIN_IDS } from './direct-payment-guard.js'\nimport { DELEGATION_MANAGER, ROOT_AUTHORITY, CAVEAT_ENFORCERS } from './settlement-child.js'\nimport { deriveDelegateAccountAddress } from './delegate-account.js'\nimport {\n DELEGATION_TYPEHASH,\n CAVEAT_TYPEHASH,\n hashDelegation,\n type DelegationForHashing,\n} from './task-budget-guards.js'\n\n/** The pinned `ERC20PeriodTransferEnforcer` (bytecode-identical on Base + Base Sepolia). */\nexport const ERC20_PERIOD_TRANSFER_ENFORCER = '0x474e3Ae7E169e940607cC624Da8A15Eb120139aB'\n\n/** The pinned `Delegation` EIP-712 domain (cross-checked by task-budget-guards' pin test). */\nconst DELEGATION_DOMAIN_NAME = 'DelegationManager'\nconst DELEGATION_DOMAIN_VERSION = '1'\n\n/** The pinned `types` shape, byte-identical to `task-budget-guards.ts`'s. */\nconst EXPECTED_DELEGATION_TYPES = {\n Caveat: [\n { name: 'enforcer', type: 'address' },\n { name: 'terms', type: 'bytes' },\n ],\n Delegation: [\n { name: 'delegate', type: 'address' },\n { name: 'delegator', type: 'address' },\n { name: 'authority', type: 'bytes32' },\n { name: 'caveats', type: 'Caveat[]' },\n { name: 'salt', type: 'uint256' },\n ],\n} as const\n\nexport interface SubBudgetChildTypedData {\n domain: { name?: string; version?: string; chainId?: number | string; verifyingContract?: string }\n types: Record<string, unknown>\n primaryType: string\n message: Record<string, unknown>\n}\n\nfunction same(a: string | undefined, b: string | undefined): boolean {\n return !!a && !!b && a.toLowerCase() === b.toLowerCase()\n}\n\n/** Byte slice [start, end) of a `0x…` string, as lowercase hex without prefix. */\nfunction sliceTerms(terms: string, start: number, end?: number): string {\n const body = terms.startsWith('0x') ? terms.slice(2).toLowerCase() : terms.toLowerCase()\n return end === undefined ? body.slice(start * 2) : body.slice(start * 2, end * 2)\n}\n\nfunction findCaveat(caveats: Array<{ enforcer: string; terms: string }>, enforcer: string) {\n return caveats.find((c) => same(c.enforcer, enforcer))\n}\n\n/** The struct hash of a sub-budget child — the redemption chain's link value. */\nexport function hashSubBudgetDelegation(delegation: DelegationForHashing): Hex {\n return hashDelegation(delegation)\n}\n\n// The kit's own struct hashes, re-exported verbatim from task-budget-guards'\n// pinned literals so a caller can name them from this module too.\nexport { DELEGATION_TYPEHASH, CAVEAT_TYPEHASH }\n\nexport interface SubBudgetChildExpectation {\n chainId: number\n tokenAddress: string\n /** Per-period amount in atomic units — the child's periodAmount. */\n periodAmountAtomic: string\n /** The PARENT's period duration (seconds), reused verbatim in the caveat. */\n periodDurationSeconds: number\n /** The PARENT's period anchor, reused verbatim in the caveat. */\n startDate: number\n recipientAddress: string | null\n /** unix seconds — the `TimestampEnforcer`'s `beforeThreshold`. */\n expiresAt: number\n /** The hash of the row's parent delegation (`parent_delegation_hash`). */\n parentDelegationHash: string\n /**\n * A's own derived delegate account — the child's `delegator` on BOTH rows.\n */\n delegateAccount: string\n /**\n * The row's own `delegate`: A's account again for the parent-child row,\n * B's delegate account for the grant row. Served in the sign-context's\n * `expected` (read from the stored child JSON, not trusted caller input).\n */\n childDelegateAccount: string\n}\n\nfunction refuseSubBudgetChild(what: string, detail: string): never {\n throw new HavenTypedDataRefusedError(\n `Refusing to sign the sub-budget child: ${what}. ${detail} ` +\n 'This signature opens a slice of the delegating agent budget for another agent, so it is ' +\n 'verified locally against the expectation Haven declared, rather than trusted.',\n )\n}\n\n/**\n * Verify a sub-budget child delegation is EXACTLY what\n * `GET /sub-budgets/:id/sign-context` (purpose `open`) declared, that its\n * `delegator` is this signer's own account, and that its `delegate` is the\n * declared child delegate. Throws `HavenTypedDataRefusedError` (never\n * returns) on any disagreement.\n */\nexport function assertOwnSubBudgetChild(\n typedData: unknown,\n expected: SubBudgetChildExpectation,\n delegateAddress: string,\n now: number = Date.now(),\n): void {\n const td = typedData as SubBudgetChildTypedData\n if (td?.primaryType !== 'Delegation') {\n refuseSubBudgetChild('it is not a Delegation payload', `primaryType was '${String(td?.primaryType)}'.`)\n }\n if (!same(td.domain?.verifyingContract, DELEGATION_MANAGER)) {\n refuseSubBudgetChild(\n 'the EIP-712 domain names an unknown DelegationManager',\n `Expected ${DELEGATION_MANAGER}, got ${td.domain?.verifyingContract}.`,\n )\n }\n if (td.domain?.name !== DELEGATION_DOMAIN_NAME || td.domain?.version !== DELEGATION_DOMAIN_VERSION) {\n refuseSubBudgetChild(\n 'the EIP-712 domain name/version does not match the pinned DelegationManager domain',\n `Expected name '${DELEGATION_DOMAIN_NAME}' version '${DELEGATION_DOMAIN_VERSION}'; got name ` +\n `'${String(td.domain?.name)}' version '${String(td.domain?.version)}'.`,\n )\n }\n if (\n JSON.stringify(td.types?.Caveat) !== JSON.stringify(EXPECTED_DELEGATION_TYPES.Caveat) ||\n JSON.stringify(td.types?.Delegation) !== JSON.stringify(EXPECTED_DELEGATION_TYPES.Delegation)\n ) {\n refuseSubBudgetChild(\n 'its EIP-712 type definitions do not match the pinned Delegation/Caveat shape',\n 'A renamed, reordered, or additional field on Delegation or Caveat could change what the ' +\n 'caveats below mean without changing this check\\'s field-by-field comparisons.',\n )\n }\n const domainChain = Number(td.domain?.chainId)\n if (!Number.isFinite(domainChain) || !DIRECT_PAYMENT_CHAIN_IDS.has(domainChain)) {\n refuseSubBudgetChild(\n 'it is scoped to a chain the delegation rail has no pinned contracts for',\n `Chains with pinned contracts: ${[...DIRECT_PAYMENT_CHAIN_IDS].join(', ')}; the child says ${String(td.domain?.chainId)}.`,\n )\n }\n if (domainChain !== expected.chainId) {\n refuseSubBudgetChild(\n 'it is scoped to the wrong chain',\n `Expected chain ${expected.chainId}; the child says ${td.domain?.chainId}.`,\n )\n }\n\n const ownAccount = deriveDelegateAccountAddress(delegateAddress as Address)\n const delegator = td.message?.delegator\n if (typeof delegator !== 'string' || !same(delegator, ownAccount)) {\n refuseSubBudgetChild(\n \"it is not delegated by this agent's own account\",\n `Expected delegator ${ownAccount}; the child names ${String(delegator)}.`,\n )\n }\n if (!same(delegator, expected.delegateAccount)) {\n refuseSubBudgetChild(\n 'its delegator does not match the account Haven declared',\n `Expected ${expected.delegateAccount}; the child names ${String(delegator)}.`,\n )\n }\n const delegate = td.message?.delegate\n if (typeof delegate !== 'string' || !same(delegate, expected.childDelegateAccount)) {\n refuseSubBudgetChild(\n 'its delegate does not match the declared child delegate',\n `Expected ${expected.childDelegateAccount}; the child names ${String(delegate)}.`,\n )\n }\n\n const authority = td.message?.authority\n if (typeof authority !== 'string' || !/^0x[0-9a-fA-F]{64}$/.test(authority)) {\n refuseSubBudgetChild('its authority is not a 32-byte delegation hash', `Got ${String(authority)}.`)\n }\n if (authority.toLowerCase() === ROOT_AUTHORITY) {\n refuseSubBudgetChild(\n 'it is a ROOT delegation, not a re-delegation of the delegating agent budget',\n 'A sub-budget always chains under the delegating agent budget delegation.',\n )\n }\n if (!same(authority, expected.parentDelegationHash)) {\n refuseSubBudgetChild(\n 'it chains under a different parent delegation',\n `Expected authority ${expected.parentDelegationHash}; the child says ${authority}.`,\n )\n }\n\n const caveats = (td.message?.caveats as Array<{ enforcer: string; terms: string }> | undefined) ?? []\n\n // The PERIOD caveat is what makes this a sub-budget — a hard-cap\n // `ERC20TransferAmountEnforcer` caveat is the task-budget shape and is\n // refused on this channel.\n if (findCaveat(caveats, CAVEAT_ENFORCERS.erc20TransferAmount)) {\n refuseSubBudgetChild(\n 'it carries a hard-cap transfer-amount caveat (the task-budget shape)',\n 'A sub-budget is period-scoped: its amount caveat must be an ERC20PeriodTransferEnforcer ' +\n 'caveat slicing the parent period meter, never a lifetime cap.',\n )\n }\n const period = findCaveat(caveats, ERC20_PERIOD_TRANSFER_ENFORCER)\n if (!period) {\n refuseSubBudgetChild(\n 'it has no ERC-20 period-transfer caveat',\n 'Without it the child is not bounded to a per-period amount.',\n )\n }\n // terms = abi.encodePacked(address token, uint256 periodAmount,\n // uint256 periodDuration, uint256 startDate) — the kit's TIGHT packing\n // (20 + 32×3 = 116 bytes, verified against `createDelegation`'s own\n // output), the same layout `task-budget-guards.ts` decodes for the\n // ERC20TransferAmountEnforcer. NOT head/tail-padded words: the address\n // occupies bytes [0,20) and the amount starts at byte 20.\n const token = `0x${sliceTerms(period.terms, 0, 20)}`\n if (!same(token, expected.tokenAddress)) {\n refuseSubBudgetChild('it spends a different token', `Expected ${expected.tokenAddress}, child pins ${token}.`)\n }\n const periodAmount = BigInt(`0x${sliceTerms(period.terms, 20, 52)}`)\n if (periodAmount !== BigInt(expected.periodAmountAtomic)) {\n refuseSubBudgetChild(\n 'the period amount does not match',\n `Expected ${expected.periodAmountAtomic}; the child allows ${periodAmount.toString()}.`,\n )\n }\n const periodDuration = Number(BigInt(`0x${sliceTerms(period.terms, 52, 84)}`))\n if (periodDuration !== expected.periodDurationSeconds) {\n refuseSubBudgetChild(\n 'its period duration does not match the parent window Haven declared',\n `Expected ${expected.periodDurationSeconds}s; the child says ${periodDuration}s. A sub-budget is a ` +\n 'slice of the parent period meter — same window, smaller amount — never a different clock.',\n )\n }\n const startDate = Number(BigInt(`0x${sliceTerms(period.terms, 84, 116)}`))\n if (startDate !== expected.startDate) {\n refuseSubBudgetChild(\n 'its period start does not match the parent anchor Haven declared',\n `Expected ${expected.startDate}; the child says ${startDate}.`,\n )\n }\n\n const timestamp = findCaveat(caveats, CAVEAT_ENFORCERS.timestamp)\n if (!timestamp) {\n refuseSubBudgetChild('it never expires', 'A sub-budget child without a timestamp caveat is open-ended.')\n }\n const beforeThreshold = Number(BigInt(`0x${sliceTerms(timestamp.terms, 16, 32)}`))\n const nowSec = Math.floor(now / 1000)\n if (beforeThreshold <= nowSec) {\n refuseSubBudgetChild('it has already expired', `Expiry ${beforeThreshold} is not in the future.`)\n }\n if (beforeThreshold !== expected.expiresAt) {\n refuseSubBudgetChild(\n 'its expiry does not match what Haven declared',\n `Expected ${expected.expiresAt}; the child says ${beforeThreshold}.`,\n )\n }\n\n const calldata = findCaveat(caveats, CAVEAT_ENFORCERS.allowedCalldata)\n if (expected.recipientAddress) {\n if (!calldata) {\n refuseSubBudgetChild(\n 'it has no recipient pin',\n 'The sub-budget was issued with a pinned recipient, so the child must carry one.',\n )\n }\n const startIndex = BigInt(`0x${sliceTerms(calldata.terms, 0, 32)}`)\n if (startIndex !== 4n) {\n refuseSubBudgetChild(\n 'the recipient pin points at the wrong calldata offset',\n `Expected offset 4, got ${startIndex.toString()}.`,\n )\n }\n const paddedRecipient = sliceTerms(calldata.terms, 32, 64)\n if (paddedRecipient.slice(0, 24) !== '0'.repeat(24)) {\n refuseSubBudgetChild('the pinned recipient is not a plain address', 'Its 32-byte word is not a padded address.')\n }\n const recipient = `0x${paddedRecipient.slice(24)}`\n if (!same(recipient, expected.recipientAddress)) {\n refuseSubBudgetChild(\n 'it pins a different recipient than Haven declared',\n `Expected ${expected.recipientAddress}; the child pins ${recipient}.`,\n )\n }\n } else if (calldata) {\n refuseSubBudgetChild(\n 'it pins a recipient the sub-budget was not issued with',\n 'This sub-budget is open (no recipient pin); the child must not add one.',\n )\n }\n}\n\n/** Close UserOp verification — byte-for-byte the task-budget discipline. */\nexport function assertOwnSubBudgetCloseUserOp(\n typedData: Record<string, unknown>,\n expected: { delegationHash: string },\n delegateAddress: string,\n): void {\n // The close UserOp shape is identical to the task budget's: a\n // disableDelegation(child) call from the agent's own delegate account.\n // Reuse the task-budget verifier — the only expectation that differs is\n // the delegation hash, which is an input here either way. Static import,\n // not lazy: these modules share no init-time cycle.\n assertOwnTaskBudgetCloseUserOp(typedData, expected, delegateAddress)\n}\n\n// The close-UserOp verifier is imported for the re-export above.\nimport { assertOwnTaskBudgetCloseUserOp } from './task-budget-guards.js'\n"]}
|