@aeternity/aepp-sdk 9.0.1 → 11.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 (284) hide show
  1. package/README.md +15 -14
  2. package/dist/aepp-sdk.browser-script.js +1 -1
  3. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +7 -36
  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/account/base.mjs +179 -0
  10. package/es/account/base.mjs.map +1 -0
  11. package/es/account/memory.mjs +99 -0
  12. package/es/account/memory.mjs.map +1 -0
  13. package/es/account/multiple.mjs +179 -0
  14. package/es/account/multiple.mjs.map +1 -0
  15. package/es/ae/aens.mjs +404 -0
  16. package/es/ae/aens.mjs.map +1 -0
  17. package/es/ae/{aepp.js → aepp.mjs} +9 -9
  18. package/es/ae/aepp.mjs.map +1 -0
  19. package/es/ae/contract.mjs +159 -0
  20. package/es/ae/contract.mjs.map +1 -0
  21. package/es/ae/index.mjs +207 -0
  22. package/es/ae/index.mjs.map +1 -0
  23. package/es/ae/oracle.mjs +339 -0
  24. package/es/ae/oracle.mjs.map +1 -0
  25. package/es/ae/{universal.js → universal.mjs} +10 -10
  26. package/es/ae/universal.mjs.map +1 -0
  27. package/es/ae/{wallet.js → wallet.mjs} +10 -10
  28. package/es/ae/wallet.mjs.map +1 -0
  29. package/es/aepp-wallet-communication/schema.d.ts +74 -0
  30. package/es/bignumber.d.ts +1 -7
  31. package/es/chain/{index.js → index.mjs} +29 -9
  32. package/es/chain/index.mjs.map +1 -0
  33. package/es/chain/node.mjs +442 -0
  34. package/es/chain/node.mjs.map +1 -0
  35. package/es/channel/handlers.mjs +1110 -0
  36. package/es/channel/handlers.mjs.map +1 -0
  37. package/es/channel/{index.js → index.mjs} +245 -472
  38. package/es/channel/index.mjs.map +1 -0
  39. package/es/channel/internal.mjs +283 -0
  40. package/es/channel/internal.mjs.map +1 -0
  41. package/es/contract/aci.mjs +477 -0
  42. package/es/contract/aci.mjs.map +1 -0
  43. package/es/contract/compiler.mjs +115 -0
  44. package/es/contract/compiler.mjs.map +1 -0
  45. package/es/contract/ga/helpers.mjs +30 -0
  46. package/es/contract/ga/helpers.mjs.map +1 -0
  47. package/es/contract/ga/index.mjs +178 -0
  48. package/es/contract/ga/index.mjs.map +1 -0
  49. package/es/errors.d.ts +284 -0
  50. package/es/index.mjs +57 -0
  51. package/es/index.mjs.map +1 -0
  52. package/es/node-pool/index.mjs +188 -0
  53. package/es/node-pool/index.mjs.map +1 -0
  54. package/es/node.mjs +133 -0
  55. package/es/node.mjs.map +1 -0
  56. package/es/other.d.ts +1 -1
  57. package/es/tx/builder/field-types.mjs +74 -0
  58. package/es/tx/builder/field-types.mjs.map +1 -0
  59. package/es/tx/builder/helpers.mjs +363 -0
  60. package/es/tx/builder/helpers.mjs.map +1 -0
  61. package/es/tx/builder/index.mjs +606 -0
  62. package/es/tx/builder/index.mjs.map +1 -0
  63. package/es/tx/builder/schema.mjs +760 -0
  64. package/es/tx/builder/schema.mjs.map +1 -0
  65. package/es/tx/{index.js → index.mjs} +107 -15
  66. package/es/tx/index.mjs.map +1 -0
  67. package/es/tx/tx-object.mjs +283 -0
  68. package/es/tx/tx-object.mjs.map +1 -0
  69. package/es/tx/tx.mjs +938 -0
  70. package/es/tx/tx.mjs.map +1 -0
  71. package/es/tx/validator.mjs +247 -0
  72. package/es/tx/validator.mjs.map +1 -0
  73. package/es/typings/blakejs/index.d.mjs +2 -0
  74. package/es/typings/blakejs/{index.d.js.map → index.d.mjs.map} +1 -1
  75. package/es/utils/aepp-wallet-communication/connection/{browser-runtime.js → browser-runtime.mjs} +27 -29
  76. package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +1 -0
  77. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs +183 -0
  78. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs.map +1 -0
  79. package/es/utils/aepp-wallet-communication/connection/{index.js → index.mjs} +7 -3
  80. package/es/utils/aepp-wallet-communication/connection/index.mjs.map +1 -0
  81. package/es/utils/aepp-wallet-communication/{content-script-bridge.js → content-script-bridge.mjs} +29 -23
  82. package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +1 -0
  83. package/es/utils/aepp-wallet-communication/helpers.mjs +92 -0
  84. package/es/utils/aepp-wallet-communication/helpers.mjs.map +1 -0
  85. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs +375 -0
  86. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +1 -0
  87. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs +275 -0
  88. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +1 -0
  89. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs +541 -0
  90. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs.map +1 -0
  91. package/es/utils/aepp-wallet-communication/schema.mjs +102 -0
  92. package/es/utils/aepp-wallet-communication/schema.mjs.map +1 -0
  93. package/es/utils/aepp-wallet-communication/wallet-detector.mjs +110 -0
  94. package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
  95. package/es/utils/amount-formatter.mjs +139 -0
  96. package/es/utils/amount-formatter.mjs.map +1 -0
  97. package/es/utils/async-init.mjs +64 -0
  98. package/es/utils/async-init.mjs.map +1 -0
  99. package/es/utils/bignumber.mjs +21 -0
  100. package/es/utils/bignumber.mjs.map +1 -0
  101. package/es/utils/{bytes.js → bytes.mjs} +16 -12
  102. package/es/utils/bytes.mjs.map +1 -0
  103. package/es/utils/{crypto-ts.js → crypto-ts.mjs} +11 -3
  104. package/es/utils/crypto-ts.mjs.map +1 -0
  105. package/es/utils/crypto.mjs +239 -0
  106. package/es/utils/crypto.mjs.map +1 -0
  107. package/es/utils/errors.mjs +689 -0
  108. package/es/utils/errors.mjs.map +1 -0
  109. package/es/utils/hd-wallet.mjs +111 -0
  110. package/es/utils/hd-wallet.mjs.map +1 -0
  111. package/es/utils/json-big.mjs +45 -0
  112. package/es/utils/json-big.mjs.map +1 -0
  113. package/es/utils/keystore.mjs +240 -0
  114. package/es/utils/keystore.mjs.map +1 -0
  115. package/es/utils/mptree.mjs +161 -0
  116. package/es/utils/mptree.mjs.map +1 -0
  117. package/es/utils/other.mjs +43 -0
  118. package/es/utils/other.mjs.map +1 -0
  119. package/es/utils/semver-satisfies.mjs +16 -0
  120. package/es/utils/semver-satisfies.mjs.map +1 -0
  121. package/es/utils/{string.js → string.mjs} +5 -14
  122. package/es/utils/string.mjs.map +1 -0
  123. package/es/utils/swagger.mjs +164 -0
  124. package/es/utils/swagger.mjs.map +1 -0
  125. package/package.json +55 -62
  126. package/src/account/base.js +3 -1
  127. package/src/account/memory.js +13 -8
  128. package/src/account/multiple.js +31 -22
  129. package/src/ae/aens.js +75 -77
  130. package/src/ae/aepp.js +2 -2
  131. package/src/ae/contract.js +27 -291
  132. package/src/ae/index.js +9 -7
  133. package/src/ae/oracle.js +51 -28
  134. package/src/ae/universal.js +4 -2
  135. package/src/ae/wallet.js +2 -2
  136. package/src/chain/index.js +29 -6
  137. package/src/chain/node.js +95 -67
  138. package/src/channel/handlers.js +30 -21
  139. package/src/channel/index.js +44 -20
  140. package/src/channel/internal.js +30 -24
  141. package/src/contract/aci.js +376 -0
  142. package/src/contract/compiler.js +41 -91
  143. package/src/contract/ga/helpers.js +10 -13
  144. package/src/contract/ga/index.js +42 -33
  145. package/src/index.js +9 -10
  146. package/src/node-pool/index.js +29 -17
  147. package/src/node.js +33 -36
  148. package/src/tx/builder/field-types.js +61 -0
  149. package/src/tx/builder/helpers.js +126 -84
  150. package/src/tx/builder/index.js +83 -41
  151. package/src/tx/builder/schema.js +50 -40
  152. package/src/tx/index.js +101 -12
  153. package/src/tx/tx-object.js +35 -15
  154. package/src/tx/tx.js +232 -153
  155. package/src/tx/validator.js +34 -12
  156. package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +11 -5
  157. package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +35 -13
  158. package/src/utils/aepp-wallet-communication/connection/index.js +2 -1
  159. package/src/utils/aepp-wallet-communication/content-script-bridge.js +14 -7
  160. package/src/utils/aepp-wallet-communication/helpers.js +6 -14
  161. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +107 -60
  162. package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +24 -11
  163. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +98 -56
  164. package/src/utils/aepp-wallet-communication/schema.ts +80 -0
  165. package/src/utils/aepp-wallet-communication/wallet-detector.js +6 -2
  166. package/src/utils/amount-formatter.ts +4 -3
  167. package/src/utils/async-init.js +10 -6
  168. package/src/utils/bignumber.ts +3 -10
  169. package/src/utils/bytes.ts +3 -2
  170. package/src/utils/crypto-ts.ts +2 -2
  171. package/src/utils/crypto.js +10 -144
  172. package/src/utils/errors.ts +683 -0
  173. package/src/utils/hd-wallet.js +11 -26
  174. package/src/utils/keystore.js +57 -53
  175. package/src/utils/mptree.ts +12 -6
  176. package/src/utils/other.ts +1 -1
  177. package/src/utils/semver-satisfies.ts +1 -1
  178. package/src/utils/swagger.js +75 -45
  179. package/es/account/base.js +0 -259
  180. package/es/account/base.js.map +0 -1
  181. package/es/account/memory.js +0 -86
  182. package/es/account/memory.js.map +0 -1
  183. package/es/account/multiple.js +0 -267
  184. package/es/account/multiple.js.map +0 -1
  185. package/es/ae/aens.js +0 -829
  186. package/es/ae/aens.js.map +0 -1
  187. package/es/ae/aepp.js.map +0 -1
  188. package/es/ae/contract.js +0 -985
  189. package/es/ae/contract.js.map +0 -1
  190. package/es/ae/index.js +0 -442
  191. package/es/ae/index.js.map +0 -1
  192. package/es/ae/oracle.js +0 -690
  193. package/es/ae/oracle.js.map +0 -1
  194. package/es/ae/universal.js.map +0 -1
  195. package/es/ae/wallet.js.map +0 -1
  196. package/es/chain/index.js.map +0 -1
  197. package/es/chain/node.js +0 -1119
  198. package/es/chain/node.js.map +0 -1
  199. package/es/channel/handlers.js +0 -1601
  200. package/es/channel/handlers.js.map +0 -1
  201. package/es/channel/index.js.map +0 -1
  202. package/es/channel/internal.js +0 -438
  203. package/es/channel/internal.js.map +0 -1
  204. package/es/contract/aci/helpers.js +0 -298
  205. package/es/contract/aci/helpers.js.map +0 -1
  206. package/es/contract/aci/index.js +0 -517
  207. package/es/contract/aci/index.js.map +0 -1
  208. package/es/contract/aci/transformation.js +0 -742
  209. package/es/contract/aci/transformation.js.map +0 -1
  210. package/es/contract/compiler.js +0 -316
  211. package/es/contract/compiler.js.map +0 -1
  212. package/es/contract/ga/helpers.js +0 -120
  213. package/es/contract/ga/helpers.js.map +0 -1
  214. package/es/contract/ga/index.js +0 -307
  215. package/es/contract/ga/index.js.map +0 -1
  216. package/es/contract/index.js +0 -158
  217. package/es/contract/index.js.map +0 -1
  218. package/es/index.js +0 -57
  219. package/es/index.js.map +0 -1
  220. package/es/node-pool/index.js +0 -204
  221. package/es/node-pool/index.js.map +0 -1
  222. package/es/node.js +0 -228
  223. package/es/node.js.map +0 -1
  224. package/es/tx/builder/helpers.js +0 -355
  225. package/es/tx/builder/helpers.js.map +0 -1
  226. package/es/tx/builder/index.js +0 -686
  227. package/es/tx/builder/index.js.map +0 -1
  228. package/es/tx/builder/schema.js +0 -695
  229. package/es/tx/builder/schema.js.map +0 -1
  230. package/es/tx/index.js.map +0 -1
  231. package/es/tx/tx-object.js +0 -297
  232. package/es/tx/tx-object.js.map +0 -1
  233. package/es/tx/tx.js +0 -1757
  234. package/es/tx/tx.js.map +0 -1
  235. package/es/tx/validator.js +0 -344
  236. package/es/tx/validator.js.map +0 -1
  237. package/es/typings/blakejs/index.d.js +0 -2
  238. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +0 -1
  239. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js +0 -211
  240. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +0 -1
  241. package/es/utils/aepp-wallet-communication/connection/index.js.map +0 -1
  242. package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +0 -1
  243. package/es/utils/aepp-wallet-communication/helpers.js +0 -148
  244. package/es/utils/aepp-wallet-communication/helpers.js.map +0 -1
  245. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js +0 -652
  246. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +0 -1
  247. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js +0 -285
  248. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +0 -1
  249. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +0 -763
  250. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +0 -1
  251. package/es/utils/aepp-wallet-communication/schema.js +0 -103
  252. package/es/utils/aepp-wallet-communication/schema.js.map +0 -1
  253. package/es/utils/aepp-wallet-communication/wallet-detector.js +0 -135
  254. package/es/utils/aepp-wallet-communication/wallet-detector.js.map +0 -1
  255. package/es/utils/amount-formatter.js +0 -152
  256. package/es/utils/amount-formatter.js.map +0 -1
  257. package/es/utils/async-init.js +0 -103
  258. package/es/utils/async-init.js.map +0 -1
  259. package/es/utils/bignumber.js +0 -36
  260. package/es/utils/bignumber.js.map +0 -1
  261. package/es/utils/bytes.js.map +0 -1
  262. package/es/utils/crypto-ts.js.map +0 -1
  263. package/es/utils/crypto.js +0 -358
  264. package/es/utils/crypto.js.map +0 -1
  265. package/es/utils/hd-wallet.js +0 -148
  266. package/es/utils/hd-wallet.js.map +0 -1
  267. package/es/utils/json-big.js +0 -78
  268. package/es/utils/json-big.js.map +0 -1
  269. package/es/utils/keystore.js +0 -393
  270. package/es/utils/keystore.js.map +0 -1
  271. package/es/utils/mptree.js +0 -198
  272. package/es/utils/mptree.js.map +0 -1
  273. package/es/utils/other.js +0 -93
  274. package/es/utils/other.js.map +0 -1
  275. package/es/utils/semver-satisfies.js +0 -35
  276. package/es/utils/semver-satisfies.js.map +0 -1
  277. package/es/utils/string.js.map +0 -1
  278. package/es/utils/swagger.js +0 -269
  279. package/es/utils/swagger.js.map +0 -1
  280. package/src/contract/aci/helpers.js +0 -140
  281. package/src/contract/aci/index.js +0 -191
  282. package/src/contract/aci/transformation.js +0 -464
  283. package/src/contract/index.js +0 -159
  284. package/src/utils/aepp-wallet-communication/schema.js +0 -110
