@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,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isSolanaRule = isSolanaRule;
4
+ exports.isUniversalRule = isUniversalRule;
5
+ exports.ruleChain = ruleChain;
6
+ exports.chainHash = chainHash;
7
+ exports.prepareRules = prepareRules;
8
+ const viem_1 = require("viem");
9
+ const capabilities_1 = require("../capabilities");
10
+ const errors_1 = require("../errors");
11
+ const native_1 = require("./native");
12
+ const session_1 = require("./session");
13
+ const universal_1 = require("./universal");
14
+ const svm_terms_1 = require("./svm-terms");
15
+ const v5_1 = require("../contracts/v5");
16
+ function isSolanaRule(rule) {
17
+ return (typeof rule.chainNamespace === 'string' &&
18
+ rule.chainNamespace.startsWith('solana:'));
19
+ }
20
+ function isUniversalRule(rule) {
21
+ return (rule.chainNamespace !== undefined && !isSolanaRule(rule));
22
+ }
23
+ /** CAIP-2 chain a rule governs: omitted = the connected Push chain. */
24
+ function ruleChain(rule, pushChainNamespace) {
25
+ return (rule.chainNamespace ?? pushChainNamespace);
26
+ }
27
+ function chainHash(chainNamespace) {
28
+ return (0, viem_1.keccak256)((0, viem_1.toBytes)(chainNamespace));
29
+ }
30
+ /** Validate before signing. V5 permits several grants for the same agent/chain. */
31
+ function prepareRules(rules, ctx) {
32
+ if (!Array.isArray(rules)) {
33
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'rules must be an array');
34
+ }
35
+ return rules.map((rule, i) => {
36
+ if (!rule || typeof rule !== 'object') {
37
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, `rules[${i}] is not an object`);
38
+ }
39
+ if (!(0, viem_1.isAddress)(rule.agent, { strict: false }) ||
40
+ /^0x0{40}$/i.test(rule.agent)) {
41
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, `rules[${i}].agent must be a non-zero Push address`);
42
+ }
43
+ const agent = (0, viem_1.getAddress)(rule.agent);
44
+ if (ctx.owner && agent === (0, viem_1.getAddress)(ctx.owner)) {
45
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.AGENT_IS_OWNER, `rules[${i}].agent is the wallet owner; the owner uses the owner door`);
46
+ }
47
+ const chainNamespace = ruleChain(rule, ctx.pushChainNamespace);
48
+ if (isUniversalRule(rule) &&
49
+ rule.chainNamespace === ctx.pushChainNamespace) {
50
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, `rules[${i}] names the connected Push chain; omit chainNamespace for a native rule`);
51
+ }
52
+ if ('ref' in rule) {
53
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'InternalRule ref is not supported; remove it from the input');
54
+ }
55
+ if (isSolanaRule(rule)) {
56
+ (0, capabilities_1.requireCapability)(ctx.capabilities, capabilities_1.AgenticCapability.UNIVERSAL_SVM_RULES);
57
+ const terms = ctx.svm?.get(rule);
58
+ if (!terms || !ctx.gateway)
59
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'SVM rules require resolved wallet context and validated instruction layouts');
60
+ return {
61
+ rule,
62
+ agent,
63
+ chainNamespace,
64
+ svmTerms: terms,
65
+ session: (0, session_1.buildSession)({
66
+ validator: ctx.validator,
67
+ rulesPolicy: ctx.rulesPolicy,
68
+ agent,
69
+ actions: [
70
+ {
71
+ target: ctx.gateway,
72
+ selector: v5_1.SEND_OUTBOUND_SELECTOR,
73
+ initData: (0, session_1.encodeEnvelope)(chainNamespace, (0, svm_terms_1.encodeSvmTerms)(terms, ctx.nowSeconds)),
74
+ },
75
+ ],
76
+ }),
77
+ };
78
+ }
79
+ if (isUniversalRule(rule)) {
80
+ return (0, universal_1.encodeUniversalRule)(rule, ctx);
81
+ }
82
+ (0, capabilities_1.requireCapability)(ctx.capabilities, capabilities_1.AgenticCapability.NATIVE_RULES);
83
+ const actions = (0, native_1.nativeRuleToActionTerms)(rule, ctx).map((a) => a.terms);
84
+ const forbidden = [
85
+ '0x0000000000000000000000000000000000000000',
86
+ '0x0000000000000000000000000000000000000001',
87
+ ...(ctx.forbiddenTargets ?? []),
88
+ ].map((a) => a.toLowerCase());
89
+ actions.forEach((terms, j) => {
90
+ if (forbidden.includes(terms.target.toLowerCase())) {
91
+ const where = actions.length > 1 ? `rules[${i}].actions[${j}]` : `rules[${i}]`;
92
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, `${where}.target ${terms.target} is a wallet-infrastructure address the contracts refuse as a native target`);
93
+ }
94
+ });
95
+ // One rule, one rule ID: every action is a separate (target, selector)
96
+ // entry of the same session, each with its own stored terms.
97
+ const session = (0, session_1.buildSession)({
98
+ validator: ctx.validator,
99
+ agent,
100
+ rulesPolicy: ctx.rulesPolicy,
101
+ actions: actions.map((terms) => ({
102
+ target: terms.target,
103
+ selector: terms.selector,
104
+ initData: (0, session_1.encodeEnvelope)(chainNamespace, (0, native_1.encodeNativeTerms)(terms)),
105
+ })),
106
+ });
107
+ return { rule, agent, chainNamespace, session, nativeTerms: actions };
108
+ });
109
+ }
110
+ //# sourceMappingURL=rules.js.map
@@ -0,0 +1,19 @@
1
+ import { type AbiParameter } from 'viem';
2
+ import type { AgenticHex, Selector } from '../agentic.types';
3
+ /** Selector URP uses for a value-only native rule (empty calldata). */
4
+ export declare const VALUE_ONLY_SELECTOR: "0xffffffff";
5
+ export interface ParsedSelector {
6
+ /** 4-byte selector, lowercase. */
7
+ selector: AgenticHex;
8
+ /** Canonical signature when the caller supplied one. */
9
+ signature?: string;
10
+ /** ABI inputs, only available when the caller supplied a signature. */
11
+ inputs?: readonly AbiParameter[];
12
+ valueOnly: boolean;
13
+ }
14
+ /**
15
+ * Parse a public selector (`'0x617ba037'`, `'supply(address,uint256,address,uint16)'`
16
+ * or `'value-only'`). A signature is required wherever pins, amounts or
17
+ * beneficiaries need argument positions; a bare hex selector carries none.
18
+ */
19
+ export declare function parseSelector(input: Selector | 'value-only'): ParsedSelector;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VALUE_ONLY_SELECTOR = void 0;
4
+ exports.parseSelector = parseSelector;
5
+ const viem_1 = require("viem");
6
+ const errors_1 = require("../errors");
7
+ /** Selector URP uses for a value-only native rule (empty calldata). */
8
+ exports.VALUE_ONLY_SELECTOR = '0xffffffff';
9
+ const HEX_SELECTOR = /^0x[0-9a-fA-F]{8}$/;
10
+ /**
11
+ * Parse a public selector (`'0x617ba037'`, `'supply(address,uint256,address,uint16)'`
12
+ * or `'value-only'`). A signature is required wherever pins, amounts or
13
+ * beneficiaries need argument positions; a bare hex selector carries none.
14
+ */
15
+ function parseSelector(input) {
16
+ if (input === 'value-only') {
17
+ return { selector: exports.VALUE_ONLY_SELECTOR, valueOnly: true };
18
+ }
19
+ if (typeof input !== 'string') {
20
+ throw invalid(`selector must be a string, got ${typeof input}`);
21
+ }
22
+ if (HEX_SELECTOR.test(input)) {
23
+ const selector = input.toLowerCase();
24
+ if (selector === exports.VALUE_ONLY_SELECTOR) {
25
+ return { selector, valueOnly: true };
26
+ }
27
+ return { selector, valueOnly: false };
28
+ }
29
+ let item;
30
+ try {
31
+ const parsed = (0, viem_1.parseAbiItem)(`function ${input}`);
32
+ if (parsed.type !== 'function')
33
+ throw new Error('not a function');
34
+ item = parsed;
35
+ }
36
+ catch (cause) {
37
+ throw invalid(`selector "${input}" is neither a 4-byte hex selector nor a function signature`, cause);
38
+ }
39
+ const selector = (0, viem_1.toFunctionSelector)(item).toLowerCase();
40
+ if (selector === exports.VALUE_ONLY_SELECTOR) {
41
+ throw invalid(`selector "${input}" collides with the reserved value-only selector`);
42
+ }
43
+ return {
44
+ selector,
45
+ signature: `${item.name}(${item.inputs.map(canonicalType).join(',')})`,
46
+ inputs: item.inputs,
47
+ valueOnly: false,
48
+ };
49
+ }
50
+ function canonicalType(p) {
51
+ if (p.type.startsWith('tuple')) {
52
+ const components = p.components ?? [];
53
+ return `(${components.map(canonicalType).join(',')})${p.type.slice(5)}`;
54
+ }
55
+ return p.type;
56
+ }
57
+ function invalid(message, cause) {
58
+ return new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, message, { cause });
59
+ }
60
+ //# sourceMappingURL=selectors.js.map
@@ -0,0 +1,54 @@
1
+ import { type AbiParameter, type Address, type Hex } from 'viem';
2
+ /**
3
+ * The smartsessions `Session` a grant carries (DataTypes.sol:56-86, pinned
4
+ * v5). The wallet overwrites `salt` with its grant nonce, so the value
5
+ * sent is irrelevant; it is always zero here.
6
+ */
7
+ export interface SessionWire {
8
+ sessionValidator: Address;
9
+ sessionValidatorInitData: Hex;
10
+ salt: Hex;
11
+ userOpPolicies: readonly {
12
+ policy: Address;
13
+ initData: Hex;
14
+ }[];
15
+ erc7739Policies: {
16
+ allowedERC7739Content: readonly {
17
+ appDomainSeparator: Hex;
18
+ contentNames: readonly string[];
19
+ }[];
20
+ erc1271Policies: readonly {
21
+ policy: Address;
22
+ initData: Hex;
23
+ }[];
24
+ };
25
+ actions: readonly {
26
+ actionTargetSelector: Hex;
27
+ actionTarget: Address;
28
+ actionPolicies: readonly {
29
+ policy: Address;
30
+ initData: Hex;
31
+ }[];
32
+ }[];
33
+ permitERC4337Paymaster: boolean;
34
+ }
35
+ export declare const SESSION_PARAM: AbiParameter;
36
+ export declare const ENVELOPE_VERSION = 1;
37
+ /** V5 URP envelope: abi.encode(uint16(1), string chainNamespace, bytes body). */
38
+ export declare function encodeEnvelope(chainNamespace: string, body: Hex): Hex;
39
+ export declare function decodeEnvelope(initData: Hex): {
40
+ chainNamespace: string;
41
+ body: Hex;
42
+ };
43
+ export declare function buildSession(ctx: {
44
+ validator: Address;
45
+ agent: Address;
46
+ rulesPolicy: Address;
47
+ actions: {
48
+ target: Address;
49
+ selector: Hex;
50
+ initData: Hex;
51
+ }[];
52
+ }): SessionWire;
53
+ export declare function encodeSession(session: SessionWire): Hex;
54
+ export declare function decodeSession(bytes: Hex): SessionWire;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ENVELOPE_VERSION = exports.SESSION_PARAM = void 0;
4
+ exports.encodeEnvelope = encodeEnvelope;
5
+ exports.decodeEnvelope = decodeEnvelope;
6
+ exports.buildSession = buildSession;
7
+ exports.encodeSession = encodeSession;
8
+ exports.decodeSession = decodeSession;
9
+ const viem_1 = require("viem");
10
+ const v5_1 = require("../contracts/abi/v5");
11
+ const ids_1 = require("./ids");
12
+ const errors_1 = require("../errors");
13
+ exports.SESSION_PARAM = (0, viem_1.getAbiItem)({
14
+ abi: v5_1.AGW_ABI,
15
+ name: 'grantRules',
16
+ }).inputs[0];
17
+ const ZERO_SALT = `0x${'00'.repeat(32)}`;
18
+ exports.ENVELOPE_VERSION = 1;
19
+ /** V5 URP envelope: abi.encode(uint16(1), string chainNamespace, bytes body). */
20
+ function encodeEnvelope(chainNamespace, body) {
21
+ return (0, viem_1.encodeAbiParameters)([{ type: 'uint16' }, { type: 'string' }, { type: 'bytes' }], [exports.ENVELOPE_VERSION, chainNamespace, body]);
22
+ }
23
+ function decodeEnvelope(initData) {
24
+ try {
25
+ const [version, chainNamespace, body] = (0, viem_1.decodeAbiParameters)([{ type: 'uint16' }, { type: 'string' }, { type: 'bytes' }], initData);
26
+ if (version !== exports.ENVELOPE_VERSION)
27
+ throw new Error(`unsupported rules envelope version ${version}`);
28
+ return { chainNamespace, body };
29
+ }
30
+ catch (cause) {
31
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.RULE_READ_FAILED, 'malformed URP envelope', {
32
+ cause,
33
+ });
34
+ }
35
+ }
36
+ function buildSession(ctx) {
37
+ return {
38
+ sessionValidator: ctx.validator,
39
+ sessionValidatorInitData: (0, ids_1.agentConfig)(ctx.agent),
40
+ salt: ZERO_SALT,
41
+ userOpPolicies: [],
42
+ erc7739Policies: { allowedERC7739Content: [], erc1271Policies: [] },
43
+ actions: ctx.actions.map((a) => ({
44
+ actionTargetSelector: a.selector,
45
+ actionTarget: a.target,
46
+ actionPolicies: [{ policy: ctx.rulesPolicy, initData: a.initData }],
47
+ })),
48
+ permitERC4337Paymaster: false,
49
+ };
50
+ }
51
+ function encodeSession(session) {
52
+ return (0, viem_1.encodeAbiParameters)([exports.SESSION_PARAM], [session]);
53
+ }
54
+ function decodeSession(bytes) {
55
+ try {
56
+ const [session] = (0, viem_1.decodeAbiParameters)([exports.SESSION_PARAM], bytes);
57
+ return session;
58
+ }
59
+ catch (cause) {
60
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.RULE_READ_FAILED, 'malformed Session encoding', {
61
+ cause,
62
+ });
63
+ }
64
+ }
65
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1,35 @@
1
+ import { type Address, type Hex } from 'viem';
2
+ import type { AssetCapWire } from './universal-terms';
3
+ export declare function svmKey(value: string): Hex;
4
+ /** Gateway is supplied by the selected cluster registry, never a global hard-coded program. */
5
+ export declare function deriveAgwSvmCea(wallet: Address, gatewayProgram: string): {
6
+ address: Hex;
7
+ bump: number;
8
+ };
9
+ export interface SvmTokenAccountInput {
10
+ mint: string;
11
+ tokenProgram?: string;
12
+ }
13
+ export type ResolvedSvmAsset = {
14
+ kind: 'native';
15
+ cap: AssetCapWire;
16
+ } | {
17
+ kind: 'spl';
18
+ cap: AssetCapWire;
19
+ mint: string;
20
+ tokenProgram?: string;
21
+ };
22
+ /** Every resolved SPL cap contributes an ATA; caller cannot omit a listed mint. */
23
+ export declare function resolveAgwSvmRuleContext(wallet: Address, gatewayProgram: string, assets: readonly ResolvedSvmAsset[], outputs?: readonly SvmTokenAccountInput[]): {
24
+ assets: AssetCapWire[];
25
+ expectedCEA: Hex;
26
+ gatewayProgram: Hex;
27
+ ceaAccounts: Hex[];
28
+ };
29
+ export declare function deriveAgwSvmAta(owner: string, token: SvmTokenAccountInput): Hex;
30
+ /** Includes every listed mint and swap-output mint. Native SOL is already covered by the CEA. */
31
+ export declare function deriveAgwSvmValueAccounts(wallet: Address, gatewayProgram: string, tokens: readonly SvmTokenAccountInput[], outputs?: readonly SvmTokenAccountInput[]): {
32
+ expectedCEA: Hex;
33
+ gatewayProgram: Hex;
34
+ ceaAccounts: Hex[];
35
+ };
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.svmKey = svmKey;
4
+ exports.deriveAgwSvmCea = deriveAgwSvmCea;
5
+ exports.resolveAgwSvmRuleContext = resolveAgwSvmRuleContext;
6
+ exports.deriveAgwSvmAta = deriveAgwSvmAta;
7
+ exports.deriveAgwSvmValueAccounts = deriveAgwSvmValueAccounts;
8
+ const web3_js_1 = require("@solana/web3.js");
9
+ const viem_1 = require("viem");
10
+ const svm_terms_1 = require("./svm-terms");
11
+ const SPL_TOKEN = new web3_js_1.PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA');
12
+ const TOKEN_2022 = new web3_js_1.PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb');
13
+ const ASSOCIATED_TOKEN = new web3_js_1.PublicKey('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL');
14
+ function svmKey(value) {
15
+ try {
16
+ if (value.startsWith('0x')) {
17
+ (0, svm_terms_1.exactHex)(value, 32, 'pubkey');
18
+ return (0, viem_1.bytesToHex)(new web3_js_1.PublicKey((0, viem_1.hexToBytes)(value)).toBytes());
19
+ }
20
+ return (0, viem_1.bytesToHex)(new web3_js_1.PublicKey(value).toBytes());
21
+ }
22
+ catch {
23
+ throw (0, svm_terms_1.svmInvalid)('invalid Solana public key');
24
+ }
25
+ }
26
+ function key(value) {
27
+ return new web3_js_1.PublicKey((0, viem_1.hexToBytes)(svmKey(value)));
28
+ }
29
+ /** Gateway is supplied by the selected cluster registry, never a global hard-coded program. */
30
+ function deriveAgwSvmCea(wallet, gatewayProgram) {
31
+ if (key(gatewayProgram).equals(web3_js_1.PublicKey.default))
32
+ throw (0, svm_terms_1.svmInvalid)('gateway program cannot be zero');
33
+ const [pda, bump] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from('push_identity'), Buffer.from((0, viem_1.hexToBytes)((0, viem_1.getAddress)(wallet)))], key(gatewayProgram));
34
+ return { address: (0, viem_1.bytesToHex)(pda.toBytes()), bump };
35
+ }
36
+ /** Every resolved SPL cap contributes an ATA; caller cannot omit a listed mint. */
37
+ function resolveAgwSvmRuleContext(wallet, gatewayProgram, assets, outputs = []) {
38
+ if (!assets.length || assets.length > svm_terms_1.SVM_LIMITS.assets)
39
+ throw (0, svm_terms_1.svmInvalid)('resolved assets must contain 1..8 entries');
40
+ const tokens = assets.flatMap((a) => a.kind === 'spl' ? [{ mint: a.mint, tokenProgram: a.tokenProgram }] : []);
41
+ return {
42
+ ...deriveAgwSvmValueAccounts(wallet, gatewayProgram, tokens, outputs),
43
+ assets: assets.map((a) => a.cap),
44
+ };
45
+ }
46
+ function deriveAgwSvmAta(owner, token) {
47
+ const mint = key(token.mint), program = token.tokenProgram ? key(token.tokenProgram) : SPL_TOKEN;
48
+ if (mint.equals(web3_js_1.PublicKey.default))
49
+ throw (0, svm_terms_1.svmInvalid)('native SOL uses the CEA, not an ATA');
50
+ if (!program.equals(SPL_TOKEN) && !program.equals(TOKEN_2022))
51
+ throw (0, svm_terms_1.svmInvalid)('unsupported token program');
52
+ const [ata] = web3_js_1.PublicKey.findProgramAddressSync([key(owner).toBuffer(), program.toBuffer(), mint.toBuffer()], ASSOCIATED_TOKEN);
53
+ return (0, viem_1.bytesToHex)(ata.toBytes());
54
+ }
55
+ /** Includes every listed mint and swap-output mint. Native SOL is already covered by the CEA. */
56
+ function deriveAgwSvmValueAccounts(wallet, gatewayProgram, tokens, outputs = []) {
57
+ const expectedCEA = deriveAgwSvmCea(wallet, gatewayProgram).address;
58
+ const ceaAccounts = [
59
+ ...new Set([
60
+ expectedCEA,
61
+ ...[...tokens, ...outputs].map((t) => deriveAgwSvmAta(expectedCEA, t)),
62
+ ]),
63
+ ];
64
+ if (ceaAccounts.length > svm_terms_1.SVM_LIMITS.ceaAccounts)
65
+ throw (0, svm_terms_1.svmInvalid)('derived CEA value-account set exceeds 16');
66
+ return { expectedCEA, gatewayProgram: svmKey(gatewayProgram), ceaAccounts };
67
+ }
68
+ //# sourceMappingURL=svm-accounts.js.map
@@ -0,0 +1,11 @@
1
+ /** Compile named Anchor IDL constraints. Unsupported layouts are never guessed. */
2
+ import type { Idl } from '@coral-xyz/anchor';
3
+ import { type Hex } from 'viem';
4
+ import type { SolanaRule } from '../agentic.types';
5
+ import { type SvmTermsWire } from './svm-terms';
6
+ export type SvmResolvedContext = Pick<SvmTermsWire, 'assets' | 'expectedCEA' | 'gatewayProgram' | 'ceaAccounts'> & {
7
+ /** Verified mint -> wallet ATA mapping, including output mints. */
8
+ tokenAccounts: ReadonlyMap<Hex, Hex>;
9
+ };
10
+ export declare function svmIdlInstruction(idl: Idl, name: string): import("@coral-xyz/anchor/dist/cjs/idl").IdlInstruction;
11
+ export declare function compileSvmIdlRule(rule: SolanaRule, ctx: SvmResolvedContext, now: number): SvmTermsWire;
@@ -0,0 +1,226 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.svmIdlInstruction = svmIdlInstruction;
4
+ exports.compileSvmIdlRule = compileSvmIdlRule;
5
+ const viem_1 = require("viem");
6
+ const svm_accounts_1 = require("./svm-accounts");
7
+ const svm_terms_1 = require("./svm-terms");
8
+ const widths = {
9
+ bool: 1,
10
+ u8: 1,
11
+ i8: 1,
12
+ u16: 2,
13
+ i16: 2,
14
+ u32: 4,
15
+ i32: 4,
16
+ f32: 4,
17
+ u64: 8,
18
+ i64: 8,
19
+ f64: 8,
20
+ u128: 16,
21
+ i128: 16,
22
+ u256: 32,
23
+ i256: 32,
24
+ pubkey: 32,
25
+ };
26
+ function width(type) {
27
+ if (typeof type === 'string' && widths[type])
28
+ return widths[type];
29
+ if (type && typeof type === 'object' && 'array' in type) {
30
+ const a = type.array;
31
+ if (Array.isArray(a) &&
32
+ a.length === 2 &&
33
+ Number.isSafeInteger(a[1]) &&
34
+ a[1] > 0) {
35
+ const size = width(a[0]) * a[1];
36
+ if (size <= 1024)
37
+ return size;
38
+ }
39
+ }
40
+ throw (0, svm_terms_1.svmInvalid)('IDL field layout is not supported: use fixed-width primitives or fixed arrays');
41
+ }
42
+ function svmIdlInstruction(idl, name) {
43
+ if (!idl || !Array.isArray(idl.instructions) || typeof name !== 'string')
44
+ throw (0, svm_terms_1.svmInvalid)('instruction requires an Anchor IDL and name');
45
+ const found = idl.instructions.filter((i) => i.name === name);
46
+ if (found.length !== 1)
47
+ throw (0, svm_terms_1.svmInvalid)(`IDL instruction ${name} must exist exactly once`);
48
+ const ix = found[0];
49
+ if (!Array.isArray(ix.discriminator) ||
50
+ ix.discriminator.length !== 8 ||
51
+ ix.discriminator.some((b) => !Number.isInteger(b) || b < 0 || b > 255))
52
+ throw (0, svm_terms_1.svmInvalid)('Anchor IDL discriminator must contain eight bytes');
53
+ return ix;
54
+ }
55
+ function accountNames(accounts, prefix = '') {
56
+ return accounts.flatMap((entry) => {
57
+ if (!entry ||
58
+ typeof entry !== 'object' ||
59
+ !('name' in entry) ||
60
+ typeof entry.name !== 'string')
61
+ throw (0, svm_terms_1.svmInvalid)('invalid IDL account');
62
+ const name = prefix + entry.name;
63
+ if ('accounts' in entry)
64
+ throw (0, svm_terms_1.svmInvalid)('nested IDL accounts are not supported by the instruction resolver');
65
+ if ('optional' in entry && entry.optional)
66
+ throw (0, svm_terms_1.svmInvalid)('optional IDL accounts are not supported for rules');
67
+ return [name];
68
+ });
69
+ }
70
+ function resolveAccount(ref, ctx) {
71
+ if (!ref || typeof ref !== 'object')
72
+ throw (0, svm_terms_1.svmInvalid)('invalid named account reference');
73
+ if (ref.kind === 'walletCEA')
74
+ return ctx.expectedCEA;
75
+ if (ref.kind === 'address')
76
+ return (0, svm_accounts_1.svmKey)(ref.address);
77
+ if (ref.kind === 'walletATA') {
78
+ const ata = ctx.tokenAccounts.get((0, svm_accounts_1.svmKey)(ref.token));
79
+ if (!ata)
80
+ throw (0, svm_terms_1.svmInvalid)('walletATA mint must be listed in assets or outputTokens');
81
+ return ata;
82
+ }
83
+ throw (0, svm_terms_1.svmInvalid)('unknown named account reference');
84
+ }
85
+ function unsigned(f) {
86
+ if (typeof f.type !== 'string' || !/^u(8|16|32|64)$/.test(f.type))
87
+ throw (0, svm_terms_1.svmInvalid)('range/ratio constraints require unsigned integer fields of at most eight bytes');
88
+ }
89
+ function bound(v, len) {
90
+ if (typeof v !== 'bigint' ||
91
+ v < BigInt(0) ||
92
+ v >= BigInt(1) << BigInt(len * 8))
93
+ throw (0, svm_terms_1.svmInvalid)('field constraint exceeds its unsigned width');
94
+ return `0x${v.toString(16).padStart(64, '0')}`;
95
+ }
96
+ function compileSvmIdlRule(rule, ctx, now) {
97
+ if (!Array.isArray(rule.allowedInstructions) ||
98
+ !rule.allowedInstructions.length ||
99
+ rule.allowedInstructions.length > 32)
100
+ throw (0, svm_terms_1.svmInvalid)('allowedInstructions must contain 1..32 entries');
101
+ const terms = {
102
+ validUntil: rule.validUntil,
103
+ maxGasPerCall: rule.maxGasPerCall,
104
+ assets: ctx.assets,
105
+ expectedCEA: ctx.expectedCEA,
106
+ gatewayProgram: ctx.gatewayProgram,
107
+ ceaAccounts: ctx.ceaAccounts,
108
+ programs: [],
109
+ pins: [],
110
+ dataPins: [],
111
+ };
112
+ const programs = [], pins = [], dataPins = [];
113
+ for (const [ruleIndex, entry] of rule.allowedInstructions.entries()) {
114
+ const ix = svmIdlInstruction(entry.instruction?.idl, entry.instruction?.name);
115
+ const program = (0, svm_accounts_1.svmKey)(entry.program);
116
+ if ((0, svm_accounts_1.svmKey)(entry.instruction.idl.address) !== program)
117
+ throw (0, svm_terms_1.svmInvalid)('IDL address differs from rule program');
118
+ if (!Array.isArray(ix.accounts) || !Array.isArray(entry.accounts))
119
+ throw (0, svm_terms_1.svmInvalid)('instruction accounts are required');
120
+ const names = accountNames(ix.accounts);
121
+ if (new Set(names).size !== names.length)
122
+ throw (0, svm_terms_1.svmInvalid)('duplicate IDL account names');
123
+ programs.push({
124
+ program,
125
+ discriminator: (0, viem_1.bytesToHex)(Uint8Array.from([
126
+ ...ix.discriminator,
127
+ ...Array(8 - ix.discriminator.length).fill(0),
128
+ ])),
129
+ discriminatorLen: ix.discriminator.length,
130
+ dataless: false,
131
+ maxAccounts: entry.maxAccounts ?? names.length,
132
+ });
133
+ for (const a of entry.accounts) {
134
+ const accountIndex = names.indexOf(a.name);
135
+ if (accountIndex < 0)
136
+ throw (0, svm_terms_1.svmInvalid)(`unknown IDL account ${a.name}`);
137
+ pins.push({
138
+ ruleIndex,
139
+ accountIndex,
140
+ expected: resolveAccount(a.expected, ctx),
141
+ });
142
+ }
143
+ const fields = new Map();
144
+ let offset = ix.discriminator.length;
145
+ // Reject variable/nested-defined layouts rather than claiming a reliable offset.
146
+ for (const arg of ix.args) {
147
+ if (fields.has(arg.name))
148
+ throw (0, svm_terms_1.svmInvalid)('duplicate IDL field name');
149
+ const len = width(arg.type);
150
+ fields.set(arg.name, { offset, len, type: arg.type });
151
+ offset += len;
152
+ }
153
+ if (offset > 1024)
154
+ throw (0, svm_terms_1.svmInvalid)('IDL instruction data exceeds the policy limit');
155
+ const field = (name) => {
156
+ const f = fields.get(name);
157
+ if (!f)
158
+ throw (0, svm_terms_1.svmInvalid)(`unknown IDL field ${name}`);
159
+ return f;
160
+ };
161
+ if (entry.fields !== undefined && !Array.isArray(entry.fields))
162
+ throw (0, svm_terms_1.svmInvalid)('fields must be an array');
163
+ for (const constraint of entry.fields ?? []) {
164
+ const keys = ['equals', 'min', 'max', 'minRatio'].filter((k) => k in constraint);
165
+ if (keys.length !== 1)
166
+ throw (0, svm_terms_1.svmInvalid)('each field constraint must specify exactly one comparison');
167
+ const f = field('numerator' in constraint ? constraint.numerator : constraint.name);
168
+ const p = {
169
+ ruleIndex,
170
+ fromEnd: false,
171
+ offset: f.offset,
172
+ offsetB: 0,
173
+ len: f.len,
174
+ mode: svm_terms_1.SvmDataPinMode.EQ,
175
+ expected: `0x${'00'.repeat(32)}`,
176
+ num: BigInt(0),
177
+ den: BigInt(0),
178
+ };
179
+ if ('minRatio' in constraint) {
180
+ unsigned(f);
181
+ const b = field(constraint.denominator);
182
+ unsigned(b);
183
+ if (f.len !== b.len)
184
+ throw (0, svm_terms_1.svmInvalid)('ratio fields must have the same width');
185
+ Object.assign(p, {
186
+ mode: svm_terms_1.SvmDataPinMode.RATIO_GTE_LE,
187
+ offsetB: b.offset,
188
+ num: constraint.minRatio.num,
189
+ den: constraint.minRatio.den,
190
+ });
191
+ }
192
+ else if ('min' in constraint || 'max' in constraint) {
193
+ unsigned(f);
194
+ const min = 'min' in constraint;
195
+ p.mode = min ? svm_terms_1.SvmDataPinMode.GTE_LE : svm_terms_1.SvmDataPinMode.LTE_LE;
196
+ p.expected = bound(min ? constraint.min : constraint.max, f.len);
197
+ }
198
+ else {
199
+ let bytes;
200
+ if (typeof constraint.equals === 'bigint') {
201
+ unsigned(f);
202
+ bound(constraint.equals, f.len);
203
+ bytes = Uint8Array.from({ length: f.len }, (_, i) => Number((constraint.equals >> BigInt(8 * i)) & BigInt(255)));
204
+ }
205
+ else if (typeof constraint.equals === 'boolean') {
206
+ if (f.type !== 'bool')
207
+ throw (0, svm_terms_1.svmInvalid)('boolean equality requires a bool field');
208
+ bytes = Uint8Array.of(constraint.equals ? 1 : 0);
209
+ }
210
+ else {
211
+ if (!/^0x(?:[a-fA-F0-9]{2})*$/.test(constraint.equals))
212
+ throw (0, svm_terms_1.svmInvalid)('equality requires exact hex bytes');
213
+ bytes = (0, viem_1.hexToBytes)(constraint.equals);
214
+ }
215
+ if (bytes.length !== f.len || f.len > 32)
216
+ throw (0, svm_terms_1.svmInvalid)('equality value must match field width (at most 32 bytes)');
217
+ p.expected = `0x${(0, viem_1.bytesToHex)(bytes).slice(2).padEnd(64, '0')}`;
218
+ }
219
+ dataPins.push(p);
220
+ }
221
+ }
222
+ Object.assign(terms, { programs, pins, dataPins });
223
+ (0, svm_terms_1.validateSvmTerms)(terms, now);
224
+ return terms;
225
+ }
226
+ //# sourceMappingURL=svm-idl.js.map