@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.
Files changed (405) hide show
  1. package/LICENSE +190 -0
  2. package/README.md +116 -108
  3. package/bin/actp +10 -0
  4. package/dist/ACTPClient.d.ts +456 -33
  5. package/dist/ACTPClient.d.ts.map +1 -1
  6. package/dist/ACTPClient.js +477 -93
  7. package/dist/ACTPClient.js.map +1 -1
  8. package/dist/abi/AgentRegistry.json +782 -0
  9. package/dist/abi/EscrowVault.json +106 -38
  10. package/dist/abi/IdentityRegistry.json +316 -0
  11. package/dist/adapters/BaseAdapter.d.ts +231 -0
  12. package/dist/adapters/BaseAdapter.d.ts.map +1 -0
  13. package/dist/adapters/BaseAdapter.js +393 -0
  14. package/dist/adapters/BaseAdapter.js.map +1 -0
  15. package/dist/adapters/BeginnerAdapter.d.ts +152 -0
  16. package/dist/adapters/BeginnerAdapter.d.ts.map +1 -0
  17. package/dist/adapters/BeginnerAdapter.js +168 -0
  18. package/dist/adapters/BeginnerAdapter.js.map +1 -0
  19. package/dist/adapters/IntermediateAdapter.d.ts +211 -0
  20. package/dist/adapters/IntermediateAdapter.d.ts.map +1 -0
  21. package/dist/adapters/IntermediateAdapter.js +260 -0
  22. package/dist/adapters/IntermediateAdapter.js.map +1 -0
  23. package/dist/adapters/index.d.ts +15 -0
  24. package/dist/adapters/index.d.ts.map +1 -0
  25. package/dist/adapters/index.js +26 -0
  26. package/dist/adapters/index.js.map +1 -0
  27. package/dist/builders/DeliveryProofBuilder.d.ts +60 -1
  28. package/dist/builders/DeliveryProofBuilder.d.ts.map +1 -1
  29. package/dist/builders/DeliveryProofBuilder.js +81 -5
  30. package/dist/builders/DeliveryProofBuilder.js.map +1 -1
  31. package/dist/builders/QuoteBuilder.d.ts +101 -0
  32. package/dist/builders/QuoteBuilder.d.ts.map +1 -1
  33. package/dist/builders/QuoteBuilder.js +120 -3
  34. package/dist/builders/QuoteBuilder.js.map +1 -1
  35. package/dist/builders/index.d.ts +4 -0
  36. package/dist/builders/index.d.ts.map +1 -1
  37. package/dist/builders/index.js +4 -0
  38. package/dist/builders/index.js.map +1 -1
  39. package/dist/cli/commands/balance.d.ts +13 -0
  40. package/dist/cli/commands/balance.d.ts.map +1 -0
  41. package/dist/cli/commands/balance.js +89 -0
  42. package/dist/cli/commands/balance.js.map +1 -0
  43. package/dist/cli/commands/batch.d.ts +24 -0
  44. package/dist/cli/commands/batch.d.ts.map +1 -0
  45. package/dist/cli/commands/batch.js +424 -0
  46. package/dist/cli/commands/batch.js.map +1 -0
  47. package/dist/cli/commands/config.d.ts +13 -0
  48. package/dist/cli/commands/config.d.ts.map +1 -0
  49. package/dist/cli/commands/config.js +192 -0
  50. package/dist/cli/commands/config.js.map +1 -0
  51. package/dist/cli/commands/init.d.ts +19 -0
  52. package/dist/cli/commands/init.d.ts.map +1 -0
  53. package/dist/cli/commands/init.js +143 -0
  54. package/dist/cli/commands/init.js.map +1 -0
  55. package/dist/cli/commands/mint.d.ts +13 -0
  56. package/dist/cli/commands/mint.d.ts.map +1 -0
  57. package/dist/cli/commands/mint.js +91 -0
  58. package/dist/cli/commands/mint.js.map +1 -0
  59. package/dist/cli/commands/pay.d.ts +18 -0
  60. package/dist/cli/commands/pay.d.ts.map +1 -0
  61. package/dist/cli/commands/pay.js +87 -0
  62. package/dist/cli/commands/pay.js.map +1 -0
  63. package/dist/cli/commands/simulate.d.ts +32 -0
  64. package/dist/cli/commands/simulate.d.ts.map +1 -0
  65. package/dist/cli/commands/simulate.js +290 -0
  66. package/dist/cli/commands/simulate.js.map +1 -0
  67. package/dist/cli/commands/time.d.ts +29 -0
  68. package/dist/cli/commands/time.d.ts.map +1 -0
  69. package/dist/cli/commands/time.js +252 -0
  70. package/dist/cli/commands/time.js.map +1 -0
  71. package/dist/cli/commands/tx.d.ts +16 -0
  72. package/dist/cli/commands/tx.d.ts.map +1 -0
  73. package/dist/cli/commands/tx.js +379 -0
  74. package/dist/cli/commands/tx.js.map +1 -0
  75. package/dist/cli/commands/watch.d.ts +20 -0
  76. package/dist/cli/commands/watch.d.ts.map +1 -0
  77. package/dist/cli/commands/watch.js +160 -0
  78. package/dist/cli/commands/watch.js.map +1 -0
  79. package/dist/cli/index.d.ts +17 -0
  80. package/dist/cli/index.d.ts.map +1 -0
  81. package/dist/cli/index.js +104 -0
  82. package/dist/cli/index.js.map +1 -0
  83. package/dist/cli/utils/client.d.ts +70 -0
  84. package/dist/cli/utils/client.d.ts.map +1 -0
  85. package/dist/cli/utils/client.js +240 -0
  86. package/dist/cli/utils/client.js.map +1 -0
  87. package/dist/cli/utils/config.d.ts +91 -0
  88. package/dist/cli/utils/config.d.ts.map +1 -0
  89. package/dist/cli/utils/config.js +240 -0
  90. package/dist/cli/utils/config.js.map +1 -0
  91. package/dist/cli/utils/output.d.ts +174 -0
  92. package/dist/cli/utils/output.d.ts.map +1 -0
  93. package/dist/cli/utils/output.js +380 -0
  94. package/dist/cli/utils/output.js.map +1 -0
  95. package/dist/config/networks.d.ts +28 -0
  96. package/dist/config/networks.d.ts.map +1 -1
  97. package/dist/config/networks.js +60 -12
  98. package/dist/config/networks.js.map +1 -1
  99. package/dist/errors/index.d.ts +165 -2
  100. package/dist/errors/index.d.ts.map +1 -1
  101. package/dist/errors/index.js +260 -2
  102. package/dist/errors/index.js.map +1 -1
  103. package/dist/index.d.ts +61 -13
  104. package/dist/index.d.ts.map +1 -1
  105. package/dist/index.js +141 -36
  106. package/dist/index.js.map +1 -1
  107. package/dist/level0/Provider.d.ts +106 -0
  108. package/dist/level0/Provider.d.ts.map +1 -0
  109. package/dist/level0/Provider.js +10 -0
  110. package/dist/level0/Provider.js.map +1 -0
  111. package/dist/level0/ServiceDirectory.d.ts +74 -0
  112. package/dist/level0/ServiceDirectory.d.ts.map +1 -0
  113. package/dist/level0/ServiceDirectory.js +122 -0
  114. package/dist/level0/ServiceDirectory.js.map +1 -0
  115. package/dist/level0/index.d.ts +10 -0
  116. package/dist/level0/index.d.ts.map +1 -0
  117. package/dist/level0/index.js +15 -0
  118. package/dist/level0/index.js.map +1 -0
  119. package/dist/level0/provide.d.ts +51 -0
  120. package/dist/level0/provide.d.ts.map +1 -0
  121. package/dist/level0/provide.js +113 -0
  122. package/dist/level0/provide.js.map +1 -0
  123. package/dist/level0/request.d.ts +53 -0
  124. package/dist/level0/request.d.ts.map +1 -0
  125. package/dist/level0/request.js +462 -0
  126. package/dist/level0/request.js.map +1 -0
  127. package/dist/level1/Agent.d.ts +472 -0
  128. package/dist/level1/Agent.d.ts.map +1 -0
  129. package/dist/level1/Agent.js +1091 -0
  130. package/dist/level1/Agent.js.map +1 -0
  131. package/dist/level1/index.d.ts +10 -0
  132. package/dist/level1/index.d.ts.map +1 -0
  133. package/dist/level1/index.js +30 -0
  134. package/dist/level1/index.js.map +1 -0
  135. package/dist/level1/pricing/PriceCalculator.d.ts +62 -0
  136. package/dist/level1/pricing/PriceCalculator.d.ts.map +1 -0
  137. package/dist/level1/pricing/PriceCalculator.js +237 -0
  138. package/dist/level1/pricing/PriceCalculator.js.map +1 -0
  139. package/dist/level1/pricing/PricingStrategy.d.ts +179 -0
  140. package/dist/level1/pricing/PricingStrategy.d.ts.map +1 -0
  141. package/dist/level1/pricing/PricingStrategy.js +11 -0
  142. package/dist/level1/pricing/PricingStrategy.js.map +1 -0
  143. package/dist/level1/types/Job.d.ts +166 -0
  144. package/dist/level1/types/Job.d.ts.map +1 -0
  145. package/dist/level1/types/Job.js +11 -0
  146. package/dist/level1/types/Job.js.map +1 -0
  147. package/dist/level1/types/Options.d.ts +258 -0
  148. package/dist/level1/types/Options.d.ts.map +1 -0
  149. package/dist/level1/types/Options.js +8 -0
  150. package/dist/level1/types/Options.js.map +1 -0
  151. package/dist/level1/types/index.d.ts +8 -0
  152. package/dist/level1/types/index.d.ts.map +1 -0
  153. package/dist/level1/types/index.js +8 -0
  154. package/dist/level1/types/index.js.map +1 -0
  155. package/dist/protocol/ACTPKernel.d.ts +229 -2
  156. package/dist/protocol/ACTPKernel.d.ts.map +1 -1
  157. package/dist/protocol/ACTPKernel.js +367 -33
  158. package/dist/protocol/ACTPKernel.js.map +1 -1
  159. package/dist/protocol/AgentRegistry.d.ts +177 -0
  160. package/dist/protocol/AgentRegistry.d.ts.map +1 -0
  161. package/dist/protocol/AgentRegistry.js +449 -0
  162. package/dist/protocol/AgentRegistry.js.map +1 -0
  163. package/dist/protocol/DIDManager.d.ts +289 -0
  164. package/dist/protocol/DIDManager.d.ts.map +1 -0
  165. package/dist/protocol/DIDManager.js +481 -0
  166. package/dist/protocol/DIDManager.js.map +1 -0
  167. package/dist/protocol/DIDResolver.d.ts +236 -0
  168. package/dist/protocol/DIDResolver.d.ts.map +1 -0
  169. package/dist/protocol/DIDResolver.js +495 -0
  170. package/dist/protocol/DIDResolver.js.map +1 -0
  171. package/dist/protocol/EASHelper.d.ts +57 -2
  172. package/dist/protocol/EASHelper.d.ts.map +1 -1
  173. package/dist/protocol/EASHelper.js +230 -37
  174. package/dist/protocol/EASHelper.js.map +1 -1
  175. package/dist/protocol/EscrowVault.d.ts +93 -2
  176. package/dist/protocol/EscrowVault.d.ts.map +1 -1
  177. package/dist/protocol/EscrowVault.js +122 -33
  178. package/dist/protocol/EscrowVault.js.map +1 -1
  179. package/dist/protocol/EventMonitor.d.ts +45 -1
  180. package/dist/protocol/EventMonitor.d.ts.map +1 -1
  181. package/dist/protocol/EventMonitor.js +64 -8
  182. package/dist/protocol/EventMonitor.js.map +1 -1
  183. package/dist/protocol/MessageSigner.d.ts +116 -2
  184. package/dist/protocol/MessageSigner.d.ts.map +1 -1
  185. package/dist/protocol/MessageSigner.js +215 -9
  186. package/dist/protocol/MessageSigner.js.map +1 -1
  187. package/dist/protocol/ProofGenerator.d.ts +93 -0
  188. package/dist/protocol/ProofGenerator.d.ts.map +1 -1
  189. package/dist/protocol/ProofGenerator.js +194 -9
  190. package/dist/protocol/ProofGenerator.js.map +1 -1
  191. package/dist/protocol/QuoteBuilder.d.ts +8 -0
  192. package/dist/protocol/QuoteBuilder.d.ts.map +1 -1
  193. package/dist/protocol/QuoteBuilder.js +8 -0
  194. package/dist/protocol/QuoteBuilder.js.map +1 -1
  195. package/dist/runtime/BlockchainRuntime.d.ts +360 -0
  196. package/dist/runtime/BlockchainRuntime.d.ts.map +1 -0
  197. package/dist/runtime/BlockchainRuntime.js +767 -0
  198. package/dist/runtime/BlockchainRuntime.js.map +1 -0
  199. package/dist/runtime/IACTPRuntime.d.ts +271 -0
  200. package/dist/runtime/IACTPRuntime.d.ts.map +1 -0
  201. package/dist/runtime/IACTPRuntime.js +15 -0
  202. package/dist/runtime/IACTPRuntime.js.map +1 -0
  203. package/dist/runtime/MockRuntime.d.ts +445 -0
  204. package/dist/runtime/MockRuntime.d.ts.map +1 -0
  205. package/dist/runtime/MockRuntime.js +1065 -0
  206. package/dist/runtime/MockRuntime.js.map +1 -0
  207. package/dist/runtime/MockStateManager.d.ts +233 -0
  208. package/dist/runtime/MockStateManager.d.ts.map +1 -0
  209. package/dist/runtime/MockStateManager.js +533 -0
  210. package/dist/runtime/MockStateManager.js.map +1 -0
  211. package/dist/runtime/index.d.ts +14 -0
  212. package/dist/runtime/index.d.ts.map +1 -0
  213. package/dist/runtime/index.js +42 -0
  214. package/dist/runtime/index.js.map +1 -0
  215. package/dist/runtime/types/MockState.d.ts +167 -0
  216. package/dist/runtime/types/MockState.d.ts.map +1 -0
  217. package/dist/runtime/types/MockState.js +43 -0
  218. package/dist/runtime/types/MockState.js.map +1 -0
  219. package/dist/types/agent.d.ts +76 -0
  220. package/dist/types/agent.d.ts.map +1 -0
  221. package/dist/types/agent.js +8 -0
  222. package/dist/types/agent.js.map +1 -0
  223. package/dist/types/did.d.ts +192 -0
  224. package/dist/types/did.d.ts.map +1 -0
  225. package/dist/types/did.js +38 -0
  226. package/dist/types/did.js.map +1 -0
  227. package/dist/types/eip712.d.ts +34 -0
  228. package/dist/types/eip712.d.ts.map +1 -1
  229. package/dist/types/eip712.js +31 -5
  230. package/dist/types/eip712.js.map +1 -1
  231. package/dist/types/escrow.d.ts +17 -10
  232. package/dist/types/escrow.d.ts.map +1 -1
  233. package/dist/types/index.d.ts +5 -0
  234. package/dist/types/index.d.ts.map +1 -1
  235. package/dist/types/index.js +8 -0
  236. package/dist/types/index.js.map +1 -1
  237. package/dist/types/message.d.ts +32 -0
  238. package/dist/types/message.d.ts.map +1 -1
  239. package/dist/types/message.js +4 -0
  240. package/dist/types/message.js.map +1 -1
  241. package/dist/types/state.d.ts +28 -0
  242. package/dist/types/state.d.ts.map +1 -1
  243. package/dist/types/state.js +37 -6
  244. package/dist/types/state.js.map +1 -1
  245. package/dist/types/transaction.d.ts +17 -0
  246. package/dist/types/transaction.d.ts.map +1 -1
  247. package/dist/utils/ErrorRecoveryGuide.d.ts +125 -0
  248. package/dist/utils/ErrorRecoveryGuide.d.ts.map +1 -0
  249. package/dist/utils/ErrorRecoveryGuide.js +579 -0
  250. package/dist/utils/ErrorRecoveryGuide.js.map +1 -0
  251. package/dist/utils/Helpers.d.ts +453 -0
  252. package/dist/utils/Helpers.d.ts.map +1 -0
  253. package/dist/utils/Helpers.js +623 -0
  254. package/dist/utils/Helpers.js.map +1 -0
  255. package/dist/utils/IPFSClient.d.ts +113 -0
  256. package/dist/utils/IPFSClient.d.ts.map +1 -1
  257. package/dist/utils/IPFSClient.js +128 -7
  258. package/dist/utils/IPFSClient.js.map +1 -1
  259. package/dist/utils/Logger.d.ts +195 -0
  260. package/dist/utils/Logger.d.ts.map +1 -0
  261. package/dist/utils/Logger.js +382 -0
  262. package/dist/utils/Logger.js.map +1 -0
  263. package/dist/utils/NonceManager.d.ts +234 -1
  264. package/dist/utils/NonceManager.d.ts.map +1 -1
  265. package/dist/utils/NonceManager.js +372 -7
  266. package/dist/utils/NonceManager.js.map +1 -1
  267. package/dist/utils/RateLimiter.d.ts +253 -0
  268. package/dist/utils/RateLimiter.d.ts.map +1 -0
  269. package/dist/utils/RateLimiter.js +424 -0
  270. package/dist/utils/RateLimiter.js.map +1 -0
  271. package/dist/utils/ReceivedNonceTracker.d.ts +175 -0
  272. package/dist/utils/ReceivedNonceTracker.d.ts.map +1 -1
  273. package/dist/utils/ReceivedNonceTracker.js +261 -5
  274. package/dist/utils/ReceivedNonceTracker.js.map +1 -1
  275. package/dist/utils/SDKLifecycle.d.ts +156 -0
  276. package/dist/utils/SDKLifecycle.d.ts.map +1 -0
  277. package/dist/utils/SDKLifecycle.js +347 -0
  278. package/dist/utils/SDKLifecycle.js.map +1 -0
  279. package/dist/utils/SecureNonce.d.ts +57 -0
  280. package/dist/utils/SecureNonce.d.ts.map +1 -0
  281. package/dist/utils/SecureNonce.js +80 -0
  282. package/dist/utils/SecureNonce.js.map +1 -0
  283. package/dist/utils/Semaphore.d.ts +123 -0
  284. package/dist/utils/Semaphore.d.ts.map +1 -0
  285. package/dist/utils/Semaphore.js +247 -0
  286. package/dist/utils/Semaphore.js.map +1 -0
  287. package/dist/utils/UsedAttestationTracker.d.ts +167 -0
  288. package/dist/utils/UsedAttestationTracker.d.ts.map +1 -0
  289. package/dist/utils/UsedAttestationTracker.js +309 -0
  290. package/dist/utils/UsedAttestationTracker.js.map +1 -0
  291. package/dist/utils/canonicalJson.d.ts +22 -0
  292. package/dist/utils/canonicalJson.d.ts.map +1 -1
  293. package/dist/utils/canonicalJson.js +26 -3
  294. package/dist/utils/canonicalJson.js.map +1 -1
  295. package/dist/utils/computeTypeHash.d.ts +14 -0
  296. package/dist/utils/computeTypeHash.d.ts.map +1 -1
  297. package/dist/utils/computeTypeHash.js +19 -2
  298. package/dist/utils/computeTypeHash.js.map +1 -1
  299. package/dist/utils/fsSafe.d.ts +14 -0
  300. package/dist/utils/fsSafe.d.ts.map +1 -0
  301. package/dist/utils/fsSafe.js +89 -0
  302. package/dist/utils/fsSafe.js.map +1 -0
  303. package/dist/utils/index.d.ts +15 -0
  304. package/dist/utils/index.d.ts.map +1 -0
  305. package/dist/utils/index.js +51 -0
  306. package/dist/utils/index.js.map +1 -0
  307. package/dist/utils/security.d.ts +147 -0
  308. package/dist/utils/security.d.ts.map +1 -0
  309. package/dist/utils/security.js +391 -0
  310. package/dist/utils/security.js.map +1 -0
  311. package/dist/utils/validation.d.ts +40 -0
  312. package/dist/utils/validation.d.ts.map +1 -1
  313. package/dist/utils/validation.js +184 -7
  314. package/dist/utils/validation.js.map +1 -1
  315. package/package.json +54 -37
  316. package/src/ACTPClient.ts +692 -178
  317. package/src/abi/AgentRegistry.json +782 -0
  318. package/src/abi/EscrowVault.json +106 -38
  319. package/src/abi/IdentityRegistry.json +316 -0
  320. package/src/adapters/BaseAdapter.ts +473 -0
  321. package/src/adapters/BeginnerAdapter.ts +232 -0
  322. package/src/adapters/IntermediateAdapter.ts +316 -0
  323. package/src/adapters/index.ts +25 -0
  324. package/src/builders/DeliveryProofBuilder.ts +3 -2
  325. package/src/cli/commands/balance.ts +110 -0
  326. package/src/cli/commands/batch.ts +487 -0
  327. package/src/cli/commands/config.ts +231 -0
  328. package/src/cli/commands/init.ts +161 -0
  329. package/src/cli/commands/mint.ts +116 -0
  330. package/src/cli/commands/pay.ts +113 -0
  331. package/src/cli/commands/simulate.ts +345 -0
  332. package/src/cli/commands/time.ts +303 -0
  333. package/src/cli/commands/tx.ts +448 -0
  334. package/src/cli/commands/watch.ts +211 -0
  335. package/src/cli/index.ts +116 -0
  336. package/src/cli/utils/client.ts +249 -0
  337. package/src/cli/utils/config.ts +282 -0
  338. package/src/cli/utils/output.ts +465 -0
  339. package/src/config/networks.ts +32 -9
  340. package/src/errors/index.ts +298 -1
  341. package/src/index.ts +207 -71
  342. package/src/level0/Provider.ts +117 -0
  343. package/src/level0/ServiceDirectory.ts +131 -0
  344. package/src/level0/index.ts +10 -0
  345. package/src/level0/provide.ts +131 -0
  346. package/src/level0/request.ts +494 -0
  347. package/src/level1/Agent.ts +1432 -0
  348. package/src/level1/index.ts +10 -0
  349. package/src/level1/pricing/PriceCalculator.ts +255 -0
  350. package/src/level1/pricing/PricingStrategy.ts +198 -0
  351. package/src/level1/types/Job.ts +179 -0
  352. package/src/level1/types/Options.ts +291 -0
  353. package/src/level1/types/index.ts +8 -0
  354. package/src/protocol/ACTPKernel.ts +175 -23
  355. package/src/protocol/AgentRegistry.ts +559 -0
  356. package/src/protocol/DIDManager.ts +629 -0
  357. package/src/protocol/DIDResolver.ts +554 -0
  358. package/src/protocol/EASHelper.ts +230 -46
  359. package/src/protocol/EscrowVault.ts +68 -50
  360. package/src/protocol/EventMonitor.ts +44 -15
  361. package/src/protocol/MessageSigner.ts +193 -13
  362. package/src/protocol/ProofGenerator.ts +223 -4
  363. package/src/runtime/BlockchainRuntime.ts +993 -0
  364. package/src/runtime/IACTPRuntime.ts +284 -0
  365. package/src/runtime/MockRuntime.ts +1244 -0
  366. package/src/runtime/MockStateManager.ts +576 -0
  367. package/src/runtime/index.ts +25 -0
  368. package/src/runtime/types/MockState.ts +227 -0
  369. package/src/types/agent.ts +79 -0
  370. package/src/types/did.ts +223 -0
  371. package/src/types/escrow.ts +12 -11
  372. package/src/types/index.ts +5 -1
  373. package/src/types/state.ts +12 -3
  374. package/src/types/transaction.ts +4 -1
  375. package/src/utils/ErrorRecoveryGuide.ts +675 -0
  376. package/src/utils/Helpers.ts +688 -0
  377. package/src/utils/IPFSClient.ts +122 -5
  378. package/src/utils/Logger.ts +484 -0
  379. package/src/utils/NonceManager.ts +305 -8
  380. package/src/utils/RateLimiter.ts +534 -0
  381. package/src/utils/ReceivedNonceTracker.ts +170 -0
  382. package/src/utils/SDKLifecycle.ts +416 -0
  383. package/src/utils/SecureNonce.ts +78 -0
  384. package/src/utils/Semaphore.ts +276 -0
  385. package/src/utils/UsedAttestationTracker.ts +387 -0
  386. package/src/utils/fsSafe.ts +75 -0
  387. package/src/utils/index.ts +80 -0
  388. package/src/utils/security.ts +418 -0
  389. package/src/utils/validation.ts +164 -0
  390. package/src/__tests__/ProofGenerator.test.ts +0 -124
  391. package/src/__tests__/QuoteBuilder.test.ts +0 -516
  392. package/src/__tests__/StateMachine.test.ts +0 -82
  393. package/src/__tests__/builders/DeliveryProofBuilder.test.ts +0 -581
  394. package/src/__tests__/integration/ACTPClient.test.ts +0 -263
  395. package/src/__tests__/integration.test.ts +0 -289
  396. package/src/__tests__/protocol/EASHelper.test.ts +0 -472
  397. package/src/__tests__/protocol/EventMonitor.test.ts +0 -382
  398. package/src/__tests__/security/ACTPKernel.security.test.ts +0 -1167
  399. package/src/__tests__/security/EscrowVault.security.test.ts +0 -570
  400. package/src/__tests__/security/MessageSigner.security.test.ts +0 -286
  401. package/src/__tests__/security/NonceReplay.security.test.ts +0 -501
  402. package/src/__tests__/security/validation.security.test.ts +0 -376
  403. package/src/__tests__/utils/IPFSClient.test.ts +0 -262
  404. package/src/__tests__/utils/NonceManager.test.ts +0 -205
  405. package/src/__tests__/utils/canonicalJson.test.ts +0 -153
