@aeternity/aepp-sdk 11.0.1 → 12.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (376) hide show
  1. package/LICENSE +1 -1
  2. package/dist/aepp-sdk.browser-script.js +1 -1
  3. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +12 -19
  4. package/dist/aepp-sdk.browser-script.js.map +1 -1
  5. package/dist/aepp-sdk.browser.js +1 -1
  6. package/dist/aepp-sdk.browser.js.map +1 -1
  7. package/dist/aepp-sdk.js +1 -1
  8. package/dist/aepp-sdk.js.map +1 -1
  9. package/es/AeSdk.d.ts +37 -0
  10. package/es/AeSdk.mjs +81 -0
  11. package/es/AeSdk.mjs.map +1 -0
  12. package/es/AeSdkAepp.d.ts +69 -0
  13. package/es/AeSdkAepp.mjs +175 -0
  14. package/es/AeSdkAepp.mjs.map +1 -0
  15. package/es/AeSdkBase.d.ts +212 -0
  16. package/es/AeSdkBase.mjs +310 -0
  17. package/es/AeSdkBase.mjs.map +1 -0
  18. package/es/AeSdkWallet.d.ts +103 -0
  19. package/es/AeSdkWallet.mjs +322 -0
  20. package/es/AeSdkWallet.mjs.map +1 -0
  21. package/es/Node.d.ts +44 -0
  22. package/es/Node.mjs +141 -0
  23. package/es/Node.mjs.map +1 -0
  24. package/es/account/Base.d.ts +65 -0
  25. package/es/account/Base.mjs +123 -0
  26. package/es/account/Base.mjs.map +1 -0
  27. package/es/account/Memory.d.ts +25 -0
  28. package/es/account/Memory.mjs +81 -0
  29. package/es/account/Memory.mjs.map +1 -0
  30. package/es/account/Rpc.d.ts +27 -0
  31. package/es/account/Rpc.mjs +79 -0
  32. package/es/account/Rpc.mjs.map +1 -0
  33. package/es/aens.d.ts +209 -0
  34. package/es/aens.mjs +332 -0
  35. package/es/aens.mjs.map +1 -0
  36. package/es/aepp-wallet-communication/connection/Browser.d.ts +32 -0
  37. package/es/aepp-wallet-communication/connection/Browser.mjs +70 -0
  38. package/es/aepp-wallet-communication/connection/Browser.mjs.map +1 -0
  39. package/es/aepp-wallet-communication/connection/BrowserRuntime.d.ts +17 -0
  40. package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs +57 -0
  41. package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs.map +1 -0
  42. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.d.ts +35 -0
  43. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs +142 -0
  44. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs.map +1 -0
  45. package/es/aepp-wallet-communication/connection-proxy.d.ts +10 -0
  46. package/{src/utils/crypto-ts.ts → es/aepp-wallet-communication/connection-proxy.mjs} +15 -11
  47. package/es/aepp-wallet-communication/connection-proxy.mjs.map +1 -0
  48. package/es/aepp-wallet-communication/rpc/RpcClient.d.ts +33 -0
  49. package/es/aepp-wallet-communication/rpc/RpcClient.mjs +169 -0
  50. package/es/aepp-wallet-communication/rpc/RpcClient.mjs.map +1 -0
  51. package/es/aepp-wallet-communication/rpc/types.d.ts +77 -0
  52. package/es/aepp-wallet-communication/rpc/types.mjs +3 -0
  53. package/es/aepp-wallet-communication/rpc/types.mjs.map +1 -0
  54. package/es/aepp-wallet-communication/schema.d.ts +90 -46
  55. package/es/aepp-wallet-communication/schema.mjs +214 -0
  56. package/es/aepp-wallet-communication/schema.mjs.map +1 -0
  57. package/es/aepp-wallet-communication/wallet-detector.d.ts +24 -0
  58. package/es/aepp-wallet-communication/wallet-detector.mjs +63 -0
  59. package/es/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
  60. package/es/apis/compiler/compiler.d.ts +92 -0
  61. package/es/apis/compiler/compiler.mjs +576 -0
  62. package/es/apis/compiler/compiler.mjs.map +1 -0
  63. package/es/apis/compiler/index.d.ts +2 -0
  64. package/es/apis/compiler/index.mjs +3 -0
  65. package/es/apis/compiler/index.mjs.map +1 -0
  66. package/es/apis/compiler/models/index.d.ts +248 -0
  67. package/es/apis/compiler/models/index.mjs +48 -0
  68. package/es/apis/compiler/models/index.mjs.map +1 -0
  69. package/es/apis/compiler/models/mappers.d.ts +23 -0
  70. package/es/apis/compiler/models/mappers.mjs +585 -0
  71. package/es/apis/compiler/models/mappers.mjs.map +1 -0
  72. package/es/apis/compiler/models/parameters.d.ts +13 -0
  73. package/es/apis/compiler/models/parameters.mjs +71 -0
  74. package/es/apis/compiler/models/parameters.mjs.map +1 -0
  75. package/es/apis/node/index.d.ts +2 -0
  76. package/es/apis/node/index.mjs +3 -0
  77. package/es/apis/node/index.mjs.map +1 -0
  78. package/es/apis/node/models/index.d.ts +1061 -0
  79. package/es/apis/node/models/index.mjs +2 -0
  80. package/es/apis/node/models/index.mjs.map +1 -0
  81. package/es/apis/node/models/mappers.d.ts +95 -0
  82. package/es/apis/node/models/mappers.mjs +4164 -0
  83. package/es/apis/node/models/mappers.mjs.map +1 -0
  84. package/es/apis/node/models/parameters.d.ts +17 -0
  85. package/es/apis/node/models/parameters.mjs +165 -0
  86. package/es/apis/node/models/parameters.mjs.map +1 -0
  87. package/es/apis/node/node.d.ts +215 -0
  88. package/es/apis/node/node.mjs +1165 -0
  89. package/es/apis/node/node.mjs.map +1 -0
  90. package/es/chain.d.ts +220 -0
  91. package/es/chain.mjs +524 -0
  92. package/es/chain.mjs.map +1 -0
  93. package/es/channel/handlers.d.ts +34 -0
  94. package/es/channel/handlers.mjs +147 -112
  95. package/es/channel/handlers.mjs.map +1 -1
  96. package/es/channel/index.d.ts +634 -0
  97. package/es/channel/index.mjs +898 -900
  98. package/es/channel/index.mjs.map +1 -1
  99. package/es/channel/internal.d.ts +100 -0
  100. package/es/channel/internal.mjs +97 -51
  101. package/es/channel/internal.mjs.map +1 -1
  102. package/es/contract/Compiler.d.ts +17 -0
  103. package/es/contract/Compiler.mjs +67 -0
  104. package/es/contract/Compiler.mjs.map +1 -0
  105. package/es/contract/aci.d.ts +125 -0
  106. package/es/contract/aci.mjs +199 -180
  107. package/es/contract/aci.mjs.map +1 -1
  108. package/es/contract/ga.d.ts +68 -0
  109. package/es/contract/ga.mjs +190 -0
  110. package/es/contract/ga.mjs.map +1 -0
  111. package/es/contract/methods.d.ts +68 -0
  112. package/es/contract/methods.mjs +101 -0
  113. package/es/contract/methods.mjs.map +1 -0
  114. package/es/index.d.ts +32 -0
  115. package/es/index.mjs +32 -40
  116. package/es/index.mjs.map +1 -1
  117. package/es/oracle.d.ts +154 -0
  118. package/es/oracle.mjs +288 -0
  119. package/es/oracle.mjs.map +1 -0
  120. package/es/spend.d.ts +61 -0
  121. package/es/spend.mjs +122 -0
  122. package/es/spend.mjs.map +1 -0
  123. package/es/tx/builder/constants.d.ts +41 -0
  124. package/es/tx/builder/constants.mjs +11 -19
  125. package/es/tx/builder/constants.mjs.map +1 -1
  126. package/es/tx/builder/field-types.d.ts +62 -0
  127. package/es/tx/builder/field-types.mjs +62 -10
  128. package/es/tx/builder/field-types.mjs.map +1 -1
  129. package/es/tx/builder/helpers.d.ts +131 -0
  130. package/es/tx/builder/helpers.mjs +71 -169
  131. package/es/tx/builder/helpers.mjs.map +1 -1
  132. package/es/tx/builder/index.d.ts +75 -0
  133. package/es/tx/builder/index.mjs +141 -323
  134. package/es/tx/builder/index.mjs.map +1 -1
  135. package/es/tx/builder/schema.d.ts +350 -0
  136. package/es/tx/builder/schema.mjs +173 -510
  137. package/es/tx/builder/schema.mjs.map +1 -1
  138. package/es/tx/index.d.ts +63 -0
  139. package/es/tx/index.mjs +171 -265
  140. package/es/tx/index.mjs.map +1 -1
  141. package/es/tx/min-fee.d.ts +20 -0
  142. package/es/tx/min-fee.mjs +148 -0
  143. package/es/tx/min-fee.mjs.map +1 -0
  144. package/es/tx/validator.d.ts +19 -0
  145. package/es/tx/validator.mjs +64 -71
  146. package/es/tx/validator.mjs.map +1 -1
  147. package/es/typings/@aeternity__argon2-browser/index.d.mjs +2 -0
  148. package/es/typings/@aeternity__argon2-browser/index.d.mjs.map +1 -0
  149. package/es/typings/@aeternity__uuid/index.d.mjs +2 -0
  150. package/es/typings/@aeternity__uuid/index.d.mjs.map +1 -0
  151. package/es/typings/bip32-path/index.d.mjs +2 -0
  152. package/es/typings/bip32-path/index.d.mjs.map +1 -0
  153. package/es/typings/blakejs/index.d.mjs.map +1 -1
  154. package/es/typings/tweetnacl-auth/index.d.mjs +2 -0
  155. package/es/typings/tweetnacl-auth/index.d.mjs.map +1 -0
  156. package/es/utils/amount-formatter.d.ts +42 -0
  157. package/es/utils/amount-formatter.mjs +28 -41
  158. package/es/utils/amount-formatter.mjs.map +1 -1
  159. package/es/utils/autorest.d.ts +6 -0
  160. package/es/utils/autorest.mjs +97 -0
  161. package/es/utils/autorest.mjs.map +1 -0
  162. package/es/utils/bignumber.d.ts +13 -0
  163. package/es/utils/bignumber.mjs +10 -8
  164. package/es/utils/bignumber.mjs.map +1 -1
  165. package/es/{bytes.d.ts → utils/bytes.d.ts} +10 -13
  166. package/es/utils/bytes.mjs +10 -14
  167. package/es/utils/bytes.mjs.map +1 -1
  168. package/es/utils/crypto.d.ts +103 -0
  169. package/es/utils/crypto.mjs +60 -68
  170. package/es/utils/crypto.mjs.map +1 -1
  171. package/es/utils/encoder.d.ts +26 -0
  172. package/es/utils/encoder.mjs +47 -51
  173. package/es/utils/encoder.mjs.map +1 -1
  174. package/es/{errors.d.ts → utils/errors.d.ts} +254 -57
  175. package/es/utils/errors.mjs +318 -125
  176. package/es/utils/errors.mjs.map +1 -1
  177. package/es/utils/hd-wallet.d.ts +34 -0
  178. package/es/utils/hd-wallet.mjs +27 -16
  179. package/es/utils/hd-wallet.mjs.map +1 -1
  180. package/es/utils/json-big.d.ts +5 -0
  181. package/es/utils/json-big.mjs +10 -14
  182. package/es/utils/json-big.mjs.map +1 -1
  183. package/es/utils/keystore.d.ts +51 -0
  184. package/es/utils/keystore.mjs +98 -193
  185. package/es/utils/keystore.mjs.map +1 -1
  186. package/es/{mptree.d.ts → utils/mptree.d.ts} +5 -8
  187. package/es/utils/mptree.mjs +13 -14
  188. package/es/utils/mptree.mjs.map +1 -1
  189. package/es/utils/other.d.ts +14 -0
  190. package/es/utils/other.mjs +21 -36
  191. package/es/utils/other.mjs.map +1 -1
  192. package/es/{semver-satisfies.d.ts → utils/semver-satisfies.d.ts} +0 -0
  193. package/es/utils/semver-satisfies.mjs.map +1 -1
  194. package/es/utils/string.d.ts +24 -0
  195. package/es/utils/string.mjs +8 -10
  196. package/es/utils/string.mjs.map +1 -1
  197. package/package.json +49 -39
  198. package/src/AeSdk.ts +65 -0
  199. package/src/AeSdkAepp.ts +167 -0
  200. package/src/AeSdkBase.ts +320 -0
  201. package/src/AeSdkWallet.ts +332 -0
  202. package/src/Node.ts +167 -0
  203. package/src/account/Base.ts +114 -0
  204. package/src/account/Memory.ts +83 -0
  205. package/src/account/Rpc.ts +67 -0
  206. package/src/aens.ts +391 -0
  207. package/src/aepp-wallet-communication/connection/Browser.ts +68 -0
  208. package/src/aepp-wallet-communication/connection/BrowserRuntime.ts +58 -0
  209. package/src/aepp-wallet-communication/connection/BrowserWindowMessage.ts +111 -0
  210. package/{es/utils/crypto-ts.mjs → src/aepp-wallet-communication/connection-proxy.ts} +16 -17
  211. package/src/aepp-wallet-communication/rpc/RpcClient.ts +137 -0
  212. package/src/aepp-wallet-communication/rpc/types.ts +66 -0
  213. package/src/aepp-wallet-communication/schema.ts +181 -0
  214. package/src/aepp-wallet-communication/wallet-detector.ts +71 -0
  215. package/src/apis/compiler/code-model-v4.yaml +2649 -0
  216. package/src/apis/compiler/compiler.ts +585 -0
  217. package/src/apis/compiler/index.ts +2 -0
  218. package/src/apis/compiler/models/index.ts +301 -0
  219. package/src/apis/compiler/models/mappers.ts +583 -0
  220. package/src/apis/compiler/models/parameters.ts +93 -0
  221. package/src/apis/node/code-model-v4.yaml +10046 -0
  222. package/src/apis/node/index.ts +2 -0
  223. package/src/apis/node/models/index.ts +1253 -0
  224. package/src/apis/node/models/mappers.ts +4278 -0
  225. package/src/apis/node/models/parameters.ts +188 -0
  226. package/src/apis/node/node.ts +1299 -0
  227. package/src/chain.ts +444 -0
  228. package/src/channel/{handlers.js → handlers.ts} +220 -128
  229. package/src/channel/index.ts +991 -0
  230. package/src/channel/internal.ts +345 -0
  231. package/src/contract/Compiler.ts +66 -0
  232. package/src/contract/aci.ts +529 -0
  233. package/src/contract/ga.ts +178 -0
  234. package/src/contract/methods.ts +118 -0
  235. package/src/index.ts +51 -0
  236. package/src/oracle.ts +342 -0
  237. package/src/spend.ts +152 -0
  238. package/src/tx/builder/{constants.js → constants.ts} +12 -16
  239. package/src/tx/builder/field-types.ts +111 -0
  240. package/src/tx/builder/helpers.ts +253 -0
  241. package/src/tx/builder/index.ts +394 -0
  242. package/src/tx/builder/schema.ts +771 -0
  243. package/src/tx/index.ts +199 -0
  244. package/src/tx/min-fee.ts +136 -0
  245. package/src/tx/validator.ts +226 -0
  246. package/src/typings/@aeternity__argon2-browser/index.d.ts +3 -0
  247. package/src/typings/@aeternity__uuid/index.d.ts +3 -0
  248. package/src/typings/bip32-path/index.d.ts +33 -0
  249. package/src/typings/tweetnacl-auth/index.d.ts +14 -0
  250. package/src/utils/amount-formatter.ts +29 -42
  251. package/src/utils/autorest.ts +90 -0
  252. package/src/utils/bignumber.ts +9 -11
  253. package/src/utils/bytes.ts +12 -14
  254. package/src/utils/crypto.ts +217 -0
  255. package/src/utils/encoder.ts +52 -55
  256. package/src/utils/errors.ts +268 -134
  257. package/src/utils/hd-wallet.ts +134 -0
  258. package/src/utils/json-big.ts +26 -0
  259. package/src/utils/keystore.ts +179 -0
  260. package/src/utils/mptree.ts +14 -14
  261. package/src/utils/other.ts +28 -43
  262. package/src/utils/string.ts +8 -10
  263. package/es/account/base.mjs +0 -179
  264. package/es/account/base.mjs.map +0 -1
  265. package/es/account/memory.mjs +0 -99
  266. package/es/account/memory.mjs.map +0 -1
  267. package/es/account/multiple.mjs +0 -179
  268. package/es/account/multiple.mjs.map +0 -1
  269. package/es/ae/aens.mjs +0 -404
  270. package/es/ae/aens.mjs.map +0 -1
  271. package/es/ae/aepp.mjs +0 -45
  272. package/es/ae/aepp.mjs.map +0 -1
  273. package/es/ae/contract.mjs +0 -159
  274. package/es/ae/contract.mjs.map +0 -1
  275. package/es/ae/index.mjs +0 -207
  276. package/es/ae/index.mjs.map +0 -1
  277. package/es/ae/oracle.mjs +0 -339
  278. package/es/ae/oracle.mjs.map +0 -1
  279. package/es/ae/universal.mjs +0 -45
  280. package/es/ae/universal.mjs.map +0 -1
  281. package/es/ae/wallet.mjs +0 -50
  282. package/es/ae/wallet.mjs.map +0 -1
  283. package/es/amount-formatter.d.ts +0 -58
  284. package/es/bignumber.d.ts +0 -18
  285. package/es/chain/index.mjs +0 -272
  286. package/es/chain/index.mjs.map +0 -1
  287. package/es/chain/node.mjs +0 -442
  288. package/es/chain/node.mjs.map +0 -1
  289. package/es/contract/compiler.mjs +0 -115
  290. package/es/contract/compiler.mjs.map +0 -1
  291. package/es/contract/ga/helpers.mjs +0 -30
  292. package/es/contract/ga/helpers.mjs.map +0 -1
  293. package/es/contract/ga/index.mjs +0 -178
  294. package/es/contract/ga/index.mjs.map +0 -1
  295. package/es/crypto-ts.d.ts +0 -9
  296. package/es/encoder.d.ts +0 -26
  297. package/es/node-pool/index.mjs +0 -188
  298. package/es/node-pool/index.mjs.map +0 -1
  299. package/es/node.mjs +0 -133
  300. package/es/node.mjs.map +0 -1
  301. package/es/other.d.ts +0 -31
  302. package/es/string.d.ts +0 -26
  303. package/es/tx/tx-object.mjs +0 -283
  304. package/es/tx/tx-object.mjs.map +0 -1
  305. package/es/tx/tx.mjs +0 -938
  306. package/es/tx/tx.mjs.map +0 -1
  307. package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs +0 -132
  308. package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +0 -1
  309. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs +0 -183
  310. package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs.map +0 -1
  311. package/es/utils/aepp-wallet-communication/connection/index.mjs +0 -87
  312. package/es/utils/aepp-wallet-communication/connection/index.mjs.map +0 -1
  313. package/es/utils/aepp-wallet-communication/content-script-bridge.mjs +0 -98
  314. package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +0 -1
  315. package/es/utils/aepp-wallet-communication/helpers.mjs +0 -92
  316. package/es/utils/aepp-wallet-communication/helpers.mjs.map +0 -1
  317. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs +0 -375
  318. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +0 -1
  319. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs +0 -275
  320. package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +0 -1
  321. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs +0 -541
  322. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs.map +0 -1
  323. package/es/utils/aepp-wallet-communication/schema.mjs +0 -102
  324. package/es/utils/aepp-wallet-communication/schema.mjs.map +0 -1
  325. package/es/utils/aepp-wallet-communication/wallet-detector.mjs +0 -110
  326. package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +0 -1
  327. package/es/utils/async-init.mjs +0 -64
  328. package/es/utils/async-init.mjs.map +0 -1
  329. package/es/utils/crypto-ts.mjs.map +0 -1
  330. package/es/utils/swagger.mjs +0 -164
  331. package/es/utils/swagger.mjs.map +0 -1
  332. package/src/account/base.js +0 -153
  333. package/src/account/memory.js +0 -83
  334. package/src/account/multiple.js +0 -162
  335. package/src/ae/aens.js +0 -361
  336. package/src/ae/aepp.js +0 -45
  337. package/src/ae/contract.js +0 -148
  338. package/src/ae/index.js +0 -168
  339. package/src/ae/oracle.js +0 -311
  340. package/src/ae/universal.js +0 -47
  341. package/src/ae/wallet.js +0 -50
  342. package/src/chain/index.js +0 -269
  343. package/src/chain/node.js +0 -320
  344. package/src/channel/index.js +0 -903
  345. package/src/channel/internal.js +0 -236
  346. package/src/contract/aci.js +0 -376
  347. package/src/contract/compiler.js +0 -100
  348. package/src/contract/ga/helpers.js +0 -19
  349. package/src/contract/ga/index.js +0 -150
  350. package/src/index.js +0 -89
  351. package/src/node-pool/index.js +0 -172
  352. package/src/node.js +0 -116
  353. package/src/tx/builder/field-types.js +0 -61
  354. package/src/tx/builder/helpers.js +0 -334
  355. package/src/tx/builder/index.js +0 -465
  356. package/src/tx/builder/schema.js +0 -1100
  357. package/src/tx/index.js +0 -301
  358. package/src/tx/tx-object.js +0 -187
  359. package/src/tx/tx.js +0 -631
  360. package/src/tx/validator.js +0 -181
  361. package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +0 -119
  362. package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +0 -165
  363. package/src/utils/aepp-wallet-communication/connection/index.js +0 -85
  364. package/src/utils/aepp-wallet-communication/content-script-bridge.js +0 -86
  365. package/src/utils/aepp-wallet-communication/helpers.js +0 -60
  366. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +0 -294
  367. package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +0 -232
  368. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +0 -401
  369. package/src/utils/aepp-wallet-communication/schema.ts +0 -80
  370. package/src/utils/aepp-wallet-communication/wallet-detector.js +0 -92
  371. package/src/utils/async-init.js +0 -45
  372. package/src/utils/crypto.js +0 -219
  373. package/src/utils/hd-wallet.js +0 -100
  374. package/src/utils/json-big.js +0 -26
  375. package/src/utils/keystore.js +0 -219
  376. package/src/utils/swagger.js +0 -156
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utils/crypto.js"],"names":["nacl","aesjs","str2buf","encode","decode","sha256hash","hash","NotImplementedError","Ecb","ModeOfOperation","ecb","getAddressFromPriv","secret","keys","sign","keyPair","fromSecretKey","publicBuffer","from","publicKey","isAddressValid","address","prefix","e","salt","Math","floor","random","Number","MAX_SAFE_INTEGER","encodeUnsigned","value","binary","allocUnsafe","writeUInt32BE","slice","findIndex","i","encodeContractAddress","owner","nonce","concat","generateKeyPairFromSecret","generateKeyPair","raw","secretBuffer","secretKey","toString","encryptKey","password","binaryData","hashedPasswordBytes","aesEcb","encrypt","decryptKey","encrypted","encryptedBytes","decrypt","data","privateKey","detached","verify","str","signature","Uint8Array","messageToHash","message","p","msg","length","signMessage","verifyMessage","isValidKeypair"],"mappings":";;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AAEA,OAAOA,IAAP,MAAiB,WAAjB;AACA,OAAOC,KAAP,MAAkB,QAAlB;SAESC,O;SACAC,M,EAAQC,M,EAAQC,U;SAChBC,I;SACAC,mB;;AAGT,SAASF,UAAT;AAEA,MAAMG,GAAG,GAAGP,KAAK,CAACQ,eAAN,CAAsBC,GAAlC;AAEA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,kBAAT,CAA6BC,MAA7B,EAAqC;AAC1C,QAAMC,IAAI,GAAGb,IAAI,CAACc,IAAL,CAAUC,OAAV,CAAkBC,aAAlB,CAAgCd,OAAO,CAACU,MAAD,CAAvC,CAAb;;AACA,QAAMK,YAAY,GAAG,QAAOC,IAAP,CAAYL,IAAI,CAACM,SAAjB,CAArB;;AACA,SAAOhB,MAAM,CAACc,YAAD,EAAe,IAAf,CAAb;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASG,cAAT,CAAyBC,OAAzB,EAAiD;AAAA,MAAfC,MAAe,uEAAN,IAAM;;AACtD,MAAI;AACFlB,IAAAA,MAAM,CAACiB,OAAD,EAAUC,MAAV,CAAN;AACA,WAAO,IAAP;AACD,GAHD,CAGE,OAAOC,CAAP,EAAU;AACV,WAAO,KAAP;AACD;AACF;AAED;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,IAAT,GAAiB;AACtB,SAAOC,IAAI,CAACC,KAAL,CAAWD,IAAI,CAACE,MAAL,KAAgBF,IAAI,CAACC,KAAL,CAAWE,MAAM,CAACC,gBA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K,OAAD,EAAUH,SAAV,EAAqB5C,SAArB,CAAb;AACD","sourcesContent":["/*\n * ISC License (ISC)\n * Copyright 2018 aeternity developers\n *\n * Permission to use, copy, modify, and/or distribute this software for any\n * purpose with or without fee is hereby granted, provided that the above\n * copyright notice and this permission notice appear in all copies.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\n * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\n * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\n * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\n * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\n * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\n * PERFORMANCE OF THIS SOFTWARE.\n */\n\n/**\n * Crypto module\n * @module @aeternity/aepp-sdk/es/utils/crypto\n * @example import { Crypto } from '@aeternity/aepp-sdk'\n */\n\nimport nacl from 'tweetnacl'\nimport aesjs from 'aes-js'\n\nimport { str2buf } from './bytes'\nimport { encode, decode, sha256hash } from './encoder'\nimport { hash } from './crypto-ts'\nimport { NotImplementedError } from './errors'\n\nexport * from './crypto-ts'\nexport { sha256hash }\n\nconst Ecb = aesjs.ModeOfOperation.ecb\n\n/**\n * Generate address from secret key\n * @rtype (secret: String) => tx: Promise[String]\n * @param {String} secret - Private key\n * @return {String} Public key\n */\nexport function getAddressFromPriv (secret) {\n const keys = nacl.sign.keyPair.fromSecretKey(str2buf(secret))\n const publicBuffer = Buffer.from(keys.publicKey)\n return encode(publicBuffer, 'ak')\n}\n\n/**\n * Check if address is valid\n * @rtype (input: String) => valid: Boolean\n * @param {String} address - Address\n * @param {String} prefix Transaction prefix. Default: 'ak'\n * @return {Boolean} valid\n */\nexport function isAddressValid (address, prefix = 'ak') {\n try {\n decode(address, prefix)\n return true\n } catch (e) {\n return false\n }\n}\n\n/**\n * Generate a random salt (positive integer)\n * @rtype () => salt: Number\n * @return {Number} random salt\n */\nexport function salt () {\n return Math.floor(Math.random() * Math.floor(Number.MAX_SAFE_INTEGER))\n}\n\n/**\n * Converts a positive integer to the smallest possible\n * representation in a binary digit representation\n * @rtype (value: Number) => Buffer\n * @param {Number} value - Value to encode\n * @return {Buffer} - Encoded data\n */\nexport function encodeUnsigned (value) {\n const binary = Buffer.allocUnsafe(4)\n binary.writeUInt32BE(value)\n return binary.slice(binary.findIndex(i => i !== 0))\n}\n\n// Todo Duplicated in tx builder. remove\n/**\n * Compute contract address\n * @rtype (owner: String, nonce: Number) => String\n * @param {String} owner - Address of contract owner\n * @param {Number} nonce - Round when contract was created\n * @return {String} - Contract address\n */\nexport function encodeContractAddress (owner, nonce) {\n const publicKey = decode(owner, 'ak')\n const binary = Buffer.concat([publicKey, encodeUnsigned(nonce)])\n return encode(hash(binary), 'ct')\n}\n\n// KEY-PAIR HELPERS\n\n/**\n * Generate keyPair from secret key\n * @rtype (secret: Uint8Array) => KeyPair\n * @param {Uint8Array} secret - secret key\n * @return {Object} - Object with Private(privateKey) and Public(publicKey) keys\n */\nexport function generateKeyPairFromSecret (secret) {\n return nacl.sign.keyPair.fromSecretKey(secret)\n}\n\n/**\n * Generate a random ED25519 keypair\n * @rtype (raw: Boolean) => {publicKey: String | Buffer, secretKey: String | Buffer}\n * @param {Boolean} raw - Whether to return raw (binary) keys\n * @return {Object} Key pair\n */\nexport function generateKeyPair (raw = false) {\n // <node>/apps/aens/test/aens_test_utils.erl\n const keyPair = nacl.sign.keyPair()\n\n const publicBuffer = Buffer.from(keyPair.publicKey)\n const secretBuffer = Buffer.from(keyPair.secretKey)\n\n if (raw) {\n return {\n publicKey: publicBuffer,\n secretKey: secretBuffer\n }\n } else {\n return {\n publicKey: encode(publicBuffer, 'ak'),\n secretKey: secretBuffer.toString('hex')\n }\n }\n}\n\n/**\n * Encrypt given data using `password`\n * @rtype (password: String, binaryData: Buffer) => Uint8Array\n * @param {String} password - Password to encrypt with\n * @param {Buffer} binaryData - Data to encrypt\n * @return {Uint8Array} Encrypted data\n */\nexport function encryptKey (password, binaryData) {\n const hashedPasswordBytes = sha256hash(password)\n const aesEcb = new Ecb(hashedPasswordBytes)\n return aesEcb.encrypt(binaryData)\n}\n\n/**\n * Decrypt given data using `password`\n * @rtype (password: String, encrypted: String) => Uint8Array\n * @param {String} password - Password to decrypt with\n * @param {String} encrypted - Data to decrypt\n * @return {Buffer} Decrypted data\n */\nexport function decryptKey (password, encrypted) {\n const encryptedBytes = Buffer.from(encrypted)\n const hashedPasswordBytes = sha256hash(password)\n const aesEcb = new Ecb(hashedPasswordBytes)\n return Buffer.from(aesEcb.decrypt(encryptedBytes))\n}\n\n// SIGNATURES\n\n/**\n * Generate signature\n * @rtype (data: String|Buffer, privateKey: Buffer) => Buffer\n * @param {String|Buffer} data - Data to sign\n * @param {String|Buffer} privateKey - Key to sign with\n * @return {Buffer|Uint8Array} Signature\n */\nexport function sign (data, privateKey) {\n return nacl.sign.detached(Buffer.from(data), Buffer.from(privateKey))\n}\n\n/**\n * Verify that signature was signed by public key\n * @rtype (str: String, signature: Buffer, publicKey: Buffer) => Boolean\n * @param {String|Buffer} str - Data to verify\n * @param {Buffer} signature - Signature to verify\n * @param {Buffer} publicKey - Key to verify against\n * @return {Boolean} Valid?\n */\nexport function verify (str, signature, publicKey) {\n return nacl.sign.detached.verify(new Uint8Array(str), signature, publicKey)\n}\n\nexport function messageToHash (message) {\n const p = Buffer.from('aeternity Signed Message:\\n', 'utf8')\n const msg = Buffer.from(message, 'utf8')\n if (msg.length >= 0xFD) throw new NotImplementedError('Message too long')\n return hash(Buffer.concat([Buffer.from([p.length]), p, Buffer.from([msg.length]), msg]))\n}\n\nexport function signMessage (message, privateKey) {\n return sign(messageToHash(message), privateKey)\n}\n\nexport function verifyMessage (str, signature, publicKey) {\n return verify(messageToHash(str), signature, publicKey)\n}\n\n/**\n * Check key pair for validity\n *\n * Sign a message, and then verifying that signature\n * @rtype (privateKey: Buffer, publicKey: Buffer) => Boolean\n * @param {Buffer} privateKey - Private key to verify\n * @param {Buffer} publicKey - Public key to verify\n * @return {Boolean} Valid?\n */\nexport function isValidKeypair (privateKey, publicKey) {\n const message = Buffer.from('TheMessage')\n const signature = sign(message, privateKey)\n return verify(message, signature, publicKey)\n}\n"],"file":"crypto.mjs"}
1
+ {"version":3,"file":"crypto.mjs","names":["nacl","aesjs","str2buf","concatBuffers","encode","decode","sha256hash","Ecb","ModeOfOperation","ecb","getAddressFromPriv","secret","secretBuffer","keys","sign","keyPair","fromSecretKey","publicKey","isAddressValid","address","prefix","e","salt","Math","floor","random","Number","MAX_SAFE_INTEGER","encodeUnsigned","value","binary","allocUnsafe","writeUInt32BE","slice","findIndex","i","hash","input","from","blake2b","undefined","encodeContractAddress","owner","nonce","generateKeyPairFromSecret","generateKeyPair","raw","publicBuffer","secretKey","toString","encryptKey","password","binaryData","hashedPasswordBytes","aesEcb","encrypt","decryptKey","encrypted","encryptedBytes","decrypt","data","privateKey","detached","verify","signature","publicKeyBuffer","messageToHash","message","p","msg","varuintEncode","length","signMessage","verifyMessage","str","isValidKeypair"],"sources":["../../src/utils/crypto.ts"],"sourcesContent":["/*\n * ISC License (ISC)\n * Copyright 2018 aeternity developers\n *\n * Permission to use, copy, modify, and/or distribute this software for any\n * purpose with or without fee is hereby granted, provided that the above\n * copyright notice and this permission notice appear in all copies.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\n * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\n * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\n * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\n * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\n * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\n * PERFORMANCE OF THIS SOFTWARE.\n */\nimport nacl, { SignKeyPair } from 'tweetnacl'\nimport aesjs from 'aes-js'\nimport { blake2b, Data } from 'blakejs'\nimport { encode as varuintEncode } from 'varuint-bitcoin'\n\nimport { str2buf } from './bytes'\nimport { concatBuffers } from './other'\nimport { encode, decode, sha256hash, EncodedData, EncodingType } from './encoder'\n\nexport { sha256hash }\n\nconst Ecb = aesjs.ModeOfOperation.ecb\n\n/**\n * Generate address from secret key\n * @param secret - Private key\n * @returns Public key encoded as address\n */\nexport function getAddressFromPriv (secret: string | Uint8Array): EncodedData<'ak'> {\n const secretBuffer = typeof secret === 'string' ? str2buf(secret) : secret\n const keys = nacl.sign.keyPair.fromSecretKey(secretBuffer)\n return encode(keys.publicKey, 'ak')\n}\n\n/**\n * Check if address is valid\n * @param address - Address\n * @param prefix - Transaction prefix. Default: 'ak'\n * @returns is valid\n */\nexport function isAddressValid (address: string, prefix: EncodingType = 'ak'): boolean {\n try {\n decode(address as EncodedData<typeof prefix>)\n return true\n } catch (e) {\n return false\n }\n}\n\n/**\n * Generate a random salt (positive integer)\n * @returns random salt\n */\nexport function salt (): number {\n return Math.floor(Math.random() * Math.floor(Number.MAX_SAFE_INTEGER))\n}\n\n/**\n * Converts a positive integer to the smallest possible\n * representation in a binary digit representation\n * @param value - Value to encode\n * @returns Encoded number\n */\nexport function encodeUnsigned (value: number): Buffer {\n const binary = Buffer.allocUnsafe(4)\n binary.writeUInt32BE(value)\n return binary.slice(binary.findIndex(i => i !== 0))\n}\n\n/**\n * Calculate 256bits Blake2b hash of `input`\n * @param input - Data to hash\n * @returns Hash\n */\nexport function hash (input: Data): Buffer {\n return Buffer.from(blake2b(input, undefined, 32)) // 256 bits\n}\n\n// Todo Duplicated in tx builder. remove\n/**\n * Compute contract address\n * @param owner - Address of contract owner\n * @param nonce - Round when contract was created\n * @returns Contract address\n */\nexport function encodeContractAddress (owner: EncodedData<'ak'>, nonce: number): EncodedData<'ct'> {\n const publicKey = decode(owner)\n const binary = concatBuffers([publicKey, encodeUnsigned(nonce)])\n return encode(hash(binary), 'ct')\n}\n\n// KEY-PAIR HELPERS\n\n/**\n * Generate keyPair from secret key\n * @param secret - secret key\n * @returns Object with Private(privateKey) and Public(publicKey) keys\n */\nexport function generateKeyPairFromSecret (secret: Uint8Array): SignKeyPair {\n return nacl.sign.keyPair.fromSecretKey(secret)\n}\n\n/**\n * Generate a random ED25519 keypair\n * @param raw - Whether to return raw (binary) keys\n * @returns Key pair\n */\nexport function generateKeyPair (raw: true): { publicKey: Buffer, secretKey: Buffer }\nexport function generateKeyPair (raw?: false): { publicKey: EncodedData<'ak'>, secretKey: string }\nexport function generateKeyPair (raw: boolean = false): {\n publicKey: EncodedData<'ak'> | Buffer\n secretKey: string | Buffer\n} {\n const keyPair = nacl.sign.keyPair()\n const publicBuffer = Buffer.from(keyPair.publicKey)\n const secretBuffer = Buffer.from(keyPair.secretKey)\n\n if (raw) {\n return {\n publicKey: publicBuffer,\n secretKey: secretBuffer\n }\n } else {\n return {\n publicKey: encode(publicBuffer, 'ak'),\n secretKey: secretBuffer.toString('hex')\n }\n }\n}\n\n/**\n * Encrypt given data using `password`\n * @param password - Password to encrypt with\n * @param binaryData - Data to encrypt\n * @returns Encrypted data\n */\nexport function encryptKey (password: string, binaryData: Uint8Array): Uint8Array {\n const hashedPasswordBytes = sha256hash(password)\n const aesEcb = new Ecb(hashedPasswordBytes)\n return aesEcb.encrypt(binaryData)\n}\n\n/**\n * Decrypt given data using `password`\n * @param password - Password to decrypt with\n * @param encrypted - Data to decrypt\n * @returns Decrypted data\n */\nexport function decryptKey (password: string, encrypted: Uint8Array): Uint8Array {\n const encryptedBytes = Buffer.from(encrypted)\n const hashedPasswordBytes = sha256hash(password)\n const aesEcb = new Ecb(hashedPasswordBytes)\n return aesEcb.decrypt(encryptedBytes)\n}\n\n// SIGNATURES\n\n/**\n * Generate signature\n * @param data - Data to sign\n * @param privateKey - Key to sign with\n * @returns Signature\n */\nexport function sign (data: string | Uint8Array, privateKey: string | Uint8Array): Uint8Array {\n return nacl.sign.detached(Buffer.from(data), Buffer.from(privateKey))\n}\n\n/**\n * Verify that signature was signed by public key\n * @param data - Data to verify\n * @param signature - Signature to verify\n * @param publicKey - Key to verify against\n * @returns Valid?\n */\nexport function verify (\n data: Uint8Array, signature: Uint8Array, publicKey: string | Uint8Array\n): boolean {\n const publicKeyBuffer = typeof publicKey === 'string' ? str2buf(publicKey) : publicKey\n return nacl.sign.detached.verify(data, signature, publicKeyBuffer)\n}\n\nexport function messageToHash (message: string): Buffer {\n const p = Buffer.from('aeternity Signed Message:\\n', 'utf8')\n const msg = Buffer.from(message, 'utf8')\n return hash(concatBuffers([varuintEncode(p.length), p, varuintEncode(msg.length), msg]))\n}\n\nexport function signMessage (message: string, privateKey: string | Buffer): Uint8Array {\n return sign(messageToHash(message), privateKey)\n}\n\nexport function verifyMessage (\n str: string, signature: Uint8Array, publicKey: string | Uint8Array): boolean {\n return verify(messageToHash(str), signature, publicKey)\n}\n\n/**\n * Check key pair for validity\n *\n * Sign a message, and then verifying that signature\n * @param privateKey - Private key to verify\n * @param publicKey - Public key to verify\n * @returns Valid?\n */\nexport function isValidKeypair (\n privateKey: string | Uint8Array, publicKey: string | Uint8Array\n): boolean {\n const message = Buffer.from('TheMessage')\n const signature = sign(message, privateKey)\n return verify(message, signature, publicKey)\n}\n"],"mappings":";;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAOA,IAAP,MAAkC,WAAlC;AACA,OAAOC,KAAP,MAAkB,QAAlB;AACA,qBAA8B,SAA9B;;;;AACA,4BAAwC,iBAAxC;;;;SAESC,O;SACAC,a;SACAC,M,EAAQC,M,EAAQC,U;AAEzB,SAASA,UAAT;AAEA,MAAM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,IAAI,CAACjC,aAAa,CAAC,CAACmE,aAAa,CAACF,CAAC,CAACG,MAAH,CAAd,EAA0BH,CAA1B,EAA6BE,aAAa,CAACD,GAAG,CAACE,MAAL,CAA1C,EAAwDF,GAAxD,CAAD,CAAd,CAAX;AACD;AAED,OAAO,SAASG,WAAT,CAAsBL,OAAtB,EAAuCN,UAAvC,EAAgF;EACrF,OAAO/C,IAAI,CAACoD,aAAa,CAACC,OAAD,CAAd,EAAyBN,UAAzB,CAAX;AACD;AAED,OAAO,SAASY,aAAT,CACLC,GADK,EACQV,SADR,EAC+B/C,SAD/B,EACwE;EAC7E,OAAO8C,MAAM,CAACG,aAAa,CAACQ,GAAD,CAAd,EAAqBV,SAArB,EAAgC/C,SAAhC,CAAb;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAAS0D,cAAT,CACLd,UADK,EAC4B5C,SAD5B,EAEI;EACT,MAAMkD,OAAO,GAAG,QAAO7B,IAAP,CAAY,YAAZ,CAAhB;;EACA,MAAM0B,SAAS,GAAGlD,IAAI,CAACqD,OAAD,EAAUN,UAAV,CAAtB;EACA,OAAOE,MAAM,CAACI,OAAD,EAAUH,SAAV,EAAqB/C,SAArB,CAAb;AACD"}
@@ -0,0 +1,26 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Calculate SHA256 hash of `input`
4
+ * @param input - Data to hash
5
+ * @returns Hash
6
+ */
7
+ export declare function sha256hash(input: Uint8Array | string): Buffer;
8
+ declare const base64Types: readonly ["ba", "cb", "or", "ov", "pi", "ss", "cs", "ck", "cv", "st", "tx"];
9
+ declare const base58Types: readonly ["ak", "bf", "bs", "bx", "ch", "cm", "ct", "kh", "mh", "nm", "ok", "oq", "pp", "sg", "th"];
10
+ export declare type EncodingType = typeof base64Types[number] | typeof base58Types[number];
11
+ export declare type EncodedData<Type extends EncodingType> = `${Type}_${string}`;
12
+ /**
13
+ * Decode data using the default encoding/decoding algorithm
14
+ * @param data - An Base58/64check encoded and prefixed string
15
+ * (ex tx_..., sg_..., ak_....)
16
+ * @returns Decoded data
17
+ */
18
+ export declare function decode(data: EncodedData<EncodingType>): Buffer;
19
+ /**
20
+ * Encode data using the default encoding/decoding algorithm
21
+ * @param data - An decoded data
22
+ * @param type - Prefix of Transaction
23
+ * @returns Encoded string Base58check or Base64check data
24
+ */
25
+ export declare function encode<Type extends EncodingType>(data: Uint8Array, type: Type): EncodedData<Type>;
26
+ export {};
@@ -2,26 +2,30 @@ import _buffer from "buffer";
2
2
  const {
3
3
  Buffer: _Buffer
4
4
  } = _buffer;