@@ -0,0 +1,1110 @@
1
+ /*
2
+ * ISC License (ISC)
3
+ * Copyright (c) 2018 aeternity developers
4
+ *
5
+ * Permission to use, copy, modify, and/or distribute this software for any
6
+ * purpose with or without fee is hereby granted, provided that the above
7
+ * copyright notice and this permission notice appear in all copies.
8
+ *
9
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ * PERFORMANCE OF THIS SOFTWARE.
16
+ */
17
+ import { generateKeyPair, encodeContractAddress } from "../utils/crypto.mjs";
18
+ import { options, changeStatus, changeState, call, send, emit, channelId, disconnect, fsmId } from "./internal.mjs";
19
+ import { unpackTx, buildTx } from "../tx/builder/index.mjs";
20
+ import { encode } from "../tx/builder/helpers.mjs";
21
+ import { IllegalArgumentError, InsufficientBalanceError, ChannelConnectionError, UnexpectedChannelMessageError } from "../utils/errors.mjs";
22
+
23
+ async function appendSignature(tx, signFn) {
24
+ const {
25
+ signatures,
26
+ encodedTx
27
+ } = unpackTx(tx).tx;
28
+ const result = await signFn(encode(encodedTx.rlpEncoded, 'tx'));
29
+
30
+ if (typeof result === 'string') {
31
+ const {
32
+ tx: signedTx,
33
+ txType
34
+ } = unpackTx(result);
35
+ return buildTx({
36
+ signatures: signatures.concat(signedTx.signatures),
37
+ encodedTx: signedTx.encodedTx.rlpEncoded
38
+ }, txType).tx;
39
+ }
40
+
41
+ return result;
42
+ }
43
+
44
+ function handleUnexpectedMessage(channel, message, state) {
45
+ if (state && state.reject) {
46
+ state.reject(Object.assign(new UnexpectedChannelMessageError(`Unexpected message received:\n\n${JSON.stringify(message)}`), {
47
+ wsMessage: message
48
+ }));
49
+ }
50
+
51
+ return {
52
+ handler: channelOpen
53
+ };
54
+ }
55
+
56
+ export function awaitingConnection(channel, message, state) {
57
+ if (message.method === 'channels.info') {
58
+ if (['channel_accept', 'funding_created'].includes(message.params.data.event)) {
59
+ changeStatus(channel, {
60
+ channel_accept: 'accepted',
61
+ funding_created: 'halfSigned'
62
+ }[message.params.data.event]);
63
+ return {
64
+ handler: awaitingChannelCreateTx
65
+ };
66
+ }
67
+
68
+ if (message.params.data.event === 'channel_reestablished') {
69
+ return {
70
+ handler: awaitingOpenConfirmation
71
+ };
72
+ }
73
+
74
+ if (message.params.data.event === 'fsm_up') {
75
+ fsmId.set(channel, message.params.data.fsm_id);
76
+ return {
77
+ handler: awaitingConnection
78
+ };
79
+ }
80
+
81
+ return {
82
+ handler: awaitingConnection
83
+ };
84
+ }
85
+
86
+ if (message.method === 'channels.error') {
87
+ emit(channel, 'error', new ChannelConnectionError(message.payload.message));
88
+ return {
89
+ handler: channelClosed
90
+ };
91
+ }
92
+ }
93
+ export async function awaitingReconnection(channel, message, state) {
94
+ if (message.method === 'channels.info') {
95
+ if (message.params.data.event === 'fsm_up') {
96
+ fsmId.set(channel, message.params.data.fsm_id);
97
+ changeState(channel, (await call(channel, 'channels.get.offchain_state', {})).signed_tx);
98
+ return {
99
+ handler: channelOpen
100
+ };
101
+ }
102
+ }
103
+
104
+ return handleUnexpectedMessage(channel, message, state);
105
+ }
106
+ export async function awaitingChannelCreateTx(channel, message, state) {
107
+ const tag = {
108
+ initiator: 'initiator_sign',
109
+ responder: 'responder_sign'
110
+ }[options.get(channel).role];
111
+
112
+ if (message.method === `channels.sign.${tag}`) {
113
+ if (message.params.data.tx) {
114
+ const signedTx = await options.get(channel).sign(tag, message.params.data.tx);
115
+ send(channel, {
116
+ jsonrpc: '2.0',
117
+ method: `channels.${tag}`,
118
+ params: {
119
+ tx: signedTx
120
+ }
121
+ });
122
+ return {
123
+ handler: awaitingOnChainTx
124
+ };
125
+ }
126
+
127
+ const signedTx = await appendSignature(message.params.data.signed_tx, tx => options.get(channel).sign(tag, tx));
128
+ send(channel, {
129
+ jsonrpc: '2.0',
130
+ method: `channels.${tag}`,
131
+ params: {
132
+ signed_tx: signedTx
133
+ }
134
+ });
135
+ return {
136
+ handler: awaitingOnChainTx
137
+ };
138
+ }
139
+ }
140
+ export function awaitingOnChainTx(channel, message, state) {
141
+ if (message.method === 'channels.on_chain_tx') {
142
+ if (message.params.data.info === 'funding_signed' && options.get(channel).role === 'initiator') {
143
+ return {
144
+ handler: awaitingBlockInclusion
145
+ };
146
+ }
147
+
148
+ if (message.params.data.info === 'funding_created' && options.get(channel).role === 'responder') {
149
+ return {
150
+ handler: awaitingBlockInclusion
151
+ };
152
+ }
153
+ }
154
+
155
+ if (message.method === 'channels.info' && message.params.data.event === 'funding_signed' && options.get(channel).role === 'initiator') {
156
+ channelId.set(channel, message.params.channel_id);
157
+ changeStatus(channel, 'signed');
158
+ return {
159
+ handler: awaitingOnChainTx
160
+ };
161
+ }
162
+ }
163
+ export function awaitingBlockInclusion(channel, message, state) {
164
+ if (message.method === 'channels.info') {
165
+ const handler = {
166
+ funding_created: awaitingBlockInclusion,
167
+ own_funding_locked: awaitingBlockInclusion,
168
+ funding_locked: awaitingOpenConfirmation
169
+ }[message.params.data.event];
170
+
171
+ if (handler) {
172
+ return {
173
+ handler
174
+ };
175
+ }
176
+ }
177
+
178
+ if (message.method === 'channels.on_chain_tx') {
179
+ emit(channel, 'onChainTx', message.params.data.tx, {
180
+ info: message.params.data.info,
181
+ type: message.params.data.type
182
+ });
183
+ return {
184
+ handler: awaitingBlockInclusion
185
+ };
186
+ }
187
+ }
188
+ export function awaitingOpenConfirmation(channel, message, state) {
189
+ if (message.method === 'channels.info' && message.params.data.event === 'open') {
190
+ channelId.set(channel, message.params.channel_id);
191
+ return {
192
+ handler: awaitingInitialState
193
+ };
194
+ }
195
+ }
196
+ export function awaitingInitialState(channel, message, state) {
197
+ if (message.method === 'channels.update') {
198
+ changeState(channel, message.params.data.state);
199
+ return {
200
+ handler: channelOpen
201
+ };
202
+ }
203
+ }
204
+ export async function channelOpen(channel, message, state) {
205
+ switch (message.method) {
206
+ case 'channels.info':
207
+ switch (message.params.data.event) {
208
+ case 'update':
209
+ case 'withdraw_created':
210
+ case 'deposit_created':
211
+ return {
212
+ handler: awaitingTxSignRequest
213
+ };
214
+
215
+ case 'own_withdraw_locked':
216
+ case 'withdraw_locked':
217
+ case 'own_deposit_locked':
218
+ case 'deposit_locked':
219
+ case 'peer_disconnected':
220
+ case 'channel_reestablished':
221
+ case 'open':
222
+ // TODO: Better handling of peer_disconnected event.
223
+ //
224
+ // We should enter intermediate state where offchain transactions
225
+ // are blocked until channel is reestablished.
226
+ emit(channel, message.params.data.event);
227
+ return {
228
+ handler: channelOpen
229
+ };
230
+
231
+ case 'fsm_up':
232
+ fsmId.set(channel, message.params.data.fsm_id);
233
+ return {
234
+ handler: channelOpen
235
+ };
236
+
237
+ case 'timeout':
238
+ case 'close_mutual':
239
+ return {
240
+ handler: channelOpen
241
+ };
242
+
243
+ case 'closing':
244
+ changeStatus(channel, 'closing');
245
+ return {
246
+ handler: channelOpen
247
+ };
248
+
249
+ case 'closed_confirmed':
250
+ changeStatus(channel, 'closed');
251
+ return {
252
+ handler: channelClosed
253
+ };
254
+
255
+ case 'died':
256
+ changeStatus(channel, 'died');
257
+ return {
258
+ handler: channelClosed
259
+ };
260
+
261
+ case 'shutdown':
262
+ return {
263
+ handler: channelOpen
264
+ };
265
+ }
266
+
267
+ break;
268
+
269
+ case 'channels.on_chain_tx':
270
+ emit(channel, 'onChainTx', message.params.data.tx, {
271
+ info: message.params.data.info,
272
+ type: message.params.data.type
273
+ });
274
+ return {
275
+ handler: channelOpen
276
+ };
277
+
278
+ case 'channels.leave':
279
+ // TODO: emit event
280
+ return {
281
+ handler: channelOpen
282
+ };
283
+
284
+ case 'channels.update':
285
+ changeState(channel, message.params.data.state);
286
+ return {
287
+ handler: channelOpen
288
+ };
289
+
290
+ case 'channels.sign.shutdown_sign_ack':
291
+ return awaitingTxSignRequest(channel, message, state);
292
+ }
293
+ }
294
+
295
+ channelOpen.enter = channel => {
296
+ changeStatus(channel, 'open');
297
+ };
298
+
299
+ export async function awaitingOffChainTx(channel, message, state) {
300
+ if (message.method === 'channels.sign.update') {
301
+ const {
302
+ sign
303
+ } = state;
304
+
305
+ if (message.params.data.tx) {
306
+ const signedTx = await sign(message.params.data.tx, {
307
+ updates: message.params.data.updates
308
+ });
309
+ send(channel, {
310
+ jsonrpc: '2.0',
311
+ method: 'channels.update',
312
+ params: {
313
+ tx: signedTx
314
+ }
315
+ });
316
+ return {
317
+ handler: awaitingOffChainUpdate,
318
+ state
319
+ };
320
+ }
321
+
322
+ const signedTx = await appendSignature(message.params.data.signed_tx, tx => sign(tx, {
323
+ updates: message.params.data.updates
324
+ }));
325
+
326
+ if (typeof signedTx === 'string') {
327
+ send(channel, {
328
+ jsonrpc: '2.0',
329
+ method: 'channels.update',
330
+ params: {
331
+ signed_tx: signedTx
332
+ }
333
+ });
334
+ return {
335
+ handler: awaitingOffChainUpdate,
336
+ state
337
+ };
338
+ }
339
+
340
+ if (typeof signedTx === 'number') {
341
+ send(channel, {
342
+ jsonrpc: '2.0',
343
+ method: 'channels.update',
344
+ params: {
345
+ error: signedTx
346
+ }
347
+ });
348
+ return {
349
+ handler: awaitingOffChainTx,
350
+ state
351
+ };
352
+ }
353
+ }
354
+
355
+ if (message.method === 'channels.error') {
356
+ state.reject(new ChannelConnectionError(message.data.message));
357
+ return {
358
+ handler: channelOpen
359
+ };
360
+ }
361
+
362
+ if (message.error) {
363
+ const {
364
+ data = []
365
+ } = message.error;
366
+
367
+ if (data.find(i => i.code === 1001)) {
368
+ state.reject(new InsufficientBalanceError('Insufficient balance'));
369
+ } else if (data.find(i => i.code === 1002)) {
370
+ state.reject(new IllegalArgumentError('Amount cannot be negative'));
371
+ } else {
372
+ state.reject(new ChannelConnectionError(message.error.message));
373
+ }
374
+
375
+ return {
376
+ handler: channelOpen
377
+ };
378
+ }
379
+
380
+ if (message.method === 'channels.conflict') {
381
+ state.resolve({
382
+ accepted: false,
383
+ errorCode: message.params.data.error_code,
384
+ errorMessage: message.params.data.error_msg
385
+ });
386
+ return {
387
+ handler: channelOpen
388
+ };
389
+ }
390
+
391
+ if (message.method === 'channels.info') {
392
+ if (message.params.data.event === 'aborted_update') {
393
+ state.resolve({
394
+ accepted: false
395
+ });
396
+ return {
397
+ handler: channelOpen
398
+ };
399
+ }
400
+ }
401
+
402
+ return handleUnexpectedMessage(channel, message, state);
403
+ }
404
+ export function awaitingOffChainUpdate(channel, message, state) {
405
+ if (message.method === 'channels.update') {
406
+ changeState(channel, message.params.data.state);
407
+ state.resolve({
408
+ accepted: true,
409
+ signedTx: message.params.data.state
410
+ });
411
+ return {
412
+ handler: channelOpen
413
+ };
414
+ }
415
+
416
+ if (message.method === 'channels.conflict') {
417
+ state.resolve({
418
+ accepted: false,
419
+ errorCode: message.params.data.error_code,
420
+ errorMessage: message.params.data.error_msg
421
+ });
422
+ return {
423
+ handler: channelOpen
424
+ };
425
+ }
426
+
427
+ if (message.method === 'channels.info') {
428
+ if (message.params.data.event === 'aborted_update') {
429
+ state.resolve({
430
+ accepted: false
431
+ });
432
+ return {
433
+ handler: channelOpen
434
+ };
435
+ }
436
+ }
437
+
438
+ if (message.error) {
439
+ state.reject(new ChannelConnectionError(message.error.message));
440
+ return {
441
+ handler: channelOpen
442
+ };
443
+ }
444
+
445
+ return handleUnexpectedMessage(channel, message, state);
446
+ }
447
+ export async function awaitingTxSignRequest(channel, message, state) {
448
+ const [, tag] = message.method.match(/^channels\.sign\.([^.]+)$/) || [];
449
+
450
+ if (tag) {
451
+ if (message.params.data.tx) {
452
+ const signedTx = await options.get(channel).sign(tag, message.params.data.tx, {
453
+ updates: message.params.data.updates
454
+ });
455
+
456
+ if (signedTx) {
457
+ send(channel, {
458
+ jsonrpc: '2.0',
459
+ method: `channels.${tag}`,
460
+ params: {
461
+ tx: signedTx
462
+ }
463
+ });
464
+ return {
465
+ handler: channelOpen
466
+ };
467
+ }
468
+ } else {
469
+ const signedTx = await appendSignature(message.params.data.signed_tx, tx => options.get(channel).sign(tag, tx, {
470
+ updates: message.params.data.updates
471
+ }));
472
+
473
+ if (typeof signedTx === 'string') {
474
+ send(channel, {
475
+ jsonrpc: '2.0',
476
+ method: `channels.${tag}`,
477
+ params: {
478
+ signed_tx: signedTx
479
+ }
480
+ });
481
+ return {
482
+ handler: channelOpen
483
+ };
484
+ }
485
+
486
+ if (typeof signedTx === 'number') {
487
+ send(channel, {
488
+ jsonrpc: '2.0',
489
+ method: `channels.${tag}`,
490
+ params: {
491
+ error: signedTx
492
+ }
493
+ });
494
+ return {
495
+ handler: awaitingUpdateConflict,
496
+ state
497
+ };
498
+ }
499
+ } // soft-reject via competing update
500
+
501
+
502
+ send(channel, {
503
+ jsonrpc: '2.0',
504
+ method: 'channels.update.new',
505
+ params: {
506
+ from: generateKeyPair().publicKey,
507
+ to: generateKeyPair().publicKey,
508
+ amount: 1
509
+ }
510
+ });
511
+ return {
512
+ handler: awaitingUpdateConflict,
513
+ state
514
+ };
515
+ }
516
+
517
+ return handleUnexpectedMessage(channel, message, state);
518
+ }
519
+ export function awaitingUpdateConflict(channel, message, state) {
520
+ if (message.error) {
521
+ return {
522
+ handler: awaitingUpdateConflict,
523
+ state
524
+ };
525
+ }
526
+
527
+ if (message.method === 'channels.conflict') {
528
+ return {
529
+ handler: channelOpen
530
+ };
531
+ }
532
+
533
+ return handleUnexpectedMessage(channel, message, state);
534
+ }
535
+ export async function awaitingShutdownTx(channel, message, state) {
536
+ if (message.method === 'channels.sign.shutdown_sign') {
537
+ if (message.params.data.tx) {
538
+ const signedTx = await state.sign(message.params.data.tx);
539
+ send(channel, {
540
+ jsonrpc: '2.0',
541
+ method: 'channels.shutdown_sign',
542
+ params: {
543
+ tx: signedTx
544
+ }
545
+ });
546
+ return {
547
+ handler: awaitingShutdownOnChainTx,
548
+ state
549
+ };
550
+ }
551
+
552
+ const signedTx = await appendSignature(message.params.data.signed_tx, tx => state.sign(tx));
553
+ send(channel, {
554
+ jsonrpc: '2.0',
555
+ method: 'channels.shutdown_sign',
556
+ params: {
557
+ signed_tx: signedTx
558
+ }
559
+ });
560
+ return {
561
+ handler: awaitingShutdownOnChainTx,
562
+ state
563
+ };
564
+ }
565
+
566
+ return handleUnexpectedMessage(channel, message, state);
567
+ }
568
+ export function awaitingShutdownOnChainTx(channel, message, state) {
569
+ if (message.method === 'channels.on_chain_tx') {
570
+ // state.resolve(message.params.data.tx)
571
+ return {
572
+ handler: channelClosed,
573
+ state
574
+ };
575
+ }
576
+
577
+ return handleUnexpectedMessage(channel, message, state);
578
+ }
579
+ export function awaitingLeave(channel, message, state) {
580
+ if (message.method === 'channels.leave') {
581
+ state.resolve({
582
+ channelId: message.params.channel_id,
583
+ signedTx: message.params.data.state
584
+ });
585
+ disconnect(channel);
586
+ return {
587
+ handler: channelClosed
588
+ };
589
+ }
590
+
591
+ if (message.method === 'channels.error') {
592
+ state.reject(new ChannelConnectionError(message.data.message));
593
+ return {
594
+ handler: channelOpen
595
+ };
596
+ }
597
+
598
+ return handleUnexpectedMessage(channel, message, state);
599
+ }
600
+ export async function awaitingWithdrawTx(channel, message, state) {
601
+ if (message.method === 'channels.sign.withdraw_tx') {
602
+ const {
603
+ sign
604
+ } = state;
605
+
606
+ if (message.params.data.tx) {
607
+ const signedTx = await sign(message.params.data.tx, {
608
+ updates: message.params.data.updates
609
+ });
610
+ send(channel, {
611
+ jsonrpc: '2.0',
612
+ method: 'channels.withdraw_tx',
613
+ params: {
614
+ tx: signedTx
615
+ }
616
+ });
617
+ return {
618
+ handler: awaitingWithdrawCompletion,
619
+ state
620
+ };
621
+ }
622
+
623
+ const signedTx = await appendSignature(message.params.data.signed_tx, tx => sign(tx, {
624
+ updates: message.params.data.updates
625
+ }));
626
+
627
+ if (typeof signedTx === 'string') {
628
+ send(channel, {
629
+ jsonrpc: '2.0',
630
+ method: 'channels.withdraw_tx',
631
+ params: {
632
+ signed_tx: signedTx
633
+ }
634
+ });
635
+ return {
636
+ handler: awaitingWithdrawCompletion,
637
+ state
638
+ };
639
+ }
640
+
641
+ if (typeof signedTx === 'number') {
642
+ send(channel, {
643
+ jsonrpc: '2.0',
644
+ method: 'channels.withdraw_tx',
645
+ params: {
646
+ error: signedTx
647
+ }
648
+ });
649
+ return {
650
+ handler: awaitingWithdrawCompletion,
651
+ state
652
+ };
653
+ }
654
+ }
655
+
656
+ return handleUnexpectedMessage(channel, message, state);
657
+ }
658
+ export function awaitingWithdrawCompletion(channel, message, state) {
659
+ if (message.method === 'channels.on_chain_tx') {
660
+ if (state.onOnChainTx) {
661
+ state.onOnChainTx(message.params.data.tx);
662
+ }
663
+
664
+ return {
665
+ handler: awaitingWithdrawCompletion,
666
+ state
667
+ };
668
+ }
669
+
670
+ if (message.method === 'channels.info') {
671
+ if (['own_withdraw_locked', 'withdraw_locked'].includes(message.params.data.event)) {
672
+ const callback = {
673
+ own_withdraw_locked: state.onOwnWithdrawLocked,
674
+ withdraw_locked: state.onWithdrawLocked
675
+ }[message.params.data.event];
676
+
677
+ if (callback) {
678
+ callback();
679
+ }
680
+
681
+ return {
682
+ handler: awaitingWithdrawCompletion,
683
+ state
684
+ };
685
+ }
686
+ }
687
+
688
+ if (message.method === 'channels.update') {
689
+ changeState(channel, message.params.data.state);
690
+ state.resolve({
691
+ accepted: true,
692
+ signedTx: message.params.data.state
693
+ });
694
+ return {
695
+ handler: channelOpen
696
+ };
697
+ }
698
+
699
+ if (message.method === 'channels.conflict') {
700
+ state.resolve({
701
+ accepted: false,
702
+ errorCode: message.params.data.error_code,
703
+ errorMessage: message.params.data.error_msg
704
+ });
705
+ return {
706
+ handler: channelOpen
707
+ };
708
+ }
709
+
710
+ if (message.method === 'channels.info') {
711
+ if (message.params.data.event === 'aborted_update') {
712
+ state.resolve({
713
+ accepted: false
714
+ });
715
+ return {
716
+ handler: channelOpen
717
+ };
718
+ }
719
+ }
720
+
721
+ return handleUnexpectedMessage(channel, message, state);
722
+ }
723
+ export async function awaitingDepositTx(channel, message, state) {
724
+ if (message.method === 'channels.sign.deposit_tx') {
725
+ const {
726
+ sign
727
+ } = state;
728
+
729
+ if (message.params.data.tx) {
730
+ const signedTx = await sign(message.params.data.tx, {
731
+ updates: message.params.data.updates
732
+ });
733
+ send(channel, {
734
+ jsonrpc: '2.0',
735
+ method: 'channels.deposit_tx',
736
+ params: {
737
+ tx: signedTx
738
+ }
739
+ });
740
+ return {
741
+ handler: awaitingDepositCompletion,
742
+ state
743
+ };
744
+ }
745
+
746
+ const signedTx = await appendSignature(message.params.data.signed_tx, tx => sign(tx, {
747
+ updates: message.params.data.updates
748
+ }));
749
+
750
+ if (typeof signedTx === 'string') {
751
+ send(channel, {
752
+ jsonrpc: '2.0',
753
+ method: 'channels.deposit_tx',
754
+ params: {
755
+ signed_tx: signedTx
756
+ }
757
+ });
758
+ return {
759
+ handler: awaitingDepositCompletion,
760
+ state
761
+ };
762
+ }
763
+
764
+ if (typeof signedTx === 'number') {
765
+ send(channel, {
766
+ jsonrpc: '2.0',
767
+ method: 'channels.deposit_tx',
768
+ params: {
769
+ error: signedTx
770
+ }
771
+ });
772
+ return {
773
+ handler: awaitingDepositCompletion,
774
+ state
775
+ };
776
+ }
777
+ }
778
+
779
+ return handleUnexpectedMessage(channel, message, state);
780
+ }
781
+ export function awaitingDepositCompletion(channel, message, state) {
782
+ if (message.method === 'channels.on_chain_tx') {
783
+ if (state.onOnChainTx) {
784
+ state.onOnChainTx(message.params.data.tx);
785
+ }
786
+
787
+ return {
788
+ handler: awaitingDepositCompletion,
789
+ state
790
+ };
791
+ }
792
+
793
+ if (message.method === 'channels.info') {
794
+ if (['own_deposit_locked', 'deposit_locked'].includes(message.params.data.event)) {
795
+ const callback = {
796
+ own_deposit_locked: state.onOwnDepositLocked,
797
+ deposit_locked: state.onDepositLocked
798
+ }[message.params.data.event];
799
+
800
+ if (callback) {
801
+ callback();
802
+ }
803
+
804
+ return {
805
+ handler: awaitingDepositCompletion,
806
+ state
807
+ };
808
+ }
809
+ }
810
+
811
+ if (message.method === 'channels.update') {
812
+ changeState(channel, message.params.data.state);
813
+ state.resolve({
814
+ accepted: true,
815
+ signedTx: message.params.data.state
816
+ });
817
+ return {
818
+ handler: channelOpen
819
+ };
820
+ }
821
+
822
+ if (message.method === 'channels.conflict') {
823
+ state.resolve({
824
+ accepted: false,
825
+ errorCode: message.params.data.error_code,
826
+ errorMessage: message.params.data.error_msg
827
+ });
828
+ return {
829
+ handler: channelOpen
830
+ };
831
+ }
832
+
833
+ if (message.method === 'channels.info') {
834
+ if (message.params.data.event === 'aborted_update') {
835
+ state.resolve({
836
+ accepted: false
837
+ });
838
+ return {
839
+ handler: channelOpen
840
+ };
841
+ }
842
+ }
843
+
844
+ return handleUnexpectedMessage(channel, message, state);
845
+ }
846
+ export async function awaitingNewContractTx(channel, message, state) {
847
+ if (message.method === 'channels.sign.update') {
848
+ if (message.params.data.tx) {
849
+ const signedTx = await state.sign(message.params.data.tx);
850
+ send(channel, {
851
+ jsonrpc: '2.0',
852
+ method: 'channels.update',
853
+ params: {
854
+ tx: signedTx
855
+ }
856
+ });
857
+ return {
858
+ handler: awaitingNewContractCompletion,
859
+ state
860
+ };
861
+ }
862
+
863
+ const signedTx = await appendSignature(message.params.data.signed_tx, tx => state.sign(tx));
864
+
865
+ if (typeof signedTx === 'string') {
866
+ send(channel, {
867
+ jsonrpc: '2.0',
868
+ method: 'channels.update',
869
+ params: {
870
+ signed_tx: signedTx
871
+ }
872
+ });
873
+ return {
874
+ handler: awaitingNewContractCompletion,
875
+ state
876
+ };
877
+ }
878
+
879
+ if (typeof signedTx === 'number') {
880
+ send(channel, {
881
+ jsonrpc: '2.0',
882
+ method: 'channels.update',
883
+ params: {
884
+ error: signedTx
885
+ }
886
+ });
887
+ return {
888
+ handler: awaitingNewContractCompletion,
889
+ state
890
+ };
891
+ }
892
+ }
893
+
894
+ return handleUnexpectedMessage(channel, message, state);
895
+ }
896
+ export function awaitingNewContractCompletion(channel, message, state) {
897
+ if (message.method === 'channels.update') {
898
+ const {
899
+ round
900
+ } = unpackTx(message.params.data.state).tx.encodedTx.tx;
901
+ const owner = options.get(channel)[{
902
+ initiator: 'initiatorId',
903
+ responder: 'responderId'
904
+ }[options.get(channel).role]];
905
+ changeState(channel, message.params.data.state);
906
+ state.resolve({
907
+ accepted: true,
908
+ address: encodeContractAddress(owner, round),
909
+ signedTx: message.params.data.state
910
+ });
911
+ return {
912
+ handler: channelOpen
913
+ };
914
+ }
915
+
916
+ if (message.method === 'channels.conflict') {
917
+ state.resolve({
918
+ accepted: false,
919
+ errorCode: message.params.data.error_code,
920
+ errorMessage: message.params.data.error_msg
921
+ });
922
+ return {
923
+ handler: channelOpen
924
+ };
925
+ }
926
+
927
+ if (message.method === 'channels.info') {
928
+ if (message.params.data.event === 'aborted_update') {
929
+ state.resolve({
930
+ accepted: false
931
+ });
932
+ return {
933
+ handler: channelOpen
934
+ };
935
+ }
936
+ }
937
+
938
+ return handleUnexpectedMessage(channel, message, state);
939
+ }
940
+ export async function awaitingCallContractUpdateTx(channel, message, state) {
941
+ if (message.method === 'channels.sign.update') {
942
+ if (message.params.data.tx) {
943
+ const signedTx = await state.sign(message.params.data.tx, {
944
+ updates: message.params.data.updates
945
+ });
946
+ send(channel, {
947
+ jsonrpc: '2.0',
948
+ method: 'channels.update',
949
+ params: {
950
+ tx: signedTx
951
+ }
952
+ });
953
+ return {
954
+ handler: awaitingCallContractCompletion,
955
+ state
956
+ };
957
+ }
958
+
959
+ const signedTx = await appendSignature(message.params.data.signed_tx, tx => state.sign(tx, {
960
+ updates: message.params.data.updates
961
+ }));
962
+
963
+ if (typeof signedTx === 'string') {
964
+ send(channel, {
965
+ jsonrpc: '2.0',
966
+ method: 'channels.update',
967
+ params: {
968
+ signed_tx: signedTx
969
+ }
970
+ });
971
+ return {
972
+ handler: awaitingCallContractCompletion,
973
+ state
974
+ };
975
+ }
976
+
977
+ if (typeof signedTx === 'number') {
978
+ send(channel, {
979
+ jsonrpc: '2.0',
980
+ method: 'channels.update',
981
+ params: {
982
+ error: signedTx
983
+ }
984
+ });
985
+ return {
986
+ handler: awaitingCallContractCompletion,
987
+ state
988
+ };
989
+ }
990
+ }
991
+
992
+ return handleUnexpectedMessage(channel, message, state);
993
+ }
994
+ export async function awaitingCallContractForceProgressUpdate(channel, message, state) {
995
+ if (message.method === 'channels.sign.force_progress_tx') {
996
+ const signedTx = await appendSignature(message.params.data.signed_tx, tx => state.sign(tx, {
997
+ updates: message.params.data.updates
998
+ }));
999
+ send(channel, {
1000
+ jsonrpc: '2.0',
1001
+ method: 'channels.force_progress_sign',
1002
+ params: {
1003
+ signed_tx: signedTx
1004
+ }
1005
+ });
1006
+ return {
1007
+ handler: awaitingForceProgressCompletion,
1008
+ state
1009
+ };
1010
+ }
1011
+
1012
+ return handleUnexpectedMessage(channel, message, state);
1013
+ }
1014
+ export function awaitingForceProgressCompletion(channel, message, state) {
1015
+ if (message.method === 'channels.on_chain_tx') {
1016
+ if (state.onOnChainTx) {
1017
+ state.onOnChainTx(message.params.data);
1018
+ }
1019
+
1020
+ emit(channel, 'onChainTx', message.params.data.tx, {
1021
+ info: message.params.data.info,
1022
+ type: message.params.data.type
1023
+ });
1024
+ state.resolve({
1025
+ accepted: true,
1026
+ tx: message.params.data.tx
1027
+ });
1028
+ }
1029
+
1030
+ return handleUnexpectedMessage(channel, message, state);
1031
+ }
1032
+ export function awaitingCallContractCompletion(channel, message, state) {
1033
+ if (message.method === 'channels.update') {
1034
+ changeState(channel, message.params.data.state);
1035
+ state.resolve({
1036
+ accepted: true,
1037
+ signedTx: message.params.data.state
1038
+ });
1039
+ return {
1040
+ handler: channelOpen
1041
+ };
1042
+ }
1043
+
1044
+ if (message.method === 'channels.conflict') {
1045
+ state.resolve({
1046
+ accepted: false,
1047
+ errorCode: message.params.data.error_code,
1048
+ errorMessage: message.params.data.error_msg
1049
+ });
1050
+ return {
1051
+ handler: channelOpen
1052
+ };
1053
+ }
1054
+
1055
+ if (message.method === 'channels.info') {
1056
+ if (message.params.data.event === 'aborted_update') {
1057
+ state.resolve({
1058
+ accepted: false
1059
+ });
1060
+ return {
1061
+ handler: channelOpen
1062
+ };
1063
+ }
1064
+ }
1065
+
1066
+ return handleUnexpectedMessage(channel, message, state);
1067
+ }
1068
+ export function awaitingCallsPruned(channels, message, state) {
1069
+ if (message.method === 'channels.calls_pruned.reply') {
1070
+ state.resolve();
1071
+ return {
1072
+ handler: channelOpen
1073
+ };
1074
+ }
1075
+
1076
+ state.reject(new UnexpectedChannelMessageError('Unexpected message received'));
1077
+ return {
1078
+ handler: channelClosed
1079
+ };
1080
+ }
1081
+ export function channelClosed(channel, message, state) {
1082
+ if (!state) return {
1083
+ handler: channelClosed
1084
+ };
1085
+ if (message.params.data.event === 'closing') return {
1086
+ handler: channelClosed,
1087
+ state
1088
+ };
1089
+
1090
+ if (message.params.data.info === 'channel_closed') {
1091
+ state.closeTx = message.params.data.tx;
1092
+ return {
1093
+ handler: channelClosed,
1094
+ state
1095
+ };
1096
+ }
1097
+
1098
+ if (message.params.data.event === 'closed_confirmed') {
1099
+ state.resolve(state.closeTx);
1100
+ return {
1101
+ handler: channelClosed
1102
+ };
1103
+ }
1104
+
1105
+ return {
1106
+ handler: channelClosed,
1107
+ state
1108
+ };
1109
+ }
1110
+ //# sourceMappingURL=handlers.mjs.map