@aeternity/aepp-sdk 11.0.1 → 12.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (376) hide show
  1. package/LICENSE +1 -1
  2. package/dist/aepp-sdk.browser-script.js +1 -1
  3. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +12 -19
  4. package/dist/aepp-sdk.browser-script.js.map +1 -1
  5. package/dist/aepp-sdk.browser.js +1 -1
  6. package/dist/aepp-sdk.browser.js.map +1 -1
  7. package/dist/aepp-sdk.js +1 -1
  8. package/dist/aepp-sdk.js.map +1 -1
  9. package/es/AeSdk.d.ts +37 -0
  10. package/es/AeSdk.mjs +81 -0
  11. package/es/AeSdk.mjs.map +1 -0
  12. package/es/AeSdkAepp.d.ts +69 -0
  13. package/es/AeSdkAepp.mjs +175 -0
  14. package/es/AeSdkAepp.mjs.map +1 -0
  15. package/es/AeSdkBase.d.ts +212 -0
  16. package/es/AeSdkBase.mjs +310 -0
  17. package/es/AeSdkBase.mjs.map +1 -0
  18. package/es/AeSdkWallet.d.ts +103 -0
  19. package/es/AeSdkWallet.mjs +322 -0
  20. package/es/AeSdkWallet.mjs.map +1 -0
  21. package/es/Node.d.ts +44 -0
  22. package/es/Node.mjs +141 -0
  23. package/es/Node.mjs.map +1 -0
  24. package/es/account/Base.d.ts +65 -0
  25. package/es/account/Base.mjs +123 -0
  26. package/es/account/Base.mjs.map +1 -0
  27. package/es/account/Memory.d.ts +25 -0
  28. package/es/account/Memory.mjs +81 -0
  29. package/es/account/Memory.mjs.map +1 -0
  30. package/es/account/Rpc.d.ts +27 -0
  31. package/es/account/Rpc.mjs +79 -0
  32. package/es/account/Rpc.mjs.map +1 -0
  33. package/es/aens.d.ts +209 -0
  34. package/es/aens.mjs +332 -0
  35. package/es/aens.mjs.map +1 -0
  36. package/es/aepp-wallet-communication/connection/Browser.d.ts +32 -0
  37. package/es/aepp-wallet-communication/connection/Browser.mjs +70 -0
  38. package/es/aepp-wallet-communication/connection/Browser.mjs.map +1 -0
  39. package/es/aepp-wallet-communication/connection/BrowserRuntime.d.ts +17 -0
  40. package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs +57 -0
  41. package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs.map +1 -0
  42. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.d.ts +35 -0
  43. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs +142 -0
  44. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs.map +1 -0
  45. package/es/aepp-wallet-communication/connection-proxy.d.ts +10 -0
  46. package/{src/utils/crypto-ts.ts → es/aepp-wallet-communication/connection-proxy.mjs} +15 -11
  47. package/es/aepp-wallet-communication/connection-proxy.mjs.map +1 -0
  48. package/es/aepp-wallet-communication/rpc/RpcClient.d.ts +33 -0
  49. package/es/aepp-wallet-communication/rpc/RpcClient.mjs +169 -0
  50. package/es/aepp-wallet-communication/rpc/RpcClient.mjs.map +1 -0
  51. package/es/aepp-wallet-communication/rpc/types.d.ts +77 -0
  52. package/es/aepp-wallet-communication/rpc/types.mjs +3 -0
  53. package/es/aepp-wallet-communication/rpc/types.mjs.map +1 -0
  54. package/es/aepp-wallet-communication/schema.d.ts +90 -46
  55. package/es/aepp-wallet-communication/schema.mjs +214 -0
  56. package/es/aepp-wallet-communication/schema.mjs.map +1 -0
  57. package/es/aepp-wallet-communication/wallet-detector.d.ts +24 -0
  58. package/es/aepp-wallet-communication/wallet-detector.mjs +63 -0
  59. package/es/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
  60. package/es/apis/compiler/compiler.d.ts +92 -0
  61. package/es/apis/compiler/compiler.mjs +576 -0
  62. package/es/apis/compiler/compiler.mjs.map +1 -0
  63. package/es/apis/compiler/index.d.ts +2 -0
  64. package/es/apis/compiler/index.mjs +3 -0
  65. package/es/apis/compiler/index.mjs.map +1 -0
  66. package/es/apis/compiler/models/index.d.ts +248 -0
  67. package/es/apis/compiler/models/index.mjs +48 -0
  68. package/es/apis/compiler/models/index.mjs.map +1 -0
  69. package/es/apis/compiler/models/mappers.d.ts +23 -0
  70. package/es/apis/compiler/models/mappers.mjs +585 -0
  71. package/es/apis/compiler/models/mappers.mjs.map +1 -0
  72. package/es/apis/compiler/models/parameters.d.ts +13 -0
  73. package/es/apis/compiler/models/parameters.mjs +71 -0
  74. package/es/apis/compiler/models/parameters.mjs.map +1 -0
  75. package/es/apis/node/index.d.ts +2 -0
  76. package/es/apis/node/index.mjs +3 -0
  77. package/es/apis/node/index.mjs.map +1 -0
  78. package/es/apis/node/models/index.d.ts +1061 -0
  79. package/es/apis/node/models/index.mjs +2 -0
  80. package/es/apis/node/models/index.mjs.map +1 -0
  81. package/es/apis/node/models/mappers.d.ts +95 -0
  82. package/es/apis/node/models/mappers.mjs +4164 -0
  83. package/es/apis/node/models/mappers.mjs.map +1 -0
  84. package/es/apis/node/models/parameters.d.ts +17 -0
  85. package/es/apis/node/models/parameters.mjs +165 -0
  86. package/es/apis/node/models/parameters.mjs.map +1 -0
  87. package/es/apis/node/node.d.ts +215 -0
  88. package/es/apis/node/node.mjs +1165 -0
  89. package/es/apis/node/node.mjs.map +1 -0
  90. package/es/chain.d.ts +220 -0
  91. package/es/chain.mjs +524 -0
  92. package/es/chain.mjs.map +1 -0
  93. package/es/channel/handlers.d.ts +34 -0
  94. package/es/channel/handlers.mjs +147 -112
  95. package/es/channel/handlers.mjs.map +1 -1
  96. package/es/channel/index.d.ts +634 -0
  97. package/es/channel/index.mjs +898 -900
  98. package/es/channel/index.mjs.map +1 -1
  99. package/es/channel/internal.d.ts +100 -0
  100. package/es/channel/internal.mjs +97 -51
  101. package/es/channel/internal.mjs.map +1 -1
  102. package/es/contract/Compiler.d.ts +17 -0
  103. package/es/contract/Compiler.mjs +67 -0
  104. package/es/contract/Compiler.mjs.map +1 -0
  105. package/es/contract/aci.d.ts +125 -0
  106. package/es/contract/aci.mjs +199 -180
  107. package/es/contract/aci.mjs.map +1 -1
  108. package/es/contract/ga.d.ts +68 -0
  109. package/es/contract/ga.mjs +190 -0
  110. package/es/contract/ga.mjs.map +1 -0
  111. package/es/contract/methods.d.ts +68 -0
  112. package/es/contract/methods.mjs +101 -0
  113. package/es/contract/methods.mjs.map +1 -0
  114. package/es/index.d.ts +32 -0
  115. package/es/index.mjs +32 -40
  116. package/es/index.mjs.map +1 -1
  117. package/es/oracle.d.ts +154 -0
  118. package/es/oracle.mjs +288 -0
  119. package/es/oracle.mjs.map +1 -0
  120. package/es/spend.d.ts +61 -0
  121. package/es/spend.mjs +122 -0
  122. package/es/spend.mjs.map +1 -0
  123. package/es/tx/builder/constants.d.ts +41 -0
  124. package/es/tx/builder/constants.mjs +11 -19
  125. package/es/tx/builder/constants.mjs.map +1 -1
  126. package/es/tx/builder/field-types.d.ts +62 -0
  127. package/es/tx/builder/field-types.mjs +62 -10
  128. package/es/tx/builder/field-types.mjs.map +1 -1
  129. package/es/tx/builder/helpers.d.ts +131 -0
  130. package/es/tx/builder/helpers.mjs +71 -169
  131. package/es/tx/builder/helpers.mjs.map +1 -1
  132. package/es/tx/builder/index.d.ts +75 -0
  133. package/es/tx/builder/index.mjs +141 -323
  134. package/es/tx/builder/index.mjs.map +1 -1
  135. package/es/tx/builder/schema.d.ts +350 -0
  136. package/es/tx/builder/schema.mjs +173 -510
  137. package/es/tx/builder/schema.mjs.map +1 -1
  138. package/es/tx/index.d.ts +63 -0
  139. package/es/tx/index.mjs +171 -265
  140. package/es/tx/index.mjs.map +1 -1
  141. package/es/tx/min-fee.d.ts +20 -0
  142. package/es/tx/min-fee.mjs +148 -0
  143. package/es/tx/min-fee.mjs.map +1 -0
  144. package/es/tx/validator.d.ts +19 -0
  145. package/es/tx/validator.mjs +64 -71
  146. package/es/tx/validator.mjs.map +1 -1
  147. package/es/typings/@aeternity__argon2-browser/index.d.mjs +2 -0
  148. package/es/typings/@aeternity__argon2-browser/index.d.mjs.map +1 -0
  149. package/es/typings/@aeternity__uuid/index.d.mjs +2 -0
  150. package/es/typings/@aeternity__uuid/index.d.mjs.map +1 -0
  151. package/es/typings/bip32-path/index.d.mjs +2 -0
  152. package/es/typings/bip32-path/index.d.mjs.map +1 -0
  153. package/es/typings/blakejs/index.d.mjs.map +1 -1
  154. package/es/typings/tweetnacl-auth/index.d.mjs +2 -0
  155. package/es/typings/tweetnacl-auth/index.d.mjs.map +1 -0
  156. package/es/utils/amount-formatter.d.ts +42 -0
  157. package/es/utils/amount-formatter.mjs +28 -41
  158. package/es/utils/amount-formatter.mjs.map +1 -1
  159. package/es/utils/autorest.d.ts +6 -0
  160. package/es/utils/autorest.mjs +97 -0
  161. package/es/utils/autorest.mjs.map +1 -0
  162. package/es/utils/bignumber.d.ts +13 -0
  163. package/es/utils/bignumber.mjs +10 -8
  164. package/es/utils/bignumber.mjs.map +1 -1
  165. package/es/{bytes.d.ts → utils/bytes.d.ts} +10 -13
  166. package/es/utils/bytes.mjs +10 -14
  167. package/es/utils/bytes.mjs.map +1 -1
  168. package/es/utils/crypto.d.ts +103 -0
  169. package/es/utils/crypto.mjs +60 -68
  170. package/es/utils/crypto.mjs.map +1 -1
  171. package/es/utils/encoder.d.ts +26 -0
  172. package/es/utils/encoder.mjs +47 -51
  173. package/es/utils/encoder.mjs.map +1 -1
  174. package/es/{errors.d.ts → utils/errors.d.ts} +254 -57
  175. package/es/utils/errors.mjs +318 -125
  176. package/es/utils/errors.mjs.map +1 -1
  177. package/es/utils/hd-wallet.d.ts +34 -0
  178. package/es/utils/hd-wallet.mjs +27 -16
  179. package/es/utils/hd-wallet.mjs.map +1 -1
  180. package/es/utils/json-big.d.ts +5 -0
  181. package/es/utils/json-big.mjs +10 -14
  182. package/es/utils/json-big.mjs.map +1 -1
  183. package/es/utils/keystore.d.ts +51 -0
  184. package/es/utils/keystore.mjs +98 -193
  185. package/es/utils/keystore.mjs.map +1 -1
  186. package/es/{mptree.d.ts → utils/mptree.d.ts} +5 -8
  187. package/es/utils/mptree.mjs +13 -14
  188. package/es/utils/mptree.mjs.map +1 -1
  189. package/es/utils/other.d.ts +14 -0
  190. package/es/utils/other.mjs +21 -36
  191. package/es/utils/other.mjs.map +1 -1
  192. package/es/{semver-satisfies.d.ts → utils/semver-satisfies.d.ts} +0 -0
  193. package/es/utils/semver-satisfies.mjs.map +1 -1
  194. package/es/utils/string.d.ts +24 -0
  195. package/es/utils/string.mjs +8 -10
  196. package/es/utils/string.mjs.map +1 -1
  197. package/package.json +49 -39
  198. package/src/AeSdk.ts +65 -0
  199. package/src/AeSdkAepp.ts +167 -0
  200. package/src/AeSdkBase.ts +320 -0
  201. package/src/AeSdkWallet.ts +332 -0
  202. package/src/Node.ts +167 -0
  203. package/src/account/Base.ts +114 -0
  204. package/src/account/Memory.ts +83 -0
  205. package/src/account/Rpc.ts +67 -0
  206. package/src/aens.ts +391 -0
  207. package/src/aepp-wallet-communication/connection/Browser.ts +68 -0
  208. package/src/aepp-wallet-communication/connection/BrowserRuntime.ts +58 -0
  209. package/src/aepp-wallet-communication/connection/BrowserWindowMessage.ts +111 -0
  210. package/{es/utils/crypto-ts.mjs → src/aepp-wallet-communication/connection-proxy.ts} +16 -17
  211. package/src/aepp-wallet-communication/rpc/RpcClient.ts +137 -0
  212. package/src/aepp-wallet-communication/rpc/types.ts +66 -0
  213. package/src/aepp-wallet-communication/schema.ts +181 -0
  214. package/src/aepp-wallet-communication/wallet-detector.ts +71 -0
  215. package/src/apis/compiler/code-model-v4.yaml +2649 -0
  216. package/src/apis/compiler/compiler.ts +585 -0
  217. package/src/apis/compiler/index.ts +2 -0
  218. package/src/apis/compiler/models/index.ts +301 -0
  219. package/src/apis/compiler/models/mappers.ts +583 -0
  220. package/src/apis/compiler/models/parameters.ts +93 -0
  221. package/src/apis/node/code-model-v4.yaml +10046 -0
  222. package/src/apis/node/index.ts +2 -0
  223. package/src/apis/node/models/index.ts +1253 -0
  224. package/src/apis/node/models/mappers.ts +4278 -0
  225. package/src/apis/node/models/parameters.ts +188 -0
  226. package/src/apis/node/node.ts +1299 -0
  227. package/src/chain.ts +444 -0
  228. package/src/channel/{handlers.js → handlers.ts} +220 -128
  229. package/src/channel/index.ts +991 -0
  230. package/src/channel/internal.ts +345 -0
  231. package/src/contract/Compiler.ts +66 -0
  232. package/src/contract/aci.ts +529 -0
  233. package/src/contract/ga.ts +178 -0
  234. package/src/contract/methods.ts +118 -0
  235. package/src/index.ts +51 -0
  236. package/src/oracle.ts +342 -0
  237. package/src/spend.ts +152 -0
  238. package/src/tx/builder/{constants.js → constants.ts} +12 -16
  239. package/src/tx/builder/field-types.ts +111 -0
  240. package/src/tx/builder/helpers.ts +253 -0
  241. package/src/tx/builder/index.ts +394 -0
  242. package/src/tx/builder/schema.ts +771 -0
  243. package/src/tx/index.ts +199 -0
  244. package/src/tx/min-fee.ts +136 -0
  245. package/src/tx/validator.ts +226 -0
  246. package/src/typings/@aeternity__argon2-browser/index.d.ts +3 -0
  247. package/src/typings/@aeternity__uuid/index.d.ts +3 -0
  248. package/src/typings/bip32-path/index.d.ts +33 -0
  249. package/src/typings/tweetnacl-auth/index.d.ts +14 -0
  250. package/src/utils/amount-formatter.ts +29 -42
  251. package/src/utils/autorest.ts +90 -0
  252. package/src/utils/bignumber.ts +9 -11
  253. package/src/utils/bytes.ts +12 -14
  254. package/src/utils/crypto.ts +217 -0
  255. package/src/utils/encoder.ts +52 -55
  256. package/src/utils/errors.ts +268 -134
  257. package/src/utils/hd-wallet.ts +134 -0
  258. package/src/utils/json-big.ts +26 -0
  259. package/src/utils/keystore.ts +179 -0
  260. package/src/utils/mptree.ts +14 -14
  261. package/src/utils/other.ts +28 -43
  262. package/src/utils/string.ts +8 -10
  263. package/es/account/base.mjs +0 -179
  264. package/es/account/base.mjs.map +0 -1
  265. package/es/account/memory.mjs +0 -99
  266. package/es/account/memory.mjs.map +0 -1
  267. package/es/account/multiple.mjs +0 -179
  268. package/es/account/multiple.mjs.map +0 -1
  269. package/es/ae/aens.mjs +0 -404
  270. package/es/ae/aens.mjs.map +0 -1
  271. package/es/ae/aepp.mjs +0 -45
  272. package/es/ae/aepp.mjs.map +0 -1
  273. package/es/ae/contract.mjs +0 -159
  274. package/es/ae/contract.mjs.map +0 -1
  275. package/es/ae/index.mjs +0 -207
  276. package/es/ae/index.mjs.map +0 -1
  277. package/es/ae/oracle.mjs +0 -339
  278. package/es/ae/oracle.mjs.map +0 -1
  279. package/es/ae/universal.mjs +0 -45
  280. package/es/ae/universal.mjs.map +0 -1
  281. package/es/ae/wallet.mjs +0 -50
  282. package/es/ae/wallet.mjs.map +0 -1
  283. package/es/amount-formatter.d.ts +0 -58
  284. package/es/bignumber.d.ts +0 -18
  285. package/es/chain/index.mjs +0 -272
  286. package/es/chain/index.mjs.map +0 -1
  287. package/es/chain/node.mjs +0 -442
  288. package/es/chain/node.mjs.map +0 -1
  289. package/es/contract/compiler.mjs +0 -115
  290. package/es/contract/compiler.mjs.map +0 -1
  291. package/es/contract/ga/helpers.mjs +0 -30
  292. package/es/contract/ga/helpers.mjs.map +0 -1
  293. package/es/contract/ga/index.mjs +0 -178
  294. package/es/contract/ga/index.mjs.map +0 -1
  295. package/es/crypto-ts.d.ts +0 -9
  296. package/es/encoder.d.ts +0 -26
  297. package/es/node-pool/index.mjs +0 -188
  298. package/es/node-pool/index.mjs.map +0 -1
  299. package/es/node.mjs +0 -133
  300. package/es/node.mjs.map +0 -1
  301. package/es/other.d.ts +0 -31
  302. package/es/string.d.ts +0 -26
  303. package/es/tx/tx-object.mjs +0 -283
  304. package/es/tx/tx-object.mjs.map +0 -1
  305. package/es/tx/tx.mjs +0 -938
  306. package/es/tx/tx.mjs.map +0 -1
  307. package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs +0 -132
  308. package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +0 -1
  309. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs +0 -183
  310. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs.map +0 -1
  311. package/es/utils/aepp-wallet-communication/connection/index.mjs +0 -87
  312. package/es/utils/aepp-wallet-communication/connection/index.mjs.map +0 -1
  313. package/es/utils/aepp-wallet-communication/content-script-bridge.mjs +0 -98
  314. package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +0 -1
  315. package/es/utils/aepp-wallet-communication/helpers.mjs +0 -92
  316. package/es/utils/aepp-wallet-communication/helpers.mjs.map +0 -1
  317. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs +0 -375
  318. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +0 -1
  319. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs +0 -275
  320. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +0 -1
  321. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs +0 -541
  322. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs.map +0 -1
  323. package/es/utils/aepp-wallet-communication/schema.mjs +0 -102
  324. package/es/utils/aepp-wallet-communication/schema.mjs.map +0 -1
  325. package/es/utils/aepp-wallet-communication/wallet-detector.mjs +0 -110
  326. package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +0 -1
  327. package/es/utils/async-init.mjs +0 -64
  328. package/es/utils/async-init.mjs.map +0 -1
  329. package/es/utils/crypto-ts.mjs.map +0 -1
  330. package/es/utils/swagger.mjs +0 -164
  331. package/es/utils/swagger.mjs.map +0 -1
  332. package/src/account/base.js +0 -153
  333. package/src/account/memory.js +0 -83
  334. package/src/account/multiple.js +0 -162
  335. package/src/ae/aens.js +0 -361
  336. package/src/ae/aepp.js +0 -45
  337. package/src/ae/contract.js +0 -148
  338. package/src/ae/index.js +0 -168
  339. package/src/ae/oracle.js +0 -311
  340. package/src/ae/universal.js +0 -47
  341. package/src/ae/wallet.js +0 -50
  342. package/src/chain/index.js +0 -269
  343. package/src/chain/node.js +0 -320
  344. package/src/channel/index.js +0 -903
  345. package/src/channel/internal.js +0 -236
  346. package/src/contract/aci.js +0 -376
  347. package/src/contract/compiler.js +0 -100
  348. package/src/contract/ga/helpers.js +0 -19
  349. package/src/contract/ga/index.js +0 -150
  350. package/src/index.js +0 -89
  351. package/src/node-pool/index.js +0 -172
  352. package/src/node.js +0 -116
  353. package/src/tx/builder/field-types.js +0 -61
  354. package/src/tx/builder/helpers.js +0 -334
  355. package/src/tx/builder/index.js +0 -465
  356. package/src/tx/builder/schema.js +0 -1100
  357. package/src/tx/index.js +0 -301
  358. package/src/tx/tx-object.js +0 -187
  359. package/src/tx/tx.js +0 -631
  360. package/src/tx/validator.js +0 -181
  361. package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +0 -119
  362. package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +0 -165
  363. package/src/utils/aepp-wallet-communication/connection/index.js +0 -85
  364. package/src/utils/aepp-wallet-communication/content-script-bridge.js +0 -86
  365. package/src/utils/aepp-wallet-communication/helpers.js +0 -60
  366. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +0 -294
  367. package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +0 -232
  368. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +0 -401
  369. package/src/utils/aepp-wallet-communication/schema.ts +0 -80
  370. package/src/utils/aepp-wallet-communication/wallet-detector.js +0 -92
  371. package/src/utils/async-init.js +0 -45
  372. package/src/utils/crypto.js +0 -219
  373. package/src/utils/hd-wallet.js +0 -100
  374. package/src/utils/json-big.js +0 -26
  375. package/src/utils/keystore.js +0 -219
  376. package/src/utils/swagger.js +0 -156