5
- import * as bs58 from 'bs58';
5
+ import _bs from 'bs58';
6
+ const {
7
+ encode: bs58Encode,
8
+ decode: bs58Decode
9
+ } = _bs;
6
10
  import _shaJs from 'sha.js';
7
11
  const {
8
- sha256
12
+ sha256: Sha256
9
13
  } = _shaJs;
10
- import { DecodeError, EncodeError, InvalidChecksumError, PayloadLengthError, PrefixMismatchError } from "./errors.mjs";
14
+ import { DecodeError, ArgumentError, InvalidChecksumError, PayloadLengthError } from "./errors.mjs";
15
+ import { concatBuffers } from "./other.mjs";
11
16
  /**
12
17
  * Calculate SHA256 hash of `input`
13
- * @rtype (input: String) => hash: String
14
- * @param {Buffer|String} input - Data to hash
15
- * @return {String} Hash
18
+ * @param input - Data to hash
19
+ * @returns Hash
16
20
  */
17
21
 
18
22
  export function sha256hash(input) {
19
- return new sha256().update(input).digest();
23
+ return new Sha256().update(input).digest();
20
24
  } // based on https://github.com/aeternity/protocol/blob/master/node/api/api_encoding.md
21
25
 
22
26
  const base64Types = ['ba', 'cb', 'or', 'ov', 'pi', 'ss', 'cs', 'ck', 'cv', 'st', 'tx'];
