@pushchain/core 6.0.26 → 6.0.27-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/AGW.md +226 -0
  2. package/CHANGELOG.md +18 -0
  3. package/README.md +2 -0
  4. package/package.json +7 -1
  5. package/src/lib/agentic/agentic.d.ts +23 -0
  6. package/src/lib/agentic/agentic.js +51 -0
  7. package/src/lib/agentic/agentic.types.d.ts +414 -0
  8. package/src/lib/agentic/agentic.types.js +3 -0
  9. package/src/lib/agentic/capabilities.d.ts +37 -0
  10. package/src/lib/agentic/capabilities.js +64 -0
  11. package/src/lib/agentic/chain.d.ts +3 -0
  12. package/src/lib/agentic/chain.js +15 -0
  13. package/src/lib/agentic/codec/abi-layout.d.ts +20 -0
  14. package/src/lib/agentic/codec/abi-layout.js +168 -0
  15. package/src/lib/agentic/codec/defaults.d.ts +51 -0
  16. package/src/lib/agentic/codec/defaults.js +53 -0
  17. package/src/lib/agentic/codec/ids.d.ts +35 -0
  18. package/src/lib/agentic/codec/ids.js +74 -0
  19. package/src/lib/agentic/codec/native.d.ts +73 -0
  20. package/src/lib/agentic/codec/native.js +341 -0
  21. package/src/lib/agentic/codec/pins.d.ts +20 -0
  22. package/src/lib/agentic/codec/pins.js +29 -0
  23. package/src/lib/agentic/codec/rules.d.ts +45 -0
  24. package/src/lib/agentic/codec/rules.js +110 -0
  25. package/src/lib/agentic/codec/selectors.d.ts +19 -0
  26. package/src/lib/agentic/codec/selectors.js +60 -0
  27. package/src/lib/agentic/codec/session.d.ts +54 -0
  28. package/src/lib/agentic/codec/session.js +65 -0
  29. package/src/lib/agentic/codec/svm-accounts.d.ts +35 -0
  30. package/src/lib/agentic/codec/svm-accounts.js +68 -0
  31. package/src/lib/agentic/codec/svm-idl.d.ts +11 -0
  32. package/src/lib/agentic/codec/svm-idl.js +226 -0
  33. package/src/lib/agentic/codec/svm-terms.d.ts +157 -0
  34. package/src/lib/agentic/codec/svm-terms.js +246 -0
  35. package/src/lib/agentic/codec/universal-terms.d.ts +82 -0
  36. package/src/lib/agentic/codec/universal-terms.js +43 -0
  37. package/src/lib/agentic/codec/universal.d.ts +14 -0
  38. package/src/lib/agentic/codec/universal.js +173 -0
  39. package/src/lib/agentic/constants.d.ts +12 -0
  40. package/src/lib/agentic/constants.js +16 -0
  41. package/src/lib/agentic/context.d.ts +29 -0
  42. package/src/lib/agentic/context.js +56 -0
  43. package/src/lib/agentic/contracts/abi/v6/agent-validator.d.ts +105 -0
  44. package/src/lib/agentic/contracts/abi/v6/agent-validator.js +149 -0
  45. package/src/lib/agentic/contracts/abi/v6/agw-factory.d.ts +1146 -0
  46. package/src/lib/agentic/contracts/abi/v6/agw-factory.js +1499 -0
  47. package/src/lib/agentic/contracts/abi/v6/agw.d.ts +1306 -0
  48. package/src/lib/agentic/contracts/abi/v6/agw.js +1713 -0
  49. package/src/lib/agentic/contracts/abi/v6/index.d.ts +6 -0
  50. package/src/lib/agentic/contracts/abi/v6/index.js +17 -0
  51. package/src/lib/agentic/contracts/abi/v6/smart-session.d.ts +1493 -0
  52. package/src/lib/agentic/contracts/abi/v6/smart-session.js +1965 -0
  53. package/src/lib/agentic/contracts/abi/v6/universal-rules-policy.d.ts +1661 -0
  54. package/src/lib/agentic/contracts/abi/v6/universal-rules-policy.js +2187 -0
  55. package/src/lib/agentic/contracts/donut-v6.d.ts +11 -0
  56. package/src/lib/agentic/contracts/donut-v6.js +15 -0
  57. package/src/lib/agentic/contracts/prc20-metadata.d.ts +26 -0
  58. package/src/lib/agentic/contracts/prc20-metadata.js +87 -0
  59. package/src/lib/agentic/contracts/reader.d.ts +35 -0
  60. package/src/lib/agentic/contracts/reader.js +16 -0
  61. package/src/lib/agentic/contracts/v6.d.ts +5739 -0
  62. package/src/lib/agentic/contracts/v6.js +343 -0
  63. package/src/lib/agentic/deployments.d.ts +61 -0
  64. package/src/lib/agentic/deployments.js +203 -0
  65. package/src/lib/agentic/errors.d.ts +65 -0
  66. package/src/lib/agentic/errors.js +84 -0
  67. package/src/lib/agentic/execution/guards.d.ts +9 -0
  68. package/src/lib/agentic/execution/guards.js +30 -0
  69. package/src/lib/agentic/execution/outbound.d.ts +55 -0
  70. package/src/lib/agentic/execution/outbound.js +187 -0
  71. package/src/lib/agentic/execution/send.d.ts +9 -0
  72. package/src/lib/agentic/execution/send.js +268 -0
  73. package/src/lib/agentic/execution/svm-instruction.d.ts +24 -0
  74. package/src/lib/agentic/execution/svm-instruction.js +40 -0
  75. package/src/lib/agentic/execution/svm-outbound.d.ts +18 -0
  76. package/src/lib/agentic/execution/svm-outbound.js +86 -0
  77. package/src/lib/agentic/execution/svm-payload.d.ts +18 -0
  78. package/src/lib/agentic/execution/svm-payload.js +136 -0
  79. package/src/lib/agentic/execution/svm-public.d.ts +5 -0
  80. package/src/lib/agentic/execution/svm-public.js +176 -0
  81. package/src/lib/agentic/execution/svm-quote.d.ts +11 -0
  82. package/src/lib/agentic/execution/svm-quote.js +24 -0
  83. package/src/lib/agentic/execution/svm-send.d.ts +44 -0
  84. package/src/lib/agentic/execution/svm-send.js +87 -0
  85. package/src/lib/agentic/index.d.ts +5 -0
  86. package/src/lib/agentic/index.js +10 -0
  87. package/src/lib/agentic/management/common.d.ts +29 -0
  88. package/src/lib/agentic/management/common.js +116 -0
  89. package/src/lib/agentic/management/create.d.ts +4 -0
  90. package/src/lib/agentic/management/create.js +138 -0
  91. package/src/lib/agentic/management/duplicates.d.ts +4 -0
  92. package/src/lib/agentic/management/duplicates.js +20 -0
  93. package/src/lib/agentic/management/label.d.ts +3 -0
  94. package/src/lib/agentic/management/label.js +17 -0
  95. package/src/lib/agentic/management/prepare.d.ts +7 -0
  96. package/src/lib/agentic/management/prepare.js +95 -0
  97. package/src/lib/agentic/management/rules-write.d.ts +42 -0
  98. package/src/lib/agentic/management/rules-write.js +279 -0
  99. package/src/lib/agentic/management/svm-context.d.ts +25 -0
  100. package/src/lib/agentic/management/svm-context.js +55 -0
  101. package/src/lib/agentic/management/svm-metadata.d.ts +10 -0
  102. package/src/lib/agentic/management/svm-metadata.js +56 -0
  103. package/src/lib/agentic/management/svm-public.d.ts +6 -0
  104. package/src/lib/agentic/management/svm-public.js +125 -0
  105. package/src/lib/agentic/management/svm.d.ts +27 -0
  106. package/src/lib/agentic/management/svm.js +150 -0
  107. package/src/lib/agentic/presets/catalog.d.ts +26 -0
  108. package/src/lib/agentic/presets/catalog.js +307 -0
  109. package/src/lib/agentic/presets/index.d.ts +5 -0
  110. package/src/lib/agentic/presets/index.js +265 -0
  111. package/src/lib/agentic/presets/types.d.ts +48 -0
  112. package/src/lib/agentic/presets/types.js +3 -0
  113. package/src/lib/agentic/public-rule.d.ts +9 -0
  114. package/src/lib/agentic/public-rule.js +326 -0
  115. package/src/lib/agentic/reads/checkpoints.d.ts +17 -0
  116. package/src/lib/agentic/reads/checkpoints.js +109 -0
  117. package/src/lib/agentic/reads/rules.d.ts +42 -0
  118. package/src/lib/agentic/reads/rules.js +302 -0
  119. package/src/lib/agentic/reads/snapshot.d.ts +28 -0
  120. package/src/lib/agentic/reads/snapshot.js +85 -0
  121. package/src/lib/agentic/reads/svm.d.ts +17 -0
  122. package/src/lib/agentic/reads/svm.js +52 -0
  123. package/src/lib/agentic/reads/wallets.d.ts +25 -0
  124. package/src/lib/agentic/reads/wallets.js +122 -0
  125. package/src/lib/agentic/response.d.ts +41 -0
  126. package/src/lib/agentic/response.js +358 -0
  127. package/src/lib/agentic/revert.d.ts +7 -0
  128. package/src/lib/agentic/revert.js +102 -0
  129. package/src/lib/agentic/runtime.d.ts +58 -0
  130. package/src/lib/agentic/runtime.js +104 -0
  131. package/src/lib/agentic/utils.d.ts +57 -0
  132. package/src/lib/agentic/utils.js +165 -0
  133. package/src/lib/agentic/wallet.d.ts +32 -0
  134. package/src/lib/agentic/wallet.js +130 -0
  135. package/src/lib/constants/enums.d.ts +6 -3
  136. package/src/lib/constants/enums.js +6 -3
  137. package/src/lib/constants/index.d.ts +61 -4
  138. package/src/lib/constants/index.js +17 -3
  139. package/src/lib/constants/read-state.d.ts +9 -0
  140. package/src/lib/constants/read-state.js +13 -1
  141. package/src/lib/index.d.ts +8 -2
  142. package/src/lib/index.js +9 -1
  143. package/src/lib/orchestrator/internals/errors.d.ts +7 -0
  144. package/src/lib/orchestrator/internals/errors.js +13 -1
  145. package/src/lib/orchestrator/internals/execute-standard.d.ts +3 -1
  146. package/src/lib/orchestrator/internals/execute-standard.js +3 -3
  147. package/src/lib/orchestrator/internals/outbound-sync.js +52 -7
  148. package/src/lib/orchestrator/internals/push-chain-tx.d.ts +1 -1
  149. package/src/lib/orchestrator/internals/push-chain-tx.js +13 -1
  150. package/src/lib/orchestrator/orchestrator.d.ts +3 -0
  151. package/src/lib/orchestrator/orchestrator.js +13 -1
  152. package/src/lib/orchestrator/orchestrator.types.d.ts +8 -0
  153. package/src/lib/orchestrator/route-detector.js +3 -2
  154. package/src/lib/orchestrator/svm-idl/resolve.d.ts +2 -0
  155. package/src/lib/orchestrator/svm-idl/resolve.js +11 -1
  156. package/src/lib/progress-hook/progress-hook.js +63 -0
  157. package/src/lib/progress-hook/progress-hook.types.d.ts +10 -1
  158. package/src/lib/progress-hook/progress-hook.types.js +10 -0
  159. package/src/lib/push-chain/push-chain.d.ts +57 -15
  160. package/src/lib/push-chain/push-chain.js +66 -7
  161. package/src/lib/read-state/destination.js +6 -1
  162. package/src/lib/read-state/read-params.d.ts +3 -3
  163. package/src/lib/read-state/read-params.js +3 -2
  164. package/src/lib/read-state/read-state.types.d.ts +3 -1
  165. package/src/lib/read-state/read-tracker.js +2 -2
  166. package/src/lib/read-state/spec-builder.js +1 -1
  167. package/src/lib/universal/prc20-address.d.ts +15 -0
  168. package/src/lib/universal/prc20-address.js +135 -0
  169. package/src/lib/utils.d.ts +6 -13
  170. package/src/lib/utils.js +5 -128
  171. package/src/lib/vm-client/evm-client.js +34 -20