@@ -0,0 +1,767 @@
1
+ "use strict";
2
+ /**
3
+ * BlockchainRuntime - Real blockchain implementation of ACTP protocol
4
+ *
5
+ * Implements IACTPRuntime interface using actual smart contracts deployed
6
+ * on Base Sepolia (testnet) or Base Mainnet (production).
7
+ *
8
+ * Features:
9
+ * - Real blockchain transactions via ethers.js
10
+ * - ACTPKernel smart contract integration
11
+ * - EscrowVault contract integration
12
+ * - EIP-712 message signing support
13
+ * - Event monitoring and indexing
14
+ * - Gas optimization
15
+ *
16
+ * @module runtime/BlockchainRuntime
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.BlockchainRuntime = void 0;
20
+ const ethers_1 = require("ethers");
21
+ const ACTPKernel_1 = require("../protocol/ACTPKernel");
22
+ const EscrowVault_1 = require("../protocol/EscrowVault");
23
+ const EventMonitor_1 = require("../protocol/EventMonitor");
24
+ const MessageSigner_1 = require("../protocol/MessageSigner");
25
+ const EASHelper_1 = require("../protocol/EASHelper");
26
+ const networks_1 = require("../config/networks");
27
+ const errors_1 = require("../errors");
28
+ const Helpers_1 = require("../utils/Helpers");
29
+ const UsedAttestationTracker_1 = require("../utils/UsedAttestationTracker");
30
+ const ReceivedNonceTracker_1 = require("../utils/ReceivedNonceTracker");
31
+ /**
32
+ * BlockchainRuntime - Production blockchain implementation
33
+ *
34
+ * Bridges the IACTPRuntime interface to actual smart contracts.
35
+ * Provides seamless migration path from MockRuntime to production.
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * const provider = new ethers.JsonRpcProvider(rpcUrl);
40
+ * const signer = new ethers.Wallet(privateKey, provider);
41
+ *
42
+ * const runtime = new BlockchainRuntime({
43
+ * network: 'base-sepolia',
44
+ * signer,
45
+ * provider
46
+ * });
47
+ *
48
+ * // Now use with adapters
49
+ * const adapter = new BeginnerAdapter(runtime, requesterAddress);
50
+ * await adapter.createJob({...});
51
+ * ```
52
+ */
53
+ class BlockchainRuntime {
54
+ /**
55
+ * Create new BlockchainRuntime instance
56
+ *
57
+ * @param config - Runtime configuration
58
+ */
59
+ constructor(config) {
60
+ // SECURITY FIX (H-4): MessageSigner created via factory in initialize()
61
+ this.messageSigner = null;
62
+ // SECURITY FIX (CRITICAL-2): EAS helper for attestation verification
63
+ this.easHelper = null;
64
+ // SECURITY FIX (HIGH-3): Provider reconnection with exponential backoff
65
+ this.reconnectAttempts = 0;
66
+ this.maxReconnectAttempts = 5;
67
+ this.baseReconnectDelay = 1000; // 1 second
68
+ this.lastConnectionCheck = 0;
69
+ this.connectionCheckInterval = 30000; // 30 seconds
70
+ /**
71
+ * Time interface (uses real blockchain time)
72
+ */
73
+ this.time = {
74
+ /**
75
+ * Get current blockchain timestamp
76
+ */
77
+ now: () => {
78
+ return Math.floor(Date.now() / 1000);
79
+ },
80
+ };
81
+ this.provider = config.provider;
82
+ this.signer = config.signer;
83
+ // Get network configuration
84
+ this.networkConfig = (0, networks_1.getNetwork)(config.network);
85
+ // Apply contract address overrides if provided
86
+ if (config.contracts) {
87
+ this.networkConfig = {
88
+ ...this.networkConfig,
89
+ contracts: {
90
+ ...this.networkConfig.contracts,
91
+ ...config.contracts,
92
+ },
93
+ };
94
+ }
95
+ // NOTE (GAS DEFAULTS):
96
+ // We intentionally do NOT force default maxFee/maxPriority caps unless the caller
97
+ // explicitly provides gasSettings. Hardcoded caps can cause "insufficient funds for
98
+ // intrinsic transaction cost" even when the wallet has enough ETH for the *actual*
99
+ // network fee (ethers uses maxFee * gasLimit for the balance check).
100
+ // SECURITY FIX (CRITICAL-2): Store EAS config for initialization
101
+ this.easConfig = config.easConfig;
102
+ // SECURITY FIX (CRITICAL-2): Default to NOT requiring attestation for backward compatibility
103
+ // Production deployments SHOULD set this to true
104
+ this.requireAttestation = config.requireAttestation ?? false;
105
+ // SECURITY FIX (HIGH-3): Create attestation tracker with optional persistence
106
+ // If stateDirectory is provided, attestations survive process restarts
107
+ this.attestationTracker = (0, UsedAttestationTracker_1.createUsedAttestationTracker)(config.stateDirectory);
108
+ // SECURITY FIX (MEDIUM-9): Create nonce tracker for message replay protection
109
+ // Uses memory-efficient strategy (tracks highest nonce per sender+type)
110
+ this.nonceTracker = (0, ReceivedNonceTracker_1.createReceivedNonceTracker)('memory-efficient');
111
+ // Initialize protocol modules
112
+ this.kernel = new ACTPKernel_1.ACTPKernel(this.networkConfig.contracts.actpKernel, this.signer, config.gasSettings);
113
+ this.escrow = new EscrowVault_1.EscrowVault(this.networkConfig.contracts.escrowVault, this.signer, config.gasSettings);
114
+ // SECURITY FIX (C-3): Use public getters instead of private field access
115
+ this.events = new EventMonitor_1.EventMonitor(this.kernel.getContract(), this.escrow.getContract());
116
+ // SECURITY FIX (H-4): MessageSigner is created in initialize() using factory pattern
117
+ // This ensures EIP-712 domain is always properly initialized before use
118
+ }
119
+ /**
120
+ * Initialize async components (must be called after construction)
121
+ *
122
+ * CRITICAL: This method MUST be called before using the runtime.
123
+ * It initializes the MessageSigner with proper EIP-712 domain and
124
+ * optionally the EASHelper for attestation verification.
125
+ *
126
+ * SECURITY FIX (CHAINID-VALIDATION): Validates that the connected network
127
+ * matches the expected network configuration to prevent cross-chain attacks.
128
+ *
129
+ * @example
130
+ * ```typescript
131
+ * const runtime = new BlockchainRuntime(config);
132
+ * await runtime.initialize();
133
+ * ```
134
+ */
135
+ async initialize() {
136
+ // SECURITY FIX (CHAINID-VALIDATION): Verify connected network matches config
137
+ // This prevents:
138
+ // 1. Cross-chain replay attacks (signing for one chain, replaying on another)
139
+ // 2. Misconfigured RPC endpoints (connecting to wrong network)
140
+ // 3. Man-in-the-middle RPC attacks (redirected to wrong chain)
141
+ try {
142
+ const network = await this.provider.getNetwork();
143
+ const connectedChainId = Number(network.chainId);
144
+ const expectedChainId = this.networkConfig.chainId;
145
+ if (connectedChainId !== expectedChainId) {
146
+ throw new Error(`Network mismatch: Connected to chainId ${connectedChainId}, ` +
147
+ `but expected ${expectedChainId} (${this.networkConfig.name}). ` +
148
+ `This could indicate a misconfigured RPC endpoint or cross-chain attack. ` +
149
+ `Please verify your RPC URL points to the correct network.`);
150
+ }
151
+ console.info(`BlockchainRuntime: Connected to ${this.networkConfig.name} (chainId: ${connectedChainId})`);
152
+ }
153
+ catch (error) {
154
+ if (error instanceof Error && error.message.includes('Network mismatch')) {
155
+ throw error; // Re-throw our validation error
156
+ }
157
+ // For other errors (e.g., network issues), log warning but continue
158
+ // This allows initialization to proceed even if network check fails temporarily
159
+ console.warn(`BlockchainRuntime: Could not verify network chainId. ` +
160
+ `Error: ${error instanceof Error ? error.message : String(error)}. ` +
161
+ `Proceeding with expected chainId ${this.networkConfig.chainId}.`);
162
+ }
163
+ // SECURITY FIX (H-4): Use factory pattern to guarantee domain initialization
164
+ // This prevents runtime errors from uninitialized domain
165
+ // SECURITY FIX (MEDIUM-9): Wire nonce tracker for message replay protection
166
+ this.messageSigner = await MessageSigner_1.MessageSigner.create(this.signer, this.networkConfig.contracts.actpKernel, {
167
+ chainId: this.networkConfig.chainId,
168
+ nonceTracker: this.nonceTracker,
169
+ });
170
+ // SECURITY FIX (CRITICAL-2): Initialize EAS helper if config provided
171
+ // This enables attestation verification for escrow release
172
+ if (this.easConfig) {
173
+ this.easHelper = new EASHelper_1.EASHelper(this.signer, this.easConfig, this.attestationTracker);
174
+ }
175
+ else if (this.requireAttestation) {
176
+ console.warn('[SECURITY WARNING] BlockchainRuntime: requireAttestation is true but no EAS config provided. ' +
177
+ 'Attestation verification will fail. Please provide easConfig in BlockchainRuntimeConfig.');
178
+ }
179
+ }
180
+ /**
181
+ * Check if runtime has been initialized
182
+ * @returns true if initialize() has been called
183
+ */
184
+ isInitialized() {
185
+ return this.messageSigner !== null;
186
+ }
187
+ /**
188
+ * Require initialization before use
189
+ *
190
+ * SECURITY FIX (M-4): Enforces initialize() call before operations
191
+ * @throws Error if initialize() has not been called
192
+ */
193
+ requireInitialized() {
194
+ if (!this.messageSigner) {
195
+ throw new Error('BlockchainRuntime not initialized. Call initialize() before using any methods. ' +
196
+ 'This ensures proper EIP-712 domain setup and prevents runtime errors.');
197
+ }
198
+ }
199
+ /**
200
+ * Ensure provider connection is healthy with automatic reconnection
201
+ *
202
+ * SECURITY FIX (HIGH-3): Implements exponential backoff reconnection
203
+ * to handle transient network failures gracefully.
204
+ *
205
+ * SECURITY FIX (H-4): Converted from recursive to iterative loop
206
+ * to prevent stack overflow on prolonged network failures.
207
+ *
208
+ * @throws Error if connection cannot be established after max attempts
209
+ */
210
+ async ensureConnected() {
211
+ const now = Date.now();
212
+ // Skip check if we recently verified connection
213
+ if (now - this.lastConnectionCheck < this.connectionCheckInterval) {
214
+ return;
215
+ }
216
+ // SECURITY FIX (H-4): Iterative loop instead of recursion (prevents stack overflow)
217
+ for (let attempt = 0; attempt <= this.maxReconnectAttempts; attempt++) {
218
+ try {
219
+ // Test connection with a simple call
220
+ await this.provider.getNetwork();
221
+ // Connection successful
222
+ this.reconnectAttempts = 0;
223
+ this.lastConnectionCheck = now;
224
+ return; // Exit successfully
225
+ }
226
+ catch (error) {
227
+ if (attempt < this.maxReconnectAttempts) {
228
+ // Not the last attempt - retry with exponential backoff
229
+ this.reconnectAttempts = attempt + 1;
230
+ const delay = this.baseReconnectDelay * Math.pow(2, attempt);
231
+ console.warn(`Provider connection lost. Attempting reconnection ${attempt + 1}/${this.maxReconnectAttempts} ` +
232
+ `after ${delay}ms delay... ` +
233
+ `(Error: ${error instanceof Error ? error.message : String(error)})`);
234
+ // Wait before next attempt
235
+ await new Promise(resolve => setTimeout(resolve, delay));
236
+ }
237
+ else {
238
+ // Last attempt failed - throw error
239
+ throw new Error(`Provider connection lost after ${this.maxReconnectAttempts} reconnection attempts. ` +
240
+ `Last error: ${error instanceof Error ? error.message : String(error)}. ` +
241
+ `Please check your network connectivity and RPC endpoint.`);
242
+ }
243
+ }
244
+ }
245
+ }
246
+ /**
247
+ * Get provider connection status
248
+ *
249
+ * SECURITY FIX (HIGH-3): Monitoring method for connection health
250
+ *
251
+ * @returns Connection status information
252
+ */
253
+ getConnectionStatus() {
254
+ return {
255
+ isHealthy: this.reconnectAttempts === 0,
256
+ reconnectAttempts: this.reconnectAttempts,
257
+ maxReconnectAttempts: this.maxReconnectAttempts,
258
+ lastCheckTimestamp: this.lastConnectionCheck,
259
+ };
260
+ }
261
+ /**
262
+ * Creates a new transaction on-chain
263
+ *
264
+ * @param params - Transaction creation parameters
265
+ * @returns Promise resolving to transaction ID (bytes32 hex string)
266
+ */
267
+ async createTransaction(params) {
268
+ // SECURITY FIX (M-4): Enforce initialization
269
+ this.requireInitialized();
270
+ // SECURITY FIX (HIGH-3): Ensure provider connection before transaction
271
+ await this.ensureConnected();
272
+ // Validate parameters
273
+ if (!params.provider || !ethers_1.ethers.isAddress(params.provider)) {
274
+ throw new errors_1.ValidationError('provider', 'Invalid provider address');
275
+ }
276
+ if (!params.requester || !ethers_1.ethers.isAddress(params.requester)) {
277
+ throw new errors_1.ValidationError('requester', 'Invalid requester address');
278
+ }
279
+ if (BigInt(params.amount) <= 0n) {
280
+ throw new errors_1.ValidationError('amount', 'Amount must be positive');
281
+ }
282
+ const now = Math.floor(Date.now() / 1000);
283
+ if (params.deadline <= now) {
284
+ throw new errors_1.ValidationError('deadline', 'Deadline must be in the future');
285
+ }
286
+ // Call ACTPKernel contract
287
+ const txId = await this.kernel.createTransaction({
288
+ provider: params.provider,
289
+ requester: params.requester,
290
+ amount: BigInt(params.amount),
291
+ deadline: params.deadline,
292
+ disputeWindow: params.disputeWindow || 172800, // Default 2 days
293
+ // SECURITY FIX (CRITICAL): serviceDescription should be a bytes32 hash
294
+ // If caller passes raw string, it will fail on-chain. Basic/Standard API now hash before calling.
295
+ metadata: this.validateServiceHash(params.serviceDescription),
296
+ });
297
+ return txId;
298
+ }
299
+ /**
300
+ * Links escrow to a transaction and locks funds
301
+ *
302
+ * SIMPLIFICATION (ESCROW-ID): Uses txId as escrowId.
303
+ * Per ACTP standard, escrowId = txId simplifies tracking and eliminates
304
+ * the need for separate escrowId→txId mapping.
305
+ *
306
+ * @param txId - Transaction ID
307
+ * @param amount - Amount to lock (must match transaction amount)
308
+ * @returns Promise resolving to escrow ID (same as txId)
309
+ */
310
+ async linkEscrow(txId, amount) {
311
+ // SECURITY FIX (M-4): Enforce initialization
312
+ this.requireInitialized();
313
+ // SECURITY FIX (HIGH-3): Ensure provider connection before transaction
314
+ await this.ensureConnected();
315
+ // Validate transaction exists and get details
316
+ const tx = await this.getTransaction(txId);
317
+ if (!tx) {
318
+ throw new Error(`Transaction not found: ${txId}`);
319
+ }
320
+ // Validate state is INITIATED or QUOTED
321
+ if (tx.state !== 'INITIATED' && tx.state !== 'QUOTED') {
322
+ throw new Error(`Cannot link escrow in current state ${tx.state}. Must be INITIATED or QUOTED.`);
323
+ }
324
+ // Validate amount matches transaction
325
+ if (amount !== tx.amount) {
326
+ throw new errors_1.ValidationError('amount', 'Amount must match transaction amount');
327
+ }
328
+ // Approve USDC to escrow vault
329
+ await this.escrow.approveToken(this.networkConfig.contracts.usdc, BigInt(amount));
330
+ // SIMPLIFICATION (ESCROW-ID): Use txId as escrowId
331
+ // This aligns with ACTP standard where escrowId = txId
332
+ // Benefits: No mapping needed, simpler tracking, direct correlation
333
+ const escrowId = txId;
334
+ // Link escrow to transaction
335
+ await this.kernel.linkEscrow(txId, this.networkConfig.contracts.escrowVault, escrowId);
336
+ return escrowId;
337
+ }
338
+ /**
339
+ * Transitions a transaction to a new state
340
+ *
341
+ * SECURITY FIX (PROOF-PARAM): Added optional proof parameter for DELIVERED state.
342
+ * The kernel contract uses proof data for dispute window configuration and
343
+ * delivery verification. Without proof, default dispute window applies.
344
+ *
345
+ * @param txId - Transaction ID
346
+ * @param newState - Target state
347
+ * @param proof - Optional proof data (hex string, e.g., ABI-encoded delivery proof)
348
+ */
349
+ async transitionState(txId, newState, proof) {
350
+ // SECURITY FIX (M-4): Enforce initialization
351
+ this.requireInitialized();
352
+ // SECURITY FIX (HIGH-3): Ensure provider connection before transaction
353
+ await this.ensureConnected();
354
+ // Map TransactionState string to State enum value
355
+ const stateMap = {
356
+ INITIATED: 0,
357
+ QUOTED: 1,
358
+ COMMITTED: 2,
359
+ IN_PROGRESS: 3,
360
+ DELIVERED: 4,
361
+ SETTLED: 5,
362
+ DISPUTED: 6,
363
+ CANCELLED: 7,
364
+ };
365
+ const stateValue = stateMap[newState];
366
+ if (stateValue === undefined) {
367
+ throw new errors_1.ValidationError('state', `Invalid state: ${newState}`);
368
+ }
369
+ // SECURITY FIX (PROOF-PARAM): Pass proof to kernel if provided
370
+ // Default to empty bytes (0x) if no proof provided
371
+ const proofBytes = proof || '0x';
372
+ await this.kernel.transitionState(txId, stateValue, proofBytes);
373
+ }
374
+ /**
375
+ * Gets a transaction by ID
376
+ *
377
+ * @param txId - Transaction ID
378
+ * @returns Promise resolving to transaction or null if not found
379
+ */
380
+ async getTransaction(txId) {
381
+ try {
382
+ const tx = await this.kernel.getTransaction(txId);
383
+ // Check if transaction exists (zero address = not found)
384
+ if (tx.requester === ethers_1.ethers.ZeroAddress) {
385
+ return null;
386
+ }
387
+ // Map blockchain transaction to MockTransaction format
388
+ const stateMap = {
389
+ 0: 'INITIATED',
390
+ 1: 'QUOTED',
391
+ 2: 'COMMITTED',
392
+ 3: 'IN_PROGRESS',
393
+ 4: 'DELIVERED',
394
+ 5: 'SETTLED',
395
+ 6: 'DISPUTED',
396
+ 7: 'CANCELLED',
397
+ };
398
+ // SECURITY FIX (H-2): Throw error for unknown states instead of silent fallback
399
+ const mappedState = stateMap[tx.state];
400
+ if (mappedState === undefined) {
401
+ throw new Error(`Unknown transaction state: ${tx.state}. ` +
402
+ `Valid states are 0-7 (INITIATED through CANCELLED). ` +
403
+ `This may indicate a contract version mismatch.`);
404
+ }
405
+ return {
406
+ id: txId,
407
+ provider: tx.provider,
408
+ requester: tx.requester,
409
+ amount: tx.amount.toString(),
410
+ state: mappedState,
411
+ deadline: Number(tx.deadline),
412
+ disputeWindow: tx.disputeWindow !== undefined ? Number(tx.disputeWindow) : 172800, // Default 2 days
413
+ escrowId: tx.escrowId,
414
+ createdAt: Number(tx.createdAt),
415
+ updatedAt: Number(tx.updatedAt),
416
+ completedAt: 0, // V2: Track via DELIVERED event timestamp
417
+ serviceDescription: '', // V2: Decode from on-chain serviceHash
418
+ deliveryProof: '', // V2: Fetch from EAS attestation
419
+ events: [], // V2: Populate via EventMonitor.getTransactionEvents()
420
+ };
421
+ }
422
+ catch (error) {
423
+ // If contract call fails, return null
424
+ return null;
425
+ }
426
+ }
427
+ /**
428
+ * Gets all transactions
429
+ *
430
+ * @returns Promise resolving to array of all transactions
431
+ */
432
+ async getAllTransactions() {
433
+ // V2: Implement event-based transaction indexing via EventMonitor
434
+ // For now, return empty array as this requires off-chain indexer
435
+ console.warn('getAllTransactions() not fully implemented for BlockchainRuntime. Use EventMonitor for event-based queries.');
436
+ return [];
437
+ }
438
+ /**
439
+ * Releases escrow funds to provider by settling the transaction
440
+ *
441
+ * SECURITY FIX (CRITICAL-2): This method now validates:
442
+ * 1. Transaction state is DELIVERED
443
+ * 2. Dispute window has elapsed
444
+ * 3. EAS attestation is valid (if requireAttestation is true)
445
+ *
446
+ * SECURITY FIX (SETTLEMENT-FLOW): Uses transitionState(SETTLED) instead of
447
+ * direct releaseEscrow() call. Per ACTPKernel.sol, settlement via state transition
448
+ * automatically handles escrow release through _releaseEscrow() internal call.
449
+ * This ensures proper state machine progression and event emission.
450
+ *
451
+ * SIMPLIFICATION (ESCROW-ID): Uses escrowId = txId standard.
452
+ * The on-chain contract uses txId as the escrow identifier, so we simply
453
+ * treat escrowId and txId as equivalent (no complex parsing needed).
454
+ *
455
+ * @param escrowId - Escrow ID (equivalent to txId in ACTP standard)
456
+ * @param attestationUID - Optional EAS attestation UID for verification
457
+ * @throws Error if transaction not found, not in DELIVERED state, or attestation invalid
458
+ */
459
+ async releaseEscrow(escrowId, attestationUID) {
460
+ // SECURITY FIX (M-4): Enforce initialization
461
+ this.requireInitialized();
462
+ // SECURITY FIX (HIGH-3): Ensure provider connection before transaction
463
+ await this.ensureConnected();
464
+ // SIMPLIFICATION (ESCROW-ID): escrowId = txId standard
465
+ // On-chain, escrowId IS the txId. No need for complex parsing.
466
+ // Support legacy format "escrow-{txId}-{timestamp}" for backward compatibility
467
+ let txId;
468
+ const legacyMatch = escrowId.match(/^escrow-(.+)-\d+$/);
469
+ if (legacyMatch) {
470
+ // Legacy SDK format - extract txId
471
+ txId = legacyMatch[1];
472
+ console.warn(`BlockchainRuntime.releaseEscrow: Using legacy escrowId format. ` +
473
+ `Please update to use txId directly as escrowId.`);
474
+ }
475
+ else {
476
+ // Standard: escrowId = txId
477
+ txId = escrowId;
478
+ }
479
+ // SECURITY FIX (MEDIUM-1): Fetch transaction and validate state
480
+ const tx = await this.getTransaction(txId);
481
+ if (!tx) {
482
+ throw new Error(`Transaction not found: ${txId}`);
483
+ }
484
+ // SECURITY FIX (MEDIUM-1): Validate transaction is in DELIVERED state
485
+ if (tx.state !== 'DELIVERED') {
486
+ throw new Error(`Cannot release escrow: transaction ${txId} is in state ${tx.state}, expected DELIVERED. ` +
487
+ `Escrow can only be released after delivery is confirmed.`);
488
+ }
489
+ // SECURITY FIX (MEDIUM-1): Validate dispute window has elapsed
490
+ if (tx.completedAt && tx.disputeWindow) {
491
+ if (Helpers_1.DisputeWindow.isActive(tx.completedAt, tx.disputeWindow)) {
492
+ const remaining = Helpers_1.DisputeWindow.remaining(tx.completedAt, tx.disputeWindow);
493
+ throw new Error(`Cannot release escrow: dispute window still active for transaction ${txId}. ` +
494
+ `Window expires in ${remaining} seconds. ` +
495
+ `Wait for dispute window to close before releasing funds.`);
496
+ }
497
+ }
498
+ // SECURITY FIX (CRITICAL-2): Verify EAS attestation if required
499
+ if (this.requireAttestation) {
500
+ if (!attestationUID) {
501
+ throw new Error(`Cannot release escrow: attestation verification is required but no attestationUID provided. ` +
502
+ `Call releaseEscrow(escrowId, attestationUID) with a valid EAS attestation UID.`);
503
+ }
504
+ if (!this.easHelper) {
505
+ throw new Error(`Cannot release escrow: attestation verification is required but EAS helper not initialized. ` +
506
+ `Provide easConfig in BlockchainRuntimeConfig and call initialize().`);
507
+ }
508
+ // Verify attestation is valid for this transaction
509
+ try {
510
+ await this.easHelper.verifyAndRecordForRelease(txId, attestationUID);
511
+ console.info(`BlockchainRuntime.releaseEscrow: Attestation ${attestationUID} verified for transaction ${txId}.`);
512
+ }
513
+ catch (error) {
514
+ throw new Error(`Cannot release escrow: attestation verification failed for transaction ${txId}. ` +
515
+ `Error: ${error instanceof Error ? error.message : String(error)}`);
516
+ }
517
+ }
518
+ else if (attestationUID && this.easHelper) {
519
+ // Even if not required, verify attestation if provided (best effort)
520
+ try {
521
+ await this.easHelper.verifyAndRecordForRelease(txId, attestationUID);
522
+ console.info(`BlockchainRuntime.releaseEscrow: Attestation ${attestationUID} verified (optional) for transaction ${txId}.`);
523
+ }
524
+ catch (error) {
525
+ console.warn(`BlockchainRuntime.releaseEscrow: Attestation verification failed but not required. ` +
526
+ `Proceeding with release. Error: ${error instanceof Error ? error.message : String(error)}`);
527
+ }
528
+ }
529
+ else {
530
+ // No attestation verification
531
+ console.info(`BlockchainRuntime.releaseEscrow: Settling transaction ${txId}. ` +
532
+ `Note: Set requireAttestation=true and provide easConfig for additional security.`);
533
+ }
534
+ // SECURITY FIX (SETTLEMENT-FLOW): Use transitionState(SETTLED) instead of releaseEscrow()
535
+ // Per ACTPKernel.sol, the settlement flow is:
536
+ // transitionState(txId, SETTLED, proof) → internally calls _releaseEscrow(txn)
537
+ // This ensures proper state machine progression and emits correct events.
538
+ // Direct kernel.releaseEscrow() may not properly update transaction state.
539
+ await this.kernel.transitionState(txId, 5); // 5 = State.SETTLED
540
+ }
541
+ /**
542
+ * Gets escrow balance
543
+ *
544
+ * MEDIUM: Returns the locked balance for a transaction from the escrow vault.
545
+ * Queries the EscrowVault contract for the actual balance.
546
+ *
547
+ * @param escrowId - Escrow ID or transaction ID
548
+ * @returns Promise resolving to balance as string (in USDC wei)
549
+ */
550
+ async getEscrowBalance(escrowId) {
551
+ // SECURITY FIX (M-4): Enforce initialization
552
+ this.requireInitialized();
553
+ try {
554
+ // Try to get balance from escrow vault
555
+ // The escrow vault tracks balances by transaction ID
556
+ const match = escrowId.match(/^escrow-(.+)-\d+$/);
557
+ const txId = match ? match[1] : escrowId;
558
+ // Query the transaction to get the locked amount
559
+ const tx = await this.getTransaction(txId);
560
+ if (!tx) {
561
+ return '0';
562
+ }
563
+ // If transaction is in an active state (COMMITTED, IN_PROGRESS, DELIVERED),
564
+ // the escrow balance is the transaction amount
565
+ if (tx.state === 'COMMITTED' || tx.state === 'IN_PROGRESS' || tx.state === 'DELIVERED') {
566
+ return tx.amount;
567
+ }
568
+ // For settled or cancelled transactions, escrow is released
569
+ return '0';
570
+ }
571
+ catch (error) {
572
+ // If query fails, return 0
573
+ console.warn('BlockchainRuntime.getEscrowBalance: Query failed', error);
574
+ return '0';
575
+ }
576
+ }
577
+ // ============================================================================
578
+ // Utility Methods (Not in IACTPRuntime but useful for blockchain runtime)
579
+ // ============================================================================
580
+ /**
581
+ * Get the current signer address
582
+ */
583
+ async getAddress() {
584
+ return await this.signer.getAddress();
585
+ }
586
+ /**
587
+ * Get current block number
588
+ */
589
+ async getBlockNumber() {
590
+ return await this.provider.getBlockNumber();
591
+ }
592
+ /**
593
+ * Get network configuration
594
+ */
595
+ getNetworkConfig() {
596
+ return this.networkConfig;
597
+ }
598
+ /**
599
+ * Get ACTPKernel instance (for advanced usage)
600
+ */
601
+ getKernel() {
602
+ return this.kernel;
603
+ }
604
+ /**
605
+ * Get EscrowVault instance (for advanced usage)
606
+ */
607
+ getEscrow() {
608
+ return this.escrow;
609
+ }
610
+ /**
611
+ * Get EventMonitor instance (for advanced usage)
612
+ */
613
+ getEvents() {
614
+ return this.events;
615
+ }
616
+ /**
617
+ * Get MessageSigner instance (for advanced usage)
618
+ *
619
+ * @throws Error if initialize() has not been called
620
+ */
621
+ getMessageSigner() {
622
+ if (!this.messageSigner) {
623
+ throw new Error('BlockchainRuntime not initialized. Call initialize() before using MessageSigner. ' +
624
+ 'This is required for proper EIP-712 domain setup.');
625
+ }
626
+ return this.messageSigner;
627
+ }
628
+ /**
629
+ * Get EASHelper instance (for attestation operations)
630
+ *
631
+ * @throws Error if EAS config not provided or initialize() not called
632
+ */
633
+ getEASHelper() {
634
+ if (!this.easHelper) {
635
+ throw new Error('EASHelper not initialized. Provide easConfig in BlockchainRuntimeConfig and call initialize().');
636
+ }
637
+ return this.easHelper;
638
+ }
639
+ /**
640
+ * Get attestation tracker instance
641
+ */
642
+ getAttestationTracker() {
643
+ return this.attestationTracker;
644
+ }
645
+ /**
646
+ * Get nonce tracker instance (for monitoring/debugging)
647
+ *
648
+ * SECURITY FIX (MEDIUM-9): Exposed for monitoring nonce replay protection
649
+ */
650
+ getNonceTracker() {
651
+ return this.nonceTracker;
652
+ }
653
+ /**
654
+ * Check if attestation verification is required
655
+ */
656
+ isAttestationRequired() {
657
+ return this.requireAttestation;
658
+ }
659
+ /**
660
+ * Validate and normalize service hash for on-chain storage
661
+ *
662
+ * SECURITY FIX (CRITICAL): ACTPKernel expects bytes32 serviceHash.
663
+ * This method validates format and hashes raw strings if needed.
664
+ *
665
+ * @param serviceDescription - Service hash or description string
666
+ * @returns Valid bytes32 hash
667
+ */
668
+ validateServiceHash(serviceDescription) {
669
+ const ZERO_HASH = '0x0000000000000000000000000000000000000000000000000000000000000000';
670
+ if (!serviceDescription) {
671
+ return ZERO_HASH;
672
+ }
673
+ // If already a valid bytes32 hash, use it directly
674
+ if (Helpers_1.ServiceHash.isValidHash(serviceDescription)) {
675
+ return serviceDescription;
676
+ }
677
+ // SECURITY FIX (CRITICAL): If it's a raw string (legacy format), hash it
678
+ // This ensures on-chain compatibility with the contract's bytes32 expectation
679
+ console.warn('BlockchainRuntime: serviceDescription is not a valid bytes32 hash. ' +
680
+ 'Hashing it now. For best practice, use ServiceHash.hash() before calling createTransaction.');
681
+ return (0, ethers_1.keccak256)((0, ethers_1.toUtf8Bytes)(serviceDescription));
682
+ }
683
+ // ============================================================================
684
+ // Gas Estimation (M-2)
685
+ // ============================================================================
686
+ /**
687
+ * Estimate gas for createTransaction operation
688
+ *
689
+ * SECURITY FIX (M-2): Pre-transaction gas estimation helps:
690
+ * - Prevent failed transactions due to insufficient gas
691
+ * - Allow users to make informed decisions about costs
692
+ * - Catch potential issues before spending gas
693
+ *
694
+ * @param params - Transaction parameters
695
+ * @returns Estimated gas limit and cost in wei
696
+ */
697
+ async estimateCreateTransactionGas(params) {
698
+ // Get current gas price
699
+ const feeData = await this.provider.getFeeData();
700
+ const gasPrice = feeData.gasPrice ?? 0n;
701
+ // V2: Use kernel.estimateGas.createTransaction() for precise estimation
702
+ // Current: Conservative estimate based on typical createTransaction costs
703
+ const estimatedGasLimit = 150000n;
704
+ const gasCostWei = estimatedGasLimit * gasPrice;
705
+ const gasCostGwei = (Number(gasCostWei) / 1e9).toFixed(4);
706
+ return {
707
+ gasLimit: estimatedGasLimit,
708
+ gasCostWei,
709
+ gasCostGwei,
710
+ };
711
+ }
712
+ /**
713
+ * Estimate gas for linkEscrow operation
714
+ *
715
+ * @param txId - Transaction ID
716
+ * @returns Estimated gas limit and cost
717
+ */
718
+ async estimateLinkEscrowGas(txId) {
719
+ const feeData = await this.provider.getFeeData();
720
+ const gasPrice = feeData.gasPrice ?? 0n;
721
+ // linkEscrow includes USDC approve + contract call
722
+ const estimatedGasLimit = 200000n; // Conservative estimate
723
+ const gasCostWei = estimatedGasLimit * gasPrice;
724
+ const gasCostGwei = (Number(gasCostWei) / 1e9).toFixed(4);
725
+ return {
726
+ gasLimit: estimatedGasLimit,
727
+ gasCostWei,
728
+ gasCostGwei,
729
+ };
730
+ }
731
+ /**
732
+ * Estimate gas for state transition
733
+ *
734
+ * @param txId - Transaction ID
735
+ * @param newState - Target state
736
+ * @returns Estimated gas limit and cost
737
+ */
738
+ async estimateTransitionGas(txId, newState) {
739
+ const feeData = await this.provider.getFeeData();
740
+ const gasPrice = feeData.gasPrice ?? 0n;
741
+ // State transitions are relatively cheap
742
+ const estimatedGasLimit = 80000n; // Conservative estimate
743
+ const gasCostWei = estimatedGasLimit * gasPrice;
744
+ const gasCostGwei = (Number(gasCostWei) / 1e9).toFixed(4);
745
+ return {
746
+ gasLimit: estimatedGasLimit,
747
+ gasCostWei,
748
+ gasCostGwei,
749
+ };
750
+ }
751
+ /**
752
+ * Get current gas price information
753
+ *
754
+ * @returns Current gas price data
755
+ */
756
+ async getGasPrice() {
757
+ const feeData = await this.provider.getFeeData();
758
+ return {
759
+ gasPrice: feeData.gasPrice ?? 0n,
760
+ gasPriceGwei: ((Number(feeData.gasPrice ?? 0n) / 1e9)).toFixed(4),
761
+ maxFeePerGas: feeData.maxFeePerGas ?? undefined,
762
+ maxPriorityFeePerGas: feeData.maxPriorityFeePerGas ?? undefined,
763
+ };
764
+ }
765
+ }
766
+ exports.BlockchainRuntime = BlockchainRuntime;
767
+ //# sourceMappingURL=BlockchainRuntime.js.map