@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,51 @@
1
+ /**
2
+ * THE single home for omitted-limit defaults. Native PC defaults are zero as
3
+ * confirmed on October 7. Nothing else in the SDK may invent
4
+ * a fallback for these fields; update this table for future changes.
5
+ *
6
+ * Explicit zero always stays zero. In the native policy a zero value/amount
7
+ * cap forbids positive value/amount, while maxCalls = 0 means unlimited.
8
+ */
9
+ export declare const UINT256_MAX: bigint;
10
+ export interface DefaultRow {
11
+ value: bigint;
12
+ meaning: string;
13
+ source: 'product' | 'v5';
14
+ }
15
+ export declare const AGENTIC_DEFAULTS: {
16
+ readonly native: {
17
+ readonly maxValuePerCall: {
18
+ readonly value: bigint;
19
+ readonly meaning: "no native value transfer";
20
+ readonly source: "product";
21
+ };
22
+ readonly maxValueTotal: {
23
+ readonly value: bigint;
24
+ readonly meaning: "no native value transfer";
25
+ readonly source: "product";
26
+ };
27
+ readonly amountMaxTotal: {
28
+ readonly value: bigint;
29
+ readonly meaning: "unlimited metered total";
30
+ readonly source: "product";
31
+ };
32
+ readonly maxCalls: {
33
+ readonly value: bigint;
34
+ readonly meaning: "unlimited calls until expiry";
35
+ readonly source: "product";
36
+ };
37
+ };
38
+ readonly universal: {
39
+ readonly assetMaxTotal: {
40
+ readonly value: bigint;
41
+ readonly meaning: "omitted or zero forbids token movement";
42
+ readonly source: "product";
43
+ };
44
+ readonly allowedCallMaxValue: {
45
+ readonly value: bigint;
46
+ readonly meaning: "no native value attached to the destination call";
47
+ readonly source: "product";
48
+ };
49
+ };
50
+ };
51
+ export declare function withDefault(value: bigint | undefined, row: DefaultRow): bigint;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AGENTIC_DEFAULTS = exports.UINT256_MAX = void 0;
4
+ exports.withDefault = withDefault;
5
+ /**
6
+ * THE single home for omitted-limit defaults. Native PC defaults are zero as
7
+ * confirmed on October 7. Nothing else in the SDK may invent
8
+ * a fallback for these fields; update this table for future changes.
9
+ *
10
+ * Explicit zero always stays zero. In the native policy a zero value/amount
11
+ * cap forbids positive value/amount, while maxCalls = 0 means unlimited.
12
+ */
13
+ exports.UINT256_MAX = BigInt(2) ** BigInt(256) - BigInt(1);
14
+ exports.AGENTIC_DEFAULTS = {
15
+ native: {
16
+ maxValuePerCall: {
17
+ value: BigInt(0),
18
+ meaning: 'no native value transfer',
19
+ source: 'product',
20
+ },
21
+ maxValueTotal: {
22
+ value: BigInt(0),
23
+ meaning: 'no native value transfer',
24
+ source: 'product',
25
+ },
26
+ amountMaxTotal: {
27
+ value: exports.UINT256_MAX,
28
+ meaning: 'unlimited metered total',
29
+ source: 'product',
30
+ },
31
+ maxCalls: {
32
+ value: BigInt(0),
33
+ meaning: 'unlimited calls until expiry',
34
+ source: 'product',
35
+ },
36
+ },
37
+ universal: {
38
+ assetMaxTotal: {
39
+ value: BigInt(0),
40
+ meaning: 'omitted or zero forbids token movement',
41
+ source: 'product',
42
+ },
43
+ allowedCallMaxValue: {
44
+ value: BigInt(0),
45
+ meaning: 'no native value attached to the destination call',
46
+ source: 'product',
47
+ },
48
+ },
49
+ };
50
+ function withDefault(value, row) {
51
+ return value === undefined ? row.value : value;
52
+ }
53
+ //# sourceMappingURL=defaults.js.map
@@ -0,0 +1,35 @@
1
+ import { type Address, type Hex } from 'viem';
2
+ /**
3
+ * Pure identity helpers mirroring the pinned AGW generation (v5):
4
+ * smartsessions IdLib.toActionId / toConfigId / toPermissionId and
5
+ * AGWFactory._predict (OZ Clones immutable-args CREATE2).
6
+ * Every input that changes the result is explicit — validator, factory and
7
+ * implementation are internal generation context.
8
+ */
9
+ /** keccak256(abi.encodePacked(address target, bytes4 selector)). */
10
+ export declare function actionId(target: Address, selector: Hex): Hex;
11
+ /** keccak256(abi.encodePacked(wallet, keccak256(abi.encodePacked(rulesId, actionId)))). */
12
+ export declare function configId(wallet: Address, rulesId: Hex, action: Hex): Hex;
13
+ /** sessionValidatorInitData for the agent validator: abi.encode(address agent). */
14
+ export declare function agentConfig(agent: Address): Hex;
15
+ /**
16
+ * The rules ID a grant produces:
17
+ * keccak256(abi.encode(validator, bytes abi.encode(agent), bytes32(grantNonce))).
18
+ * Chain is NOT part of the hash; records are keyed by wallet + rulesId.
19
+ */
20
+ export declare function rulesId(ctx: {
21
+ validator: Address;
22
+ agent: Address;
23
+ grantNonce: bigint;
24
+ }): Hex;
25
+ /**
26
+ * Counterfactual wallet address: CREATE2 by the factory proxy of an OZ clone
27
+ * of `walletImplementation` with immutable args (owner ‖ factory), salt
28
+ * keccak256(abi.encode(owner, uint96 index)).
29
+ */
30
+ export declare function deriveWallet(ctx: {
31
+ factory: Address;
32
+ walletImplementation: Address;
33
+ owner: Address;
34
+ index: bigint | number;
35
+ }): Address;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.actionId = actionId;
4
+ exports.configId = configId;
5
+ exports.agentConfig = agentConfig;
6
+ exports.rulesId = rulesId;
7
+ exports.deriveWallet = deriveWallet;
8
+ const viem_1 = require("viem");
9
+ const errors_1 = require("../errors");
10
+ /**
11
+ * Pure identity helpers mirroring the pinned AGW generation (v5):
12
+ * smartsessions IdLib.toActionId / toConfigId / toPermissionId and
13
+ * AGWFactory._predict (OZ Clones immutable-args CREATE2).
14
+ * Every input that changes the result is explicit — validator, factory and
15
+ * implementation are internal generation context.
16
+ */
17
+ /** keccak256(abi.encodePacked(address target, bytes4 selector)). */
18
+ function actionId(target, selector) {
19
+ return (0, viem_1.keccak256)((0, viem_1.encodePacked)(['address', 'bytes4'], [target, selector]));
20
+ }
21
+ /** keccak256(abi.encodePacked(wallet, keccak256(abi.encodePacked(rulesId, actionId)))). */
22
+ function configId(wallet, rulesId, action) {
23
+ const inner = (0, viem_1.keccak256)((0, viem_1.encodePacked)(['bytes32', 'bytes32'], [rulesId, action]));
24
+ return (0, viem_1.keccak256)((0, viem_1.encodePacked)(['address', 'bytes32'], [wallet, inner]));
25
+ }
26
+ /** sessionValidatorInitData for the agent validator: abi.encode(address agent). */
27
+ function agentConfig(agent) {
28
+ return (0, viem_1.encodeAbiParameters)([{ type: 'address' }], [agent]);
29
+ }
30
+ /**
31
+ * The rules ID a grant produces:
32
+ * keccak256(abi.encode(validator, bytes abi.encode(agent), bytes32(grantNonce))).
33
+ * Chain is NOT part of the hash; records are keyed by wallet + rulesId.
34
+ */
35
+ function rulesId(ctx) {
36
+ if (ctx.grantNonce < BigInt(0) || ctx.grantNonce >= BigInt(2) ** BigInt(64)) {
37
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'grantNonce must fit uint64');
38
+ }
39
+ return (0, viem_1.keccak256)((0, viem_1.encodeAbiParameters)([{ type: 'address' }, { type: 'bytes' }, { type: 'bytes32' }], [
40
+ ctx.validator,
41
+ agentConfig(ctx.agent),
42
+ (0, viem_1.numberToHex)(ctx.grantNonce, { size: 32 }),
43
+ ]));
44
+ }
45
+ const UINT96_MAX = BigInt(2) ** BigInt(96) - BigInt(1);
46
+ /**
47
+ * Counterfactual wallet address: CREATE2 by the factory proxy of an OZ clone
48
+ * of `walletImplementation` with immutable args (owner ‖ factory), salt
49
+ * keccak256(abi.encode(owner, uint96 index)).
50
+ */
51
+ function deriveWallet(ctx) {
52
+ const index = BigInt(ctx.index);
53
+ if (index < BigInt(0) || index > UINT96_MAX) {
54
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, 'wallet index must fit uint96');
55
+ }
56
+ const salt = (0, viem_1.keccak256)((0, viem_1.encodeAbiParameters)([{ type: 'address' }, { type: 'uint96' }], [ctx.owner, index]));
57
+ const args = (0, viem_1.encodePacked)(['address', 'address'], [ctx.owner, ctx.factory]);
58
+ const argsLength = (args.length - 2) / 2;
59
+ const initCode = (0, viem_1.concat)([
60
+ '0x61',
61
+ (0, viem_1.numberToHex)(argsLength + 0x2d, { size: 2 }),
62
+ '0x3d81600a3d39f3363d3d373d3d3d363d73',
63
+ ctx.walletImplementation,
64
+ '0x5af43d82803e903d91602b57fd5bf3',
65
+ args,
66
+ ]);
67
+ return (0, viem_1.getAddress)((0, viem_1.getContractAddress)({
68
+ opcode: 'CREATE2',
69
+ from: ctx.factory,
70
+ salt,
71
+ bytecodeHash: (0, viem_1.keccak256)(initCode),
72
+ }));
73
+ }
74
+ //# sourceMappingURL=ids.js.map
@@ -0,0 +1,75 @@
1
+ import { type AbiParameter, type Address, type Hex } from 'viem';
2
+ import type { NativeAction, NativeRule, NativeSingleRule } from '../agentic.types';
3
+ import { type ParsedSelector } from './selectors';
4
+ /** URP native limits at v5 (Types.sol MAX_PINS, AGW MAX_NATIVE_ACTIONS). */
5
+ export declare const MAX_PINS = 8;
6
+ export declare const MAX_NATIVE_ACTIONS = 8;
7
+ /** NativeTerms (Types.sol:354-363 at v5). */
8
+ export interface NativeTermsWire {
9
+ validUntil: number;
10
+ target: Address;
11
+ selector: Hex;
12
+ maxValuePerCall: bigint;
13
+ maxValueTotal: bigint;
14
+ amount: {
15
+ enabled: boolean;
16
+ offset: number;
17
+ maxPerCall: bigint;
18
+ maxTotal: bigint;
19
+ };
20
+ maxCalls: number;
21
+ pins: readonly {
22
+ offset: number;
23
+ expected: Hex;
24
+ }[];
25
+ }
26
+ export declare const NATIVE_TERMS_PARAM: AbiParameter;
27
+ export interface NativeEncodeContext {
28
+ /** Current unix time in seconds; grants with validUntil <= now are rejected. */
29
+ nowSeconds: number;
30
+ }
31
+ /**
32
+ * The actions a native rule allows: the single-action shorthand, or the
33
+ * explicit `actions` list (1..8). Each carries its effective `validUntil`.
34
+ */
35
+ export declare function nativeActionsOf(rule: NativeRule): (NativeAction & {
36
+ validUntil: number;
37
+ })[];
38
+ /**
39
+ * Normalize and validate every action of a native rule into wire terms, in
40
+ * input order. (target, selector) pairs must be unique — the wallet refuses
41
+ * duplicates (DuplicateAction) because they share one action ID. Pure.
42
+ */
43
+ export declare function nativeRuleToActionTerms(rule: NativeRule, ctx: NativeEncodeContext): {
44
+ terms: NativeTermsWire;
45
+ parsed: ParsedSelector;
46
+ }[];
47
+ /** Single-action convenience; multi-action rules use nativeRuleToActionTerms. */
48
+ export declare function nativeRuleToTerms(rule: NativeRule, ctx: NativeEncodeContext): {
49
+ terms: NativeTermsWire;
50
+ parsed: ParsedSelector;
51
+ };
52
+ /** Normalize and validate one native action into wire terms. Pure. */
53
+ export declare function nativeActionToTerms(rule: NativeAction & {
54
+ validUntil: number;
55
+ }, ctx: NativeEncodeContext): {
56
+ terms: NativeTermsWire;
57
+ parsed: ParsedSelector;
58
+ };
59
+ export declare function encodeNativeTerms(terms: NativeTermsWire): Hex;
60
+ export declare function decodeNativeTerms(body: Hex): NativeTermsWire;
61
+ /**
62
+ * Wire terms back to the public NativeRule shape. Stored terms carry calldata
63
+ * offsets and exact words but no ABI, so pins and amount are returned in the
64
+ * raw `offset` form (never a guessed argument index) and `selector` is the
65
+ * 4-byte hex. The result re-encodes to the same terms.
66
+ */
67
+ export declare function nativeTermsToRule(terms: NativeTermsWire, agent: Address): NativeSingleRule;
68
+ /**
69
+ * Stored action terms back to a public NativeRule: the single-action
70
+ * shorthand for one action, otherwise `actions` in stored order. The rule's
71
+ * `validUntil` is the earliest action expiry; an action expiring later keeps
72
+ * its own `validUntil`, so the result re-encodes to identical terms.
73
+ */
74
+ export declare function nativeActionTermsToRule(termsList: readonly NativeTermsWire[], agent: Address): NativeRule;
75
+ export declare function isUnlimited(value: bigint): boolean;
@@ -0,0 +1,328 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NATIVE_TERMS_PARAM = exports.MAX_NATIVE_ACTIONS = exports.MAX_PINS = void 0;
4
+ exports.nativeActionsOf = nativeActionsOf;
5
+ exports.nativeRuleToActionTerms = nativeRuleToActionTerms;
6
+ exports.nativeRuleToTerms = nativeRuleToTerms;
7
+ exports.nativeActionToTerms = nativeActionToTerms;
8
+ exports.encodeNativeTerms = encodeNativeTerms;
9
+ exports.decodeNativeTerms = decodeNativeTerms;
10
+ exports.nativeTermsToRule = nativeTermsToRule;
11
+ exports.nativeActionTermsToRule = nativeActionTermsToRule;
12
+ exports.isUnlimited = isUnlimited;
13
+ const viem_1 = require("viem");
14
+ const errors_1 = require("../errors");
15
+ const abi_layout_1 = require("./abi-layout");
16
+ const defaults_1 = require("./defaults");
17
+ const selectors_1 = require("./selectors");
18
+ /** URP native limits at v5 (Types.sol MAX_PINS, AGW MAX_NATIVE_ACTIONS). */
19
+ exports.MAX_PINS = 8;
20
+ exports.MAX_NATIVE_ACTIONS = 8;
21
+ const UINT32_MAX = 2 ** 32 - 1;
22
+ const UINT48_MAX = 2 ** 48 - 1;
23
+ exports.NATIVE_TERMS_PARAM = {
24
+ type: 'tuple',
25
+ components: [
26
+ { name: 'validUntil', type: 'uint48' },
27
+ { name: 'target', type: 'address' },
28
+ { name: 'selector', type: 'bytes4' },
29
+ { name: 'maxValuePerCall', type: 'uint256' },
30
+ { name: 'maxValueTotal', type: 'uint256' },
31
+ {
32
+ name: 'amount',
33
+ type: 'tuple',
34
+ components: [
35
+ { name: 'enabled', type: 'bool' },
36
+ { name: 'offset', type: 'uint16' },
37
+ { name: 'maxPerCall', type: 'uint256' },
38
+ { name: 'maxTotal', type: 'uint256' },
39
+ ],
40
+ },
41
+ { name: 'maxCalls', type: 'uint32' },
42
+ {
43
+ name: 'pins',
44
+ type: 'tuple[]',
45
+ components: [
46
+ { name: 'offset', type: 'uint16' },
47
+ { name: 'expected', type: 'bytes32' },
48
+ ],
49
+ },
50
+ ],
51
+ };
52
+ function invalid(message, details) {
53
+ return new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.INVALID_RULE, message, {
54
+ details,
55
+ });
56
+ }
57
+ function assertUint(value, bits, field) {
58
+ if (typeof value !== 'bigint' ||
59
+ value < BigInt(0) ||
60
+ value >= BigInt(2) ** BigInt(bits)) {
61
+ throw invalid(`${field} must be a uint${bits} bigint`);
62
+ }
63
+ }
64
+ const SINGLE_ACTION_FIELDS = [
65
+ 'target',
66
+ 'selector',
67
+ 'maxValuePerCall',
68
+ 'maxValueTotal',
69
+ 'maxCalls',
70
+ 'pins',
71
+ 'amount',
72
+ ];
73
+ /**
74
+ * The actions a native rule allows: the single-action shorthand, or the
75
+ * explicit `actions` list (1..8). Each carries its effective `validUntil`.
76
+ */
77
+ function nativeActionsOf(rule) {
78
+ if (!Number.isInteger(rule.validUntil) ||
79
+ rule.validUntil < 1 ||
80
+ rule.validUntil > UINT48_MAX) {
81
+ throw invalid('rule validUntil must be a positive unix timestamp in seconds (uint48)');
82
+ }
83
+ if (rule.actions !== undefined) {
84
+ if (!Array.isArray(rule.actions) ||
85
+ rule.actions.length < 1 ||
86
+ rule.actions.length > exports.MAX_NATIVE_ACTIONS) {
87
+ throw invalid(`actions must contain 1..${exports.MAX_NATIVE_ACTIONS} entries (the AGW native action limit)`);
88
+ }
89
+ const mixed = SINGLE_ACTION_FIELDS.filter((f) => rule[f] !== undefined);
90
+ if (mixed.length > 0) {
91
+ throw invalid(`actions cannot be combined with top-level ${mixed.join(', ')}; put them inside each action`);
92
+ }
93
+ return rule.actions.map((a, i) => {
94
+ if (!a || typeof a !== 'object' || Array.isArray(a)) {
95
+ throw invalid(`actions[${i}] must be an action object`, { action: i });
96
+ }
97
+ return { ...a, validUntil: a.validUntil ?? rule.validUntil };
98
+ });
99
+ }
100
+ const single = rule;
101
+ return [
102
+ {
103
+ target: single.target,
104
+ selector: single.selector,
105
+ maxValuePerCall: single.maxValuePerCall,
106
+ maxValueTotal: single.maxValueTotal,
107
+ maxCalls: single.maxCalls,
108
+ pins: single.pins,
109
+ amount: single.amount,
110
+ validUntil: single.validUntil,
111
+ },
112
+ ];
113
+ }
114
+ /**
115
+ * Normalize and validate every action of a native rule into wire terms, in
116
+ * input order. (target, selector) pairs must be unique — the wallet refuses
117
+ * duplicates (DuplicateAction) because they share one action ID. Pure.
118
+ */
119
+ function nativeRuleToActionTerms(rule, ctx) {
120
+ if (rule.validUntil <= ctx.nowSeconds) {
121
+ throw invalid('rule validUntil must be a future unix timestamp in seconds (uint48)');
122
+ }
123
+ const actions = nativeActionsOf(rule);
124
+ const multi = rule.actions !== undefined;
125
+ const out = actions.map((action, i) => {
126
+ try {
127
+ return nativeActionToTerms(action, ctx);
128
+ }
129
+ catch (err) {
130
+ if (multi && err instanceof errors_1.AgenticError) {
131
+ throw new errors_1.AgenticError(err.code, `actions[${i}]: ${err.message}`, {
132
+ hint: err.hint,
133
+ details: { ...(err.details ?? {}), action: i },
134
+ cause: err,
135
+ });
136
+ }
137
+ throw err;
138
+ }
139
+ });
140
+ const seen = new Map();
141
+ out.forEach(({ terms }, i) => {
142
+ const key = `${terms.target.toLowerCase()}:${terms.selector}`;
143
+ const first = seen.get(key);
144
+ if (first !== undefined) {
145
+ throw invalid(`actions[${i}] repeats the target and selector of actions[${first}]`, { action: i });
146
+ }
147
+ seen.set(key, i);
148
+ });
149
+ return out;
150
+ }
151
+ /** Single-action convenience; multi-action rules use nativeRuleToActionTerms. */
152
+ function nativeRuleToTerms(rule, ctx) {
153
+ const all = nativeRuleToActionTerms(rule, ctx);
154
+ if (all.length !== 1) {
155
+ throw invalid('nativeRuleToTerms takes a single-action rule');
156
+ }
157
+ return all[0];
158
+ }
159
+ /** Normalize and validate one native action into wire terms. Pure. */
160
+ function nativeActionToTerms(rule, ctx) {
161
+ if (!(0, viem_1.isAddress)(rule.target, { strict: false }) ||
162
+ /^0x0{40}$/i.test(rule.target)) {
163
+ throw invalid('native rule target must be a non-zero address');
164
+ }
165
+ if (!Number.isInteger(rule.validUntil) ||
166
+ rule.validUntil <= ctx.nowSeconds ||
167
+ rule.validUntil > UINT48_MAX) {
168
+ throw invalid('validUntil must be a future unix timestamp in seconds (uint48)', {
169
+ validUntil: rule.validUntil,
170
+ });
171
+ }
172
+ const parsed = (0, selectors_1.parseSelector)(rule.selector);
173
+ const pinsIn = rule.pins ?? [];
174
+ if (parsed.valueOnly && (pinsIn.length > 0 || rule.amount)) {
175
+ throw invalid('a value-only rule cannot carry pins or an amount rule');
176
+ }
177
+ if (pinsIn.length > exports.MAX_PINS) {
178
+ throw invalid(`at most ${exports.MAX_PINS} pins are allowed`, {
179
+ pins: pinsIn.length,
180
+ });
181
+ }
182
+ const pins = pinsIn.map((p, i) => {
183
+ if (p.arg !== undefined && p.offset !== undefined)
184
+ throw invalid('pin arg and offset are mutually exclusive');
185
+ if (p.offset !== undefined) {
186
+ if (typeof p.expected !== 'string' ||
187
+ !(0, viem_1.isHex)(p.expected) ||
188
+ p.expected.length !== 66) {
189
+ throw invalid(`pins[${i}].expected must be the exact 32-byte calldata word in offset form`);
190
+ }
191
+ return {
192
+ offset: (0, abi_layout_1.rawOffset)(p.offset, `pins[${i}]`),
193
+ expected: p.expected.toLowerCase(),
194
+ };
195
+ }
196
+ if (typeof p.arg !== 'number')
197
+ throw invalid('pin requires arg or offset');
198
+ const offset = (0, abi_layout_1.argumentOffset)(parsed.inputs, p.arg, `pins[${i}]`);
199
+ const param = parsed.inputs[p.arg];
200
+ return { offset, expected: (0, abi_layout_1.encodeArgWord)(param, p.expected, `pins[${i}]`) };
201
+ });
202
+ const offsets = new Set(pins.map((p) => p.offset));
203
+ if (offsets.size !== pins.length)
204
+ throw invalid('two pins address the same argument');
205
+ let amount = {
206
+ enabled: false,
207
+ offset: 0,
208
+ maxPerCall: BigInt(0),
209
+ maxTotal: BigInt(0),
210
+ };
211
+ if (rule.amount) {
212
+ let offset;
213
+ if (rule.amount.arg !== undefined && rule.amount.offset !== undefined)
214
+ throw invalid('amount arg and offset are mutually exclusive');
215
+ if (rule.amount.offset !== undefined) {
216
+ offset = (0, abi_layout_1.rawOffset)(rule.amount.offset, 'amount');
217
+ }
218
+ else {
219
+ if (typeof rule.amount.arg !== 'number')
220
+ throw invalid('amount requires arg or offset');
221
+ offset = (0, abi_layout_1.argumentOffset)(parsed.inputs, rule.amount.arg, 'amount');
222
+ const param = parsed.inputs[rule.amount.arg];
223
+ if (!/^uint\d*$/.test(param.type)) {
224
+ throw invalid(`amount argument must be an unsigned integer, got ${param.type}`);
225
+ }
226
+ }
227
+ if (offsets.has(offset))
228
+ throw invalid('amount argument is also pinned');
229
+ assertUint(rule.amount.maxPerCall, 256, 'amount.maxPerCall');
230
+ const maxTotal = (0, defaults_1.withDefault)(rule.amount.maxTotal, defaults_1.AGENTIC_DEFAULTS.native.amountMaxTotal);
231
+ assertUint(maxTotal, 256, 'amount.maxTotal');
232
+ amount = {
233
+ enabled: true,
234
+ offset,
235
+ maxPerCall: rule.amount.maxPerCall,
236
+ maxTotal,
237
+ };
238
+ }
239
+ const maxValuePerCall = (0, defaults_1.withDefault)(rule.maxValuePerCall, defaults_1.AGENTIC_DEFAULTS.native.maxValuePerCall);
240
+ const maxValueTotal = (0, defaults_1.withDefault)(rule.maxValueTotal, defaults_1.AGENTIC_DEFAULTS.native.maxValueTotal);
241
+ assertUint(maxValuePerCall, 256, 'maxValuePerCall');
242
+ assertUint(maxValueTotal, 256, 'maxValueTotal');
243
+ const maxCalls = rule.maxCalls === undefined
244
+ ? Number(defaults_1.AGENTIC_DEFAULTS.native.maxCalls.value)
245
+ : rule.maxCalls;
246
+ if (!Number.isInteger(maxCalls) || maxCalls < 0 || maxCalls > UINT32_MAX) {
247
+ throw invalid('maxCalls must be a uint32 integer');
248
+ }
249
+ return {
250
+ parsed,
251
+ terms: {
252
+ validUntil: rule.validUntil,
253
+ target: (0, viem_1.getAddress)(rule.target),
254
+ selector: parsed.selector,
255
+ maxValuePerCall,
256
+ maxValueTotal,
257
+ amount,
258
+ maxCalls,
259
+ pins,
260
+ },
261
+ };
262
+ }
263
+ function encodeNativeTerms(terms) {
264
+ return (0, viem_1.encodeAbiParameters)([exports.NATIVE_TERMS_PARAM], [terms]);
265
+ }
266
+ function decodeNativeTerms(body) {
267
+ const [terms] = (0, viem_1.decodeAbiParameters)([exports.NATIVE_TERMS_PARAM], body);
268
+ return terms;
269
+ }
270
+ /**
271
+ * Wire terms back to the public NativeRule shape. Stored terms carry calldata
272
+ * offsets and exact words but no ABI, so pins and amount are returned in the
273
+ * raw `offset` form (never a guessed argument index) and `selector` is the
274
+ * 4-byte hex. The result re-encodes to the same terms.
275
+ */
276
+ function nativeTermsToRule(terms, agent) {
277
+ const rule = {
278
+ agent: (0, viem_1.getAddress)(agent),
279
+ target: (0, viem_1.getAddress)(terms.target),
280
+ selector: terms.selector.toLowerCase() === selectors_1.VALUE_ONLY_SELECTOR
281
+ ? 'value-only'
282
+ : terms.selector.toLowerCase(),
283
+ validUntil: Number(terms.validUntil),
284
+ maxValuePerCall: terms.maxValuePerCall,
285
+ maxValueTotal: terms.maxValueTotal,
286
+ maxCalls: Number(terms.maxCalls),
287
+ };
288
+ if (terms.pins.length > 0) {
289
+ rule.pins = terms.pins.map((p) => ({
290
+ offset: Number(p.offset),
291
+ expected: p.expected.toLowerCase(),
292
+ }));
293
+ }
294
+ if (terms.amount.enabled) {
295
+ rule.amount = {
296
+ offset: Number(terms.amount.offset),
297
+ maxPerCall: terms.amount.maxPerCall,
298
+ maxTotal: terms.amount.maxTotal,
299
+ };
300
+ }
301
+ return rule;
302
+ }
303
+ /**
304
+ * Stored action terms back to a public NativeRule: the single-action
305
+ * shorthand for one action, otherwise `actions` in stored order. The rule's
306
+ * `validUntil` is the earliest action expiry; an action expiring later keeps
307
+ * its own `validUntil`, so the result re-encodes to identical terms.
308
+ */
309
+ function nativeActionTermsToRule(termsList, agent) {
310
+ if (termsList.length === 0) {
311
+ throw new errors_1.AgenticError(errors_1.AGENTIC_ERROR_CODE.RULE_READ_FAILED, 'a native rule needs at least one action');
312
+ }
313
+ if (termsList.length === 1)
314
+ return nativeTermsToRule(termsList[0], agent);
315
+ const decoded = termsList.map((t) => nativeTermsToRule(t, agent));
316
+ const validUntil = Math.min(...decoded.map((d) => d.validUntil));
317
+ return {
318
+ agent: (0, viem_1.getAddress)(agent),
319
+ validUntil,
320
+ actions: decoded.map(({ agent: _agent, validUntil: actionValidUntil, ...action }) => actionValidUntil === validUntil
321
+ ? action
322
+ : { ...action, validUntil: actionValidUntil }),
323
+ };
324
+ }
325
+ function isUnlimited(value) {
326
+ return value === defaults_1.UINT256_MAX;
327
+ }
328
+ //# sourceMappingURL=native.js.map
@@ -0,0 +1,45 @@
1
+ import { type Address, type Hex } from 'viem';
2
+ import { AgenticCapability } from '../capabilities';
3
+ import type { InternalRule, UniversalRule, SolanaRule } from '../agentic.types';
4
+ import { type NativeTermsWire } from './native';
5
+ import { type SessionWire } from './session';
6
+ import { type ResolvedUniversalRule } from './universal';
7
+ import { type SvmTermsWire } from './svm-terms';
8
+ import type { UniversalTermsWire } from './universal-terms';
9
+ export declare function isSolanaRule(rule: InternalRule): rule is SolanaRule;
10
+ export declare function isUniversalRule(rule: InternalRule): rule is UniversalRule;
11
+ /** CAIP-2 chain a rule governs: omitted = the connected Push chain. */
12
+ export declare function ruleChain(rule: InternalRule, pushChainNamespace: string): string;
13
+ export declare function chainHash(chainNamespace: string): Hex;
14
+ export interface RuleEncodeContext {
15
+ /** 'eip155:<pushChainId>' of the connected network. */
16
+ pushChainNamespace: string;
17
+ validator: Address;
18
+ rulesPolicy: Address;
19
+ capabilities: ReadonlySet<AgenticCapability>;
20
+ nowSeconds: number;
21
+ gateway?: Address;
22
+ universal?: ReadonlyMap<UniversalRule, ResolvedUniversalRule>;
23
+ svm?: ReadonlyMap<SolanaRule, SvmTermsWire>;
24
+ /**
25
+ * Addresses a native rule may never target (AGW _requireGrantableTarget /
26
+ * URP NativeTargetIsGateway): wallet, engine, policy, validator, factory,
27
+ * gateway. 0x0 and 0x1 are always refused.
28
+ */
29
+ forbiddenTargets?: readonly Address[];
30
+ }
31
+ export interface PreparedRule {
32
+ rule: InternalRule;
33
+ agent: Address;
34
+ chainNamespace: string;
35
+ session: SessionWire;
36
+ /** Native terms, one per action in input order, when the rule is native. */
37
+ nativeTerms?: NativeTermsWire[];
38
+ universalTerms?: UniversalTermsWire;
39
+ svmTerms?: SvmTermsWire;
40
+ }
41
+ /** Validate before signing. V5 permits several grants for the same agent/chain. */
42
+ export declare function prepareRules(rules: readonly InternalRule[], ctx: RuleEncodeContext & {
43
+ /** Wallet owner; when given, an agent equal to it is AGENT_IS_OWNER. */
44
+ owner?: Address;
45
+ }): PreparedRule[];