@agirails/sdk 2.0.0 → 2.0.1-beta

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/README.md +108 -116
  2. package/dist/ACTPClient.d.ts +33 -456
  3. package/dist/ACTPClient.d.ts.map +1 -1
  4. package/dist/ACTPClient.js +93 -477
  5. package/dist/ACTPClient.js.map +1 -1
  6. package/dist/abi/EscrowVault.json +38 -106
  7. package/dist/builders/DeliveryProofBuilder.d.ts +1 -60
  8. package/dist/builders/DeliveryProofBuilder.d.ts.map +1 -1
  9. package/dist/builders/DeliveryProofBuilder.js +5 -81
  10. package/dist/builders/DeliveryProofBuilder.js.map +1 -1
  11. package/dist/builders/QuoteBuilder.d.ts +0 -101
  12. package/dist/builders/QuoteBuilder.d.ts.map +1 -1
  13. package/dist/builders/QuoteBuilder.js +3 -120
  14. package/dist/builders/QuoteBuilder.js.map +1 -1
  15. package/dist/builders/index.d.ts +0 -4
  16. package/dist/builders/index.d.ts.map +1 -1
  17. package/dist/builders/index.js +0 -4
  18. package/dist/builders/index.js.map +1 -1
  19. package/dist/config/networks.d.ts +0 -28
  20. package/dist/config/networks.d.ts.map +1 -1
  21. package/dist/config/networks.js +12 -60
  22. package/dist/config/networks.js.map +1 -1
  23. package/dist/errors/index.d.ts +2 -165
  24. package/dist/errors/index.d.ts.map +1 -1
  25. package/dist/errors/index.js +2 -260
  26. package/dist/errors/index.js.map +1 -1
  27. package/dist/index.d.ts +13 -61
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +36 -141
  30. package/dist/index.js.map +1 -1
  31. package/dist/protocol/ACTPKernel.d.ts +2 -229
  32. package/dist/protocol/ACTPKernel.d.ts.map +1 -1
  33. package/dist/protocol/ACTPKernel.js +33 -367
  34. package/dist/protocol/ACTPKernel.js.map +1 -1
  35. package/dist/protocol/EASHelper.d.ts +2 -57
  36. package/dist/protocol/EASHelper.d.ts.map +1 -1
  37. package/dist/protocol/EASHelper.js +37 -230
  38. package/dist/protocol/EASHelper.js.map +1 -1
  39. package/dist/protocol/EscrowVault.d.ts +2 -93
  40. package/dist/protocol/EscrowVault.d.ts.map +1 -1
  41. package/dist/protocol/EscrowVault.js +33 -122
  42. package/dist/protocol/EscrowVault.js.map +1 -1
  43. package/dist/protocol/EventMonitor.d.ts +1 -45
  44. package/dist/protocol/EventMonitor.d.ts.map +1 -1
  45. package/dist/protocol/EventMonitor.js +8 -64
  46. package/dist/protocol/EventMonitor.js.map +1 -1
  47. package/dist/protocol/MessageSigner.d.ts +2 -116
  48. package/dist/protocol/MessageSigner.d.ts.map +1 -1
  49. package/dist/protocol/MessageSigner.js +9 -215
  50. package/dist/protocol/MessageSigner.js.map +1 -1
  51. package/dist/protocol/ProofGenerator.d.ts +0 -93
  52. package/dist/protocol/ProofGenerator.d.ts.map +1 -1
  53. package/dist/protocol/ProofGenerator.js +9 -194
  54. package/dist/protocol/ProofGenerator.js.map +1 -1
  55. package/dist/protocol/QuoteBuilder.d.ts +0 -8
  56. package/dist/protocol/QuoteBuilder.d.ts.map +1 -1
  57. package/dist/protocol/QuoteBuilder.js +0 -8
  58. package/dist/protocol/QuoteBuilder.js.map +1 -1
  59. package/dist/types/eip712.d.ts +0 -34
  60. package/dist/types/eip712.d.ts.map +1 -1
  61. package/dist/types/eip712.js +5 -31
  62. package/dist/types/eip712.js.map +1 -1
  63. package/dist/types/escrow.d.ts +10 -17
  64. package/dist/types/escrow.d.ts.map +1 -1
  65. package/dist/types/index.d.ts +0 -5
  66. package/dist/types/index.d.ts.map +1 -1
  67. package/dist/types/index.js +0 -8
  68. package/dist/types/index.js.map +1 -1
  69. package/dist/types/message.d.ts +0 -32
  70. package/dist/types/message.d.ts.map +1 -1
  71. package/dist/types/message.js +0 -4
  72. package/dist/types/message.js.map +1 -1
  73. package/dist/types/state.d.ts +0 -28
  74. package/dist/types/state.d.ts.map +1 -1
  75. package/dist/types/state.js +6 -37
  76. package/dist/types/state.js.map +1 -1
  77. package/dist/types/transaction.d.ts +0 -17
  78. package/dist/types/transaction.d.ts.map +1 -1
  79. package/dist/utils/IPFSClient.d.ts +0 -113
  80. package/dist/utils/IPFSClient.d.ts.map +1 -1
  81. package/dist/utils/IPFSClient.js +7 -128
  82. package/dist/utils/IPFSClient.js.map +1 -1
  83. package/dist/utils/NonceManager.d.ts +1 -234
  84. package/dist/utils/NonceManager.d.ts.map +1 -1
  85. package/dist/utils/NonceManager.js +7 -372
  86. package/dist/utils/NonceManager.js.map +1 -1
  87. package/dist/utils/ReceivedNonceTracker.d.ts +0 -175
  88. package/dist/utils/ReceivedNonceTracker.d.ts.map +1 -1
  89. package/dist/utils/ReceivedNonceTracker.js +5 -261
  90. package/dist/utils/ReceivedNonceTracker.js.map +1 -1
  91. package/dist/utils/canonicalJson.d.ts +0 -22
  92. package/dist/utils/canonicalJson.d.ts.map +1 -1
  93. package/dist/utils/canonicalJson.js +3 -26
  94. package/dist/utils/canonicalJson.js.map +1 -1
  95. package/dist/utils/computeTypeHash.d.ts +0 -14
  96. package/dist/utils/computeTypeHash.d.ts.map +1 -1
  97. package/dist/utils/computeTypeHash.js +2 -19
  98. package/dist/utils/computeTypeHash.js.map +1 -1
  99. package/dist/utils/validation.d.ts +0 -40
  100. package/dist/utils/validation.d.ts.map +1 -1
  101. package/dist/utils/validation.js +7 -184
  102. package/dist/utils/validation.js.map +1 -1
  103. package/package.json +37 -54
  104. package/src/ACTPClient.ts +178 -692
  105. package/src/__tests__/ProofGenerator.test.ts +124 -0
  106. package/src/__tests__/QuoteBuilder.test.ts +516 -0
  107. package/src/__tests__/StateMachine.test.ts +82 -0
  108. package/src/__tests__/builders/DeliveryProofBuilder.test.ts +581 -0
  109. package/src/__tests__/integration/ACTPClient.test.ts +263 -0
  110. package/src/__tests__/integration.test.ts +289 -0
  111. package/src/__tests__/protocol/EASHelper.test.ts +472 -0
  112. package/src/__tests__/protocol/EventMonitor.test.ts +382 -0
  113. package/src/__tests__/security/ACTPKernel.security.test.ts +1167 -0
  114. package/src/__tests__/security/EscrowVault.security.test.ts +570 -0
  115. package/src/__tests__/security/MessageSigner.security.test.ts +286 -0
  116. package/src/__tests__/security/NonceReplay.security.test.ts +501 -0
  117. package/src/__tests__/security/validation.security.test.ts +376 -0
  118. package/src/__tests__/utils/IPFSClient.test.ts +262 -0
  119. package/src/__tests__/utils/NonceManager.test.ts +205 -0
  120. package/src/__tests__/utils/canonicalJson.test.ts +153 -0
  121. package/src/abi/EscrowVault.json +38 -106
  122. package/src/builders/DeliveryProofBuilder.ts +2 -3
  123. package/src/config/networks.ts +9 -32
  124. package/src/errors/index.ts +1 -298
  125. package/src/index.ts +71 -207
  126. package/src/protocol/ACTPKernel.ts +23 -175
  127. package/src/protocol/EASHelper.ts +46 -230
  128. package/src/protocol/EscrowVault.ts +50 -68
  129. package/src/protocol/EventMonitor.ts +15 -44
  130. package/src/protocol/MessageSigner.ts +13 -193
  131. package/src/protocol/ProofGenerator.ts +4 -223
  132. package/src/types/escrow.ts +11 -12
  133. package/src/types/index.ts +1 -5
  134. package/src/types/state.ts +3 -12
  135. package/src/types/transaction.ts +1 -4
  136. package/src/utils/IPFSClient.ts +5 -122
  137. package/src/utils/NonceManager.ts +8 -305
  138. package/src/utils/ReceivedNonceTracker.ts +0 -170
  139. package/src/utils/validation.ts +0 -164
  140. package/LICENSE +0 -190
  141. package/bin/actp +0 -10
  142. package/dist/abi/AgentRegistry.json +0 -782
  143. package/dist/abi/IdentityRegistry.json +0 -316
  144. package/dist/adapters/BaseAdapter.d.ts +0 -231
  145. package/dist/adapters/BaseAdapter.d.ts.map +0 -1
  146. package/dist/adapters/BaseAdapter.js +0 -393
  147. package/dist/adapters/BaseAdapter.js.map +0 -1
  148. package/dist/adapters/BeginnerAdapter.d.ts +0 -152
  149. package/dist/adapters/BeginnerAdapter.d.ts.map +0 -1
  150. package/dist/adapters/BeginnerAdapter.js +0 -168
  151. package/dist/adapters/BeginnerAdapter.js.map +0 -1
  152. package/dist/adapters/IntermediateAdapter.d.ts +0 -211
  153. package/dist/adapters/IntermediateAdapter.d.ts.map +0 -1
  154. package/dist/adapters/IntermediateAdapter.js +0 -260
  155. package/dist/adapters/IntermediateAdapter.js.map +0 -1
  156. package/dist/adapters/index.d.ts +0 -15
  157. package/dist/adapters/index.d.ts.map +0 -1
  158. package/dist/adapters/index.js +0 -26
  159. package/dist/adapters/index.js.map +0 -1
  160. package/dist/cli/commands/balance.d.ts +0 -13
  161. package/dist/cli/commands/balance.d.ts.map +0 -1
  162. package/dist/cli/commands/balance.js +0 -89
  163. package/dist/cli/commands/balance.js.map +0 -1
  164. package/dist/cli/commands/batch.d.ts +0 -24
  165. package/dist/cli/commands/batch.d.ts.map +0 -1
  166. package/dist/cli/commands/batch.js +0 -424
  167. package/dist/cli/commands/batch.js.map +0 -1
  168. package/dist/cli/commands/config.d.ts +0 -13
  169. package/dist/cli/commands/config.d.ts.map +0 -1
  170. package/dist/cli/commands/config.js +0 -192
  171. package/dist/cli/commands/config.js.map +0 -1
  172. package/dist/cli/commands/init.d.ts +0 -19
  173. package/dist/cli/commands/init.d.ts.map +0 -1
  174. package/dist/cli/commands/init.js +0 -143
  175. package/dist/cli/commands/init.js.map +0 -1
  176. package/dist/cli/commands/mint.d.ts +0 -13
  177. package/dist/cli/commands/mint.d.ts.map +0 -1
  178. package/dist/cli/commands/mint.js +0 -91
  179. package/dist/cli/commands/mint.js.map +0 -1
  180. package/dist/cli/commands/pay.d.ts +0 -18
  181. package/dist/cli/commands/pay.d.ts.map +0 -1
  182. package/dist/cli/commands/pay.js +0 -87
  183. package/dist/cli/commands/pay.js.map +0 -1
  184. package/dist/cli/commands/simulate.d.ts +0 -32
  185. package/dist/cli/commands/simulate.d.ts.map +0 -1
  186. package/dist/cli/commands/simulate.js +0 -290
  187. package/dist/cli/commands/simulate.js.map +0 -1
  188. package/dist/cli/commands/time.d.ts +0 -29
  189. package/dist/cli/commands/time.d.ts.map +0 -1
  190. package/dist/cli/commands/time.js +0 -252
  191. package/dist/cli/commands/time.js.map +0 -1
  192. package/dist/cli/commands/tx.d.ts +0 -16
  193. package/dist/cli/commands/tx.d.ts.map +0 -1
  194. package/dist/cli/commands/tx.js +0 -379
  195. package/dist/cli/commands/tx.js.map +0 -1
  196. package/dist/cli/commands/watch.d.ts +0 -20
  197. package/dist/cli/commands/watch.d.ts.map +0 -1
  198. package/dist/cli/commands/watch.js +0 -160
  199. package/dist/cli/commands/watch.js.map +0 -1
  200. package/dist/cli/index.d.ts +0 -17
  201. package/dist/cli/index.d.ts.map +0 -1
  202. package/dist/cli/index.js +0 -104
  203. package/dist/cli/index.js.map +0 -1
  204. package/dist/cli/utils/client.d.ts +0 -70
  205. package/dist/cli/utils/client.d.ts.map +0 -1
  206. package/dist/cli/utils/client.js +0 -240
  207. package/dist/cli/utils/client.js.map +0 -1
  208. package/dist/cli/utils/config.d.ts +0 -91
  209. package/dist/cli/utils/config.d.ts.map +0 -1
  210. package/dist/cli/utils/config.js +0 -240
  211. package/dist/cli/utils/config.js.map +0 -1
  212. package/dist/cli/utils/output.d.ts +0 -174
  213. package/dist/cli/utils/output.d.ts.map +0 -1
  214. package/dist/cli/utils/output.js +0 -380
  215. package/dist/cli/utils/output.js.map +0 -1
  216. package/dist/level0/Provider.d.ts +0 -106
  217. package/dist/level0/Provider.d.ts.map +0 -1
  218. package/dist/level0/Provider.js +0 -10
  219. package/dist/level0/Provider.js.map +0 -1
  220. package/dist/level0/ServiceDirectory.d.ts +0 -74
  221. package/dist/level0/ServiceDirectory.d.ts.map +0 -1
  222. package/dist/level0/ServiceDirectory.js +0 -122
  223. package/dist/level0/ServiceDirectory.js.map +0 -1
  224. package/dist/level0/index.d.ts +0 -10
  225. package/dist/level0/index.d.ts.map +0 -1
  226. package/dist/level0/index.js +0 -15
  227. package/dist/level0/index.js.map +0 -1
  228. package/dist/level0/provide.d.ts +0 -51
  229. package/dist/level0/provide.d.ts.map +0 -1
  230. package/dist/level0/provide.js +0 -113
  231. package/dist/level0/provide.js.map +0 -1
  232. package/dist/level0/request.d.ts +0 -53
  233. package/dist/level0/request.d.ts.map +0 -1
  234. package/dist/level0/request.js +0 -462
  235. package/dist/level0/request.js.map +0 -1
  236. package/dist/level1/Agent.d.ts +0 -472
  237. package/dist/level1/Agent.d.ts.map +0 -1
  238. package/dist/level1/Agent.js +0 -1091
  239. package/dist/level1/Agent.js.map +0 -1
  240. package/dist/level1/index.d.ts +0 -10
  241. package/dist/level1/index.d.ts.map +0 -1
  242. package/dist/level1/index.js +0 -30
  243. package/dist/level1/index.js.map +0 -1
  244. package/dist/level1/pricing/PriceCalculator.d.ts +0 -62
  245. package/dist/level1/pricing/PriceCalculator.d.ts.map +0 -1
  246. package/dist/level1/pricing/PriceCalculator.js +0 -237
  247. package/dist/level1/pricing/PriceCalculator.js.map +0 -1
  248. package/dist/level1/pricing/PricingStrategy.d.ts +0 -179
  249. package/dist/level1/pricing/PricingStrategy.d.ts.map +0 -1
  250. package/dist/level1/pricing/PricingStrategy.js +0 -11
  251. package/dist/level1/pricing/PricingStrategy.js.map +0 -1
  252. package/dist/level1/types/Job.d.ts +0 -166
  253. package/dist/level1/types/Job.d.ts.map +0 -1
  254. package/dist/level1/types/Job.js +0 -11
  255. package/dist/level1/types/Job.js.map +0 -1
  256. package/dist/level1/types/Options.d.ts +0 -258
  257. package/dist/level1/types/Options.d.ts.map +0 -1
  258. package/dist/level1/types/Options.js +0 -8
  259. package/dist/level1/types/Options.js.map +0 -1
  260. package/dist/level1/types/index.d.ts +0 -8
  261. package/dist/level1/types/index.d.ts.map +0 -1
  262. package/dist/level1/types/index.js +0 -8
  263. package/dist/level1/types/index.js.map +0 -1
  264. package/dist/protocol/AgentRegistry.d.ts +0 -177
  265. package/dist/protocol/AgentRegistry.d.ts.map +0 -1
  266. package/dist/protocol/AgentRegistry.js +0 -449
  267. package/dist/protocol/AgentRegistry.js.map +0 -1
  268. package/dist/protocol/DIDManager.d.ts +0 -289
  269. package/dist/protocol/DIDManager.d.ts.map +0 -1
  270. package/dist/protocol/DIDManager.js +0 -481
  271. package/dist/protocol/DIDManager.js.map +0 -1
  272. package/dist/protocol/DIDResolver.d.ts +0 -236
  273. package/dist/protocol/DIDResolver.d.ts.map +0 -1
  274. package/dist/protocol/DIDResolver.js +0 -495
  275. package/dist/protocol/DIDResolver.js.map +0 -1
  276. package/dist/runtime/BlockchainRuntime.d.ts +0 -360
  277. package/dist/runtime/BlockchainRuntime.d.ts.map +0 -1
  278. package/dist/runtime/BlockchainRuntime.js +0 -767
  279. package/dist/runtime/BlockchainRuntime.js.map +0 -1
  280. package/dist/runtime/IACTPRuntime.d.ts +0 -271
  281. package/dist/runtime/IACTPRuntime.d.ts.map +0 -1
  282. package/dist/runtime/IACTPRuntime.js +0 -15
  283. package/dist/runtime/IACTPRuntime.js.map +0 -1
  284. package/dist/runtime/MockRuntime.d.ts +0 -445
  285. package/dist/runtime/MockRuntime.d.ts.map +0 -1
  286. package/dist/runtime/MockRuntime.js +0 -1065
  287. package/dist/runtime/MockRuntime.js.map +0 -1
  288. package/dist/runtime/MockStateManager.d.ts +0 -233
  289. package/dist/runtime/MockStateManager.d.ts.map +0 -1
  290. package/dist/runtime/MockStateManager.js +0 -533
  291. package/dist/runtime/MockStateManager.js.map +0 -1
  292. package/dist/runtime/index.d.ts +0 -14
  293. package/dist/runtime/index.d.ts.map +0 -1
  294. package/dist/runtime/index.js +0 -42
  295. package/dist/runtime/index.js.map +0 -1
  296. package/dist/runtime/types/MockState.d.ts +0 -167
  297. package/dist/runtime/types/MockState.d.ts.map +0 -1
  298. package/dist/runtime/types/MockState.js +0 -43
  299. package/dist/runtime/types/MockState.js.map +0 -1
  300. package/dist/types/agent.d.ts +0 -76
  301. package/dist/types/agent.d.ts.map +0 -1
  302. package/dist/types/agent.js +0 -8
  303. package/dist/types/agent.js.map +0 -1
  304. package/dist/types/did.d.ts +0 -192
  305. package/dist/types/did.d.ts.map +0 -1
  306. package/dist/types/did.js +0 -38
  307. package/dist/types/did.js.map +0 -1
  308. package/dist/utils/ErrorRecoveryGuide.d.ts +0 -125
  309. package/dist/utils/ErrorRecoveryGuide.d.ts.map +0 -1
  310. package/dist/utils/ErrorRecoveryGuide.js +0 -579
  311. package/dist/utils/ErrorRecoveryGuide.js.map +0 -1
  312. package/dist/utils/Helpers.d.ts +0 -453
  313. package/dist/utils/Helpers.d.ts.map +0 -1
  314. package/dist/utils/Helpers.js +0 -623
  315. package/dist/utils/Helpers.js.map +0 -1
  316. package/dist/utils/Logger.d.ts +0 -195
  317. package/dist/utils/Logger.d.ts.map +0 -1
  318. package/dist/utils/Logger.js +0 -382
  319. package/dist/utils/Logger.js.map +0 -1
  320. package/dist/utils/RateLimiter.d.ts +0 -253
  321. package/dist/utils/RateLimiter.d.ts.map +0 -1
  322. package/dist/utils/RateLimiter.js +0 -424
  323. package/dist/utils/RateLimiter.js.map +0 -1
  324. package/dist/utils/SDKLifecycle.d.ts +0 -156
  325. package/dist/utils/SDKLifecycle.d.ts.map +0 -1
  326. package/dist/utils/SDKLifecycle.js +0 -347
  327. package/dist/utils/SDKLifecycle.js.map +0 -1
  328. package/dist/utils/SecureNonce.d.ts +0 -57
  329. package/dist/utils/SecureNonce.d.ts.map +0 -1
  330. package/dist/utils/SecureNonce.js +0 -80
  331. package/dist/utils/SecureNonce.js.map +0 -1
  332. package/dist/utils/Semaphore.d.ts +0 -123
  333. package/dist/utils/Semaphore.d.ts.map +0 -1
  334. package/dist/utils/Semaphore.js +0 -247
  335. package/dist/utils/Semaphore.js.map +0 -1
  336. package/dist/utils/UsedAttestationTracker.d.ts +0 -167
  337. package/dist/utils/UsedAttestationTracker.d.ts.map +0 -1
  338. package/dist/utils/UsedAttestationTracker.js +0 -309
  339. package/dist/utils/UsedAttestationTracker.js.map +0 -1
  340. package/dist/utils/fsSafe.d.ts +0 -14
  341. package/dist/utils/fsSafe.d.ts.map +0 -1
  342. package/dist/utils/fsSafe.js +0 -89
  343. package/dist/utils/fsSafe.js.map +0 -1
  344. package/dist/utils/index.d.ts +0 -15
  345. package/dist/utils/index.d.ts.map +0 -1
  346. package/dist/utils/index.js +0 -51
  347. package/dist/utils/index.js.map +0 -1
  348. package/dist/utils/security.d.ts +0 -147
  349. package/dist/utils/security.d.ts.map +0 -1
  350. package/dist/utils/security.js +0 -391
  351. package/dist/utils/security.js.map +0 -1
  352. package/src/abi/AgentRegistry.json +0 -782
  353. package/src/abi/IdentityRegistry.json +0 -316
  354. package/src/adapters/BaseAdapter.ts +0 -473
  355. package/src/adapters/BeginnerAdapter.ts +0 -232
  356. package/src/adapters/IntermediateAdapter.ts +0 -316
  357. package/src/adapters/index.ts +0 -25
  358. package/src/cli/commands/balance.ts +0 -110
  359. package/src/cli/commands/batch.ts +0 -487
  360. package/src/cli/commands/config.ts +0 -231
  361. package/src/cli/commands/init.ts +0 -161
  362. package/src/cli/commands/mint.ts +0 -116
  363. package/src/cli/commands/pay.ts +0 -113
  364. package/src/cli/commands/simulate.ts +0 -345
  365. package/src/cli/commands/time.ts +0 -303
  366. package/src/cli/commands/tx.ts +0 -448
  367. package/src/cli/commands/watch.ts +0 -211
  368. package/src/cli/index.ts +0 -116
  369. package/src/cli/utils/client.ts +0 -249
  370. package/src/cli/utils/config.ts +0 -282
  371. package/src/cli/utils/output.ts +0 -465
  372. package/src/level0/Provider.ts +0 -117
  373. package/src/level0/ServiceDirectory.ts +0 -131
  374. package/src/level0/index.ts +0 -10
  375. package/src/level0/provide.ts +0 -131
  376. package/src/level0/request.ts +0 -494
  377. package/src/level1/Agent.ts +0 -1432
  378. package/src/level1/index.ts +0 -10
  379. package/src/level1/pricing/PriceCalculator.ts +0 -255
  380. package/src/level1/pricing/PricingStrategy.ts +0 -198
  381. package/src/level1/types/Job.ts +0 -179
  382. package/src/level1/types/Options.ts +0 -291
  383. package/src/level1/types/index.ts +0 -8
  384. package/src/protocol/AgentRegistry.ts +0 -559
  385. package/src/protocol/DIDManager.ts +0 -629
  386. package/src/protocol/DIDResolver.ts +0 -554
  387. package/src/runtime/BlockchainRuntime.ts +0 -993
  388. package/src/runtime/IACTPRuntime.ts +0 -284
  389. package/src/runtime/MockRuntime.ts +0 -1244
  390. package/src/runtime/MockStateManager.ts +0 -576
  391. package/src/runtime/index.ts +0 -25
  392. package/src/runtime/types/MockState.ts +0 -227
  393. package/src/types/agent.ts +0 -79
  394. package/src/types/did.ts +0 -223
  395. package/src/utils/ErrorRecoveryGuide.ts +0 -675
  396. package/src/utils/Helpers.ts +0 -688
  397. package/src/utils/Logger.ts +0 -484
  398. package/src/utils/RateLimiter.ts +0 -534
  399. package/src/utils/SDKLifecycle.ts +0 -416
  400. package/src/utils/SecureNonce.ts +0 -78
  401. package/src/utils/Semaphore.ts +0 -276
  402. package/src/utils/UsedAttestationTracker.ts +0 -387
  403. package/src/utils/fsSafe.ts +0 -75
  404. package/src/utils/index.ts +0 -80
  405. package/src/utils/security.ts +0 -418