23
- const base58Types = ['ak', 'bf', 'bs', 'bx', 'ch', 'cm', 'ct', 'kh', 'mh', 'nm', 'ok', 'oq', 'pp', 'sg', 'th']; // TODO: add all types with a fixed length
24
-
27
+ const base58Types = ['ak', 'bf', 'bs', 'bx', 'ch', 'cm', 'ct', 'kh', 'mh', 'nm', 'ok', 'oq', 'pp', 'sg', 'th'];
28
+ // TODO: add all types with a fixed length
25
29
  const typesLength = {
26
30
  ak: 32,
27
31
  ct: 32,
@@ -29,17 +33,15 @@ const typesLength = {
29
33
  };
30
34
 
31
35
  function ensureValidLength(data, type) {
32
- if (!typesLength[type]) return;
33
- if (data.length === typesLength[type]) return;
34
- throw new PayloadLengthError(`Payload should be ${typesLength[type]} bytes, got ${data.length} instead`);
36
+ const reqLen = typesLength[type];
37
+ if (reqLen == null || data.length === reqLen) return;
38
+ throw new PayloadLengthError(`Payload should be ${reqLen} bytes, got ${data.length} instead`);
35
39
  }
36
40
 
37
41
  const getChecksum = payload => sha256hash(sha256hash(payload)).slice(0, 4);
38
42
 
39
- const addChecksum = input => {
40
- const payload = _Buffer.from(input);
41
-
42
- return _Buffer.concat([payload, getChecksum(payload)]);
43
+ const addChecksum = payload => {
44
+ return concatBuffers([payload, getChecksum(payload)]);
43
45
  };
44
46
 
45
47
  function getPayload(buffer) {
@@ -53,54 +55,48 @@ const base64 = {
53
55
  decode: string => getPayload(_Buffer.from(string, 'base64'))
54
56
  };
55
57
  const base58 = {
56
- encode: buffer => bs58.encode(addChecksum(buffer)),
57
- decode: string => getPayload(bs58.decode(string))
58
+ encode: buffer => bs58Encode(addChecksum(buffer)),
59
+ decode: string => getPayload(_Buffer.from(bs58Decode(string)))
60
+ };
61
+
62
+ const parseType = maybeType => {
63
+ const base64Type = base64Types.find(t => t === maybeType);
64
+ if (base64Type != null) return [base64Type, base64];
65
+ const base58Type = base58Types.find(t => t === maybeType);
66
+ if (base58Type != null) return [base58Type, base58];
67
+ throw new ArgumentError('prefix', `one of ${[...base58Types, ...base64Types].join(', ')}`, maybeType);
58
68
  };
59
69
  /**
60
70
  * Decode data using the default encoding/decoding algorithm
61
- * @function
62
- * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
63
- * @param {string} data An Base58/64check encoded and prefixed string (ex tx_..., sg_..., ak_....)
64
- * @param {string} [requiredPrefix] Ensure that data have this prefix
65
- * @return {Buffer} Decoded data
71
+ * @param data - An Base58/64check encoded and prefixed string
72
+ * (ex tx_..., sg_..., ak_....)
73
+ * @returns Decoded data
66
74
  */
67
75
 
68
- export function decode(data, requiredPrefix) {
69
- const [prefix, encodedPayload, extra] = data.split('_');
70
- if (!encodedPayload) throw new DecodeError(`Encoded string missing payload: ${data}`);
71
- if (extra) throw new DecodeError(`Encoded string have extra parts: ${data}`);
72
-
73
- if (requiredPrefix && requiredPrefix !== prefix) {
74
- throw new PrefixMismatchError(prefix, requiredPrefix);
75
- }
76
-
77
- const decoder = base64Types.includes(prefix) && base64.decode || base58Types.includes(prefix) && base58.decode;
78
-
79
- if (!decoder) {
80
- throw new DecodeError(`Encoded string have unknown type: ${prefix}`);
81
- }
82
76
 
83
- const payload = decoder(encodedPayload);
84
- ensureValidLength(payload, prefix);
77
+ export function decode(data) {
78
+ const [prefix, encodedPayload, extra] = data.split('_');
79
+ if (encodedPayload == null) throw new DecodeError(`Encoded string missing payload: ${data}`);
80
+ if (extra != null) throw new DecodeError(`Encoded string have extra parts: ${data}`);
81
+ const [type, {
82
+ decode
83
+ }] = parseType(prefix);
84
+ const payload = decode(encodedPayload);
85
+ ensureValidLength(payload, type);
85
86
  return payload;
86
87
  }
87
88
  /**
88
89
  * Encode data using the default encoding/decoding algorithm
89
- * @function
90
- * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
91
- * @param {Buffer|String} data An decoded data
92
- * @param {string} type Prefix of Transaction
93
- * @return {String} Encoded string Base58check or Base64check data
90
+ * @param data - An decoded data
91
+ * @param type - Prefix of Transaction
92
+ * @returns Encoded string Base58check or Base64check data
94
93
  */
95
94
 
96
95
  export function encode(data, type) {
97
- const encoder = base64Types.includes(type) && base64.encode || base58Types.includes(type) && base58.encode;
98
-
99
- if (!encoder) {
100
- throw new EncodeError(`Unknown type: ${type}`);
101
- }
102
-
96
+ const [, {
97
+ encode
98
+ }] = parseType(type);
103
99
  ensureValidLength(data, type);
104
- return `${type}_${encoder(data)}`;
100
+ return `${type}_${encode(data)}`;
105
101
  }
106
102
  //# sourceMappingURL=encoder.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utils/encoder.ts"],"names":["bs58","DecodeError","EncodeError","InvalidChecksumError","PayloadLengthError","PrefixMismatchError","sha256hash","input","sha256","update","digest","base64Types","base58Types","typesLength","ak","ct","ok","ensureValidLength","data","type","length","getChecksum","payload","slice","addChecksum","from","concat","getPayload","buffer","equals","base64","encode","toString","decode","string","base58","requiredPrefix","prefix","encodedPayload","extra","split","decoder","includes","encoder"],"mappings":";;;;AAAA,OAAO,KAAKA,IAAZ,MAAsB,MAAtB;AACA,mBAAuB,QAAvB;;;;SAEEC,W,EACAC,W,EACAC,oB,EACAC,kB,EACAC,mB;AAGF;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,UAAT,CAAqBC,KAArB,EAA6C;AAClD,SAAO,IAAIC,MAAJ,GAAaC,MAAb,CAAoBF,KAApB,EAA2BG,MAA3B,EAAP;AACD,C,CAED;;AACA,MAAMC,WAAW,GAAG,CAAC,IAAD,EAAO,IAAP,EAAa,IAAb,EAAmB,IAAnB,EAAyB,IAAzB,EAA+B,IAA/B,EAAqC,IAArC,EAA2C,IAA3C,EAAiD,IAAjD,EAAuD,IAAvD,EAA6D,IAA7D,CAApB;AACA,MAAMC,WAAW,GAAG,CAAC,IAAD,EAAO,IAAP,EAAa,IAAb,EAAmB,IAAnB,EAAyB,IAAzB,EAA+B,IAA/B,EAAqC,IAArC,EAA2C,IAA3C,EAAiD,IAAjD,EAAuD,IAAvD,EAA6D,IAA7D,EAAmE,IAAnE,EAAyE,IAAzE,EAA+E,IAA/E,EAAqF,IAArF,CAApB,C,CACA;;AACA,MAAMC,WAAuC,GAAG;AAC9CC,EAAAA,EAAE,EAAE,EAD0C;AAE9CC,EAAAA,EAAE,EAAE,EAF0C;AAG9CC,EAAAA,EAAE,EAAE;AAH0C,CAAhD;;AAMA,SAASC,iBAAT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,KAA8BW,MAAM,CAACC,MAAtC,IACbnB,WAAW,CAAC8B,QAAZ,CAAqBvB,IAArB,KAA8BgB,MAAM,CAACJ,MADxC;;AAEA,MAAI,CAACY,OAAL,EAAc;AACZ,UAAM,IAAIzC,WAAJ,CAAiB,iBAAgBiB,IAAK,EAAtC,CAAN;AACD;;AACDF,EAAAA,iBAAiB,CAACC,IAAD,EAAOC,IAAP,CAAjB;AACA,SAAQ,GAAEA,IAAK,IAAGwB,OAAO,CAACzB,IAAD,CAAO,EAAhC;AACD","sourcesContent":["import * as bs58 from 'bs58'\nimport { sha256 } from 'sha.js'\nimport {\n DecodeError,\n EncodeError,\n InvalidChecksumError,\n PayloadLengthError,\n PrefixMismatchError\n} from './errors'\n\n/**\n * Calculate SHA256 hash of `input`\n * @rtype (input: String) => hash: String\n * @param {Buffer|String} input - Data to hash\n * @return {String} Hash\n */\nexport function sha256hash (input: Buffer | string) {\n return new sha256().update(input).digest()\n}\n\n// based on https://github.com/aeternity/protocol/blob/master/node/api/api_encoding.md\nconst base64Types = ['ba', 'cb', 'or', 'ov', 'pi', 'ss', 'cs', 'ck', 'cv', 'st', 'tx']\nconst base58Types = ['ak', 'bf', 'bs', 'bx', 'ch', 'cm', 'ct', 'kh', 'mh', 'nm', 'ok', 'oq', 'pp', 'sg', 'th']\n// TODO: add all types with a fixed length\nconst typesLength: { [name: string]: number } = {\n ak: 32,\n ct: 32,\n ok: 32\n} as const\n\nfunction ensureValidLength (data: Buffer | string, type: string) {\n if (!typesLength[type]) return\n if (data.length === typesLength[type]) return\n throw new PayloadLengthError(`Payload should be ${typesLength[type]} bytes, got ${data.length} instead`)\n}\n\nconst getChecksum = (payload: Buffer | string) => sha256hash(sha256hash(payload)).slice(0, 4)\n\nconst addChecksum = (input: Buffer | string) => {\n const payload = Buffer.from(input)\n return Buffer.concat([payload, getChecksum(payload)])\n}\n\nfunction getPayload (buffer: Buffer) {\n const payload = buffer.slice(0, -4)\n if (!getChecksum(payload).equals(buffer.slice(-4))) throw new InvalidChecksumError()\n return payload\n}\n\nconst base64 = {\n encode: (buffer: Buffer | string) => addChecksum(buffer).toString('base64'),\n decode: (string: string) => getPayload(Buffer.from(string, 'base64'))\n}\n\nconst base58 = {\n encode: (buffer: Buffer | string) => bs58.encode(addChecksum(buffer)),\n decode: (string: string) => getPayload(bs58.decode(string))\n}\n\n/**\n * Decode data using the default encoding/decoding algorithm\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {string} data An Base58/64check encoded and prefixed string (ex tx_..., sg_..., ak_....)\n * @param {string} [requiredPrefix] Ensure that data have this prefix\n * @return {Buffer} Decoded data\n */\nexport function decode (data: string, requiredPrefix?: string) {\n const [prefix, encodedPayload, extra] = data.split('_')\n if (!encodedPayload) throw new DecodeError(`Encoded string missing payload: ${data}`)\n if (extra) throw new DecodeError(`Encoded string have extra parts: ${data}`)\n if (requiredPrefix && requiredPrefix !== prefix) {\n throw new PrefixMismatchError(prefix, requiredPrefix)\n }\n const decoder = (base64Types.includes(prefix) && base64.decode) ||\n (base58Types.includes(prefix) && base58.decode)\n if (!decoder) {\n throw new DecodeError(`Encoded string have unknown type: ${prefix}`)\n }\n const payload = decoder(encodedPayload)\n ensureValidLength(payload, prefix)\n return payload\n}\n\n/**\n * Encode data using the default encoding/decoding algorithm\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {Buffer|String} data An decoded data\n * @param {string} type Prefix of Transaction\n * @return {String} Encoded string Base58check or Base64check data\n */\nexport function encode (data: Buffer | string, type: string) {\n const encoder = (base64Types.includes(type) && base64.encode) ||\n (base58Types.includes(type) && base58.encode)\n if (!encoder) {\n throw new EncodeError(`Unknown type: ${type}`)\n }\n ensureValidLength(data, type)\n return `${type}_${encoder(data)}`\n}\n"],"file":"encoder.mjs"}
1
+ {"version":3,"file":"encoder.mjs","names":["DecodeError","ArgumentError","InvalidChecksumError","PayloadLengthError","concatBuffers","sha256hash","input","Sha256","update","digest","base64Types","base58Types","typesLength","ak","ct","ok","ensureValidLength","data","type","reqLen","length","getChecksum","payload","slice","addChecksum","getPayload","buffer","equals","base64","encode","toString","decode","string","from","base58","bs58Encode","bs58Decode","parseType","maybeType","base64Type","find","t","base58Type","join","prefix","encodedPayload","extra","split"],"sources":["../../src/utils/encoder.ts"],"sourcesContent":["import { encode as bs58Encode, decode as bs58Decode } from 'bs58'\nimport { sha256 as Sha256 } from 'sha.js'\nimport {\n DecodeError,\n ArgumentError,\n InvalidChecksumError,\n PayloadLengthError\n} from './errors'\nimport { concatBuffers } from './other'\n\n/**\n * Calculate SHA256 hash of `input`\n * @param input - Data to hash\n * @returns Hash\n */\nexport function sha256hash (input: Uint8Array | string): Buffer {\n return new Sha256().update(input).digest()\n}\n\n// based on https://github.com/aeternity/protocol/blob/master/node/api/api_encoding.md\nconst base64Types = ['ba', 'cb', 'or', 'ov', 'pi', 'ss', 'cs', 'ck', 'cv', 'st', 'tx'] as const\nconst base58Types = ['ak', 'bf', 'bs', 'bx', 'ch', 'cm', 'ct', 'kh', 'mh', 'nm', 'ok', 'oq', 'pp', 'sg', 'th'] as const\n\nexport type EncodingType = typeof base64Types[number] | typeof base58Types[number]\nexport type EncodedData<Type extends EncodingType> = `${Type}_${string}`\n// TODO: add all types with a fixed length\nconst typesLength: { [name in EncodingType]?: number } = {\n ak: 32,\n ct: 32,\n ok: 32\n} as const\n\nfunction ensureValidLength (data: Uint8Array, type: EncodingType): void {\n const reqLen = typesLength[type]\n if (reqLen == null || data.length === reqLen) return\n throw new PayloadLengthError(`Payload should be ${reqLen} bytes, got ${data.length} instead`)\n}\n\nconst getChecksum = (payload: Uint8Array): Buffer =>\n sha256hash(sha256hash(payload)).slice(0, 4)\n\nconst addChecksum = (payload: Uint8Array): Buffer => {\n return concatBuffers([payload, getChecksum(payload)])\n}\n\nfunction getPayload (buffer: Buffer): Buffer {\n const payload = buffer.slice(0, -4)\n if (!getChecksum(payload).equals(buffer.slice(-4))) throw new InvalidChecksumError()\n return payload\n}\n\nconst base64 = {\n encode: (buffer: Uint8Array) => addChecksum(buffer).toString('base64'),\n decode: (string: string) => getPayload(Buffer.from(string, 'base64'))\n}\n\nconst base58 = {\n encode: (buffer: Uint8Array) => bs58Encode(addChecksum(buffer)),\n decode: (string: string) => getPayload(Buffer.from(bs58Decode(string)))\n}\n\nconst parseType = (maybeType: unknown): [EncodingType, typeof base64] => {\n const base64Type = base64Types.find(t => t === maybeType)\n if (base64Type != null) return [base64Type, base64]\n const base58Type = base58Types.find(t => t === maybeType)\n if (base58Type != null) return [base58Type, base58]\n throw new ArgumentError('prefix', `one of ${[...base58Types, ...base64Types].join(', ')}`, maybeType)\n}\n\n/**\n * Decode data using the default encoding/decoding algorithm\n * @param data - An Base58/64check encoded and prefixed string\n * (ex tx_..., sg_..., ak_....)\n * @returns Decoded data\n */\nexport function decode (data: EncodedData<EncodingType>): Buffer {\n const [prefix, encodedPayload, extra] = data.split('_')\n if (encodedPayload == null) throw new DecodeError(`Encoded string missing payload: ${data}`)\n if (extra != null) throw new DecodeError(`Encoded string have extra parts: ${data}`)\n const [type, { decode }] = parseType(prefix)\n const payload = decode(encodedPayload)\n ensureValidLength(payload, type)\n return payload\n}\n\n/**\n * Encode data using the default encoding/decoding algorithm\n * @param data - An decoded data\n * @param type - Prefix of Transaction\n * @returns Encoded string Base58check or Base64check data\n */\nexport function encode<Type extends EncodingType> (\n data: Uint8Array, type: Type\n): EncodedData<Type> {\n const [, { encode }] = parseType(type)\n ensureValidLength(data, type)\n return `${type}_${encode(data)}`\n}\n"],"mappings":";;;;AAAA,gBAA2D,MAA3D;;;;;AACA,mBAAiC,QAAjC;;;;SAEEA,W,EACAC,a,EACAC,oB,EACAC,kB;SAEOC,a;AAET;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,UAAT,CAAqBC,KAArB,EAAyD;EAC9D,OAAO,IAAIC,MAAJ,GAAaC,MAAb,CAAoBF,KAApB,EAA2BG,MAA3B,EAAP;AACD,C,CAED;;AACA,MAA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