@agirails/sdk 2.0.1-beta → 2.0.2
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/LICENSE +190 -0
- package/README.md +116 -108
- package/bin/actp +10 -0
- package/dist/ACTPClient.d.ts +456 -33
- package/dist/ACTPClient.d.ts.map +1 -1
- package/dist/ACTPClient.js +477 -93
- package/dist/ACTPClient.js.map +1 -1
- package/dist/abi/AgentRegistry.json +782 -0
- package/dist/abi/EscrowVault.json +106 -38
- package/dist/abi/IdentityRegistry.json +316 -0
- package/dist/adapters/BaseAdapter.d.ts +231 -0
- package/dist/adapters/BaseAdapter.d.ts.map +1 -0
- package/dist/adapters/BaseAdapter.js +393 -0
- package/dist/adapters/BaseAdapter.js.map +1 -0
- package/dist/adapters/BeginnerAdapter.d.ts +152 -0
- package/dist/adapters/BeginnerAdapter.d.ts.map +1 -0
- package/dist/adapters/BeginnerAdapter.js +168 -0
- package/dist/adapters/BeginnerAdapter.js.map +1 -0
- package/dist/adapters/IntermediateAdapter.d.ts +211 -0
- package/dist/adapters/IntermediateAdapter.d.ts.map +1 -0
- package/dist/adapters/IntermediateAdapter.js +260 -0
- package/dist/adapters/IntermediateAdapter.js.map +1 -0
- package/dist/adapters/index.d.ts +15 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +26 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/builders/DeliveryProofBuilder.d.ts +60 -1
- package/dist/builders/DeliveryProofBuilder.d.ts.map +1 -1
- package/dist/builders/DeliveryProofBuilder.js +81 -5
- package/dist/builders/DeliveryProofBuilder.js.map +1 -1
- package/dist/builders/QuoteBuilder.d.ts +101 -0
- package/dist/builders/QuoteBuilder.d.ts.map +1 -1
- package/dist/builders/QuoteBuilder.js +120 -3
- package/dist/builders/QuoteBuilder.js.map +1 -1
- package/dist/builders/index.d.ts +4 -0
- package/dist/builders/index.d.ts.map +1 -1
- package/dist/builders/index.js +4 -0
- package/dist/builders/index.js.map +1 -1
- package/dist/cli/commands/balance.d.ts +13 -0
- package/dist/cli/commands/balance.d.ts.map +1 -0
- package/dist/cli/commands/balance.js +89 -0
- package/dist/cli/commands/balance.js.map +1 -0
- package/dist/cli/commands/batch.d.ts +24 -0
- package/dist/cli/commands/batch.d.ts.map +1 -0
- package/dist/cli/commands/batch.js +424 -0
- package/dist/cli/commands/batch.js.map +1 -0
- package/dist/cli/commands/config.d.ts +13 -0
- package/dist/cli/commands/config.d.ts.map +1 -0
- package/dist/cli/commands/config.js +192 -0
- package/dist/cli/commands/config.js.map +1 -0
- package/dist/cli/commands/init.d.ts +19 -0
- package/dist/cli/commands/init.d.ts.map +1 -0
- package/dist/cli/commands/init.js +143 -0
- package/dist/cli/commands/init.js.map +1 -0
- package/dist/cli/commands/mint.d.ts +13 -0
- package/dist/cli/commands/mint.d.ts.map +1 -0
- package/dist/cli/commands/mint.js +91 -0
- package/dist/cli/commands/mint.js.map +1 -0
- package/dist/cli/commands/pay.d.ts +18 -0
- package/dist/cli/commands/pay.d.ts.map +1 -0
- package/dist/cli/commands/pay.js +87 -0
- package/dist/cli/commands/pay.js.map +1 -0
- package/dist/cli/commands/simulate.d.ts +32 -0
- package/dist/cli/commands/simulate.d.ts.map +1 -0
- package/dist/cli/commands/simulate.js +290 -0
- package/dist/cli/commands/simulate.js.map +1 -0
- package/dist/cli/commands/time.d.ts +29 -0
- package/dist/cli/commands/time.d.ts.map +1 -0
- package/dist/cli/commands/time.js +252 -0
- package/dist/cli/commands/time.js.map +1 -0
- package/dist/cli/commands/tx.d.ts +16 -0
- package/dist/cli/commands/tx.d.ts.map +1 -0
- package/dist/cli/commands/tx.js +379 -0
- package/dist/cli/commands/tx.js.map +1 -0
- package/dist/cli/commands/watch.d.ts +20 -0
- package/dist/cli/commands/watch.d.ts.map +1 -0
- package/dist/cli/commands/watch.js +160 -0
- package/dist/cli/commands/watch.js.map +1 -0
- package/dist/cli/index.d.ts +17 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +104 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/cli/utils/client.d.ts +70 -0
- package/dist/cli/utils/client.d.ts.map +1 -0
- package/dist/cli/utils/client.js +240 -0
- package/dist/cli/utils/client.js.map +1 -0
- package/dist/cli/utils/config.d.ts +91 -0
- package/dist/cli/utils/config.d.ts.map +1 -0
- package/dist/cli/utils/config.js +240 -0
- package/dist/cli/utils/config.js.map +1 -0
- package/dist/cli/utils/output.d.ts +174 -0
- package/dist/cli/utils/output.d.ts.map +1 -0
- package/dist/cli/utils/output.js +380 -0
- package/dist/cli/utils/output.js.map +1 -0
- package/dist/config/networks.d.ts +28 -0
- package/dist/config/networks.d.ts.map +1 -1
- package/dist/config/networks.js +60 -12
- package/dist/config/networks.js.map +1 -1
- package/dist/errors/index.d.ts +165 -2
- package/dist/errors/index.d.ts.map +1 -1
- package/dist/errors/index.js +260 -2
- package/dist/errors/index.js.map +1 -1
- package/dist/index.d.ts +61 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +141 -36
- package/dist/index.js.map +1 -1
- package/dist/level0/Provider.d.ts +106 -0
- package/dist/level0/Provider.d.ts.map +1 -0
- package/dist/level0/Provider.js +10 -0
- package/dist/level0/Provider.js.map +1 -0
- package/dist/level0/ServiceDirectory.d.ts +74 -0
- package/dist/level0/ServiceDirectory.d.ts.map +1 -0
- package/dist/level0/ServiceDirectory.js +122 -0
- package/dist/level0/ServiceDirectory.js.map +1 -0
- package/dist/level0/index.d.ts +10 -0
- package/dist/level0/index.d.ts.map +1 -0
- package/dist/level0/index.js +15 -0
- package/dist/level0/index.js.map +1 -0
- package/dist/level0/provide.d.ts +51 -0
- package/dist/level0/provide.d.ts.map +1 -0
- package/dist/level0/provide.js +113 -0
- package/dist/level0/provide.js.map +1 -0
- package/dist/level0/request.d.ts +53 -0
- package/dist/level0/request.d.ts.map +1 -0
- package/dist/level0/request.js +462 -0
- package/dist/level0/request.js.map +1 -0
- package/dist/level1/Agent.d.ts +472 -0
- package/dist/level1/Agent.d.ts.map +1 -0
- package/dist/level1/Agent.js +1091 -0
- package/dist/level1/Agent.js.map +1 -0
- package/dist/level1/index.d.ts +10 -0
- package/dist/level1/index.d.ts.map +1 -0
- package/dist/level1/index.js +30 -0
- package/dist/level1/index.js.map +1 -0
- package/dist/level1/pricing/PriceCalculator.d.ts +62 -0
- package/dist/level1/pricing/PriceCalculator.d.ts.map +1 -0
- package/dist/level1/pricing/PriceCalculator.js +237 -0
- package/dist/level1/pricing/PriceCalculator.js.map +1 -0
- package/dist/level1/pricing/PricingStrategy.d.ts +179 -0
- package/dist/level1/pricing/PricingStrategy.d.ts.map +1 -0
- package/dist/level1/pricing/PricingStrategy.js +11 -0
- package/dist/level1/pricing/PricingStrategy.js.map +1 -0
- package/dist/level1/types/Job.d.ts +166 -0
- package/dist/level1/types/Job.d.ts.map +1 -0
- package/dist/level1/types/Job.js +11 -0
- package/dist/level1/types/Job.js.map +1 -0
- package/dist/level1/types/Options.d.ts +258 -0
- package/dist/level1/types/Options.d.ts.map +1 -0
- package/dist/level1/types/Options.js +8 -0
- package/dist/level1/types/Options.js.map +1 -0
- package/dist/level1/types/index.d.ts +8 -0
- package/dist/level1/types/index.d.ts.map +1 -0
- package/dist/level1/types/index.js +8 -0
- package/dist/level1/types/index.js.map +1 -0
- package/dist/protocol/ACTPKernel.d.ts +229 -2
- package/dist/protocol/ACTPKernel.d.ts.map +1 -1
- package/dist/protocol/ACTPKernel.js +367 -33
- package/dist/protocol/ACTPKernel.js.map +1 -1
- package/dist/protocol/AgentRegistry.d.ts +177 -0
- package/dist/protocol/AgentRegistry.d.ts.map +1 -0
- package/dist/protocol/AgentRegistry.js +449 -0
- package/dist/protocol/AgentRegistry.js.map +1 -0
- package/dist/protocol/DIDManager.d.ts +289 -0
- package/dist/protocol/DIDManager.d.ts.map +1 -0
- package/dist/protocol/DIDManager.js +481 -0
- package/dist/protocol/DIDManager.js.map +1 -0
- package/dist/protocol/DIDResolver.d.ts +236 -0
- package/dist/protocol/DIDResolver.d.ts.map +1 -0
- package/dist/protocol/DIDResolver.js +495 -0
- package/dist/protocol/DIDResolver.js.map +1 -0
- package/dist/protocol/EASHelper.d.ts +57 -2
- package/dist/protocol/EASHelper.d.ts.map +1 -1
- package/dist/protocol/EASHelper.js +230 -37
- package/dist/protocol/EASHelper.js.map +1 -1
- package/dist/protocol/EscrowVault.d.ts +93 -2
- package/dist/protocol/EscrowVault.d.ts.map +1 -1
- package/dist/protocol/EscrowVault.js +122 -33
- package/dist/protocol/EscrowVault.js.map +1 -1
- package/dist/protocol/EventMonitor.d.ts +45 -1
- package/dist/protocol/EventMonitor.d.ts.map +1 -1
- package/dist/protocol/EventMonitor.js +64 -8
- package/dist/protocol/EventMonitor.js.map +1 -1
- package/dist/protocol/MessageSigner.d.ts +116 -2
- package/dist/protocol/MessageSigner.d.ts.map +1 -1
- package/dist/protocol/MessageSigner.js +215 -9
- package/dist/protocol/MessageSigner.js.map +1 -1
- package/dist/protocol/ProofGenerator.d.ts +93 -0
- package/dist/protocol/ProofGenerator.d.ts.map +1 -1
- package/dist/protocol/ProofGenerator.js +194 -9
- package/dist/protocol/ProofGenerator.js.map +1 -1
- package/dist/protocol/QuoteBuilder.d.ts +8 -0
- package/dist/protocol/QuoteBuilder.d.ts.map +1 -1
- package/dist/protocol/QuoteBuilder.js +8 -0
- package/dist/protocol/QuoteBuilder.js.map +1 -1
- package/dist/runtime/BlockchainRuntime.d.ts +360 -0
- package/dist/runtime/BlockchainRuntime.d.ts.map +1 -0
- package/dist/runtime/BlockchainRuntime.js +767 -0
- package/dist/runtime/BlockchainRuntime.js.map +1 -0
- package/dist/runtime/IACTPRuntime.d.ts +271 -0
- package/dist/runtime/IACTPRuntime.d.ts.map +1 -0
- package/dist/runtime/IACTPRuntime.js +15 -0
- package/dist/runtime/IACTPRuntime.js.map +1 -0
- package/dist/runtime/MockRuntime.d.ts +445 -0
- package/dist/runtime/MockRuntime.d.ts.map +1 -0
- package/dist/runtime/MockRuntime.js +1065 -0
- package/dist/runtime/MockRuntime.js.map +1 -0
- package/dist/runtime/MockStateManager.d.ts +233 -0
- package/dist/runtime/MockStateManager.d.ts.map +1 -0
- package/dist/runtime/MockStateManager.js +533 -0
- package/dist/runtime/MockStateManager.js.map +1 -0
- package/dist/runtime/index.d.ts +14 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +42 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/runtime/types/MockState.d.ts +167 -0
- package/dist/runtime/types/MockState.d.ts.map +1 -0
- package/dist/runtime/types/MockState.js +43 -0
- package/dist/runtime/types/MockState.js.map +1 -0
- package/dist/types/agent.d.ts +76 -0
- package/dist/types/agent.d.ts.map +1 -0
- package/dist/types/agent.js +8 -0
- package/dist/types/agent.js.map +1 -0
- package/dist/types/did.d.ts +192 -0
- package/dist/types/did.d.ts.map +1 -0
- package/dist/types/did.js +38 -0
- package/dist/types/did.js.map +1 -0
- package/dist/types/eip712.d.ts +34 -0
- package/dist/types/eip712.d.ts.map +1 -1
- package/dist/types/eip712.js +31 -5
- package/dist/types/eip712.js.map +1 -1
- package/dist/types/escrow.d.ts +17 -10
- package/dist/types/escrow.d.ts.map +1 -1
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +8 -0
- package/dist/types/index.js.map +1 -1
- package/dist/types/message.d.ts +32 -0
- package/dist/types/message.d.ts.map +1 -1
- package/dist/types/message.js +4 -0
- package/dist/types/message.js.map +1 -1
- package/dist/types/state.d.ts +28 -0
- package/dist/types/state.d.ts.map +1 -1
- package/dist/types/state.js +37 -6
- package/dist/types/state.js.map +1 -1
- package/dist/types/transaction.d.ts +17 -0
- package/dist/types/transaction.d.ts.map +1 -1
- package/dist/utils/ErrorRecoveryGuide.d.ts +125 -0
- package/dist/utils/ErrorRecoveryGuide.d.ts.map +1 -0
- package/dist/utils/ErrorRecoveryGuide.js +579 -0
- package/dist/utils/ErrorRecoveryGuide.js.map +1 -0
- package/dist/utils/Helpers.d.ts +453 -0
- package/dist/utils/Helpers.d.ts.map +1 -0
- package/dist/utils/Helpers.js +623 -0
- package/dist/utils/Helpers.js.map +1 -0
- package/dist/utils/IPFSClient.d.ts +113 -0
- package/dist/utils/IPFSClient.d.ts.map +1 -1
- package/dist/utils/IPFSClient.js +128 -7
- package/dist/utils/IPFSClient.js.map +1 -1
- package/dist/utils/Logger.d.ts +195 -0
- package/dist/utils/Logger.d.ts.map +1 -0
- package/dist/utils/Logger.js +382 -0
- package/dist/utils/Logger.js.map +1 -0
- package/dist/utils/NonceManager.d.ts +234 -1
- package/dist/utils/NonceManager.d.ts.map +1 -1
- package/dist/utils/NonceManager.js +372 -7
- package/dist/utils/NonceManager.js.map +1 -1
- package/dist/utils/RateLimiter.d.ts +253 -0
- package/dist/utils/RateLimiter.d.ts.map +1 -0
- package/dist/utils/RateLimiter.js +424 -0
- package/dist/utils/RateLimiter.js.map +1 -0
- package/dist/utils/ReceivedNonceTracker.d.ts +175 -0
- package/dist/utils/ReceivedNonceTracker.d.ts.map +1 -1
- package/dist/utils/ReceivedNonceTracker.js +261 -5
- package/dist/utils/ReceivedNonceTracker.js.map +1 -1
- package/dist/utils/SDKLifecycle.d.ts +156 -0
- package/dist/utils/SDKLifecycle.d.ts.map +1 -0
- package/dist/utils/SDKLifecycle.js +347 -0
- package/dist/utils/SDKLifecycle.js.map +1 -0
- package/dist/utils/SecureNonce.d.ts +57 -0
- package/dist/utils/SecureNonce.d.ts.map +1 -0
- package/dist/utils/SecureNonce.js +80 -0
- package/dist/utils/SecureNonce.js.map +1 -0
- package/dist/utils/Semaphore.d.ts +123 -0
- package/dist/utils/Semaphore.d.ts.map +1 -0
- package/dist/utils/Semaphore.js +247 -0
- package/dist/utils/Semaphore.js.map +1 -0
- package/dist/utils/UsedAttestationTracker.d.ts +167 -0
- package/dist/utils/UsedAttestationTracker.d.ts.map +1 -0
- package/dist/utils/UsedAttestationTracker.js +309 -0
- package/dist/utils/UsedAttestationTracker.js.map +1 -0
- package/dist/utils/canonicalJson.d.ts +22 -0
- package/dist/utils/canonicalJson.d.ts.map +1 -1
- package/dist/utils/canonicalJson.js +26 -3
- package/dist/utils/canonicalJson.js.map +1 -1
- package/dist/utils/computeTypeHash.d.ts +14 -0
- package/dist/utils/computeTypeHash.d.ts.map +1 -1
- package/dist/utils/computeTypeHash.js +19 -2
- package/dist/utils/computeTypeHash.js.map +1 -1
- package/dist/utils/fsSafe.d.ts +14 -0
- package/dist/utils/fsSafe.d.ts.map +1 -0
- package/dist/utils/fsSafe.js +89 -0
- package/dist/utils/fsSafe.js.map +1 -0
- package/dist/utils/index.d.ts +15 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +51 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/security.d.ts +147 -0
- package/dist/utils/security.d.ts.map +1 -0
- package/dist/utils/security.js +391 -0
- package/dist/utils/security.js.map +1 -0
- package/dist/utils/validation.d.ts +40 -0
- package/dist/utils/validation.d.ts.map +1 -1
- package/dist/utils/validation.js +184 -7
- package/dist/utils/validation.js.map +1 -1
- package/package.json +54 -37
- package/src/ACTPClient.ts +692 -178
- package/src/abi/AgentRegistry.json +782 -0
- package/src/abi/EscrowVault.json +106 -38
- package/src/abi/IdentityRegistry.json +316 -0
- package/src/adapters/BaseAdapter.ts +473 -0
- package/src/adapters/BeginnerAdapter.ts +232 -0
- package/src/adapters/IntermediateAdapter.ts +316 -0
- package/src/adapters/index.ts +25 -0
- package/src/builders/DeliveryProofBuilder.ts +3 -2
- package/src/cli/commands/balance.ts +110 -0
- package/src/cli/commands/batch.ts +487 -0
- package/src/cli/commands/config.ts +231 -0
- package/src/cli/commands/init.ts +161 -0
- package/src/cli/commands/mint.ts +116 -0
- package/src/cli/commands/pay.ts +113 -0
- package/src/cli/commands/simulate.ts +345 -0
- package/src/cli/commands/time.ts +303 -0
- package/src/cli/commands/tx.ts +448 -0
- package/src/cli/commands/watch.ts +211 -0
- package/src/cli/index.ts +116 -0
- package/src/cli/utils/client.ts +249 -0
- package/src/cli/utils/config.ts +282 -0
- package/src/cli/utils/output.ts +465 -0
- package/src/config/networks.ts +32 -9
- package/src/errors/index.ts +298 -1
- package/src/index.ts +207 -71
- package/src/level0/Provider.ts +117 -0
- package/src/level0/ServiceDirectory.ts +131 -0
- package/src/level0/index.ts +10 -0
- package/src/level0/provide.ts +131 -0
- package/src/level0/request.ts +494 -0
- package/src/level1/Agent.ts +1432 -0
- package/src/level1/index.ts +10 -0
- package/src/level1/pricing/PriceCalculator.ts +255 -0
- package/src/level1/pricing/PricingStrategy.ts +198 -0
- package/src/level1/types/Job.ts +179 -0
- package/src/level1/types/Options.ts +291 -0
- package/src/level1/types/index.ts +8 -0
- package/src/protocol/ACTPKernel.ts +175 -23
- package/src/protocol/AgentRegistry.ts +559 -0
- package/src/protocol/DIDManager.ts +629 -0
- package/src/protocol/DIDResolver.ts +554 -0
- package/src/protocol/EASHelper.ts +230 -46
- package/src/protocol/EscrowVault.ts +68 -50
- package/src/protocol/EventMonitor.ts +44 -15
- package/src/protocol/MessageSigner.ts +193 -13
- package/src/protocol/ProofGenerator.ts +223 -4
- package/src/runtime/BlockchainRuntime.ts +993 -0
- package/src/runtime/IACTPRuntime.ts +284 -0
- package/src/runtime/MockRuntime.ts +1244 -0
- package/src/runtime/MockStateManager.ts +576 -0
- package/src/runtime/index.ts +25 -0
- package/src/runtime/types/MockState.ts +227 -0
- package/src/types/agent.ts +79 -0
- package/src/types/did.ts +223 -0
- package/src/types/escrow.ts +12 -11
- package/src/types/index.ts +5 -1
- package/src/types/state.ts +12 -3
- package/src/types/transaction.ts +4 -1
- package/src/utils/ErrorRecoveryGuide.ts +675 -0
- package/src/utils/Helpers.ts +688 -0
- package/src/utils/IPFSClient.ts +122 -5
- package/src/utils/Logger.ts +484 -0
- package/src/utils/NonceManager.ts +305 -8
- package/src/utils/RateLimiter.ts +534 -0
- package/src/utils/ReceivedNonceTracker.ts +170 -0
- package/src/utils/SDKLifecycle.ts +416 -0
- package/src/utils/SecureNonce.ts +78 -0
- package/src/utils/Semaphore.ts +276 -0
- package/src/utils/UsedAttestationTracker.ts +387 -0
- package/src/utils/fsSafe.ts +75 -0
- package/src/utils/index.ts +80 -0
- package/src/utils/security.ts +418 -0
- package/src/utils/validation.ts +164 -0
- package/src/__tests__/ProofGenerator.test.ts +0 -124
- package/src/__tests__/QuoteBuilder.test.ts +0 -516
- package/src/__tests__/StateMachine.test.ts +0 -82
- package/src/__tests__/builders/DeliveryProofBuilder.test.ts +0 -581
- package/src/__tests__/integration/ACTPClient.test.ts +0 -263
- package/src/__tests__/integration.test.ts +0 -289
- package/src/__tests__/protocol/EASHelper.test.ts +0 -472
- package/src/__tests__/protocol/EventMonitor.test.ts +0 -382
- package/src/__tests__/security/ACTPKernel.security.test.ts +0 -1167
- package/src/__tests__/security/EscrowVault.security.test.ts +0 -570
- package/src/__tests__/security/MessageSigner.security.test.ts +0 -286
- package/src/__tests__/security/NonceReplay.security.test.ts +0 -501
- package/src/__tests__/security/validation.security.test.ts +0 -376
- package/src/__tests__/utils/IPFSClient.test.ts +0 -262
- package/src/__tests__/utils/NonceManager.test.ts +0 -205
- package/src/__tests__/utils/canonicalJson.test.ts +0 -153
package/src/cli/index.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* ACTP CLI - Agent Commerce Transaction Protocol Command Line Interface
|
|
4
|
+
*
|
|
5
|
+
* The ACTP CLI is designed for AI agents - not humans clicking buttons.
|
|
6
|
+
* Key differentiators:
|
|
7
|
+
*
|
|
8
|
+
* 1. **Machine-readable output**: JSON by default for scripting
|
|
9
|
+
* 2. **Pipe-friendly**: Commands output just what you need
|
|
10
|
+
* 3. **Structured exit codes**: 0=success, 1=error, 2=pending
|
|
11
|
+
* 4. **Agent-first features**: watch, simulate, batch
|
|
12
|
+
*
|
|
13
|
+
* @module cli
|
|
14
|
+
* @see https://docs.agirails.io/cli
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { Command } from 'commander';
|
|
18
|
+
import { readFileSync } from 'fs';
|
|
19
|
+
import { join } from 'path';
|
|
20
|
+
|
|
21
|
+
// Read version from package.json (works in both src and dist)
|
|
22
|
+
function getVersion(): string {
|
|
23
|
+
try {
|
|
24
|
+
// Try dist location first (when running compiled)
|
|
25
|
+
const distPath = join(__dirname, '../../package.json');
|
|
26
|
+
const pkg = JSON.parse(readFileSync(distPath, 'utf-8'));
|
|
27
|
+
return pkg.version;
|
|
28
|
+
} catch {
|
|
29
|
+
try {
|
|
30
|
+
// Fallback to src location (when running ts-node)
|
|
31
|
+
const srcPath = join(__dirname, '../../../package.json');
|
|
32
|
+
const pkg = JSON.parse(readFileSync(srcPath, 'utf-8'));
|
|
33
|
+
return pkg.version;
|
|
34
|
+
} catch {
|
|
35
|
+
return '0.0.0'; // Fallback if package.json not found
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Import commands
|
|
41
|
+
import { createInitCommand } from './commands/init';
|
|
42
|
+
import { createPayCommand } from './commands/pay';
|
|
43
|
+
import { createTxCommand } from './commands/tx';
|
|
44
|
+
import { createBalanceCommand } from './commands/balance';
|
|
45
|
+
import { createMintCommand } from './commands/mint';
|
|
46
|
+
import { createConfigCommand } from './commands/config';
|
|
47
|
+
import { createWatchCommand } from './commands/watch';
|
|
48
|
+
import { createSimulateCommand } from './commands/simulate';
|
|
49
|
+
import { createBatchCommand } from './commands/batch';
|
|
50
|
+
import { createTimeCommand } from './commands/time';
|
|
51
|
+
|
|
52
|
+
// ============================================================================
|
|
53
|
+
// Program Setup
|
|
54
|
+
// ============================================================================
|
|
55
|
+
|
|
56
|
+
const program = new Command();
|
|
57
|
+
|
|
58
|
+
program
|
|
59
|
+
.name('actp')
|
|
60
|
+
.description(
|
|
61
|
+
'ACTP CLI - Agent Commerce Transaction Protocol\n\n' +
|
|
62
|
+
'The payment layer for AI agents. Create escrow-backed transactions,\n' +
|
|
63
|
+
'track state changes, and settle payments programmatically.\n\n' +
|
|
64
|
+
'Quick Start:\n' +
|
|
65
|
+
' $ actp init Initialize in current directory\n' +
|
|
66
|
+
' $ actp pay 0xProvider 100 Create a payment\n' +
|
|
67
|
+
' $ actp tx status <txId> Check transaction status\n\n' +
|
|
68
|
+
'Output Modes:\n' +
|
|
69
|
+
' --json Machine-readable JSON output\n' +
|
|
70
|
+
' --quiet Minimal output (just the essential value)'
|
|
71
|
+
)
|
|
72
|
+
.version(getVersion(), '-v, --version', 'Output the version number')
|
|
73
|
+
.helpOption('-h, --help', 'Display help for command');
|
|
74
|
+
|
|
75
|
+
// ============================================================================
|
|
76
|
+
// Register Commands
|
|
77
|
+
// ============================================================================
|
|
78
|
+
|
|
79
|
+
// Core commands (most used)
|
|
80
|
+
program.addCommand(createInitCommand());
|
|
81
|
+
program.addCommand(createPayCommand());
|
|
82
|
+
program.addCommand(createTxCommand());
|
|
83
|
+
program.addCommand(createBalanceCommand());
|
|
84
|
+
program.addCommand(createMintCommand());
|
|
85
|
+
program.addCommand(createConfigCommand());
|
|
86
|
+
|
|
87
|
+
// Agent-first features
|
|
88
|
+
program.addCommand(createWatchCommand());
|
|
89
|
+
program.addCommand(createSimulateCommand());
|
|
90
|
+
program.addCommand(createBatchCommand());
|
|
91
|
+
|
|
92
|
+
// Mock mode utilities
|
|
93
|
+
program.addCommand(createTimeCommand());
|
|
94
|
+
|
|
95
|
+
// ============================================================================
|
|
96
|
+
// Error Handling
|
|
97
|
+
// ============================================================================
|
|
98
|
+
|
|
99
|
+
// Handle unknown commands
|
|
100
|
+
program.on('command:*', (operands) => {
|
|
101
|
+
console.error(`Error: Unknown command '${operands[0]}'`);
|
|
102
|
+
console.error('');
|
|
103
|
+
console.error('Run "actp --help" for available commands.');
|
|
104
|
+
process.exit(1);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// ============================================================================
|
|
108
|
+
// Parse & Execute
|
|
109
|
+
// ============================================================================
|
|
110
|
+
|
|
111
|
+
program.parse(process.argv);
|
|
112
|
+
|
|
113
|
+
// If no arguments, show help
|
|
114
|
+
if (process.argv.length === 2) {
|
|
115
|
+
program.outputHelp();
|
|
116
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK Client Factory for CLI
|
|
3
|
+
*
|
|
4
|
+
* Creates and manages ACTPClient instances based on CLI configuration.
|
|
5
|
+
* Handles mode-specific initialization and error mapping.
|
|
6
|
+
*
|
|
7
|
+
* SECURITY: Error messages are sanitized to prevent information disclosure.
|
|
8
|
+
*
|
|
9
|
+
* @module cli/utils/client
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import * as os from 'os';
|
|
13
|
+
import { ACTPClient, ACTPClientConfig } from '../../ACTPClient';
|
|
14
|
+
import { loadConfig, validateConfigForMode } from './config';
|
|
15
|
+
|
|
16
|
+
// ============================================================================
|
|
17
|
+
// Security: Path Sanitization
|
|
18
|
+
// ============================================================================
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Sanitize a file path for safe display in error messages.
|
|
22
|
+
*
|
|
23
|
+
* SECURITY: Replaces home directory with ~ to prevent disclosure of:
|
|
24
|
+
* - Full home directory paths
|
|
25
|
+
* - Usernames embedded in paths
|
|
26
|
+
* - System-specific directory structures
|
|
27
|
+
*
|
|
28
|
+
* @param fullPath - The full file path to sanitize
|
|
29
|
+
* @returns Sanitized path safe for display
|
|
30
|
+
*/
|
|
31
|
+
function sanitizePath(fullPath: string): string {
|
|
32
|
+
const home = os.homedir();
|
|
33
|
+
// Replace home directory with ~ to avoid disclosing username/path
|
|
34
|
+
return fullPath.replace(home, '~');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// ============================================================================
|
|
38
|
+
// Client Factory
|
|
39
|
+
// ============================================================================
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Create an ACTPClient instance from CLI configuration
|
|
43
|
+
*
|
|
44
|
+
* Reads configuration from .actp/config.json and creates
|
|
45
|
+
* an appropriate client instance for the configured mode.
|
|
46
|
+
*
|
|
47
|
+
* @param projectRoot - Project root directory (defaults to cwd)
|
|
48
|
+
* @returns Initialized ACTPClient
|
|
49
|
+
* @throws Error if config invalid or client creation fails
|
|
50
|
+
*/
|
|
51
|
+
export async function createClient(
|
|
52
|
+
projectRoot: string = process.cwd()
|
|
53
|
+
): Promise<ACTPClient> {
|
|
54
|
+
// Load configuration
|
|
55
|
+
const config = loadConfig(projectRoot);
|
|
56
|
+
|
|
57
|
+
// Validate config for the mode
|
|
58
|
+
validateConfigForMode(config);
|
|
59
|
+
|
|
60
|
+
// Build client config
|
|
61
|
+
const clientConfig: ACTPClientConfig = {
|
|
62
|
+
mode: config.mode,
|
|
63
|
+
requesterAddress: config.address,
|
|
64
|
+
stateDirectory: projectRoot,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// Create and return client
|
|
68
|
+
return ACTPClient.create(clientConfig);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// ============================================================================
|
|
72
|
+
// Error Mapping
|
|
73
|
+
// ============================================================================
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Error codes for CLI errors
|
|
77
|
+
*/
|
|
78
|
+
export const ErrorCode = {
|
|
79
|
+
// Configuration errors
|
|
80
|
+
NOT_INITIALIZED: 'NOT_INITIALIZED',
|
|
81
|
+
CONFIG_CORRUPTED: 'CONFIG_CORRUPTED',
|
|
82
|
+
INVALID_CONFIG: 'INVALID_CONFIG',
|
|
83
|
+
|
|
84
|
+
// Input validation errors
|
|
85
|
+
INVALID_ADDRESS: 'INVALID_ADDRESS',
|
|
86
|
+
INVALID_AMOUNT: 'INVALID_AMOUNT',
|
|
87
|
+
INVALID_DEADLINE: 'INVALID_DEADLINE',
|
|
88
|
+
INVALID_TX_ID: 'INVALID_TX_ID',
|
|
89
|
+
|
|
90
|
+
// Transaction errors
|
|
91
|
+
TX_NOT_FOUND: 'TX_NOT_FOUND',
|
|
92
|
+
INVALID_STATE_TRANSITION: 'INVALID_STATE_TRANSITION',
|
|
93
|
+
INSUFFICIENT_BALANCE: 'INSUFFICIENT_BALANCE',
|
|
94
|
+
DEADLINE_PASSED: 'DEADLINE_PASSED',
|
|
95
|
+
DISPUTE_WINDOW_ACTIVE: 'DISPUTE_WINDOW_ACTIVE',
|
|
96
|
+
|
|
97
|
+
// System errors
|
|
98
|
+
FILE_LOCK_ERROR: 'FILE_LOCK_ERROR',
|
|
99
|
+
NETWORK_ERROR: 'NETWORK_ERROR',
|
|
100
|
+
UNKNOWN_ERROR: 'UNKNOWN_ERROR',
|
|
101
|
+
} as const;
|
|
102
|
+
|
|
103
|
+
export type ErrorCodeValue = (typeof ErrorCode)[keyof typeof ErrorCode];
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Structured CLI error with code and details
|
|
107
|
+
*/
|
|
108
|
+
export interface StructuredError {
|
|
109
|
+
code: ErrorCodeValue;
|
|
110
|
+
message: string;
|
|
111
|
+
details?: Record<string, unknown>;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Map SDK errors to CLI-friendly structured errors.
|
|
116
|
+
*
|
|
117
|
+
* SECURITY: Sanitizes paths and stack traces to prevent information disclosure.
|
|
118
|
+
*/
|
|
119
|
+
export function mapError(error: unknown): StructuredError {
|
|
120
|
+
const err = error as Error;
|
|
121
|
+
// Sanitize the message to remove full paths
|
|
122
|
+
const rawMessage = err?.message || String(error);
|
|
123
|
+
const message = sanitizePath(rawMessage);
|
|
124
|
+
|
|
125
|
+
// Configuration errors
|
|
126
|
+
if (message.includes('not initialized') || message.includes('actp init')) {
|
|
127
|
+
return {
|
|
128
|
+
code: ErrorCode.NOT_INITIALIZED,
|
|
129
|
+
message: 'ACTP not initialized. Run "actp init" first.',
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (message.includes('Config file corrupted')) {
|
|
134
|
+
return {
|
|
135
|
+
code: ErrorCode.CONFIG_CORRUPTED,
|
|
136
|
+
message: 'Configuration file is corrupted.',
|
|
137
|
+
details: { suggestion: 'Run "actp init --force" to reinitialize.' },
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Transaction errors (from SDK error classes)
|
|
142
|
+
if (err?.name === 'TransactionNotFoundError' || message.includes('Transaction not found')) {
|
|
143
|
+
const txIdMatch = message.match(/Transaction not found: (0x[a-fA-F0-9]+)/);
|
|
144
|
+
return {
|
|
145
|
+
code: ErrorCode.TX_NOT_FOUND,
|
|
146
|
+
message: 'Transaction not found.',
|
|
147
|
+
details: txIdMatch ? { txId: txIdMatch[1] } : undefined,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (err?.name === 'InvalidStateTransitionError' || message.includes('Invalid state transition')) {
|
|
152
|
+
return {
|
|
153
|
+
code: ErrorCode.INVALID_STATE_TRANSITION,
|
|
154
|
+
message: 'Invalid state transition.',
|
|
155
|
+
// SECURITY: Don't expose original message which may contain sensitive info
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (err?.name === 'InsufficientBalanceError' || message.includes('Insufficient balance')) {
|
|
160
|
+
return {
|
|
161
|
+
code: ErrorCode.INSUFFICIENT_BALANCE,
|
|
162
|
+
message: 'Insufficient balance for this operation.',
|
|
163
|
+
// SECURITY: Don't expose balance details
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (err?.name === 'DeadlinePassedError' || message.includes('Deadline passed')) {
|
|
168
|
+
return {
|
|
169
|
+
code: ErrorCode.DEADLINE_PASSED,
|
|
170
|
+
message: 'Transaction deadline has passed.',
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (err?.name === 'DisputeWindowActiveError' || message.includes('Dispute window')) {
|
|
175
|
+
return {
|
|
176
|
+
code: ErrorCode.DISPUTE_WINDOW_ACTIVE,
|
|
177
|
+
message: 'Dispute window is still active.',
|
|
178
|
+
// SECURITY: Don't expose window timing details
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Validation errors
|
|
183
|
+
if (err?.name === 'ValidationError') {
|
|
184
|
+
if (message.includes('address')) {
|
|
185
|
+
return {
|
|
186
|
+
code: ErrorCode.INVALID_ADDRESS,
|
|
187
|
+
message: sanitizePath(message),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
if (message.includes('amount') || message.includes('Amount')) {
|
|
191
|
+
return {
|
|
192
|
+
code: ErrorCode.INVALID_AMOUNT,
|
|
193
|
+
message: sanitizePath(message),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
if (message.includes('deadline') || message.includes('Deadline')) {
|
|
197
|
+
return {
|
|
198
|
+
code: ErrorCode.INVALID_DEADLINE,
|
|
199
|
+
message: sanitizePath(message),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Lock errors
|
|
205
|
+
if (message.includes('lock') || message.includes('ELOCKED')) {
|
|
206
|
+
return {
|
|
207
|
+
code: ErrorCode.FILE_LOCK_ERROR,
|
|
208
|
+
message: 'Could not acquire lock. Another process may be using ACTP.',
|
|
209
|
+
details: { suggestion: 'Wait a moment and try again.' },
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Default: unknown error
|
|
214
|
+
// SECURITY: Do NOT include stack traces in production - they reveal internal paths
|
|
215
|
+
return {
|
|
216
|
+
code: ErrorCode.UNKNOWN_ERROR,
|
|
217
|
+
message: sanitizePath(message) || 'An unknown error occurred.',
|
|
218
|
+
// SECURITY FIX: Stack traces removed to prevent information disclosure
|
|
219
|
+
// In development mode, errors can be logged separately if needed
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// ============================================================================
|
|
224
|
+
// Utilities
|
|
225
|
+
// ============================================================================
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Validate transaction ID format
|
|
229
|
+
*/
|
|
230
|
+
export function isValidTxId(txId: string): boolean {
|
|
231
|
+
return /^0x[a-fA-F0-9]{64}$/.test(txId);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Format address for display (truncate middle)
|
|
236
|
+
*/
|
|
237
|
+
export function formatAddress(address: string, length: number = 10): string {
|
|
238
|
+
if (address.length <= length * 2 + 2) {
|
|
239
|
+
return address;
|
|
240
|
+
}
|
|
241
|
+
return `${address.slice(0, length + 2)}...${address.slice(-length)}`;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Format transaction ID for display (truncate)
|
|
246
|
+
*/
|
|
247
|
+
export function formatTxId(txId: string, length: number = 8): string {
|
|
248
|
+
return `${txId.slice(0, length + 2)}...`;
|
|
249
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI Configuration Management
|
|
3
|
+
*
|
|
4
|
+
* Handles reading/writing CLI configuration stored in .actp/config.json.
|
|
5
|
+
* Supports mock, testnet, and mainnet modes with mode-specific validation.
|
|
6
|
+
*
|
|
7
|
+
* @module cli/utils/config
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import * as fs from 'fs';
|
|
11
|
+
import * as path from 'path';
|
|
12
|
+
|
|
13
|
+
// ============================================================================
|
|
14
|
+
// Types
|
|
15
|
+
// ============================================================================
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* CLI operating mode
|
|
19
|
+
*/
|
|
20
|
+
export type CLIMode = 'mock' | 'testnet' | 'mainnet';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* CLI configuration stored in .actp/config.json
|
|
24
|
+
*/
|
|
25
|
+
export interface CLIConfig {
|
|
26
|
+
/** Operating mode */
|
|
27
|
+
mode: CLIMode;
|
|
28
|
+
|
|
29
|
+
/** User's Ethereum address */
|
|
30
|
+
address: string;
|
|
31
|
+
|
|
32
|
+
/** Optional: Private key for testnet/mainnet mode */
|
|
33
|
+
privateKey?: string;
|
|
34
|
+
|
|
35
|
+
/** Optional: RPC URL override */
|
|
36
|
+
rpcUrl?: string;
|
|
37
|
+
|
|
38
|
+
/** Configuration version for migrations */
|
|
39
|
+
version: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Default configuration values
|
|
44
|
+
*/
|
|
45
|
+
export const CONFIG_DEFAULTS: CLIConfig = {
|
|
46
|
+
mode: 'mock',
|
|
47
|
+
address: '',
|
|
48
|
+
version: '1.0',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// ============================================================================
|
|
52
|
+
// Paths
|
|
53
|
+
// ============================================================================
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Get the .actp directory path
|
|
57
|
+
*/
|
|
58
|
+
export function getActpDir(projectRoot: string = process.cwd()): string {
|
|
59
|
+
return path.join(projectRoot, '.actp');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Get the config.json path
|
|
64
|
+
*/
|
|
65
|
+
export function getConfigPath(projectRoot: string = process.cwd()): string {
|
|
66
|
+
return path.join(getActpDir(projectRoot), 'config.json');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// ============================================================================
|
|
70
|
+
// Config Operations
|
|
71
|
+
// ============================================================================
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Check if ACTP is initialized in the current directory
|
|
75
|
+
*/
|
|
76
|
+
export function isInitialized(projectRoot: string = process.cwd()): boolean {
|
|
77
|
+
return fs.existsSync(getConfigPath(projectRoot));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Load configuration from disk
|
|
82
|
+
*
|
|
83
|
+
* SECURITY: Warns about private key storage in config file.
|
|
84
|
+
*
|
|
85
|
+
* @throws Error if config doesn't exist or is corrupted
|
|
86
|
+
*/
|
|
87
|
+
export function loadConfig(projectRoot: string = process.cwd()): CLIConfig {
|
|
88
|
+
const configPath = getConfigPath(projectRoot);
|
|
89
|
+
|
|
90
|
+
if (!fs.existsSync(configPath)) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
'ACTP not initialized in this directory.\n' +
|
|
93
|
+
'Run "actp init" to initialize.'
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
const raw = fs.readFileSync(configPath, 'utf-8');
|
|
99
|
+
const config = JSON.parse(raw) as CLIConfig;
|
|
100
|
+
|
|
101
|
+
// Validate required fields
|
|
102
|
+
if (!config.mode) {
|
|
103
|
+
throw new Error('Config missing required field: mode');
|
|
104
|
+
}
|
|
105
|
+
if (!config.address) {
|
|
106
|
+
throw new Error('Config missing required field: address');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// SECURITY FIX (L-2): Warn about private key storage in config file
|
|
110
|
+
if (config.privateKey) {
|
|
111
|
+
console.warn('\x1b[33m%s\x1b[0m', 'WARNING: Private key stored in config file.');
|
|
112
|
+
console.warn('\x1b[33m%s\x1b[0m', ' Consider using ACTP_PRIVATE_KEY environment variable instead.');
|
|
113
|
+
console.warn('\x1b[33m%s\x1b[0m', ' Run: export ACTP_PRIVATE_KEY=<your-key>');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return config;
|
|
117
|
+
} catch (error) {
|
|
118
|
+
if (error instanceof SyntaxError) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
`Config file corrupted: ${configPath}\n` +
|
|
121
|
+
'Delete it manually or run "actp init --force"'
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
throw error;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Save configuration to disk
|
|
130
|
+
*
|
|
131
|
+
* Creates .actp directory if it doesn't exist.
|
|
132
|
+
* Uses atomic write (temp file + rename).
|
|
133
|
+
*/
|
|
134
|
+
export function saveConfig(
|
|
135
|
+
config: CLIConfig,
|
|
136
|
+
projectRoot: string = process.cwd()
|
|
137
|
+
): void {
|
|
138
|
+
const actpDir = getActpDir(projectRoot);
|
|
139
|
+
const configPath = getConfigPath(projectRoot);
|
|
140
|
+
const tempPath = `${configPath}.tmp`;
|
|
141
|
+
|
|
142
|
+
// Ensure .actp directory exists
|
|
143
|
+
if (!fs.existsSync(actpDir)) {
|
|
144
|
+
fs.mkdirSync(actpDir, { recursive: true, mode: 0o755 });
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
// Atomic write: write to temp, then rename
|
|
149
|
+
const json = JSON.stringify(config, null, 2);
|
|
150
|
+
fs.writeFileSync(tempPath, json, { encoding: 'utf-8', mode: 0o600 }); // Secure permissions
|
|
151
|
+
fs.renameSync(tempPath, configPath);
|
|
152
|
+
} catch (error) {
|
|
153
|
+
// Clean up temp file on error
|
|
154
|
+
if (fs.existsSync(tempPath)) {
|
|
155
|
+
try {
|
|
156
|
+
fs.unlinkSync(tempPath);
|
|
157
|
+
} catch {
|
|
158
|
+
// Ignore cleanup errors
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Update specific config values
|
|
167
|
+
*
|
|
168
|
+
* Loads existing config, merges updates, and saves.
|
|
169
|
+
*/
|
|
170
|
+
export function updateConfig(
|
|
171
|
+
updates: Partial<CLIConfig>,
|
|
172
|
+
projectRoot: string = process.cwd()
|
|
173
|
+
): CLIConfig {
|
|
174
|
+
const config = loadConfig(projectRoot);
|
|
175
|
+
const updated = { ...config, ...updates };
|
|
176
|
+
saveConfig(updated, projectRoot);
|
|
177
|
+
return updated;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Get a specific config value
|
|
182
|
+
*/
|
|
183
|
+
export function getConfigValue<K extends keyof CLIConfig>(
|
|
184
|
+
key: K,
|
|
185
|
+
projectRoot: string = process.cwd()
|
|
186
|
+
): CLIConfig[K] {
|
|
187
|
+
const config = loadConfig(projectRoot);
|
|
188
|
+
return config[key];
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Set a specific config value
|
|
193
|
+
*/
|
|
194
|
+
export function setConfigValue<K extends keyof CLIConfig>(
|
|
195
|
+
key: K,
|
|
196
|
+
value: CLIConfig[K],
|
|
197
|
+
projectRoot: string = process.cwd()
|
|
198
|
+
): void {
|
|
199
|
+
updateConfig({ [key]: value } as Partial<CLIConfig>, projectRoot);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// ============================================================================
|
|
203
|
+
// Validation
|
|
204
|
+
// ============================================================================
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Validate Ethereum address format
|
|
208
|
+
*/
|
|
209
|
+
export function validateAddress(address: string): boolean {
|
|
210
|
+
return /^0x[a-fA-F0-9]{40}$/.test(address);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Validate private key format (64 hex chars, optionally with 0x prefix)
|
|
215
|
+
*/
|
|
216
|
+
export function validatePrivateKey(key: string): boolean {
|
|
217
|
+
// Remove 0x prefix if present
|
|
218
|
+
const normalized = key.startsWith('0x') ? key.slice(2) : key;
|
|
219
|
+
return /^[a-fA-F0-9]{64}$/.test(normalized);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Validate config for the specified mode
|
|
224
|
+
*
|
|
225
|
+
* @throws Error if config is invalid for the mode
|
|
226
|
+
*/
|
|
227
|
+
export function validateConfigForMode(config: CLIConfig): void {
|
|
228
|
+
// All modes require a valid address
|
|
229
|
+
if (!validateAddress(config.address)) {
|
|
230
|
+
throw new Error(
|
|
231
|
+
`Invalid address: "${config.address}"\n` +
|
|
232
|
+
'Expected 0x-prefixed 40-character hex string.'
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Testnet and mainnet require private key
|
|
237
|
+
if (config.mode !== 'mock') {
|
|
238
|
+
if (!config.privateKey) {
|
|
239
|
+
throw new Error(
|
|
240
|
+
`Private key required for ${config.mode} mode.\n` +
|
|
241
|
+
'Run: actp config set privateKey <your-private-key>'
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (!validatePrivateKey(config.privateKey)) {
|
|
246
|
+
throw new Error(
|
|
247
|
+
'Invalid private key format.\n' +
|
|
248
|
+
'Expected 64-character hex string (with or without 0x prefix).'
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ============================================================================
|
|
255
|
+
// Gitignore Management
|
|
256
|
+
// ============================================================================
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Add .actp to .gitignore if not already present
|
|
260
|
+
*/
|
|
261
|
+
export function addToGitignore(projectRoot: string = process.cwd()): void {
|
|
262
|
+
const gitignorePath = path.join(projectRoot, '.gitignore');
|
|
263
|
+
|
|
264
|
+
let content = '';
|
|
265
|
+
if (fs.existsSync(gitignorePath)) {
|
|
266
|
+
content = fs.readFileSync(gitignorePath, 'utf-8');
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Check if .actp is already in gitignore
|
|
270
|
+
if (content.includes('.actp')) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Add .actp to gitignore
|
|
275
|
+
const newContent =
|
|
276
|
+
content +
|
|
277
|
+
(content.endsWith('\n') ? '' : '\n') +
|
|
278
|
+
'# ACTP local state (contains mock blockchain state)\n' +
|
|
279
|
+
'.actp/\n';
|
|
280
|
+
|
|
281
|
+
fs.writeFileSync(gitignorePath, newContent, 'utf-8');
|
|
282
|
+
}
|