cronos-agent-wallet 1.2.4 → 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
@@ -97,15 +97,24 @@ async function main() {
97
97
  | `trustedFacilitators` | `string[]` | No | List of Gateway URLs to trust (e.g., localhost). |
98
98
 
99
99
  ## 🛡️ Security Workflow (Strict Mode)
100
-
101
- When `strictPolicy` is `true`, you must register your configuration hash on-chain whenever you change limits.
102
-
100
+
101
+ When `strictPolicy` is `true` (default), you must register your configuration hash on-chain whenever you change limits.
102
+
103
103
  1. **Define Limits**: Set `dailyLimit` in your code.
104
- 2. **Seal Policy**: Run the setup script to write the hash to the `AgentPolicyRegistry`.
105
- ```bash
106
- npx ts-node set_policy.ts
107
- ```
108
- 3. **Run Agent**: The Agent verifies `Local Hash == On-Chain Hash` before making any payment.
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.
109
118
 
110
119
  ## API Reference
111
120
 
@@ -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/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.4",
3
+ "version": "1.2.5",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [