@aeternity/aepp-sdk 12.0.0 → 12.1.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 (283) hide show
  1. package/dist/aepp-sdk.browser-script.js +1 -1
  2. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +0 -15
  3. package/dist/aepp-sdk.browser-script.js.map +1 -1
  4. package/dist/aepp-sdk.browser.js +2 -1
  5. package/dist/aepp-sdk.browser.js.LICENSE.txt +1 -0
  6. package/dist/aepp-sdk.browser.js.map +1 -1
  7. package/dist/aepp-sdk.js +20764 -1
  8. package/dist/aepp-sdk.js.map +1 -1
  9. package/es/AeSdk.d.ts +7 -7
  10. package/es/AeSdk.mjs.map +1 -1
  11. package/es/AeSdkAepp.d.ts +4 -3
  12. package/es/AeSdkAepp.mjs +2 -1
  13. package/es/AeSdkAepp.mjs.map +1 -1
  14. package/es/AeSdkBase.d.ts +22 -21
  15. package/es/AeSdkBase.mjs +25 -21
  16. package/es/AeSdkBase.mjs.map +1 -1
  17. package/es/AeSdkWallet.d.ts +4 -3
  18. package/es/AeSdkWallet.mjs +5 -1
  19. package/es/AeSdkWallet.mjs.map +1 -1
  20. package/es/Node.d.ts +7 -10
  21. package/es/Node.mjs +63 -57
  22. package/es/Node.mjs.map +1 -1
  23. package/es/account/Base.d.ts +7 -4
  24. package/es/account/Base.mjs +3 -3
  25. package/es/account/Base.mjs.map +1 -1
  26. package/es/account/Memory.d.ts +5 -4
  27. package/es/account/Memory.mjs +24 -2
  28. package/es/account/Memory.mjs.map +1 -1
  29. package/es/account/Rpc.d.ts +6 -6
  30. package/es/account/Rpc.mjs +3 -2
  31. package/es/account/Rpc.mjs.map +1 -1
  32. package/es/aens.d.ts +27 -14
  33. package/es/aens.mjs +27 -21
  34. package/es/aens.mjs.map +1 -1
  35. package/es/aepp-wallet-communication/connection/Browser.d.ts +2 -1
  36. package/es/aepp-wallet-communication/connection/Browser.mjs +5 -2
  37. package/es/aepp-wallet-communication/connection/Browser.mjs.map +1 -1
  38. package/es/aepp-wallet-communication/connection/BrowserRuntime.d.ts +1 -0
  39. package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs +1 -0
  40. package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs.map +1 -1
  41. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.d.ts +1 -0
  42. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs +5 -1
  43. package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs.map +1 -1
  44. package/es/aepp-wallet-communication/connection-proxy.d.ts +1 -0
  45. package/es/aepp-wallet-communication/connection-proxy.mjs +1 -0
  46. package/es/aepp-wallet-communication/connection-proxy.mjs.map +1 -1
  47. package/es/aepp-wallet-communication/rpc/RpcClient.d.ts +1 -0
  48. package/es/aepp-wallet-communication/rpc/RpcClient.mjs +5 -5
  49. package/es/aepp-wallet-communication/rpc/RpcClient.mjs.map +1 -1
  50. package/es/aepp-wallet-communication/rpc/types.d.ts +8 -8
  51. package/es/aepp-wallet-communication/rpc/types.mjs.map +1 -1
  52. package/es/aepp-wallet-communication/schema.d.ts +17 -2
  53. package/es/aepp-wallet-communication/schema.mjs +54 -17
  54. package/es/aepp-wallet-communication/schema.mjs.map +1 -1
  55. package/es/aepp-wallet-communication/wallet-detector.d.ts +2 -1
  56. package/es/aepp-wallet-communication/wallet-detector.mjs +2 -3
  57. package/es/aepp-wallet-communication/wallet-detector.mjs.map +1 -1
  58. package/es/apis/node/models/index.d.ts +6 -2
  59. package/es/apis/node/models/index.mjs.map +1 -1
  60. package/es/apis/node/models/mappers.mjs +4 -2
  61. package/es/apis/node/models/mappers.mjs.map +1 -1
  62. package/es/chain.d.ts +92 -64
  63. package/es/chain.mjs +190 -154
  64. package/es/chain.mjs.map +1 -1
  65. package/es/channel/handlers.d.ts +3 -3
  66. package/es/channel/handlers.mjs +42 -44
  67. package/es/channel/handlers.mjs.map +1 -1
  68. package/es/channel/index.d.ts +43 -52
  69. package/es/channel/index.mjs +46 -49
  70. package/es/channel/index.mjs.map +1 -1
  71. package/es/channel/internal.d.ts +16 -15
  72. package/es/channel/internal.mjs +67 -62
  73. package/es/channel/internal.mjs.map +1 -1
  74. package/es/contract/Compiler.d.ts +1 -0
  75. package/es/contract/Compiler.mjs +5 -4
  76. package/es/contract/Compiler.mjs.map +1 -1
  77. package/es/contract/aci.d.ts +17 -15
  78. package/es/contract/aci.mjs +58 -45
  79. package/es/contract/aci.mjs.map +1 -1
  80. package/es/contract/ga.d.ts +16 -15
  81. package/es/contract/ga.mjs +12 -7
  82. package/es/contract/ga.mjs.map +1 -1
  83. package/es/contract/methods.d.ts +9 -6
  84. package/es/contract/methods.mjs +9 -6
  85. package/es/contract/methods.mjs.map +1 -1
  86. package/es/deprecated/index.d.ts +143 -0
  87. package/es/deprecated/index.mjs +162 -0
  88. package/es/deprecated/index.mjs.map +1 -0
  89. package/es/deprecated/methods.d.ts +18 -0
  90. package/es/deprecated/methods.mjs +26 -0
  91. package/es/deprecated/methods.mjs.map +1 -0
  92. package/es/index.d.ts +4 -1
  93. package/es/index.mjs +4 -1
  94. package/es/index.mjs.map +1 -1
  95. package/es/oracle.d.ts +69 -62
  96. package/es/oracle.mjs +121 -103
  97. package/es/oracle.mjs.map +1 -1
  98. package/es/spend.d.ts +16 -15
  99. package/es/spend.mjs +19 -30
  100. package/es/spend.mjs.map +1 -1
  101. package/es/tx/builder/address.d.ts +25 -0
  102. package/es/tx/builder/address.mjs +45 -0
  103. package/es/tx/builder/address.mjs.map +1 -0
  104. package/es/tx/builder/constants.d.ts +55 -26
  105. package/es/tx/builder/constants.mjs +60 -38
  106. package/es/tx/builder/constants.mjs.map +1 -1
  107. package/es/tx/builder/field-types/coin-amount.d.ts +13 -0
  108. package/es/tx/builder/field-types/coin-amount.mjs +26 -0
  109. package/es/tx/builder/field-types/coin-amount.mjs.map +1 -0
  110. package/es/tx/builder/field-types/deposit.d.ts +15 -0
  111. package/es/tx/builder/field-types/deposit.mjs +19 -0
  112. package/es/tx/builder/field-types/deposit.mjs.map +1 -0
  113. package/es/tx/builder/field-types/fee.d.ts +25 -0
  114. package/es/tx/{min-fee.mjs → builder/field-types/fee.mjs} +62 -60
  115. package/es/tx/builder/field-types/fee.mjs.map +1 -0
  116. package/es/tx/builder/field-types/field.d.ts +6 -0
  117. package/es/tx/builder/field-types/field.mjs +15 -0
  118. package/es/tx/builder/field-types/field.mjs.map +1 -0
  119. package/es/tx/builder/field-types/gas-limit.d.ts +12 -0
  120. package/es/tx/builder/field-types/gas-limit.mjs +33 -0
  121. package/es/tx/builder/field-types/gas-limit.mjs.map +1 -0
  122. package/es/tx/builder/field-types/gas-price.d.ts +8 -0
  123. package/es/tx/builder/field-types/gas-price.mjs +18 -0
  124. package/es/tx/builder/field-types/gas-price.mjs.map +1 -0
  125. package/es/tx/builder/field-types/index.d.ts +70 -0
  126. package/es/tx/builder/field-types/index.mjs +25 -0
  127. package/es/tx/builder/field-types/index.mjs.map +1 -0
  128. package/es/tx/builder/field-types/name-fee.d.ts +20 -0
  129. package/es/tx/builder/field-types/name-fee.mjs +24 -0
  130. package/es/tx/builder/field-types/name-fee.mjs.map +1 -0
  131. package/es/tx/builder/field-types/name-id.d.ts +14 -0
  132. package/es/tx/builder/field-types/name-id.mjs +19 -0
  133. package/es/tx/builder/field-types/name-id.mjs.map +1 -0
  134. package/es/tx/builder/field-types/name.d.ts +13 -0
  135. package/es/tx/builder/field-types/name.mjs +18 -0
  136. package/es/tx/builder/field-types/name.mjs.map +1 -0
  137. package/es/tx/builder/field-types/short-u-int.d.ts +6 -0
  138. package/es/tx/builder/field-types/short-u-int.mjs +12 -0
  139. package/es/tx/builder/field-types/short-u-int.mjs.map +1 -0
  140. package/es/tx/builder/field-types/u-int.d.ts +8 -0
  141. package/es/tx/builder/field-types/u-int.mjs +14 -0
  142. package/es/tx/builder/field-types/u-int.mjs.map +1 -0
  143. package/es/tx/builder/helpers.d.ts +27 -33
  144. package/es/tx/builder/helpers.mjs +41 -47
  145. package/es/tx/builder/helpers.mjs.map +1 -1
  146. package/es/tx/builder/index.d.ts +25 -12
  147. package/es/tx/builder/index.mjs +74 -83
  148. package/es/tx/builder/index.mjs.map +1 -1
  149. package/es/tx/builder/schema.d.ts +995 -144
  150. package/es/tx/builder/schema.mjs +121 -170
  151. package/es/tx/builder/schema.mjs.map +1 -1
  152. package/es/tx/index.d.ts +28 -27
  153. package/es/tx/index.mjs +97 -117
  154. package/es/tx/index.mjs.map +1 -1
  155. package/es/tx/validator.d.ts +4 -3
  156. package/es/tx/validator.mjs +79 -92
  157. package/es/tx/validator.mjs.map +1 -1
  158. package/es/typings/@aeternity__argon2-browser/index.d.mjs.map +1 -1
  159. package/es/typings/@aeternity__uuid/index.d.mjs.map +1 -1
  160. package/es/typings/bip32-path/index.d.mjs.map +1 -1
  161. package/es/typings/blakejs/index.d.mjs.map +1 -1
  162. package/es/typings/sha-js/index.d.mjs +2 -0
  163. package/es/typings/sha-js/index.d.mjs.map +1 -0
  164. package/es/typings/tweetnacl-auth/index.d.mjs.map +1 -1
  165. package/es/utils/amount-formatter.d.ts +9 -9
  166. package/es/utils/amount-formatter.mjs +17 -17
  167. package/es/utils/amount-formatter.mjs.map +1 -1
  168. package/es/utils/autorest.mjs +7 -7
  169. package/es/utils/autorest.mjs.map +1 -1
  170. package/es/utils/bignumber.mjs +2 -1
  171. package/es/utils/bignumber.mjs.map +1 -1
  172. package/es/utils/bytes.mjs +1 -1
  173. package/es/utils/bytes.mjs.map +1 -1
  174. package/es/utils/crypto.d.ts +8 -23
  175. package/es/utils/crypto.mjs +15 -41
  176. package/es/utils/crypto.mjs.map +1 -1
  177. package/es/utils/encoder-types.d.ts +61 -0
  178. package/es/utils/encoder-types.mjs +36 -0
  179. package/es/utils/encoder-types.mjs.map +1 -0
  180. package/es/utils/encoder.d.ts +6 -7
  181. package/es/utils/encoder.mjs +42 -26
  182. package/es/utils/encoder.mjs.map +1 -1
  183. package/es/utils/errors.d.ts +1 -11
  184. package/es/utils/errors.mjs +4 -20
  185. package/es/utils/errors.mjs.map +1 -1
  186. package/es/utils/hd-wallet.d.ts +18 -2
  187. package/es/utils/hd-wallet.mjs +64 -34
  188. package/es/utils/hd-wallet.mjs.map +1 -1
  189. package/es/utils/json-big.mjs +2 -2
  190. package/es/utils/json-big.mjs.map +1 -1
  191. package/es/utils/keystore.mjs +4 -2
  192. package/es/utils/keystore.mjs.map +1 -1
  193. package/es/utils/mptree.d.ts +2 -2
  194. package/es/utils/mptree.mjs +11 -6
  195. package/es/utils/mptree.mjs.map +1 -1
  196. package/es/utils/other.d.ts +8 -2
  197. package/es/utils/other.mjs +15 -3
  198. package/es/utils/other.mjs.map +1 -1
  199. package/es/utils/semver-satisfies.d.ts +1 -1
  200. package/es/utils/semver-satisfies.mjs +2 -2
  201. package/es/utils/semver-satisfies.mjs.map +1 -1
  202. package/es/utils/string.mjs.map +1 -1
  203. package/package.json +35 -33
  204. package/src/AeSdk.ts +30 -27
  205. package/src/AeSdkAepp.ts +93 -84
  206. package/src/AeSdkBase.ts +148 -130
  207. package/src/AeSdkWallet.ts +172 -156
  208. package/src/Node.ts +108 -102
  209. package/src/account/Base.ts +42 -32
  210. package/src/account/Memory.ts +45 -30
  211. package/src/account/Rpc.ts +32 -30
  212. package/src/aens.ts +124 -108
  213. package/src/aepp-wallet-communication/connection/Browser.ts +18 -15
  214. package/src/aepp-wallet-communication/connection/BrowserRuntime.ts +22 -21
  215. package/src/aepp-wallet-communication/connection/BrowserWindowMessage.ts +63 -52
  216. package/src/aepp-wallet-communication/connection-proxy.ts +8 -7
  217. package/src/aepp-wallet-communication/rpc/RpcClient.ts +67 -61
  218. package/src/aepp-wallet-communication/rpc/types.ts +32 -32
  219. package/src/aepp-wallet-communication/schema.ts +117 -74
  220. package/src/aepp-wallet-communication/wallet-detector.ts +29 -28
  221. package/src/apis/node/code-model-v4.yaml +54 -20
  222. package/src/apis/node/models/index.ts +6 -2
  223. package/src/apis/node/models/mappers.ts +4 -2
  224. package/src/chain.ts +286 -228
  225. package/src/channel/handlers.ts +424 -372
  226. package/src/channel/index.ts +285 -251
  227. package/src/channel/internal.ts +248 -232
  228. package/src/contract/Compiler.ts +22 -21
  229. package/src/contract/aci.ts +267 -258
  230. package/src/contract/ga.ts +93 -72
  231. package/src/contract/methods.ts +32 -27
  232. package/src/deprecated/index.ts +170 -0
  233. package/src/deprecated/methods.ts +25 -0
  234. package/src/index.ts +37 -32
  235. package/src/oracle.ts +211 -189
  236. package/src/spend.ts +82 -70
  237. package/src/tx/builder/address.ts +50 -0
  238. package/src/tx/builder/constants.ts +82 -52
  239. package/src/tx/builder/field-types/coin-amount.ts +27 -0
  240. package/src/tx/builder/field-types/deposit.ts +21 -0
  241. package/src/tx/builder/field-types/fee.ts +143 -0
  242. package/src/tx/builder/field-types/field.ts +9 -0
  243. package/src/tx/builder/field-types/gas-limit.ts +41 -0
  244. package/src/tx/builder/field-types/gas-price.ts +16 -0
  245. package/src/tx/builder/field-types/index.ts +45 -0
  246. package/src/tx/builder/field-types/name-fee.ts +32 -0
  247. package/src/tx/builder/field-types/name-id.ts +20 -0
  248. package/src/tx/builder/field-types/name.ts +18 -0
  249. package/src/tx/builder/field-types/short-u-int.ts +11 -0
  250. package/src/tx/builder/field-types/u-int.ts +16 -0
  251. package/src/tx/builder/helpers.ts +112 -117
  252. package/src/tx/builder/index.ts +203 -197
  253. package/src/tx/builder/schema.ts +421 -465
  254. package/src/tx/index.ts +128 -135
  255. package/src/tx/validator.ts +165 -161
  256. package/src/typings/@aeternity__argon2-browser/index.d.ts +1 -1
  257. package/src/typings/@aeternity__uuid/index.d.ts +3 -1
  258. package/src/typings/bip32-path/index.d.ts +6 -6
  259. package/src/typings/blakejs/index.d.ts +4 -32
  260. package/src/typings/sha-js/index.d.ts +5 -0
  261. package/src/typings/tweetnacl-auth/index.d.ts +2 -2
  262. package/src/utils/amount-formatter.ts +41 -41
  263. package/src/utils/autorest.ts +57 -54
  264. package/src/utils/bignumber.ts +7 -6
  265. package/src/utils/bytes.ts +19 -19
  266. package/src/utils/crypto.ts +82 -94
  267. package/src/utils/encoder-types.ts +63 -0
  268. package/src/utils/encoder.ts +104 -56
  269. package/src/utils/errors.ts +246 -264
  270. package/src/utils/hd-wallet.ts +114 -82
  271. package/src/utils/json-big.ts +16 -15
  272. package/src/utils/keystore.ts +65 -62
  273. package/src/utils/mptree.ts +59 -56
  274. package/src/utils/other.ts +26 -18
  275. package/src/utils/semver-satisfies.ts +15 -11
  276. package/src/utils/string.ts +10 -10
  277. package/es/tx/builder/field-types.d.ts +0 -62
  278. package/es/tx/builder/field-types.mjs +0 -126
  279. package/es/tx/builder/field-types.mjs.map +0 -1
  280. package/es/tx/min-fee.d.ts +0 -20
  281. package/es/tx/min-fee.mjs.map +0 -1
  282. package/src/tx/builder/field-types.ts +0 -111
  283. package/src/tx/min-fee.ts +0 -136
