@pushchain/core 6.0.25 → 6.0.27-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.
Files changed (183) hide show
  1. package/AGW.md +194 -0
  2. package/CHANGELOG.md +74 -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 +409 -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 +15 -0
  14. package/src/lib/agentic/codec/abi-layout.js +121 -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 +75 -0
  20. package/src/lib/agentic/codec/native.js +328 -0
  21. package/src/lib/agentic/codec/rules.d.ts +45 -0
  22. package/src/lib/agentic/codec/rules.js +110 -0
  23. package/src/lib/agentic/codec/selectors.d.ts +19 -0
  24. package/src/lib/agentic/codec/selectors.js +60 -0
  25. package/src/lib/agentic/codec/session.d.ts +54 -0
  26. package/src/lib/agentic/codec/session.js +65 -0
  27. package/src/lib/agentic/codec/svm-accounts.d.ts +35 -0
  28. package/src/lib/agentic/codec/svm-accounts.js +68 -0
  29. package/src/lib/agentic/codec/svm-idl.d.ts +11 -0
  30. package/src/lib/agentic/codec/svm-idl.js +226 -0
  31. package/src/lib/agentic/codec/svm-terms.d.ts +157 -0
  32. package/src/lib/agentic/codec/svm-terms.js +242 -0
  33. package/src/lib/agentic/codec/universal-terms.d.ts +67 -0
  34. package/src/lib/agentic/codec/universal-terms.js +41 -0
  35. package/src/lib/agentic/codec/universal.d.ts +14 -0
  36. package/src/lib/agentic/codec/universal.js +157 -0
  37. package/src/lib/agentic/constants.d.ts +11 -0
  38. package/src/lib/agentic/constants.js +15 -0
  39. package/src/lib/agentic/context.d.ts +29 -0
  40. package/src/lib/agentic/context.js +56 -0
  41. package/src/lib/agentic/contracts/abi/v5/agent-validator.d.ts +105 -0
  42. package/src/lib/agentic/contracts/abi/v5/agent-validator.js +149 -0
  43. package/src/lib/agentic/contracts/abi/v5/agw-factory.d.ts +847 -0
  44. package/src/lib/agentic/contracts/abi/v5/agw-factory.js +1108 -0
  45. package/src/lib/agentic/contracts/abi/v5/agw.d.ts +1211 -0
  46. package/src/lib/agentic/contracts/abi/v5/agw.js +1588 -0
  47. package/src/lib/agentic/contracts/abi/v5/index.d.ts +6 -0
  48. package/src/lib/agentic/contracts/abi/v5/index.js +17 -0
  49. package/src/lib/agentic/contracts/abi/v5/smart-session.d.ts +1493 -0
  50. package/src/lib/agentic/contracts/abi/v5/smart-session.js +1965 -0
  51. package/src/lib/agentic/contracts/abi/v5/universal-rules-policy.d.ts +1640 -0
  52. package/src/lib/agentic/contracts/abi/v5/universal-rules-policy.js +2160 -0
  53. package/src/lib/agentic/contracts/donut-v5.d.ts +11 -0
  54. package/src/lib/agentic/contracts/donut-v5.js +15 -0
  55. package/src/lib/agentic/contracts/prc20-metadata.d.ts +26 -0
  56. package/src/lib/agentic/contracts/prc20-metadata.js +87 -0
  57. package/src/lib/agentic/contracts/reader.d.ts +35 -0
  58. package/src/lib/agentic/contracts/reader.js +16 -0
  59. package/src/lib/agentic/contracts/v5.d.ts +5323 -0
  60. package/src/lib/agentic/contracts/v5.js +316 -0
  61. package/src/lib/agentic/deployments.d.ts +61 -0
  62. package/src/lib/agentic/deployments.js +203 -0
  63. package/src/lib/agentic/errors.d.ts +71 -0
  64. package/src/lib/agentic/errors.js +92 -0
  65. package/src/lib/agentic/execution/guards.d.ts +9 -0
  66. package/src/lib/agentic/execution/guards.js +30 -0
  67. package/src/lib/agentic/execution/outbound.d.ts +55 -0
  68. package/src/lib/agentic/execution/outbound.js +187 -0
  69. package/src/lib/agentic/execution/send.d.ts +9 -0
  70. package/src/lib/agentic/execution/send.js +276 -0
  71. package/src/lib/agentic/execution/svm-instruction.d.ts +24 -0
  72. package/src/lib/agentic/execution/svm-instruction.js +40 -0
  73. package/src/lib/agentic/execution/svm-outbound.d.ts +18 -0
  74. package/src/lib/agentic/execution/svm-outbound.js +86 -0
  75. package/src/lib/agentic/execution/svm-payload.d.ts +18 -0
  76. package/src/lib/agentic/execution/svm-payload.js +136 -0
  77. package/src/lib/agentic/execution/svm-public.d.ts +5 -0
  78. package/src/lib/agentic/execution/svm-public.js +182 -0
  79. package/src/lib/agentic/execution/svm-quote.d.ts +11 -0
  80. package/src/lib/agentic/execution/svm-quote.js +24 -0
  81. package/src/lib/agentic/execution/svm-send.d.ts +44 -0
  82. package/src/lib/agentic/execution/svm-send.js +90 -0
  83. package/src/lib/agentic/index.d.ts +5 -0
  84. package/src/lib/agentic/index.js +10 -0
  85. package/src/lib/agentic/management/common.d.ts +29 -0
  86. package/src/lib/agentic/management/common.js +114 -0
  87. package/src/lib/agentic/management/create.d.ts +16 -0
  88. package/src/lib/agentic/management/create.js +271 -0
  89. package/src/lib/agentic/management/duplicates.d.ts +4 -0
  90. package/src/lib/agentic/management/duplicates.js +20 -0
  91. package/src/lib/agentic/management/label.d.ts +3 -0
  92. package/src/lib/agentic/management/label.js +17 -0
  93. package/src/lib/agentic/management/prepare.d.ts +7 -0
  94. package/src/lib/agentic/management/prepare.js +95 -0
  95. package/src/lib/agentic/management/rules-write.d.ts +42 -0
  96. package/src/lib/agentic/management/rules-write.js +279 -0
  97. package/src/lib/agentic/management/svm-context.d.ts +25 -0
  98. package/src/lib/agentic/management/svm-context.js +55 -0
  99. package/src/lib/agentic/management/svm-metadata.d.ts +10 -0
  100. package/src/lib/agentic/management/svm-metadata.js +56 -0
  101. package/src/lib/agentic/management/svm-public.d.ts +6 -0
  102. package/src/lib/agentic/management/svm-public.js +125 -0
  103. package/src/lib/agentic/management/svm.d.ts +27 -0
  104. package/src/lib/agentic/management/svm.js +150 -0
  105. package/src/lib/agentic/presets/catalog.d.ts +21 -0
  106. package/src/lib/agentic/presets/catalog.js +10 -0
  107. package/src/lib/agentic/presets/index.d.ts +5 -0
  108. package/src/lib/agentic/presets/index.js +190 -0
  109. package/src/lib/agentic/presets/types.d.ts +44 -0
  110. package/src/lib/agentic/presets/types.js +3 -0
  111. package/src/lib/agentic/public-rule.d.ts +9 -0
  112. package/src/lib/agentic/public-rule.js +342 -0
  113. package/src/lib/agentic/reads/checkpoints.d.ts +17 -0
  114. package/src/lib/agentic/reads/checkpoints.js +109 -0
  115. package/src/lib/agentic/reads/rules.d.ts +42 -0
  116. package/src/lib/agentic/reads/rules.js +286 -0
  117. package/src/lib/agentic/reads/snapshot.d.ts +28 -0
  118. package/src/lib/agentic/reads/snapshot.js +85 -0
  119. package/src/lib/agentic/reads/svm.d.ts +17 -0
  120. package/src/lib/agentic/reads/svm.js +52 -0
  121. package/src/lib/agentic/reads/wallets.d.ts +25 -0
  122. package/src/lib/agentic/reads/wallets.js +122 -0
  123. package/src/lib/agentic/response.d.ts +41 -0
  124. package/src/lib/agentic/response.js +358 -0
  125. package/src/lib/agentic/revert.d.ts +7 -0
  126. package/src/lib/agentic/revert.js +102 -0
  127. package/src/lib/agentic/runtime.d.ts +58 -0
  128. package/src/lib/agentic/runtime.js +95 -0
  129. package/src/lib/agentic/utils.d.ts +57 -0
  130. package/src/lib/agentic/utils.js +165 -0
  131. package/src/lib/agentic/wallet.d.ts +32 -0
  132. package/src/lib/agentic/wallet.js +130 -0
  133. package/src/lib/constants/chain.js +1 -1
  134. package/src/lib/constants/enums.d.ts +6 -3
  135. package/src/lib/constants/enums.js +6 -3
  136. package/src/lib/constants/index.d.ts +65 -7
  137. package/src/lib/constants/index.js +18 -3
  138. package/src/lib/constants/read-state.d.ts +15 -2
  139. package/src/lib/constants/read-state.js +19 -3
  140. package/src/lib/index.d.ts +9 -3
  141. package/src/lib/index.js +11 -2
  142. package/src/lib/orchestrator/internals/errors.d.ts +7 -0
  143. package/src/lib/orchestrator/internals/errors.js +13 -1
  144. package/src/lib/orchestrator/internals/execute-standard.d.ts +3 -1
  145. package/src/lib/orchestrator/internals/execute-standard.js +3 -3
  146. package/src/lib/orchestrator/internals/outbound-sync.js +52 -7
  147. package/src/lib/orchestrator/internals/push-chain-tx.d.ts +1 -1
  148. package/src/lib/orchestrator/internals/push-chain-tx.js +13 -1
  149. package/src/lib/orchestrator/orchestrator.d.ts +3 -0
  150. package/src/lib/orchestrator/orchestrator.js +13 -1
  151. package/src/lib/orchestrator/orchestrator.types.d.ts +8 -0
  152. package/src/lib/orchestrator/route-detector.js +3 -2
  153. package/src/lib/orchestrator/svm-idl/resolve.d.ts +2 -0
  154. package/src/lib/orchestrator/svm-idl/resolve.js +11 -1
  155. package/src/lib/progress-hook/progress-hook.js +84 -0
  156. package/src/lib/progress-hook/progress-hook.types.d.ts +14 -2
  157. package/src/lib/progress-hook/progress-hook.types.js +13 -0
  158. package/src/lib/push-chain/push-chain.d.ts +58 -16
  159. package/src/lib/push-chain/push-chain.js +68 -8
  160. package/src/lib/push-client/push-client.d.ts +7 -0
  161. package/src/lib/push-client/push-client.js +41 -15
  162. package/src/lib/read-state/destination.js +6 -1
  163. package/src/lib/read-state/envelopes/evm.js +2 -3
  164. package/src/lib/read-state/envelopes/svm.js +11 -1
  165. package/src/lib/read-state/read-executor.js +4 -0
  166. package/src/lib/read-state/read-params.d.ts +71 -26
  167. package/src/lib/read-state/read-params.js +60 -15
  168. package/src/lib/read-state/read-state.types.d.ts +44 -1
  169. package/src/lib/read-state/read-state.types.js +23 -1
  170. package/src/lib/read-state/read-tracker.d.ts +6 -1
  171. package/src/lib/read-state/read-tracker.js +83 -17
  172. package/src/lib/read-state/registry.js +2 -3
  173. package/src/lib/read-state/result-decoder.js +6 -0
  174. package/src/lib/read-state/spec-builder.js +2 -2
  175. package/src/lib/read-state/svm-account.d.ts +20 -0
  176. package/src/lib/read-state/svm-account.js +114 -0
  177. package/src/lib/read-state/svm-pda.d.ts +13 -0
  178. package/src/lib/read-state/svm-pda.js +147 -0
  179. package/src/lib/universal/prc20-address.d.ts +15 -0
  180. package/src/lib/universal/prc20-address.js +135 -0
  181. package/src/lib/utils.d.ts +6 -13
  182. package/src/lib/utils.js +5 -128
  183. package/src/lib/vm-client/evm-client.js +34 -20
