@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,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prepareWalletRules = prepareWalletRules;
4
+ const public_rule_1 = require("../public-rule");
5
+ const svm_public_1 = require("./svm-public");
6
+ const viem_1 = require("viem");
7
+ const enums_1 = require("../../constants/enums");
8
+ const chain_1 = require("../../constants/chain");
9
+ const defaults_1 = require("../codec/defaults");
10
+ const rules_1 = require("../codec/rules");
11
+ const universal_1 = require("../codec/universal");
12
+ const deployments_1 = require("../deployments");
13
+ const capabilities_1 = require("../capabilities");
14
+ const errors_1 = require("../errors");
15
+ const prc20_metadata_1 = require("../contracts/prc20-metadata");
16
+ /** Resolve wallet-dependent wire inputs without signing or mutating authority. */
17
+ async function prepareWalletRules(runtime, gen, owner, wallet, inputs, snap) {
18
+ const rules = await Promise.all(inputs.map((rule) => (0, public_rule_1.isPublicRule)(rule) ? (0, public_rule_1.normalizePublicRule)(rule, wallet, runtime) : rule));
19
+ if (rules.length)
20
+ await (0, deployments_1.verifyPolicyVersion)(runtime.reader, gen);
21
+ const svm = new Map();
22
+ const universal = new Map();
23
+ for (const rule of rules) {
24
+ if (rule && (0, rules_1.isSolanaRule)(rule)) {
25
+ (0, capabilities_1.requireCapability)(gen.capabilities, capabilities_1.AgenticCapability.UNIVERSAL_SVM_RULES);
26
+ svm.set(rule, await (0, svm_public_1.preparePublicSvmRule)(runtime, snap, wallet, rule));
27
+ continue;
28
+ }
29
+ if (!(0, rules_1.isUniversalRule)(rule))
30
+ continue;
31
+ if (rule.chainNamespace.startsWith('solana:'))
32
+ (0, capabilities_1.requireCapability)(gen.capabilities, capabilities_1.AgenticCapability.UNIVERSAL_SVM_RULES);
33
+ (0, universal_1.validateUniversalRuleShape)(rule, runtime.nowSeconds());
34
+ (0, capabilities_1.requireCapability)(gen.capabilities, rule.chainNamespace.startsWith('solana:')
35
+ ? capabilities_1.AgenticCapability.UNIVERSAL_SVM_RULES
36
+ : capabilities_1.AgenticCapability.UNIVERSAL_EVM_RULES);
37
+ const chain = rule.chainNamespace;
38
+ if (chain_1.CHAIN_INFO[chain]?.vm !== enums_1.VM.EVM)
39
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, `unsupported EVM rule chain ${chain}`);
40
+ const { cea } = await runtime.resolveCEA(wallet, chain);
41
+ const assets = rule.assets.length
42
+ ? rule.assets.map((a) => ({
43
+ token: typeof a.token === 'string' && (0, viem_1.getAddress)(a.token) === viem_1.zeroAddress
44
+ ? undefined
45
+ : a.token,
46
+ maxPerCall: a.maxPerCall,
47
+ maxTotal: (0, defaults_1.withDefault)(a.maxTotal, defaults_1.AGENTIC_DEFAULTS.universal.assetMaxTotal),
48
+ }))
49
+ : [{ token: undefined, maxPerCall: BigInt(0), maxTotal: BigInt(0) }];
50
+ const resolved = [];
51
+ for (const asset of assets) {
52
+ const token = asset.token === undefined
53
+ ? await (0, prc20_metadata_1.readGasPrc20)(snap, chain)
54
+ : runtime.resolvePrc20(asset.token, chain);
55
+ if (token === viem_1.zeroAddress)
56
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, `no gas PRC20 registered for ${chain}`);
57
+ const namespace = await snap.read(token, prc20_metadata_1.PRC20_SOURCE_ABI, 'SOURCE_CHAIN_NAMESPACE');
58
+ if (namespace !== chain)
59
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.ASSET_CHAIN_MISMATCH, `PRC20 ${token} belongs to ${namespace}, not ${chain}`);
60
+ if (asset.token !== undefined) {
61
+ const expected = typeof asset.token === 'string'
62
+ ? (0, viem_1.getAddress)(asset.token)
63
+ : (0, viem_1.getAddress)(asset.token.address);
64
+ if (expected === viem_1.zeroAddress) {
65
+ if ((await (0, prc20_metadata_1.readGasPrc20)(snap, chain)) !== (0, viem_1.getAddress)(token))
66
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'resolved token is not the registered native gas token');
67
+ }
68
+ else {
69
+ const source = await (0, prc20_metadata_1.readOriginToken)(snap, token, chain);
70
+ if (source !== expected)
71
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, `PRC20 ${token} represents ${source}, not requested token ${expected}`);
72
+ }
73
+ }
74
+ resolved.push({
75
+ token: (0, viem_1.getAddress)(token),
76
+ maxPerCall: asset.maxPerCall,
77
+ maxTotal: asset.maxTotal,
78
+ });
79
+ }
80
+ universal.set(rule, { expectedCEA: (0, viem_1.getAddress)(cea), assets: resolved });
81
+ }
82
+ return (0, rules_1.prepareRules)(rules, {
83
+ owner,
84
+ pushChainNamespace: runtime.pushChainNamespace,
85
+ validator: gen.addresses.sessionValidator,
86
+ rulesPolicy: gen.addresses.rulesPolicy,
87
+ gateway: gen.addresses.gateway,
88
+ capabilities: gen.capabilities,
89
+ nowSeconds: runtime.nowSeconds(),
90
+ forbiddenTargets: (0, deployments_1.forbiddenTargets)(gen, wallet),
91
+ universal,
92
+ svm,
93
+ });
94
+ }
95
+ //# sourceMappingURL=prepare.js.map
@@ -0,0 +1,42 @@
1
+ import type { Address } from 'viem';
2
+ import type { UniversalTxResponse } from '../../orchestrator/orchestrator.types';
3
+ import { type AgenticGeneration } from '../deployments';
4
+ import type { AgenticRuntime } from '../runtime';
5
+ import type { AgenticHex, AgenticProgressHook, InternalRule } from '../agentic.types';
6
+ /** w.rules.add(rules): owner grants on an existing wallet, IDs in input order. */
7
+ export declare function addRules(runtime: AgenticRuntime, gen: AgenticGeneration, wallet: Address, rules: readonly InternalRule[], hook?: AgenticProgressHook): Promise<{
8
+ rulesIds: AgenticHex[];
9
+ tx: UniversalTxResponse;
10
+ }>;
11
+ /** Bare revoke (no IDs, empty list, or anything but { all: true }) is refused. */
12
+ export declare function assertRevokeTarget(target: unknown): {
13
+ all: boolean;
14
+ ids?: AgenticHex[];
15
+ };
16
+ /** w.rules.delete(ids) / w.rules.delete({ all: true }). Bare revoke is refused. */
17
+ export declare function revokeRules(runtime: AgenticRuntime, gen: AgenticGeneration, wallet: Address, target: AgenticHex[] | {
18
+ all: true;
19
+ progressHook?: AgenticProgressHook;
20
+ } | undefined, hook?: AgenticProgressHook): Promise<UniversalTxResponse>;
21
+ /**
22
+ * w.rules.update({ rules: [{ rulesId, rule }] }): for every pair, inside ONE
23
+ * owner `execute` batch: assertSpent(expected) → revokeRules(old) →
24
+ * grantRules(new). Never falls back to sequential sends. An agent spending
25
+ * between the read and inclusion makes the whole batch revert; retry needs a
26
+ * fresh read and a new owner signature. New counters start at zero.
27
+ */
28
+ export declare function updateRules(runtime: AgenticRuntime, gen: AgenticGeneration, wallet: Address, params: {
29
+ rules: {
30
+ rulesId: AgenticHex;
31
+ rule: InternalRule;
32
+ }[];
33
+ progressHook?: AgenticProgressHook;
34
+ }): Promise<{
35
+ rules: {
36
+ rulesId: AgenticHex;
37
+ replaced: AgenticHex;
38
+ }[];
39
+ tx: UniversalTxResponse;
40
+ }>;
41
+ /** Direct owner rename; empty resets the default label without a checkpoint. */
42
+ export declare function setLabel(runtime: AgenticRuntime, gen: AgenticGeneration, wallet: Address, label: string, hook?: AgenticProgressHook): Promise<UniversalTxResponse>;
@@ -0,0 +1,279 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addRules = addRules;
4
+ exports.assertRevokeTarget = assertRevokeTarget;
5
+ exports.revokeRules = revokeRules;
6
+ exports.updateRules = updateRules;
7
+ exports.setLabel = setLabel;
8
+ const duplicates_1 = require("./duplicates");
9
+ const progress_hook_types_1 = require("../../progress-hook/progress-hook.types");
10
+ const capabilities_1 = require("../capabilities");
11
+ const ids_1 = require("../codec/ids");
12
+ const prepare_1 = require("./prepare");
13
+ const label_1 = require("./label");
14
+ const errors_1 = require("../errors");
15
+ const rules_1 = require("../reads/rules");
16
+ const snapshot_1 = require("../reads/snapshot");
17
+ const common_1 = require("./common");
18
+ const MODE_NATIVE = 1;
19
+ async function finish(runtime, hook, operation, run) {
20
+ try {
21
+ const result = await run();
22
+ (0, common_1.emitAgentic)(runtime, hook, progress_hook_types_1.PROGRESS_HOOK.AGENTIC_TX_199_01, operation, result.tx.hash);
23
+ return result;
24
+ }
25
+ catch (err) {
26
+ const wrapped = (0, common_1.wrapSendError)(err);
27
+ (0, common_1.emitAgentic)(runtime, hook, progress_hook_types_1.PROGRESS_HOOK.AGENTIC_TX_199_02, operation, wrapped?.code ?? 'UNKNOWN', wrapped?.message ?? String(err), wrapped?.decodedError);
28
+ throw wrapped;
29
+ }
30
+ }
31
+ /** w.rules.add(rules): owner grants on an existing wallet, IDs in input order. */
32
+ async function addRules(runtime, gen, wallet, rules, hook) {
33
+ (0, common_1.assertCanSign)(runtime, 'rules.add');
34
+ if (!Array.isArray(rules) || rules.length === 0) {
35
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'rules.add needs at least one rule');
36
+ }
37
+ const snap = await snapshot_1.Snapshot.at(runtime.reader);
38
+ const owner = await (0, common_1.assertOwner)(runtime, gen, wallet, snap);
39
+ const prepared = await (0, prepare_1.prepareWalletRules)(runtime, gen, owner, wallet, rules, snap);
40
+ (0, duplicates_1.assertNoDuplicateRules)(prepared, await (0, rules_1.readActiveRules)(snap, gen, wallet));
41
+ const nonce = BigInt(await snap.read(wallet, gen.contracts.abis.wallet, 'grantNonce'));
42
+ const predicted = prepared.map((p, i) => (0, ids_1.rulesId)({
43
+ validator: gen.addresses.sessionValidator,
44
+ agent: p.agent,
45
+ grantNonce: nonce + BigInt(i),
46
+ }));
47
+ // One rule: a direct owner grant (one RULES_GRANTED tick). Several: one
48
+ // owner `execute` batch of self-calls, so the grants are atomic and need a
49
+ // single signature on every signer type (each grant then also ticks one
50
+ // OWNER_ACTION before its RULES_GRANTED).
51
+ const grants = prepared.map((p) => gen.contracts.encodeGrantRules(p.session));
52
+ const calls = grants.length === 1
53
+ ? [{ to: wallet, value: BigInt(0), data: grants[0] }]
54
+ : [
55
+ {
56
+ to: wallet,
57
+ value: BigInt(0),
58
+ data: gen.contracts.encodeExecute(grants.map((data) => ({ target: wallet, value: BigInt(0), data }))),
59
+ },
60
+ ];
61
+ return finish(runtime, hook, 'rules.add', async () => {
62
+ const tx = await (0, common_1.sendOwnerCalls)(runtime, calls, hook);
63
+ const { logs } = await (0, common_1.confirmedLogs)(runtime, tx);
64
+ const granted = gen.contracts
65
+ .parseRulesGranted(logs, wallet)
66
+ .map((g) => g.rulesId);
67
+ if (!sameIds(granted, predicted)) {
68
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.RECEIPT_MISMATCH, 'RulesGranted events do not match the requested rules in order (a concurrent grant may have raced)', { details: { granted, predicted, txHash: tx.hash } });
69
+ }
70
+ (0, common_1.emitAgentic)(runtime, hook, progress_hook_types_1.PROGRESS_HOOK.AGENTIC_TX_104, granted);
71
+ return { rulesIds: granted, tx };
72
+ });
73
+ }
74
+ /** Bare revoke (no IDs, empty list, or anything but { all: true }) is refused. */
75
+ function assertRevokeTarget(target) {
76
+ const all = !!target &&
77
+ !Array.isArray(target) &&
78
+ target.all === true;
79
+ const ids = Array.isArray(target) ? target : undefined;
80
+ if (!all && (!ids || ids.length === 0)) {
81
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.DELETE_NEEDS_TARGET, 'rules.delete needs explicit rule IDs or { all: true }', {
82
+ hint: 'A bare revoke that wipes every rule is too easy to make by mistake.',
83
+ });
84
+ }
85
+ return { all, ids };
86
+ }
87
+ /** w.rules.delete(ids) / w.rules.delete({ all: true }). Bare revoke is refused. */
88
+ async function revokeRules(runtime, gen, wallet, target, hook) {
89
+ const { all, ids } = assertRevokeTarget(target);
90
+ if (all)
91
+ hook =
92
+ target.progressHook ?? hook;
93
+ (0, common_1.assertCanSign)(runtime, 'rules.delete');
94
+ const snap = await snapshot_1.Snapshot.at(runtime.reader);
95
+ await (0, common_1.assertOwner)(runtime, gen, wallet, snap);
96
+ let calls;
97
+ if (all) {
98
+ calls = [
99
+ {
100
+ to: wallet,
101
+ value: BigInt(0),
102
+ data: gen.contracts.encodeRevokeAllRules(),
103
+ },
104
+ ];
105
+ }
106
+ else {
107
+ const unique = new Set(ids.map((i) => i.toLowerCase()));
108
+ if (unique.size !== ids.length) {
109
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'rules.delete got the same rule ID twice');
110
+ }
111
+ for (const id of ids) {
112
+ const enabled = await snap.read(gen.addresses.sessionEngine, gen.contracts.abis.engine, 'isPermissionEnabled', [id, wallet]);
113
+ if (!enabled) {
114
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.RULE_NOT_FOUND, `rule ${id} is not enabled on ${wallet}`);
115
+ }
116
+ }
117
+ // Several IDs go through one owner `execute` batch so revocation is atomic
118
+ // regardless of signer batching support.
119
+ calls =
120
+ ids.length === 1
121
+ ? [
122
+ {
123
+ to: wallet,
124
+ value: BigInt(0),
125
+ data: gen.contracts.encodeRevokeRules(ids[0]),
126
+ },
127
+ ]
128
+ : [
129
+ {
130
+ to: wallet,
131
+ value: BigInt(0),
132
+ data: gen.contracts.encodeExecute(ids.map((id) => ({
133
+ target: wallet,
134
+ value: BigInt(0),
135
+ data: gen.contracts.encodeRevokeRules(id),
136
+ }))),
137
+ },
138
+ ];
139
+ }
140
+ const result = await finish(runtime, hook, 'rules.delete', async () => {
141
+ const tx = await (0, common_1.sendOwnerCalls)(runtime, calls, hook);
142
+ const { logs } = await (0, common_1.confirmedLogs)(runtime, tx);
143
+ const revoked = gen.contracts
144
+ .parseRulesRevoked(logs, wallet)
145
+ .map((r) => r.rulesId);
146
+ (0, common_1.emitAgentic)(runtime, hook, progress_hook_types_1.PROGRESS_HOOK.AGENTIC_TX_105, all ? { all: true } : { rulesIds: revoked });
147
+ return { tx };
148
+ });
149
+ return result.tx;
150
+ }
151
+ /**
152
+ * w.rules.update({ rules: [{ rulesId, rule }] }): for every pair, inside ONE
153
+ * owner `execute` batch: assertSpent(expected) → revokeRules(old) →
154
+ * grantRules(new). Never falls back to sequential sends. An agent spending
155
+ * between the read and inclusion makes the whole batch revert; retry needs a
156
+ * fresh read and a new owner signature. New counters start at zero.
157
+ */
158
+ async function updateRules(runtime, gen, wallet, params) {
159
+ const hook = params?.progressHook;
160
+ (0, common_1.assertCanSign)(runtime, 'rules.update');
161
+ if (!params || !Array.isArray(params.rules) || params.rules.length === 0) {
162
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'rules.update needs at least one { rulesId, rule } pair');
163
+ }
164
+ (0, capabilities_1.requireCapability)(gen.capabilities, capabilities_1.AgenticCapability.OWNER_EXECUTE);
165
+ const oldIds = params.rules.map((p) => p.rulesId.toLowerCase());
166
+ if (new Set(oldIds).size !== oldIds.length) {
167
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'rules.update names the same old rule twice');
168
+ }
169
+ const snap = await snapshot_1.Snapshot.at(runtime.reader);
170
+ const owner = await (0, common_1.assertOwner)(runtime, gen, wallet, snap);
171
+ const active = await (0, rules_1.readActiveRules)(snap, gen, wallet);
172
+ const byId = new Map(active.map((r) => [r.rulesId.toLowerCase(), r]));
173
+ const assertions = [];
174
+ for (const pair of params.rules) {
175
+ const old = byId.get(pair.rulesId.toLowerCase());
176
+ if (!old)
177
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.RULE_NOT_FOUND, `rule ${pair.rulesId} is not enabled on ${wallet}`);
178
+ if (old.mode === MODE_NATIVE) {
179
+ (0, capabilities_1.requireCapability)(gen.capabilities, capabilities_1.AgenticCapability.ASSERT_SPENT_NATIVE);
180
+ const guards = [];
181
+ for (const action of old.actionIds) {
182
+ const spend = await (0, rules_1.readNativeSpend)(snap, gen, wallet, old, action);
183
+ guards.push({
184
+ configId: spend.configId,
185
+ data: gen.contracts.encodeAssertSpentNative(spend.configId, wallet, spend),
186
+ });
187
+ }
188
+ assertions.push(guards);
189
+ }
190
+ else {
191
+ (0, capabilities_1.requireCapability)(gen.capabilities, capabilities_1.AgenticCapability.ASSERT_SPENT_UNIVERSAL_MULTI);
192
+ const spend = await (0, rules_1.readUniversalSpend)(snap, gen, wallet, old);
193
+ assertions.push([
194
+ {
195
+ configId: spend.configId,
196
+ data: gen.contracts.encodeAssertSpentUniversal(spend.configId, wallet, spend.expectedSpent),
197
+ },
198
+ ]);
199
+ }
200
+ }
201
+ const prepared = await (0, prepare_1.prepareWalletRules)(runtime, gen, owner, wallet, params.rules.map((p) => p.rule), snap);
202
+ const nonce = BigInt(await snap.read(wallet, gen.contracts.abis.wallet, 'grantNonce'));
203
+ const predicted = prepared.map((p, i) => (0, ids_1.rulesId)({
204
+ validator: gen.addresses.sessionValidator,
205
+ agent: p.agent,
206
+ grantNonce: nonce + BigInt(i),
207
+ }));
208
+ const batch = params.rules.flatMap((pair, i) => [
209
+ ...assertions[i].map((a) => ({
210
+ target: gen.addresses.rulesPolicy,
211
+ value: BigInt(0),
212
+ data: a.data,
213
+ })),
214
+ {
215
+ target: wallet,
216
+ value: BigInt(0),
217
+ data: gen.contracts.encodeRevokeRules(pair.rulesId),
218
+ },
219
+ {
220
+ target: wallet,
221
+ value: BigInt(0),
222
+ data: gen.contracts.encodeGrantRules(prepared[i].session),
223
+ },
224
+ ]);
225
+ const call = {
226
+ to: wallet,
227
+ value: BigInt(0),
228
+ data: gen.contracts.encodeExecute(batch),
229
+ };
230
+ return finish(runtime, hook, 'rules.update', async () => {
231
+ const tx = await (0, common_1.sendOwnerCalls)(runtime, [call], hook);
232
+ const { logs } = await (0, common_1.confirmedLogs)(runtime, tx);
233
+ const granted = gen.contracts
234
+ .parseRulesGranted(logs, wallet)
235
+ .map((g) => g.rulesId);
236
+ const revoked = gen.contracts
237
+ .parseRulesRevoked(logs, wallet)
238
+ .map((r) => r.rulesId.toLowerCase());
239
+ if (!sameIds(granted, predicted) ||
240
+ !sameIds(revoked, oldIds)) {
241
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.RECEIPT_MISMATCH, 'rules.update receipt does not show the expected revoke/grant pairs', { details: { granted, predicted, revoked, txHash: tx.hash } });
242
+ }
243
+ (0, common_1.emitAgentic)(runtime, hook, progress_hook_types_1.PROGRESS_HOOK.AGENTIC_TX_105, {
244
+ rulesIds: params.rules.map((p) => p.rulesId),
245
+ });
246
+ (0, common_1.emitAgentic)(runtime, hook, progress_hook_types_1.PROGRESS_HOOK.AGENTIC_TX_104, granted);
247
+ return {
248
+ tx,
249
+ rules: granted.map((id, i) => ({
250
+ rulesId: id,
251
+ replaced: params.rules[i].rulesId,
252
+ })),
253
+ };
254
+ });
255
+ }
256
+ function sameIds(a, b) {
257
+ return (a.length === b.length &&
258
+ a.every((x, i) => x.toLowerCase() === b[i].toLowerCase()));
259
+ }
260
+ /** Direct owner rename; empty resets the default label without a checkpoint. */
261
+ async function setLabel(runtime, gen, wallet, label, hook) {
262
+ (0, label_1.assertLabel)(label);
263
+ (0, common_1.assertCanSign)(runtime, 'setLabel');
264
+ (0, capabilities_1.requireCapability)(gen.capabilities, capabilities_1.AgenticCapability.SET_LABEL);
265
+ return finish(runtime, hook, 'setLabel', async () => {
266
+ await (0, common_1.assertOwner)(runtime, gen, wallet, await snapshot_1.Snapshot.at(runtime.reader));
267
+ const tx = await (0, common_1.sendOwnerCalls)(runtime, [
268
+ {
269
+ to: wallet,
270
+ value: BigInt(0),
271
+ data: gen.contracts.encodeSetLabel(label),
272
+ },
273
+ ], hook);
274
+ await (0, common_1.confirmedLogs)(runtime, tx);
275
+ (0, common_1.emitAgentic)(runtime, hook, progress_hook_types_1.PROGRESS_HOOK.AGENTIC_TX_106, label);
276
+ return { tx };
277
+ }).then(({ tx }) => tx);
278
+ }
279
+ //# sourceMappingURL=rules-write.js.map
@@ -0,0 +1,25 @@
1
+ /** Internal resolved context; no public Rule schema or cluster assumptions. */
2
+ import { type Address, type Hex } from 'viem';
3
+ import { PUSH_NETWORK } from '../../constants/enums';
4
+ import { type SvmTokenAccountInput, type ResolvedSvmAsset } from '../codec/svm-accounts';
5
+ import type { AssetCapWire } from '../codec/universal-terms';
6
+ import type { Snapshot } from '../reads/snapshot';
7
+ export interface SvmMetadataProvider {
8
+ /** Must identify the requested full CAIP-2 cluster; no global gateway default. */
9
+ gateway(chainNamespace: `solana:${string}`): Promise<{
10
+ chainNamespace: string;
11
+ program: string;
12
+ }>;
13
+ /** RPC-backed mint owner (legacy SPL or Token-2022), not an assumed default. */
14
+ mint(chainNamespace: `solana:${string}`, address: Hex): Promise<{
15
+ mint: string;
16
+ tokenProgram: string;
17
+ }>;
18
+ }
19
+ export declare function resolveSvmAssets(snap: Snapshot, chain: `solana:${string}`, caps: readonly AssetCapWire[], metadata: SvmMetadataProvider, network?: PUSH_NETWORK): Promise<ResolvedSvmAsset[]>;
20
+ export declare function resolveSvmContext(snap: Snapshot, wallet: Address, chain: `solana:${string}`, caps: readonly AssetCapWire[], metadata: SvmMetadataProvider, outputs?: readonly SvmTokenAccountInput[], network?: PUSH_NETWORK): Promise<{
21
+ assets: AssetCapWire[];
22
+ expectedCEA: Hex;
23
+ gatewayProgram: Hex;
24
+ ceaAccounts: Hex[];
25
+ }>;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveSvmAssets = resolveSvmAssets;
4
+ exports.resolveSvmContext = resolveSvmContext;
5
+ /** Internal resolved context; no public Rule schema or cluster assumptions. */
6
+ const viem_1 = require("viem");
7
+ const enums_1 = require("../../constants/enums");
8
+ const errors_1 = require("../errors");
9
+ const prc20_metadata_1 = require("../contracts/prc20-metadata");
10
+ const svm_accounts_1 = require("../codec/svm-accounts");
11
+ const ZERO_KEY = `0x${'00'.repeat(32)}`;
12
+ async function resolveSvmAssets(snap, chain, caps, metadata, network = enums_1.PUSH_NETWORK.TESTNET_DONUT) {
13
+ const gas = await (0, prc20_metadata_1.readGasPrc20)(snap, chain);
14
+ const out = [];
15
+ for (const cap of caps) {
16
+ const token = (0, viem_1.getAddress)(cap.token);
17
+ const namespace = await snap.read(token, prc20_metadata_1.PRC20_SOURCE_ABI, 'SOURCE_CHAIN_NAMESPACE');
18
+ if (namespace !== chain)
19
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.ASSET_CHAIN_MISMATCH, `PRC20 ${token} belongs to ${namespace}, not ${chain}`);
20
+ const source = await (0, prc20_metadata_1.readSvmSourceToken)(snap, token, chain, network);
21
+ const native = source === ZERO_KEY;
22
+ if (native) {
23
+ if (token !== gas)
24
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'native marker is not the registered gas PRC20');
25
+ out.push({ kind: 'native', cap: { ...cap, token } });
26
+ }
27
+ else {
28
+ const key = (0, svm_accounts_1.svmKey)(source);
29
+ const mint = await metadata.mint(chain, key);
30
+ if ((0, svm_accounts_1.svmKey)(mint.mint) !== key)
31
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'mint resolver returned a different mint');
32
+ out.push({ kind: 'spl', cap: { ...cap, token }, ...mint });
33
+ }
34
+ }
35
+ return out;
36
+ }
37
+ async function resolveSvmContext(snap, wallet, chain, caps, metadata, outputs = [], network = enums_1.PUSH_NETWORK.TESTNET_DONUT) {
38
+ const gateway = await metadata.gateway(chain);
39
+ if (gateway.chainNamespace !== chain)
40
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'gateway registry cluster mismatch');
41
+ const assets = await resolveSvmAssets(snap, chain, caps, metadata, network);
42
+ const resolvedOutputs = [];
43
+ for (const output of outputs) {
44
+ const key = (0, svm_accounts_1.svmKey)(output.mint);
45
+ const resolved = await metadata.mint(chain, key);
46
+ if ((0, svm_accounts_1.svmKey)(resolved.mint) !== key ||
47
+ (output.tokenProgram &&
48
+ (0, svm_accounts_1.svmKey)(output.tokenProgram) !== (0, svm_accounts_1.svmKey)(resolved.tokenProgram)))
49
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'output mint/token-program mismatch');
50
+ resolvedOutputs.push(resolved);
51
+ }
52
+ // Derivation validates token programs and covers every mint/output.
53
+ return (0, svm_accounts_1.resolveAgwSvmRuleContext)(wallet, gateway.program, assets, resolvedOutputs);
54
+ }
55
+ //# sourceMappingURL=svm-context.js.map
@@ -0,0 +1,10 @@
1
+ import { Connection } from '@solana/web3.js';
2
+ import type { SvmMetadataProvider } from './svm-context';
3
+ export interface SvmClusterConfig {
4
+ gatewayProgram: string;
5
+ rpcUrls: readonly string[];
6
+ }
7
+ type MintRpc = Pick<Connection, 'getGenesisHash' | 'getAccountInfo'>;
8
+ /** Explicit cluster configuration; HTTP reads only, no signing/subscriptions. */
9
+ export declare function createSvmMetadataProvider(clusters: Readonly<Partial<Record<`solana:${string}`, SvmClusterConfig>>>, connect?: (url: string) => MintRpc): SvmMetadataProvider;
10
+ export {};
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSvmMetadataProvider = createSvmMetadataProvider;
4
+ const web3_js_1 = require("@solana/web3.js");
5
+ const svm_accounts_1 = require("../codec/svm-accounts");
6
+ const svm_terms_1 = require("../codec/svm-terms");
7
+ const errors_1 = require("../errors");
8
+ const SPL = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
9
+ const TOKEN_2022 = 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb';
10
+ /** Explicit cluster configuration; HTTP reads only, no signing/subscriptions. */
11
+ function createSvmMetadataProvider(clusters, connect = (url) => new web3_js_1.Connection(url, 'confirmed')) {
12
+ function config(chain) {
13
+ const c = clusters[chain];
14
+ if (!c || !c.rpcUrls.length)
15
+ throw (0, svm_terms_1.svmInvalid)(`no SVM registry/RPC configuration for ${chain}`);
16
+ (0, svm_accounts_1.svmKey)(c.gatewayProgram);
17
+ return c;
18
+ }
19
+ async function read(chain, fn) {
20
+ const c = config(chain);
21
+ let last;
22
+ for (const url of c.rpcUrls) {
23
+ try {
24
+ const rpc = connect(url);
25
+ // CAIP-2 Solana references are the first 32 characters of genesisHash.
26
+ if (`solana:${(await rpc.getGenesisHash()).slice(0, 32)}` !== chain)
27
+ throw (0, svm_terms_1.svmInvalid)('SVM RPC genesis/cluster mismatch');
28
+ return await fn(rpc);
29
+ }
30
+ catch (error) {
31
+ last = error;
32
+ }
33
+ }
34
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, `SVM metadata lookup failed for ${chain}`, { cause: last });
35
+ }
36
+ return {
37
+ gateway: async (chain) => read(chain, async () => ({
38
+ chainNamespace: chain,
39
+ program: config(chain).gatewayProgram,
40
+ })),
41
+ mint: async (chain, address) => read(chain, async (rpc) => {
42
+ const mint = new web3_js_1.PublicKey(Buffer.from((0, svm_accounts_1.svmKey)(address).slice(2), 'hex'));
43
+ const info = await rpc.getAccountInfo(mint, 'confirmed');
44
+ if (!info || info.executable)
45
+ throw (0, svm_terms_1.svmInvalid)('mint account missing or executable');
46
+ const owner = info.owner.toBase58(), data = info.data;
47
+ const legacy = owner === SPL && data.length === 82;
48
+ const extended = owner === TOKEN_2022 &&
49
+ (data.length === 82 || (data.length >= 166 && data[165] === 1));
50
+ if ((!legacy && !extended) || data[45] !== 1)
51
+ throw (0, svm_terms_1.svmInvalid)('account is not an initialized supported SPL mint');
52
+ return { mint: mint.toBase58(), tokenProgram: owner };
53
+ }),
54
+ };
55
+ }
56
+ //# sourceMappingURL=svm-metadata.js.map
@@ -0,0 +1,6 @@
1
+ import type { Address } from 'viem';
2
+ import type { SolanaRule, DecodedSolanaRule } from '../agentic.types';
3
+ import type { AgenticRuntime } from '../runtime';
4
+ import type { Snapshot } from '../reads/snapshot';
5
+ export declare function svmMetadata(runtime: AgenticRuntime): import("./svm-context").SvmMetadataProvider;
6
+ export declare function preparePublicSvmRule(runtime: AgenticRuntime, snap: Snapshot, wallet: Address, rule: SolanaRule | DecodedSolanaRule): Promise<import("../codec/svm-terms").SvmTermsWire>;