@@ -1,134 +1,166 @@
1
- import nacl from 'tweetnacl'
2
- import { full as hmac } from 'tweetnacl-auth'
3
- import { fromString } from 'bip32-path'
4
- import { decryptKey, encryptKey } from './crypto'
5
- import { encode } from './encoder'
6
- import { CryptographyError } from './errors'
7
- import { bytesToHex } from './bytes'
8
- import { concatBuffers } from './other'
1
+ import nacl from 'tweetnacl';
2
+ import { full as hmac } from 'tweetnacl-auth';
3
+ import { fromString } from 'bip32-path';
4
+ import aesjs from 'aes-js';
5
+ import { sha256hash, encode, Encoding } from './encoder';
6
+ import { CryptographyError } from './errors';
7
+ import { bytesToHex } from './bytes';
8
+ import { concatBuffers } from './other';
9
+
10
+ const Ecb = aesjs.ModeOfOperation.ecb;
11
+
12
+ // TODO: at least don't export `encryptKey` and `decryptKey`
13
+ /**
14
+ * Encrypt given data using `password`
15
+ * @param password - Password to encrypt with
16
+ * @param binaryData - Data to encrypt
17
+ * @returns Encrypted data
18
+ * @deprecated use 'sha.js' and 'aes-js' packages directly instead
19
+ */
20
+ export function encryptKey(password: string, binaryData: Uint8Array): Uint8Array {
21
+ const hashedPasswordBytes = sha256hash(password);
22
+ const aesEcb = new Ecb(hashedPasswordBytes);
23
+ return aesEcb.encrypt(binaryData);
24
+ }
25
+
26
+ /**
27
+ * Decrypt given data using `password`
28
+ * @param password - Password to decrypt with
29
+ * @param encrypted - Data to decrypt
30
+ * @returns Decrypted data
31
+ * @deprecated use 'sha.js' and 'aes-js' packages directly instead
32
+ */
33
+ export function decryptKey(password: string, encrypted: Uint8Array): Uint8Array {
34
+ const encryptedBytes = Buffer.from(encrypted);
35
+ const hashedPasswordBytes = sha256hash(password);
36
+ const aesEcb = new Ecb(hashedPasswordBytes);
37
+ return aesEcb.decrypt(encryptedBytes);
38
+ }
9
39
 
