@pushchain/core 6.0.26 → 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 (169) hide show
  1. package/AGW.md +194 -0
  2. package/CHANGELOG.md +8 -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/enums.d.ts +6 -3
  134. package/src/lib/constants/enums.js +6 -3
  135. package/src/lib/constants/index.d.ts +60 -4
  136. package/src/lib/constants/index.js +17 -3
  137. package/src/lib/constants/read-state.d.ts +9 -0
  138. package/src/lib/constants/read-state.js +13 -1
  139. package/src/lib/index.d.ts +8 -2
  140. package/src/lib/index.js +9 -1
  141. package/src/lib/orchestrator/internals/errors.d.ts +7 -0
  142. package/src/lib/orchestrator/internals/errors.js +13 -1
  143. package/src/lib/orchestrator/internals/execute-standard.d.ts +3 -1
  144. package/src/lib/orchestrator/internals/execute-standard.js +3 -3
  145. package/src/lib/orchestrator/internals/outbound-sync.js +52 -7
  146. package/src/lib/orchestrator/internals/push-chain-tx.d.ts +1 -1
  147. package/src/lib/orchestrator/internals/push-chain-tx.js +13 -1
  148. package/src/lib/orchestrator/orchestrator.d.ts +3 -0
  149. package/src/lib/orchestrator/orchestrator.js +13 -1
  150. package/src/lib/orchestrator/orchestrator.types.d.ts +8 -0
  151. package/src/lib/orchestrator/route-detector.js +3 -2
  152. package/src/lib/orchestrator/svm-idl/resolve.d.ts +2 -0
  153. package/src/lib/orchestrator/svm-idl/resolve.js +11 -1
  154. package/src/lib/progress-hook/progress-hook.js +63 -0
  155. package/src/lib/progress-hook/progress-hook.types.d.ts +10 -1
  156. package/src/lib/progress-hook/progress-hook.types.js +10 -0
  157. package/src/lib/push-chain/push-chain.d.ts +56 -15
  158. package/src/lib/push-chain/push-chain.js +66 -7
  159. package/src/lib/read-state/destination.js +6 -1
  160. package/src/lib/read-state/read-params.d.ts +3 -3
  161. package/src/lib/read-state/read-params.js +3 -2
  162. package/src/lib/read-state/read-state.types.d.ts +3 -1
  163. package/src/lib/read-state/read-tracker.js +2 -2
  164. package/src/lib/read-state/spec-builder.js +1 -1
  165. package/src/lib/universal/prc20-address.d.ts +15 -0
  166. package/src/lib/universal/prc20-address.js +135 -0
  167. package/src/lib/utils.d.ts +6 -13
  168. package/src/lib/utils.js +5 -128
  169. package/src/lib/vm-client/evm-client.js +34 -20