@@ -17,6 +17,7 @@
17
17
 
18
18
  import { generateKeyPair, encodeContractAddress } from '../utils/crypto'
19
19
  import {
20
+ ChannelState,
20
21
  options,
21
22
  changeStatus,
22
23
  changeState,
@@ -25,32 +26,42 @@ import {
25
26
  emit,
26
27
  channelId,
27
28
  disconnect,
28
- fsmId
29
+ fsmId,
30
+ ChannelMessage,
31
+ ChannelFsm,
32
+ SignTx
29
33
  } from './internal'
30
34
  import { unpackTx, buildTx } from '../tx/builder'
31
35
  import { encode } from '../tx/builder/helpers'
36
+ import { EncodedData } from '../utils/encoder'
32
37
  import {
33
38
  IllegalArgumentError,
34
39
  InsufficientBalanceError,
35
40
  ChannelConnectionError,
36
41
  UnexpectedChannelMessageError
37
42
  } from '../utils/errors'
43
+ import Channel from '.'
44
+ import { TX_TYPE } from '../tx/builder/schema'
38
45
 
39
- async function appendSignature (tx, signFn) {
40
- const { signatures, encodedTx } = unpackTx(tx).tx
46
+ export async function appendSignature (
47
+ tx: EncodedData<'tx'>, signFn: SignTx
48
+ ): Promise<EncodedData<'tx'> | number | null> {
49
+ const { signatures, encodedTx } = unpackTx(tx, TX_TYPE.signed).tx
41
50
  const result = await signFn(encode(encodedTx.rlpEncoded, 'tx'))
42
51
  if (typeof result === 'string') {
43
- const { tx: signedTx, txType } = unpackTx(result)
52
+ const { tx: signedTx } = unpackTx(result, TX_TYPE.signed)
44
53
  return buildTx({
45
54
  signatures: signatures.concat(signedTx.signatures),
46
55
  encodedTx: signedTx.encodedTx.rlpEncoded
47
- }, txType).tx
56
+ }, TX_TYPE.signed).tx
48
57
  }
49
58
  return result
50
59
  }
