@aeternity/aepp-sdk 9.0.1 → 11.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -14
- package/dist/aepp-sdk.browser-script.js +1 -1
- package/dist/aepp-sdk.browser-script.js.LICENSE.txt +7 -36
- package/dist/aepp-sdk.browser-script.js.map +1 -1
- package/dist/aepp-sdk.browser.js +1 -1
- package/dist/aepp-sdk.browser.js.map +1 -1
- package/dist/aepp-sdk.js +1 -1
- package/dist/aepp-sdk.js.map +1 -1
- package/es/account/base.mjs +179 -0
- package/es/account/base.mjs.map +1 -0
- package/es/account/memory.mjs +99 -0
- package/es/account/memory.mjs.map +1 -0
- package/es/account/multiple.mjs +179 -0
- package/es/account/multiple.mjs.map +1 -0
- package/es/ae/aens.mjs +404 -0
- package/es/ae/aens.mjs.map +1 -0
- package/es/ae/{aepp.js → aepp.mjs} +9 -9
- package/es/ae/aepp.mjs.map +1 -0
- package/es/ae/contract.mjs +159 -0
- package/es/ae/contract.mjs.map +1 -0
- package/es/ae/index.mjs +207 -0
- package/es/ae/index.mjs.map +1 -0
- package/es/ae/oracle.mjs +339 -0
- package/es/ae/oracle.mjs.map +1 -0
- package/es/ae/{universal.js → universal.mjs} +10 -10
- package/es/ae/universal.mjs.map +1 -0
- package/es/ae/{wallet.js → wallet.mjs} +10 -10
- package/es/ae/wallet.mjs.map +1 -0
- package/es/aepp-wallet-communication/schema.d.ts +74 -0
- package/es/bignumber.d.ts +1 -7
- package/es/chain/{index.js → index.mjs} +29 -9
- package/es/chain/index.mjs.map +1 -0
- package/es/chain/node.mjs +442 -0
- package/es/chain/node.mjs.map +1 -0
- package/es/channel/handlers.mjs +1110 -0
- package/es/channel/handlers.mjs.map +1 -0
- package/es/channel/{index.js → index.mjs} +245 -472
- package/es/channel/index.mjs.map +1 -0
- package/es/channel/internal.mjs +283 -0
- package/es/channel/internal.mjs.map +1 -0
- package/es/contract/aci.mjs +477 -0
- package/es/contract/aci.mjs.map +1 -0
- package/es/contract/compiler.mjs +115 -0
- package/es/contract/compiler.mjs.map +1 -0
- package/es/contract/ga/helpers.mjs +30 -0
- package/es/contract/ga/helpers.mjs.map +1 -0
- package/es/contract/ga/index.mjs +178 -0
- package/es/contract/ga/index.mjs.map +1 -0
- package/es/errors.d.ts +284 -0
- package/es/index.mjs +57 -0
- package/es/index.mjs.map +1 -0
- package/es/node-pool/index.mjs +188 -0
- package/es/node-pool/index.mjs.map +1 -0
- package/es/node.mjs +133 -0
- package/es/node.mjs.map +1 -0
- package/es/other.d.ts +1 -1
- package/es/tx/builder/field-types.mjs +74 -0
- package/es/tx/builder/field-types.mjs.map +1 -0
- package/es/tx/builder/helpers.mjs +363 -0
- package/es/tx/builder/helpers.mjs.map +1 -0
- package/es/tx/builder/index.mjs +606 -0
- package/es/tx/builder/index.mjs.map +1 -0
- package/es/tx/builder/schema.mjs +760 -0
- package/es/tx/builder/schema.mjs.map +1 -0
- package/es/tx/{index.js → index.mjs} +107 -15
- package/es/tx/index.mjs.map +1 -0
- package/es/tx/tx-object.mjs +283 -0
- package/es/tx/tx-object.mjs.map +1 -0
- package/es/tx/tx.mjs +938 -0
- package/es/tx/tx.mjs.map +1 -0
- package/es/tx/validator.mjs +247 -0
- package/es/tx/validator.mjs.map +1 -0
- package/es/typings/blakejs/index.d.mjs +2 -0
- package/es/typings/blakejs/{index.d.js.map → index.d.mjs.map} +1 -1
- package/es/utils/aepp-wallet-communication/connection/{browser-runtime.js → browser-runtime.mjs} +27 -29
- package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs +183 -0
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/connection/{index.js → index.mjs} +7 -3
- package/es/utils/aepp-wallet-communication/connection/index.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/{content-script-bridge.js → content-script-bridge.mjs} +29 -23
- package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/helpers.mjs +92 -0
- package/es/utils/aepp-wallet-communication/helpers.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs +375 -0
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs +275 -0
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs +541 -0
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/schema.mjs +102 -0
- package/es/utils/aepp-wallet-communication/schema.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/wallet-detector.mjs +110 -0
- package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
- package/es/utils/amount-formatter.mjs +139 -0
- package/es/utils/amount-formatter.mjs.map +1 -0
- package/es/utils/async-init.mjs +64 -0
- package/es/utils/async-init.mjs.map +1 -0
- package/es/utils/bignumber.mjs +21 -0
- package/es/utils/bignumber.mjs.map +1 -0
- package/es/utils/{bytes.js → bytes.mjs} +16 -12
- package/es/utils/bytes.mjs.map +1 -0
- package/es/utils/{crypto-ts.js → crypto-ts.mjs} +11 -3
- package/es/utils/crypto-ts.mjs.map +1 -0
- package/es/utils/crypto.mjs +239 -0
- package/es/utils/crypto.mjs.map +1 -0
- package/es/utils/errors.mjs +689 -0
- package/es/utils/errors.mjs.map +1 -0
- package/es/utils/hd-wallet.mjs +111 -0
- package/es/utils/hd-wallet.mjs.map +1 -0
- package/es/utils/json-big.mjs +45 -0
- package/es/utils/json-big.mjs.map +1 -0
- package/es/utils/keystore.mjs +240 -0
- package/es/utils/keystore.mjs.map +1 -0
- package/es/utils/mptree.mjs +161 -0
- package/es/utils/mptree.mjs.map +1 -0
- package/es/utils/other.mjs +43 -0
- package/es/utils/other.mjs.map +1 -0
- package/es/utils/semver-satisfies.mjs +16 -0
- package/es/utils/semver-satisfies.mjs.map +1 -0
- package/es/utils/{string.js → string.mjs} +5 -14
- package/es/utils/string.mjs.map +1 -0
- package/es/utils/swagger.mjs +164 -0
- package/es/utils/swagger.mjs.map +1 -0
- package/package.json +55 -62
- package/src/account/base.js +3 -1
- package/src/account/memory.js +13 -8
- package/src/account/multiple.js +31 -22
- package/src/ae/aens.js +75 -77
- package/src/ae/aepp.js +2 -2
- package/src/ae/contract.js +27 -291
- package/src/ae/index.js +9 -7
- package/src/ae/oracle.js +51 -28
- package/src/ae/universal.js +4 -2
- package/src/ae/wallet.js +2 -2
- package/src/chain/index.js +29 -6
- package/src/chain/node.js +95 -67
- package/src/channel/handlers.js +30 -21
- package/src/channel/index.js +44 -20
- package/src/channel/internal.js +30 -24
- package/src/contract/aci.js +376 -0
- package/src/contract/compiler.js +41 -91
- package/src/contract/ga/helpers.js +10 -13
- package/src/contract/ga/index.js +42 -33
- package/src/index.js +9 -10
- package/src/node-pool/index.js +29 -17
- package/src/node.js +33 -36
- package/src/tx/builder/field-types.js +61 -0
- package/src/tx/builder/helpers.js +126 -84
- package/src/tx/builder/index.js +83 -41
- package/src/tx/builder/schema.js +50 -40
- package/src/tx/index.js +101 -12
- package/src/tx/tx-object.js +35 -15
- package/src/tx/tx.js +232 -153
- package/src/tx/validator.js +34 -12
- package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +11 -5
- package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +35 -13
- package/src/utils/aepp-wallet-communication/connection/index.js +2 -1
- package/src/utils/aepp-wallet-communication/content-script-bridge.js +14 -7
- package/src/utils/aepp-wallet-communication/helpers.js +6 -14
- package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +107 -60
- package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +24 -11
- package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +98 -56
- package/src/utils/aepp-wallet-communication/schema.ts +80 -0
- package/src/utils/aepp-wallet-communication/wallet-detector.js +6 -2
- package/src/utils/amount-formatter.ts +4 -3
- package/src/utils/async-init.js +10 -6
- package/src/utils/bignumber.ts +3 -10
- package/src/utils/bytes.ts +3 -2
- package/src/utils/crypto-ts.ts +2 -2
- package/src/utils/crypto.js +10 -144
- package/src/utils/errors.ts +683 -0
- package/src/utils/hd-wallet.js +11 -26
- package/src/utils/keystore.js +57 -53
- package/src/utils/mptree.ts +12 -6
- package/src/utils/other.ts +1 -1
- package/src/utils/semver-satisfies.ts +1 -1
- package/src/utils/swagger.js +75 -45
- package/es/account/base.js +0 -259
- package/es/account/base.js.map +0 -1
- package/es/account/memory.js +0 -86
- package/es/account/memory.js.map +0 -1
- package/es/account/multiple.js +0 -267
- package/es/account/multiple.js.map +0 -1
- package/es/ae/aens.js +0 -829
- package/es/ae/aens.js.map +0 -1
- package/es/ae/aepp.js.map +0 -1
- package/es/ae/contract.js +0 -985
- package/es/ae/contract.js.map +0 -1
- package/es/ae/index.js +0 -442
- package/es/ae/index.js.map +0 -1
- package/es/ae/oracle.js +0 -690
- package/es/ae/oracle.js.map +0 -1
- package/es/ae/universal.js.map +0 -1
- package/es/ae/wallet.js.map +0 -1
- package/es/chain/index.js.map +0 -1
- package/es/chain/node.js +0 -1119
- package/es/chain/node.js.map +0 -1
- package/es/channel/handlers.js +0 -1601
- package/es/channel/handlers.js.map +0 -1
- package/es/channel/index.js.map +0 -1
- package/es/channel/internal.js +0 -438
- package/es/channel/internal.js.map +0 -1
- package/es/contract/aci/helpers.js +0 -298
- package/es/contract/aci/helpers.js.map +0 -1
- package/es/contract/aci/index.js +0 -517
- package/es/contract/aci/index.js.map +0 -1
- package/es/contract/aci/transformation.js +0 -742
- package/es/contract/aci/transformation.js.map +0 -1
- package/es/contract/compiler.js +0 -316
- package/es/contract/compiler.js.map +0 -1
- package/es/contract/ga/helpers.js +0 -120
- package/es/contract/ga/helpers.js.map +0 -1
- package/es/contract/ga/index.js +0 -307
- package/es/contract/ga/index.js.map +0 -1
- package/es/contract/index.js +0 -158
- package/es/contract/index.js.map +0 -1
- package/es/index.js +0 -57
- package/es/index.js.map +0 -1
- package/es/node-pool/index.js +0 -204
- package/es/node-pool/index.js.map +0 -1
- package/es/node.js +0 -228
- package/es/node.js.map +0 -1
- package/es/tx/builder/helpers.js +0 -355
- package/es/tx/builder/helpers.js.map +0 -1
- package/es/tx/builder/index.js +0 -686
- package/es/tx/builder/index.js.map +0 -1
- package/es/tx/builder/schema.js +0 -695
- package/es/tx/builder/schema.js.map +0 -1
- package/es/tx/index.js.map +0 -1
- package/es/tx/tx-object.js +0 -297
- package/es/tx/tx-object.js.map +0 -1
- package/es/tx/tx.js +0 -1757
- package/es/tx/tx.js.map +0 -1
- package/es/tx/validator.js +0 -344
- package/es/tx/validator.js.map +0 -1
- package/es/typings/blakejs/index.d.js +0 -2
- package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +0 -1
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.js +0 -211
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +0 -1
- package/es/utils/aepp-wallet-communication/connection/index.js.map +0 -1
- package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +0 -1
- package/es/utils/aepp-wallet-communication/helpers.js +0 -148
- package/es/utils/aepp-wallet-communication/helpers.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js +0 -652
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.js +0 -285
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +0 -763
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +0 -1
- package/es/utils/aepp-wallet-communication/schema.js +0 -103
- package/es/utils/aepp-wallet-communication/schema.js.map +0 -1
- package/es/utils/aepp-wallet-communication/wallet-detector.js +0 -135
- package/es/utils/aepp-wallet-communication/wallet-detector.js.map +0 -1
- package/es/utils/amount-formatter.js +0 -152
- package/es/utils/amount-formatter.js.map +0 -1
- package/es/utils/async-init.js +0 -103
- package/es/utils/async-init.js.map +0 -1
- package/es/utils/bignumber.js +0 -36
- package/es/utils/bignumber.js.map +0 -1
- package/es/utils/bytes.js.map +0 -1
- package/es/utils/crypto-ts.js.map +0 -1
- package/es/utils/crypto.js +0 -358
- package/es/utils/crypto.js.map +0 -1
- package/es/utils/hd-wallet.js +0 -148
- package/es/utils/hd-wallet.js.map +0 -1
- package/es/utils/json-big.js +0 -78
- package/es/utils/json-big.js.map +0 -1
- package/es/utils/keystore.js +0 -393
- package/es/utils/keystore.js.map +0 -1
- package/es/utils/mptree.js +0 -198
- package/es/utils/mptree.js.map +0 -1
- package/es/utils/other.js +0 -93
- package/es/utils/other.js.map +0 -1
- package/es/utils/semver-satisfies.js +0 -35
- package/es/utils/semver-satisfies.js.map +0 -1
- package/es/utils/string.js.map +0 -1
- package/es/utils/swagger.js +0 -269
- package/es/utils/swagger.js.map +0 -1
- package/src/contract/aci/helpers.js +0 -140
- package/src/contract/aci/index.js +0 -191
- package/src/contract/aci/transformation.js +0 -464
- package/src/contract/index.js +0 -159
- package/src/utils/aepp-wallet-communication/schema.js +0 -110
package/src/utils/hd-wallet.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import nacl from 'tweetnacl'
|
|
2
2
|
import { full as hmac } from 'tweetnacl-auth'
|
|
3
3
|
import { fromString } from 'bip32-path'
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { decryptKey, encryptKey } from './crypto'
|
|
5
|
+
import { encode } from '../tx/builder/helpers'
|
|
6
|
+
import {
|
|
7
|
+
InvalidDerivationPathError, NotHardenedSegmentError, UnsupportedChildIndexError
|
|
8
|
+
} from './errors'
|
|
6
9
|
|
|
7
10
|
const ED25519_CURVE = Buffer.from('ed25519 seed')
|
|
8
11
|
const HARDENED_OFFSET = 0x80000000
|
|
@@ -13,7 +16,7 @@ export function derivePathFromKey (path, key) {
|
|
|
13
16
|
const segments = path === '' ? [] : fromString(path).toPathArray()
|
|
14
17
|
segments.forEach((segment, i) => {
|
|
15
18
|
if (segment < HARDENED_OFFSET) {
|
|
16
|
-
throw new
|
|
19
|
+
throw new NotHardenedSegmentError(`Segment #${i + 1} is not hardened`)
|
|
17
20
|
}
|
|
18
21
|
})
|
|
19
22
|
|
|
@@ -22,7 +25,7 @@ export function derivePathFromKey (path, key) {
|
|
|
22
25
|
|
|
23
26
|
export function derivePathFromSeed (path, seed) {
|
|
24
27
|
if (!['m', 'm/'].includes(path.slice(0, 2))) {
|
|
25
|
-
throw new
|
|
28
|
+
throw new InvalidDerivationPathError()
|
|
26
29
|
}
|
|
27
30
|
const masterKey = getMasterKeyFromSeed(seed)
|
|
28
31
|
return derivePathFromKey(path.slice(2), masterKey)
|
|
@@ -32,7 +35,7 @@ function formatAccount (keys) {
|
|
|
32
35
|
const { secretKey, publicKey } = keys
|
|
33
36
|
return {
|
|
34
37
|
secretKey: toHex(secretKey),
|
|
35
|
-
publicKey:
|
|
38
|
+
publicKey: encode(publicKey, 'ak')
|
|
36
39
|
}
|
|
37
40
|
}
|
|
38
41
|
|
|
@@ -40,10 +43,6 @@ export function getKeyPair (secretKey) {
|
|
|
40
43
|
return nacl.sign.keyPair.fromSeed(secretKey)
|
|
41
44
|
}
|
|
42
45
|
|
|
43
|
-
export function generateMnemonic () {
|
|
44
|
-
return genMnemonic()
|
|
45
|
-
}
|
|
46
|
-
|
|
47
46
|
export function getMasterKeyFromSeed (seed) {
|
|
48
47
|
const I = hmac(seed, ED25519_CURVE)
|
|
49
48
|
const IL = I.slice(0, 32)
|
|
@@ -56,7 +55,7 @@ export function getMasterKeyFromSeed (seed) {
|
|
|
56
55
|
|
|
57
56
|
export function deriveChild ({ secretKey, chainCode }, index) {
|
|
58
57
|
if (index < HARDENED_OFFSET) {
|
|
59
|
-
throw new
|
|
58
|
+
throw new UnsupportedChildIndexError(index)
|
|
60
59
|
}
|
|
61
60
|
const indexBuffer = Buffer.allocUnsafe(4)
|
|
62
61
|
indexBuffer.writeUInt32BE(index, 0)
|
|
@@ -72,11 +71,7 @@ export function deriveChild ({ secretKey, chainCode }, index) {
|
|
|
72
71
|
}
|
|
73
72
|
}
|
|
74
73
|
|
|
75
|
-
export function
|
|
76
|
-
if (!validateMnemonic(mnemonic)) {
|
|
77
|
-
throw new Error('Invalid mnemonic')
|
|
78
|
-
}
|
|
79
|
-
const seed = mnemonicToSeed(mnemonic)
|
|
74
|
+
export function generateSaveHDWalletFromSeed (seed, password) {
|
|
80
75
|
const walletKey = derivePathFromSeed('m/44h/457h', seed)
|
|
81
76
|
return {
|
|
82
77
|
secretKey: toHex(encryptKey(password, walletKey.secretKey)),
|
|
@@ -94,8 +89,7 @@ export function getSaveHDWalletAccounts (saveHDWallet, password, accountCount) {
|
|
|
94
89
|
formatAccount(getKeyPair(derivePathFromKey(`${idx}h/0h/0h`, walletKey).secretKey)))
|
|
95
90
|
}
|
|
96
91
|
|
|
97
|
-
export const
|
|
98
|
-
const seed = mnemonicToSeed(mnemonic)
|
|
92
|
+
export const getHdWalletAccountFromSeed = (seed, accountIdx) => {
|
|
99
93
|
const walletKey = derivePathFromSeed('m/44h/457h', seed)
|
|
100
94
|
const derived = derivePathFromKey(`${accountIdx}h/0h/0h`, walletKey)
|
|
101
95
|
const keyPair = getKeyPair(derived.secretKey)
|
|
@@ -104,12 +98,3 @@ export const getHdWalletAccountFromMnemonic = (mnemonic, accountIdx) => {
|
|
|
104
98
|
idx: accountIdx
|
|
105
99
|
}
|
|
106
100
|
}
|
|
107
|
-
|
|
108
|
-
export default {
|
|
109
|
-
getHdWalletAccountFromMnemonic,
|
|
110
|
-
getSaveHDWalletAccounts,
|
|
111
|
-
generateSaveHDWallet,
|
|
112
|
-
generateMnemonic,
|
|
113
|
-
deriveChild,
|
|
114
|
-
getMasterKeyFromSeed
|
|
115
|
-
}
|
package/src/utils/keystore.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import nacl from 'tweetnacl'
|
|
2
|
-
import { v4 as uuid } from 'uuid'
|
|
3
|
-
|
|
4
|
-
import {
|
|
2
|
+
import { v4 as uuid } from '@aeternity/uuid'
|
|
3
|
+
import { ArgonType, hash } from '@aeternity/argon2-browser/dist/argon2-bundled.min.js'
|
|
4
|
+
import { encode } from '../tx/builder/helpers'
|
|
5
5
|
import { str2buf } from './bytes'
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
import {
|
|
7
|
+
ArgumentError, InvalidKeyError, UnsupportedAlgorithmError, InvalidPasswordError
|
|
8
|
+
} from './errors'
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* KeyStore module
|
|
@@ -31,24 +32,18 @@ const DERIVED_KEY_FUNCTIONS = {
|
|
|
31
32
|
argon2id: deriveKeyUsingArgon2id
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
export async function deriveKeyUsingArgon2id (
|
|
35
|
-
const { memlimit_kib:
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
timeCost,
|
|
47
|
-
memoryCost * 1024,
|
|
48
|
-
sodium.crypto_pwhash_ALG_ARGON2ID13
|
|
49
|
-
)
|
|
50
|
-
return Buffer.from(result)
|
|
51
|
-
})
|
|
35
|
+
export async function deriveKeyUsingArgon2id (pass, salt, options) {
|
|
36
|
+
const { memlimit_kib: mem, opslimit: time } = options.kdf_params
|
|
37
|
+
|
|
38
|
+
const result = (await hash({
|
|
39
|
+
hashLen: 32,
|
|
40
|
+
pass,
|
|
41
|
+
salt,
|
|
42
|
+
time,
|
|
43
|
+
mem,
|
|
44
|
+
type: ArgonType.Argon2id
|
|
45
|
+
})).hash
|
|
46
|
+
return Buffer.from(result)
|
|
52
47
|
}
|
|
53
48
|
|
|
54
49
|
// CRYPTO PART
|
|
@@ -62,65 +57,66 @@ function encryptXsalsa20Poly1305 ({ plaintext, key, nonce }) {
|
|
|
62
57
|
|
|
63
58
|
function decryptXsalsa20Poly1305 ({ ciphertext, key, nonce }) {
|
|
64
59
|
const res = nacl.secretbox.open(ciphertext, nonce, key)
|
|
65
|
-
if (!res) throw new
|
|
60
|
+
if (!res) throw new InvalidPasswordError('Invalid password or nonce')
|
|
66
61
|
return res
|
|
67
62
|
}
|
|
68
63
|
|
|
69
64
|
/**
|
|
70
65
|
* Symmetric private key encryption using secret (derived) key.
|
|
71
|
-
* @param {
|
|
72
|
-
* @param {
|
|
73
|
-
* @param {
|
|
66
|
+
* @param {Buffer|string} plaintext Data to be encrypted.
|
|
67
|
+
* @param {Buffer|string} key Secret key.
|
|
68
|
+
* @param {Buffer|string} nonce Randomly generated nonce.
|
|
74
69
|
* @param {string=} algo Encryption algorithm (default: DEFAULTS.crypto.symmetric_alg).
|
|
75
|
-
* @return {
|
|
70
|
+
* @return {Buffer} Encrypted data.
|
|
76
71
|
*/
|
|
77
72
|
function encrypt (plaintext, key, nonce, algo = DEFAULTS.crypto.symmetric_alg) {
|
|
78
|
-
if (!CRYPTO_FUNCTIONS[algo]) throw new
|
|
73
|
+
if (!CRYPTO_FUNCTIONS[algo]) throw new UnsupportedAlgorithmError(algo)
|
|
79
74
|
return CRYPTO_FUNCTIONS[algo].encrypt({ plaintext, nonce, key })
|
|
80
75
|
}
|
|
81
76
|
|
|
82
77
|
/**
|
|
83
78
|
* Symmetric private key decryption using secret (derived) key.
|
|
84
|
-
* @param {
|
|
85
|
-
* @param {
|
|
86
|
-
* @param {
|
|
79
|
+
* @param {Buffer|Uint8Array} ciphertext Data to be decrypted.
|
|
80
|
+
* @param {Buffer|Uint8Array} key Secret key.
|
|
81
|
+
* @param {Buffer|Uint8Array} nonce Nonce from key-object.
|
|
87
82
|
* @param {string=} algo Encryption algorithm.
|
|
88
|
-
* @return {
|
|
83
|
+
* @return {Buffer} Decrypted data.
|
|
89
84
|
*/
|
|
90
85
|
function decrypt (ciphertext, key, nonce, algo) {
|
|
91
|
-
if (!CRYPTO_FUNCTIONS[algo]) throw new
|
|
86
|
+
if (!CRYPTO_FUNCTIONS[algo]) throw new UnsupportedAlgorithmError(algo)
|
|
92
87
|
return CRYPTO_FUNCTIONS[algo].decrypt({ ciphertext, nonce, key })
|
|
93
88
|
}
|
|
94
89
|
|
|
95
90
|
/**
|
|
96
91
|
* Derive secret key from password with key derivation function.
|
|
97
92
|
* @param {string} password User-supplied password.
|
|
98
|
-
* @param {
|
|
93
|
+
* @param {Buffer|Uint8Array} nonce Randomly generated nonce.
|
|
99
94
|
* @param {Object=} options Encryption parameters.
|
|
100
95
|
* @param {string=} options.kdf Key derivation function (default: DEFAULTS.crypto.kdf).
|
|
101
96
|
* @param {Object=} options.kdf_params KDF parameters (default: DEFAULTS.crypto.kdf_params).
|
|
102
|
-
* @return {
|
|
97
|
+
* @return {Buffer} Secret key derived from password.
|
|
103
98
|
*/
|
|
104
99
|
async function deriveKey (password, nonce, options = {
|
|
105
100
|
kdf_params: DEFAULTS.crypto.kdf_params,
|
|
106
101
|
kdf: DEFAULTS.crypto.kdf
|
|
107
102
|
}) {
|
|
108
|
-
if (
|
|
109
|
-
|
|
110
|
-
}
|
|
103
|
+
if (!nonce) throw new ArgumentError('nonce', 'provided', nonce)
|
|
104
|
+
if (password == null) throw new ArgumentError('password', 'provided', password)
|
|
111
105
|
|
|
112
|
-
if (!Object.prototype.hasOwnProperty.call(DERIVED_KEY_FUNCTIONS, options.kdf))
|
|
106
|
+
if (!Object.prototype.hasOwnProperty.call(DERIVED_KEY_FUNCTIONS, options.kdf)) {
|
|
107
|
+
throw new UnsupportedAlgorithmError(options.kdf)
|
|
108
|
+
}
|
|
113
109
|
|
|
114
110
|
return DERIVED_KEY_FUNCTIONS[options.kdf](password, nonce, options)
|
|
115
111
|
}
|
|
116
112
|
|
|
117
113
|
/**
|
|
118
114
|
* Assemble key data object in secret-storage format.
|
|
119
|
-
* @param {
|
|
120
|
-
* @param {
|
|
121
|
-
* @param {
|
|
122
|
-
* @param {
|
|
123
|
-
* @param {
|
|
115
|
+
* @param {Buffer} name Key name.
|
|
116
|
+
* @param {Buffer} derivedKey Password-derived secret key.
|
|
117
|
+
* @param {Buffer} privateKey Private key.
|
|
118
|
+
* @param {Buffer} nonce Randomly generated 24byte nonce.
|
|
119
|
+
* @param {Buffer} salt Randomly generated 16byte salt.
|
|
124
120
|
* @param {Object=} options Encryption parameters.
|
|
125
121
|
* @param {string=} options.kdf Key derivation function (default: argon2id).
|
|
126
122
|
* @param {string=} options.cipher Symmetric cipher (default: constants.cipher).
|
|
@@ -148,7 +144,7 @@ function marshal (name, derivedKey, privateKey, nonce, salt, options = {}) {
|
|
|
148
144
|
export function getAddressFromPriv (secret) {
|
|
149
145
|
const keys = nacl.sign.keyPair.fromSecretKey(str2buf(secret))
|
|
150
146
|
const publicBuffer = Buffer.from(keys.publicKey)
|
|
151
|
-
return
|
|
147
|
+
return encode(publicBuffer, 'ak')
|
|
152
148
|
}
|
|
153
149
|
|
|
154
150
|
/**
|
|
@@ -170,7 +166,7 @@ export async function recover (password, keyObject) {
|
|
|
170
166
|
nonce,
|
|
171
167
|
keyObject.crypto.symmetric_alg
|
|
172
168
|
)
|
|
173
|
-
if (!key) throw new
|
|
169
|
+
if (!key) throw new InvalidPasswordError('Invalid password')
|
|
174
170
|
|
|
175
171
|
if (Buffer.from(key).length === 64) return Buffer.from(key).toString('hex')
|
|
176
172
|
return Buffer.from(key).toString('utf-8')
|
|
@@ -180,7 +176,7 @@ export async function recover (password, keyObject) {
|
|
|
180
176
|
* Export private key to keystore secret-storage format.
|
|
181
177
|
* @param {String} name Key name.
|
|
182
178
|
* @param {String} password User-supplied password.
|
|
183
|
-
* @param {String} privateKey Private key.
|
|
179
|
+
* @param {String|Buffer} privateKey Private key as hex-string or a Buffer.
|
|
184
180
|
* @param {Buffer} nonce Randomly generated 24byte nonce.
|
|
185
181
|
* @param {Buffer} salt Randomly generated 16byte salt.
|
|
186
182
|
* @param {Object=} options Encryption parameters.
|
|
@@ -189,12 +185,19 @@ export async function recover (password, keyObject) {
|
|
|
189
185
|
* @param {Object=} options.kdfparams KDF parameters (default: constants.<kdf>).
|
|
190
186
|
* @return {Object}
|
|
191
187
|
*/
|
|
192
|
-
export async function dump (
|
|
188
|
+
export async function dump (
|
|
189
|
+
name,
|
|
190
|
+
password,
|
|
191
|
+
privateKey,
|
|
192
|
+
nonce = nacl.randomBytes(24),
|
|
193
|
+
salt = nacl.randomBytes(16),
|
|
194
|
+
options = {}
|
|
195
|
+
) {
|
|
193
196
|
const opt = Object.assign({}, DEFAULTS.crypto, options)
|
|
194
197
|
return marshal(
|
|
195
198
|
name,
|
|
196
199
|
await deriveKey(password, salt, opt),
|
|
197
|
-
privateKey,
|
|
200
|
+
typeof privateKey === 'string' ? Buffer.from(privateKey, 'hex') : privateKey,
|
|
198
201
|
nonce,
|
|
199
202
|
salt,
|
|
200
203
|
opt
|
|
@@ -206,10 +209,11 @@ export function validateKeyObj (obj) {
|
|
|
206
209
|
const cryptoKeys = ['cipher_params', 'ciphertext', 'symmetric_alg', 'kdf', 'kdf_params']
|
|
207
210
|
|
|
208
211
|
const missingRootKeys = root.filter(key => !Object.prototype.hasOwnProperty.call(obj, key))
|
|
209
|
-
if (missingRootKeys.length) throw new
|
|
212
|
+
if (missingRootKeys.length) throw new InvalidKeyError(`Invalid key file format. Require properties: ${missingRootKeys}`)
|
|
210
213
|
|
|
211
|
-
const missingCryptoKeys = cryptoKeys
|
|
212
|
-
|
|
214
|
+
const missingCryptoKeys = cryptoKeys
|
|
215
|
+
.filter(key => !Object.prototype.hasOwnProperty.call(obj.crypto, key))
|
|
216
|
+
if (missingCryptoKeys.length) throw new InvalidKeyError(`Invalid key file format. Require properties: ${missingCryptoKeys}`)
|
|
213
217
|
|
|
214
218
|
return true
|
|
215
219
|
}
|
package/src/utils/mptree.ts
CHANGED
|
@@ -18,6 +18,12 @@
|
|
|
18
18
|
import { encode as rlpEncode } from 'rlp'
|
|
19
19
|
import type { Input } from 'rlp'
|
|
20
20
|
import { hash } from './crypto-ts'
|
|
21
|
+
import {
|
|
22
|
+
MerkleTreeHashMismatchError,
|
|
23
|
+
MissingNodeInTreeError,
|
|
24
|
+
UnknownPathNibbleError,
|
|
25
|
+
UnknownNodeLengthError
|
|
26
|
+
} from './errors'
|
|
21
27
|
|
|
22
28
|
enum NodeType {
|
|
23
29
|
Branch,
|
|
@@ -47,9 +53,9 @@ export default class MPTree {
|
|
|
47
53
|
binary[1].map((node) => [node[0].toString('hex'), node[1]])
|
|
48
54
|
)
|
|
49
55
|
|
|
50
|
-
if (this.nodes[this.rootHash] === undefined) throw new
|
|
56
|
+
if (this.nodes[this.rootHash] === undefined) throw new MissingNodeInTreeError('Can\'t find a node by root hash')
|
|
51
57
|
Object.entries(this.nodes).forEach(([key, node]) => {
|
|
52
|
-
if (MPTree.nodeHash(node) !== key) throw new
|
|
58
|
+
if (MPTree.nodeHash(node) !== key) throw new MerkleTreeHashMismatchError()
|
|
53
59
|
const { type, payload } = MPTree.parseNode(node)
|
|
54
60
|
switch (type) {
|
|
55
61
|
case NodeType.Branch:
|
|
@@ -58,13 +64,13 @@ export default class MPTree {
|
|
|
58
64
|
.filter(n => n.length)
|
|
59
65
|
.forEach((n) => {
|
|
60
66
|
if (this.nodes[n.toString('hex')] === undefined) {
|
|
61
|
-
throw new
|
|
67
|
+
throw new MissingNodeInTreeError('Can\'t find a node by hash in branch node')
|
|
62
68
|
}
|
|
63
69
|
})
|
|
64
70
|
break
|
|
65
71
|
case NodeType.Extension:
|
|
66
72
|
if (this.nodes[payload[0].toString('hex')] === undefined) {
|
|
67
|
-
throw new
|
|
73
|
+
throw new MissingNodeInTreeError('Can\'t find a node by hash in extension node')
|
|
68
74
|
}
|
|
69
75
|
}
|
|
70
76
|
})
|
|
@@ -81,13 +87,13 @@ export default class MPTree {
|
|
|
81
87
|
case 2: {
|
|
82
88
|
const path = node[0].toString('hex')
|
|
83
89
|
const nibble = parseInt(path[0], 16)
|
|
84
|
-
if (nibble > 3) throw new
|
|
90
|
+
if (nibble > 3) throw new UnknownPathNibbleError(nibble)
|
|
85
91
|
const type = nibble <= 1 ? NodeType.Extension : NodeType.Leaf
|
|
86
92
|
const slice = [0, 2].includes(nibble) ? 2 : 1
|
|
87
93
|
return { type, payload: [node[1]], path: path.slice(slice) }
|
|
88
94
|
}
|
|
89
95
|
default:
|
|
90
|
-
throw new
|
|
96
|
+
throw new UnknownNodeLengthError(node.length)
|
|
91
97
|
}
|
|
92
98
|
}
|
|
93
99
|
|
package/src/utils/other.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { snakeToPascal, pascalToSnake } from './string'
|
|
2
2
|
|
|
3
|
-
export const pause = async (duration: number): Promise<
|
|
3
|
+
export const pause = async (duration: number): Promise<void> =>
|
|
4
4
|
await new Promise(resolve => setTimeout(resolve, duration))
|
|
5
5
|
|
|
6
6
|
export const mapObject = (
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export default function (version: string, geVersion: string, ltVersion: string): boolean {
|
|
2
2
|
const getComponents = (v: string): number[] => v
|
|
3
|
-
.split(
|
|
3
|
+
.split(/[-+]/)[0].split('.').map(i => +i)
|
|
4
4
|
|
|
5
5
|
const versionComponents = getComponents(version)
|
|
6
6
|
const geComponents = getComponents(geVersion)
|
package/src/utils/swagger.js
CHANGED
|
@@ -25,6 +25,8 @@ import SwaggerClient from 'swagger-client'
|
|
|
25
25
|
import fetch from 'cross-fetch'
|
|
26
26
|
import JsonBig from './json-big'
|
|
27
27
|
import { snakizeKeys, pascalizeKeys, mapObject, filterObject } from './other'
|
|
28
|
+
import { ArgumentCountMismatchError } from './errors'
|
|
29
|
+
import { snakeToPascal } from './string'
|
|
28
30
|
|
|
29
31
|
let warnedAboutInternalApiUsage = false
|
|
30
32
|
|
|
@@ -39,40 +41,52 @@ let warnedAboutInternalApiUsage = false
|
|
|
39
41
|
* @param {String} [options.internalUrl] - Node internal URL
|
|
40
42
|
* @param {Boolean} [options.disableBigNumbers]
|
|
41
43
|
* @param {Boolean} [options.disableCaseConversion]
|
|
44
|
+
* @param {Function} [options.responseInterceptor]
|
|
42
45
|
* @return {Object} Swagger client
|
|
43
46
|
* @example (await genSwaggerClient('https://mainnet.aeternity.io/api')).getAccountByPubkey('ak_jupBUgZNbcC4krDLR3tAkw1iBZoBbkNeShAq4atBtpFWmz36r')
|
|
44
47
|
*/
|
|
45
|
-
export default async (
|
|
48
|
+
export default async (
|
|
49
|
+
specUrl,
|
|
50
|
+
{ spec, internalUrl, disableBigNumbers, disableCaseConversion, responseInterceptor } = {}
|
|
51
|
+
) => {
|
|
46
52
|
spec = spec || await (await fetch(specUrl)).json()
|
|
47
53
|
const jsonImp = disableBigNumbers ? JSON : JsonBig
|
|
48
54
|
|
|
49
|
-
if (
|
|
50
|
-
[
|
|
51
|
-
'https://compiler.aepps.com',
|
|
52
|
-
'https://latest.compiler.aepps.com'
|
|
53
|
-
].includes(new URL(specUrl).origin) ||
|
|
54
|
-
new URL(specUrl).origin.includes('https://')
|
|
55
|
-
) {
|
|
56
|
-
spec.schemes = ['https']
|
|
57
|
-
}
|
|
58
|
-
|
|
59
55
|
const [external, internal] = await Promise.all([specUrl, internalUrl].map((url) => {
|
|
60
56
|
if (!url) return null
|
|
57
|
+
const pendingGetRequests = {}
|
|
61
58
|
return SwaggerClient({
|
|
62
59
|
url,
|
|
63
60
|
spec,
|
|
61
|
+
requestInterceptor: request => {
|
|
62
|
+
if (request.method !== 'GET') return
|
|
63
|
+
return {
|
|
64
|
+
...request,
|
|
65
|
+
userFetch: async (url, request) => {
|
|
66
|
+
const key = JSON.stringify({ ...request, url })
|
|
67
|
+
pendingGetRequests[key] ??= fetch(url, request)
|
|
68
|
+
try {
|
|
69
|
+
return (await pendingGetRequests[key]).clone()
|
|
70
|
+
} finally {
|
|
71
|
+
delete pendingGetRequests[key]
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
},
|
|
64
76
|
responseInterceptor: response => {
|
|
65
|
-
if (
|
|
77
|
+
if (response.text === '' || response.text?.size === 0) return response
|
|
66
78
|
const body = jsonImp.parse(response.text)
|
|
67
|
-
|
|
79
|
+
Object.assign(response, {
|
|
68
80
|
body: disableCaseConversion ? body : pascalizeKeys(body)
|
|
69
81
|
})
|
|
82
|
+
return (responseInterceptor && responseInterceptor(response)) || response
|
|
70
83
|
}
|
|
71
84
|
})
|
|
72
85
|
}))
|
|
73
86
|
|
|
74
|
-
const combinedApi =
|
|
75
|
-
|
|
87
|
+
const combinedApi = Object.assign(
|
|
88
|
+
{},
|
|
89
|
+
...external.apis.external ? [external.apis.external] : Object.values(external.apis),
|
|
76
90
|
mapObject(internal?.apis.internal || {}, ([key, handler]) => [key, (...args) => {
|
|
77
91
|
if (!warnedAboutInternalApiUsage) {
|
|
78
92
|
console.warn(
|
|
@@ -84,43 +98,59 @@ export default async (specUrl, { spec, internalUrl, disableBigNumbers, disableCa
|
|
|
84
98
|
}
|
|
85
99
|
return handler(...args)
|
|
86
100
|
}])
|
|
87
|
-
|
|
101
|
+
)
|
|
88
102
|
|
|
89
103
|
const opSpecs = Object.values(spec.paths)
|
|
90
104
|
.map(paths => Object.values(paths))
|
|
91
105
|
.flat()
|
|
92
106
|
.reduce((acc, n) => ({ ...acc, [n.operationId]: n }), {})
|
|
93
107
|
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
108
|
+
const requestQueues = {}
|
|
109
|
+
const api = mapObject(combinedApi, ([opId, handler]) => {
|
|
110
|
+
const functionName = opId.slice(0, 1).toLowerCase() + snakeToPascal(opId.slice(1))
|
|
111
|
+
return [
|
|
112
|
+
functionName,
|
|
113
|
+
async (...args) => {
|
|
114
|
+
const opSpec = opSpecs[opId]
|
|
115
|
+
const parameters = [
|
|
116
|
+
...opSpec.parameters,
|
|
117
|
+
...opSpec.requestBody
|
|
118
|
+
? [{
|
|
119
|
+
required: opSpec.requestBody.required,
|
|
120
|
+
schema: Object.values(opSpec.requestBody.content)[0].schema,
|
|
121
|
+
name: '__requestBody'
|
|
122
|
+
}]
|
|
123
|
+
: []
|
|
124
|
+
]
|
|
125
|
+
const required = parameters.filter(param => param.required).map(p => p.name)
|
|
126
|
+
if (![0, 1].includes(args.length - required.length)) {
|
|
127
|
+
throw new ArgumentCountMismatchError(functionName, required.length, args.length)
|
|
128
|
+
}
|
|
129
|
+
const values = required.reduce(
|
|
130
|
+
(acc, req, idx) => ({ ...acc, [req]: args[idx] }),
|
|
131
|
+
args[required.length] || {}
|
|
118
132
|
)
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
133
|
+
const { __requestBody, __queue, ...stringified } = mapObject(values, ([param, value]) => {
|
|
134
|
+
if (typeof value !== 'object') return [param, value]
|
|
135
|
+
const rootKeys = Object.keys(parameters.find(p => p.name === param).schema.properties)
|
|
136
|
+
const filteredValue = filterObject(
|
|
137
|
+
disableCaseConversion ? value : snakizeKeys(value),
|
|
138
|
+
([key]) => rootKeys.includes(key)
|
|
139
|
+
)
|
|
140
|
+
return [param, jsonImp.stringify(filteredValue)]
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
const request = async () => (await handler(stringified, { requestBody: __requestBody }))
|
|
144
|
+
.body
|
|
145
|
+
if (!__queue) return request()
|
|
146
|
+
const res = (requestQueues[__queue] ?? Promise.resolve()).then(request, request)
|
|
147
|
+
// TODO: remove after fixing https://github.com/aeternity/aeternity/issues/3803
|
|
148
|
+
// gap to ensure that node won't reject the nonce
|
|
149
|
+
requestQueues[__queue] = res.then(() => new Promise(resolve => setTimeout(resolve, 750)))
|
|
150
|
+
return res
|
|
151
|
+
}
|
|
152
|
+
]
|
|
153
|
+
})
|
|
124
154
|
|
|
125
155
|
return Object.assign(external, { api })
|
|
126
156
|
}
|