10
40
  /**
11
41
  * @category exception
12
42
  */
13
43
  export class DerivationError extends CryptographyError {
14
- constructor (message: string) {
15
- super(message)
16
- this.name = 'DerivationError'
44
+ constructor(message: string) {
45
+ super(message);
46
+ this.name = 'DerivationError';
17
47
  }
18
48
  }
19
49
 
20
- const ED25519_CURVE = Buffer.from('ed25519 seed')
21
- const HARDENED_OFFSET = 0x80000000
50
+ const ED25519_CURVE = Buffer.from('ed25519 seed');
51
+ const HARDENED_OFFSET = 0x80000000;
22
52
 
23
53
  interface KeyTreeNode {
24
- secretKey: Uint8Array
25
- chainCode: Uint8Array
54
+ secretKey: Uint8Array;
55
+ chainCode: Uint8Array;
26
56
  }
27
57
 
28
58
  interface HDWallet {
29
- secretKey: string
30
- chainCode: string
59
+ secretKey: string;
60
+ chainCode: string;
31
61
  }
32
62
 
33
63
  interface Account {
34
- secretKey: string
35
- publicKey: string
64
+ secretKey: string;
65
+ publicKey: string;
36
66
  }
37
67
 
38
- type Dec<N extends number> = [-1, 0, 1, 2, 3, 4][N]
68
+ type Dec<N extends number> = [-1, 0, 1, 2, 3, 4][N];
39
69
  type Bip32PathT<MaxLen extends number, H extends 'H' | 'h' | '\''> = MaxLen extends -1
40
70
  ? `${number}${H}`
41
- : Bip32PathT<Dec<MaxLen>, H> | `${Bip32PathT<Dec<MaxLen>, H>}/${number}${H}`
71
+ : Bip32PathT<Dec<MaxLen>, H> | `${Bip32PathT<Dec<MaxLen>, H>}/${number}${H}`;
42
72
  type Bip32Path<MaxLen extends number> =
43
- '' | Bip32PathT<MaxLen, 'H'> | Bip32PathT<MaxLen, 'h'> | Bip32PathT<MaxLen, '\''>
73
+ '' | Bip32PathT<MaxLen, 'H'> | Bip32PathT<MaxLen, 'h'> | Bip32PathT<MaxLen, '\''>;
44
74
 
45
- export function derivePathFromKey (path: Bip32Path<5>, key: KeyTreeNode): KeyTreeNode {
46
- const segments = path === '' ? [] : fromString(path).toPathArray()
47
- segments.forEach((segment, i) => {
48
- if (segment < HARDENED_OFFSET) {
49
- throw new DerivationError(`Segment #${i + 1} is not hardened`)
50
- }
51
- })
52
-
53
- return segments.reduce((parentKey, segment) => deriveChild(parentKey, segment), key)
54
- }
55
-
56
- export function derivePathFromSeed (path: 'm' | `m/${Bip32Path<5>}`, seed: Uint8Array): KeyTreeNode {
57
- if (!['m', 'm/'].includes(path.slice(0, 2))) {
58
- throw new DerivationError('Root element is required')
75
+ export function deriveChild({ secretKey, chainCode }: KeyTreeNode, index: number): KeyTreeNode {
76
+ if (index < HARDENED_OFFSET) {
77
+ throw new DerivationError(`Segment ${index} is not hardened`);
59
78
  }
60
- const masterKey = getMasterKeyFromSeed(seed)
61
- return derivePathFromKey(path.slice(2) as Bip32Path<5>, masterKey)
62
- }
79
+ const indexBuffer = Buffer.allocUnsafe(4);
80
+ indexBuffer.writeUInt32BE(index, 0);
81
+
82
+ const data = concatBuffers([Buffer.alloc(1, 0), secretKey, indexBuffer]);
63
83
 
64
- function formatAccount (keys: nacl.SignKeyPair): Account {
65
- const { secretKey, publicKey } = keys
84
+ const I = hmac(data, chainCode);
85
+ const IL = I.slice(0, 32);
86
+ const IR = I.slice(32);
66
87
  return {
67
- secretKey: bytesToHex(secretKey),
68
- publicKey: encode(publicKey, 'ak')
69
- }
88
+ secretKey: IL,
89
+ chainCode: IR,
90
+ };
70
91
  }
71
92
 
72
- export function getKeyPair (secretKey: Uint8Array): nacl.SignKeyPair {
73
- return nacl.sign.keyPair.fromSeed(secretKey)
93
+ export function derivePathFromKey(path: Bip32Path<5>, key: KeyTreeNode): KeyTreeNode {
94
+ const segments = path === '' ? [] : fromString(path).toPathArray();
95
+ segments.forEach((segment, i) => {
96
+ if (segment < HARDENED_OFFSET) {
97
+ throw new DerivationError(`Segment #${i + 1} is not hardened`);
98
+ }
99
+ });
100
+
101
+ return segments.reduce((parentKey, segment) => deriveChild(parentKey, segment), key);
74
102
  }
75
103
 
76
- export function getMasterKeyFromSeed (seed: Uint8Array): KeyTreeNode {
77
- const I = hmac(seed, ED25519_CURVE)
78
- const IL = I.slice(0, 32)
79
- const IR = I.slice(32)
104
+ export function getMasterKeyFromSeed(seed: Uint8Array): KeyTreeNode {
105
+ const I = hmac(seed, ED25519_CURVE);
106
+ const IL = I.slice(0, 32);
107
+ const IR = I.slice(32);
80
108
  return {
81
109
  secretKey: IL,
82
- chainCode: IR
83
- }
110
+ chainCode: IR,
111
+ };
84
112
  }
85
113
 
86
- export function deriveChild ({ secretKey, chainCode }: KeyTreeNode, index: number): KeyTreeNode {
87
- if (index < HARDENED_OFFSET) {
88
- throw new DerivationError(`Segment ${index} is not hardened`)
114
+ export function derivePathFromSeed(path: 'm' | `m/${Bip32Path<5>}`, seed: Uint8Array): KeyTreeNode {
115
+ if (!['m', 'm/'].includes(path.slice(0, 2))) {
116
+ throw new DerivationError('Root element is required');
89
117
  }
90
- const indexBuffer = Buffer.allocUnsafe(4)
91
- indexBuffer.writeUInt32BE(index, 0)
92
-
93
- const data = concatBuffers([Buffer.alloc(1, 0), secretKey, indexBuffer])
118
+ const masterKey = getMasterKeyFromSeed(seed);
119
+ return derivePathFromKey(path.slice(2) as Bip32Path<5>, masterKey);
120
+ }
94
121
 
95
- const I = hmac(data, chainCode)
96
- const IL = I.slice(0, 32)
97
- const IR = I.slice(32)
122
+ function formatAccount(keys: nacl.SignKeyPair): Account {
123
+ const { secretKey, publicKey } = keys;
98
124
  return {
99
- secretKey: IL,
100
- chainCode: IR
101
- }
125
+ secretKey: bytesToHex(secretKey),
126
+ publicKey: encode(publicKey, Encoding.AccountAddress),
127
+ };
128
+ }
129
+
130
+ export function getKeyPair(secretKey: Uint8Array): nacl.SignKeyPair {
131
+ return nacl.sign.keyPair.fromSeed(secretKey);
102
132
  }
103
133
 
104
- export function generateSaveHDWalletFromSeed (seed: Uint8Array, password: string): HDWallet {
105
- const walletKey = derivePathFromSeed('m/44h/457h', seed)
134
+ export function generateSaveHDWalletFromSeed(seed: Uint8Array, password: string): HDWallet {
135
+ const walletKey = derivePathFromSeed('m/44h/457h', seed);
106
136
  return {
107
137
  secretKey: bytesToHex(encryptKey(password, walletKey.secretKey)),
108
- chainCode: bytesToHex(encryptKey(password, walletKey.chainCode))
109
- }
138
+ chainCode: bytesToHex(encryptKey(password, walletKey.chainCode)),
139
+ };
110
140
  }
111
141
 
112
- export function getSaveHDWalletAccounts (
113
- saveHDWallet: HDWallet, password: string, accountCount: number
142
+ export function getSaveHDWalletAccounts(
143
+ saveHDWallet: HDWallet,
144
+ password: string,
145
+ accountCount: number,
114
146
  ): Account[] {
115
147
  const walletKey = {
116
148
  secretKey: decryptKey(password, Buffer.from(saveHDWallet.secretKey, 'hex')),
117
- chainCode: decryptKey(password, Buffer.from(saveHDWallet.chainCode, 'hex'))
118
- }
149
+ chainCode: decryptKey(password, Buffer.from(saveHDWallet.chainCode, 'hex')),
150
+ };
119
151
  return (new Array(accountCount)).fill(undefined)
120
- .map((_, idx) =>
121
- formatAccount(getKeyPair(derivePathFromKey(`${idx}h/0h/0h`, walletKey).secretKey)))
152
+ .map((_, idx) => formatAccount(getKeyPair(derivePathFromKey(`${idx}h/0h/0h`, walletKey).secretKey)));
122
153
  }
123
154
 
124
155
  export const getHdWalletAccountFromSeed = (
125
- seed: Uint8Array, accountIdx: number
156
+ seed: Uint8Array,
157
+ accountIdx: number,
126
158
  ): Account & { idx: number } => {
127
- const walletKey = derivePathFromSeed('m/44h/457h', seed)
128
- const derived = derivePathFromKey(`${accountIdx}h/0h/0h`, walletKey)
129
- const keyPair = getKeyPair(derived.secretKey)
159
+ const walletKey = derivePathFromSeed('m/44h/457h', seed);
160
+ const derived = derivePathFromKey(`${accountIdx}h/0h/0h`, walletKey);
161
+ const keyPair = getKeyPair(derived.secretKey);
130
162
  return {
131
163
  ...formatAccount(keyPair),
132
- idx: accountIdx
133
- }
134
- }
164
+ idx: accountIdx,
165
+ };
166
+ };
@@ -1,26 +1,27 @@
1
- import JsonBig from 'json-bigint'
2
- import BigNumber from 'bignumber.js'
3
- import { mapObject } from './other'
1
+ import JsonBig from 'json-bigint';
2
+ import BigNumber from 'bignumber.js';
3
+ import { mapObject } from './other';
4
4
 
5
- const jsonBig = JsonBig({ storeAsString: true })
5
+ const jsonBig = JsonBig({ storeAsString: true });
6
6
 
7
7
  const convertValuesToBigNumbers = (value: any): any => {
8
8
  if (typeof value === 'object' && value !== null && value.constructor === Object) {
9
- return mapObject(value, ([key, value]) => [key, convertValuesToBigNumbers(value)])
9
+ return mapObject(value, ([k, v]) => [k, convertValuesToBigNumbers(v)]);
10
10
  }
11
11
  if (Array.isArray(value)) {
12
- return value.map(item => convertValuesToBigNumbers(item))
12
+ return value.map((item) => convertValuesToBigNumbers(item));
13
13
  }
14
14
  if (typeof value === 'string' && new BigNumber(value).toString(10) === value) {
15
- const bn = new BigNumber(value)
16
- bn.toJSON = () => bn.toString(10)
17
- return bn
15
+ const bn = new BigNumber(value);
16
+ bn.toJSON = () => bn.toString(10);
17
+ return bn;
18
18
  }
19
- return value
20
- }
19
+ return value;
20
+ };
21
21
 
22
22
  export default {
23
- stringify: (...args: Parameters<typeof JsonBig['stringify']>): string =>
24
- jsonBig.stringify(convertValuesToBigNumbers(args[0]), ...args.slice(1)),
25
- parse: jsonBig.parse
26
- }
23
+ stringify: (...args: Parameters<typeof JsonBig['stringify']>): string => (
24
+ jsonBig.stringify(convertValuesToBigNumbers(args[0]), ...args.slice(1))
25
+ ),
26
+ parse: jsonBig.parse,
27
+ };
@@ -1,17 +1,19 @@
1
- import nacl from 'tweetnacl'
2
- import { v4 as uuid } from '@aeternity/uuid'
3
- import { ArgonType, hash } from '@aeternity/argon2-browser/dist/argon2-bundled.min.js'
4
- import { getAddressFromPriv } from './crypto'
5
- import { bytesToHex, hexToBytes } from './bytes'
6
- import { InvalidPasswordError } from './errors'
1
+ import nacl from 'tweetnacl';
2
+ import { v4 as uuid } from '@aeternity/uuid';
3
+ // js extension is required for mjs build
4
+ // eslint-disable-next-line import/extensions
5
+ import { ArgonType, hash } from '@aeternity/argon2-browser/dist/argon2-bundled.min.js';
6
+ import { getAddressFromPriv } from './crypto';
7
+ import { bytesToHex, hexToBytes } from './bytes';
8
+ import { InvalidPasswordError } from './errors';
7
9
 
