@aeternity/aepp-sdk 10.0.0 → 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 (424) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +15 -14
  3. package/dist/aepp-sdk.browser-script.js +1 -1
  4. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +13 -40
  5. package/dist/aepp-sdk.browser-script.js.map +1 -1
  6. package/dist/aepp-sdk.browser.js +1 -1
  7. package/dist/aepp-sdk.browser.js.map +1 -1
  8. package/dist/aepp-sdk.js +1 -1
  9. package/dist/aepp-sdk.js.map +1 -1
  10. package/es/AeSdk.d.ts +37 -0
  11. package/es/AeSdk.mjs +81 -0
  12. package/es/AeSdk.mjs.map +1 -0
  13. package/es/AeSdkAepp.d.ts +69 -0
  14. package/es/AeSdkAepp.mjs +175 -0
  15. package/es/AeSdkAepp.mjs.map +1 -0
  16. package/es/AeSdkBase.d.ts +212 -0
  17. package/es/AeSdkBase.mjs +310 -0
  18. package/es/AeSdkBase.mjs.map +1 -0
  19. package/es/AeSdkWallet.d.ts +103 -0
  20. package/es/AeSdkWallet.mjs +322 -0
  21. package/es/AeSdkWallet.mjs.map +1 -0
  22. package/es/Node.d.ts +44 -0
  23. package/es/Node.mjs +141 -0
  24. package/es/Node.mjs.map +1 -0
  25. package/es/account/Base.d.ts +65 -0
  26. package/es/account/Base.mjs +123 -0
  27. package/es/account/Base.mjs.map +1 -0
  28. package/es/account/Memory.d.ts +25 -0
  29. package/es/account/Memory.mjs +81 -0
  30. package/es/account/Memory.mjs.map +1 -0
  31. package/es/account/Rpc.d.ts +27 -0
  32. package/es/account/Rpc.mjs +79 -0
  33. package/es/account/Rpc.mjs.map +1 -0
  34. package/es/aens.d.ts +209 -0
  35. package/es/aens.mjs +332 -0
  36. package/es/aens.mjs.map +1 -0
  37. package/es/aepp-wallet-communication/connection/Browser.d.ts +32 -0
  38. package/es/aepp-wallet-communication/connection/Browser.mjs +70 -0
  39. package/es/aepp-wallet-communication/connection/Browser.mjs.map +1 -0
  40. package/es/aepp-wallet-communication/connection/BrowserRuntime.d.ts +17 -0
  41. package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs +57 -0
  42. package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs.map +1 -0
  43. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.d.ts +35 -0
  44. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs +142 -0
  45. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs.map +1 -0
  46. package/es/aepp-wallet-communication/connection-proxy.d.ts +10 -0
  47. package/{src/utils/crypto-ts.ts → es/aepp-wallet-communication/connection-proxy.mjs} +15 -11
  48. package/es/aepp-wallet-communication/connection-proxy.mjs.map +1 -0
  49. package/es/aepp-wallet-communication/rpc/RpcClient.d.ts +33 -0
  50. package/es/aepp-wallet-communication/rpc/RpcClient.mjs +169 -0
  51. package/es/aepp-wallet-communication/rpc/RpcClient.mjs.map +1 -0
  52. package/es/aepp-wallet-communication/rpc/types.d.ts +77 -0
  53. package/es/aepp-wallet-communication/rpc/types.mjs +3 -0
  54. package/es/aepp-wallet-communication/rpc/types.mjs.map +1 -0
  55. package/es/aepp-wallet-communication/schema.d.ts +118 -0
  56. package/es/aepp-wallet-communication/schema.mjs +214 -0
  57. package/es/aepp-wallet-communication/schema.mjs.map +1 -0
  58. package/es/aepp-wallet-communication/wallet-detector.d.ts +24 -0
  59. package/es/aepp-wallet-communication/wallet-detector.mjs +63 -0
  60. package/es/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
  61. package/es/apis/compiler/compiler.d.ts +92 -0
  62. package/es/apis/compiler/compiler.mjs +576 -0
  63. package/es/apis/compiler/compiler.mjs.map +1 -0
  64. package/es/apis/compiler/index.d.ts +2 -0
  65. package/es/apis/compiler/index.mjs +3 -0
  66. package/es/apis/compiler/index.mjs.map +1 -0
  67. package/es/apis/compiler/models/index.d.ts +248 -0
  68. package/es/apis/compiler/models/index.mjs +48 -0
  69. package/es/apis/compiler/models/index.mjs.map +1 -0
  70. package/es/apis/compiler/models/mappers.d.ts +23 -0
  71. package/es/apis/compiler/models/mappers.mjs +585 -0
  72. package/es/apis/compiler/models/mappers.mjs.map +1 -0
  73. package/es/apis/compiler/models/parameters.d.ts +13 -0
  74. package/es/apis/compiler/models/parameters.mjs +71 -0
  75. package/es/apis/compiler/models/parameters.mjs.map +1 -0
  76. package/es/apis/node/index.d.ts +2 -0
  77. package/es/apis/node/index.mjs +3 -0
  78. package/es/apis/node/index.mjs.map +1 -0
  79. package/es/apis/node/models/index.d.ts +1061 -0
  80. package/es/apis/node/models/index.mjs +2 -0
  81. package/es/apis/node/models/index.mjs.map +1 -0
  82. package/es/apis/node/models/mappers.d.ts +95 -0
  83. package/es/apis/node/models/mappers.mjs +4164 -0
  84. package/es/apis/node/models/mappers.mjs.map +1 -0
  85. package/es/apis/node/models/parameters.d.ts +17 -0
  86. package/es/apis/node/models/parameters.mjs +165 -0
  87. package/es/apis/node/models/parameters.mjs.map +1 -0
  88. package/es/apis/node/node.d.ts +215 -0
  89. package/es/apis/node/node.mjs +1165 -0
  90. package/es/apis/node/node.mjs.map +1 -0
  91. package/es/chain.d.ts +220 -0
  92. package/es/chain.mjs +524 -0
  93. package/es/chain.mjs.map +1 -0
  94. package/es/channel/handlers.d.ts +34 -0
  95. package/es/channel/{handlers.js → handlers.mjs} +163 -130
  96. package/es/channel/handlers.mjs.map +1 -0
  97. package/es/channel/index.d.ts +634 -0
  98. package/es/channel/index.mjs +978 -0
  99. package/es/channel/index.mjs.map +1 -0
  100. package/es/channel/internal.d.ts +100 -0
  101. package/es/channel/internal.mjs +328 -0
  102. package/es/channel/internal.mjs.map +1 -0
  103. package/es/contract/Compiler.d.ts +17 -0
  104. package/es/contract/Compiler.mjs +67 -0
  105. package/es/contract/Compiler.mjs.map +1 -0
  106. package/es/contract/aci.d.ts +125 -0
  107. package/es/contract/aci.mjs +496 -0
  108. package/es/contract/aci.mjs.map +1 -0
  109. package/es/contract/ga.d.ts +68 -0
  110. package/es/contract/ga.mjs +190 -0
  111. package/es/contract/ga.mjs.map +1 -0
  112. package/es/contract/methods.d.ts +68 -0
  113. package/es/contract/methods.mjs +101 -0
  114. package/es/contract/methods.mjs.map +1 -0
  115. package/es/index.d.ts +32 -0
  116. package/es/index.mjs +49 -0
  117. package/es/index.mjs.map +1 -0
  118. package/es/oracle.d.ts +154 -0
  119. package/es/oracle.mjs +288 -0
  120. package/es/oracle.mjs.map +1 -0
  121. package/es/spend.d.ts +61 -0
  122. package/es/spend.mjs +122 -0
  123. package/es/spend.mjs.map +1 -0
  124. package/es/tx/builder/constants.d.ts +41 -0
  125. package/es/tx/builder/constants.mjs +100 -0
  126. package/es/tx/builder/constants.mjs.map +1 -0
  127. package/es/tx/builder/field-types.d.ts +62 -0
  128. package/es/tx/builder/field-types.mjs +126 -0
  129. package/es/tx/builder/field-types.mjs.map +1 -0
  130. package/es/tx/builder/helpers.d.ts +131 -0
  131. package/es/tx/builder/helpers.mjs +223 -0
  132. package/es/tx/builder/helpers.mjs.map +1 -0
  133. package/es/tx/builder/index.d.ts +75 -0
  134. package/es/tx/builder/index.mjs +424 -0
  135. package/es/tx/builder/index.mjs.map +1 -0
  136. package/es/tx/builder/schema.d.ts +350 -0
  137. package/es/tx/builder/schema.mjs +320 -0
  138. package/es/tx/builder/schema.mjs.map +1 -0
  139. package/es/tx/index.d.ts +63 -0
  140. package/es/tx/index.mjs +209 -0
  141. package/es/tx/index.mjs.map +1 -0
  142. package/es/tx/min-fee.d.ts +20 -0
  143. package/es/tx/min-fee.mjs +148 -0
  144. package/es/tx/min-fee.mjs.map +1 -0
  145. package/es/tx/validator.d.ts +19 -0
  146. package/es/tx/validator.mjs +240 -0
  147. package/es/tx/validator.mjs.map +1 -0
  148. package/es/typings/@aeternity__argon2-browser/index.d.mjs +2 -0
  149. package/es/typings/@aeternity__argon2-browser/index.d.mjs.map +1 -0
  150. package/es/typings/@aeternity__uuid/index.d.mjs +2 -0
  151. package/es/typings/@aeternity__uuid/index.d.mjs.map +1 -0
  152. package/es/typings/bip32-path/index.d.mjs +2 -0
  153. package/es/typings/bip32-path/index.d.mjs.map +1 -0
  154. package/es/typings/blakejs/index.d.mjs +2 -0
  155. package/es/typings/blakejs/index.d.mjs.map +1 -0
  156. package/es/typings/tweetnacl-auth/index.d.mjs +2 -0
  157. package/es/typings/tweetnacl-auth/index.d.mjs.map +1 -0
  158. package/es/utils/amount-formatter.d.ts +42 -0
  159. package/es/utils/{amount-formatter.js → amount-formatter.mjs} +38 -44
  160. package/es/utils/amount-formatter.mjs.map +1 -0
  161. package/es/utils/autorest.d.ts +6 -0
  162. package/es/utils/autorest.mjs +97 -0
  163. package/es/utils/autorest.mjs.map +1 -0
  164. package/es/utils/bignumber.d.ts +13 -0
  165. package/es/utils/bignumber.mjs +23 -0
  166. package/es/utils/bignumber.mjs.map +1 -0
  167. package/es/{bytes.d.ts → utils/bytes.d.ts} +10 -13
  168. package/es/utils/{bytes.js → bytes.mjs} +26 -22
  169. package/es/utils/bytes.mjs.map +1 -0
  170. package/es/utils/crypto.d.ts +103 -0
  171. package/es/utils/crypto.mjs +221 -0
  172. package/es/utils/crypto.mjs.map +1 -0
  173. package/es/utils/encoder.d.ts +26 -0
  174. package/es/utils/encoder.mjs +102 -0
  175. package/es/utils/encoder.mjs.map +1 -0
  176. package/es/utils/errors.d.ts +481 -0
  177. package/es/utils/errors.mjs +882 -0
  178. package/es/utils/errors.mjs.map +1 -0
  179. package/es/utils/hd-wallet.d.ts +34 -0
  180. package/es/utils/hd-wallet.mjs +122 -0
  181. package/es/utils/hd-wallet.mjs.map +1 -0
  182. package/es/utils/json-big.d.ts +5 -0
  183. package/es/utils/json-big.mjs +41 -0
  184. package/es/utils/json-big.mjs.map +1 -0
  185. package/es/utils/keystore.d.ts +51 -0
  186. package/es/utils/keystore.mjs +145 -0
  187. package/es/utils/keystore.mjs.map +1 -0
  188. package/es/{mptree.d.ts → utils/mptree.d.ts} +5 -8
  189. package/es/utils/{mptree.js → mptree.mjs} +33 -25
  190. package/es/utils/mptree.mjs.map +1 -0
  191. package/es/utils/other.d.ts +14 -0
  192. package/es/utils/other.mjs +28 -0
  193. package/es/utils/other.mjs.map +1 -0
  194. package/es/{semver-satisfies.d.ts → utils/semver-satisfies.d.ts} +0 -0
  195. package/es/utils/{semver-satisfies.js → semver-satisfies.mjs} +1 -1
  196. package/es/utils/semver-satisfies.mjs.map +1 -0
  197. package/es/utils/string.d.ts +24 -0
  198. package/es/utils/{string.js → string.mjs} +9 -11
  199. package/es/utils/string.mjs.map +1 -0
  200. package/package.json +67 -64
  201. package/src/AeSdk.ts +65 -0
  202. package/src/AeSdkAepp.ts +167 -0
  203. package/src/AeSdkBase.ts +320 -0
  204. package/src/AeSdkWallet.ts +332 -0
  205. package/src/Node.ts +167 -0
  206. package/src/account/Base.ts +114 -0
  207. package/src/account/Memory.ts +83 -0
  208. package/src/account/Rpc.ts +67 -0
  209. package/src/aens.ts +391 -0
  210. package/src/aepp-wallet-communication/connection/Browser.ts +68 -0
  211. package/src/aepp-wallet-communication/connection/BrowserRuntime.ts +58 -0
  212. package/src/aepp-wallet-communication/connection/BrowserWindowMessage.ts +111 -0
  213. package/{es/utils/crypto-ts.js → src/aepp-wallet-communication/connection-proxy.ts} +16 -9
  214. package/src/aepp-wallet-communication/rpc/RpcClient.ts +137 -0
  215. package/src/aepp-wallet-communication/rpc/types.ts +66 -0
  216. package/src/aepp-wallet-communication/schema.ts +181 -0
  217. package/src/aepp-wallet-communication/wallet-detector.ts +71 -0
  218. package/src/apis/compiler/code-model-v4.yaml +2649 -0
  219. package/src/apis/compiler/compiler.ts +585 -0
  220. package/src/apis/compiler/index.ts +2 -0
  221. package/src/apis/compiler/models/index.ts +301 -0
  222. package/src/apis/compiler/models/mappers.ts +583 -0
  223. package/src/apis/compiler/models/parameters.ts +93 -0
  224. package/src/apis/node/code-model-v4.yaml +10046 -0
  225. package/src/apis/node/index.ts +2 -0
  226. package/src/apis/node/models/index.ts +1253 -0
  227. package/src/apis/node/models/mappers.ts +4278 -0
  228. package/src/apis/node/models/parameters.ts +188 -0
  229. package/src/apis/node/node.ts +1299 -0
  230. package/src/chain.ts +444 -0
  231. package/src/channel/{handlers.js → handlers.ts} +238 -143
  232. package/src/channel/index.ts +991 -0
  233. package/src/channel/internal.ts +345 -0
  234. package/src/contract/Compiler.ts +66 -0
  235. package/src/contract/aci.ts +529 -0
  236. package/src/contract/ga.ts +178 -0
  237. package/src/contract/methods.ts +118 -0
  238. package/src/index.ts +51 -0
  239. package/src/oracle.ts +342 -0
  240. package/src/spend.ts +152 -0
  241. package/src/tx/builder/constants.ts +96 -0
  242. package/src/tx/builder/field-types.ts +111 -0
  243. package/src/tx/builder/helpers.ts +253 -0
  244. package/src/tx/builder/index.ts +394 -0
  245. package/src/tx/builder/schema.ts +771 -0
  246. package/src/tx/index.ts +199 -0
  247. package/src/tx/min-fee.ts +136 -0
  248. package/src/tx/validator.ts +226 -0
  249. package/src/typings/@aeternity__argon2-browser/index.d.ts +3 -0
  250. package/src/typings/@aeternity__uuid/index.d.ts +3 -0
  251. package/src/typings/bip32-path/index.d.ts +33 -0
  252. package/src/typings/tweetnacl-auth/index.d.ts +14 -0
  253. package/src/utils/amount-formatter.ts +29 -41
  254. package/src/utils/autorest.ts +90 -0
  255. package/src/utils/bignumber.ts +9 -19
  256. package/src/utils/bytes.ts +15 -16
  257. package/src/utils/crypto.ts +217 -0
  258. package/src/utils/encoder.ts +98 -0
  259. package/src/utils/errors.ts +817 -0
  260. package/src/utils/hd-wallet.ts +134 -0
  261. package/src/utils/json-big.ts +26 -0
  262. package/src/utils/keystore.ts +179 -0
  263. package/src/utils/mptree.ts +26 -20
  264. package/src/utils/other.ts +28 -43
  265. package/src/utils/string.ts +8 -10
  266. package/CHANGELOG.md +0 -1603
  267. package/es/account/base.js +0 -169
  268. package/es/account/base.js.map +0 -1
  269. package/es/account/memory.js +0 -84
  270. package/es/account/memory.js.map +0 -1
  271. package/es/account/multiple.js +0 -178
  272. package/es/account/multiple.js.map +0 -1
  273. package/es/ae/aens.js +0 -408
  274. package/es/ae/aens.js.map +0 -1
  275. package/es/ae/aepp.js +0 -45
  276. package/es/ae/aepp.js.map +0 -1
  277. package/es/ae/contract.js +0 -312
  278. package/es/ae/contract.js.map +0 -1
  279. package/es/ae/index.js +0 -206
  280. package/es/ae/index.js.map +0 -1
  281. package/es/ae/oracle.js +0 -322
  282. package/es/ae/oracle.js.map +0 -1
  283. package/es/ae/universal.js +0 -45
  284. package/es/ae/universal.js.map +0 -1
  285. package/es/ae/wallet.js +0 -50
  286. package/es/ae/wallet.js.map +0 -1
  287. package/es/amount-formatter.d.ts +0 -58
  288. package/es/bignumber.d.ts +0 -24
  289. package/es/chain/index.js +0 -256
  290. package/es/chain/index.js.map +0 -1
  291. package/es/chain/node.js +0 -394
  292. package/es/chain/node.js.map +0 -1
  293. package/es/channel/handlers.js.map +0 -1
  294. package/es/channel/index.js +0 -980
  295. package/es/channel/index.js.map +0 -1
  296. package/es/channel/internal.js +0 -266
  297. package/es/channel/internal.js.map +0 -1
  298. package/es/contract/aci/helpers.js +0 -298
  299. package/es/contract/aci/helpers.js.map +0 -1
  300. package/es/contract/aci/index.js +0 -361
  301. package/es/contract/aci/index.js.map +0 -1
  302. package/es/contract/aci/transformation.js +0 -66
  303. package/es/contract/aci/transformation.js.map +0 -1
  304. package/es/contract/compiler.js +0 -244
  305. package/es/contract/compiler.js.map +0 -1
  306. package/es/contract/ga/helpers.js +0 -22
  307. package/es/contract/ga/helpers.js.map +0 -1
  308. package/es/contract/ga/index.js +0 -170
  309. package/es/contract/ga/index.js.map +0 -1
  310. package/es/contract/index.js +0 -162
  311. package/es/contract/index.js.map +0 -1
  312. package/es/crypto-ts.d.ts +0 -9
  313. package/es/index.js +0 -57
  314. package/es/index.js.map +0 -1
  315. package/es/node-pool/index.js +0 -187
  316. package/es/node-pool/index.js.map +0 -1
  317. package/es/node.js +0 -132
  318. package/es/node.js.map +0 -1
  319. package/es/other.d.ts +0 -31
  320. package/es/string.d.ts +0 -26
  321. package/es/tx/builder/helpers.js +0 -305
  322. package/es/tx/builder/helpers.js.map +0 -1
  323. package/es/tx/builder/index.js +0 -595
  324. package/es/tx/builder/index.js.map +0 -1
  325. package/es/tx/builder/schema.js +0 -814
  326. package/es/tx/builder/schema.js.map +0 -1
  327. package/es/tx/index.js +0 -300
  328. package/es/tx/index.js.map +0 -1
  329. package/es/tx/tx-object.js +0 -276
  330. package/es/tx/tx-object.js.map +0 -1
  331. package/es/tx/tx.js +0 -937
  332. package/es/tx/tx.js.map +0 -1
  333. package/es/tx/validator.js +0 -208
  334. package/es/tx/validator.js.map +0 -1
  335. package/es/typings/blakejs/index.d.js +0 -2
  336. package/es/typings/blakejs/index.d.js.map +0 -1
  337. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js +0 -131
  338. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +0 -1
  339. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js +0 -184
  340. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +0 -1
  341. package/es/utils/aepp-wallet-communication/connection/index.js +0 -84
  342. package/es/utils/aepp-wallet-communication/connection/index.js.map +0 -1
  343. package/es/utils/aepp-wallet-communication/content-script-bridge.js +0 -97
  344. package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +0 -1
  345. package/es/utils/aepp-wallet-communication/helpers.js +0 -103
  346. package/es/utils/aepp-wallet-communication/helpers.js.map +0 -1
  347. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js +0 -329
  348. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +0 -1
  349. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js +0 -270
  350. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +0 -1
  351. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +0 -533
  352. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +0 -1
  353. package/es/utils/aepp-wallet-communication/schema.js +0 -107
  354. package/es/utils/aepp-wallet-communication/schema.js.map +0 -1
  355. package/es/utils/aepp-wallet-communication/wallet-detector.js +0 -109
  356. package/es/utils/aepp-wallet-communication/wallet-detector.js.map +0 -1
  357. package/es/utils/amount-formatter.js.map +0 -1
  358. package/es/utils/async-init.js +0 -67
  359. package/es/utils/async-init.js.map +0 -1
  360. package/es/utils/bignumber.js +0 -28
  361. package/es/utils/bignumber.js.map +0 -1
  362. package/es/utils/bytes.js.map +0 -1
  363. package/es/utils/crypto-ts.js.map +0 -1
  364. package/es/utils/crypto.js +0 -304
  365. package/es/utils/crypto.js.map +0 -1
  366. package/es/utils/hd-wallet.js +0 -112
  367. package/es/utils/hd-wallet.js.map +0 -1
  368. package/es/utils/json-big.js +0 -45
  369. package/es/utils/json-big.js.map +0 -1
  370. package/es/utils/keystore.js +0 -228
  371. package/es/utils/keystore.js.map +0 -1
  372. package/es/utils/mptree.js.map +0 -1
  373. package/es/utils/other.js +0 -43
  374. package/es/utils/other.js.map +0 -1
  375. package/es/utils/semver-satisfies.js.map +0 -1
  376. package/es/utils/string.js.map +0 -1
  377. package/es/utils/swagger.js +0 -132
  378. package/es/utils/swagger.js.map +0 -1
  379. package/src/account/base.js +0 -153
  380. package/src/account/memory.js +0 -78
  381. package/src/account/multiple.js +0 -154
  382. package/src/ae/aens.js +0 -366
  383. package/src/ae/aepp.js +0 -45
  384. package/src/ae/contract.js +0 -285
  385. package/src/ae/index.js +0 -167
  386. package/src/ae/oracle.js +0 -296
  387. package/src/ae/universal.js +0 -47
  388. package/src/ae/wallet.js +0 -50
  389. package/src/chain/index.js +0 -250
  390. package/src/chain/node.js +0 -293
  391. package/src/channel/index.js +0 -898
  392. package/src/channel/internal.js +0 -231
  393. package/src/contract/aci/index.js +0 -317
  394. package/src/contract/aci/transformation.js +0 -72
  395. package/src/contract/compiler.js +0 -195
  396. package/src/contract/ga/helpers.js +0 -17
  397. package/src/contract/ga/index.js +0 -147
  398. package/src/contract/index.js +0 -163
  399. package/src/index.js +0 -90
  400. package/src/node-pool/index.js +0 -171
  401. package/src/node.js +0 -118
  402. package/src/tx/builder/helpers.js +0 -325
  403. package/src/tx/builder/index.js +0 -460
  404. package/src/tx/builder/schema.js +0 -1211
  405. package/src/tx/index.js +0 -301
  406. package/src/tx/tx-object.js +0 -186
  407. package/src/tx/tx.js +0 -622
  408. package/src/tx/validator.js +0 -161
  409. package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +0 -114
  410. package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +0 -161
  411. package/src/utils/aepp-wallet-communication/connection/index.js +0 -85
  412. package/src/utils/aepp-wallet-communication/content-script-bridge.js +0 -85
  413. package/src/utils/aepp-wallet-communication/helpers.js +0 -70
  414. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +0 -268
  415. package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +0 -223
  416. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +0 -390
  417. package/src/utils/aepp-wallet-communication/schema.js +0 -108
  418. package/src/utils/aepp-wallet-communication/wallet-detector.js +0 -91
  419. package/src/utils/async-init.js +0 -41
  420. package/src/utils/crypto.js +0 -312
  421. package/src/utils/hd-wallet.js +0 -115
  422. package/src/utils/json-big.js +0 -26
  423. package/src/utils/keystore.js +0 -223
  424. package/src/utils/swagger.js +0 -133
