cronos-agent-wallet 1.2.3 → 1.2.5

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.
package/README.md CHANGED
@@ -90,9 +90,32 @@ async function main() {
90
90
  | `chainId` | `number` | Yes | Chain ID (e.g., 338). Sent to backend for negotiation. |
91
91
  | `usdcAddress` | `string` | Yes | ERC20 Token Address used for payment. |
92
92
  | `dailyLimit` | `number` | No | Max USDC allowed to spend per 24h. Default: 1.0 |
93
+ | `strictPolicy` | `boolean` | No | If `true`, Agent crashes if local config hash != on-chain hash. Default: `true`. |
94
+ | `anchors` | `object` | No | On-chain registry addresses. Auto-filled for Cronos Testnet. |
95
+ | `analyticsUrl` | `string` | No | URL for centralized logging of payment decisions (e.g. `https://api.myapp.com/analytics`). |
93
96
  | `allowedMerchants` | `string[]` | No | List of Merchant IDs to trust. If empty, allows all. |
94
97
  | `trustedFacilitators` | `string[]` | No | List of Gateway URLs to trust (e.g., localhost). |
95
98
 
99
+ ## 🛡️ Security Workflow (Strict Mode)
100
+
101
+ When `strictPolicy` is `true` (default), you must register your configuration hash on-chain whenever you change limits.
102
+
103
+ 1. **Define Limits**: Set `dailyLimit` in your code.
104
+ 2. **Seal Policy**: Use the Admin helper to write the hash to the chain.
105
+
106
+ ```typescript
107
+ import { AgentAdmin } from "@cronos-merchant/sdk";
108
+
109
+ await AgentAdmin.setPolicy({
110
+ privateKey: process.env.AGENT_KEY
111
+ }, {
112
+ dailyLimit: 0.5,
113
+ maxPerTransaction: 0.5
114
+ });
115
+ ```
116
+
117
+ 3. **Run Agent**: The Agent checks `Local Limit == On-Chain Limit` before spending.
118
+
96
119
  ## API Reference
97
120
 
98
121
  ### `agent.fetch<T>(url, options): Promise<T>`
@@ -0,0 +1,18 @@
1
+ export interface AdminConfig {
2
+ privateKey: string;
3
+ rpcUrl?: string;
4
+ }
5
+ export declare class AgentAdmin {
6
+ /**
7
+ * Sets the on-chain policy anchor for an agent.
8
+ * Use this to "Seal" your local configuration.
9
+ */
10
+ static setPolicy(config: AdminConfig, limits: {
11
+ dailyLimit: number;
12
+ maxPerTransaction: number;
13
+ }): Promise<string>;
14
+ /**
15
+ * Registers a new merchant ID on-chain.
16
+ */
17
+ static registerMerchant(config: AdminConfig, merchantId: string): Promise<string>;
18
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AgentAdmin = void 0;
4
+ const ethers_1 = require("ethers");
5
+ const config_1 = require("./config");
6
+ // Minimal ABIs required for setup
7
+ const POLICY_REGISTRY_ABI = [
8
+ "function setPolicy(uint256 dailySpendLimit, uint256 maxPerTransaction, bytes32 policyHash) external",
9
+ "function getPolicy(address agentAddress) external view returns (uint256, uint256, bytes32, bool, uint256)"
10
+ ];
11
+ const MERCHANT_REGISTRY_ABI = [
12
+ "function registerMerchant(string calldata merchantId, string calldata metadataURI) external",
13
+ "function getMerchant(string calldata merchantId) external view returns (address, bool, string)"
14
+ ];
15
+ class AgentAdmin {
16
+ /**
17
+ * Sets the on-chain policy anchor for an agent.
18
+ * Use this to "Seal" your local configuration.
19
+ */
20
+ static async setPolicy(config, limits) {
21
+ const rpc = config.rpcUrl || "https://evm-t3.cronos.org";
22
+ const provider = new ethers_1.ethers.JsonRpcProvider(rpc);
23
+ const wallet = new ethers_1.ethers.Wallet(config.privateKey, provider);
24
+ const registry = new ethers_1.ethers.Contract(config_1.AGENT_CONFIG_DEFAULTS.anchors.agentPolicyRegistry, POLICY_REGISTRY_ABI, wallet);
25
+ // 1. Calculate Hash (Must match AgentWallet logic)
26
+ const policyData = {
27
+ dailyLimit: limits.dailyLimit || 0,
28
+ maxPerTransaction: limits.maxPerTransaction || 0
29
+ };
30
+ const policyHash = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes(JSON.stringify(policyData)));
31
+ // 2. Prepare visual limits (18 decimals for display)
32
+ const dailyLimitFn = BigInt(Math.floor(limits.dailyLimit * 1e18));
33
+ const maxPerTxFn = BigInt(Math.floor(limits.maxPerTransaction * 1e18));
34
+ console.log(`[AgentAdmin] Setting Policy for ${wallet.address}...`);
35
+ console.log(`[AgentAdmin] Hash: ${policyHash}`);
36
+ const tx = await registry.setPolicy(dailyLimitFn, maxPerTxFn, policyHash);
37
+ console.log(`[AgentAdmin] Tx Sent: ${tx.hash}`);
38
+ await tx.wait();
39
+ console.log(`[AgentAdmin] ✅ Policy Secured.`);
40
+ return tx.hash;
41
+ }
42
+ /**
43
+ * Registers a new merchant ID on-chain.
44
+ */
45
+ static async registerMerchant(config, merchantId) {
46
+ const rpc = config.rpcUrl || "https://evm-t3.cronos.org";
47
+ const provider = new ethers_1.ethers.JsonRpcProvider(rpc);
48
+ const wallet = new ethers_1.ethers.Wallet(config.privateKey, provider);
49
+ const registry = new ethers_1.ethers.Contract(config_1.AGENT_CONFIG_DEFAULTS.anchors.merchantRegistry, MERCHANT_REGISTRY_ABI, wallet);
50
+ console.log(`[AgentAdmin] Registering Merchant '${merchantId}'...`);
51
+ // Check exist
52
+ const existing = await registry.getMerchant(merchantId);
53
+ if (existing[0] !== ethers_1.ethers.ZeroAddress) {
54
+ throw new Error(`Merchant '${merchantId}' is already registered to ${existing[0]}`);
55
+ }
56
+ const tx = await registry.registerMerchant(merchantId, "ipfs://placeholder");
57
+ console.log(`[AgentAdmin] Tx Sent: ${tx.hash}`);
58
+ await tx.wait();
59
+ console.log(`[AgentAdmin] ✅ Merchant Registered.`);
60
+ return tx.hash;
61
+ }
62
+ }
63
+ exports.AgentAdmin = AgentAdmin;
package/dist/config.d.ts CHANGED
@@ -23,4 +23,5 @@ export declare const AGENT_CONFIG_DEFAULTS: {
23
23
  agentPolicyRegistry: string;
24
24
  policyVerifier: string;
25
25
  };
26
+ strictPolicy: boolean;
26
27
  };
