@pushchain/core 6.0.26 → 6.0.27-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/AGW.md +226 -0
  2. package/CHANGELOG.md +18 -0
  3. package/README.md +2 -0
  4. package/package.json +7 -1
  5. package/src/lib/agentic/agentic.d.ts +23 -0
  6. package/src/lib/agentic/agentic.js +51 -0
  7. package/src/lib/agentic/agentic.types.d.ts +414 -0
  8. package/src/lib/agentic/agentic.types.js +3 -0
  9. package/src/lib/agentic/capabilities.d.ts +37 -0
  10. package/src/lib/agentic/capabilities.js +64 -0
  11. package/src/lib/agentic/chain.d.ts +3 -0
  12. package/src/lib/agentic/chain.js +15 -0
  13. package/src/lib/agentic/codec/abi-layout.d.ts +20 -0
  14. package/src/lib/agentic/codec/abi-layout.js +168 -0
  15. package/src/lib/agentic/codec/defaults.d.ts +51 -0
  16. package/src/lib/agentic/codec/defaults.js +53 -0
  17. package/src/lib/agentic/codec/ids.d.ts +35 -0
  18. package/src/lib/agentic/codec/ids.js +74 -0
  19. package/src/lib/agentic/codec/native.d.ts +73 -0
  20. package/src/lib/agentic/codec/native.js +341 -0
  21. package/src/lib/agentic/codec/pins.d.ts +20 -0
  22. package/src/lib/agentic/codec/pins.js +29 -0
  23. package/src/lib/agentic/codec/rules.d.ts +45 -0
  24. package/src/lib/agentic/codec/rules.js +110 -0
  25. package/src/lib/agentic/codec/selectors.d.ts +19 -0
  26. package/src/lib/agentic/codec/selectors.js +60 -0
  27. package/src/lib/agentic/codec/session.d.ts +54 -0
  28. package/src/lib/agentic/codec/session.js +65 -0
  29. package/src/lib/agentic/codec/svm-accounts.d.ts +35 -0
  30. package/src/lib/agentic/codec/svm-accounts.js +68 -0
  31. package/src/lib/agentic/codec/svm-idl.d.ts +11 -0
  32. package/src/lib/agentic/codec/svm-idl.js +226 -0
  33. package/src/lib/agentic/codec/svm-terms.d.ts +157 -0
  34. package/src/lib/agentic/codec/svm-terms.js +246 -0
  35. package/src/lib/agentic/codec/universal-terms.d.ts +82 -0
  36. package/src/lib/agentic/codec/universal-terms.js +43 -0
  37. package/src/lib/agentic/codec/universal.d.ts +14 -0
  38. package/src/lib/agentic/codec/universal.js +173 -0
  39. package/src/lib/agentic/constants.d.ts +12 -0
  40. package/src/lib/agentic/constants.js +16 -0
  41. package/src/lib/agentic/context.d.ts +29 -0
  42. package/src/lib/agentic/context.js +56 -0
  43. package/src/lib/agentic/contracts/abi/v6/agent-validator.d.ts +105 -0
  44. package/src/lib/agentic/contracts/abi/v6/agent-validator.js +149 -0
  45. package/src/lib/agentic/contracts/abi/v6/agw-factory.d.ts +1146 -0
  46. package/src/lib/agentic/contracts/abi/v6/agw-factory.js +1499 -0
  47. package/src/lib/agentic/contracts/abi/v6/agw.d.ts +1306 -0
  48. package/src/lib/agentic/contracts/abi/v6/agw.js +1713 -0
  49. package/src/lib/agentic/contracts/abi/v6/index.d.ts +6 -0
  50. package/src/lib/agentic/contracts/abi/v6/index.js +17 -0
  51. package/src/lib/agentic/contracts/abi/v6/smart-session.d.ts +1493 -0
  52. package/src/lib/agentic/contracts/abi/v6/smart-session.js +1965 -0
  53. package/src/lib/agentic/contracts/abi/v6/universal-rules-policy.d.ts +1661 -0
  54. package/src/lib/agentic/contracts/abi/v6/universal-rules-policy.js +2187 -0
  55. package/src/lib/agentic/contracts/donut-v6.d.ts +11 -0
  56. package/src/lib/agentic/contracts/donut-v6.js +15 -0
  57. package/src/lib/agentic/contracts/prc20-metadata.d.ts +26 -0
  58. package/src/lib/agentic/contracts/prc20-metadata.js +87 -0
  59. package/src/lib/agentic/contracts/reader.d.ts +35 -0
  60. package/src/lib/agentic/contracts/reader.js +16 -0
  61. package/src/lib/agentic/contracts/v6.d.ts +5739 -0
  62. package/src/lib/agentic/contracts/v6.js +343 -0
  63. package/src/lib/agentic/deployments.d.ts +61 -0
  64. package/src/lib/agentic/deployments.js +203 -0
  65. package/src/lib/agentic/errors.d.ts +65 -0
  66. package/src/lib/agentic/errors.js +84 -0
  67. package/src/lib/agentic/execution/guards.d.ts +9 -0
  68. package/src/lib/agentic/execution/guards.js +30 -0
  69. package/src/lib/agentic/execution/outbound.d.ts +55 -0
  70. package/src/lib/agentic/execution/outbound.js +187 -0
  71. package/src/lib/agentic/execution/send.d.ts +9 -0
  72. package/src/lib/agentic/execution/send.js +268 -0
  73. package/src/lib/agentic/execution/svm-instruction.d.ts +24 -0
  74. package/src/lib/agentic/execution/svm-instruction.js +40 -0
  75. package/src/lib/agentic/execution/svm-outbound.d.ts +18 -0
  76. package/src/lib/agentic/execution/svm-outbound.js +86 -0
  77. package/src/lib/agentic/execution/svm-payload.d.ts +18 -0
  78. package/src/lib/agentic/execution/svm-payload.js +136 -0
  79. package/src/lib/agentic/execution/svm-public.d.ts +5 -0
  80. package/src/lib/agentic/execution/svm-public.js +176 -0
  81. package/src/lib/agentic/execution/svm-quote.d.ts +11 -0
  82. package/src/lib/agentic/execution/svm-quote.js +24 -0
  83. package/src/lib/agentic/execution/svm-send.d.ts +44 -0
  84. package/src/lib/agentic/execution/svm-send.js +87 -0
  85. package/src/lib/agentic/index.d.ts +5 -0
  86. package/src/lib/agentic/index.js +10 -0
  87. package/src/lib/agentic/management/common.d.ts +29 -0
  88. package/src/lib/agentic/management/common.js +116 -0
  89. package/src/lib/agentic/management/create.d.ts +4 -0
  90. package/src/lib/agentic/management/create.js +138 -0
  91. package/src/lib/agentic/management/duplicates.d.ts +4 -0
  92. package/src/lib/agentic/management/duplicates.js +20 -0
  93. package/src/lib/agentic/management/label.d.ts +3 -0
  94. package/src/lib/agentic/management/label.js +17 -0
  95. package/src/lib/agentic/management/prepare.d.ts +7 -0
  96. package/src/lib/agentic/management/prepare.js +95 -0
  97. package/src/lib/agentic/management/rules-write.d.ts +42 -0
  98. package/src/lib/agentic/management/rules-write.js +279 -0
  99. package/src/lib/agentic/management/svm-context.d.ts +25 -0
  100. package/src/lib/agentic/management/svm-context.js +55 -0
  101. package/src/lib/agentic/management/svm-metadata.d.ts +10 -0
  102. package/src/lib/agentic/management/svm-metadata.js +56 -0
  103. package/src/lib/agentic/management/svm-public.d.ts +6 -0
  104. package/src/lib/agentic/management/svm-public.js +125 -0
  105. package/src/lib/agentic/management/svm.d.ts +27 -0
  106. package/src/lib/agentic/management/svm.js +150 -0
  107. package/src/lib/agentic/presets/catalog.d.ts +26 -0
  108. package/src/lib/agentic/presets/catalog.js +307 -0
  109. package/src/lib/agentic/presets/index.d.ts +5 -0
  110. package/src/lib/agentic/presets/index.js +265 -0
  111. package/src/lib/agentic/presets/types.d.ts +48 -0
  112. package/src/lib/agentic/presets/types.js +3 -0
  113. package/src/lib/agentic/public-rule.d.ts +9 -0
  114. package/src/lib/agentic/public-rule.js +326 -0
  115. package/src/lib/agentic/reads/checkpoints.d.ts +17 -0
  116. package/src/lib/agentic/reads/checkpoints.js +109 -0
  117. package/src/lib/agentic/reads/rules.d.ts +42 -0
  118. package/src/lib/agentic/reads/rules.js +302 -0
  119. package/src/lib/agentic/reads/snapshot.d.ts +28 -0
  120. package/src/lib/agentic/reads/snapshot.js +85 -0
  121. package/src/lib/agentic/reads/svm.d.ts +17 -0
  122. package/src/lib/agentic/reads/svm.js +52 -0
  123. package/src/lib/agentic/reads/wallets.d.ts +25 -0
  124. package/src/lib/agentic/reads/wallets.js +122 -0
  125. package/src/lib/agentic/response.d.ts +41 -0
  126. package/src/lib/agentic/response.js +358 -0
  127. package/src/lib/agentic/revert.d.ts +7 -0
  128. package/src/lib/agentic/revert.js +102 -0
  129. package/src/lib/agentic/runtime.d.ts +58 -0
  130. package/src/lib/agentic/runtime.js +104 -0
  131. package/src/lib/agentic/utils.d.ts +57 -0
  132. package/src/lib/agentic/utils.js +165 -0
  133. package/src/lib/agentic/wallet.d.ts +32 -0
  134. package/src/lib/agentic/wallet.js +130 -0
  135. package/src/lib/constants/enums.d.ts +6 -3
  136. package/src/lib/constants/enums.js +6 -3
  137. package/src/lib/constants/index.d.ts +61 -4
  138. package/src/lib/constants/index.js +17 -3
  139. package/src/lib/constants/read-state.d.ts +9 -0
  140. package/src/lib/constants/read-state.js +13 -1
  141. package/src/lib/index.d.ts +8 -2
  142. package/src/lib/index.js +9 -1
  143. package/src/lib/orchestrator/internals/errors.d.ts +7 -0
  144. package/src/lib/orchestrator/internals/errors.js +13 -1
  145. package/src/lib/orchestrator/internals/execute-standard.d.ts +3 -1
  146. package/src/lib/orchestrator/internals/execute-standard.js +3 -3
  147. package/src/lib/orchestrator/internals/outbound-sync.js +52 -7
  148. package/src/lib/orchestrator/internals/push-chain-tx.d.ts +1 -1
  149. package/src/lib/orchestrator/internals/push-chain-tx.js +13 -1
  150. package/src/lib/orchestrator/orchestrator.d.ts +3 -0
  151. package/src/lib/orchestrator/orchestrator.js +13 -1
  152. package/src/lib/orchestrator/orchestrator.types.d.ts +8 -0
  153. package/src/lib/orchestrator/route-detector.js +3 -2
  154. package/src/lib/orchestrator/svm-idl/resolve.d.ts +2 -0
  155. package/src/lib/orchestrator/svm-idl/resolve.js +11 -1
  156. package/src/lib/progress-hook/progress-hook.js +63 -0
  157. package/src/lib/progress-hook/progress-hook.types.d.ts +10 -1
  158. package/src/lib/progress-hook/progress-hook.types.js +10 -0
  159. package/src/lib/push-chain/push-chain.d.ts +57 -15
  160. package/src/lib/push-chain/push-chain.js +66 -7
  161. package/src/lib/read-state/destination.js +6 -1
  162. package/src/lib/read-state/read-params.d.ts +3 -3
  163. package/src/lib/read-state/read-params.js +3 -2
  164. package/src/lib/read-state/read-state.types.d.ts +3 -1
  165. package/src/lib/read-state/read-tracker.js +2 -2
  166. package/src/lib/read-state/spec-builder.js +1 -1
  167. package/src/lib/universal/prc20-address.d.ts +15 -0
  168. package/src/lib/universal/prc20-address.js +135 -0
  169. package/src/lib/utils.d.ts +6 -13
  170. package/src/lib/utils.js +5 -128
  171. package/src/lib/vm-client/evm-client.js +34 -20