@@ -0,0 +1,342 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.WALLET = void 0;
27
+ exports.publicRules = publicRules;
28
+ exports.isPublicRule = isPublicRule;
29
+ exports.evmPinOffset = evmPinOffset;
30
+ exports.normalizePublicRule = normalizePublicRule;
31
+ const web3_js_1 = require("@solana/web3.js");
32
+ const viem_1 = require("viem");
33
+ const errors_1 = require("./errors");
34
+ const selectors_1 = require("./codec/selectors");
35
+ const abi_layout_1 = require("./codec/abi-layout");
36
+ const defaults_1 = require("./codec/defaults");
37
+ const svm_accounts_1 = require("./codec/svm-accounts");
38
+ exports.WALLET = 'agentic:wallet';
39
+ const PUBLIC_INPUT = Symbol('AGW public rule');
40
+ /** Mark authored inputs for normalization after the wallet address is fixed. */
41
+ function publicRules(rules) {
42
+ if (!Array.isArray(rules))
43
+ throw invalid('rules must be an array');
44
+ return rules.map((rule) => {
45
+ if (!rule || typeof rule !== 'object')
46
+ throw invalid('rule must be an object');
47
+ return { ...rule, [PUBLIC_INPUT]: true };
48
+ });
49
+ }
50
+ function isPublicRule(rule) {
51
+ return !!rule && !!rule[PUBLIC_INPUT];
52
+ }
53
+ function invalid(message) {
54
+ return new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, message);
55
+ }
56
+ function rejectFields(obj, fields, where) {
57
+ for (const field of fields)
58
+ if (field in obj)
59
+ throw invalid(`${where}.${field} is not supported by the current rule shape`);
60
+ }
61
+ function uint(value, name) {
62
+ if (typeof value !== 'bigint' || value < BigInt(0) || value > defaults_1.UINT256_MAX)
63
+ throw invalid(`${name} must be a uint256 bigint`);
64
+ return value;
65
+ }
66
+ function evmPinOffset(selector, arg) {
67
+ if (!Number.isInteger(arg) || arg < 0 || 4 + 32 * arg + 32 > 65535)
68
+ throw invalid('pin arg is outside the supported calldata range');
69
+ const parsed = (0, selectors_1.parseSelector)(selector);
70
+ if (parsed.valueOnly)
71
+ throw invalid('value-only calls cannot have argument pins');
72
+ if (!parsed.inputs)
73
+ return 4 + 32 * arg;
74
+ if (parsed.inputs.slice(0, arg).some((p) => (0, abi_layout_1.headSize)(p) !== 32))
75
+ throw (0, errors_1.capabilityUnavailable)('argumentLayout', 'pins after multi-word tuple/array arguments need an explicit lossless index convention');
76
+ return (0, abi_layout_1.argumentOffset)(parsed.inputs, arg, 'pin');
77
+ }
78
+ function expectedWord(selector, arg, value) {
79
+ const parsed = (0, selectors_1.parseSelector)(selector);
80
+ if (parsed.inputs)
81
+ return (0, abi_layout_1.encodeArgWord)(parsed.inputs[arg], value, 'pin');
82
+ if (typeof value === 'bigint')
83
+ return (0, viem_1.numberToHex)(uint(value, 'pin.expected'), { size: 32 });
84
+ if (typeof value === 'string' && (0, viem_1.isAddress)(value, { strict: false }))
85
+ return `0x${value.slice(2).padStart(64, '0')}`.toLowerCase();
86
+ if (typeof value === 'string' && (0, viem_1.isHex)(value) && value.length === 66)
87
+ return value.toLowerCase();
88
+ throw invalid('a raw EVM selector needs an address, uint256 or exact 32-byte expected word');
89
+ }
90
+ async function normalizePublicRule(input, wallet, runtime) {
91
+ const rule = input;
92
+ if ('format' in rule && rule.format === 'decoded')
93
+ return input;
94
+ const r = rule;
95
+ rejectFields(r, [
96
+ 'target',
97
+ 'selector',
98
+ 'actions',
99
+ 'pins',
100
+ 'amount',
101
+ 'maxCalls',
102
+ 'maxValuePerCall',
103
+ 'maxValueTotal',
104
+ 'ref',
105
+ 'allowedInstructions',
106
+ 'outputTokens',
107
+ ], 'rule');
108
+ if (!Number.isSafeInteger(r.validUntil) ||
109
+ r.validUntil <= runtime.nowSeconds() ||
110
+ r.validUntil >= 2 ** 48)
111
+ throw invalid('validUntil must be a future uint48 timestamp');
112
+ if (!Array.isArray(r.allowedCalls) ||
113
+ r.allowedCalls.length < 1 ||
114
+ r.allowedCalls.length > 32)
115
+ throw invalid('allowedCalls must contain 1..32 calls');
116
+ for (const c of r.allowedCalls) {
117
+ if (!c || typeof c !== 'object')
118
+ throw invalid('allowedCall must be an object');
119
+ rejectFields(c, [
120
+ 'beneficiary',
121
+ 'beneficiaryOffset',
122
+ 'amount',
123
+ 'maxCalls',
124
+ 'maxValuePerCall',
125
+ 'maxValueTotal',
126
+ 'validUntil',
127
+ ], 'allowedCall');
128
+ uint(c.maxValue ?? BigInt(0), 'allowedCall.maxValue');
129
+ if (c.pins !== undefined && !Array.isArray(c.pins))
130
+ throw invalid('pins must be an array');
131
+ for (const pin of c.pins ?? []) {
132
+ if (!pin || typeof pin !== 'object')
133
+ throw invalid('pin must be an object');
134
+ rejectFields(pin, ['offset', 'min'], 'pin');
135
+ if ('token' in pin && (!('account' in pin) || pin.expected !== exports.WALLET))
136
+ throw invalid('token is only valid on a WALLET account pin');
137
+ if ('arg' in pin === 'account' in pin)
138
+ throw invalid('pin must identify exactly one arg or account');
139
+ if ('expected' in pin === 'max' in pin)
140
+ throw invalid('pin must specify exactly one expected or max');
141
+ }
142
+ }
143
+ if (r.chainNamespace?.startsWith('solana:')) {
144
+ const { svmMetadata } = await Promise.resolve().then(() => __importStar(require('./management/svm-public')));
145
+ const gateway = await svmMetadata(runtime).gateway(r.chainNamespace);
146
+ if (gateway.chainNamespace !== r.chainNamespace)
147
+ throw invalid('gateway registry cluster mismatch');
148
+ return svmRule(r, (0, svm_accounts_1.deriveAgwSvmCea)(wallet, gateway.program).address);
149
+ }
150
+ const native = r.chainNamespace === undefined;
151
+ if (r.chainNamespace === runtime.pushChainNamespace)
152
+ throw invalid('omit chainNamespace for Push rules');
153
+ if (native && r.allowedCalls.length > 8)
154
+ throw (0, errors_1.capabilityUnavailable)('nativeActions', 'the deployed v5 wallet permits at most eight native actions');
155
+ if (native && (r.assets !== undefined || r.maxGasPerCall !== undefined))
156
+ throw invalid('native rules do not carry assets or maxGasPerCall');
157
+ const own = native
158
+ ? wallet
159
+ : (await runtime.resolveCEA(wallet, r.chainNamespace)).cea;
160
+ const nativeActions = [];
161
+ const calls = [];
162
+ for (const call of r.allowedCalls) {
163
+ const pins = call.pins ?? [];
164
+ let amount;
165
+ const eq = [];
166
+ let beneficiaryOffset;
167
+ const occupied = new Set();
168
+ for (const pin of pins) {
169
+ if ('account' in pin)
170
+ throw invalid('account pins only apply to Solana');
171
+ const offset = evmPinOffset(call.selector, pin.arg);
172
+ if (occupied.has(offset))
173
+ throw invalid('duplicate pin argument');
174
+ occupied.add(offset);
175
+ if (!native) {
176
+ if (!('expected' in pin) ||
177
+ pin.expected !== exports.WALLET ||
178
+ beneficiaryOffset !== undefined)
179
+ throw (0, errors_1.capabilityUnavailable)('evmDestinationPins', 'general or multiple EVM destination pins require contract change 6.3; v5 supports one WALLET pin per call');
180
+ beneficiaryOffset = offset;
181
+ }
182
+ else if ('max' in pin) {
183
+ if (amount)
184
+ throw (0, errors_1.capabilityUnavailable)('nativeMaxPins', 'v5 supports one max pin per native call');
185
+ amount = {
186
+ offset,
187
+ maxPerCall: uint(pin.max, 'pin.max'),
188
+ maxTotal: defaults_1.UINT256_MAX,
189
+ };
190
+ }
191
+ else {
192
+ eq.push({
193
+ offset,
194
+ expected: expectedWord(call.selector, pin.arg, pin.expected === exports.WALLET ? own : pin.expected),
195
+ });
196
+ }
197
+ }
198
+ if (native)
199
+ nativeActions.push({
200
+ target: (0, viem_1.getAddress)(call.target),
201
+ selector: call.selector,
202
+ pins: eq,
203
+ amount,
204
+ maxValuePerCall: call.maxValue ?? BigInt(0),
205
+ maxValueTotal: defaults_1.UINT256_MAX,
206
+ maxCalls: 0,
207
+ });
208
+ else
209
+ calls.push({
210
+ target: (0, viem_1.getAddress)(call.target),
211
+ selector: call.selector,
212
+ beneficiaryOffset,
213
+ maxValue: call.maxValue ?? BigInt(0),
214
+ });
215
+ }
216
+ if (native)
217
+ return { agent: r.agent, validUntil: r.validUntil, actions: nativeActions };
218
+ return {
219
+ agent: r.agent,
220
+ validUntil: r.validUntil,
221
+ chainNamespace: r.chainNamespace,
222
+ assets: (r.assets ?? []).map((a) => ({
223
+ ...a,
224
+ token: a.token,
225
+ maxTotal: a.maxTotal ?? BigInt(0),
226
+ })),
227
+ maxGasPerCall: r.maxGasPerCall ?? BigInt(0),
228
+ allowedCalls: calls,
229
+ };
230
+ }
231
+ function svmType(text) {
232
+ const array = text.match(/^(.+)\[(\d+)\]$/);
233
+ if (array) {
234
+ const count = Number(array[2]);
235
+ if (!Number.isSafeInteger(count) || count < 1 || count > 128)
236
+ throw invalid('unsupported SVM fixed array size');
237
+ return { array: [svmType(array[1]), count] };
238
+ }
239
+ if ([
240
+ 'u8',
241
+ 'u16',
242
+ 'u32',
243
+ 'u64',
244
+ 'i8',
245
+ 'i16',
246
+ 'i32',
247
+ 'i64',
248
+ 'bool',
249
+ 'pubkey',
250
+ ].includes(text))
251
+ return text;
252
+ if (text === 'publicKey')
253
+ return 'pubkey';
254
+ throw (0, errors_1.capabilityUnavailable)('svmArgumentLayout', `unsupported fixed-size Solana type ${text}`);
255
+ }
256
+ function svmRule(rule, cea) {
257
+ return {
258
+ agent: rule.agent,
259
+ validUntil: rule.validUntil,
260
+ chainNamespace: rule.chainNamespace,
261
+ outputTokens: [
262
+ ...new Set(rule.allowedCalls.flatMap((c) => (c.pins ?? []).flatMap((p) => 'account' in p && 'token' in p && p.expected === exports.WALLET && p.token
263
+ ? [typeof p.token === 'string' ? p.token : p.token.address]
264
+ : []))),
265
+ ],
266
+ assets: (rule.assets ?? []).map((asset) => ({
267
+ ...asset,
268
+ maxTotal: asset.maxTotal ?? BigInt(0),
269
+ })),
270
+ maxGasPerCall: rule.maxGasPerCall ?? BigInt(0),
271
+ allowedInstructions: rule.allowedCalls.map((call) => {
272
+ if ((call.maxValue ?? BigInt(0)) !== BigInt(0))
273
+ throw (0, errors_1.capabilityUnavailable)('svmCallValue', 'native Solana instruction value is governed by its transfer/pin, not an EVM call value');
274
+ const full = String(call.selector).match(/^([A-Za-z_][A-Za-z0-9_]*)\((.*)\)$/);
275
+ const raw = /^0x[0-9a-fA-F]{16}$/.test(call.selector);
276
+ if (!full && !raw)
277
+ throw invalid('Solana selector needs a fixed-width signature or eight-byte discriminator');
278
+ const types = full && full[2] ? full[2].split(',').map((s) => svmType(s.trim())) : [];
279
+ const name = full?.[1] ?? 'raw';
280
+ const discriminator = raw
281
+ ? (0, viem_1.hexToBytes)(call.selector)
282
+ : (0, viem_1.hexToBytes)((0, viem_1.sha256)((0, viem_1.toHex)(`global:${name
283
+ .replace(/([a-z0-9])([A-Z])/g, '$1_$2')
284
+ .toLowerCase()}`))).slice(0, 8);
285
+ const pins = call.pins ?? [];
286
+ const accounts = pins.filter((p) => 'account' in p);
287
+ if (accounts.some((p) => !Number.isInteger(p.account) || p.account < 0 || p.account >= 64))
288
+ throw invalid('Solana account position must be 0..63');
289
+ const accountCount = Math.max(0, ...accounts.map((p) => p.account + 1));
290
+ const idl = {
291
+ address: new web3_js_1.PublicKey((0, viem_1.hexToBytes)((0, svm_accounts_1.svmKey)(call.target))).toBase58(),
292
+ metadata: { name: 'rule_layout', version: '1', spec: '0.1.0' },
293
+ instructions: [
294
+ {
295
+ name,
296
+ discriminator: Array.from(discriminator),
297
+ accounts: Array.from({ length: accountCount }, (_, i) => ({
298
+ name: `account${i}`,
299
+ })),
300
+ args: types.map((type, i) => ({ name: `arg${i}`, type })),
301
+ },
302
+ ],
303
+ };
304
+ return {
305
+ maxAccounts: 0,
306
+ program: call.target,
307
+ instruction: { idl, name },
308
+ accounts: accounts.map((p) => ({
309
+ name: `account${p.account}`,
310
+ expected: p.expected === exports.WALLET
311
+ ? p.token
312
+ ? {
313
+ kind: 'walletATA',
314
+ token: typeof p.token === 'string' ? p.token : p.token.address,
315
+ }
316
+ : { kind: 'walletCEA' }
317
+ : { kind: 'address', address: p.expected },
318
+ })),
319
+ fields: pins
320
+ .filter((p) => 'arg' in p)
321
+ .map((p) => {
322
+ const arg = p.arg;
323
+ if (!Number.isInteger(arg) || arg < 0 || arg >= types.length)
324
+ throw (0, errors_1.capabilityUnavailable)('svmArgumentLayout', 'argument pins need a signature with the indexed argument type; use decoded raw constraints for stored rules');
325
+ if ('max' in p)
326
+ return { name: `arg${arg}`, max: uint(p.max, 'pin.max') };
327
+ const rawExpected = p.expected;
328
+ const eq = rawExpected === exports.WALLET ? cea : rawExpected;
329
+ return {
330
+ name: `arg${arg}`,
331
+ equals: typeof eq === 'bigint'
332
+ ? eq
333
+ : /^0x/.test(eq)
334
+ ? eq
335
+ : (0, svm_accounts_1.svmKey)(eq),
336
+ };
337
+ }),
338
+ };
339
+ }),
340
+ };
341
+ }
342
+ //# sourceMappingURL=public-rule.js.map
@@ -0,0 +1,17 @@
1
+ import type { Address } from 'viem';
2
+ import type { AgenticGeneration } from '../deployments';
3
+ import type { ChainReader } from '../contracts/reader';
4
+ import type { Checkpoint } from '../agentic.types';
5
+ /**
6
+ * w.checkpoints(): Checkpointed events in (block, tx, log) order, checked
7
+ * against the wallet's checkpointCount() at the same snapshot block.
8
+ * `sinceBlock` is inclusive. Evaluators should compare counts — one block can
9
+ * hold several ticks, and allowance pulls change balances without any tick.
10
+ */
11
+ export declare function readCheckpoints(reader: ChainReader, gen: AgenticGeneration, wallet: Address, sinceBlock?: bigint): Promise<{
12
+ checkpoints: Checkpoint[];
13
+ count: bigint;
14
+ blockNumber: bigint;
15
+ }>;
16
+ /** Sequence must be contiguous, end at `count`, and start at 1 for a full scan. */
17
+ export declare function validateSequence(seqs: number[], count: bigint, fullRange: boolean): string | null;
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readCheckpoints = readCheckpoints;
4
+ exports.validateSequence = validateSequence;
5
+ const capabilities_1 = require("../capabilities");
6
+ const errors_1 = require("../errors");
7
+ const snapshot_1 = require("./snapshot");
8
+ const MAX_ATTEMPTS = 2;
9
+ /**
10
+ * w.checkpoints(): Checkpointed events in (block, tx, log) order, checked
11
+ * against the wallet's checkpointCount() at the same snapshot block.
12
+ * `sinceBlock` is inclusive. Evaluators should compare counts — one block can
13
+ * hold several ticks, and allowance pulls change balances without any tick.
14
+ */
15
+ async function readCheckpoints(reader, gen, wallet, sinceBlock) {
16
+ (0, capabilities_1.requireCapability)(gen.capabilities, capabilities_1.AgenticCapability.CHECKPOINTS);
17
+ let lastError;
18
+ for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
19
+ const snap = await snapshot_1.Snapshot.at(reader);
20
+ const count = BigInt(await snap.read(wallet, gen.contracts.abis.wallet, 'checkpointCount'));
21
+ const from = sinceBlock !== undefined && sinceBlock > gen.startBlock
22
+ ? sinceBlock
23
+ : gen.startBlock;
24
+ const filter = {
25
+ address: wallet,
26
+ event: gen.contracts.events.checkpointed,
27
+ };
28
+ let logs;
29
+ try {
30
+ logs = await (0, snapshot_1.scanLogs)(reader, filter, from, snap.blockNumber);
31
+ }
32
+ catch (cause) {
33
+ const retained = lowestRetainedHeight(cause);
34
+ // A fresh wallet may have its entire history after the RPC retention
35
+ // floor. Never treat truncation as success: the FULL sequence/count
36
+ // validation below must still prove every checkpoint is present.
37
+ if (sinceBlock !== undefined ||
38
+ retained === undefined ||
39
+ retained <= from ||
40
+ retained > snap.blockNumber)
41
+ throw cause;
42
+ logs = await (0, snapshot_1.scanLogs)(reader, filter, retained, snap.blockNumber);
43
+ }
44
+ const decoded = gen.contracts
45
+ .parseCheckpointed(logs, wallet)
46
+ .sort((a, b) => a.blockNumber !== b.blockNumber
47
+ ? a.blockNumber < b.blockNumber
48
+ ? -1
49
+ : 1
50
+ : a.transactionIndex !== b.transactionIndex
51
+ ? a.transactionIndex - b.transactionIndex
52
+ : a.logIndex - b.logIndex);
53
+ const problem = validateSequence(decoded.map((c) => c.seq), count, sinceBlock === undefined || from === gen.startBlock);
54
+ if (!problem) {
55
+ return {
56
+ count,
57
+ blockNumber: snap.blockNumber,
58
+ checkpoints: decoded.map(({ seq, kind, ref, blockNumber, txHash }) => ({
59
+ seq,
60
+ kind,
61
+ ref,
62
+ blockNumber,
63
+ txHash,
64
+ })),
65
+ };
66
+ }
67
+ lastError = new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INCONSISTENT_READ, problem, {
68
+ details: { wallet, count, blockNumber: snap.blockNumber },
69
+ });
70
+ }
71
+ throw lastError;
72
+ }
73
+ /** Sequence must be contiguous, end at `count`, and start at 1 for a full scan. */
74
+ function validateSequence(seqs, count, fullRange) {
75
+ for (let i = 1; i < seqs.length; i++) {
76
+ if (seqs[i] !== seqs[i - 1] + 1)
77
+ return `checkpoint sequence gap between ${seqs[i - 1]} and ${seqs[i]}`;
78
+ }
79
+ if (fullRange) {
80
+ if (BigInt(seqs.length) !== count) {
81
+ return `found ${seqs.length} Checkpointed events but checkpointCount() is ${count}`;
82
+ }
83
+ if (seqs.length > 0 && seqs[0] !== 1)
84
+ return `first checkpoint seq is ${seqs[0]}, expected 1`;
85
+ return null;
86
+ }
87
+ if (seqs.length > 0 && BigInt(seqs[seqs.length - 1]) !== count) {
88
+ return `last checkpoint seq ${seqs[seqs.length - 1]} does not match checkpointCount() ${count}`;
89
+ }
90
+ return null;
91
+ }
92
+ /** Recognize Donut/CometBFT's explicit pruning error, not arbitrary RPC failures. */
93
+ function lowestRetainedHeight(error) {
94
+ const visited = new Set();
95
+ for (let depth = 0; depth < 8 && error && typeof error === 'object' && !visited.has(error); depth++) {
96
+ visited.add(error);
97
+ const value = error;
98
+ for (const text of [value.message, value.details]) {
99
+ if (typeof text !== 'string')
100
+ continue;
101
+ const match = /height \d+ is not available, lowest height is (\d+)/.exec(text);
102
+ if (match)
103
+ return BigInt(match[1]);
104
+ }
105
+ error = value.cause;
106
+ }
107
+ return undefined;
108
+ }
109
+ //# sourceMappingURL=checkpoints.js.map
@@ -0,0 +1,42 @@
1
+ import { type Address, type Hex } from 'viem';
2
+ import type { AgenticGeneration } from '../deployments';
3
+ import type { RulesRecord } from '../agentic.types';
4
+ import { Snapshot } from './snapshot';
5
+ export interface ActiveRule {
6
+ rulesId: Hex;
7
+ /** agentOf(rulesId); zero means the engine no longer resolves it. */
8
+ agent: Address;
9
+ actionIds: readonly Hex[];
10
+ mode: number;
11
+ vm: number;
12
+ chainHash: Hex;
13
+ }
14
+ /** Enabled rule IDs with their agent and governed chain, all at one block. */
15
+ export declare function readActiveRules(snap: Snapshot, gen: AgenticGeneration, wallet: Address): Promise<ActiveRule[]>;
16
+ /** Map a stored chain hash back to its CAIP-2 string from known chains. */
17
+ export declare function chainFromHash(hash: Hex, pushChainNamespace: string): string | undefined;
18
+ /**
19
+ * Agent-door rule selection for one send (spec 2.a): enabled rules whose agent
20
+ * is the signer and whose chain is the destination, read uncached at one
21
+ * block. Expiry, caps and policy remain on-chain checks.
22
+ */
23
+ export declare function selectRuleForSend(snap: Snapshot, gen: AgenticGeneration, wallet: Address, agent: Address, destinationChain: string): Promise<ActiveRule>;
24
+ /** Decode one active rule into the public record. */
25
+ export declare function decodeActiveRule(snap: Snapshot, gen: AgenticGeneration, wallet: Address, rule: ActiveRule, pushChainNamespace: string): Promise<RulesRecord>;
26
+ /** Native spend counters for the update assertion. */
27
+ export declare function readNativeSpend(snap: Snapshot, gen: AgenticGeneration, wallet: Address, rule: ActiveRule, action?: Hex): Promise<{
28
+ configId: Hex;
29
+ valueSpent: bigint;
30
+ amountSpent: bigint;
31
+ callsUsed: number;
32
+ }>;
33
+ export declare function readUniversalSpend(snap: Snapshot, gen: AgenticGeneration, wallet: Address, rule: ActiveRule): Promise<{
34
+ configId: Hex;
35
+ expectedSpent: bigint[];
36
+ }>;
37
+ export declare function listRules(snap: Snapshot, gen: AgenticGeneration, wallet: Address, pushChainNamespace: string): Promise<RulesRecord[]>;
38
+ /**
39
+ * rules.get returns enabled records only. Unknown/revoked IDs are deliberately
40
+ * indistinguishable without historical reconstruction, which is outside v1.
41
+ */
42
+ export declare function getRule(snap: Snapshot, gen: AgenticGeneration, wallet: Address, rulesId: Hex, pushChainNamespace: string): Promise<RulesRecord>;