51
60
 
52
- function handleUnexpectedMessage (channel, message, state) {
53
- if (state && state.reject) {
61
+ function handleUnexpectedMessage (
62
+ _channel: Channel, message: ChannelMessage, state: ChannelState
63
+ ): ChannelFsm {
64
+ if (state?.reject != null) {
54
65
  state.reject(Object.assign(
55
66
  new UnexpectedChannelMessageError(`Unexpected message received:\n\n${JSON.stringify(message)}`),
56
67
  { wsMessage: message }
@@ -59,13 +70,16 @@ function handleUnexpectedMessage (channel, message, state) {
59
70
  return { handler: channelOpen }
60
71
  }
61
72
 
62
- export function awaitingConnection (channel, message, state) {
73
+ export function awaitingConnection (
74
+ channel: Channel, message: ChannelMessage
75
+ ): ChannelFsm | undefined {
63
76
  if (message.method === 'channels.info') {
64
- if (['channel_accept', 'funding_created'].includes(message.params.data.event)) {
77
+ const channelInfoStatus: string = message.params.data.event
78
+ if (['channel_accept', 'funding_created'].includes(channelInfoStatus)) {
65
79
  changeStatus(channel, {
66
80
  channel_accept: 'accepted',
67
81
  funding_created: 'halfSigned'
68
- }[message.params.data.event])
82
+ }[channelInfoStatus as 'channel_accept' | 'funding_created'])
69
83
  return { handler: awaitingChannelCreateTx }
70
84
  }
71
85
  if (message.params.data.event === 'channel_reestablished') {
@@ -78,12 +92,14 @@ export function awaitingConnection (channel, message, state) {
78
92
  return { handler: awaitingConnection }
79
93
  }
80
94
  if (message.method === 'channels.error') {
81
- emit(channel, 'error', new ChannelConnectionError(message.payload.message))
95
+ emit(channel, 'error', new ChannelConnectionError(message?.payload?.message))
82
96
  return { handler: channelClosed }
83
97
  }
84
98
  }
85
99
 
86
- export async function awaitingReconnection (channel, message, state) {
100
+ export async function awaitingReconnection (
101
+ channel: Channel, message: ChannelMessage, state: ChannelState
102
+ ): Promise<ChannelFsm> {
87
103
  if (message.method === 'channels.info') {
88
104
  if (message.params.data.event === 'fsm_up') {
89
105
  fsmId.set(channel, message.params.data.fsm_id)
@@ -94,59 +110,74 @@ export async function awaitingReconnection (channel, message, state) {
94
110
  return handleUnexpectedMessage(channel, message, state)
95
111
  }
96
112
 
97
- export async function awaitingChannelCreateTx (channel, message, state) {
98
- const tag = {
99
- initiator: 'initiator_sign',
100
- responder: 'responder_sign'
101
- }[options.get(channel).role]
102
- if (message.method === `channels.sign.${tag}`) {
103
- if (message.params.data.tx) {
104
- const signedTx = await options.get(channel).sign(tag, message.params.data.tx)
105
- send(channel, { jsonrpc: '2.0', method: `channels.${tag}`, params: { tx: signedTx } })
113
+ export async function awaitingChannelCreateTx (
114
+ channel: Channel, message: ChannelMessage
115
+ ): Promise<ChannelFsm | undefined> {
116
+ const channelOptions = options.get(channel)
117
+ if (channelOptions != null) {
118
+ const tag = {
119
+ initiator: 'initiator_sign',
120
+ responder: 'responder_sign'
121
+ }[channelOptions.role]
122
+ if (message.method === `channels.sign.${tag}`) {
123
+ if (message.params.data.tx != null) {
124
+ const signedTx = await channelOptions.sign(tag, message.params.data.tx)
125
+ send(channel, { jsonrpc: '2.0', method: `channels.${tag}`, params: { tx: signedTx } })
126
+ return { handler: awaitingOnChainTx }
127
+ }
128
+ const signedTx = await appendSignature(
129
+ message.params.data.signed_tx, async tx => await channelOptions.sign(tag, tx)
130
+ )
131
+ send(channel, { jsonrpc: '2.0', method: `channels.${tag}`, params: { signed_tx: signedTx } })
106
132
  return { handler: awaitingOnChainTx }
107
133
  }
108
- const signedTx = await appendSignature(
109
- message.params.data.signed_tx, tx => options.get(channel).sign(tag, tx)
110
- )
111
- send(channel, { jsonrpc: '2.0', method: `channels.${tag}`, params: { signed_tx: signedTx } })
112
- return { handler: awaitingOnChainTx }
113
134
  }
114
135
  }
115
136
 
116
- export function awaitingOnChainTx (channel, message, state) {
117
- if (message.method === 'channels.on_chain_tx') {
118
- if (
119
- message.params.data.info === 'funding_signed' &&
120
- options.get(channel).role === 'initiator'
121
- ) {
122
- return { handler: awaitingBlockInclusion }
137
+ export function awaitingOnChainTx (
138
+ channel: Channel, message: ChannelMessage
139
+ ): ChannelFsm | undefined {
140
+ const channelOptions = options.get(channel)
141
+ if (channelOptions != null) {
142
+ if (message.method === 'channels.on_chain_tx') {
143
+ if (
144
+ message.params.data.info === 'funding_signed' &&
145
+ channelOptions.role === 'initiator'
146
+ ) {
147
+ return { handler: awaitingBlockInclusion }
148
+ }
149
+ if (
150
+ message.params.data.info === 'funding_created' &&
151
+ channelOptions.role === 'responder'
152
+ ) {
153
+ return { handler: awaitingBlockInclusion }
154
+ }
123
155
  }
124
156
  if (
125
- message.params.data.info === 'funding_created' &&
126
- options.get(channel).role === 'responder'
157
+ message.method === 'channels.info' &&
158
+ message.params.data.event === 'funding_signed' &&
159
+ channelOptions.role === 'initiator'
127
160
  ) {
128
- return { handler: awaitingBlockInclusion }
161
+ channelId.set(channel, message.params.channel_id)
162
+ changeStatus(channel, 'signed')
163
+ return { handler: awaitingOnChainTx }
129
164
  }
130
165
  }
131
- if (
132
- message.method === 'channels.info' &&
133
- message.params.data.event === 'funding_signed' &&
134
- options.get(channel).role === 'initiator'
135
- ) {
136
- channelId.set(channel, message.params.channel_id)
137
- changeStatus(channel, 'signed')
138
- return { handler: awaitingOnChainTx }
139
- }
140
166
  }
141
167
 
142
- export function awaitingBlockInclusion (channel, message, state) {
168
+ export function awaitingBlockInclusion (
169
+ channel: Channel, message: ChannelMessage
170
+ ): ChannelFsm | undefined {
143
171
  if (message.method === 'channels.info') {
144
- const handler = {
172
+ const handlers: {
173
+ [key: string]: (channel: Channel, message: ChannelMessage) => ChannelFsm | undefined
174
+ } = {
145
175
  funding_created: awaitingBlockInclusion,
146
176
  own_funding_locked: awaitingBlockInclusion,
147
177
  funding_locked: awaitingOpenConfirmation
148
- }[message.params.data.event]
149
- if (handler) {
178
+ }
179
+ const handler = handlers[message.params.data.event as string]
180
+ if (handler != null) {
150
181
  return { handler }
151
182
  }
152
183
  }
@@ -159,21 +190,27 @@ export function awaitingBlockInclusion (channel, message, state) {
159
190
  }
160
191
  }
161
192
 
162
- export function awaitingOpenConfirmation (channel, message, state) {
193
+ export function awaitingOpenConfirmation (
194
+ channel: Channel, message: ChannelMessage
195
+ ): ChannelFsm | undefined {
163
196
  if (message.method === 'channels.info' && message.params.data.event === 'open') {
164
197
  channelId.set(channel, message.params.channel_id)
165
198
  return { handler: awaitingInitialState }
166
199
  }
167
200
  }
168
201
 
169
- export function awaitingInitialState (channel, message, state) {
202
+ export function awaitingInitialState (
203
+ channel: Channel, message: ChannelMessage
204
+ ): ChannelFsm | undefined {
170
205
  if (message.method === 'channels.update') {
171
206
  changeState(channel, message.params.data.state)
172
207
  return { handler: channelOpen }
173
208
  }
174
209
  }
175
210
 
176
- export async function channelOpen (channel, message, state) {
211
+ export async function channelOpen (
212
+ channel: Channel, message: ChannelMessage, state: ChannelState
213
+ ): Promise<ChannelFsm | undefined> {
177
214
  switch (message.method) {
178
215
  case 'channels.info':
179
216
  switch (message.params.data.event) {
@@ -226,23 +263,26 @@ export async function channelOpen (channel, message, state) {
226
263
  changeState(channel, message.params.data.state)
227
264
  return { handler: channelOpen }
228
265
  case 'channels.sign.shutdown_sign_ack':
229
- return awaitingTxSignRequest(channel, message, state)
266
+ return await awaitingTxSignRequest(channel, message, state)
230
267
  }
231
268
  }
232
- channelOpen.enter = (channel) => {
269
+ channelOpen.enter = (channel: Channel) => {
233
270
  changeStatus(channel, 'open')
234
271
  }
235
272
 
236
- export async function awaitingOffChainTx (channel, message, state) {
273
+ export async function awaitingOffChainTx (
274
+ channel: Channel, message: ChannelMessage, state: ChannelState
275
+ ): Promise<ChannelFsm> {
237
276
  if (message.method === 'channels.sign.update') {
238
277
  const { sign } = state
239
- if (message.params.data.tx) {
240
- const signedTx = await sign(message.params.data.tx, { updates: message.params.data.updates })
278
+ if (message.params.data.tx != null) {
279
+ const signedTx =
280
+ await sign(message.params.data.tx, { updates: message.params.data.updates })
241
281
  send(channel, { jsonrpc: '2.0', method: 'channels.update', params: { tx: signedTx } })
242
282
  return { handler: awaitingOffChainUpdate, state }
243
283
  }
244
- const signedTx = await appendSignature(message.params.data.signed_tx, tx =>
245
- sign(tx, { updates: message.params.data.updates })
284
+ const signedTx = await appendSignature(message.params.data.signed_tx, async tx =>
285
+ await sign(tx, { updates: message.params.data.updates })
246
286
  )
247
287
  if (typeof signedTx === 'string') {
248
288
  send(channel, { jsonrpc: '2.0', method: 'channels.update', params: { signed_tx: signedTx } })
@@ -257,11 +297,11 @@ export async function awaitingOffChainTx (channel, message, state) {
257
297
  state.reject(new ChannelConnectionError(message.data.message))
258
298
  return { handler: channelOpen }
259
299
  }
260
- if (message.error) {
261
- const { data = [] } = message.error
262
- if (data.find(i => i.code === 1001)) {
300
+ if (message.error != null) {
301
+ const { data } = message.error ?? { data: [] }
302
+ if (data.find(i => i.code === 1001) != null) {
263
303
  state.reject(new InsufficientBalanceError('Insufficient balance'))
264
- } else if (data.find(i => i.code === 1002)) {
304
+ } else if (data.find(i => i.code === 1002) != null) {
265
305
  state.reject(new IllegalArgumentError('Amount cannot be negative'))
266
306
  } else {
267
307
  state.reject(new ChannelConnectionError(message.error.message))
@@ -285,7 +325,9 @@ export async function awaitingOffChainTx (channel, message, state) {
285
325
  return handleUnexpectedMessage(channel, message, state)
286
326
  }
287
327
 
288
- export function awaitingOffChainUpdate (channel, message, state) {
328
+ export function awaitingOffChainUpdate (
329
+ channel: Channel, message: ChannelMessage, state: ChannelState
330
+ ): ChannelFsm | undefined {
289
331
  if (message.method === 'channels.update') {
290
332
  changeState(channel, message.params.data.state)
291
333
  state.resolve({ accepted: true, signedTx: message.params.data.state })
@@ -305,27 +347,30 @@ export function awaitingOffChainUpdate (channel, message, state) {
305
347
  return { handler: channelOpen }
306
348
  }
307
349
  }
308
- if (message.error) {
350
+ if (message.error != null) {
309
351
  state.reject(new ChannelConnectionError(message.error.message))
310
352
  return { handler: channelOpen }
311
353
  }
312
354
  return handleUnexpectedMessage(channel, message, state)
313
355
  }
314
356
 
315
- export async function awaitingTxSignRequest (channel, message, state) {
316
- const [, tag] = message.method.match(/^channels\.sign\.([^.]+)$/) || []
317
- if (tag) {
318
- if (message.params.data.tx) {
319
- const signedTx = await options.get(channel).sign(tag, message.params.data.tx, {
357
+ export async function awaitingTxSignRequest (
358
+ channel: Channel, message: ChannelMessage, state: ChannelState
359
+ ): Promise<ChannelFsm | undefined> {
360
+ const [, tag] = message.method.match(/^channels\.sign\.([^.]+)$/) ?? []
361
+ const channelOptions = options.get(channel)
362
+ if (tag != null && (channelOptions != null)) {
363
+ if (message.params.data.tx != null) {
364
+ const signedTx = await channelOptions.sign(tag, message.params.data.tx, {
320
365
  updates: message.params.data.updates
321
366
  })
322
- if (signedTx) {
367
+ if (signedTx != null) {
323
368
  send(channel, { jsonrpc: '2.0', method: `channels.${tag}`, params: { tx: signedTx } })
324
369
  return { handler: channelOpen }
325
370
  }
326
371
  } else {
327
- const signedTx = await appendSignature(message.params.data.signed_tx, tx =>
328
- options.get(channel).sign(tag, tx, { updates: message.params.data.updates })
372
+ const signedTx = await appendSignature(message.params.data.signed_tx, async tx =>
373
+ await channelOptions.sign(tag, tx, { updates: message.params.data.updates })
329
374
  )
330
375
  if (typeof signedTx === 'string') {
331
376
  send(channel, { jsonrpc: '2.0', method: `channels.${tag}`, params: { signed_tx: signedTx } })
@@ -351,8 +396,10 @@ export async function awaitingTxSignRequest (channel, message, state) {
351
396
  return handleUnexpectedMessage(channel, message, state)
352
397
  }
353
398
 
354
- export function awaitingUpdateConflict (channel, message, state) {
355
- if (message.error) {
399
+ export function awaitingUpdateConflict (
400
+ channel: Channel, message: ChannelMessage, state: ChannelState
401
+ ): ChannelFsm {
402
+ if (message.error != null) {
356
403
  return { handler: awaitingUpdateConflict, state }
357
404
  }
358
405
  if (message.method === 'channels.conflict') {
@@ -361,21 +408,26 @@ export function awaitingUpdateConflict (channel, message, state) {
361
408
  return handleUnexpectedMessage(channel, message, state)
362
409
  }
363
410
 
364
- export async function awaitingShutdownTx (channel, message, state) {
411
+ export async function awaitingShutdownTx (
412
+ channel: Channel, message: ChannelMessage, state: ChannelState
413
+ ): Promise<ChannelFsm | undefined> {
365
414
  if (message.method === 'channels.sign.shutdown_sign') {
366
- if (message.params.data.tx) {
415
+ if (message.params.data.tx != null) {
367
416
  const signedTx = await state.sign(message.params.data.tx)
368
417
  send(channel, { jsonrpc: '2.0', method: 'channels.shutdown_sign', params: { tx: signedTx } })
369
418
  return { handler: awaitingShutdownOnChainTx, state }
370
419
  }
371
- const signedTx = await appendSignature(message.params.data.signed_tx, tx => state.sign(tx))
420
+ const signedTx = await appendSignature(message.params.data.signed_tx, async tx =>
421
+ await state.sign(tx))
372
422
  send(channel, { jsonrpc: '2.0', method: 'channels.shutdown_sign', params: { signed_tx: signedTx } })
373
423
  return { handler: awaitingShutdownOnChainTx, state }
374
424
  }
375
425
  return handleUnexpectedMessage(channel, message, state)
376
426
  }
377
427
 
378
- export function awaitingShutdownOnChainTx (channel, message, state) {
428
+ export function awaitingShutdownOnChainTx (
429
+ channel: Channel, message: ChannelMessage, state: ChannelState
430
+ ): ChannelFsm {
379
431
  if (message.method === 'channels.on_chain_tx') {
380
432
  // state.resolve(message.params.data.tx)
381
433
  return { handler: channelClosed, state }
@@ -383,7 +435,9 @@ export function awaitingShutdownOnChainTx (channel, message, state) {
383
435
  return handleUnexpectedMessage(channel, message, state)
384
436
  }
385
437
 
386
- export function awaitingLeave (channel, message, state) {
438
+ export function awaitingLeave (
439
+ channel: Channel, message: ChannelMessage, state: ChannelState
440
+ ): ChannelFsm {
387
441
  if (message.method === 'channels.leave') {
388
442
  state.resolve({ channelId: message.params.channel_id, signedTx: message.params.data.state })
389
443
  disconnect(channel)
@@ -396,16 +450,19 @@ export function awaitingLeave (channel, message, state) {
396
450
  return handleUnexpectedMessage(channel, message, state)
397
451
  }
398
452
 
399
- export async function awaitingWithdrawTx (channel, message, state) {
453
+ export async function awaitingWithdrawTx (
454
+ channel: Channel, message: ChannelMessage, state: ChannelState
455
+ ): Promise<ChannelFsm | undefined> {
400
456
  if (message.method === 'channels.sign.withdraw_tx') {
401
457
  const { sign } = state
402
- if (message.params.data.tx) {
403
- const signedTx = await sign(message.params.data.tx, { updates: message.params.data.updates })
458
+ if (message.params.data.tx != null) {
459
+ const signedTx =
460
+ await sign(message.params.data.tx, { updates: message.params.data.updates })
404
461
  send(channel, { jsonrpc: '2.0', method: 'channels.withdraw_tx', params: { tx: signedTx } })
405
462
  return { handler: awaitingWithdrawCompletion, state }
406
463
  }
407
- const signedTx = await appendSignature(message.params.data.signed_tx, tx =>
408
- sign(tx, { updates: message.params.data.updates })
464
+ const signedTx = await appendSignature(message.params.data.signed_tx, async tx =>
465
+ await sign(tx, { updates: message.params.data.updates })
409
466
  )
410
467
  if (typeof signedTx === 'string') {
411
468
  send(channel, { jsonrpc: '2.0', method: 'channels.withdraw_tx', params: { signed_tx: signedTx } })
@@ -419,20 +476,25 @@ export async function awaitingWithdrawTx (channel, message, state) {
419
476
  return handleUnexpectedMessage(channel, message, state)
420
477
  }
421
478
 
422
- export function awaitingWithdrawCompletion (channel, message, state) {
479
+ export function awaitingWithdrawCompletion (
480
+ channel: Channel, message: ChannelMessage, state: ChannelState
481
+ ): ChannelFsm {
423
482
  if (message.method === 'channels.on_chain_tx') {
424
- if (state.onOnChainTx) {
483
+ if (state.onOnChainTx != null) {
425
484
  state.onOnChainTx(message.params.data.tx)
426
485
  }
427
486
  return { handler: awaitingWithdrawCompletion, state }
428
487
  }
429
488
  if (message.method === 'channels.info') {
430
489
  if (['own_withdraw_locked', 'withdraw_locked'].includes(message.params.data.event)) {
431
- const callback = {
490
+ const callbacks: {
491
+ [key: string]: Function | undefined
492
+ } = {
432
493
  own_withdraw_locked: state.onOwnWithdrawLocked,
433
494
  withdraw_locked: state.onWithdrawLocked
434
- }[message.params.data.event]
435
- if (callback) {
495
+ }
496
+ const callback = callbacks[message.params.data.event]
497
+ if (callback != null) {
436
498
  callback()
437
499
  }
438
500
  return { handler: awaitingWithdrawCompletion, state }
@@ -460,16 +522,19 @@ export function awaitingWithdrawCompletion (channel, message, state) {
460
522
  return handleUnexpectedMessage(channel, message, state)
461
523
  }
462
524
 
463
- export async function awaitingDepositTx (channel, message, state) {
525
+ export async function awaitingDepositTx (
526
+ channel: Channel, message: ChannelMessage, state: ChannelState
527
+ ): Promise<ChannelFsm | undefined> {
464
528
  if (message.method === 'channels.sign.deposit_tx') {
465
529
  const { sign } = state
466
- if (message.params.data.tx) {
467
- const signedTx = await sign(message.params.data.tx, { updates: message.params.data.updates })
530
+ if (message.params.data.tx != null) {
531
+ const signedTx = await sign(message.params.data.tx,
532
+ { updates: message.params.data.updates })
468
533
  send(channel, { jsonrpc: '2.0', method: 'channels.deposit_tx', params: { tx: signedTx } })
469
534
  return { handler: awaitingDepositCompletion, state }
470
535
  }
471
- const signedTx = await appendSignature(message.params.data.signed_tx, tx =>
472
- sign(tx, { updates: message.params.data.updates })
536
+ const signedTx = await appendSignature(message.params.data.signed_tx, async tx =>
537
+ await sign(tx, { updates: message.params.data.updates })
473
538
  )
474
539
  if (typeof signedTx === 'string') {
475
540
  send(channel, { jsonrpc: '2.0', method: 'channels.deposit_tx', params: { signed_tx: signedTx } })
@@ -483,20 +548,25 @@ export async function awaitingDepositTx (channel, message, state) {
483
548
  return handleUnexpectedMessage(channel, message, state)
484
549
  }
485
550
 
486
- export function awaitingDepositCompletion (channel, message, state) {
551
+ export function awaitingDepositCompletion (
552
+ channel: Channel, message: ChannelMessage, state: ChannelState
553
+ ): ChannelFsm {
487
554
  if (message.method === 'channels.on_chain_tx') {
488
- if (state.onOnChainTx) {
555
+ if (state.onOnChainTx != null) {
489
556
  state.onOnChainTx(message.params.data.tx)
490
557
  }
491
558
  return { handler: awaitingDepositCompletion, state }
492
559
  }
493
560
  if (message.method === 'channels.info') {
494
561
  if (['own_deposit_locked', 'deposit_locked'].includes(message.params.data.event)) {
495
- const callback = {
562
+ const callbacks: {
563
+ [key: string]: Function | undefined
564
+ } = {
496
565
  own_deposit_locked: state.onOwnDepositLocked,
497
566
  deposit_locked: state.onDepositLocked
498
- }[message.params.data.event]
499
- if (callback) {
567
+ }
568
+ const callback = callbacks[message.params.data.event]
569
+ if (callback != null) {
500
570
  callback()
501
571
  }
502
572
  return { handler: awaitingDepositCompletion, state }
@@ -524,14 +594,17 @@ export function awaitingDepositCompletion (channel, message, state) {
524
594
  return handleUnexpectedMessage(channel, message, state)
525
595
  }
526
596
 
527
- export async function awaitingNewContractTx (channel, message, state) {
597
+ export async function awaitingNewContractTx (
598
+ channel: Channel, message: ChannelMessage, state: ChannelState
599
+ ): Promise<ChannelFsm | undefined> {
528
600
  if (message.method === 'channels.sign.update') {
529
- if (message.params.data.tx) {
601
+ if (message.params.data.tx != null) {
530
602
  const signedTx = await state.sign(message.params.data.tx)
531
603
  send(channel, { jsonrpc: '2.0', method: 'channels.update', params: { tx: signedTx } })
532
604
  return { handler: awaitingNewContractCompletion, state }
533
605
  }
534
- const signedTx = await appendSignature(message.params.data.signed_tx, tx => state.sign(tx))
606
+ const signedTx = await appendSignature(message.params.data.signed_tx,
607
+ async tx => await state.sign(tx))
535
608
  if (typeof signedTx === 'string') {
536
609
  send(channel, { jsonrpc: '2.0', method: 'channels.update', params: { signed_tx: signedTx } })
537
610
  return { handler: awaitingNewContractCompletion, state }
@@ -544,20 +617,27 @@ export async function awaitingNewContractTx (channel, message, state) {
544
617
  return handleUnexpectedMessage(channel, message, state)
545
618
  }
546
619
 
547
- export function awaitingNewContractCompletion (channel, message, state) {
620
+ export function awaitingNewContractCompletion (
621
+ channel: Channel, message: ChannelMessage, state: ChannelState
622
+ ): ChannelFsm {
623
+ const channelOptions = options.get(channel)
548
624
  if (message.method === 'channels.update') {
549
- const { round } = unpackTx(message.params.data.state).tx.encodedTx.tx
550
- const owner = options.get(channel)[{
551
- initiator: 'initiatorId',
552
- responder: 'responderId'
553
- }[options.get(channel).role]]
554
- changeState(channel, message.params.data.state)
555
- state.resolve({
556
- accepted: true,
557
- address: encodeContractAddress(owner, round),
558
- signedTx: message.params.data.state
559
- })
560
- return { handler: channelOpen }
625
+ const { round } = unpackTx(message.params.data.state, TX_TYPE.signed).tx.encodedTx.tx
626
+ if (channelOptions?.role != null) {
627
+ const role = channelOptions.role === 'initiator'
628
+ ? 'initiatorId'
629
+ : channelOptions.role === 'responder' ? 'responderId' : null
630
+ if (role != null) {
631
+ const owner = channelOptions?.[role]
632
+ changeState(channel, message.params.data.state)
633
+ state.resolve({
634
+ accepted: true,
635
+ address: encodeContractAddress(owner, round),
636
+ signedTx: message.params.data.state
637
+ })
638
+ return { handler: channelOpen }
639
+ }
640
+ }
561
641
  }
562
642
  if (message.method === 'channels.conflict') {
563
643
  state.resolve({
@@ -576,9 +656,11 @@ export function awaitingNewContractCompletion (channel, message, state) {
576
656
  return handleUnexpectedMessage(channel, message, state)
577
657
  }
578
658
 
579
- export async function awaitingCallContractUpdateTx (channel, message, state) {
659
+ export async function awaitingCallContractUpdateTx (
660
+ channel: Channel, message: ChannelMessage, state: ChannelState
661
+ ): Promise<ChannelFsm | undefined> {
580
662
  if (message.method === 'channels.sign.update') {
581
- if (message.params.data.tx) {
663
+ if (message.params.data.tx != null) {
582
664
  const signedTx = await state.sign(
583
665
  message.params.data.tx, { updates: message.params.data.updates }
584
666
  )
@@ -587,7 +669,7 @@ export async function awaitingCallContractUpdateTx (channel, message, state) {
587
669
  }
588
670
  const signedTx = await appendSignature(
589
671
  message.params.data.signed_tx,
590
- tx => state.sign(tx, { updates: message.params.data.updates })
672
+ async tx => await state.sign(tx, { updates: message.params.data.updates })
591
673
  )
592
674
  if (typeof signedTx === 'string') {
593
675
  send(channel, { jsonrpc: '2.0', method: 'channels.update', params: { signed_tx: signedTx } })
@@ -601,10 +683,12 @@ export async function awaitingCallContractUpdateTx (channel, message, state) {
601
683
  return handleUnexpectedMessage(channel, message, state)
602
684
  }
603
685
 
604
- export async function awaitingCallContractForceProgressUpdate (channel, message, state) {
686
+ export async function awaitingCallContractForceProgressUpdate (
687
+ channel: Channel, message: ChannelMessage, state: ChannelState
688
+ ): Promise<ChannelFsm | undefined> {
605
689
  if (message.method === 'channels.sign.force_progress_tx') {
606
- const signedTx = await appendSignature(message.params.data.signed_tx, tx =>
607
- state.sign(tx, { updates: message.params.data.updates })
690
+ const signedTx = await appendSignature(message.params.data.signed_tx, async tx =>
691
+ await state.sign(tx, { updates: message.params.data.updates })
608
692
  )
609
693
  send(channel, { jsonrpc: '2.0', method: 'channels.force_progress_sign', params: { signed_tx: signedTx } })
610
694
  return { handler: awaitingForceProgressCompletion, state }
@@ -612,9 +696,11 @@ export async function awaitingCallContractForceProgressUpdate (channel, message,
612
696
  return handleUnexpectedMessage(channel, message, state)
613
697
  }
614
698
 
615
- export function awaitingForceProgressCompletion (channel, message, state) {
699
+ export function awaitingForceProgressCompletion (
700
+ channel: Channel, message: ChannelMessage, state: ChannelState
701
+ ): ChannelFsm {
616
702
  if (message.method === 'channels.on_chain_tx') {
617
- if (state.onOnChainTx) {
703
+ if (state.onOnChainTx != null) {
618
704
  state.onOnChainTx(message.params.data)
619
705
  }
620
706
  emit(channel, 'onChainTx', message.params.data.tx, {
@@ -626,7 +712,9 @@ export function awaitingForceProgressCompletion (channel, message, state) {
626
712
  return handleUnexpectedMessage(channel, message, state)
627
713
  }
628
714
 
629
- export function awaitingCallContractCompletion (channel, message, state) {
715
+ export function awaitingCallContractCompletion (
716
+ channel: Channel, message: ChannelMessage, state: ChannelState
717
+ ): ChannelFsm {
630
718
  if (message.method === 'channels.update') {
631
719
  changeState(channel, message.params.data.state)
632
720
  state.resolve({ accepted: true, signedTx: message.params.data.state })
@@ -649,7 +737,9 @@ export function awaitingCallContractCompletion (channel, message, state) {
649
737
  return handleUnexpectedMessage(channel, message, state)
650
738
  }
651
739
 
652
- export function awaitingCallsPruned (channels, message, state) {
740
+ export function awaitingCallsPruned (
741
+ _channels: Channel[], message: ChannelMessage, state: ChannelState
742
+ ): ChannelFsm {
653
743
  if (message.method === 'channels.calls_pruned.reply') {
654
744
  state.resolve()
655
745
  return { handler: channelOpen }
@@ -658,8 +748,10 @@ export function awaitingCallsPruned (channels, message, state) {
658
748
  return { handler: channelClosed }
659
749
  }
660
750
 
661
- export function channelClosed (channel, message, state) {
662
- if (!state) return { handler: channelClosed }
751
+ export function channelClosed (
752
+ _channel: Channel, message: ChannelMessage, state: ChannelState
753
+ ): ChannelFsm {
754
+ if (state == null) return { handler: channelClosed }
663
755
  if (message.params.data.event === 'closing') return { handler: channelClosed, state }
664
756
  if (message.params.data.info === 'channel_closed') {
665
757
  state.closeTx = message.params.data.tx