@@ -0,0 +1,341 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NATIVE_TERMS_PARAM = exports.MAX_NATIVE_ACTIONS = exports.MAX_PINS = void 0;
4
+ exports.nativeActionsOf = nativeActionsOf;
5
+ exports.nativeRuleToActionTerms = nativeRuleToActionTerms;
6
+ exports.nativeRuleToTerms = nativeRuleToTerms;
7
+ exports.nativeActionToTerms = nativeActionToTerms;
8
+ exports.encodeNativeTerms = encodeNativeTerms;
9
+ exports.decodeNativeTerms = decodeNativeTerms;
10
+ exports.nativeTermsToRule = nativeTermsToRule;
11
+ exports.nativeActionTermsToRule = nativeActionTermsToRule;
12
+ exports.isUnlimited = isUnlimited;
13
+ const pins_1 = require("./pins");
14
+ const viem_1 = require("viem");
15
+ const errors_1 = require("../errors");
16
+ const abi_layout_1 = require("./abi-layout");
17
+ const defaults_1 = require("./defaults");
18
+ const selectors_1 = require("./selectors");
19
+ /** URP native limits at v6 (Types.sol MAX_PINS, AGW MAX_NATIVE_ACTIONS). */
20
+ exports.MAX_PINS = 8;
21
+ exports.MAX_NATIVE_ACTIONS = 8;
22
+ const UINT32_MAX = 2 ** 32 - 1;
23
+ const UINT48_MAX = 2 ** 48 - 1;
24
+ exports.NATIVE_TERMS_PARAM = {
25
+ type: 'tuple',
26
+ components: [
27
+ { name: 'validUntil', type: 'uint48' },
28
+ { name: 'target', type: 'address' },
29
+ { name: 'selector', type: 'bytes4' },
30
+ { name: 'maxValuePerCall', type: 'uint256' },
31
+ { name: 'maxValueTotal', type: 'uint256' },
32
+ {
33
+ name: 'amount',
34
+ type: 'tuple',
35
+ components: [
36
+ { name: 'enabled', type: 'bool' },
37
+ { name: 'offset', type: 'uint16' },
38
+ { name: 'maxPerCall', type: 'uint256' },
39
+ { name: 'maxTotal', type: 'uint256' },
40
+ ],
41
+ },
42
+ { name: 'maxCalls', type: 'uint32' },
43
+ {
44
+ name: 'pins',
45
+ type: 'tuple[]',
46
+ components: pins_1.ARG_PIN_COMPONENTS,
47
+ },
48
+ ],
49
+ };
50
+ function invalid(message, details) {
51
+ return new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, message, {
52
+ details,
53
+ });
54
+ }
55
+ function assertUint(value, bits, field) {
56
+ if (typeof value !== 'bigint' ||
57
+ value < BigInt(0) ||
58
+ value >= BigInt(2) ** BigInt(bits)) {
59
+ throw invalid(`${field} must be a uint${bits} bigint`);
60
+ }
61
+ }
62
+ const SINGLE_ACTION_FIELDS = [
63
+ 'target',
64
+ 'selector',
65
+ 'maxValuePerCall',
66
+ 'maxValueTotal',
67
+ 'maxCalls',
68
+ 'pins',
69
+ 'wirePins',
70
+ 'amount',
71
+ ];
72
+ /**
73
+ * The actions a native rule allows: the single-action shorthand, or the
74
+ * explicit `actions` list (1..8). Each carries its effective `validUntil`.
75
+ */
76
+ function nativeActionsOf(rule) {
77
+ if (!Number.isInteger(rule.validUntil) ||
78
+ rule.validUntil < 1 ||
79
+ rule.validUntil > UINT48_MAX) {
80
+ throw invalid('rule validUntil must be a positive unix timestamp in seconds (uint48)');
81
+ }
82
+ if (rule.actions !== undefined) {
83
+ if (!Array.isArray(rule.actions) ||
84
+ rule.actions.length < 1 ||
85
+ rule.actions.length > exports.MAX_NATIVE_ACTIONS) {
86
+ throw invalid(`actions must contain 1..${exports.MAX_NATIVE_ACTIONS} entries (the AGW native action limit)`);
87
+ }
88
+ const mixed = SINGLE_ACTION_FIELDS.filter((f) => rule[f] !== undefined);
89
+ if (mixed.length > 0) {
90
+ throw invalid(`actions cannot be combined with top-level ${mixed.join(', ')}; put them inside each action`);
91
+ }
92
+ return rule.actions.map((a, i) => {
93
+ if (!a || typeof a !== 'object' || Array.isArray(a)) {
94
+ throw invalid(`actions[${i}] must be an action object`, { action: i });
95
+ }
96
+ return { ...a, validUntil: a.validUntil ?? rule.validUntil };
97
+ });
98
+ }
99
+ const single = rule;
100
+ return [
101
+ {
102
+ target: single.target,
103
+ selector: single.selector,
104
+ maxValuePerCall: single.maxValuePerCall,
105
+ maxValueTotal: single.maxValueTotal,
106
+ maxCalls: single.maxCalls,
107
+ pins: single.pins,
108
+ wirePins: single.wirePins,
109
+ amount: single.amount,
110
+ validUntil: single.validUntil,
111
+ },
112
+ ];
113
+ }
114
+ /**
115
+ * Normalize and validate every action of a native rule into wire terms, in
116
+ * input order. (target, selector) pairs must be unique — the wallet refuses
117
+ * duplicates (DuplicateAction) because they share one action ID. Pure.
118
+ */
119
+ function nativeRuleToActionTerms(rule, ctx) {
120
+ if (rule.validUntil <= ctx.nowSeconds) {
121
+ throw invalid('rule validUntil must be a future unix timestamp in seconds (uint48)');
122
+ }
123
+ const actions = nativeActionsOf(rule);
124
+ const multi = rule.actions !== undefined;
125
+ const out = actions.map((action, i) => {
126
+ try {
127
+ return nativeActionToTerms(action, ctx);
128
+ }
129
+ catch (err) {
130
+ if (multi && err instanceof errors_1.AgenticError) {
131
+ throw new errors_1.AgenticError(err.code, `actions[${i}]: ${err.message}`, {
132
+ hint: err.hint,
133
+ details: { ...(err.details ?? {}), action: i },
134
+ cause: err,
135
+ });
136
+ }
137
+ throw err;
138
+ }
139
+ });
140
+ const seen = new Map();
141
+ out.forEach(({ terms }, i) => {
142
+ const key = `${terms.target.toLowerCase()}:${terms.selector}`;
143
+ const first = seen.get(key);
144
+ if (first !== undefined) {
145
+ throw invalid(`actions[${i}] repeats the target and selector of actions[${first}]`, { action: i });
146
+ }
147
+ seen.set(key, i);
148
+ });
149
+ return out;
150
+ }
151
+ /** Single-action convenience; multi-action rules use nativeRuleToActionTerms. */
152
+ function nativeRuleToTerms(rule, ctx) {
153
+ const all = nativeRuleToActionTerms(rule, ctx);
154
+ if (all.length !== 1) {
155
+ throw invalid('nativeRuleToTerms takes a single-action rule');
156
+ }
157
+ return all[0];
158
+ }
159
+ /** Normalize and validate one native action into wire terms. Pure. */
160
+ function nativeActionToTerms(rule, ctx) {
161
+ if (!(0, viem_1.isAddress)(rule.target, { strict: false }) ||
162
+ /^0x0{40}$/i.test(rule.target)) {
163
+ throw invalid('native rule target must be a non-zero address');
164
+ }
165
+ if (!Number.isInteger(rule.validUntil) ||
166
+ rule.validUntil <= ctx.nowSeconds ||
167
+ rule.validUntil > UINT48_MAX) {
168
+ throw invalid('validUntil must be a future unix timestamp in seconds (uint48)', {
169
+ validUntil: rule.validUntil,
170
+ });
171
+ }
172
+ const parsed = (0, selectors_1.parseSelector)(rule.selector);
173
+ const pinsIn = rule.pins ?? [];
174
+ if (parsed.valueOnly &&
175
+ (pinsIn.length > 0 || rule.wirePins?.length || rule.amount)) {
176
+ throw invalid('a value-only rule cannot carry pins or an amount rule');
177
+ }
178
+ if (pinsIn.length > exports.MAX_PINS) {
179
+ throw invalid(`at most ${exports.MAX_PINS} pins are allowed`, {
180
+ pins: pinsIn.length,
181
+ });
182
+ }
183
+ const pins = pinsIn.map((p, i) => {
184
+ if (p.arg !== undefined && p.offset !== undefined)
185
+ throw invalid('pin arg and offset are mutually exclusive');
186
+ if (p.offset !== undefined) {
187
+ if (typeof p.expected !== 'string' ||
188
+ !(0, viem_1.isHex)(p.expected) ||
189
+ p.expected.length !== 66) {
190
+ throw invalid(`pins[${i}].expected must be the exact 32-byte calldata word in offset form`);
191
+ }
192
+ return {
193
+ offset: (0, abi_layout_1.rawOffset)(p.offset, `pins[${i}]`),
194
+ expected: p.expected.toLowerCase(),
195
+ };
196
+ }
197
+ if (typeof p.arg !== 'number')
198
+ throw invalid('pin requires arg or offset');
199
+ const offset = (0, abi_layout_1.argumentOffset)(parsed.inputs, p.arg, `pins[${i}]`);
200
+ const param = parsed.inputs[p.arg];
201
+ return { offset, expected: (0, abi_layout_1.encodeArgWord)(param, p.expected, `pins[${i}]`) };
202
+ });
203
+ const offsets = new Set(pins.map((p) => p.offset));
204
+ if (offsets.size !== pins.length)
205
+ throw invalid('two pins address the same argument');
206
+ let amount = {
207
+ enabled: false,
208
+ offset: 0,
209
+ maxPerCall: BigInt(0),
210
+ maxTotal: BigInt(0),
211
+ };
212
+ if (rule.amount) {
213
+ let offset;
214
+ if (rule.amount.arg !== undefined && rule.amount.offset !== undefined)
215
+ throw invalid('amount arg and offset are mutually exclusive');
216
+ if (rule.amount.offset !== undefined) {
217
+ offset = (0, abi_layout_1.rawOffset)(rule.amount.offset, 'amount');
218
+ }
219
+ else {
220
+ if (typeof rule.amount.arg !== 'number')
221
+ throw invalid('amount requires arg or offset');
222
+ offset = (0, abi_layout_1.argumentOffset)(parsed.inputs, rule.amount.arg, 'amount');
223
+ const param = parsed.inputs[rule.amount.arg];
224
+ if (!/^uint\d*$/.test(param.type)) {
225
+ throw invalid(`amount argument must be an unsigned integer, got ${param.type}`);
226
+ }
227
+ }
228
+ if (offsets.has(offset))
229
+ throw invalid('amount argument is also pinned');
230
+ assertUint(rule.amount.maxPerCall, 256, 'amount.maxPerCall');
231
+ const maxTotal = (0, defaults_1.withDefault)(rule.amount.maxTotal, defaults_1.AGENTIC_DEFAULTS.native.amountMaxTotal);
232
+ assertUint(maxTotal, 256, 'amount.maxTotal');
233
+ amount = {
234
+ enabled: true,
235
+ offset,
236
+ maxPerCall: rule.amount.maxPerCall,
237
+ maxTotal,
238
+ };
239
+ }
240
+ const maxValuePerCall = (0, defaults_1.withDefault)(rule.maxValuePerCall, defaults_1.AGENTIC_DEFAULTS.native.maxValuePerCall);
241
+ const maxValueTotal = (0, defaults_1.withDefault)(rule.maxValueTotal, defaults_1.AGENTIC_DEFAULTS.native.maxValueTotal);
242
+ assertUint(maxValuePerCall, 256, 'maxValuePerCall');
243
+ assertUint(maxValueTotal, 256, 'maxValueTotal');
244
+ const maxCalls = rule.maxCalls === undefined
245
+ ? Number(defaults_1.AGENTIC_DEFAULTS.native.maxCalls.value)
246
+ : rule.maxCalls;
247
+ if (!Number.isInteger(maxCalls) || maxCalls <= 0 || maxCalls > UINT32_MAX) {
248
+ throw invalid('maxCalls must be a uint32 integer');
249
+ }
250
+ try {
251
+ (0, pins_1.validatePins)(rule.wirePins ??
252
+ pins.map((p) => ({
253
+ offset: p.offset,
254
+ op: 0,
255
+ value: p.expected,
256
+ })));
257
+ }
258
+ catch (error) {
259
+ throw invalid(error.message);
260
+ }
261
+ return {
262
+ parsed,
263
+ terms: {
264
+ validUntil: rule.validUntil,
265
+ target: (0, viem_1.getAddress)(rule.target),
266
+ selector: parsed.selector,
267
+ maxValuePerCall,
268
+ maxValueTotal,
269
+ amount,
270
+ maxCalls,
271
+ pins: rule.wirePins ??
272
+ pins.map((p) => ({
273
+ offset: p.offset,
274
+ op: 0,
275
+ value: p.expected,
276
+ })),
277
+ },
278
+ };
279
+ }
280
+ function encodeNativeTerms(terms) {
281
+ return (0, viem_1.encodeAbiParameters)([exports.NATIVE_TERMS_PARAM], [terms]);
282
+ }
283
+ function decodeNativeTerms(body) {
284
+ const [terms] = (0, viem_1.decodeAbiParameters)([exports.NATIVE_TERMS_PARAM], body);
285
+ return terms;
286
+ }
287
+ /**
288
+ * Wire terms back to the public NativeRule shape. Stored terms carry calldata
289
+ * offsets and exact words but no ABI, so pins and amount are returned in the
290
+ * raw `offset` form (never a guessed argument index) and `selector` is the
291
+ * 4-byte hex. The result re-encodes to the same terms.
292
+ */
293
+ function nativeTermsToRule(terms, agent) {
294
+ const rule = {
295
+ agent: (0, viem_1.getAddress)(agent),
296
+ target: (0, viem_1.getAddress)(terms.target),
297
+ selector: terms.selector.toLowerCase() === selectors_1.VALUE_ONLY_SELECTOR
298
+ ? 'value-only'
299
+ : terms.selector.toLowerCase(),
300
+ validUntil: Number(terms.validUntil),
301
+ maxValuePerCall: terms.maxValuePerCall,
302
+ maxValueTotal: terms.maxValueTotal,
303
+ maxCalls: Number(terms.maxCalls),
304
+ };
305
+ if (terms.pins.length > 0)
306
+ rule.wirePins = terms.pins.map((p) => ({ ...p }));
307
+ if (terms.amount.enabled) {
308
+ rule.amount = {
309
+ offset: Number(terms.amount.offset),
310
+ maxPerCall: terms.amount.maxPerCall,
311
+ maxTotal: terms.amount.maxTotal,
312
+ };
313
+ }
314
+ return rule;
315
+ }
316
+ /**
317
+ * Stored action terms back to a public NativeRule: the single-action
318
+ * shorthand for one action, otherwise `actions` in stored order. The rule's
319
+ * `validUntil` is the earliest action expiry; an action expiring later keeps
320
+ * its own `validUntil`, so the result re-encodes to identical terms.
321
+ */
322
+ function nativeActionTermsToRule(termsList, agent) {
323
+ if (termsList.length === 0) {
324
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.RULE_READ_FAILED, 'a native rule needs at least one action');
325
+ }
326
+ if (termsList.length === 1)
327
+ return nativeTermsToRule(termsList[0], agent);
328
+ const decoded = termsList.map((t) => nativeTermsToRule(t, agent));
329
+ const validUntil = Math.min(...decoded.map((d) => d.validUntil));
330
+ return {
331
+ agent: (0, viem_1.getAddress)(agent),
332
+ validUntil,
333
+ actions: decoded.map(({ agent: _agent, validUntil: actionValidUntil, ...action }) => actionValidUntil === validUntil
334
+ ? action
335
+ : { ...action, validUntil: actionValidUntil }),
336
+ };
337
+ }
338
+ function isUnlimited(value) {
339
+ return value === defaults_1.UINT256_MAX;
340
+ }
341
+ //# sourceMappingURL=native.js.map
@@ -0,0 +1,20 @@
1
+ import type { Hex } from 'viem';
2
+ export interface ArgPinWire {
3
+ offset: number;
4
+ op: 0 | 1;
5
+ value: Hex;
6
+ }
7
+ export declare const ARG_PIN_COMPONENTS: readonly [{
8
+ readonly name: "offset";
9
+ readonly type: "uint16";
10
+ }, {
11
+ readonly name: "op";
12
+ readonly type: "uint8";
13
+ }, {
14
+ readonly name: "value";
15
+ readonly type: "bytes32";
16
+ }];
17
+ export declare const UNLIMITED_CALLS = 4294967295;
18
+ export declare const ANY_ACCOUNT_COUNT = 255;
19
+ /** Validate every encoded constraint before signing. */
20
+ export declare function validatePins(pins: readonly ArgPinWire[]): void;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ANY_ACCOUNT_COUNT = exports.UNLIMITED_CALLS = exports.ARG_PIN_COMPONENTS = void 0;
4
+ exports.validatePins = validatePins;
5
+ exports.ARG_PIN_COMPONENTS = [
6
+ { name: 'offset', type: 'uint16' },
7
+ { name: 'op', type: 'uint8' },
8
+ { name: 'value', type: 'bytes32' },
9
+ ];
10
+ exports.UNLIMITED_CALLS = 0xffffffff;
11
+ exports.ANY_ACCOUNT_COUNT = 255;
12
+ /** Validate every encoded constraint before signing. */
13
+ function validatePins(pins) {
14
+ if (!Array.isArray(pins) || pins.length > 8)
15
+ throw new Error('pins must contain at most eight constraints');
16
+ const seen = new Set();
17
+ for (const p of pins) {
18
+ if (!Number.isInteger(p.offset) ||
19
+ p.offset < 4 ||
20
+ p.offset + 32 > 65535 ||
21
+ ![0, 1].includes(p.op) ||
22
+ !/^0x[0-9a-fA-F]{64}$/.test(p.value))
23
+ throw new Error('invalid argument pin');
24
+ if (seen.has(p.offset))
25
+ throw new Error('duplicate pin offset');
26
+ seen.add(p.offset);
27
+ }
28
+ }
29
+ //# sourceMappingURL=pins.js.map
@@ -0,0 +1,45 @@
1
+ import { type Address, type Hex } from 'viem';
2
+ import { AgenticCapability } from '../capabilities';
3
+ import type { InternalRule, UniversalRule, SolanaRule } from '../agentic.types';
4
+ import { type NativeTermsWire } from './native';
5
+ import { type SessionWire } from './session';
6
+ import { type ResolvedUniversalRule } from './universal';
7
+ import { type SvmTermsWire } from './svm-terms';
8
+ import type { UniversalTermsWire } from './universal-terms';
9
+ export declare function isSolanaRule(rule: InternalRule): rule is SolanaRule;
10
+ export declare function isUniversalRule(rule: InternalRule): rule is UniversalRule;
11
+ /** CAIP-2 chain a rule governs: omitted = the connected Push chain. */
12
+ export declare function ruleChain(rule: InternalRule, pushChainNamespace: string): string;
13
+ export declare function chainHash(chainNamespace: string): Hex;
14
+ export interface RuleEncodeContext {
15
+ /** 'eip155:<pushChainId>' of the connected network. */
16
+ pushChainNamespace: string;
17
+ validator: Address;
18
+ rulesPolicy: Address;
19
+ capabilities: ReadonlySet<AgenticCapability>;
20
+ nowSeconds: number;
21
+ gateway?: Address;
22
+ universal?: ReadonlyMap<UniversalRule, ResolvedUniversalRule>;
23
+ svm?: ReadonlyMap<SolanaRule, SvmTermsWire>;
24
+ /**
25
+ * Addresses a native rule may never target (AGW _requireGrantableTarget /
26
+ * URP NativeTargetIsGateway): wallet, engine, policy, validator, factory,
27
+ * gateway. 0x0 and 0x1 are always refused.
28
+ */
29
+ forbiddenTargets?: readonly Address[];
30
+ }
31
+ export interface PreparedRule {
32
+ rule: InternalRule;
33
+ agent: Address;
34
+ chainNamespace: string;
35
+ session: SessionWire;
36
+ /** Native terms, one per action in input order, when the rule is native. */
37
+ nativeTerms?: NativeTermsWire[];
38
+ universalTerms?: UniversalTermsWire;
39
+ svmTerms?: SvmTermsWire;
40
+ }
41
+ /** Validate before signing. V6 permits several grants for the same agent/chain. */
42
+ export declare function prepareRules(rules: readonly InternalRule[], ctx: RuleEncodeContext & {
43
+ /** Wallet owner; when given, an agent equal to it is AGENT_IS_OWNER. */
44
+ owner?: Address;
45
+ }): PreparedRule[];
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isSolanaRule = isSolanaRule;
4
+ exports.isUniversalRule = isUniversalRule;
5
+ exports.ruleChain = ruleChain;
6
+ exports.chainHash = chainHash;
7
+ exports.prepareRules = prepareRules;
8
+ const viem_1 = require("viem");
9
+ const capabilities_1 = require("../capabilities");
10
+ const errors_1 = require("../errors");
11
+ const native_1 = require("./native");
12
+ const session_1 = require("./session");
13
+ const universal_1 = require("./universal");
14
+ const svm_terms_1 = require("./svm-terms");
15
+ const v6_1 = require("../contracts/v6");
16
+ function isSolanaRule(rule) {
17
+ return (typeof rule.chainNamespace === 'string' &&
18
+ rule.chainNamespace.startsWith('solana:'));
19
+ }
20
+ function isUniversalRule(rule) {
21
+ return (rule.chainNamespace !== undefined && !isSolanaRule(rule));
22
+ }
23
+ /** CAIP-2 chain a rule governs: omitted = the connected Push chain. */
24
+ function ruleChain(rule, pushChainNamespace) {
25
+ return (rule.chainNamespace ?? pushChainNamespace);
26
+ }
27
+ function chainHash(chainNamespace) {
28
+ return (0, viem_1.keccak256)((0, viem_1.toBytes)(chainNamespace));
29
+ }
30
+ /** Validate before signing. V6 permits several grants for the same agent/chain. */
31
+ function prepareRules(rules, ctx) {
32
+ if (!Array.isArray(rules)) {
33
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'rules must be an array');
34
+ }
35
+ return rules.map((rule, i) => {
36
+ if (!rule || typeof rule !== 'object') {
37
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, `rules[${i}] is not an object`);
38
+ }
39
+ if (!(0, viem_1.isAddress)(rule.agent, { strict: false }) ||
40
+ /^0x0{40}$/i.test(rule.agent)) {
41
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, `rules[${i}].agent must be a non-zero Push address`);
42
+ }
43
+ const agent = (0, viem_1.getAddress)(rule.agent);
44
+ if (ctx.owner && agent === (0, viem_1.getAddress)(ctx.owner)) {
45
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.AGENT_IS_OWNER, `rules[${i}].agent is the wallet owner; the owner uses the owner door`);
46
+ }
47
+ const chainNamespace = ruleChain(rule, ctx.pushChainNamespace);
48
+ if (isUniversalRule(rule) &&
49
+ rule.chainNamespace === ctx.pushChainNamespace) {
50
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, `rules[${i}] names the connected Push chain; omit chainNamespace for a native rule`);
51
+ }
52
+ if ('ref' in rule) {
53
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'InternalRule ref is not supported; remove it from the input');
54
+ }
55
+ if (isSolanaRule(rule)) {
56
+ (0, capabilities_1.requireCapability)(ctx.capabilities, capabilities_1.AgenticCapability.UNIVERSAL_SVM_RULES);
57
+ const terms = ctx.svm?.get(rule);
58
+ if (!terms || !ctx.gateway)
59
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'SVM rules require resolved wallet context and validated instruction layouts');
60
+ return {
61
+ rule,
62
+ agent,
63
+ chainNamespace,
64
+ svmTerms: terms,
65
+ session: (0, session_1.buildSession)({
66
+ validator: ctx.validator,
67
+ rulesPolicy: ctx.rulesPolicy,
68
+ agent,
69
+ actions: [
70
+ {
71
+ target: ctx.gateway,
72
+ selector: v6_1.SEND_OUTBOUND_SELECTOR,
73
+ initData: (0, session_1.encodeEnvelope)(chainNamespace, (0, svm_terms_1.encodeSvmTerms)(terms, ctx.nowSeconds)),
74
+ },
75
+ ],
76
+ }),
77
+ };
78
+ }
79
+ if (isUniversalRule(rule)) {
80
+ return (0, universal_1.encodeUniversalRule)(rule, ctx);
81
+ }
82
+ (0, capabilities_1.requireCapability)(ctx.capabilities, capabilities_1.AgenticCapability.NATIVE_RULES);
83
+ const actions = (0, native_1.nativeRuleToActionTerms)(rule, ctx).map((a) => a.terms);
84
+ const forbidden = [
85
+ '0x0000000000000000000000000000000000000000',
86
+ '0x0000000000000000000000000000000000000001',
87
+ ...(ctx.forbiddenTargets ?? []),
88
+ ].map((a) => a.toLowerCase());
89
+ actions.forEach((terms, j) => {
90
+ if (forbidden.includes(terms.target.toLowerCase())) {
91
+ const where = actions.length > 1 ? `rules[${i}].actions[${j}]` : `rules[${i}]`;
92
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, `${where}.target ${terms.target} is a wallet-infrastructure address the contracts refuse as a native target`);
93
+ }
94
+ });
95
+ // One rule, one rule ID: every action is a separate (target, selector)
96
+ // entry of the same session, each with its own stored terms.
97
+ const session = (0, session_1.buildSession)({
98
+ validator: ctx.validator,
99
+ agent,
100
+ rulesPolicy: ctx.rulesPolicy,
101
+ actions: actions.map((terms) => ({
102
+ target: terms.target,
103
+ selector: terms.selector,
104
+ initData: (0, session_1.encodeEnvelope)(chainNamespace, (0, native_1.encodeNativeTerms)(terms)),
105
+ })),
106
+ });
107
+ return { rule, agent, chainNamespace, session, nativeTerms: actions };
108
+ });
109
+ }
110
+ //# sourceMappingURL=rules.js.map
@@ -0,0 +1,19 @@
1
+ import { type AbiParameter } from 'viem';
2
+ import type { AgenticHex, Selector } from '../agentic.types';
3
+ /** Selector URP uses for a value-only native rule (empty calldata). */
4
+ export declare const VALUE_ONLY_SELECTOR: "0xffffffff";
5
+ export interface ParsedSelector {
6
+ /** 4-byte selector, lowercase. */
7
+ selector: AgenticHex;
8
+ /** Canonical signature when the caller supplied one. */
9
+ signature?: string;
10
+ /** ABI inputs, only available when the caller supplied a signature. */
11
+ inputs?: readonly AbiParameter[];
12
+ valueOnly: boolean;
13
+ }
14
+ /**
15
+ * Parse a public selector (`'0x617ba037'`, `'supply(address,uint256,address,uint16)'`
16
+ * or `'value-only'`). A signature is required wherever pins, amounts or
17
+ * beneficiaries need argument positions; a bare hex selector carries none.
18
+ */
19
+ export declare function parseSelector(input: Selector | 'value-only'): ParsedSelector;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VALUE_ONLY_SELECTOR = void 0;
4
+ exports.parseSelector = parseSelector;
5
+ const viem_1 = require("viem");
6
+ const errors_1 = require("../errors");
7
+ /** Selector URP uses for a value-only native rule (empty calldata). */
8
+ exports.VALUE_ONLY_SELECTOR = '0xffffffff';
9
+ const HEX_SELECTOR = /^0x[0-9a-fA-F]{8}$/;
10
+ /**
11
+ * Parse a public selector (`'0x617ba037'`, `'supply(address,uint256,address,uint16)'`
12
+ * or `'value-only'`). A signature is required wherever pins, amounts or
13
+ * beneficiaries need argument positions; a bare hex selector carries none.
14
+ */
15
+ function parseSelector(input) {
16
+ if (input === 'value-only') {
17
+ return { selector: exports.VALUE_ONLY_SELECTOR, valueOnly: true };
18
+ }
19
+ if (typeof input !== 'string') {
20
+ throw invalid(`selector must be a string, got ${typeof input}`);
21
+ }
22
+ if (HEX_SELECTOR.test(input)) {
23
+ const selector = input.toLowerCase();
24
+ if (selector === exports.VALUE_ONLY_SELECTOR) {
25
+ return { selector, valueOnly: true };
26
+ }
27
+ return { selector, valueOnly: false };
28
+ }
29
+ let item;
30
+ try {
31
+ const parsed = (0, viem_1.parseAbiItem)(`function ${input}`);
32
+ if (parsed.type !== 'function')
33
+ throw new Error('not a function');
34
+ item = parsed;
35
+ }
36
+ catch (cause) {
37
+ throw invalid(`selector "${input}" is neither a 4-byte hex selector nor a function signature`, cause);
38
+ }
39
+ const selector = (0, viem_1.toFunctionSelector)(item).toLowerCase();
40
+ if (selector === exports.VALUE_ONLY_SELECTOR) {
41
+ throw invalid(`selector "${input}" collides with the reserved value-only selector`);
42
+ }
43
+ return {
44
+ selector,
45
+ signature: `${item.name}(${item.inputs.map(canonicalType).join(',')})`,
46
+ inputs: item.inputs,
47
+ valueOnly: false,
48
+ };
49
+ }
50
+ function canonicalType(p) {
51
+ if (p.type.startsWith('tuple')) {
52
+ const components = p.components ?? [];
53
+ return `(${components.map(canonicalType).join(',')})${p.type.slice(5)}`;
54
+ }
55
+ return p.type;
56
+ }
57
+ function invalid(message, cause) {
58
+ return new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, message, { cause });
59
+ }
60
+ //# sourceMappingURL=selectors.js.map
@@ -0,0 +1,54 @@
1
+ import { type AbiParameter, type Address, type Hex } from 'viem';
2
+ /**
3
+ * The smartsessions `Session` a grant carries (DataTypes.sol:56-86, pinned
4
+ * v6). The wallet overwrites `salt` with its grant nonce, so the value
5
+ * sent is irrelevant; it is always zero here.
6
+ */
7
+ export interface SessionWire {
8
+ sessionValidator: Address;
9
+ sessionValidatorInitData: Hex;
10
+ salt: Hex;
11
+ userOpPolicies: readonly {
12
+ policy: Address;
13
+ initData: Hex;
14
+ }[];
15
+ erc7739Policies: {
16
+ allowedERC7739Content: readonly {
17
+ appDomainSeparator: Hex;
18
+ contentNames: readonly string[];
19
+ }[];
20
+ erc1271Policies: readonly {
21
+ policy: Address;
22
+ initData: Hex;
23
+ }[];
24
+ };
25
+ actions: readonly {
26
+ actionTargetSelector: Hex;
27
+ actionTarget: Address;
28
+ actionPolicies: readonly {
29
+ policy: Address;
30
+ initData: Hex;
31
+ }[];
32
+ }[];
33
+ permitERC4337Paymaster: boolean;
34
+ }
35
+ export declare const SESSION_PARAM: AbiParameter;
36
+ export declare const ENVELOPE_VERSION = 1;
37
+ /** V6 URP envelope: abi.encode(uint16(1), string chainNamespace, bytes body). */
38
+ export declare function encodeEnvelope(chainNamespace: string, body: Hex): Hex;
39
+ export declare function decodeEnvelope(initData: Hex): {
40
+ chainNamespace: string;
41
+ body: Hex;
42
+ };
43
+ export declare function buildSession(ctx: {
44
+ validator: Address;
45
+ agent: Address;
46
+ rulesPolicy: Address;
47
+ actions: {
48
+ target: Address;
49
+ selector: Hex;
50
+ initData: Hex;
51
+ }[];
52
+ }): SessionWire;
53
+ export declare function encodeSession(session: SessionWire): Hex;
54
+ export declare function decodeSession(bytes: Hex): SessionWire;