8
10
  const DERIVED_KEY_FUNCTIONS = {
9
- async argon2id (
11
+ async argon2id(
10
12
  pass: string | Uint8Array,
11
13
  salt: string | Uint8Array,
12
- params: Partial<Keystore['crypto']['kdf_params']>
14
+ params: Partial<Keystore['crypto']['kdf_params']>,
13
15
  ): Promise<Uint8Array> {
14
- const { memlimit_kib: mem, opslimit: time } = params
16
+ const { memlimit_kib: mem, opslimit: time } = params;
15
17
 
16
18
  return (await hash({
17
19
  hashLen: 32,
@@ -19,42 +21,42 @@ const DERIVED_KEY_FUNCTIONS = {
19
21
  salt,
20
22
  time,
21
23
  mem,
22
- type: ArgonType.Argon2id
23
- })).hash
24
- }
25
- }
24
+ type: ArgonType.Argon2id,
25
+ })).hash;
26
+ },
27
+ };
26
28
 
27
29
  const CRYPTO_FUNCTIONS = {
28
30
  'xsalsa20-poly1305': {
29
31
  encrypt: nacl.secretbox,
30
- decrypt (...args: Parameters<typeof nacl.secretbox.open>): Uint8Array {
31
- const res = nacl.secretbox.open(...args)
32
- if (res == null) throw new InvalidPasswordError()
33
- return res
34
- }
35
- }
36
- }
32
+ decrypt(...args: Parameters<typeof nacl.secretbox.open>): Uint8Array {
33
+ const res = nacl.secretbox.open(...args);
34
+ if (res == null) throw new InvalidPasswordError();
35
+ return res;
36
+ },
37
+ },
38
+ };
37
39
 