@@ -0,0 +1,286 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readActiveRules = readActiveRules;
4
+ exports.chainFromHash = chainFromHash;
5
+ exports.selectRuleForSend = selectRuleForSend;
6
+ exports.decodeActiveRule = decodeActiveRule;
7
+ exports.readNativeSpend = readNativeSpend;
8
+ exports.readUniversalSpend = readUniversalSpend;
9
+ exports.listRules = listRules;
10
+ exports.getRule = getRule;
11
+ const svm_1 = require("./svm");
12
+ const prc20_metadata_1 = require("../contracts/prc20-metadata");
13
+ const viem_1 = require("viem");
14
+ const enums_1 = require("../../constants/enums");
15
+ const capabilities_1 = require("../capabilities");
16
+ const ids_1 = require("../codec/ids");
17
+ const native_1 = require("../codec/native");
18
+ const defaults_1 = require("../codec/defaults");
19
+ const public_rule_1 = require("../public-rule");
20
+ const errors_1 = require("../errors");
21
+ const prc20_metadata_2 = require("../contracts/prc20-metadata");
22
+ const rules_1 = require("../codec/rules");
23
+ const errors_2 = require("../errors");
24
+ const MODE_UNIVERSAL = 0;
25
+ const MODE_NATIVE = 1;
26
+ /** Enabled rule IDs with their agent and governed chain, all at one block. */
27
+ async function readActiveRules(snap, gen, wallet) {
28
+ const ids = await snap.read(gen.addresses.sessionEngine, gen.contracts.abis.engine, 'getPermissionIDs', [wallet]);
29
+ return Promise.all(ids.map((id) => readActiveRule(snap, gen, wallet, id)));
30
+ }
31
+ async function readActiveRule(snap, gen, wallet, rulesId) {
32
+ const [agent, actionIds] = await Promise.all([
33
+ snap.read(wallet, gen.contracts.abis.wallet, 'agentOf', [rulesId]),
34
+ snap.read(gen.addresses.sessionEngine, gen.contracts.abis.engine, 'getEnabledActions', [wallet, rulesId]),
35
+ ]);
36
+ if (actionIds.length === 0) {
37
+ throw new errors_2.AgenticError(errors_2.AGENTIC_ERROR_CODE.INCONSISTENT_READ, `enabled rule ${rulesId} has no enabled actions`);
38
+ }
39
+ const modes = await Promise.all(actionIds.map((a) => snap.read(gen.addresses.rulesPolicy, gen.contracts.abis.policy, 'getMode', [(0, ids_1.configId)(wallet, rulesId, a), wallet])));
40
+ const first = modes[0];
41
+ if (!modes.every((m) => m.initialized &&
42
+ m.chainHash === first.chainHash &&
43
+ m.mode === first.mode)) {
44
+ throw new errors_2.AgenticError(errors_2.AGENTIC_ERROR_CODE.INCONSISTENT_READ, `rule ${rulesId} has uninitialized or mixed-chain policy configs`);
45
+ }
46
+ return {
47
+ rulesId,
48
+ agent: (0, viem_1.getAddress)(agent),
49
+ actionIds,
50
+ mode: Number(first.mode),
51
+ vm: Number(first.vm),
52
+ chainHash: first.chainHash,
53
+ };
54
+ }
55
+ /** Map a stored chain hash back to its CAIP-2 string from known chains. */
56
+ function chainFromHash(hash, pushChainNamespace) {
57
+ const known = [pushChainNamespace, ...Object.values(enums_1.CHAIN)];
58
+ return known.find((c) => (0, rules_1.chainHash)(c) === hash);
59
+ }
60
+ /**
61
+ * Agent-door rule selection for one send (spec 2.a): enabled rules whose agent
62
+ * is the signer and whose chain is the destination, read uncached at one
63
+ * block. Expiry, caps and policy remain on-chain checks.
64
+ */
65
+ async function selectRuleForSend(snap, gen, wallet, agent, destinationChain) {
66
+ const active = await readActiveRules(snap, gen, wallet);
67
+ const want = (0, rules_1.chainHash)(destinationChain);
68
+ const matches = active.filter((r) => r.agent === (0, viem_1.getAddress)(agent) && r.chainHash === want);
69
+ if (matches.length === 0) {
70
+ throw new errors_2.AgenticError(errors_2.AGENTIC_ERROR_CODE.NO_RULES_FOR_CHAIN, `no enabled rule on ${wallet} names ${agent} for ${destinationChain}`, {
71
+ details: {
72
+ wallet,
73
+ agent,
74
+ destinationChain,
75
+ blockNumber: snap.blockNumber,
76
+ },
77
+ });
78
+ }
79
+ if (matches.length > 1) {
80
+ throw new errors_2.AgenticError(errors_2.AGENTIC_ERROR_CODE.AMBIGUOUS_RULE, `${matches.length} enabled rules on ${wallet} name ${agent} for ${destinationChain}`, {
81
+ hint: 'Multiple enabled rules match this agent and chain. Inspect details.rulesIds and ask the owner to resolve the overlap; no transaction was submitted.',
82
+ details: { rulesIds: matches.map((m) => m.rulesId) },
83
+ });
84
+ }
85
+ return matches[0];
86
+ }
87
+ /** Decode one active rule into the public record. */
88
+ async function decodeActiveRule(snap, gen, wallet, rule, pushChainNamespace) {
89
+ if (rule.mode === MODE_UNIVERSAL) {
90
+ if (rule.vm === 1) {
91
+ (0, capabilities_1.requireCapability)(gen.capabilities, capabilities_1.AgenticCapability.UNIVERSAL_SVM_RULES);
92
+ const stored = await (0, svm_1.readSvmConfig)(snap, gen, wallet, rule, pushChainNamespace);
93
+ const cfg = stored.config;
94
+ const gas = await (0, prc20_metadata_1.readGasPrc20)(snap, stored.chainNamespace);
95
+ const assets = await Promise.all(cfg.assets.map(async (a) => ({
96
+ token: (0, viem_1.getAddress)(a.token) === gas
97
+ ? viem_1.zeroAddress
98
+ : await (0, prc20_metadata_1.readSvmSourceToken)(snap, a.token, stored.chainNamespace, gen.network),
99
+ maxPerCall: a.maxPerCall,
100
+ maxTotal: a.maxTotal,
101
+ })));
102
+ return {
103
+ rulesId: rule.rulesId,
104
+ enabled: true,
105
+ agent: rule.agent,
106
+ chainNamespace: stored.chainNamespace,
107
+ validUntil: Number(cfg.validUntil),
108
+ spent: {
109
+ kind: 'universal',
110
+ assets: assets.map((a, i) => ({
111
+ token: a.token,
112
+ spent: cfg.assets[i].spent,
113
+ })),
114
+ },
115
+ rule: {
116
+ format: 'decoded',
117
+ agent: rule.agent,
118
+ chainNamespace: stored.chainNamespace,
119
+ validUntil: Number(cfg.validUntil),
120
+ expectedCEA: cfg.expectedCEA,
121
+ gatewayProgram: cfg.gatewayProgram,
122
+ maxGasPerCall: cfg.maxGasPerCall,
123
+ assets,
124
+ ceaAccounts: cfg.ceaAccounts,
125
+ programs: cfg.programs,
126
+ pins: cfg.pins,
127
+ dataPins: cfg.dataPins,
128
+ },
129
+ };
130
+ }
131
+ if (rule.vm !== 0)
132
+ throw new errors_2.AgenticError(errors_2.AGENTIC_ERROR_CODE.RULE_READ_FAILED, 'unknown universal VM');
133
+ (0, capabilities_1.requireCapability)(gen.capabilities, capabilities_1.AgenticCapability.UNIVERSAL_EVM_RULES);
134
+ const chain = chainFromHash(rule.chainHash, pushChainNamespace);
135
+ if (!chain || !chain.startsWith('eip155:'))
136
+ throw new errors_2.AgenticError(errors_2.AGENTIC_ERROR_CODE.RULE_READ_FAILED, 'unknown EVM rule chain');
137
+ const cfg = await snap.read(gen.addresses.rulesPolicy, gen.contracts.abis.policy, 'getConfig', [(0, ids_1.configId)(wallet, rule.rulesId, rule.actionIds[0]), wallet]);
138
+ if (!cfg.initialized)
139
+ throw new errors_2.AgenticError(errors_2.AGENTIC_ERROR_CODE.INCONSISTENT_READ, 'uninitialized universal rule');
140
+ if (!cfg.assets.length ||
141
+ cfg.assets.length > 8 ||
142
+ !cfg.allowedCalls.length ||
143
+ cfg.allowedCalls.length > 32 ||
144
+ cfg.expectedCEA === viem_1.zeroAddress)
145
+ throw new errors_2.AgenticError(errors_2.AGENTIC_ERROR_CODE.INCONSISTENT_READ, 'invalid stored universal rule shape');
146
+ const tokens = await Promise.all(cfg.assets.map((a) => (0, prc20_metadata_2.readOriginToken)(snap, a.token, chain)));
147
+ return {
148
+ rulesId: rule.rulesId,
149
+ enabled: true,
150
+ chainNamespace: chain,
151
+ agent: rule.agent,
152
+ validUntil: Number(cfg.validUntil),
153
+ spent: {
154
+ kind: 'universal',
155
+ assets: tokens.map((token, i) => ({
156
+ token,
157
+ spent: cfg.assets[i].spent,
158
+ })),
159
+ },
160
+ rule: {
161
+ agent: rule.agent,
162
+ chainNamespace: chain,
163
+ validUntil: Number(cfg.validUntil),
164
+ maxGasPerCall: cfg.maxGasPerCall,
165
+ assets: cfg.assets.map((a, i) => ({
166
+ token: tokens[i],
167
+ maxPerCall: a.maxPerCall,
168
+ maxTotal: a.maxTotal,
169
+ })),
170
+ allowedCalls: cfg.allowedCalls.map((c) => ({
171
+ target: c.target,
172
+ selector: c.selector,
173
+ maxValue: c.maxValue,
174
+ pins: c.hasBeneficiary
175
+ ? [{ arg: storedArg(c.beneficiaryOffset), expected: public_rule_1.WALLET }]
176
+ : [],
177
+ })),
178
+ },
179
+ };
180
+ }
181
+ if (rule.mode !== MODE_NATIVE) {
182
+ throw new errors_2.AgenticError(errors_2.AGENTIC_ERROR_CODE.RULE_READ_FAILED, `rule ${rule.rulesId} has unknown mode ${rule.mode}`);
183
+ }
184
+ // One stored config per action, read at the same block, in engine order.
185
+ const configs = await Promise.all(rule.actionIds.map((action) => snap.read(gen.addresses.rulesPolicy, gen.contracts.abis.policy, 'getNativeConfig', [(0, ids_1.configId)(wallet, rule.rulesId, action), wallet])));
186
+ if (configs.some((c) => !c.initialized)) {
187
+ throw new errors_2.AgenticError(errors_2.AGENTIC_ERROR_CODE.INCONSISTENT_READ, `native config for ${rule.rulesId} is not initialized`);
188
+ }
189
+ const decoded = (0, native_1.nativeActionTermsToRule)(configs, rule.agent);
190
+ if (configs.some((c) => c.maxValueTotal !== defaults_1.UINT256_MAX ||
191
+ Number(c.maxCalls) !== 0 ||
192
+ (c.amount.enabled && c.amount.maxTotal !== defaults_1.UINT256_MAX) ||
193
+ Number(c.validUntil) !== decoded.validUntil)) {
194
+ throw (0, errors_1.capabilityUnavailable)('nativeRuleRead', 'stored lifetime limits or differing action expiries cannot be represented by section-6 rules without losing constraints');
195
+ }
196
+ const allowedCalls = configs.map((c) => ({
197
+ target: (0, viem_1.getAddress)(c.target),
198
+ selector: c.selector === '0xffffffff' ? 'value-only' : c.selector,
199
+ maxValue: c.maxValuePerCall,
200
+ pins: [
201
+ ...c.pins.map((p) => ({
202
+ arg: storedArg(p.offset),
203
+ expected: p.expected.toLowerCase() ===
204
+ `0x${wallet.slice(2).toLowerCase().padStart(64, '0')}`
205
+ ? public_rule_1.WALLET
206
+ : p.expected,
207
+ })),
208
+ ...(c.amount.enabled
209
+ ? [{ arg: storedArg(c.amount.offset), max: c.amount.maxPerCall }]
210
+ : []),
211
+ ],
212
+ }));
213
+ const counters = configs.map((c, i) => ({
214
+ target: allowedCalls[i].target,
215
+ selector: allowedCalls[i].selector,
216
+ valueSpent: c.valueSpent,
217
+ amountSpent: c.amountSpent,
218
+ callsUsed: Number(c.callsUsed),
219
+ }));
220
+ if (rule.agent === (0, viem_1.getAddress)(`0x${'00'.repeat(20)}`)) {
221
+ throw new errors_2.AgenticError(errors_2.AGENTIC_ERROR_CODE.INCONSISTENT_READ, `enabled rule ${rule.rulesId} does not resolve to an agent`);
222
+ }
223
+ return {
224
+ rulesId: rule.rulesId,
225
+ enabled: true,
226
+ chainNamespace: pushChainNamespace,
227
+ agent: rule.agent,
228
+ validUntil: decoded.validUntil,
229
+ rule: { agent: rule.agent, validUntil: decoded.validUntil, allowedCalls },
230
+ spent: counters.length === 1
231
+ ? {
232
+ kind: 'native',
233
+ valueSpent: counters[0].valueSpent,
234
+ amountSpent: counters[0].amountSpent,
235
+ callsUsed: counters[0].callsUsed,
236
+ }
237
+ : { kind: 'native', actions: counters },
238
+ };
239
+ }
240
+ /** Native spend counters for the update assertion. */
241
+ async function readNativeSpend(snap, gen, wallet, rule, action = rule.actionIds[0]) {
242
+ const id = (0, ids_1.configId)(wallet, rule.rulesId, action);
243
+ const cfg = await snap.read(gen.addresses.rulesPolicy, gen.contracts.abis.policy, 'getNativeConfig', [id, wallet]);
244
+ return {
245
+ configId: id,
246
+ valueSpent: cfg.valueSpent,
247
+ amountSpent: cfg.amountSpent,
248
+ callsUsed: Number(cfg.callsUsed),
249
+ };
250
+ }
251
+ async function readUniversalSpend(snap, gen, wallet, rule) {
252
+ const id = (0, ids_1.configId)(wallet, rule.rulesId, rule.actionIds[0]);
253
+ const fn = rule.vm === 1 ? 'getSvmConfig' : 'getConfig';
254
+ const cfg = await snap.read(gen.addresses.rulesPolicy, gen.contracts.abis.policy, fn, [id, wallet]);
255
+ if (!cfg.initialized)
256
+ throw new errors_2.AgenticError(errors_2.AGENTIC_ERROR_CODE.INCONSISTENT_READ, 'uninitialized universal spend config');
257
+ return { configId: id, expectedSpent: cfg.assets.map((a) => a.spent) };
258
+ }
259
+ async function listRules(snap, gen, wallet, pushChainNamespace) {
260
+ (0, capabilities_1.requireCapability)(gen.capabilities, capabilities_1.AgenticCapability.ACTIVE_RULE_READS);
261
+ const active = await readActiveRules(snap, gen, wallet);
262
+ const records = [];
263
+ for (const r of active)
264
+ records.push(await decodeActiveRule(snap, gen, wallet, r, pushChainNamespace));
265
+ return records;
266
+ }
267
+ /**
268
+ * rules.get returns enabled records only. Unknown/revoked IDs are deliberately
269
+ * indistinguishable without historical reconstruction, which is outside v1.
270
+ */
271
+ async function getRule(snap, gen, wallet, rulesId, pushChainNamespace) {
272
+ (0, capabilities_1.requireCapability)(gen.capabilities, capabilities_1.AgenticCapability.ACTIVE_RULE_READS);
273
+ const enabled = await snap.read(gen.addresses.sessionEngine, gen.contracts.abis.engine, 'isPermissionEnabled', [rulesId, wallet]);
274
+ if (enabled) {
275
+ const active = await readActiveRule(snap, gen, wallet, rulesId);
276
+ return decodeActiveRule(snap, gen, wallet, active, pushChainNamespace);
277
+ }
278
+ throw new errors_2.AgenticError(errors_2.AGENTIC_ERROR_CODE.RULE_NOT_FOUND, `rule ${rulesId} is not enabled on ${wallet} (unknown or revoked; v1 has no rule history)`);
279
+ }
280
+ /** With a raw selector, arg denotes its ABI head-word position. */
281
+ function storedArg(offset) {
282
+ if (offset < 4 || (offset - 4) % 32 !== 0)
283
+ throw (0, errors_1.capabilityUnavailable)('argumentLayout', 'stored pin is not aligned to an ABI argument word');
284
+ return (offset - 4) / 32;
285
+ }
286
+ //# sourceMappingURL=rules.js.map
@@ -0,0 +1,28 @@
1
+ import type { AbiEvent, Address, Log } from 'viem';
2
+ import type { ChainReader } from '../contracts/reader';
3
+ import { type AgenticErrorCode } from '../errors';
4
+ /**
5
+ * All reads of one logical operation are pinned to a single block so dependent
6
+ * reads (enumerate IDs → per-ID config) observe one state. An RPC failure is
7
+ * surfaced as an error; it is never treated as "empty".
8
+ */
9
+ export declare class Snapshot {
10
+ readonly reader: ChainReader;
11
+ readonly blockNumber: bigint;
12
+ private readonly failCode;
13
+ constructor(reader: ChainReader, blockNumber: bigint, failCode?: AgenticErrorCode);
14
+ static at(reader: ChainReader, failCode?: AgenticErrorCode): Promise<Snapshot>;
15
+ read<T>(address: Address, abi: readonly unknown[], functionName: string, args?: readonly unknown[]): Promise<T>;
16
+ code(address: Address): Promise<boolean>;
17
+ }
18
+ /** Max block span per eth_getLogs request. */
19
+ export declare const LOG_PAGE_BLOCKS: bigint;
20
+ /**
21
+ * Paginated log scan [fromBlock, toBlock] inclusive. A failed page fails the
22
+ * whole scan; partial results are never returned.
23
+ */
24
+ export declare function scanLogs(reader: ChainReader, args: {
25
+ address: Address;
26
+ event: AbiEvent;
27
+ args?: Record<string, unknown>;
28
+ }, fromBlock: bigint, toBlock: bigint, page?: bigint): Promise<Log[]>;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LOG_PAGE_BLOCKS = exports.Snapshot = void 0;
4
+ exports.scanLogs = scanLogs;
5
+ const errors_1 = require("../errors");
6
+ /**
7
+ * All reads of one logical operation are pinned to a single block so dependent
8
+ * reads (enumerate IDs → per-ID config) observe one state. An RPC failure is
9
+ * surfaced as an error; it is never treated as "empty".
10
+ */
11
+ class Snapshot {
12
+ reader;
13
+ blockNumber;
14
+ failCode;
15
+ constructor(reader, blockNumber, failCode = errors_1.AGENTIC_ERROR_CODE.RULE_READ_FAILED) {
16
+ this.reader = reader;
17
+ this.blockNumber = blockNumber;
18
+ this.failCode = failCode;
19
+ }
20
+ static async at(reader, failCode = errors_1.AGENTIC_ERROR_CODE.RULE_READ_FAILED) {
21
+ let block;
22
+ try {
23
+ block = await reader.getBlockNumber();
24
+ }
25
+ catch (cause) {
26
+ throw new errors_1.AgenticError(failCode, 'could not read the current Push block number', { cause });
27
+ }
28
+ return new Snapshot(reader, block, failCode);
29
+ }
30
+ async read(address, abi, functionName, args = []) {
31
+ try {
32
+ return (await this.reader.readContract({
33
+ address,
34
+ abi,
35
+ functionName,
36
+ args,
37
+ blockNumber: this.blockNumber,
38
+ }));
39
+ }
40
+ catch (cause) {
41
+ throw new errors_1.AgenticError(this.failCode, `${functionName} read on ${address} failed at block ${this.blockNumber}`, {
42
+ cause,
43
+ details: { address, functionName, blockNumber: this.blockNumber },
44
+ });
45
+ }
46
+ }
47
+ async code(address) {
48
+ try {
49
+ const code = await this.reader.getCode({
50
+ address,
51
+ blockNumber: this.blockNumber,
52
+ });
53
+ return !!code && code !== '0x';
54
+ }
55
+ catch (cause) {
56
+ throw new errors_1.AgenticError(this.failCode, `getCode(${address}) failed`, {
57
+ cause,
58
+ });
59
+ }
60
+ }
61
+ }
62
+ exports.Snapshot = Snapshot;
63
+ /** Max block span per eth_getLogs request. */
64
+ // Donut's public RPC limits eth_getLogs ranges to a distance of 1,000 blocks.
65
+ exports.LOG_PAGE_BLOCKS = BigInt(1_000);
66
+ /**
67
+ * Paginated log scan [fromBlock, toBlock] inclusive. A failed page fails the
68
+ * whole scan; partial results are never returned.
69
+ */
70
+ async function scanLogs(reader, args, fromBlock, toBlock, page = exports.LOG_PAGE_BLOCKS) {
71
+ const out = [];
72
+ if (fromBlock > toBlock)
73
+ return out;
74
+ for (let start = fromBlock; start <= toBlock; start += page) {
75
+ const end = start + page - BigInt(1) > toBlock ? toBlock : start + page - BigInt(1);
76
+ try {
77
+ out.push(...(await reader.getLogs({ ...args, fromBlock: start, toBlock: end })));
78
+ }
79
+ catch (cause) {
80
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.RULE_READ_FAILED, `log scan for ${args.event.name} failed for blocks ${start}-${end}`, { cause });
81
+ }
82
+ }
83
+ return out;
84
+ }
85
+ //# sourceMappingURL=snapshot.js.map
@@ -0,0 +1,17 @@
1
+ /** Internal lossless SVM state shared by public reads and execution. */
2
+ import type { Address, Hex } from 'viem';
3
+ import type { AgenticGeneration } from '../deployments';
4
+ import type { SvmConfigRead } from '../contracts/v5';
5
+ import { type ActiveRule } from './rules';
6
+ import type { Snapshot } from './snapshot';
7
+ export interface SvmRuleSnapshot {
8
+ wallet: Address;
9
+ rulesId: Hex;
10
+ agent: Address;
11
+ chainNamespace: `solana:${string}`;
12
+ configId: Hex;
13
+ config: SvmConfigRead;
14
+ blockNumber: bigint;
15
+ }
16
+ export declare function readSvmRule(snap: Snapshot, gen: AgenticGeneration, wallet: Address, rulesId: Hex, pushChainNamespace: string): Promise<SvmRuleSnapshot>;
17
+ export declare function readSvmConfig(snap: Snapshot, gen: AgenticGeneration, wallet: Address, active: ActiveRule, pushChainNamespace: string): Promise<SvmRuleSnapshot>;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readSvmRule = readSvmRule;
4
+ exports.readSvmConfig = readSvmConfig;
5
+ const errors_1 = require("../errors");
6
+ const ids_1 = require("../codec/ids");
7
+ const v5_1 = require("../contracts/v5");
8
+ const svm_terms_1 = require("../codec/svm-terms");
9
+ const rules_1 = require("./rules");
10
+ async function readSvmRule(snap, gen, wallet, rulesId, pushChainNamespace) {
11
+ const active = (await (0, rules_1.readActiveRules)(snap, gen, wallet)).find((r) => r.rulesId.toLowerCase() === rulesId.toLowerCase());
12
+ if (!active)
13
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.RULE_NOT_FOUND, `no enabled SVM rule ${rulesId} on ${wallet}`);
14
+ return readSvmConfig(snap, gen, wallet, active, pushChainNamespace);
15
+ }
16
+ async function readSvmConfig(snap, gen, wallet, active, pushChainNamespace) {
17
+ const chain = (0, rules_1.chainFromHash)(active.chainHash, pushChainNamespace);
18
+ const expectedAction = (0, ids_1.actionId)(gen.addresses.gateway, v5_1.SEND_OUTBOUND_SELECTOR);
19
+ if (active.mode !== 0 ||
20
+ active.vm !== 1 ||
21
+ !chain?.startsWith('solana:') ||
22
+ active.actionIds.length !== 1 ||
23
+ active.actionIds[0].toLowerCase() !== expectedAction.toLowerCase())
24
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INCONSISTENT_READ, 'rule is not a single gateway SVM action');
25
+ const id = (0, ids_1.configId)(wallet, active.rulesId, active.actionIds[0]);
26
+ const cfg = await snap.read(gen.addresses.rulesPolicy, gen.contracts.abis.policy, 'getSvmConfig', [id, wallet]);
27
+ try {
28
+ if (!cfg.initialized)
29
+ throw new Error('uninitialized SVM config');
30
+ // Stored expired rules remain readable. Grant-time validation still checks expiry.
31
+ (0, svm_terms_1.validateSvmTerms)(cfg, -1);
32
+ for (const cap of cfg.assets) {
33
+ if (typeof cap.spent !== 'bigint' ||
34
+ cap.spent < BigInt(0) ||
35
+ cap.spent > cap.maxTotal)
36
+ throw new Error('invalid stored asset counter');
37
+ }
38
+ }
39
+ catch (cause) {
40
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INCONSISTENT_READ, 'malformed stored SVM config', { cause });
41
+ }
42
+ return {
43
+ wallet,
44
+ rulesId: active.rulesId,
45
+ agent: active.agent,
46
+ chainNamespace: chain,
47
+ configId: id,
48
+ config: cfg,
49
+ blockNumber: snap.blockNumber,
50
+ };
51
+ }
52
+ //# sourceMappingURL=svm.js.map
@@ -0,0 +1,25 @@
1
+ import { type Address } from 'viem';
2
+ import type { AgenticGeneration } from '../deployments';
3
+ import type { ChainReader } from '../contracts/reader';
4
+ import type { WalletInfo, WalletSummary } from '../agentic.types';
5
+ import { Snapshot } from './snapshot';
6
+ export declare function walletCount(snap: Snapshot, gen: AgenticGeneration, owner: Address): Promise<bigint>;
7
+ /** client.agentic.derive: default index is the owner's next unused slot. */
8
+ export declare function deriveForOwner(reader: ChainReader, gen: AgenticGeneration, owner: Address, index?: number): Promise<{
9
+ address: Address;
10
+ index: number;
11
+ deployed: boolean;
12
+ }>;
13
+ export declare function rulesCount(snap: Snapshot, gen: AgenticGeneration, wallet: Address): Promise<number>;
14
+ /**
15
+ * client.agentic.list: every deployed slot plus the next undeployed slot
16
+ * (deployed=false, rulesCount=0), as in the spec example.
17
+ */
18
+ export declare function listForOwner(reader: ChainReader, gen: AgenticGeneration, owner: Address): Promise<WalletSummary[]>;
19
+ /** w.owner(): the owner as the wallet stores it. */
20
+ export declare function walletOwner(reader: ChainReader, gen: AgenticGeneration, wallet: Address): Promise<Address>;
21
+ /**
22
+ * w.info(). A deployed wallet is read from chain. An undeployed address is
23
+ * only describable when it is `signerIdentity`'s next predicted slot.
24
+ */
25
+ export declare function walletInfo(reader: ChainReader, gen: AgenticGeneration, wallet: Address, signerIdentity?: Address): Promise<WalletInfo>;
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.walletCount = walletCount;
4
+ exports.deriveForOwner = deriveForOwner;
5
+ exports.rulesCount = rulesCount;
6
+ exports.listForOwner = listForOwner;
7
+ exports.walletOwner = walletOwner;
8
+ exports.walletInfo = walletInfo;
9
+ const viem_1 = require("viem");
10
+ const ids_1 = require("../codec/ids");
11
+ const errors_1 = require("../errors");
12
+ const snapshot_1 = require("./snapshot");
13
+ /** Factory-predicted address for (owner, index), cross-checked against the pure mirror. */
14
+ async function predict(snap, gen, owner, index) {
15
+ const [address, deployed] = await snap.read(gen.addresses.factory, gen.contracts.abis.factory, 'predictWallet', [owner, index]);
16
+ const mirror = (0, ids_1.deriveWallet)({
17
+ factory: gen.addresses.factory,
18
+ walletImplementation: gen.addresses.walletImplementation,
19
+ owner,
20
+ index,
21
+ });
22
+ if ((0, viem_1.getAddress)(address) !== mirror) {
23
+ // Derivation drift between the SDK mirror and the factory (obligation 13).
24
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INCONSISTENT_READ, `factory predicted ${address} for (${owner}, ${index}) but the SDK derives ${mirror}`, { details: { generation: gen.id } });
25
+ }
26
+ return { address: (0, viem_1.getAddress)(address), deployed };
27
+ }
28
+ async function walletCount(snap, gen, owner) {
29
+ return BigInt(await snap.read(gen.addresses.factory, gen.contracts.abis.factory, 'walletCount', [owner]));
30
+ }
31
+ /** client.agentic.derive: default index is the owner's next unused slot. */
32
+ async function deriveForOwner(reader, gen, owner, index) {
33
+ const snap = await snapshot_1.Snapshot.at(reader);
34
+ const next = await walletCount(snap, gen, owner);
35
+ let target = next;
36
+ if (index !== undefined) {
37
+ if (!Number.isInteger(index) || index < 0) {
38
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'index must be a non-negative integer');
39
+ }
40
+ target = BigInt(index);
41
+ if (target > next) {
42
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, `index ${index} is beyond the owner's next unused index ${next}; the factory only predicts up to the next slot`);
43
+ }
44
+ }
45
+ const p = await predict(snap, gen, owner, target);
46
+ return { address: p.address, index: Number(target), deployed: p.deployed };
47
+ }
48
+ async function rulesCount(snap, gen, wallet) {
49
+ const ids = await snap.read(gen.addresses.sessionEngine, gen.contracts.abis.engine, 'getPermissionIDs', [wallet]);
50
+ return ids.length;
51
+ }
52
+ /**
53
+ * client.agentic.list: every deployed slot plus the next undeployed slot
54
+ * (deployed=false, rulesCount=0), as in the spec example.
55
+ */
56
+ async function listForOwner(reader, gen, owner) {
57
+ const snap = await snapshot_1.Snapshot.at(reader);
58
+ const count = await walletCount(snap, gen, owner);
59
+ const out = [];
60
+ for (let i = BigInt(0); i <= count; i++) {
61
+ const p = await predict(snap, gen, owner, i);
62
+ const deployed = i < count;
63
+ if (deployed !== p.deployed) {
64
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INCONSISTENT_READ, `factory reports slot ${i} deployed=${p.deployed} but walletCount is ${count}`);
65
+ }
66
+ const label = deployed
67
+ ? await snap.read(p.address, gen.contracts.abis.wallet, 'label')
68
+ : '';
69
+ out.push({
70
+ address: p.address,
71
+ index: Number(i),
72
+ label,
73
+ deployed,
74
+ rulesCount: deployed ? await rulesCount(snap, gen, p.address) : 0,
75
+ });
76
+ }
77
+ return out;
78
+ }
79
+ /** w.owner(): the owner as the wallet stores it. */
80
+ async function walletOwner(reader, gen, wallet) {
81
+ const snap = await snapshot_1.Snapshot.at(reader);
82
+ return (0, viem_1.getAddress)(await snap.read(wallet, gen.contracts.abis.wallet, 'owner'));
83
+ }
84
+ /**
85
+ * w.info(). A deployed wallet is read from chain. An undeployed address is
86
+ * only describable when it is `signerIdentity`'s next predicted slot.
87
+ */
88
+ async function walletInfo(reader, gen, wallet, signerIdentity) {
89
+ const snap = await snapshot_1.Snapshot.at(reader);
90
+ const deployed = await snap.code(wallet);
91
+ if (!deployed) {
92
+ if (signerIdentity) {
93
+ const next = await walletCount(snap, gen, signerIdentity);
94
+ const p = await predict(snap, gen, signerIdentity, next);
95
+ if (p.address === (0, viem_1.getAddress)(wallet)) {
96
+ return {
97
+ address: p.address,
98
+ label: '',
99
+ index: Number(next),
100
+ owner: (0, viem_1.getAddress)(signerIdentity),
101
+ deployed: false,
102
+ rulesCount: 0,
103
+ };
104
+ }
105
+ }
106
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.WALLET_NOT_DEPLOYED, `${wallet} is not deployed and is not the connected owner's next wallet slot`);
107
+ }
108
+ const [owner, index] = await Promise.all([
109
+ snap.read(wallet, gen.contracts.abis.wallet, 'owner'),
110
+ snap.read(gen.addresses.factory, gen.contracts.abis.factory, 'indexOf', [wallet]),
111
+ ]);
112
+ const label = await snap.read(wallet, gen.contracts.abis.wallet, 'label');
113
+ return {
114
+ address: (0, viem_1.getAddress)(wallet),
115
+ label,
116
+ index: Number(index),
117
+ owner: (0, viem_1.getAddress)(owner),
118
+ deployed: true,
119
+ rulesCount: await rulesCount(snap, gen, wallet),
120
+ };
121
+ }
122
+ //# sourceMappingURL=wallets.js.map
@@ -0,0 +1,41 @@
1
+ import { type Address, type Hex } from 'viem';
2
+ import { CHAIN } from '../constants/enums';
3
+ import type { TransactionRouteType, UniversalTxResponse } from '../orchestrator/orchestrator.types';
4
+ import type { AgenticRuntime } from './runtime';
5
+ import type { AgenticTxMetadata } from './agentic.types';
6
+ export interface LogicalCall {
7
+ to: string;
8
+ data: string;
9
+ value: bigint;
10
+ }
11
+ /**
12
+ * Present an AGW send as the wallet's own transaction: `from` is the AGW and
13
+ * `to`/`data`/`value` are the logical call. The signer's `origin`, hashes,
14
+ * nonce, gas and the on-chain `raw` fields are kept untouched; the wrapped
15
+ * call the signer actually sent is recorded in `agentic.rawTo/rawData`.
16
+ * Mutates in place so `wait()` (which reads these fields) reports the same
17
+ * identity on the receipt.
18
+ */
19
+ export declare function adaptAgenticResponse(resp: UniversalTxResponse, meta: Omit<AgenticTxMetadata, 'rawTo' | 'rawData'> & {
20
+ logical: LogicalCall;
21
+ route?: TransactionRouteType;
22
+ chain?: CHAIN;
23
+ }): UniversalTxResponse;
24
+ /**
25
+ * trackTransaction replay: if the tracked Push tx called execute/executeAsAgent
26
+ * on a wallet of a registered, verified generation, restore the same logical
27
+ * identity a live send reports. Anything unrecognised is returned unchanged.
28
+ */
29
+ export declare function adaptTrackedResponse(runtime: Pick<AgenticRuntime, 'reader' | 'network'>, resp: UniversalTxResponse): Promise<UniversalTxResponse>;
30
+ /**
31
+ * Canonical outbound summary for both send and replay: the first actual
32
+ * destination call. The complete ordered calls live in agentic.destinationCalls.
33
+ * Identical calldata can come from explicit calls or generated transfers, so
34
+ * never infer the original input shape from a selector. For an unrecognized
35
+ * historical payload, retain the actual gateway call instead of inventing one.
36
+ */
37
+ export declare function outboundResponseCall(calls: readonly {
38
+ to: Address;
39
+ value: bigint;
40
+ data: Hex;
41
+ }[], fallback: LogicalCall): LogicalCall;