@@ -20,19 +20,15 @@ interface GasOptions {
20
20
  /**
21
21
  * EscrowVault - Escrow contract wrapper
22
22
  *
23
- * IMPORTANT:
24
- * - Escrow creation happens atomically inside `ACTPKernel.linkEscrow()`.
25
- * - Payout/refund functions are `onlyKernel` on-chain and MUST NOT be called by users.
26
- *
27
- * This module provides:
28
- * - Helper methods for USDC approvals (requester → EscrowVault allowance)
29
- * - Read-only access to escrow state (`escrows()` / `remaining()`)
23
+ * IMPORTANT: Per AIP-3 specification, escrow creation happens atomically
24
+ * inside ACTPKernel.linkEscrow(). This module provides read-only access
25
+ * to escrow state and helper methods for USDC approvals.
30
26
  *
31
27
  * Workflow (per AIP-3):
32
28
  * 1. Consumer approves USDC to EscrowVault address (use approveToken)
33
29
  * 2. Consumer calls ACTPKernel.linkEscrow(txId, escrowVault, escrowId)
34
- * 3. Kernel internally calls IEscrowValidator.createEscrow(escrowId, requester, provider, amount)
35
- * 4. Escrow pulls USDC from requester
30
+ * 3. Kernel internally calls EscrowVault.createEscrow() (onlyKernel modifier)
31
+ * 4. Escrow pulls USDC from consumer and auto-transitions to COMMITTED
36
32
  *
37
33
  * Reference: AIP-3 §3.2 (Escrow Linking Workflow), lines 258-336
38
34
  */
@@ -56,6 +52,7 @@ export class EscrowVault {
56
52
  */
57
53
  private getGasBufferMultiplier(operation: string): number {
58
54
  const buffers: Record<string, number> = {
55
+ 'releaseEscrow': 1.30, // 30% - Multi-recipient disbursement
59
56
  'approveToken': 1.20 // 20% - Standard ERC20 approval
60
57
  };
61
58
 
@@ -65,35 +62,12 @@ export class EscrowVault {
65
62
  /**
66
63
  * Build transaction options with gas settings and estimated gas
67
64
  * V6 Enhancement: Dynamic buffer based on operation type
68
- *
69
- * SECURITY FIX (NEW-C-1): Gas estimation manipulation attack protection
70
- * - Enforces minimum gas floor regardless of estimate
71
- * - Uses safe BigInt arithmetic with overflow detection
72
65
  */
73
66
  private buildTxOptions(estimatedGas: bigint, operation: string = 'default'): any {
74
- // SECURITY FIX (NEW-C-1): Minimum gas floor to prevent manipulation
75
- // Malicious contracts could return artificially low gas estimates
76
- const MIN_GAS_FLOOR = 100000n;
77
- const safeEstimate = estimatedGas > MIN_GAS_FLOOR ? estimatedGas : MIN_GAS_FLOOR;
78
-
79
67
  const bufferMultiplier = this.getGasBufferMultiplier(operation);
80
68
 
81
- // SECURITY FIX (NEW-H-1): Safe BigInt arithmetic with overflow check
82
- // Use 10000 denominator to avoid floating point precision issues
83
- const bufferNumerator = BigInt(Math.floor(bufferMultiplier * 10000));
84
- const bufferDenominator = 10000n;
85
- const gasLimit = (safeEstimate * bufferNumerator) / bufferDenominator;
86
-
87
- // Overflow detection: result should always be >= original estimate
88
- if (gasLimit < safeEstimate) {
89
- throw new Error(
90
- `Gas calculation overflow detected for operation ${operation}. ` +
91
- `Estimate: ${safeEstimate}, Buffer: ${bufferMultiplier}x, Result: ${gasLimit}`
92
- );
93
- }
94
-
95
69
  const options: any = {
96
- gasLimit
70
+ gasLimit: (estimatedGas * BigInt(Math.round(bufferMultiplier * 100))) / 100n
97
71
  };
98
72
 
99
73
  if (this.gasSettings?.maxFeePerGas) {
@@ -113,18 +87,6 @@ export class EscrowVault {
113
87
  return this.address;
114
88
  }
115
89
 
116
- /**
117
- * Get the underlying ethers Contract instance.
118
- *
119
- * SECURITY FIX (C-3): Provides public access to contract for EventMonitor
120
- * instead of accessing private field via bracket notation.
121
- *
122
- * @returns ethers Contract instance
123
- */
124
- getContract(): Contract {
125
- return this.contract;
126
- }
127
-
128
90
  /**
129
91
  * Approve USDC token for escrow creation
130
92
  *
@@ -189,48 +151,68 @@ export class EscrowVault {
189
151
  * Get escrow details
190
152
  */
191
153
  async getEscrow(escrowId: string): Promise<Escrow> {
192
- validateTxId(escrowId, 'escrowId');
193
154
  const escrowData = await this.contract.escrows(escrowId);
194
155
 
195
156
  return {
196
157
  escrowId,
197
- requester: escrowData.requester,
198
- provider: escrowData.provider,
158
+ kernel: escrowData.kernel,
159
+ txId: escrowData.txId,
160
+ token: escrowData.token,
199
161
  amount: escrowData.amount,
200
- releasedAmount: escrowData.releasedAmount,
201
- active: escrowData.active
162
+ beneficiary: escrowData.beneficiary,
163
+ createdAt: 0, // Not exposed in minimal ABI
164
+ released: escrowData.released
202
165
  };
203
166
  }
204
167
 
205
168
  /**
206
- * Get escrow remaining balance (amount - releasedAmount)
169
+ * Get escrow balance
207
170
  */
208
171
  async getEscrowBalance(escrowId: string): Promise<bigint> {
209
- validateTxId(escrowId, 'escrowId');
210
- return await this.contract.remaining(escrowId);
172
+ const escrow = await this.getEscrow(escrowId);
173
+ return escrow.amount;
211
174
  }
212
175
 
213
176
  /**
214
- * @deprecated
215
- *
216
- * Payouts/refunds are executed by ACTPKernel (on-chain) as part of state transitions.
217
- * EscrowVault disbursement methods are `onlyKernel` and cannot be called by EOAs.
218
- *
219
- * Use:
220
- * - `BlockchainRuntime.releaseEscrow(txId, attestationUID?)` (recommended)
221
- * - or `ACTPKernel.transitionState(txId, State.SETTLED, proof)` (advanced)
177
+ * Release escrow to recipients
178
+ * Note: Only callable by authorized kernel
222
179
  */
223
180
  async releaseEscrow(
224
181
  escrowId: string,
225
- _recipients: string[],
226
- _amounts: bigint[]
182
+ recipients: string[],
183
+ amounts: bigint[]
227
184
  ): Promise<void> {
185
+ // Input validation
228
186
  validateTxId(escrowId, 'escrowId');
229
- throw new ValidationError(
230
- 'EscrowVault.releaseEscrow',
231
- 'Escrow payouts are performed by ACTPKernel (onlyKernel). ' +
232
- 'Use BlockchainRuntime.releaseEscrow(txId, attestationUID?) or ACTPKernel.transitionState(txId, SETTLED).'
233
- );
187
+
188
+ if (recipients.length !== amounts.length) {
189
+ throw new ValidationError('recipients/amounts', 'Recipients and amounts length mismatch');
190
+ }
191
+
192
+ if (recipients.length === 0) {
193
+ throw new ValidationError('recipients', 'Must provide at least one recipient');
194
+ }
195
+
196
+ // Validate each recipient and amount
197
+ recipients.forEach((recipient, i) => {
198
+ validateAddress(recipient, `recipients[${i}]`);
199
+ validateAmount(amounts[i], `amounts[${i}]`);
200
+ });
201
+
202
+ try {
203
+ // ethers v6: use getFunction()
204
+ const disburseFunc = this.contract.getFunction('disburse');
205
+
206
+ // Estimate gas with safety buffer (30% for multi-recipient disbursement)
207
+ const estimatedGas = await disburseFunc.estimateGas(escrowId, recipients, amounts);
208
+ const txOptions = this.buildTxOptions(estimatedGas, 'releaseEscrow');
209
+
210
+ const tx = await disburseFunc(escrowId, recipients, amounts, txOptions);
211
+
212
+ await tx.wait();
213
+ } catch (error: any) {
214
+ throw new TransactionRevertedError(error.transactionHash, error.reason || error.message);
215
+ }
234
216
  }
235
217
 
236
218
  /**
@@ -3,12 +3,6 @@ import { State, Transaction } from '../types';
3
3
 
4
4
  /**
5
5
  * EventMonitor - Listen to blockchain events
6
- *
7
- * SECURITY FIX (EVENT-MONITOR): Corrected event parameter order to match ABI.
8
- * Per ACTPKernel.json, TransactionCreated signature is:
9
- * (bytes32 indexed transactionId, address indexed requester, address indexed provider, uint256 amount, bytes32 serviceHash)
10
- *
11
- * Previous code had requester/provider swapped which caused wrong filter results.
12
6
  */
13
7
  export class EventMonitor {
14
8
  constructor(
@@ -61,29 +55,18 @@ export class EventMonitor {
61
55
 
62
56
  /**
63
57
  * Get all transactions for an address
64
- *
65
- * SECURITY FIX (EVENT-MONITOR): Corrected filter parameter order.
66
- * Per ACTPKernel.json ABI, TransactionCreated event signature is:
67
- * (bytes32 indexed transactionId, address indexed requester, address indexed provider, uint256 amount, bytes32 serviceHash)
68
- *
69
- * Filter order: TransactionCreated(txId, requester, provider)
70
- * - To filter by requester: (null, address, null)
71
- * - To filter by provider: (null, null, address)
72
- *
73
- * SECURITY FIX (EVENT-MONITOR): Use getTransaction() instead of transactions()
74
- * The kernel contract exposes getTransaction(bytes32) not transactions(bytes32).
58
+ * Fixed: Correct filter parameters (txId, provider, requester, amount)
75
59
  */
76
60
  async getTransactionHistory(
77
61
  address: string,
78
62
  role: 'requester' | 'provider' = 'requester'
79
63
  ): Promise<Transaction[]> {
80
- // TransactionCreated event signature per ABI:
81
- // (bytes32 indexed transactionId, address indexed requester, address indexed provider, uint256 amount, bytes32 serviceHash)
82
- // Filter format: TransactionCreated(txId, requester, provider)
64
+ // TransactionCreated event signature: (bytes32 indexed txId, address indexed provider, address indexed requester, uint256 amount)
65
+ // Filter format: TransactionCreated(txId, provider, requester)
83
66
  const filter =
84
67
  role === 'requester'
85
- ? this.kernelContract.filters.TransactionCreated(null, address, null) // Match requester (2nd indexed param)
86
- : this.kernelContract.filters.TransactionCreated(null, null, address); // Match provider (3rd indexed param)
68
+ ? this.kernelContract.filters.TransactionCreated(null, null, address) // Match requester
69
+ : this.kernelContract.filters.TransactionCreated(null, address, null); // Match provider
87
70
 
88
71
  const events = await this.kernelContract.queryFilter(filter);
89
72
 
@@ -94,28 +77,20 @@ export class EventMonitor {
94
77
  throw new Error('Event does not contain args (not an EventLog)');
95
78
  }
96
79
  const txId = (event as EventLog).args?.transactionId;
97
-
98
- // SECURITY FIX: Use getTransaction() - the actual ABI function
99
- // Previous code called transactions(txId) which doesn't exist in ABI
100
- const txData = await this.kernelContract.getTransaction(txId);
80
+ const txData = await this.kernelContract.transactions(txId);
101
81
 
102
82
  return {
103
- txId: txData.transactionId || txId,
83
+ txId: txData.transactionId,
104
84
  requester: txData.requester,
105
85
  provider: txData.provider,
106
86
  amount: txData.amount,
107
- state: (typeof txData.state === 'bigint' ? Number(txData.state) : txData.state) as State,
87
+ state: txData.state as State,
108
88
  createdAt: Number(txData.createdAt),
109
- updatedAt: Number(txData.updatedAt),
110
89
  deadline: Number(txData.deadline),
111
90
  disputeWindow: Number(txData.disputeWindow),
112
91
  escrowContract: txData.escrowContract,
113
92
  escrowId: txData.escrowId,
114
- serviceHash: txData.serviceHash,
115
- attestationUID: txData.attestationUID,
116
- // Use metadata field (quote hash for QUOTED state) if available, fallback to serviceHash
117
- metadata: txData.metadata || txData.serviceHash,
118
- platformFeeBpsLocked: Number(txData.platformFeeBpsLocked)
93
+ metadata: txData.serviceHash
119
94
  };
120
95
  })
121
96
  );
@@ -123,25 +98,21 @@ export class EventMonitor {
123
98
 
124
99
  /**
125
100
  * Subscribe to transaction creation events
126
- *
127
- * SECURITY FIX (EVENT-MONITOR): Corrected event parameter order.
128
- * Per ACTPKernel.json ABI:
129
- * TransactionCreated(bytes32 indexed transactionId, address indexed requester, address indexed provider, uint256 amount, bytes32 serviceHash)
101
+ * Fixed: Correct event parameter order (txId, provider, requester, amount)
130
102
  */
131
103
  onTransactionCreated(
132
- callback: (tx: { txId: string; requester: string; provider: string; amount: bigint; serviceHash?: string }) => void
104
+ callback: (tx: { txId: string; provider: string; requester: string; amount: bigint }) => void
133
105
  ): () => void {
134
106
  const filter = this.kernelContract.filters.TransactionCreated();
135
107
 
136
- // Event signature per ABI: (txId, requester, provider, amount, serviceHash)
108
+ // Event signature: TransactionCreated(bytes32 indexed txId, address indexed provider, address indexed requester, uint256 amount)
137
109
  const listener = async (
138
110
  txId: string,
139
- requester: string,
140
111
  provider: string,
141
- amount: bigint,
142
- serviceHash?: string
112
+ requester: string,
113
+ amount: bigint
143
114
  ) => {
144
- callback({ txId, requester, provider, amount, serviceHash });
115
+ callback({ txId, provider, requester, amount });
145
116
  };
146
117
 
147
118
  this.kernelContract.on(filter, listener);
@@ -32,79 +32,15 @@ interface SignerWithTypedData extends Signer {
32
32
  * Reference: Yellow Paper §11.4.2
33
33
  *
34
34
  * V4 Security Enhancement: Optional nonce replay protection via ReceivedNonceTracker
35
- *
36
- * IMPORTANT: Use MessageSigner.create() factory method to ensure domain is initialized.
37
35
  */
38
36
  export class MessageSigner {
39
37
  private domain: EIP712Domain | null = null;
40
38
 
41
- /**
42
- * SECURITY FIX (H-5): Private constructor - MUST use MessageSigner.create() factory method
43
- *
44
- * This ensures EIP-712 domain is ALWAYS initialized before use (prevents race conditions).
45
- * Direct construction would allow calling sign/verify without domain initialization.
46
- */
47
- private constructor(
39
+ constructor(
48
40
  private readonly signer: Signer,
49
41
  private readonly nonceTracker?: IReceivedNonceTracker
50
42
  ) {}
51
43
 
52
- /**
53
- * SECURITY FIX (H-4): Factory method to create MessageSigner with guaranteed domain initialization
54
- *
55
- * This factory ensures the EIP-712 domain is always properly initialized before use.
56
- * Prevents the common bug of calling sign/verify without initializing domain first.
57
- *
58
- * @param signer - Ethers signer for signing messages
59
- * @param kernelAddress - Address of ACTP Kernel contract (for domain separation)
60
- * @param options - Optional configuration (chainId, nonceTracker)
61
- * @returns Promise resolving to initialized MessageSigner
62
- *
63
- * @example
64
- * ```typescript
65
- * const messageSigner = await MessageSigner.create(
66
- * signer,
67
- * KERNEL_ADDRESS,
68
- * { chainId: 84532 }
69
- * );
70
- * const signature = await messageSigner.signMessage(message);
71
- * ```
72
- */
73
- static async create(
74
- signer: Signer,
75
- kernelAddress: string,
76
- options?: {
77
- chainId?: number;
78
- nonceTracker?: IReceivedNonceTracker;
79
- }
80
- ): Promise<MessageSigner> {
81
- const messageSigner = new MessageSigner(signer, options?.nonceTracker);
82
- await messageSigner.initDomain(kernelAddress, options?.chainId);
83
- return messageSigner;
84
- }
85
-
86
- /**
87
- * Check if domain is initialized
88
- * @returns true if domain has been initialized
89
- */
90
- isDomainInitialized(): boolean {
91
- return this.domain !== null;
92
- }
93
-
94
- /**
95
- * Get the current domain (throws if not initialized)
96
- * @returns Current EIP-712 domain
97
- * @throws Error if domain not initialized
98
- */
99
- getDomain(): EIP712Domain {
100
- if (!this.domain) {
101
- throw new Error(
102
- 'Domain not initialized. Use MessageSigner.create() factory or call initDomain() first.'
103
- );
104
- }
105
- return this.domain;
106
- }
107
-
108
44
  /**
109
45
  * Initialize EIP-712 domain (must be called before signing)
110
46
  * @param kernelAddress - Address of ACTP Kernel contract
@@ -131,10 +67,8 @@ export class MessageSigner {
131
67
  }
132
68
  }
133
69
 
134
- // SECURITY FIX (H-6): Standardize domain name to 'AGIRAILS' for brand consistency
135
- // Note: This change requires coordination with any existing signed messages
136
70
  this.domain = {
137
- name: 'AGIRAILS',
71
+ name: 'ACTP',
138
72
  version: '1.0',
139
73
  chainId: resolvedChainId,
140
74
  verifyingContract: kernelAddress
@@ -144,55 +78,17 @@ export class MessageSigner {
144
78
  /**
145
79
  * Sign ACTP message using EIP-712 typed data
146
80
  * Uses ECDSA (secp256k1) with domain separation per Yellow Paper §11.4.2
147
- *
148
- * SECURITY FIX (H-3): Validates nonce format and warns about sequential nonces
149
- *
81
+ *
150
82
  * Generic ACTPMessage format (backward compatible).
151
83
  * For strict typed AIP messages, use signQuoteRequest/signQuoteResponse/signDeliveryProof
152
84
  */
153
85
  async signMessage(message: ACTPMessage): Promise<string> {
154
86
  if (!this.domain) {
155
- throw new Error(
156
- 'Domain not initialized. Use MessageSigner.create() factory or call initDomain() first.'
157
- );
87
+ throw new Error('Domain not initialized. Call initDomain() first.');
158
88
  }
159
89
 
160
90
  const { type, version, from, to, timestamp, nonce, signature, ...payload } = message;
161
91
 
162
- // SECURITY FIX (H-3): Validate nonce format (must be bytes32)
163
- if (!nonce || !/^0x[a-fA-F0-9]{64}$/.test(nonce)) {
164
- throw new Error(
165
- `Invalid nonce format: "${nonce}". ` +
166
- `Nonce MUST be a bytes32 hex string (0x + 64 hex chars). ` +
167
- `Use SecureNonce.generateSecureNonce() to generate cryptographically secure nonces. ` +
168
- `Never use sequential integers (1, 2, 3...) or timestamps as nonces.`
169
- );
170
- }
171
-
172
- // SECURITY FIX (H-3): Warn about sequential nonces (low entropy)
173
- // Sequential nonces like 0x0000...0001, 0x0000...0002 are weak
174
- // Check if nonce has low entropy (e.g., last 8 bytes are zero, or all same digits)
175
- const nonceValue = BigInt(nonce);
176
- if (nonceValue < 0xFFFFFFFFn) {
177
- // Nonce is suspiciously small (< 4 billion = likely sequential)
178
- console.warn(
179
- `[SECURITY WARNING] Nonce ${nonce} appears to be sequential (value < 2^32). ` +
180
- `This makes replay attacks easier. ` +
181
- `Use SecureNonce.generateSecureNonce() for cryptographically secure random nonces.`
182
- );
183
- }
184
-
185
- // Check if nonce has all same digits (e.g., 0x111...111 or 0x000...000)
186
- const hexDigits = nonce.slice(2); // Remove '0x'
187
- const firstDigit = hexDigits[0];
188
- if (hexDigits.split('').every(d => d === firstDigit)) {
189
- console.warn(
190
- `[SECURITY WARNING] Nonce ${nonce} has low entropy (all digits are '${firstDigit}'). ` +
191
- `This is NOT cryptographically secure. ` +
192
- `Use SecureNonce.generateSecureNonce() instead.`
193
- );
194
- }
195
-
196
92
  // Generic ACTPMessage with payload encoding (backward compatible)
197
93
  const abiCoder = AbiCoder.defaultAbiCoder();
198
94
  const payloadBytes = abiCoder.encode(
@@ -225,9 +121,7 @@ export class MessageSigner {
225
121
  */
226
122
  async signQuoteRequest(data: QuoteRequestData): Promise<string> {
227
123
  if (!this.domain) {
228
- throw new Error(
229
- 'Domain not initialized. Use MessageSigner.create() factory or call initDomain() first.'
230
- );
124
+ throw new Error('Domain not initialized. Call initDomain() first.');
231
125
  }
232
126
 
233
127
  const messageTypes = getMessageTypes('quote.request');
@@ -240,9 +134,7 @@ export class MessageSigner {
240
134
  */
241
135
  async signQuoteResponse(data: QuoteResponseData): Promise<string> {
242
136
  if (!this.domain) {
243
- throw new Error(
244
- 'Domain not initialized. Use MessageSigner.create() factory or call initDomain() first.'
245
- );
137
+ throw new Error('Domain not initialized. Call initDomain() first.');
246
138
  }
247
139
 
248
140
  const messageTypes = getMessageTypes('quote.response');
@@ -255,9 +147,7 @@ export class MessageSigner {
255
147
  */
256
148
  async signDeliveryProof(data: DeliveryProofData): Promise<string> {
257
149
  if (!this.domain) {
258
- throw new Error(
259
- 'Domain not initialized. Use MessageSigner.create() factory or call initDomain() first.'
260
- );
150
+ throw new Error('Domain not initialized. Call initDomain() first.');
261
151
  }
262
152
 
263
153
  const messageTypes = getMessageTypes('delivery.proof');
@@ -281,9 +171,7 @@ export class MessageSigner {
281
171
  */
282
172
  async verifySignature(message: ACTPMessage, signature: string): Promise<boolean> {
283
173
  if (!this.domain) {
284
- throw new Error(
285
- 'Domain not initialized. Use MessageSigner.create() factory or call initDomain() first.'
286
- );
174
+ throw new Error('Domain not initialized. Call initDomain() first.');
287
175
  }
288
176
 
289
177
  const { type, version, from, to, timestamp, nonce, signature: _, ...payload } = message;
@@ -420,97 +308,29 @@ export class MessageSigner {
420
308
 
421
309
  /**
422
310
  * Convert DID to Ethereum address
423
- *
424
- * SECURITY FIX (DID-FORMAT): Handles both DID formats:
425
- * - Legacy: did:ethr:<address>
426
- * - Canonical (EIP-3770): did:ethr:<chainId>:<address>
427
- *
428
- * Examples:
429
- * - "did:ethr:0x1234...abcd" → "0x1234...abcd"
430
- * - "did:ethr:84532:0x1234...abcd" → "0x1234...abcd"
431
- * - "0x1234...abcd" → "0x1234...abcd" (raw address passthrough)
311
+ * MVP: Simple did:ethr → address conversion
432
312
  */
433
313
  private didToAddress(did: string): string {
434
- // Check for DID format first
435
- const DID_PREFIX = 'did:ethr:';
436
- if (did.startsWith(DID_PREFIX)) {
437
- const remainder = did.slice(DID_PREFIX.length);
438
-
439
- // Check if it's canonical format: did:ethr:<chainId>:<address>
440
- // chainId is numeric, address starts with 0x
441
- const parts = remainder.split(':');
442
-
443
- if (parts.length === 2) {
444
- // Canonical format: did:ethr:<chainId>:<address>
445
- const [chainIdStr, address] = parts;
446
- const chainId = parseInt(chainIdStr, 10);
447
-
448
- if (isNaN(chainId)) {
449
- throw new Error(
450
- `Invalid DID format: ${did}. ` +
451
- `Expected did:ethr:<chainId>:<address> but chainId "${chainIdStr}" is not a number.`
452
- );
453
- }
454
-
455
- if (!ethers.isAddress(address)) {
456
- throw new Error(
457
- `Invalid DID format: ${did}. ` +
458
- `Expected did:ethr:<chainId>:<address> but "${address}" is not a valid Ethereum address.`
459
- );
460
- }
461
-
462
- // SECURITY: Optionally validate chainId matches domain chainId
463
- // This prevents cross-chain replay attacks where a message signed for one chain
464
- // is replayed on another. For now, we just extract the address but log a warning.
465
- if (this.domain && this.domain.chainId !== chainId) {
466
- console.warn(
467
- `[SECURITY WARNING] DID chainId (${chainId}) does not match domain chainId (${this.domain.chainId}). ` +
468
- `This could indicate a cross-chain replay attempt. DID: ${did}`
469
- );
470
- }
471
-
472
- return address;
473
- } else if (parts.length === 1 && ethers.isAddress(parts[0])) {
474
- // Legacy format: did:ethr:<address>
475
- return parts[0];
476
- } else {
477
- throw new Error(
478
- `Invalid DID format: ${did}. ` +
479
- `Expected did:ethr:<address> or did:ethr:<chainId>:<address>.`
480
- );
481
- }
314
+ if (did.startsWith('did:ethr:')) {
315
+ return did.replace('did:ethr:', '');
482
316
  }
483
317
 
484
- // If already an address (raw 0x format), return as-is
318
+ // If already an address, return as-is
485
319
  if (ethers.isAddress(did)) {
486
320
  return did;
487
321
  }
488
322
 
489
- throw new Error(
490
- `Invalid DID format: ${did}. ` +
491
- `Expected Ethereum address (0x...) or DID (did:ethr:...).`
492
- );
323
+ throw new Error(`Invalid DID format: ${did}`);
493
324
  }
494
325
 
495
326
  /**
496
327
  * Convert Ethereum address to DID
497
- *
498
- * SECURITY FIX (DID-FORMAT): Now generates canonical DID format
499
- * with chainId when domain is initialized: did:ethr:<chainId>:<address>
500
- *
501
- * Falls back to legacy format if domain not initialized.
502
328
  */
503
329
  addressToDID(address: string): string {
504
330
  if (!ethers.isAddress(address)) {
505
331
  throw new Error(`Invalid Ethereum address: ${address}`);
506
332
  }
507
333
 
508
- // Use canonical format with chainId if domain is initialized
509
- if (this.domain && this.domain.chainId) {
510
- return `did:ethr:${this.domain.chainId}:${address}`;
511
- }
512
-
513
- // Fallback to legacy format (backward compatible)
514
334
  return `did:ethr:${address}`;
515
335
  }
516
336
  }