@agirails/sdk 2.2.3 → 2.3.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 (93) hide show
  1. package/README.md +20 -23
  2. package/dist/ACTPClient.d.ts +7 -0
  3. package/dist/ACTPClient.d.ts.map +1 -1
  4. package/dist/ACTPClient.js +56 -1
  5. package/dist/ACTPClient.js.map +1 -1
  6. package/dist/abi/AgentRegistry.json +133 -0
  7. package/dist/adapters/X402Adapter.d.ts +34 -7
  8. package/dist/adapters/X402Adapter.d.ts.map +1 -1
  9. package/dist/adapters/X402Adapter.js +36 -8
  10. package/dist/adapters/X402Adapter.js.map +1 -1
  11. package/dist/adapters/index.d.ts +1 -1
  12. package/dist/adapters/index.d.ts.map +1 -1
  13. package/dist/adapters/index.js.map +1 -1
  14. package/dist/cli/commands/diff.d.ts +11 -0
  15. package/dist/cli/commands/diff.d.ts.map +1 -0
  16. package/dist/cli/commands/diff.js +115 -0
  17. package/dist/cli/commands/diff.js.map +1 -0
  18. package/dist/cli/commands/init.d.ts.map +1 -1
  19. package/dist/cli/commands/init.js +51 -2
  20. package/dist/cli/commands/init.js.map +1 -1
  21. package/dist/cli/commands/publish.d.ts +11 -0
  22. package/dist/cli/commands/publish.d.ts.map +1 -0
  23. package/dist/cli/commands/publish.js +170 -0
  24. package/dist/cli/commands/publish.js.map +1 -0
  25. package/dist/cli/commands/pull.d.ts +12 -0
  26. package/dist/cli/commands/pull.d.ts.map +1 -0
  27. package/dist/cli/commands/pull.js +99 -0
  28. package/dist/cli/commands/pull.js.map +1 -0
  29. package/dist/cli/index.js +7 -0
  30. package/dist/cli/index.js.map +1 -1
  31. package/dist/config/agirailsmd.d.ts +94 -0
  32. package/dist/config/agirailsmd.d.ts.map +1 -0
  33. package/dist/config/agirailsmd.js +209 -0
  34. package/dist/config/agirailsmd.js.map +1 -0
  35. package/dist/config/networks.d.ts +2 -0
  36. package/dist/config/networks.d.ts.map +1 -1
  37. package/dist/config/networks.js +10 -4
  38. package/dist/config/networks.js.map +1 -1
  39. package/dist/config/publishPipeline.d.ts +61 -0
  40. package/dist/config/publishPipeline.d.ts.map +1 -0
  41. package/dist/config/publishPipeline.js +192 -0
  42. package/dist/config/publishPipeline.js.map +1 -0
  43. package/dist/config/syncOperations.d.ts +67 -0
  44. package/dist/config/syncOperations.d.ts.map +1 -0
  45. package/dist/config/syncOperations.js +208 -0
  46. package/dist/config/syncOperations.js.map +1 -0
  47. package/dist/index.d.ts +1 -0
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +7 -3
  50. package/dist/index.js.map +1 -1
  51. package/dist/level0/request.d.ts.map +1 -1
  52. package/dist/level0/request.js +23 -86
  53. package/dist/level0/request.js.map +1 -1
  54. package/dist/level1/Agent.d.ts +0 -11
  55. package/dist/level1/Agent.d.ts.map +1 -1
  56. package/dist/level1/Agent.js +15 -32
  57. package/dist/level1/Agent.js.map +1 -1
  58. package/dist/registry/AgentRegistryClient.d.ts +75 -0
  59. package/dist/registry/AgentRegistryClient.d.ts.map +1 -0
  60. package/dist/registry/AgentRegistryClient.js +160 -0
  61. package/dist/registry/AgentRegistryClient.js.map +1 -0
  62. package/dist/types/adapter.d.ts +39 -0
  63. package/dist/types/adapter.d.ts.map +1 -1
  64. package/dist/types/adapter.js +7 -0
  65. package/dist/types/adapter.js.map +1 -1
  66. package/dist/types/x402.d.ts +23 -0
  67. package/dist/types/x402.d.ts.map +1 -1
  68. package/dist/types/x402.js.map +1 -1
  69. package/dist/wallet/keystore.d.ts +16 -0
  70. package/dist/wallet/keystore.d.ts.map +1 -0
  71. package/dist/wallet/keystore.js +132 -0
  72. package/dist/wallet/keystore.js.map +1 -0
  73. package/package.json +2 -1
  74. package/src/ACTPClient.ts +63 -1
  75. package/src/abi/AgentRegistry.json +133 -0
  76. package/src/adapters/X402Adapter.ts +94 -16
  77. package/src/adapters/index.ts +9 -1
  78. package/src/cli/commands/diff.ts +141 -0
  79. package/src/cli/commands/init.ts +65 -4
  80. package/src/cli/commands/publish.ts +209 -0
  81. package/src/cli/commands/pull.ts +124 -0
  82. package/src/cli/index.ts +8 -0
  83. package/src/config/agirailsmd.ts +262 -0
  84. package/src/config/networks.ts +12 -4
  85. package/src/config/publishPipeline.ts +276 -0
  86. package/src/config/syncOperations.ts +279 -0
  87. package/src/index.ts +3 -0
  88. package/src/level0/request.ts +27 -88
  89. package/src/level1/Agent.ts +16 -32
  90. package/src/registry/AgentRegistryClient.ts +202 -0
  91. package/src/types/adapter.ts +14 -0
  92. package/src/types/x402.ts +32 -0
  93. package/src/wallet/keystore.ts +119 -0
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Keystore auto-resolution for ACTP wallets.
3
+ *
4
+ * Resolution order:
5
+ * 1. ACTP_PRIVATE_KEY env var (backward compat, highest priority)
6
+ * 2. .actp/keystore.json decrypted with ACTP_KEY_PASSWORD
7
+ * 3. undefined (caller decides what to do)
8
+ */
9
+ import * as fs from 'fs';
10
+ import * as path from 'path';
11
+ import { Wallet } from 'ethers';
12
+
13
+ // Cache keyed by resolved keystorePath to support multiple stateDirectories
14
+ const _cache = new Map<string, { key: string; address: string }>();
15
+
16
+ // Separate cache for env-var-resolved key (no path dependency)
17
+ let _envCache: { key: string; address: string } | null = null;
18
+
19
+ /**
20
+ * Validate that stateDirectory doesn't escape expected boundaries.
21
+ * Guards against path traversal when stateDirectory comes from user input.
22
+ */
23
+ function validateStateDirectory(stateDirectory: string): void {
24
+ if (stateDirectory.includes('\0')) {
25
+ throw new Error('Invalid stateDirectory: null byte detected');
26
+ }
27
+ // Reject raw '..' in the input (before normalization resolves it)
28
+ // Catches both relative traversal (../../etc) and embedded traversal (/tmp/../../etc)
29
+ if (stateDirectory.includes('..')) {
30
+ throw new Error('Invalid stateDirectory: path traversal detected (..)');
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Validate and normalize a raw private key string.
36
+ * Trims whitespace and verifies 0x-prefixed 64-char hex format.
37
+ */
38
+ function validateRawKey(raw: string, source: string): string {
39
+ const trimmed = raw.trim();
40
+ if (!/^0x[0-9a-fA-F]{64}$/.test(trimmed)) {
41
+ throw new Error(
42
+ `Invalid private key from ${source}: expected 0x-prefixed 64-char hex string`
43
+ );
44
+ }
45
+ return trimmed;
46
+ }
47
+
48
+ /**
49
+ * Auto-resolve private key: env var → keystore → undefined.
50
+ * Never logs or prints the key itself.
51
+ */
52
+ export async function resolvePrivateKey(
53
+ stateDirectory?: string
54
+ ): Promise<string | undefined> {
55
+ // 1. Env var (highest priority, backward compat)
56
+ if (process.env.ACTP_PRIVATE_KEY) {
57
+ if (_envCache) return _envCache.key;
58
+
59
+ const key = validateRawKey(process.env.ACTP_PRIVATE_KEY, 'ACTP_PRIVATE_KEY env var');
60
+ const address = new Wallet(key).address;
61
+ _envCache = { key, address };
62
+ return key;
63
+ }
64
+
65
+ // 2. Resolve keystore path
66
+ if (stateDirectory) {
67
+ validateStateDirectory(stateDirectory);
68
+ }
69
+ const actpDir = stateDirectory
70
+ ? path.join(stateDirectory, '.actp')
71
+ : path.join(process.cwd(), '.actp');
72
+ const keystorePath = path.resolve(actpDir, 'keystore.json');
73
+
74
+ // 3. Cache hit (keyed by resolved path)
75
+ const cached = _cache.get(keystorePath);
76
+ if (cached) return cached.key;
77
+
78
+ // 4. Keystore file
79
+ if (!fs.existsSync(keystorePath)) return undefined;
80
+
81
+ const password = process.env.ACTP_KEY_PASSWORD;
82
+ if (!password) {
83
+ throw new Error(
84
+ 'Keystore found at ' + keystorePath + ' but ACTP_KEY_PASSWORD is not set.\n' +
85
+ 'Set it: export ACTP_KEY_PASSWORD="your-password"'
86
+ );
87
+ }
88
+
89
+ const keystore = fs.readFileSync(keystorePath, 'utf-8');
90
+ const wallet = await Wallet.fromEncryptedJson(keystore, password);
91
+
92
+ _cache.set(keystorePath, { key: wallet.privateKey, address: wallet.address });
93
+ return wallet.privateKey;
94
+ }
95
+
96
+ /**
97
+ * Get cached address from last resolvePrivateKey() call.
98
+ * Works for both env-var and keystore resolution paths.
99
+ */
100
+ export function getCachedAddress(stateDirectory?: string): string | undefined {
101
+ // Env var path
102
+ if (_envCache) return _envCache.address;
103
+
104
+ // Keystore path — look up by resolved path
105
+ const actpDir = stateDirectory
106
+ ? path.join(stateDirectory, '.actp')
107
+ : path.join(process.cwd(), '.actp');
108
+ const keystorePath = path.resolve(actpDir, 'keystore.json');
109
+ return _cache.get(keystorePath)?.address;
110
+ }
111
+
112
+ /**
113
+ * Clear all cached keys and addresses (for testing).
114
+ * @internal
115
+ */
116
+ export function _clearCache(): void {
117
+ _cache.clear();
118
+ _envCache = null;
119
+ }