@haven_ai/sdk 0.5.0-alpha.1 → 0.7.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 +24 -8
- package/dist/{base64-BUMXmruY.d.cts → base64-_seD1uw8.d.cts} +425 -18
- package/dist/{base64-BUMXmruY.d.ts → base64-_seD1uw8.d.ts} +425 -18
- package/dist/{chunk-JKUIE45B.cjs → chunk-3SVPI4LS.cjs} +130 -9
- package/dist/chunk-3SVPI4LS.cjs.map +1 -0
- package/dist/{chunk-QSSVOPWA.cjs → chunk-J6DQEBK2.cjs} +340 -17
- package/dist/chunk-J6DQEBK2.cjs.map +1 -0
- package/dist/{chunk-GLNG2GF7.js → chunk-NNIT7WAI.js} +127 -10
- package/dist/chunk-NNIT7WAI.js.map +1 -0
- package/dist/{chunk-KQ7JXKTV.js → chunk-R3RZJJWL.js} +321 -6
- package/dist/chunk-R3RZJJWL.js.map +1 -0
- package/dist/edge.cjs +296 -57
- package/dist/edge.cjs.map +1 -1
- package/dist/edge.d.cts +122 -5
- package/dist/edge.d.ts +122 -5
- package/dist/edge.js +204 -4
- package/dist/edge.js.map +1 -1
- package/dist/index.cjs +772 -350
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +162 -28
- package/dist/index.d.ts +162 -28
- package/dist/index.js +461 -80
- 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-GLNG2GF7.js.map +0 -1
- package/dist/chunk-JKUIE45B.cjs.map +0 -1
- package/dist/chunk-KQ7JXKTV.js.map +0 -1
- package/dist/chunk-QSSVOPWA.cjs.map +0 -1
package/dist/edge.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { cg as DelegationForHashing } from './base64-_seD1uw8.cjs';
|
|
2
|
+
export { U as AgentPaymentFailureCode, Y as AgentPaymentNextAction, ch as CAVEAT_ENFORCERS, ci as CAVEAT_TYPEHASH, cj as DELEGATION_MANAGER, ck as DELEGATION_TYPEHASH, cl as DIRECT_PAYMENT_CHAIN_IDS, ac as DIRECT_SIGN_CONTEXT_VERSION, ad as ENTRY_POINT_V07, cm as EXECUTE_ABI, af as HAVEN_CLIENT_HEADER, ag as HAVEN_CONNECTOR_CHANNEL, ah as HAVEN_MINIMUM_NODE_VERSION, cn as HYBRID_DELEGATOR_IMPLEMENTATION, an as HavenApiError, a as HavenClientUpdate, I as HavenError, ar as HavenSigningError, at as HavenTypedDataRefusedError, au as HavenUnsupportedSignerVersionError, av as HavenUserOpBindingError, co as MAX_SETTLEMENT_WINDOW_SECONDS, ax as MAX_TASK_BUDGET_TTL_SECONDS, aC as NextStep, aE as NextStepHandoff, aG as NextStepTarget, aO as ROOT_AUTHORITY, aR as SIGNER_UPDATE_FALLBACK, cp as SIMPLE_FACTORY_ADDRESS, aW as SettlementChildExpectation, cq as SettlementChildTypedData, aX as SignerRefusalCode, j as SweepAuthorization, a$ as SweepExpectedAuth, b3 as TASK_SIGN_CONTEXT_VERSION, b5 as TYPED_DATA_NOT_ALLOWED, b6 as TaskBudgetCloseExpectation, b7 as TaskChildExpectation, b8 as TaskChildTypedData, bc as X402ExpectedAuth, bg as X402PaymentOption, X as X402PaymentRequired, bs as assertBoundDirectPaymentUserOp, cr as assertFundingLegPaysDelegate, cs as assertOwnSettlementChild, bt as assertOwnTaskBudgetCloseUserOp, bu as assertOwnTaskChild, bv as assertUserOpTypedDataBinding, bw as buildSweepAuthorizationMessage, bx as buildSweepTypedData, by as buildX402ExpectedMessage, ct as chainIdForNetwork, bA as connectorRerunCommand, bC as connectorUpgradeCommand, bD as createNextStepBuilder, bE as decodeBase64Json, bH as deriveDelegateAccountAddress, bI as encodeBase64Json, bL as hashDelegation, bM as havenClientIdentity, bP as isPackedUserOperationTypedData, cu as isSettlementChildTypedData, bQ as isSupportedNodeVersion, bS as isTaskChildTypedData, bU as packedUserOperationHash, bY as readClientUpdate, c3 as selectStandardPaymentOption, c8 as toStandardPaymentRequirements, c9 as unsupportedNodeVersionMessage, cb as verifySettlementChild, cd as x402AuthorizationAmount, cf as x402V2PaymentEnvelope } from './base64-_seD1uw8.cjs';
|
|
2
3
|
import { Hex, Address } from 'viem';
|
|
3
4
|
import 'x402/types';
|
|
4
5
|
|
|
@@ -36,6 +37,37 @@ declare function verifySignature(hash: string, signature: string, expectedAddres
|
|
|
36
37
|
* `@metamask/delegation-abis`' `IDelegationManager` ABI (a devDependency
|
|
37
38
|
* only, in `@haven_ai/signer`'s `package.json`) and pinned against the kit's
|
|
38
39
|
* own encoder by `packages/signer/src/redemption-guard.pins.test.ts`.
|
|
40
|
+
*
|
|
41
|
+
* TASK BUDGETS (#3329) widen the accepted chain from exactly one link to ONE
|
|
42
|
+
* OR TWO: `[budget]` (unchanged) or `[task child, budget]` — leaf first, as
|
|
43
|
+
* everywhere else in this file. The two-link case is safe by the SAME
|
|
44
|
+
* reasoning as the one-link case, extended one hop: the task child's own
|
|
45
|
+
* `delegate` AND `delegator` are this signer's OWN account (a
|
|
46
|
+
* SELF-delegation — `assertOwnTaskChild` in `task-budget-guards.ts` is what
|
|
47
|
+
* proves its caveats narrow, never widen, the budget it chains under), and
|
|
48
|
+
* the budget link underneath it keeps the existing (3)/(4) shape: delegate ==
|
|
49
|
+
* own account, delegator != own account (a real grant from elsewhere). A
|
|
50
|
+
* chain redeemed this way can therefore only ever be AS OR MORE restrictive
|
|
51
|
+
* than redeeming the budget directly — the task child ADDS caveats (amount,
|
|
52
|
+
* recipient, expiry); it can never remove one, because
|
|
53
|
+
* `DelegationManager.redeemDelegations` ANDs every caveat of every link in
|
|
54
|
+
* the chain (the same AND-only property `settlement-child.ts`'s header
|
|
55
|
+
* documents). Any OTHER two-link chain — a leaf delegated by a third party, a
|
|
56
|
+
* leaf whose delegate is not this signer's own account, a chain longer than
|
|
57
|
+
* two — is refused exactly as before.
|
|
58
|
+
*
|
|
59
|
+
* SUB-BUDGETS (#3330) widen it once more, to exactly ONE, TWO or THREE
|
|
60
|
+
* links: `[sub grant, sub parent-child, budget]` is the third shape. The
|
|
61
|
+
* signer here is agent B's signer (B redeems its own grant), so the leaf's
|
|
62
|
+
* `delegate` is this signer's own account and its `delegator` is agent A's
|
|
63
|
+
* account — a REAL grant from another account, not a self-delegation. The
|
|
64
|
+
* middle link is A's parent-child (delegate == A's account, delegator == A's
|
|
65
|
+
* account: A's SELF-narrowing, the task-child shape class), and the root
|
|
66
|
+
* keeps the one-link shape (delegate == A's account, delegator != A). The
|
|
67
|
+
* AND-only property again makes the three-link chain AS OR MORE restrictive
|
|
68
|
+
* than the two-hop task chain; a chain longer than three, a leaf whose
|
|
69
|
+
* delegate is not this signer, or a middle link that is not a self-delegation
|
|
70
|
+
* is refused exactly as before.
|
|
39
71
|
*/
|
|
40
72
|
|
|
41
73
|
/** `Delegation` — `(address delegate, address delegator, bytes32 authority, Caveat[] caveats, uint256 salt, bytes signature)`. */
|
|
@@ -95,11 +127,96 @@ declare const REDEEM_DELEGATIONS_ABI: readonly [{
|
|
|
95
127
|
declare const SINGLE_DEFAULT_MODE: Hex;
|
|
96
128
|
/**
|
|
97
129
|
* Verify `redeemCalldata` (the bytes passed to `execute()`, selector already
|
|
98
|
-
* confirmed to be `redeemDelegations` by the caller) is EXACTLY Haven's
|
|
99
|
-
*
|
|
100
|
-
*
|
|
130
|
+
* confirmed to be `redeemDelegations` by the caller) is EXACTLY one of Haven's
|
|
131
|
+
* two shapes: a single delegation redeemed by `ownAccount` and granted by
|
|
132
|
+
* someone else, or (#3329) that grant behind one task-budget child `ownAccount`
|
|
133
|
+
* delegated to itself; in `SingleDefault` mode, with no encoding slack
|
|
134
|
+
* anywhere. Throws
|
|
101
135
|
* `HavenSigningError` (never returns) on any disagreement.
|
|
102
136
|
*/
|
|
103
137
|
declare function assertRedeemsOwnBudgetDelegation(redeemCalldata: Hex, ownAccount: Address): void;
|
|
104
138
|
|
|
105
|
-
|
|
139
|
+
/**
|
|
140
|
+
* Sub-budget child guards (#3330) — the SDK-side local verification the
|
|
141
|
+
* signer runs before its delegate key signs a sub-budget child, mirroring
|
|
142
|
+
* `task-budget-guards.ts`'s `assertOwnTaskChild` for the two-row sub-budget
|
|
143
|
+
* tree. ONE verifier covers BOTH rows, because the same signer (the
|
|
144
|
+
* delegating agent A's own key) signs both:
|
|
145
|
+
*
|
|
146
|
+
* - A's PARENT-CHILD row: `delegate === delegator === A's own account`
|
|
147
|
+
* (self-delegated, the task-child shape class).
|
|
148
|
+
* - B's GRANT row: `delegator === A's own account`, `delegate === B's
|
|
149
|
+
* delegate account` (a real grant to another account).
|
|
150
|
+
*
|
|
151
|
+
* What makes a sub-budget child a sub-budget child (vs a task budget's
|
|
152
|
+
* hard-cap child) is the AMOUNT caveat: an `ERC20PeriodTransferEnforcer`
|
|
153
|
+
* caveat whose terms are `abi.encode(address token, uint256 periodAmount,
|
|
154
|
+
* uint256 periodDuration, uint256 startDate)` — the period-scoped slice of
|
|
155
|
+
* the parent's own period meter, with the parent's periodDuration and
|
|
156
|
+
* startDate reused verbatim. A hard-cap `ERC20TransferAmountEnforcer` caveat
|
|
157
|
+
* is refused: that would be a task budget, which this channel must never
|
|
158
|
+
* sign (two different products, two different sign-context endpoints).
|
|
159
|
+
*
|
|
160
|
+
* Authority, timestamp and recipient-pin checks are byte-for-byte the
|
|
161
|
+
* task-child discipline: authority must equal the declared parent hash and
|
|
162
|
+
* never be ROOT; expiry must be in the future and equal the declared one; a
|
|
163
|
+
* pinned recipient must match exactly, and an unpinned budget must not grow
|
|
164
|
+
* one. Enforcers are the pinned constants from `settlement-child.ts` (no kit
|
|
165
|
+
* dependency) — same reasoning as that file's header documents.
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
/** The pinned `ERC20PeriodTransferEnforcer` (bytecode-identical on Base + Base Sepolia). */
|
|
169
|
+
declare const ERC20_PERIOD_TRANSFER_ENFORCER = "0x474e3Ae7E169e940607cC624Da8A15Eb120139aB";
|
|
170
|
+
interface SubBudgetChildTypedData {
|
|
171
|
+
domain: {
|
|
172
|
+
name?: string;
|
|
173
|
+
version?: string;
|
|
174
|
+
chainId?: number | string;
|
|
175
|
+
verifyingContract?: string;
|
|
176
|
+
};
|
|
177
|
+
types: Record<string, unknown>;
|
|
178
|
+
primaryType: string;
|
|
179
|
+
message: Record<string, unknown>;
|
|
180
|
+
}
|
|
181
|
+
/** The struct hash of a sub-budget child — the redemption chain's link value. */
|
|
182
|
+
declare function hashSubBudgetDelegation(delegation: DelegationForHashing): Hex;
|
|
183
|
+
|
|
184
|
+
interface SubBudgetChildExpectation {
|
|
185
|
+
chainId: number;
|
|
186
|
+
tokenAddress: string;
|
|
187
|
+
/** Per-period amount in atomic units — the child's periodAmount. */
|
|
188
|
+
periodAmountAtomic: string;
|
|
189
|
+
/** The PARENT's period duration (seconds), reused verbatim in the caveat. */
|
|
190
|
+
periodDurationSeconds: number;
|
|
191
|
+
/** The PARENT's period anchor, reused verbatim in the caveat. */
|
|
192
|
+
startDate: number;
|
|
193
|
+
recipientAddress: string | null;
|
|
194
|
+
/** unix seconds — the `TimestampEnforcer`'s `beforeThreshold`. */
|
|
195
|
+
expiresAt: number;
|
|
196
|
+
/** The hash of the row's parent delegation (`parent_delegation_hash`). */
|
|
197
|
+
parentDelegationHash: string;
|
|
198
|
+
/**
|
|
199
|
+
* A's own derived delegate account — the child's `delegator` on BOTH rows.
|
|
200
|
+
*/
|
|
201
|
+
delegateAccount: string;
|
|
202
|
+
/**
|
|
203
|
+
* The row's own `delegate`: A's account again for the parent-child row,
|
|
204
|
+
* B's delegate account for the grant row. Served in the sign-context's
|
|
205
|
+
* `expected` (read from the stored child JSON, not trusted caller input).
|
|
206
|
+
*/
|
|
207
|
+
childDelegateAccount: string;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Verify a sub-budget child delegation is EXACTLY what
|
|
211
|
+
* `GET /sub-budgets/:id/sign-context` (purpose `open`) declared, that its
|
|
212
|
+
* `delegator` is this signer's own account, and that its `delegate` is the
|
|
213
|
+
* declared child delegate. Throws `HavenTypedDataRefusedError` (never
|
|
214
|
+
* returns) on any disagreement.
|
|
215
|
+
*/
|
|
216
|
+
declare function assertOwnSubBudgetChild(typedData: unknown, expected: SubBudgetChildExpectation, delegateAddress: string, now?: number): void;
|
|
217
|
+
/** Close UserOp verification — byte-for-byte the task-budget discipline. */
|
|
218
|
+
declare function assertOwnSubBudgetCloseUserOp(typedData: Record<string, unknown>, expected: {
|
|
219
|
+
delegationHash: string;
|
|
220
|
+
}, delegateAddress: string): void;
|
|
221
|
+
|
|
222
|
+
export { DELEGATION_TUPLE_COMPONENTS, DelegationForHashing, ERC20_PERIOD_TRANSFER_ENFORCER, REDEEM_DELEGATIONS_ABI, SINGLE_DEFAULT_MODE, type SubBudgetChildExpectation, type SubBudgetChildTypedData, addressFromKey, assertOwnSubBudgetChild, assertOwnSubBudgetCloseUserOp, assertRedeemsOwnBudgetDelegation, hashSubBudgetDelegation, signHash, verifySignature };
|
package/dist/edge.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { cg as DelegationForHashing } from './base64-_seD1uw8.js';
|
|
2
|
+
export { U as AgentPaymentFailureCode, Y as AgentPaymentNextAction, ch as CAVEAT_ENFORCERS, ci as CAVEAT_TYPEHASH, cj as DELEGATION_MANAGER, ck as DELEGATION_TYPEHASH, cl as DIRECT_PAYMENT_CHAIN_IDS, ac as DIRECT_SIGN_CONTEXT_VERSION, ad as ENTRY_POINT_V07, cm as EXECUTE_ABI, af as HAVEN_CLIENT_HEADER, ag as HAVEN_CONNECTOR_CHANNEL, ah as HAVEN_MINIMUM_NODE_VERSION, cn as HYBRID_DELEGATOR_IMPLEMENTATION, an as HavenApiError, a as HavenClientUpdate, I as HavenError, ar as HavenSigningError, at as HavenTypedDataRefusedError, au as HavenUnsupportedSignerVersionError, av as HavenUserOpBindingError, co as MAX_SETTLEMENT_WINDOW_SECONDS, ax as MAX_TASK_BUDGET_TTL_SECONDS, aC as NextStep, aE as NextStepHandoff, aG as NextStepTarget, aO as ROOT_AUTHORITY, aR as SIGNER_UPDATE_FALLBACK, cp as SIMPLE_FACTORY_ADDRESS, aW as SettlementChildExpectation, cq as SettlementChildTypedData, aX as SignerRefusalCode, j as SweepAuthorization, a$ as SweepExpectedAuth, b3 as TASK_SIGN_CONTEXT_VERSION, b5 as TYPED_DATA_NOT_ALLOWED, b6 as TaskBudgetCloseExpectation, b7 as TaskChildExpectation, b8 as TaskChildTypedData, bc as X402ExpectedAuth, bg as X402PaymentOption, X as X402PaymentRequired, bs as assertBoundDirectPaymentUserOp, cr as assertFundingLegPaysDelegate, cs as assertOwnSettlementChild, bt as assertOwnTaskBudgetCloseUserOp, bu as assertOwnTaskChild, bv as assertUserOpTypedDataBinding, bw as buildSweepAuthorizationMessage, bx as buildSweepTypedData, by as buildX402ExpectedMessage, ct as chainIdForNetwork, bA as connectorRerunCommand, bC as connectorUpgradeCommand, bD as createNextStepBuilder, bE as decodeBase64Json, bH as deriveDelegateAccountAddress, bI as encodeBase64Json, bL as hashDelegation, bM as havenClientIdentity, bP as isPackedUserOperationTypedData, cu as isSettlementChildTypedData, bQ as isSupportedNodeVersion, bS as isTaskChildTypedData, bU as packedUserOperationHash, bY as readClientUpdate, c3 as selectStandardPaymentOption, c8 as toStandardPaymentRequirements, c9 as unsupportedNodeVersionMessage, cb as verifySettlementChild, cd as x402AuthorizationAmount, cf as x402V2PaymentEnvelope } from './base64-_seD1uw8.js';
|
|
2
3
|
import { Hex, Address } from 'viem';
|
|
3
4
|
import 'x402/types';
|
|
4
5
|
|
|
@@ -36,6 +37,37 @@ declare function verifySignature(hash: string, signature: string, expectedAddres
|
|
|
36
37
|
* `@metamask/delegation-abis`' `IDelegationManager` ABI (a devDependency
|
|
37
38
|
* only, in `@haven_ai/signer`'s `package.json`) and pinned against the kit's
|
|
38
39
|
* own encoder by `packages/signer/src/redemption-guard.pins.test.ts`.
|
|
40
|
+
*
|
|
41
|
+
* TASK BUDGETS (#3329) widen the accepted chain from exactly one link to ONE
|
|
42
|
+
* OR TWO: `[budget]` (unchanged) or `[task child, budget]` — leaf first, as
|
|
43
|
+
* everywhere else in this file. The two-link case is safe by the SAME
|
|
44
|
+
* reasoning as the one-link case, extended one hop: the task child's own
|
|
45
|
+
* `delegate` AND `delegator` are this signer's OWN account (a
|
|
46
|
+
* SELF-delegation — `assertOwnTaskChild` in `task-budget-guards.ts` is what
|
|
47
|
+
* proves its caveats narrow, never widen, the budget it chains under), and
|
|
48
|
+
* the budget link underneath it keeps the existing (3)/(4) shape: delegate ==
|
|
49
|
+
* own account, delegator != own account (a real grant from elsewhere). A
|
|
50
|
+
* chain redeemed this way can therefore only ever be AS OR MORE restrictive
|
|
51
|
+
* than redeeming the budget directly — the task child ADDS caveats (amount,
|
|
52
|
+
* recipient, expiry); it can never remove one, because
|
|
53
|
+
* `DelegationManager.redeemDelegations` ANDs every caveat of every link in
|
|
54
|
+
* the chain (the same AND-only property `settlement-child.ts`'s header
|
|
55
|
+
* documents). Any OTHER two-link chain — a leaf delegated by a third party, a
|
|
56
|
+
* leaf whose delegate is not this signer's own account, a chain longer than
|
|
57
|
+
* two — is refused exactly as before.
|
|
58
|
+
*
|
|
59
|
+
* SUB-BUDGETS (#3330) widen it once more, to exactly ONE, TWO or THREE
|
|
60
|
+
* links: `[sub grant, sub parent-child, budget]` is the third shape. The
|
|
61
|
+
* signer here is agent B's signer (B redeems its own grant), so the leaf's
|
|
62
|
+
* `delegate` is this signer's own account and its `delegator` is agent A's
|
|
63
|
+
* account — a REAL grant from another account, not a self-delegation. The
|
|
64
|
+
* middle link is A's parent-child (delegate == A's account, delegator == A's
|
|
65
|
+
* account: A's SELF-narrowing, the task-child shape class), and the root
|
|
66
|
+
* keeps the one-link shape (delegate == A's account, delegator != A). The
|
|
67
|
+
* AND-only property again makes the three-link chain AS OR MORE restrictive
|
|
68
|
+
* than the two-hop task chain; a chain longer than three, a leaf whose
|
|
69
|
+
* delegate is not this signer, or a middle link that is not a self-delegation
|
|
70
|
+
* is refused exactly as before.
|
|
39
71
|
*/
|
|
40
72
|
|
|
41
73
|
/** `Delegation` — `(address delegate, address delegator, bytes32 authority, Caveat[] caveats, uint256 salt, bytes signature)`. */
|
|
@@ -95,11 +127,96 @@ declare const REDEEM_DELEGATIONS_ABI: readonly [{
|
|
|
95
127
|
declare const SINGLE_DEFAULT_MODE: Hex;
|
|
96
128
|
/**
|
|
97
129
|
* Verify `redeemCalldata` (the bytes passed to `execute()`, selector already
|
|
98
|
-
* confirmed to be `redeemDelegations` by the caller) is EXACTLY Haven's
|
|
99
|
-
*
|
|
100
|
-
*
|
|
130
|
+
* confirmed to be `redeemDelegations` by the caller) is EXACTLY one of Haven's
|
|
131
|
+
* two shapes: a single delegation redeemed by `ownAccount` and granted by
|
|
132
|
+
* someone else, or (#3329) that grant behind one task-budget child `ownAccount`
|
|
133
|
+
* delegated to itself; in `SingleDefault` mode, with no encoding slack
|
|
134
|
+
* anywhere. Throws
|
|
101
135
|
* `HavenSigningError` (never returns) on any disagreement.
|
|
102
136
|
*/
|
|
103
137
|
declare function assertRedeemsOwnBudgetDelegation(redeemCalldata: Hex, ownAccount: Address): void;
|
|
104
138
|
|
|
105
|
-
|
|
139
|
+
/**
|
|
140
|
+
* Sub-budget child guards (#3330) — the SDK-side local verification the
|
|
141
|
+
* signer runs before its delegate key signs a sub-budget child, mirroring
|
|
142
|
+
* `task-budget-guards.ts`'s `assertOwnTaskChild` for the two-row sub-budget
|
|
143
|
+
* tree. ONE verifier covers BOTH rows, because the same signer (the
|
|
144
|
+
* delegating agent A's own key) signs both:
|
|
145
|
+
*
|
|
146
|
+
* - A's PARENT-CHILD row: `delegate === delegator === A's own account`
|
|
147
|
+
* (self-delegated, the task-child shape class).
|
|
148
|
+
* - B's GRANT row: `delegator === A's own account`, `delegate === B's
|
|
149
|
+
* delegate account` (a real grant to another account).
|
|
150
|
+
*
|
|
151
|
+
* What makes a sub-budget child a sub-budget child (vs a task budget's
|
|
152
|
+
* hard-cap child) is the AMOUNT caveat: an `ERC20PeriodTransferEnforcer`
|
|
153
|
+
* caveat whose terms are `abi.encode(address token, uint256 periodAmount,
|
|
154
|
+
* uint256 periodDuration, uint256 startDate)` — the period-scoped slice of
|
|
155
|
+
* the parent's own period meter, with the parent's periodDuration and
|
|
156
|
+
* startDate reused verbatim. A hard-cap `ERC20TransferAmountEnforcer` caveat
|
|
157
|
+
* is refused: that would be a task budget, which this channel must never
|
|
158
|
+
* sign (two different products, two different sign-context endpoints).
|
|
159
|
+
*
|
|
160
|
+
* Authority, timestamp and recipient-pin checks are byte-for-byte the
|
|
161
|
+
* task-child discipline: authority must equal the declared parent hash and
|
|
162
|
+
* never be ROOT; expiry must be in the future and equal the declared one; a
|
|
163
|
+
* pinned recipient must match exactly, and an unpinned budget must not grow
|
|
164
|
+
* one. Enforcers are the pinned constants from `settlement-child.ts` (no kit
|
|
165
|
+
* dependency) — same reasoning as that file's header documents.
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
/** The pinned `ERC20PeriodTransferEnforcer` (bytecode-identical on Base + Base Sepolia). */
|
|
169
|
+
declare const ERC20_PERIOD_TRANSFER_ENFORCER = "0x474e3Ae7E169e940607cC624Da8A15Eb120139aB";
|
|
170
|
+
interface SubBudgetChildTypedData {
|
|
171
|
+
domain: {
|
|
172
|
+
name?: string;
|
|
173
|
+
version?: string;
|
|
174
|
+
chainId?: number | string;
|
|
175
|
+
verifyingContract?: string;
|
|
176
|
+
};
|
|
177
|
+
types: Record<string, unknown>;
|
|
178
|
+
primaryType: string;
|
|
179
|
+
message: Record<string, unknown>;
|
|
180
|
+
}
|
|
181
|
+
/** The struct hash of a sub-budget child — the redemption chain's link value. */
|
|
182
|
+
declare function hashSubBudgetDelegation(delegation: DelegationForHashing): Hex;
|
|
183
|
+
|
|
184
|
+
interface SubBudgetChildExpectation {
|
|
185
|
+
chainId: number;
|
|
186
|
+
tokenAddress: string;
|
|
187
|
+
/** Per-period amount in atomic units — the child's periodAmount. */
|
|
188
|
+
periodAmountAtomic: string;
|
|
189
|
+
/** The PARENT's period duration (seconds), reused verbatim in the caveat. */
|
|
190
|
+
periodDurationSeconds: number;
|
|
191
|
+
/** The PARENT's period anchor, reused verbatim in the caveat. */
|
|
192
|
+
startDate: number;
|
|
193
|
+
recipientAddress: string | null;
|
|
194
|
+
/** unix seconds — the `TimestampEnforcer`'s `beforeThreshold`. */
|
|
195
|
+
expiresAt: number;
|
|
196
|
+
/** The hash of the row's parent delegation (`parent_delegation_hash`). */
|
|
197
|
+
parentDelegationHash: string;
|
|
198
|
+
/**
|
|
199
|
+
* A's own derived delegate account — the child's `delegator` on BOTH rows.
|
|
200
|
+
*/
|
|
201
|
+
delegateAccount: string;
|
|
202
|
+
/**
|
|
203
|
+
* The row's own `delegate`: A's account again for the parent-child row,
|
|
204
|
+
* B's delegate account for the grant row. Served in the sign-context's
|
|
205
|
+
* `expected` (read from the stored child JSON, not trusted caller input).
|
|
206
|
+
*/
|
|
207
|
+
childDelegateAccount: string;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Verify a sub-budget child delegation is EXACTLY what
|
|
211
|
+
* `GET /sub-budgets/:id/sign-context` (purpose `open`) declared, that its
|
|
212
|
+
* `delegator` is this signer's own account, and that its `delegate` is the
|
|
213
|
+
* declared child delegate. Throws `HavenTypedDataRefusedError` (never
|
|
214
|
+
* returns) on any disagreement.
|
|
215
|
+
*/
|
|
216
|
+
declare function assertOwnSubBudgetChild(typedData: unknown, expected: SubBudgetChildExpectation, delegateAddress: string, now?: number): void;
|
|
217
|
+
/** Close UserOp verification — byte-for-byte the task-budget discipline. */
|
|
218
|
+
declare function assertOwnSubBudgetCloseUserOp(typedData: Record<string, unknown>, expected: {
|
|
219
|
+
delegationHash: string;
|
|
220
|
+
}, delegateAddress: string): void;
|
|
221
|
+
|
|
222
|
+
export { DELEGATION_TUPLE_COMPONENTS, DelegationForHashing, ERC20_PERIOD_TRANSFER_ENFORCER, REDEEM_DELEGATIONS_ABI, SINGLE_DEFAULT_MODE, type SubBudgetChildExpectation, type SubBudgetChildTypedData, addressFromKey, assertOwnSubBudgetChild, assertOwnSubBudgetCloseUserOp, assertRedeemsOwnBudgetDelegation, hashSubBudgetDelegation, signHash, verifySignature };
|
package/dist/edge.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { hashDelegation, DIRECT_PAYMENT_CHAIN_IDS, assertOwnTaskBudgetCloseUserOp, HavenTypedDataRefusedError } from './chunk-R3RZJJWL.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, TYPED_DATA_NOT_ALLOWED, assertBoundDirectPaymentUserOp, assertFundingLegPaysDelegate, assertOwnSettlementChild, assertOwnTaskBudgetCloseUserOp, assertOwnTaskChild, buildSweepAuthorizationMessage, buildSweepTypedData, buildX402ExpectedMessage, createNextStepBuilder, decodeBase64Json, encodeBase64Json, hashDelegation, havenClientIdentity, isSupportedNodeVersion, isTaskChildTypedData, readClientUpdate, selectStandardPaymentOption, toStandardPaymentRequirements, unsupportedNodeVersionMessage, x402AuthorizationAmount, x402V2PaymentEnvelope } from './chunk-R3RZJJWL.js';
|
|
3
|
+
import { HavenSigningError, DELEGATION_MANAGER, deriveDelegateAccountAddress, ROOT_AUTHORITY, CAVEAT_ENFORCERS } from './chunk-NNIT7WAI.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-NNIT7WAI.js';
|
|
4
5
|
import { secp256k1 } from '@noble/curves/secp256k1';
|
|
5
6
|
import { privateKeyToAddress, publicKeyToAddress } from 'viem/accounts';
|
|
6
7
|
import { serializeSignature } from 'viem';
|
|
@@ -51,6 +52,205 @@ function verifySignature(hash, signature, expectedAddress) {
|
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
54
|
|
|
54
|
-
|
|
55
|
+
// src/sub-budget-guards.ts
|
|
56
|
+
var ERC20_PERIOD_TRANSFER_ENFORCER = "0x474e3Ae7E169e940607cC624Da8A15Eb120139aB";
|
|
57
|
+
var DELEGATION_DOMAIN_NAME = "DelegationManager";
|
|
58
|
+
var DELEGATION_DOMAIN_VERSION = "1";
|
|
59
|
+
var EXPECTED_DELEGATION_TYPES = {
|
|
60
|
+
Caveat: [
|
|
61
|
+
{ name: "enforcer", type: "address" },
|
|
62
|
+
{ name: "terms", type: "bytes" }
|
|
63
|
+
],
|
|
64
|
+
Delegation: [
|
|
65
|
+
{ name: "delegate", type: "address" },
|
|
66
|
+
{ name: "delegator", type: "address" },
|
|
67
|
+
{ name: "authority", type: "bytes32" },
|
|
68
|
+
{ name: "caveats", type: "Caveat[]" },
|
|
69
|
+
{ name: "salt", type: "uint256" }
|
|
70
|
+
]
|
|
71
|
+
};
|
|
72
|
+
function same(a, b) {
|
|
73
|
+
return !!a && !!b && a.toLowerCase() === b.toLowerCase();
|
|
74
|
+
}
|
|
75
|
+
function sliceTerms(terms, start, end) {
|
|
76
|
+
const body = terms.startsWith("0x") ? terms.slice(2).toLowerCase() : terms.toLowerCase();
|
|
77
|
+
return end === void 0 ? body.slice(start * 2) : body.slice(start * 2, end * 2);
|
|
78
|
+
}
|
|
79
|
+
function findCaveat(caveats, enforcer) {
|
|
80
|
+
return caveats.find((c) => same(c.enforcer, enforcer));
|
|
81
|
+
}
|
|
82
|
+
function hashSubBudgetDelegation(delegation) {
|
|
83
|
+
return hashDelegation(delegation);
|
|
84
|
+
}
|
|
85
|
+
function refuseSubBudgetChild(what, detail) {
|
|
86
|
+
throw new HavenTypedDataRefusedError(
|
|
87
|
+
`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.`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
function assertOwnSubBudgetChild(typedData, expected, delegateAddress, now = Date.now()) {
|
|
91
|
+
const td = typedData;
|
|
92
|
+
if (td?.primaryType !== "Delegation") {
|
|
93
|
+
refuseSubBudgetChild("it is not a Delegation payload", `primaryType was '${String(td?.primaryType)}'.`);
|
|
94
|
+
}
|
|
95
|
+
if (!same(td.domain?.verifyingContract, DELEGATION_MANAGER)) {
|
|
96
|
+
refuseSubBudgetChild(
|
|
97
|
+
"the EIP-712 domain names an unknown DelegationManager",
|
|
98
|
+
`Expected ${DELEGATION_MANAGER}, got ${td.domain?.verifyingContract}.`
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
if (td.domain?.name !== DELEGATION_DOMAIN_NAME || td.domain?.version !== DELEGATION_DOMAIN_VERSION) {
|
|
102
|
+
refuseSubBudgetChild(
|
|
103
|
+
"the EIP-712 domain name/version does not match the pinned DelegationManager domain",
|
|
104
|
+
`Expected name '${DELEGATION_DOMAIN_NAME}' version '${DELEGATION_DOMAIN_VERSION}'; got name '${String(td.domain?.name)}' version '${String(td.domain?.version)}'.`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
if (JSON.stringify(td.types?.Caveat) !== JSON.stringify(EXPECTED_DELEGATION_TYPES.Caveat) || JSON.stringify(td.types?.Delegation) !== JSON.stringify(EXPECTED_DELEGATION_TYPES.Delegation)) {
|
|
108
|
+
refuseSubBudgetChild(
|
|
109
|
+
"its EIP-712 type definitions do not match the pinned Delegation/Caveat shape",
|
|
110
|
+
"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."
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
const domainChain = Number(td.domain?.chainId);
|
|
114
|
+
if (!Number.isFinite(domainChain) || !DIRECT_PAYMENT_CHAIN_IDS.has(domainChain)) {
|
|
115
|
+
refuseSubBudgetChild(
|
|
116
|
+
"it is scoped to a chain the delegation rail has no pinned contracts for",
|
|
117
|
+
`Chains with pinned contracts: ${[...DIRECT_PAYMENT_CHAIN_IDS].join(", ")}; the child says ${String(td.domain?.chainId)}.`
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
if (domainChain !== expected.chainId) {
|
|
121
|
+
refuseSubBudgetChild(
|
|
122
|
+
"it is scoped to the wrong chain",
|
|
123
|
+
`Expected chain ${expected.chainId}; the child says ${td.domain?.chainId}.`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
const ownAccount = deriveDelegateAccountAddress(delegateAddress);
|
|
127
|
+
const delegator = td.message?.delegator;
|
|
128
|
+
if (typeof delegator !== "string" || !same(delegator, ownAccount)) {
|
|
129
|
+
refuseSubBudgetChild(
|
|
130
|
+
"it is not delegated by this agent's own account",
|
|
131
|
+
`Expected delegator ${ownAccount}; the child names ${String(delegator)}.`
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
if (!same(delegator, expected.delegateAccount)) {
|
|
135
|
+
refuseSubBudgetChild(
|
|
136
|
+
"its delegator does not match the account Haven declared",
|
|
137
|
+
`Expected ${expected.delegateAccount}; the child names ${String(delegator)}.`
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
const delegate = td.message?.delegate;
|
|
141
|
+
if (typeof delegate !== "string" || !same(delegate, expected.childDelegateAccount)) {
|
|
142
|
+
refuseSubBudgetChild(
|
|
143
|
+
"its delegate does not match the declared child delegate",
|
|
144
|
+
`Expected ${expected.childDelegateAccount}; the child names ${String(delegate)}.`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
const authority = td.message?.authority;
|
|
148
|
+
if (typeof authority !== "string" || !/^0x[0-9a-fA-F]{64}$/.test(authority)) {
|
|
149
|
+
refuseSubBudgetChild("its authority is not a 32-byte delegation hash", `Got ${String(authority)}.`);
|
|
150
|
+
}
|
|
151
|
+
if (authority.toLowerCase() === ROOT_AUTHORITY) {
|
|
152
|
+
refuseSubBudgetChild(
|
|
153
|
+
"it is a ROOT delegation, not a re-delegation of the delegating agent budget",
|
|
154
|
+
"A sub-budget always chains under the delegating agent budget delegation."
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
if (!same(authority, expected.parentDelegationHash)) {
|
|
158
|
+
refuseSubBudgetChild(
|
|
159
|
+
"it chains under a different parent delegation",
|
|
160
|
+
`Expected authority ${expected.parentDelegationHash}; the child says ${authority}.`
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
const caveats = td.message?.caveats ?? [];
|
|
164
|
+
if (findCaveat(caveats, CAVEAT_ENFORCERS.erc20TransferAmount)) {
|
|
165
|
+
refuseSubBudgetChild(
|
|
166
|
+
"it carries a hard-cap transfer-amount caveat (the task-budget shape)",
|
|
167
|
+
"A sub-budget is period-scoped: its amount caveat must be an ERC20PeriodTransferEnforcer caveat slicing the parent period meter, never a lifetime cap."
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
const period = findCaveat(caveats, ERC20_PERIOD_TRANSFER_ENFORCER);
|
|
171
|
+
if (!period) {
|
|
172
|
+
refuseSubBudgetChild(
|
|
173
|
+
"it has no ERC-20 period-transfer caveat",
|
|
174
|
+
"Without it the child is not bounded to a per-period amount."
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
const token = `0x${sliceTerms(period.terms, 0, 20)}`;
|
|
178
|
+
if (!same(token, expected.tokenAddress)) {
|
|
179
|
+
refuseSubBudgetChild("it spends a different token", `Expected ${expected.tokenAddress}, child pins ${token}.`);
|
|
180
|
+
}
|
|
181
|
+
const periodAmount = BigInt(`0x${sliceTerms(period.terms, 20, 52)}`);
|
|
182
|
+
if (periodAmount !== BigInt(expected.periodAmountAtomic)) {
|
|
183
|
+
refuseSubBudgetChild(
|
|
184
|
+
"the period amount does not match",
|
|
185
|
+
`Expected ${expected.periodAmountAtomic}; the child allows ${periodAmount.toString()}.`
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
const periodDuration = Number(BigInt(`0x${sliceTerms(period.terms, 52, 84)}`));
|
|
189
|
+
if (periodDuration !== expected.periodDurationSeconds) {
|
|
190
|
+
refuseSubBudgetChild(
|
|
191
|
+
"its period duration does not match the parent window Haven declared",
|
|
192
|
+
`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.`
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
const startDate = Number(BigInt(`0x${sliceTerms(period.terms, 84, 116)}`));
|
|
196
|
+
if (startDate !== expected.startDate) {
|
|
197
|
+
refuseSubBudgetChild(
|
|
198
|
+
"its period start does not match the parent anchor Haven declared",
|
|
199
|
+
`Expected ${expected.startDate}; the child says ${startDate}.`
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
const timestamp = findCaveat(caveats, CAVEAT_ENFORCERS.timestamp);
|
|
203
|
+
if (!timestamp) {
|
|
204
|
+
refuseSubBudgetChild("it never expires", "A sub-budget child without a timestamp caveat is open-ended.");
|
|
205
|
+
}
|
|
206
|
+
const beforeThreshold = Number(BigInt(`0x${sliceTerms(timestamp.terms, 16, 32)}`));
|
|
207
|
+
const nowSec = Math.floor(now / 1e3);
|
|
208
|
+
if (beforeThreshold <= nowSec) {
|
|
209
|
+
refuseSubBudgetChild("it has already expired", `Expiry ${beforeThreshold} is not in the future.`);
|
|
210
|
+
}
|
|
211
|
+
if (beforeThreshold !== expected.expiresAt) {
|
|
212
|
+
refuseSubBudgetChild(
|
|
213
|
+
"its expiry does not match what Haven declared",
|
|
214
|
+
`Expected ${expected.expiresAt}; the child says ${beforeThreshold}.`
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
const calldata = findCaveat(caveats, CAVEAT_ENFORCERS.allowedCalldata);
|
|
218
|
+
if (expected.recipientAddress) {
|
|
219
|
+
if (!calldata) {
|
|
220
|
+
refuseSubBudgetChild(
|
|
221
|
+
"it has no recipient pin",
|
|
222
|
+
"The sub-budget was issued with a pinned recipient, so the child must carry one."
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
const startIndex = BigInt(`0x${sliceTerms(calldata.terms, 0, 32)}`);
|
|
226
|
+
if (startIndex !== 4n) {
|
|
227
|
+
refuseSubBudgetChild(
|
|
228
|
+
"the recipient pin points at the wrong calldata offset",
|
|
229
|
+
`Expected offset 4, got ${startIndex.toString()}.`
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
const paddedRecipient = sliceTerms(calldata.terms, 32, 64);
|
|
233
|
+
if (paddedRecipient.slice(0, 24) !== "0".repeat(24)) {
|
|
234
|
+
refuseSubBudgetChild("the pinned recipient is not a plain address", "Its 32-byte word is not a padded address.");
|
|
235
|
+
}
|
|
236
|
+
const recipient = `0x${paddedRecipient.slice(24)}`;
|
|
237
|
+
if (!same(recipient, expected.recipientAddress)) {
|
|
238
|
+
refuseSubBudgetChild(
|
|
239
|
+
"it pins a different recipient than Haven declared",
|
|
240
|
+
`Expected ${expected.recipientAddress}; the child pins ${recipient}.`
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
} else if (calldata) {
|
|
244
|
+
refuseSubBudgetChild(
|
|
245
|
+
"it pins a recipient the sub-budget was not issued with",
|
|
246
|
+
"This sub-budget is open (no recipient pin); the child must not add one."
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
function assertOwnSubBudgetCloseUserOp(typedData, expected, delegateAddress) {
|
|
251
|
+
assertOwnTaskBudgetCloseUserOp(typedData, expected, delegateAddress);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export { ERC20_PERIOD_TRANSFER_ENFORCER, addressFromKey, assertOwnSubBudgetChild, assertOwnSubBudgetCloseUserOp, hashSubBudgetDelegation, signHash, verifySignature };
|
|
55
255
|
//# sourceMappingURL=edge.js.map
|
|
56
256
|
//# 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,G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AAA,MAAM,aAAa,MAAA,CAAO,QAAA,CAAS,IAAI,KAAA,CAAM,GAAG,GAAG,EAAE,CAAA;AACrD,IAAA,MAAM,WAAA,GAAc,UAAA,KAAe,CAAA,IAAK,UAAA,KAAe,EAAA,GAAK,IAAI,UAAA,KAAe,CAAA,IAAK,UAAA,KAAe,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/edge-signing.ts","../src/sub-budget-guards.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,QA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ACF;AACA,IAAA,MAAM,eAAA,GAAkB,UAAA,CAAW,QAAA,CAAS,KAAA,EAAO,IAAI,EAAE,CAAA;AACzD,IAAA,IAAI,eAAA,CAAgB,MAAM,CAAA,EAAG,EAAE,MAAM,GAAA,CAAI,MAAA,CAAO,EAAE,CAAA,EAAG;AACnD,MAAA,oBAAA,CAAqB,+CAA+C,2CAA2C,CAAA;AAAA,IACjH;AACA,IAAA,MAAM,SAAA,GAAY,CAAA,EAAA,EAAK,eAAA,CAAgB,KAAA,CAAM,EAAE,CAAC,CAAA,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":["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","/**\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"]}
|