38
40
  export interface Keystore {
39
- name: string
40
- version: 1
41
- public_key: string
42
- id: string
41
+ name: string;
42
+ version: 1;
43
+ public_key: string;
44
+ id: string;
43
45
  crypto: {
44
- secret_type: 'ed25519'
45
- symmetric_alg: keyof typeof CRYPTO_FUNCTIONS
46
- ciphertext: string
46
+ secret_type: 'ed25519';
47
+ symmetric_alg: keyof typeof CRYPTO_FUNCTIONS;
48
+ ciphertext: string;
47
49
  cipher_params: {
48
- nonce: string
49
- }
50
- kdf: keyof typeof DERIVED_KEY_FUNCTIONS
50
+ nonce: string;
51
+ };
52
+ kdf: keyof typeof DERIVED_KEY_FUNCTIONS;
51
53
  kdf_params: {
52
- memlimit_kib: number
53
- opslimit: number
54
- parallelism: number
55
- salt: string
56
- }
57
- }
54
+ memlimit_kib: number;
55
+ opslimit: number;
56
+ parallelism: number;
57
+ salt: string;
58
+ };
59
+ };
58
60
  }
59
61
 
60
62
  const CRYPTO_DEFAULTS = {
@@ -64,9 +66,9 @@ const CRYPTO_DEFAULTS = {
64
66
  kdf_params: {
65
67
  memlimit_kib: 65536,
66
68
  opslimit: 3,
67
- parallelism: 1
68
- }
69
- } as const
69
+ parallelism: 1,
70
+ },
71
+ } as const;
70
72
 