@@ -0,0 +1,991 @@
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 BigNumber from 'bignumber.js'
18
+ import { snakeToPascal } from '../utils/string'
19
+ import { buildTx, unpackTx } from '../tx/builder'
20
+ import { TX_TYPE } from '../tx/builder/schema'
21
+ import * as handlers from './handlers'
22
+ import {
23
+ eventEmitters,
24
+ status as channelStatus,
25
+ state as channelState,
26
+ initialize,
27
+ enqueueAction,
28
+ send,
29
+ channelId,
30
+ call,
31
+ disconnect as channelDisconnect,
32
+ fsmId as channelFsmId,
33
+ SignTx,
34
+ ChannelOptions,
35
+ ChannelState,
36
+ ChannelMessage
37
+ } from './internal'
38
+ import { UnknownChannelStateError, ChannelError } from '../utils/errors'
39
+ import { EncodedData } from '../utils/encoder'
40
+
41
+ function snakeToPascalObjKeys<Type> (obj: object): Type {
42
+ return Object.entries(obj).reduce((result, [key, val]) => ({
43
+ ...result,
44
+ [snakeToPascal(key)]: val
45
+ }), {}) as Type
46
+ }
47
+
48
+ type EventCallback = (...args: any[]) => void
49
+
50
+ interface CallContractOptions {
51
+ amount?: number | BigNumber
52
+ callData?: string
53
+ abiVersion?: number
54
+ contract?: string
55
+ returnValue?: any
56
+ gasUsed?: number | BigNumber
57
+ gasPrice?: number | BigNumber
58
+ height?: number
59
+ callerNonce?: number
60
+ log?: any
61
+ returnType?: string
62
+ }
63
+
64
+ interface Contract {
65
+ abiVersion: number
66
+ active: boolean
67
+ deposit: number | BigNumber
68
+ id: string
69
+ ownerId: string
70
+ referrerIds: string[]
71
+ vmVersion: number
72
+ }
73
+
74
+ /**
75
+ * Channel
76
+ * @example
77
+ * ```js
78
+ * await Channel.initialize({
79
+ * url: 'ws://localhost:3001',
80
+ * role: 'initiator'
81
+ * initiatorId: 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
82
+ * responderId: 'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E',
83
+ * initiatorAmount: 1e18,
84
+ * responderAmount: 1e18,
85
+ * pushAmount: 0,
86
+ * channelReserve: 0,
87
+ * ttl: 1000,
88
+ * host: 'localhost',
89
+ * port: 3002,
90
+ * lockPeriod: 10,
91
+ * async sign (tag, tx) => await account.signTransaction(tx)
92
+ * })
93
+ * ```
94
+ */
95
+ export default class Channel {
96
+ /**
97
+ * @param options - Channel params
98
+ * @param options.url - Channel url (for example: "ws://localhost:3001")
99
+ * @param options.role - Participant role ("initiator" or "responder")
100
+ * @param options.initiatorId - Initiator's public key
101
+ * @param options.responderId - Responder's public key
102
+ * @param options.pushAmount - Initial deposit in favour of the responder by the initiator
103
+ * @param options.initiatorAmount - Amount of coins the initiator has committed to
104
+ * the channel
105
+ * @param options.responderAmount - Amount of coins the responder has committed to
106
+ * the channel
107
+ * @param options.channelReserve - The minimum amount both peers need to maintain
108
+ * @param options.ttl - Minimum block height to include the channel_create_tx
109
+ * @param options.host - Host of the responder's node
110
+ * @param options.port - The port of the responders node
111
+ * @param options.lockPeriod - Amount of blocks for disputing a solo close
112
+ * @param options.existingChannelId - Existing channel id (required if reestablishing a
113
+ * channel)
114
+ * @param options.offchainTx - Offchain transaction (required if reestablishing
115
+ * a channel)
116
+ * @param options.timeoutIdle - The time waiting for a new event to be initiated
117
+ * (default: 600000)
118
+ * @param options.timeoutFundingCreate - The time waiting for the initiator to produce
119
+ * the create channel transaction after the noise session had been established (default: 120000)
120
+ * @param options.timeoutFundingSign - The time frame the other client has to sign an
121
+ * off-chain update after our client had initiated and signed it. This applies only for double
122
+ * signed on-chain intended updates: channel create transaction, deposit, withdrawal and etc.
123
+ * (default: 120000)
124
+ * @param options.timeoutFundingLock - The time frame the other client has to confirm an
125
+ * on-chain transaction reaching maturity (passing minimum depth) after the local node has
126
+ * detected this. This applies only for double signed on-chain intended updates: channel create
127
+ * transaction, deposit, withdrawal and etc. (default: 360000)
128
+ * @param options.timeoutSign - The time frame the client has to return a signed
129
+ * off-chain update or to decline it. This applies for all off-chain updates (default: 500000)
130
+ * @param options.timeoutAccept - The time frame the other client has to react to an
131
+ * event. This applies for all off-chain updates that are not meant to land on-chain, as well as
132
+ * some special cases: opening a noise connection, mutual closing acknowledgement and
133
+ * reestablishing an existing channel (default: 120000)
134
+ * @param options.timeoutInitialized - the time frame the responder has to accept an
135
+ * incoming noise session. Applicable only for initiator (default: timeout_accept's value)
136
+ * @param options.timeoutAwaitingOpen - The time frame the initiator has to start an
137
+ * outgoing noise session to the responder's node. Applicable only for responder (default:
138
+ * timeout_idle's value)
139
+ * @param options.debug=false - Log websocket communication
140
+ * @param options.sign - Function which verifies and signs transactions
141
+ */
142
+ static async initialize (options: ChannelOptions): Promise<Channel> {
143
+ const channel = new Channel()
144
+ await initialize(
145
+ channel,
146
+ options.existingFsmId != null ? handlers.awaitingReconnection : handlers.awaitingConnection,
147
+ handlers.channelOpen,
148
+ options
149
+ )
150
+ return channel
151
+ }
152
+
153
+ /**
154
+ * Register event listener function
155
+ *
156
+ * Possible events:
157
+ *
158
+ * - "error"
159
+ * - "onChainTx"
160
+ * - "ownWithdrawLocked"
161
+ * - "withdrawLocked"
162
+ * - "ownDepositLocked"
163
+ * - "depositLocked"
164
+ *
165
+ * TODO: the event list looks outdated
166
+ *
167
+ * @param eventName - Event name
168
+ * @param callback - Callback function
169
+ */
170
+ // TODO define specific callback type depending on the event name
171
+ on (eventName: string, callback: EventCallback): void {
172
+ const eventEmitter = eventEmitters.get(this)
173
+ if (eventEmitter == null) throw new UnknownChannelStateError()
174
+ eventEmitter.on(eventName, callback)
175
+ }
176
+
177
+ /**
178
+ * Remove event listener function
179
+ * @param eventName - Event name
180
+ * @param callback - Callback function
181
+ */
182
+ off (eventName: string, callback: EventCallback): void {
183
+ const eventEmitter = eventEmitters.get(this)
184
+ if (eventEmitter == null) throw new UnknownChannelStateError()
185
+ eventEmitter.removeListener(eventName, callback)
186
+ }
187
+
188
+ /**
189
+ * Close the connection
190
+ */
191
+ disconnect (): void {
192
+ return channelDisconnect(this)
193
+ }
194
+
195
+ /**
196
+ * Get current status
197
+ *
198
+ */
199
+ status (): string {
200
+ const status = channelStatus.get(this)
201
+ if (status == null) throw new UnknownChannelStateError()
202
+ return status
203
+ }
204
+
205
+ /**
206
+ * Get current state
207
+ */
208
+ async state (): Promise<ChannelState> {
209
+ return await snakeToPascalObjKeys(await call(this, 'channels.get.offchain_state', {}))
210
+ }
211
+
212
+ /**
213
+ * Get current round
214
+ *
215
+ * If round cannot be determined (for example when channel has not been opened)
216
+ * it will return `null`.
217
+ *
218
+ */
219
+ round (): number | null {
220
+ const state = channelState.get(this)
221
+ if (state == null) {
222
+ return null
223
+ }
224
+ const { txType, tx } = unpackTx(state, TX_TYPE.signed).tx.encodedTx
225
+ switch (txType) {
226
+ case TX_TYPE.channelCreate:
227
+ return 1
228
+ case TX_TYPE.channelOffChain:
229
+ case TX_TYPE.channelWithdraw:
230
+ case TX_TYPE.channelDeposit:
231
+ return +tx.round
232
+ default:
233
+ return null
234
+ }
235
+ }
236
+
237
+ /**
238
+ * Get channel id
239
+ *
240
+ */
241
+ id (): string {
242
+ const id = channelId.get(this)
243
+ if (id == null) throw new ChannelError('Channel is not initialized')
244
+ return id
245
+ }
246
+
247
+ /**
248
+ * Get channel's fsm id
249
+ *
250
+ */
251
+ fsmId (): string {
252
+ const id = channelFsmId.get(this)
253
+ if (id == null) throw new ChannelError('Channel is not initialized')
254
+ return id
255
+ }
256
+
257
+ /**
258
+ * Trigger a transfer update
259
+ *
260
+ * The transfer update is moving coins from one channel account to another.
261
+ * The update is a change to be applied on top of the latest state.
262
+ *
263
+ * Sender and receiver are the channel parties. Both the initiator and responder
264
+ * can take those roles. Any public key outside of the channel is considered invalid.
265
+ *
266
+ * @param from - Sender's public address
267
+ * @param to - Receiver's public address
268
+ * @param amount - Transaction amount
269
+ * @param sign - Function which verifies and signs offchain transaction
270
+ * @param metadata - Metadata
271
+
272
+ * @example
273
+ * ```js
274
+ * channel.update(
275
+ * 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
276
+ * 'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E',
277
+ * 10,
278
+ * async (tx) => await account.signTransaction(tx)
279
+ * ).then(({ accepted, signedTx }) =>
280
+ * if (accepted) {
281
+ * console.log('Update has been accepted')
282
+ * }
283
+ * )
284
+ * ```
285
+ */
286
+ async update (
287
+ from: string,
288
+ to: string,
289
+ amount: number | BigNumber,
290
+ sign: SignTx,
291
+ metadata: string[] = []): Promise<{
292
+ accepted: boolean
293
+ signedTx?: string
294
+ errorCode?: number
295
+ errorMessage?: string
296
+ }> {
297
+ return await new Promise((resolve, reject) => {
298
+ enqueueAction(
299
+ this,
300
+ (_channel, state) => state?.handler === handlers.channelOpen,
301
+ (channel, _state) => {
302
+ send(channel, {
303
+ jsonrpc: '2.0',
304
+ method: 'channels.update.new',
305
+ params: { from, to, amount, meta: metadata }
306
+ })
307
+ return {
308
+ handler: handlers.awaitingOffChainTx,
309
+ state: {
310
+ resolve,
311
+ reject,
312
+ sign
313
+ }
314
+ }
315
+ }
316
+ )
317
+ })
318
+ }
319
+
320
+ /**
321
+ * Get proof of inclusion
322
+ *
323
+ * If a certain address of an account or a contract is not found
324
+ * in the state tree - the response is an error.
325
+ *
326
+ * @param addresses - Addresses
327
+ * @param addresses.accounts - List of account addresses to include in poi
328
+ * @param addresses.contracts - List of contract addresses to include in poi
329
+ * @example
330
+ * ```js
331
+ * channel.poi({
332
+ * accounts: [
333
+ * 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
334
+ * 'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E'
335
+ * ],
336
+ * contracts: ['ct_2dCUAWYZdrWfACz3a2faJeKVTVrfDYxCQHCqAt5zM15f3u2UfA']
337
+ * }).then(poi => console.log(poi))
338
+ * ```
339
+ */
340
+ async poi (
341
+ { accounts, contracts }: { accounts: string[], contracts?: string[] }
342
+ ): Promise<EncodedData<'pi'>> {
343
+ return (await call(this, 'channels.get.poi', { accounts, contracts })).poi
344
+ }
345
+
346
+ /**
347
+ * Get balances
348
+ *
349
+ * The accounts paramcontains a list of addresses to fetch balances of.
350
+ * Those can be either account balances or a contract ones, encoded as an account addresses.
351
+ *
352
+ * If a certain account address had not being found in the state tree - it is simply
353
+ * skipped in the response.
354
+ *
355
+ * @param accounts - List of addresses to fetch balances from
356
+ * @example
357
+ * ```js
358
+ * channel.balances([
359
+ * 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
360
+ * 'ak_V6an1xhec1xVaAhLuak7QoEbi6t7w5hEtYWp9bMKaJ19i6A9E'
361
+ * 'ct_2dCUAWYZdrWfACz3a2faJeKVTVrfDYxCQHCqAt5zM15f3u2UfA'
362
+ * ]).then(balances =>
363
+ * console.log(balances['ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH'])
364
+ * )
365
+ * ```
366
+ */
367
+ async balances (accounts: Array<EncodedData<'ak'>>): Promise<{ [key: EncodedData<'ak'>]: string }> {
368
+ return Object.fromEntries(
369
+ (await call(this, 'channels.get.balances', { accounts }))
370
+ .map((item: {
371
+ account: EncodedData<'ak'>
372
+ balance: string
373
+ }) => [item.account, item.balance])
374
+ )
375
+ }
376
+
377
+ /**
378
+ * Leave channel
379
+ *
380
+ * It is possible to leave a channel and then later reestablish the channel
381
+ * off-chain state and continue operation. When a leave method is called,
382
+ * the channel fsm passes it on to the peer fsm, reports the current mutually
383
+ * signed state and then terminates.
384
+ *
385
+ * The channel can be reestablished by instantiating another Channel instance
386
+ * with two extra params: existingChannelId and offchainTx (returned from leave
387
+ * method as channelId and signedTx respectively).
388
+ *
389
+ * @example
390
+ * ```js
391
+ * channel.leave().then(({ channelId, signedTx }) => {
392
+ * console.log(channelId)
393
+ * console.log(signedTx)
394
+ * })
395
+ * ```
396
+ */
397
+ async leave (): Promise<{ channelId: string, signedTx: string }> {
398
+ return await new Promise((resolve, reject) => {
399
+ enqueueAction(
400
+ this,
401
+ (channel, state) => state?.handler === handlers.channelOpen,
402
+ (channel, _state) => {
403
+ send(channel, { jsonrpc: '2.0', method: 'channels.leave', params: {} })
404
+ return {
405
+ handler: handlers.awaitingLeave,
406
+ state: { resolve, reject }
407
+ }
408
+ })
409
+ })
410
+ }
411
+
412
+ /**
413
+ * Trigger mutual close
414
+ *
415
+ * At any moment after the channel is opened, a closing procedure can be triggered.
416
+ * This can be done by either of the parties. The process is similar to the off-chain updates.
417
+ *
418
+ * @param sign - Function which verifies and signs mutual close transaction
419
+ * @example
420
+ * ```js
421
+ * channel.shutdown(
422
+ * async (tx) => await account.signTransaction(tx)
423
+ * ).then(tx => console.log('on_chain_tx', tx))
424
+ * ```
425
+ */
426
+ async shutdown (sign: Function): Promise<string> {
427
+ return await new Promise((resolve, reject) => {
428
+ enqueueAction(
429
+ this,
430
+ (_channel, state) => state?.handler === handlers.channelOpen,
431
+ (channel) => {
432
+ send(channel, { jsonrpc: '2.0', method: 'channels.shutdown', params: {} })
433
+ return {
434
+ handler: handlers.awaitingShutdownTx,
435
+ state: {
436
+ sign,
437
+ resolve,
438
+ reject
439
+ }
440
+ }
441
+ }
442
+ )
443
+ })
444
+ }
445
+
446
+ /**
447
+ * Withdraw coins from the channel
448
+ *
449
+ * After the channel had been opened any of the participants can initiate a withdrawal.
450
+ * The process closely resembles the update. The most notable difference is that the
451
+ * transaction has been co-signed: it is channel_withdraw_tx and after the procedure
452
+ * is finished - it is being posted on-chain.
453
+ *
454
+ * Any of the participants can initiate a withdrawal. The only requirements are:
455
+ *
456
+ * - Channel is already opened
457
+ * - No off-chain update/deposit/withdrawal is currently being performed
458
+ * - Channel is not being closed or in a solo closing state
459
+ * - The withdrawal amount must be equal to or greater than zero, and cannot exceed
460
+ * the available balance on the channel (minus the channel_reserve)
461
+ *
462
+ * After the other party had signed the withdraw transaction, the transaction is posted
463
+ * on-chain and onOnChainTx callback is called with on-chain transaction as first argument.
464
+ * After computing transaction hash it can be tracked on the chain: entering the mempool,
465
+ * block inclusion and a number of confirmations.
466
+ *
467
+ * After the minimum_depth block confirmations onOwnWithdrawLocked callback is called
468
+ * (without any arguments).
469
+ *
470
+ * When the other party had confirmed that the block height needed is reached
471
+ * onWithdrawLocked callback is called (without any arguments).
472
+ *
473
+ * @param amount - Amount of coins to withdraw
474
+ * @param sign - Function which verifies and signs withdraw transaction
475
+ * @param callbacks - Callbacks
476
+ * @param callbacks.onOnChainTx - Called when withdraw transaction has been posted
477
+ * on chain
478
+ * @param callbacks.onOwnWithdrawLocked
479
+ * @param callbacks.onWithdrawLocked
480
+ * @example
481
+ * ```js
482
+ * channel.withdraw(
483
+ * 100,
484
+ * async (tx) => await account.signTransaction(tx),
485
+ * { onOnChainTx: (tx) => console.log('on_chain_tx', tx) }
486
+ * ).then(({ accepted, signedTx }) => {
487
+ * if (accepted) {
488
+ * console.log('Withdrawal has been accepted')
489
+ * } else {
490
+ * console.log('Withdrawal has been rejected')
491
+ * }
492
+ * })
493
+ * ```
494
+ */
495
+ async withdraw (
496
+ amount: number | BigNumber,
497
+ sign: SignTx,
498
+ { onOnChainTx, onOwnWithdrawLocked, onWithdrawLocked }:
499
+ { onOnChainTx?: Function, onOwnWithdrawLocked?: Function, onWithdrawLocked?: Function } = {}
500
+ ): Promise<{ accepted: boolean, signedTx: string }> {
501
+ return await new Promise((resolve, reject) => {
502
+ enqueueAction(
503
+ this,
504
+ (_channel, state) => state?.handler === handlers.channelOpen,
505
+ (channel) => {
506
+ send(channel, { jsonrpc: '2.0', method: 'channels.withdraw', params: { amount } })
507
+ return {
508
+ handler: handlers.awaitingWithdrawTx,
509
+ state: {
510
+ sign,
511
+ resolve,
512
+ reject,
513
+ onOnChainTx,
514
+ onOwnWithdrawLocked,
515
+ onWithdrawLocked
516
+ }
517
+ }
518
+ }
519
+ )
520
+ })
521
+ }
522
+
523
+ /**
524
+ * Deposit coins into the channel
525
+ *
526
+ * After the channel had been opened any of the participants can initiate a deposit.
527
+ * The process closely resembles the update. The most notable difference is that the
528
+ * transaction has been co-signed: it is channel_deposit_tx and after the procedure
529
+ * is finished - it is being posted on-chain.
530
+ *
531
+ * Any of the participants can initiate a deposit. The only requirements are:
532
+ *
533
+ * - Channel is already opened
534
+ * - No off-chain update/deposit/withdrawal is currently being performed
535
+ * - Channel is not being closed or in a solo closing state
536
+ * - The deposit amount must be equal to or greater than zero, and cannot exceed
537
+ * the available balance on the channel (minus the channel_reserve)
538
+ *
539
+ * After the other party had signed the deposit transaction, the transaction is posted
540
+ * on-chain and onOnChainTx callback is called with on-chain transaction as first argument.
541
+ * After computing transaction hash it can be tracked on the chain: entering the mempool,
542
+ * block inclusion and a number of confirmations.
543
+ *
544
+ * After the minimum_depth block confirmations onOwnDepositLocked callback is called
545
+ * (without any arguments).
546
+ *
547
+ * When the other party had confirmed that the block height needed is reached
548
+ * onDepositLocked callback is called (without any arguments).
549
+ *
550
+ * @param amount - Amount of coins to deposit
551
+ * @param sign - Function which verifies and signs deposit transaction
552
+ * @param callbacks - Callbacks
553
+ * @param callbacks.onOnChainTx - Called when deposit transaction has been posted
554
+ * on chain
555
+ * @param callbacks.onOwnDepositLocked
556
+ * @param callbacks.onDepositLocked
557
+ * @example
558
+ * ```js
559
+ * channel.deposit(
560
+ * 100,
561
+ * async (tx) => await account.signTransaction(tx),
562
+ * { onOnChainTx: (tx) => console.log('on_chain_tx', tx) }
563
+ * ).then(({ accepted, state }) => {
564
+ * if (accepted) {
565
+ * console.log('Deposit has been accepted')
566
+ * console.log('The new state is:', state)
567
+ * } else {
568
+ * console.log('Deposit has been rejected')
569
+ * }
570
+ * })
571
+ * ```
572
+ */
573
+ async deposit (
574
+ amount: number | BigNumber,
575
+ sign: SignTx,
576
+ { onOnChainTx, onOwnDepositLocked, onDepositLocked }:
577
+ { onOnChainTx?: Function, onOwnDepositLocked?: Function, onDepositLocked?: Function } = {}
578
+ ): Promise<{ accepted: boolean, state: ChannelState }> {
579
+ return await new Promise((resolve, reject) => {
580
+ enqueueAction(
581
+ this,
582
+ (_channel, state) => state?.handler === handlers.channelOpen,
583
+ (channel) => {
584
+ send(channel, { jsonrpc: '2.0', method: 'channels.deposit', params: { amount } })
585
+ return {
586
+ handler: handlers.awaitingDepositTx,
587
+ state: {
588
+ sign,
589
+ resolve,
590
+ reject,
591
+ onOnChainTx,
592
+ onOwnDepositLocked,
593
+ onDepositLocked
594
+ }
595
+ }
596
+ }
597
+ )
598
+ })
599
+ }
600
+
601
+ /**
602
+ * Trigger create contract update
603
+ *
604
+ * The create contract update is creating a contract inside the channel's internal state tree.
605
+ * The update is a change to be applied on top of the latest state.
606
+ *
607
+ * That would create a contract with the poster being the owner of it. Poster commits initially
608
+ * a deposit amount of coins to the new contract.
609
+ *
610
+ * @param options - Options
611
+ * @param options.code - Api encoded compiled AEVM byte code
612
+ * @param options.callData - Api encoded compiled AEVM call data for the code
613
+ * @param options.deposit - Initial amount the owner of the contract commits to it
614
+ * @param options.vmVersion - Version of the Virtual Machine
615
+ * @param options.abiVersion - Version of the Application Binary Interface
616
+ * @param sign - Function which verifies and signs create contract transaction
617
+ * @example
618
+ * ```js
619
+ * channel.createContract({
620
+ * code: 'cb_HKtpipK4aCgYb17wZ...',
621
+ * callData: 'cb_1111111111111111...',
622
+ * deposit: 10,
623
+ * vmVersion: 3,
624
+ * abiVersion: 1
625
+ * }).then(({ accepted, signedTx, address }) => {
626
+ * if (accepted) {
627
+ * console.log('New contract has been created')
628
+ * console.log('Contract address:', address)
629
+ * } else {
630
+ * console.log('New contract has been rejected')
631
+ * }
632
+ * })
633
+ * ```
634
+ */
635
+ async createContract (
636
+ { code, callData, deposit, vmVersion, abiVersion }: {
637
+ code: string
638
+ callData: string
639
+ deposit: number | BigNumber
640
+ vmVersion: number
641
+ abiVersion: number
642
+ },
643
+ sign: SignTx
644
+ ): Promise<{ accepted: boolean, signedTx: string, address: string }> {
645
+ return await new Promise((resolve, reject) => {
646
+ enqueueAction(
647
+ this,
648
+ (_channel, state) => state?.handler === handlers.channelOpen,
649
+ (channel) => {
650
+ send(channel, {
651
+ jsonrpc: '2.0',
652
+ method: 'channels.update.new_contract',
653
+ params: {
654
+ code,
655
+ call_data: callData,
656
+ deposit,
657
+ vm_version: vmVersion,
658
+ abi_version: abiVersion
659
+ }
660
+ })
661
+ return {
662
+ handler: handlers.awaitingNewContractTx,
663
+ state: {
664
+ sign,
665
+ resolve,
666
+ reject
667
+ }
668
+ }
669
+ }
670
+ )
671
+ })
672
+ }
673
+
674
+ /**
675
+ * Trigger call a contract update
676
+ *
677
+ * The call contract update is calling a preexisting contract inside the channel's
678
+ * internal state tree. The update is a change to be applied on top of the latest state.
679
+ *
680
+ * That would call a contract with the poster being the caller of it. Poster commits
681
+ * an amount of coins to the contract.
682
+ *
683
+ * The call would also create a call object inside the channel state tree. It contains
684
+ * the result of the contract call.
685
+ *
686
+ * It is worth mentioning that the gas is not consumed, because this is an off-chain
687
+ * contract call. It would be consumed if it were an on-chain one. This could happen
688
+ * if a call with a similar computation amount is to be forced on-chain.
689
+ *
690
+ * @param options - Options
691
+ * @param options.amount - Amount the caller of the contract commits to it
692
+ * @param options.callData - ABI encoded compiled AEVM call data for the code
693
+ * @param options.contract - Address of the contract to call
694
+ * @param options.abiVersion - Version of the ABI
695
+ * @param sign - Function which verifies and signs contract call transaction
696
+ * @example
697
+ * ```js
698
+ * channel.callContract({
699
+ * contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
700
+ * callData: 'cb_1111111111111111...',
701
+ * amount: 0,
702
+ * abiVersion: 1
703
+ * }).then(({ accepted, signedTx }) => {
704
+ * if (accepted) {
705
+ * console.log('Contract called succesfully')
706
+ * } else {
707
+ * console.log('Contract call has been rejected')
708
+ * }
709
+ * })
710
+ * ```
711
+ */
712
+ async callContract (
713
+ { amount, callData, contract, abiVersion }: CallContractOptions,
714
+ sign: SignTx
715
+ ): Promise<{ accepted: boolean, signedTx: string }> {
716
+ return await new Promise((resolve, reject) => {
717
+ enqueueAction(
718
+ this,
719
+ (_channel, state) => state?.handler === handlers.channelOpen,
720
+ (channel) => {
721
+ send(channel, {
722
+ jsonrpc: '2.0',
723
+ method: 'channels.update.call_contract',
724
+ params: {
725
+ amount,
726
+ call_data: callData,
727
+ contract_id: contract,
728
+ abi_version: abiVersion
729
+ }
730
+ })
731
+ return {
732
+ handler: handlers.awaitingCallContractUpdateTx,
733
+ state: { resolve, reject, sign }
734
+ }
735
+ }
736
+ )
737
+ })
738
+ }
739
+
740
+ /**
741
+ * Trigger a force progress contract call
742
+ * This call is going on-chain
743
+ * @param options - Options
744
+ * @param options.amount - Amount the caller of the contract commits to it
745
+ * @param options.callData - ABI encoded compiled AEVM call data for the code
746
+ * @param options.contract - Address of the contract to call
747
+ * @param options.abiVersion - Version of the ABI
748
+ * @param options.gasPrice=1000000000]
749
+ * @param options.gasLimit=1000000]
750
+ * @param sign - Function which verifies and signs contract force progress transaction
751
+ * @param callbacks - Callbacks
752
+ * @example
753
+ * ```js
754
+ * channel.forceProgress({
755
+ * contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
756
+ * callData: 'cb_1111111111111111...',
757
+ * amount: 0,
758
+ * abiVersion: 1,
759
+ * gasPrice: 1000005554
760
+ * }).then(({ accepted, signedTx }) => {
761
+ * if (accepted) {
762
+ * console.log('Contract force progress call successful')
763
+ * } else {
764
+ * console.log('Contract force progress call has been rejected')
765
+ * }
766
+ * })
767
+ * ```
768
+ */
769
+ async forceProgress (
770
+ { amount, callData, contract, abiVersion, gasLimit = 1000000, gasPrice = 1000000000 }: {
771
+ amount: number
772
+ callData: string
773
+ contract: string
774
+ abiVersion: number
775
+ gasLimit?: number
776
+ gasPrice?: number
777
+ },
778
+ sign: SignTx,
779
+ { onOnChainTx }: { onOnChainTx?: Function } = {}
780
+ ): Promise<{ accepted: boolean, signedTx: string, tx: EncodedData<'tx'> | Uint8Array }> {
781
+ return await new Promise((resolve, reject) => {
782
+ enqueueAction(
783
+ this,
784
+ (_channel, state) => state?.handler === handlers.channelOpen,
785
+ (channel) => {
786
+ send(channel, {
787
+ jsonrpc: '2.0',
788
+ method: 'channels.force_progress',
789
+ params: {
790
+ amount,
791
+ call_data: callData,
792
+ contract_id: contract,
793
+ abi_version: abiVersion,
794
+ gas_price: gasPrice,
795
+ gas: gasLimit
796
+ }
797
+ })
798
+ return {
799
+ handler: handlers.awaitingCallContractForceProgressUpdate,
800
+ state: { resolve, reject, sign, onOnChainTx }
801
+ }
802
+ }
803
+ )
804
+ })
805
+ }
806
+
807
+ /**
808
+ * Call contract using dry-run
809
+ *
810
+ * In order to get the result of a potential contract call, one might need to
811
+ * dry-run a contract call. It takes the exact same arguments as a call would
812
+ * and returns the call object.
813
+ *
814
+ * The call is executed in the channel's state but it does not impact the state
815
+ * whatsoever. It uses as an environment the latest channel's state and the current
816
+ * top of the blockchain as seen by the node.
817
+ *
818
+ * @param options - Options
819
+ * @param options.amount - Amount the caller of the contract commits to it
820
+ * @param options.callData - ABI encoded compiled AEVM call data for the code
821
+ * @param options.contract - Address of the contract to call
822
+ * @param options.abiVersion - Version of the ABI
823
+ * @example
824
+ * ```js
825
+ * channel.callContractStatic({
826
+ * contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
827
+ * callData: 'cb_1111111111111111...',
828
+ * amount: 0,
829
+ * abiVersion: 1
830
+ * }).then(({ returnValue, gasUsed }) => {
831
+ * console.log('Returned value:', returnValue)
832
+ * console.log('Gas used:', gasUsed)
833
+ * })
834
+ * ```
835
+ */
836
+ async callContractStatic (
837
+ { amount, callData, contract, abiVersion }:
838
+ { amount: number, callData: string, contract: string, abiVersion: number }
839
+ ): Promise<CallContractOptions> {
840
+ return await snakeToPascalObjKeys(await call(this, 'channels.dry_run.call_contract', {
841
+ amount,
842
+ call_data: callData,
843
+ contract_id: contract,
844
+ abi_version: abiVersion
845
+ }))
846
+ }
847
+
848
+ /**
849
+ * Get contract call result
850
+ *
851
+ * The combination of a caller, contract and a round of execution determines the
852
+ * contract call. Providing an incorrect set of those results in an error response.
853
+ *
854
+ * @param options - Options
855
+ * @param options.caller - Address of contract caller
856
+ * @param options.contract - Address of the contract
857
+ * @param options.round - Round when contract was called
858
+ * @example
859
+ * ```js
860
+ * channel.getContractCall({
861
+ * caller: 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH',
862
+ * contract: 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa',
863
+ * round: 3
864
+ * }).then(({ returnType, returnValue }) => {
865
+ * if (returnType === 'ok') console.log(returnValue)
866
+ * })
867
+ * ```
868
+ */
869
+ async getContractCall (
870
+ { caller, contract, round }: { caller: string, contract: string, round: number }
871
+ ): Promise<{
872
+ returnType: string
873
+ returnValue: string
874
+ gasPrice: number | BigNumber
875
+ gasUsed: number | BigNumber
876
+ height: number
877
+ log: string
878
+ }> {
879
+ return await snakeToPascalObjKeys(
880
+ await call(this, 'channels.get.contract_call', {
881
+ caller_id: caller,
882
+ contract_id: contract,
883
+ round
884
+ })
885
+ )
886
+ }
887
+
888
+ /**
889
+ * Get contract latest state
890
+ *
891
+ * @param contract - Address of the contract
892
+ * @example
893
+ * ```js
894
+ * channel.getContractState(
895
+ * 'ct_9sRA9AVE4BYTAkh5RNfJYmwQe1NZ4MErasQLXZkFWG43TPBqa'
896
+ * ).then(({ contract }) => {
897
+ * console.log('deposit:', contract.deposit)
898
+ * })
899
+ * ```
900
+ */
901
+ async getContractState (
902
+ contract: string
903
+ ): Promise<{ contract: Contract, contractState: object }> {
904
+ const result = await call(this, 'channels.get.contract', { pubkey: contract })
905
+ return await snakeToPascalObjKeys({
906
+ ...result,
907
+ contract: snakeToPascalObjKeys(result.contract)
908
+ })
909
+ }
910
+
911
+ /**
912
+ * Clean up all locally stored contract calls
913
+ *
914
+ * Contract calls are kept locally in order for the participant to be able to look them up.
915
+ * They consume memory and in order for the participant to free it - one can prune all messages.
916
+ * This cleans up all locally stored contract calls and those will no longer be available for
917
+ * fetching and inspection.
918
+ */
919
+ async cleanContractCalls (): Promise<void> {
920
+ return await new Promise((resolve, reject) => {
921
+ enqueueAction(
922
+ this,
923
+ (_channel, state) => state?.handler === handlers.channelOpen,
924
+ (channel) => {
925
+ send(channel, {
926
+ jsonrpc: '2.0',
927
+ method: 'channels.clean_contract_calls',
928
+ params: {}
929
+ })
930
+ return {
931
+ handler: handlers.awaitingCallsPruned,
932
+ state: { resolve, reject }
933
+ }
934
+ }
935
+ )
936
+ })
937
+ }
938
+
939
+ /**
940
+ * Send generic message
941
+ *
942
+ * If message is an object it will be serialized into JSON string
943
+ * before sending.
944
+ *
945
+ * If there is ongoing update that has not yet been finished the message
946
+ * will be sent after that update is finalized.
947
+ *
948
+ * @param message - Message
949
+ * @param recipient - Address of the recipient
950
+ * @example
951
+ * ```js
952
+ * channel.sendMessage(
953
+ * 'hello world',
954
+ * 'ak_Y1NRjHuoc3CGMYMvCmdHSBpJsMDR6Ra2t5zjhRcbtMeXXLpLH'
955
+ * )
956
+ * ```
957
+ */
958
+ sendMessage (message: string | ChannelMessage, recipient: string): void {
959
+ let info = message
960
+ if (typeof message === 'object') {
961
+ info = JSON.stringify(message)
962
+ }
963
+ const doSend = (channel: Channel): void => send(channel, {
964
+ jsonrpc: '2.0',
965
+ method: 'channels.message',
966
+ params: { info, to: recipient }
967
+ })
968
+ if (this.status() === 'connecting') {
969
+ const onStatusChanged = (status: string): void => {
970
+ if (status !== 'connecting') {
971
+ // For some reason we can't immediately send a message when connection is
972
+ // established. Thus we wait 500ms which seems to work.
973
+ setTimeout(() => doSend(this), 500)
974
+ this.off('statusChanged', onStatusChanged)
975
+ }
976
+ }
977
+ this.on('statusChanged', onStatusChanged)
978
+ } else {
979
+ doSend(this)
980
+ }
981
+ }
982
+
983
+ static async reconnect (options: ChannelOptions, txParams: any): Promise<Channel> {
984
+ const { sign } = options
985
+
986
+ return await Channel.initialize({
987
+ ...options,
988
+ reconnectTx: await sign('reconnect', buildTx(txParams, TX_TYPE.channelReconnect).tx)
989
+ })
990
+ }
991
+ }