@@ -0,0 +1,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 v6_1 = require("../contracts/abi/v6");
11
+ const ids_1 = require("./ids");
12
+ const errors_1 = require("../errors");
13
+ exports.SESSION_PARAM = (0, viem_1.getAbiItem)({
14
+ abi: v6_1.AGW_ABI,
15
+ name: 'grantRules',
16
+ }).inputs[0];
17
+ const ZERO_SALT = `0x${'00'.repeat(32)}`;
18
+ exports.ENVELOPE_VERSION = 1;
19
+ /** V6 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
@@ -0,0 +1,157 @@
1
+ /** Internal v6 SVM wire representation; not a proposed public Rule API. */
2
+ import { type Hex } from 'viem';
3
+ import { AgenticError } from '../errors';
4
+ import type { AssetCapWire } from './universal-terms';
5
+ export declare enum SvmDataPinMode {
6
+ EQ = 0,
7
+ GTE_LE = 1,
8
+ LTE_LE = 2,
9
+ RATIO_GTE_LE = 3
10
+ }
11
+ export interface AllowedProgramWire {
12
+ program: Hex;
13
+ discriminator: Hex;
14
+ discriminatorLen: number;
15
+ dataless: boolean;
16
+ maxAccounts: number;
17
+ }
18
+ export interface SvmAccountPinWire {
19
+ ruleIndex: number;
20
+ accountIndex: number;
21
+ expected: Hex;
22
+ }
23
+ export interface SvmDataPinWire {
24
+ ruleIndex: number;
25
+ fromEnd: boolean;
26
+ offset: number;
27
+ offsetB: number;
28
+ len: number;
29
+ mode: SvmDataPinMode;
30
+ expected: Hex;
31
+ num: bigint;
32
+ den: bigint;
33
+ }
34
+ export interface SvmTermsWire {
35
+ validUntil: number;
36
+ expectedCEA: Hex;
37
+ gatewayProgram: Hex;
38
+ assets: readonly AssetCapWire[];
39
+ maxGasPerCall: bigint;
40
+ ceaAccounts: readonly Hex[];
41
+ programs: readonly AllowedProgramWire[];
42
+ pins: readonly SvmAccountPinWire[];
43
+ dataPins: readonly SvmDataPinWire[];
44
+ }
45
+ export declare const SVM_LIMITS: {
46
+ readonly assets: 8;
47
+ readonly programs: 32;
48
+ readonly pins: 16;
49
+ readonly dataPins: 8;
50
+ readonly ceaAccounts: 16;
51
+ readonly accounts: 64;
52
+ readonly ixData: 1024;
53
+ };
54
+ export declare const UINT64_MAX: bigint;
55
+ export declare const FORBIDDEN_SVM_PROGRAMS: readonly Hex[];
56
+ export declare const SVM_TERMS_PARAM: {
57
+ readonly type: "tuple";
58
+ readonly components: readonly [{
59
+ readonly name: "validUntil";
60
+ readonly type: "uint48";
61
+ }, {
62
+ readonly name: "expectedCEA";
63
+ readonly type: "bytes32";
64
+ }, {
65
+ readonly name: "gatewayProgram";
66
+ readonly type: "bytes32";
67
+ }, {
68
+ readonly name: "assets";
69
+ readonly type: "tuple[]";
70
+ readonly components: readonly [{
71
+ readonly name: "token";
72
+ readonly type: "address";
73
+ }, {
74
+ readonly name: "maxPerCall";
75
+ readonly type: "uint256";
76
+ }, {
77
+ readonly name: "maxTotal";
78
+ readonly type: "uint256";
79
+ }];
80
+ }, {
81
+ readonly name: "maxGasPerCall";
82
+ readonly type: "uint256";
83
+ }, {
84
+ readonly name: "ceaAccounts";
85
+ readonly type: "bytes32[]";
86
+ }, {
87
+ readonly name: "programs";
88
+ readonly type: "tuple[]";
89
+ readonly components: readonly [{
90
+ readonly name: "program";
91
+ readonly type: "bytes32";
92
+ }, {
93
+ readonly name: "discriminator";
94
+ readonly type: "bytes8";
95
+ }, {
96
+ readonly name: "discriminatorLen";
97
+ readonly type: "uint8";
98
+ }, {
99
+ readonly name: "dataless";
100
+ readonly type: "bool";
101
+ }, {
102
+ readonly name: "maxAccounts";
103
+ readonly type: "uint8";
104
+ }];
105
+ }, {
106
+ readonly name: "pins";
107
+ readonly type: "tuple[]";
108
+ readonly components: readonly [{
109
+ readonly name: "ruleIndex";
110
+ readonly type: "uint8";
111
+ }, {
112
+ readonly name: "accountIndex";
113
+ readonly type: "uint8";
114
+ }, {
115
+ readonly name: "expected";
116
+ readonly type: "bytes32";
117
+ }];
118
+ }, {
119
+ readonly name: "dataPins";
120
+ readonly type: "tuple[]";
121
+ readonly components: readonly [{
122
+ readonly name: "ruleIndex";
123
+ readonly type: "uint8";
124
+ }, {
125
+ readonly name: "fromEnd";
126
+ readonly type: "bool";
127
+ }, {
128
+ readonly name: "offset";
129
+ readonly type: "uint16";
130
+ }, {
131
+ readonly name: "offsetB";
132
+ readonly type: "uint16";
133
+ }, {
134
+ readonly name: "len";
135
+ readonly type: "uint8";
136
+ }, {
137
+ readonly name: "mode";
138
+ readonly type: "uint8";
139
+ }, {
140
+ readonly name: "expected";
141
+ readonly type: "bytes32";
142
+ }, {
143
+ readonly name: "num";
144
+ readonly type: "uint64";
145
+ }, {
146
+ readonly name: "den";
147
+ readonly type: "uint64";
148
+ }];
149
+ }];
150
+ };
151
+ export declare function svmInvalid(message: string): AgenticError;
152
+ export declare function exactHex(value: unknown, bytes: number, field: string): asserts value is Hex;
153
+ export declare function isForbiddenSvmProgram(program: Hex, terms: Pick<SvmTermsWire, 'expectedCEA' | 'gatewayProgram'>): boolean;
154
+ /** Grant-time structural rules. Chain/token/mint correctness requires resolved on-chain inputs. */
155
+ export declare function validateSvmTerms(t: SvmTermsWire, nowSeconds: number): void;
156
+ export declare function encodeSvmTerms(terms: SvmTermsWire, nowSeconds: number): Hex;
157
+ export declare function decodeSvmTerms(body: Hex): SvmTermsWire;