71
73
  /**
72
74
  * Symmetric private key encryption using secret (derived) key.
@@ -76,13 +78,13 @@ const CRYPTO_DEFAULTS = {
76
78
  * @param algo - Encryption algorithm.
77
79
  * @returns Encrypted data.
78
80
  */
79
- function encrypt (
81
+ function encrypt(
80
82
  plaintext: Uint8Array,
81
83
  key: Uint8Array,
82
84
  nonce: Uint8Array,
83
- algo: keyof typeof CRYPTO_FUNCTIONS = CRYPTO_DEFAULTS.symmetric_alg
85
+ algo: keyof typeof CRYPTO_FUNCTIONS = CRYPTO_DEFAULTS.symmetric_alg,
84
86
  ): Uint8Array {
85
- return CRYPTO_FUNCTIONS[algo].encrypt(plaintext, nonce, key)
87
+ return CRYPTO_FUNCTIONS[algo].encrypt(plaintext, nonce, key);
86
88
  }
87
89
 
88
90
  /**
@@ -93,13 +95,13 @@ function encrypt (
93
95
  * @param algo - Encryption algorithm.
94
96
  * @returns Decrypted data.
95
97
  */
96
- function decrypt (
98
+ function decrypt(
97
99
  ciphertext: Uint8Array,
98
100
  key: Uint8Array,
99
101
  nonce: Uint8Array,
100
- algo: keyof typeof CRYPTO_FUNCTIONS = CRYPTO_DEFAULTS.symmetric_alg
102
+ algo: keyof typeof CRYPTO_FUNCTIONS = CRYPTO_DEFAULTS.symmetric_alg,
101
103
  ): Uint8Array {
102
- return CRYPTO_FUNCTIONS[algo].decrypt(ciphertext, nonce, key)
104
+ return CRYPTO_FUNCTIONS[algo].decrypt(ciphertext, nonce, key);
103
105
  }
104
106
 
105
107
  /**
@@ -110,13 +112,13 @@ function decrypt (
110
112
  * @param kdfParams - KDF parameters.
111
113
  * @returns Secret key derived from password.
112
114
  */
113
- async function deriveKey (
115
+ async function deriveKey(
114
116
  password: string | Uint8Array,
115
117
  nonce: string | Uint8Array,
116
118
  kdf: Keystore['crypto']['kdf'],
117
- kdfParams: Omit<Keystore['crypto']['kdf_params'], 'salt'>
119
+ kdfParams: Omit<Keystore['crypto']['kdf_params'], 'salt'>,
118
120
  ): Promise<Uint8Array> {
119
- return await DERIVED_KEY_FUNCTIONS[kdf](password, nonce, kdfParams)
121
+ return DERIVED_KEY_FUNCTIONS[kdf](password, nonce, kdfParams);
120
122
  }
121
123
 
122
124
  /**
@@ -125,16 +127,17 @@ async function deriveKey (
125
127
  * @param keystore - Keystore object.
126
128
  * @returns Plaintext private key.
127
129
  */
128
- export async function recover (
129
- password: string | Uint8Array, { crypto }: Keystore
130
+ export async function recover(
131
+ password: string | Uint8Array,
132
+ { crypto }: Keystore,
130
133
  ): Promise<string> {
131
- const salt = hexToBytes(crypto.kdf_params.salt)
134
+ const salt = hexToBytes(crypto.kdf_params.salt);
132
135
  return bytesToHex(decrypt(
133
136
  hexToBytes(crypto.ciphertext),
134
137
  await deriveKey(password, salt, crypto.kdf, crypto.kdf_params),
135
138
  hexToBytes(crypto.cipher_params.nonce),
136
- crypto.symmetric_alg
137
- ))
139
+ crypto.symmetric_alg,
140
+ ));
138
141
  }
139
142
 
140
143
  /**
@@ -148,17 +151,17 @@ export async function recover (
148
151
  * @param options.kdf - Key derivation function.
149
152
  * @param options.kdf_params - KDF parameters.
150
153
  */
151
- export async function dump (
154
+ export async function dump(
152
155
  name: string,
153
156
  password: string | Uint8Array,
154
157
  privateKey: string | Uint8Array,
155
158
  nonce: Uint8Array = nacl.randomBytes(24),
156
159
  salt: Uint8Array = nacl.randomBytes(16),
157
- options?: Partial<Keystore['crypto']>
160
+ options?: Partial<Keystore['crypto']>,
158
161
  ): Promise<Keystore> {
159
- const opt = { ...CRYPTO_DEFAULTS, ...options }
160
- const derivedKey = await deriveKey(password, salt, opt.kdf, opt.kdf_params)
161
- const payload = typeof privateKey === 'string' ? hexToBytes(privateKey) : privateKey
162
+ const opt = { ...CRYPTO_DEFAULTS, ...options };
163
+ const derivedKey = await deriveKey(password, salt, opt.kdf, opt.kdf_params);
164
+ const payload = typeof privateKey === 'string' ? hexToBytes(privateKey) : privateKey;
162
165
  return {
163
166
  name,
164
167
  version: 1,
@@ -172,8 +175,8 @@ export async function dump (
172
175
  kdf: opt.kdf,
173
176
  kdf_params: {
174
177
  ...opt.kdf_params,
175
- salt: bytesToHex(salt)
176
- }
177
- }
178
- }
178
+ salt: bytesToHex(salt),
179
+ },
180
+ },
181
+ };
179
182
  }