@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.
- package/dist/aepp-sdk.browser-script.js +1 -1
- package/dist/aepp-sdk.browser-script.js.LICENSE.txt +0 -15
- package/dist/aepp-sdk.browser-script.js.map +1 -1
- package/dist/aepp-sdk.browser.js +2 -1
- package/dist/aepp-sdk.browser.js.LICENSE.txt +1 -0
- package/dist/aepp-sdk.browser.js.map +1 -1
- package/dist/aepp-sdk.js +20764 -1
- package/dist/aepp-sdk.js.map +1 -1
- package/es/AeSdk.d.ts +7 -7
- package/es/AeSdk.mjs.map +1 -1
- package/es/AeSdkAepp.d.ts +4 -3
- package/es/AeSdkAepp.mjs +2 -1
- package/es/AeSdkAepp.mjs.map +1 -1
- package/es/AeSdkBase.d.ts +22 -21
- package/es/AeSdkBase.mjs +25 -21
- package/es/AeSdkBase.mjs.map +1 -1
- package/es/AeSdkWallet.d.ts +4 -3
- package/es/AeSdkWallet.mjs +5 -1
- package/es/AeSdkWallet.mjs.map +1 -1
- package/es/Node.d.ts +7 -10
- package/es/Node.mjs +63 -57
- package/es/Node.mjs.map +1 -1
- package/es/account/Base.d.ts +7 -4
- package/es/account/Base.mjs +3 -3
- package/es/account/Base.mjs.map +1 -1
- package/es/account/Memory.d.ts +5 -4
- package/es/account/Memory.mjs +24 -2
- package/es/account/Memory.mjs.map +1 -1
- package/es/account/Rpc.d.ts +6 -6
- package/es/account/Rpc.mjs +3 -2
- package/es/account/Rpc.mjs.map +1 -1
- package/es/aens.d.ts +27 -14
- package/es/aens.mjs +27 -21
- package/es/aens.mjs.map +1 -1
- package/es/aepp-wallet-communication/connection/Browser.d.ts +2 -1
- package/es/aepp-wallet-communication/connection/Browser.mjs +5 -2
- package/es/aepp-wallet-communication/connection/Browser.mjs.map +1 -1
- package/es/aepp-wallet-communication/connection/BrowserRuntime.d.ts +1 -0
- package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs +1 -0
- package/es/aepp-wallet-communication/connection/BrowserRuntime.mjs.map +1 -1
- package/es/aepp-wallet-communication/connection/BrowserWindowMessage.d.ts +1 -0
- package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs +5 -1
- package/es/aepp-wallet-communication/connection/BrowserWindowMessage.mjs.map +1 -1
- package/es/aepp-wallet-communication/connection-proxy.d.ts +1 -0
- package/es/aepp-wallet-communication/connection-proxy.mjs +1 -0
- package/es/aepp-wallet-communication/connection-proxy.mjs.map +1 -1
- package/es/aepp-wallet-communication/rpc/RpcClient.d.ts +1 -0
- package/es/aepp-wallet-communication/rpc/RpcClient.mjs +5 -5
- package/es/aepp-wallet-communication/rpc/RpcClient.mjs.map +1 -1
- package/es/aepp-wallet-communication/rpc/types.d.ts +8 -8
- package/es/aepp-wallet-communication/rpc/types.mjs.map +1 -1
- package/es/aepp-wallet-communication/schema.d.ts +17 -2
- package/es/aepp-wallet-communication/schema.mjs +54 -17
- package/es/aepp-wallet-communication/schema.mjs.map +1 -1
- package/es/aepp-wallet-communication/wallet-detector.d.ts +2 -1
- package/es/aepp-wallet-communication/wallet-detector.mjs +2 -3
- package/es/aepp-wallet-communication/wallet-detector.mjs.map +1 -1
- package/es/apis/node/models/index.d.ts +6 -2
- package/es/apis/node/models/index.mjs.map +1 -1
- package/es/apis/node/models/mappers.mjs +4 -2
- package/es/apis/node/models/mappers.mjs.map +1 -1
- package/es/chain.d.ts +92 -64
- package/es/chain.mjs +190 -154
- package/es/chain.mjs.map +1 -1
- package/es/channel/handlers.d.ts +3 -3
- package/es/channel/handlers.mjs +42 -44
- package/es/channel/handlers.mjs.map +1 -1
- package/es/channel/index.d.ts +43 -52
- package/es/channel/index.mjs +46 -49
- package/es/channel/index.mjs.map +1 -1
- package/es/channel/internal.d.ts +16 -15
- package/es/channel/internal.mjs +67 -62
- package/es/channel/internal.mjs.map +1 -1
- package/es/contract/Compiler.d.ts +1 -0
- package/es/contract/Compiler.mjs +5 -4
- package/es/contract/Compiler.mjs.map +1 -1
- package/es/contract/aci.d.ts +17 -15
- package/es/contract/aci.mjs +58 -45
- package/es/contract/aci.mjs.map +1 -1
- package/es/contract/ga.d.ts +16 -15
- package/es/contract/ga.mjs +12 -7
- package/es/contract/ga.mjs.map +1 -1
- package/es/contract/methods.d.ts +9 -6
- package/es/contract/methods.mjs +9 -6
- package/es/contract/methods.mjs.map +1 -1
- package/es/deprecated/index.d.ts +143 -0
- package/es/deprecated/index.mjs +162 -0
- package/es/deprecated/index.mjs.map +1 -0
- package/es/deprecated/methods.d.ts +18 -0
- package/es/deprecated/methods.mjs +26 -0
- package/es/deprecated/methods.mjs.map +1 -0
- package/es/index.d.ts +4 -1
- package/es/index.mjs +4 -1
- package/es/index.mjs.map +1 -1
- package/es/oracle.d.ts +69 -62
- package/es/oracle.mjs +121 -103
- package/es/oracle.mjs.map +1 -1
- package/es/spend.d.ts +16 -15
- package/es/spend.mjs +19 -30
- package/es/spend.mjs.map +1 -1
- package/es/tx/builder/address.d.ts +25 -0
- package/es/tx/builder/address.mjs +45 -0
- package/es/tx/builder/address.mjs.map +1 -0
- package/es/tx/builder/constants.d.ts +55 -26
- package/es/tx/builder/constants.mjs +60 -38
- package/es/tx/builder/constants.mjs.map +1 -1
- package/es/tx/builder/field-types/coin-amount.d.ts +13 -0
- package/es/tx/builder/field-types/coin-amount.mjs +26 -0
- package/es/tx/builder/field-types/coin-amount.mjs.map +1 -0
- package/es/tx/builder/field-types/deposit.d.ts +15 -0
- package/es/tx/builder/field-types/deposit.mjs +19 -0
- package/es/tx/builder/field-types/deposit.mjs.map +1 -0
- package/es/tx/builder/field-types/fee.d.ts +25 -0
- package/es/tx/{min-fee.mjs → builder/field-types/fee.mjs} +62 -60
- package/es/tx/builder/field-types/fee.mjs.map +1 -0
- package/es/tx/builder/field-types/field.d.ts +6 -0
- package/es/tx/builder/field-types/field.mjs +15 -0
- package/es/tx/builder/field-types/field.mjs.map +1 -0
- package/es/tx/builder/field-types/gas-limit.d.ts +12 -0
- package/es/tx/builder/field-types/gas-limit.mjs +33 -0
- package/es/tx/builder/field-types/gas-limit.mjs.map +1 -0
- package/es/tx/builder/field-types/gas-price.d.ts +8 -0
- package/es/tx/builder/field-types/gas-price.mjs +18 -0
- package/es/tx/builder/field-types/gas-price.mjs.map +1 -0
- package/es/tx/builder/field-types/index.d.ts +70 -0
- package/es/tx/builder/field-types/index.mjs +25 -0
- package/es/tx/builder/field-types/index.mjs.map +1 -0
- package/es/tx/builder/field-types/name-fee.d.ts +20 -0
- package/es/tx/builder/field-types/name-fee.mjs +24 -0
- package/es/tx/builder/field-types/name-fee.mjs.map +1 -0
- package/es/tx/builder/field-types/name-id.d.ts +14 -0
- package/es/tx/builder/field-types/name-id.mjs +19 -0
- package/es/tx/builder/field-types/name-id.mjs.map +1 -0
- package/es/tx/builder/field-types/name.d.ts +13 -0
- package/es/tx/builder/field-types/name.mjs +18 -0
- package/es/tx/builder/field-types/name.mjs.map +1 -0
- package/es/tx/builder/field-types/short-u-int.d.ts +6 -0
- package/es/tx/builder/field-types/short-u-int.mjs +12 -0
- package/es/tx/builder/field-types/short-u-int.mjs.map +1 -0
- package/es/tx/builder/field-types/u-int.d.ts +8 -0
- package/es/tx/builder/field-types/u-int.mjs +14 -0
- package/es/tx/builder/field-types/u-int.mjs.map +1 -0
- package/es/tx/builder/helpers.d.ts +27 -33
- package/es/tx/builder/helpers.mjs +41 -47
- package/es/tx/builder/helpers.mjs.map +1 -1
- package/es/tx/builder/index.d.ts +25 -12
- package/es/tx/builder/index.mjs +74 -83
- package/es/tx/builder/index.mjs.map +1 -1
- package/es/tx/builder/schema.d.ts +995 -144
- package/es/tx/builder/schema.mjs +121 -170
- package/es/tx/builder/schema.mjs.map +1 -1
- package/es/tx/index.d.ts +28 -27
- package/es/tx/index.mjs +97 -117
- package/es/tx/index.mjs.map +1 -1
- package/es/tx/validator.d.ts +4 -3
- package/es/tx/validator.mjs +79 -92
- package/es/tx/validator.mjs.map +1 -1
- package/es/typings/@aeternity__argon2-browser/index.d.mjs.map +1 -1
- package/es/typings/@aeternity__uuid/index.d.mjs.map +1 -1
- package/es/typings/bip32-path/index.d.mjs.map +1 -1
- package/es/typings/blakejs/index.d.mjs.map +1 -1
- package/es/typings/sha-js/index.d.mjs +2 -0
- package/es/typings/sha-js/index.d.mjs.map +1 -0
- package/es/typings/tweetnacl-auth/index.d.mjs.map +1 -1
- package/es/utils/amount-formatter.d.ts +9 -9
- package/es/utils/amount-formatter.mjs +17 -17
- package/es/utils/amount-formatter.mjs.map +1 -1
- package/es/utils/autorest.mjs +7 -7
- package/es/utils/autorest.mjs.map +1 -1
- package/es/utils/bignumber.mjs +2 -1
- package/es/utils/bignumber.mjs.map +1 -1
- package/es/utils/bytes.mjs +1 -1
- package/es/utils/bytes.mjs.map +1 -1
- package/es/utils/crypto.d.ts +8 -23
- package/es/utils/crypto.mjs +15 -41
- package/es/utils/crypto.mjs.map +1 -1
- package/es/utils/encoder-types.d.ts +61 -0
- package/es/utils/encoder-types.mjs +36 -0
- package/es/utils/encoder-types.mjs.map +1 -0
- package/es/utils/encoder.d.ts +6 -7
- package/es/utils/encoder.mjs +42 -26
- package/es/utils/encoder.mjs.map +1 -1
- package/es/utils/errors.d.ts +1 -11
- package/es/utils/errors.mjs +4 -20
- package/es/utils/errors.mjs.map +1 -1
- package/es/utils/hd-wallet.d.ts +18 -2
- package/es/utils/hd-wallet.mjs +64 -34
- package/es/utils/hd-wallet.mjs.map +1 -1
- package/es/utils/json-big.mjs +2 -2
- package/es/utils/json-big.mjs.map +1 -1
- package/es/utils/keystore.mjs +4 -2
- package/es/utils/keystore.mjs.map +1 -1
- package/es/utils/mptree.d.ts +2 -2
- package/es/utils/mptree.mjs +11 -6
- package/es/utils/mptree.mjs.map +1 -1
- package/es/utils/other.d.ts +8 -2
- package/es/utils/other.mjs +15 -3
- package/es/utils/other.mjs.map +1 -1
- package/es/utils/semver-satisfies.d.ts +1 -1
- package/es/utils/semver-satisfies.mjs +2 -2
- package/es/utils/semver-satisfies.mjs.map +1 -1
- package/es/utils/string.mjs.map +1 -1
- package/package.json +35 -33
- package/src/AeSdk.ts +30 -27
- package/src/AeSdkAepp.ts +93 -84
- package/src/AeSdkBase.ts +148 -130
- package/src/AeSdkWallet.ts +172 -156
- package/src/Node.ts +108 -102
- package/src/account/Base.ts +42 -32
- package/src/account/Memory.ts +45 -30
- package/src/account/Rpc.ts +32 -30
- package/src/aens.ts +124 -108
- package/src/aepp-wallet-communication/connection/Browser.ts +18 -15
- package/src/aepp-wallet-communication/connection/BrowserRuntime.ts +22 -21
- package/src/aepp-wallet-communication/connection/BrowserWindowMessage.ts +63 -52
- package/src/aepp-wallet-communication/connection-proxy.ts +8 -7
- package/src/aepp-wallet-communication/rpc/RpcClient.ts +67 -61
- package/src/aepp-wallet-communication/rpc/types.ts +32 -32
- package/src/aepp-wallet-communication/schema.ts +117 -74
- package/src/aepp-wallet-communication/wallet-detector.ts +29 -28
- package/src/apis/node/code-model-v4.yaml +54 -20
- package/src/apis/node/models/index.ts +6 -2
- package/src/apis/node/models/mappers.ts +4 -2
- package/src/chain.ts +286 -228
- package/src/channel/handlers.ts +424 -372
- package/src/channel/index.ts +285 -251
- package/src/channel/internal.ts +248 -232
- package/src/contract/Compiler.ts +22 -21
- package/src/contract/aci.ts +267 -258
- package/src/contract/ga.ts +93 -72
- package/src/contract/methods.ts +32 -27
- package/src/deprecated/index.ts +170 -0
- package/src/deprecated/methods.ts +25 -0
- package/src/index.ts +37 -32
- package/src/oracle.ts +211 -189
- package/src/spend.ts +82 -70
- package/src/tx/builder/address.ts +50 -0
- package/src/tx/builder/constants.ts +82 -52
- package/src/tx/builder/field-types/coin-amount.ts +27 -0
- package/src/tx/builder/field-types/deposit.ts +21 -0
- package/src/tx/builder/field-types/fee.ts +143 -0
- package/src/tx/builder/field-types/field.ts +9 -0
- package/src/tx/builder/field-types/gas-limit.ts +41 -0
- package/src/tx/builder/field-types/gas-price.ts +16 -0
- package/src/tx/builder/field-types/index.ts +45 -0
- package/src/tx/builder/field-types/name-fee.ts +32 -0
- package/src/tx/builder/field-types/name-id.ts +20 -0
- package/src/tx/builder/field-types/name.ts +18 -0
- package/src/tx/builder/field-types/short-u-int.ts +11 -0
- package/src/tx/builder/field-types/u-int.ts +16 -0
- package/src/tx/builder/helpers.ts +112 -117
- package/src/tx/builder/index.ts +203 -197
- package/src/tx/builder/schema.ts +421 -465
- package/src/tx/index.ts +128 -135
- package/src/tx/validator.ts +165 -161
- package/src/typings/@aeternity__argon2-browser/index.d.ts +1 -1
- package/src/typings/@aeternity__uuid/index.d.ts +3 -1
- package/src/typings/bip32-path/index.d.ts +6 -6
- package/src/typings/blakejs/index.d.ts +4 -32
- package/src/typings/sha-js/index.d.ts +5 -0
- package/src/typings/tweetnacl-auth/index.d.ts +2 -2
- package/src/utils/amount-formatter.ts +41 -41
- package/src/utils/autorest.ts +57 -54
- package/src/utils/bignumber.ts +7 -6
- package/src/utils/bytes.ts +19 -19
- package/src/utils/crypto.ts +82 -94
- package/src/utils/encoder-types.ts +63 -0
- package/src/utils/encoder.ts +104 -56
- package/src/utils/errors.ts +246 -264
- package/src/utils/hd-wallet.ts +114 -82
- package/src/utils/json-big.ts +16 -15
- package/src/utils/keystore.ts +65 -62
- package/src/utils/mptree.ts +59 -56
- package/src/utils/other.ts +26 -18
- package/src/utils/semver-satisfies.ts +15 -11
- package/src/utils/string.ts +10 -10
- package/es/tx/builder/field-types.d.ts +0 -62
- package/es/tx/builder/field-types.mjs +0 -126
- package/es/tx/builder/field-types.mjs.map +0 -1
- package/es/tx/min-fee.d.ts +0 -20
- package/es/tx/min-fee.mjs.map +0 -1
- package/src/tx/builder/field-types.ts +0 -111
- package/src/tx/min-fee.ts +0 -136
package/src/utils/hd-wallet.ts
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
46
|
-
|
|
47
|
-
|
|
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
|
|
61
|
-
|
|
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
|
-
|
|
65
|
-
const
|
|
84
|
+
const I = hmac(data, chainCode);
|
|
85
|
+
const IL = I.slice(0, 32);
|
|
86
|
+
const IR = I.slice(32);
|
|
66
87
|
return {
|
|
67
|
-
secretKey:
|
|
68
|
-
|
|
69
|
-
}
|
|
88
|
+
secretKey: IL,
|
|
89
|
+
chainCode: IR,
|
|
90
|
+
};
|
|
70
91
|
}
|
|
71
92
|
|
|
72
|
-
export function
|
|
73
|
-
|
|
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
|
|
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
|
|
87
|
-
if (
|
|
88
|
-
throw new DerivationError(
|
|
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
|
|
91
|
-
|
|
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
|
-
|
|
96
|
-
const
|
|
97
|
-
const IR = I.slice(32)
|
|
122
|
+
function formatAccount(keys: nacl.SignKeyPair): Account {
|
|
123
|
+
const { secretKey, publicKey } = keys;
|
|
98
124
|
return {
|
|
99
|
-
secretKey:
|
|
100
|
-
|
|
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
|
|
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,
|
|
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,
|
|
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
|
+
};
|
package/src/utils/json-big.ts
CHANGED
|
@@ -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, ([
|
|
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
|
-
|
|
26
|
-
|
|
23
|
+
stringify: (...args: Parameters<typeof JsonBig['stringify']>): string => (
|
|
24
|
+
jsonBig.stringify(convertValuesToBigNumbers(args[0]), ...args.slice(1))
|
|
25
|
+
),
|
|
26
|
+
parse: jsonBig.parse,
|
|
27
|
+
};
|
package/src/utils/keystore.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import nacl from 'tweetnacl'
|
|
2
|
-
import { v4 as uuid } from '@aeternity/uuid'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
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
|
|
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
|
|
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,
|
|
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
|
}
|