package/dist/config.js CHANGED
@@ -7,5 +7,6 @@ exports.AGENT_CONFIG_DEFAULTS = {
7
7
  merchantRegistry: "0x1948175dDB81DA08a4cf17BE4E0C95B97dD11F5c",
8
8
  agentPolicyRegistry: "0xce3b58c9ae8CA4724d6FA8684d2Cb89546FF4E43",
9
9
  policyVerifier: "0xFCb2D2279256B62A1E4E07BCDed26B6546bBc33b"
10
- }
10
+ },
11
+ strictPolicy: true // 🛡️ ENFORCE: Agent will fail if policy hash mismatches chain
11
12
  };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  export { AgentClient } from "./AgentClient";
2
2
  export type { AgentConfig } from "./config";
3
+ export { AGENT_CONFIG_DEFAULTS } from "./config";
3
4
  export { AgentError } from "./errors";
5
+ export { AgentAdmin } from "./AgentAdmin";
package/dist/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AgentError = exports.AgentClient = void 0;
3
+ exports.AgentAdmin = exports.AgentError = exports.AGENT_CONFIG_DEFAULTS = exports.AgentClient = void 0;
4
4
  var AgentClient_1 = require("./AgentClient");
5
5
  Object.defineProperty(exports, "AgentClient", { enumerable: true, get: function () { return AgentClient_1.AgentClient; } });
6
+ var config_1 = require("./config");
7
+ Object.defineProperty(exports, "AGENT_CONFIG_DEFAULTS", { enumerable: true, get: function () { return config_1.AGENT_CONFIG_DEFAULTS; } });
6
8
  var errors_1 = require("./errors");
7
9
  Object.defineProperty(exports, "AgentError", { enumerable: true, get: function () { return errors_1.AgentError; } });
10
+ var AgentAdmin_1 = require("./AgentAdmin");
11
+ Object.defineProperty(exports, "AgentAdmin", { enumerable: true, get: function () { return AgentAdmin_1.AgentAdmin; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cronos-agent-wallet",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [