@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/tx/validator.ts
CHANGED
|
@@ -1,226 +1,230 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import BigNumber from 'bignumber.js'
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
import { hash, verify } from '../utils/crypto';
|
|
4
3
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
import
|
|
4
|
+
CtVersion,
|
|
5
|
+
PROTOCOL_VM_ABI,
|
|
6
|
+
RawTxObject,
|
|
7
|
+
TxParamsCommon,
|
|
8
|
+
TxSchema,
|
|
9
|
+
TxTypeSchemas,
|
|
10
|
+
} from './builder/schema';
|
|
11
|
+
import { Tag } from './builder/constants';
|
|
12
|
+
import { TxUnpacked, unpackTx } from './builder';
|
|
13
|
+
import { UnsupportedProtocolError } from '../utils/errors';
|
|
14
|
+
import { concatBuffers, isKeyOfObject } from '../utils/other';
|
|
15
|
+
import {
|
|
16
|
+
decode, encode, Encoded, Encoding,
|
|
17
|
+
} from '../utils/encoder';
|
|
18
|
+
import Node from '../Node';
|
|
13
19
|
|
|
14
20
|
interface Account {
|
|
15
|
-
balance: bigint
|
|
16
|
-
id:
|
|
17
|
-
nonce: number
|
|
21
|
+
balance: bigint;
|
|
22
|
+
id: Encoded.AccountAddress;
|
|
23
|
+
nonce: number;
|
|
18
24
|
}
|
|
19
25
|
|
|
20
26
|
export interface ValidatorResult {
|
|
21
|
-
message: string
|
|
22
|
-
key: string
|
|
23
|
-
checkedKeys: string[]
|
|
27
|
+
message: string;
|
|
28
|
+
key: string;
|
|
29
|
+
checkedKeys: string[];
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
type Validator = (
|
|
27
33
|
tx: {
|
|
28
|
-
encodedTx: TxUnpacked<TxSchema
|
|
29
|
-
signatures: Buffer[]
|
|
34
|
+
encodedTx: TxUnpacked<TxSchema>;
|
|
35
|
+
signatures: Buffer[];
|
|
30
36
|
tx: TxUnpacked<TxSchema> & {
|
|
31
|
-
tx: TxTypeSchemas[
|
|
32
|
-
}
|
|
33
|
-
nonce?: number
|
|
34
|
-
ttl?: number
|
|
35
|
-
amount?: number
|
|
36
|
-
fee?: number
|
|
37
|
-
nameFee?: number
|
|
38
|
-
ctVersion?: Partial<CtVersion
|
|
39
|
-
abiVersion?: number
|
|
40
|
-
contractId?:
|
|
37
|
+
tx: TxTypeSchemas[Tag.SignedTx];
|
|
38
|
+
};
|
|
39
|
+
nonce?: number;
|
|
40
|
+
ttl?: number;
|
|
41
|
+
amount?: number;
|
|
42
|
+
fee?: number;
|
|
43
|
+
nameFee?: number;
|
|
44
|
+
ctVersion?: Partial<CtVersion>;
|
|
45
|
+
abiVersion?: number;
|
|
46
|
+
contractId?: Encoded.ContractAddress;
|
|
41
47
|
},
|
|
42
48
|
options: {
|
|
43
|
-
account?: Account
|
|
44
|
-
nodeNetworkId: string
|
|
45
|
-
parentTxTypes:
|
|
46
|
-
node: Node
|
|
47
|
-
txType:
|
|
48
|
-
height: number
|
|
49
|
-
consensusProtocolVersion: number
|
|
49
|
+
account?: Account;
|
|
50
|
+
nodeNetworkId: string;
|
|
51
|
+
parentTxTypes: Tag[];
|
|
52
|
+
node: Node;
|
|
53
|
+
txType: Tag;
|
|
54
|
+
height: number;
|
|
55
|
+
consensusProtocolVersion: number;
|
|
50
56
|
}
|
|
51
|
-
) => ValidatorResult[] | Promise<ValidatorResult[]
|
|
57
|
+
) => ValidatorResult[] | Promise<ValidatorResult[]>;
|
|
58
|
+
|
|
59
|
+
const validators: Validator[] = [];
|
|
52
60
|
|
|
53
|
-
const
|
|
61
|
+
const getSenderAddress = (
|
|
62
|
+
tx: TxParamsCommon | RawTxObject<TxSchema>,
|
|
63
|
+
): Encoded.AccountAddress | undefined => [
|
|
64
|
+
'senderId', 'accountId', 'ownerId', 'callerId',
|
|
65
|
+
'oracleId', 'fromId', 'initiator', 'gaId', 'payerId',
|
|
66
|
+
]
|
|
67
|
+
.map((key: keyof TxSchema) => tx[key])
|
|
68
|
+
.filter((a) => a)
|
|
69
|
+
.map((a) => a?.toString().replace(/^ok_/, 'ak_'))[0] as Encoded.AccountAddress | undefined;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Transaction Validator
|
|
73
|
+
* This function validates some of transaction properties,
|
|
74
|
+
* to make sure it can be posted it to the chain
|
|
75
|
+
* @category transaction builder
|
|
76
|
+
* @param transaction - Base64Check-encoded transaction
|
|
77
|
+
* @param node - Node to validate transaction against
|
|
78
|
+
* @param parentTxTypes - Types of parent transactions
|
|
79
|
+
* @returns Array with verification errors
|
|
80
|
+
* @example const errors = await verifyTransaction(transaction, node)
|
|
81
|
+
*/
|
|
82
|
+
export default async function verifyTransaction(
|
|
83
|
+
transaction: Encoded.Transaction | Encoded.Poi,
|
|
84
|
+
node: Node,
|
|
85
|
+
parentTxTypes: Tag[] = [],
|
|
86
|
+
): Promise<ValidatorResult[]> {
|
|
87
|
+
const { tx, txType } = unpackTx<Tag.SignedTx>(transaction);
|
|
88
|
+
const address = getSenderAddress(tx)
|
|
89
|
+
?? (txType === Tag.SignedTx ? getSenderAddress(tx.encodedTx.tx) : undefined);
|
|
90
|
+
const [account, { height }, { consensusProtocolVersion, nodeNetworkId }] = await Promise.all([
|
|
91
|
+
address == null
|
|
92
|
+
? undefined
|
|
93
|
+
: node.getAccountByPubkey(address)
|
|
94
|
+
.catch(() => ({ id: address, balance: 0n, nonce: 0 }))
|
|
95
|
+
// TODO: remove after fixing https://github.com/aeternity/aepp-sdk-js/issues/1537
|
|
96
|
+
.then((acc) => ({ ...acc, id: acc.id as Encoded.AccountAddress })),
|
|
97
|
+
node.getCurrentKeyBlockHeight(),
|
|
98
|
+
node.getNodeInfo(),
|
|
99
|
+
]);
|
|
100
|
+
|
|
101
|
+
return (await Promise.all(
|
|
102
|
+
validators.map((v) => v(
|
|
103
|
+
tx as any,
|
|
104
|
+
{
|
|
105
|
+
txType, node, account, height, consensusProtocolVersion, nodeNetworkId, parentTxTypes,
|
|
106
|
+
},
|
|
107
|
+
)),
|
|
108
|
+
)).flat();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
validators.push(
|
|
54
112
|
({ encodedTx, signatures }, { account, nodeNetworkId, parentTxTypes }) => {
|
|
55
|
-
if ((encodedTx ?? signatures) === undefined) return []
|
|
56
|
-
if (account == null) return []
|
|
57
|
-
if (signatures.length !== 1) return [] // TODO: Support multisignature?
|
|
113
|
+
if ((encodedTx ?? signatures) === undefined) return [];
|
|
114
|
+
if (account == null) return [];
|
|
115
|
+
if (signatures.length !== 1) return []; // TODO: Support multisignature?
|
|
58
116
|
const prefix = Buffer.from([
|
|
59
117
|
nodeNetworkId,
|
|
60
|
-
...parentTxTypes.includes(
|
|
61
|
-
].join('-'))
|
|
62
|
-
const txWithNetworkId = concatBuffers([prefix, encodedTx.rlpEncoded])
|
|
63
|
-
const txHashWithNetworkId = concatBuffers([prefix, hash(encodedTx.rlpEncoded)])
|
|
64
|
-
const decodedPub = decode(account.id)
|
|
65
|
-
if (verify(txWithNetworkId, signatures[0], decodedPub)
|
|
66
|
-
verify(txHashWithNetworkId, signatures[0], decodedPub)) return []
|
|
118
|
+
...parentTxTypes.includes(Tag.PayingForTx) ? ['inner_tx'] : [],
|
|
119
|
+
].join('-'));
|
|
120
|
+
const txWithNetworkId = concatBuffers([prefix, encodedTx.rlpEncoded]);
|
|
121
|
+
const txHashWithNetworkId = concatBuffers([prefix, hash(encodedTx.rlpEncoded)]);
|
|
122
|
+
const decodedPub = decode(account.id);
|
|
123
|
+
if (verify(txWithNetworkId, signatures[0], decodedPub)
|
|
124
|
+
|| verify(txHashWithNetworkId, signatures[0], decodedPub)) return [];
|
|
67
125
|
return [{
|
|
68
|
-
message: 'Signature cannot be verified, please ensure that you transaction have'
|
|
69
|
-
' the correct prefix and the correct private key for the sender address',
|
|
126
|
+
message: 'Signature cannot be verified, please ensure that you transaction have'
|
|
127
|
+
+ ' the correct prefix and the correct private key for the sender address',
|
|
70
128
|
key: 'InvalidSignature',
|
|
71
|
-
checkedKeys: ['encodedTx', 'signatures']
|
|
72
|
-
}]
|
|
129
|
+
checkedKeys: ['encodedTx', 'signatures'],
|
|
130
|
+
}];
|
|
73
131
|
},
|
|
74
132
|
async ({ encodedTx, tx }, { node, parentTxTypes, txType }) => {
|
|
75
|
-
if ((encodedTx ?? tx) === undefined) return []
|
|
76
|
-
return
|
|
77
|
-
encode((encodedTx ?? tx).rlpEncoded,
|
|
133
|
+
if ((encodedTx ?? tx) === undefined) return [];
|
|
134
|
+
return verifyTransaction(
|
|
135
|
+
encode((encodedTx ?? tx).rlpEncoded, Encoding.Transaction),
|
|
78
136
|
node,
|
|
79
|
-
[...parentTxTypes, txType]
|
|
80
|
-
)
|
|
81
|
-
},
|
|
82
|
-
(tx: any, { txType }) => {
|
|
83
|
-
if (tx.fee === undefined) return []
|
|
84
|
-
const minFee = calculateMinFee(txType, { params: tx, vsn: tx.VSN })
|
|
85
|
-
if (minFee.lte(tx.fee)) return []
|
|
86
|
-
return [{
|
|
87
|
-
message: `Fee ${tx.fee as string} is too low, minimum fee for this transaction is ${minFee.toString()}`,
|
|
88
|
-
key: 'InsufficientFee',
|
|
89
|
-
checkedKeys: ['fee']
|
|
90
|
-
}]
|
|
137
|
+
[...parentTxTypes, txType],
|
|
138
|
+
);
|
|
91
139
|
},
|
|
92
140
|
({ ttl }, { height }) => {
|
|
93
|
-
if (ttl === undefined) return []
|
|
94
|
-
ttl = +ttl
|
|
95
|
-
if (ttl === 0 || ttl >= height) return []
|
|
141
|
+
if (ttl === undefined) return [];
|
|
142
|
+
ttl = +ttl;
|
|
143
|
+
if (ttl === 0 || ttl >= height) return [];
|
|
96
144
|
return [{
|
|
97
145
|
message: `TTL ${ttl} is already expired, current height is ${height}`,
|
|
98
146
|
key: 'ExpiredTTL',
|
|
99
|
-
checkedKeys: ['ttl']
|
|
100
|
-
}]
|
|
147
|
+
checkedKeys: ['ttl'],
|
|
148
|
+
}];
|
|
101
149
|
},
|
|
102
|
-
({
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
150
|
+
({
|
|
151
|
+
amount, fee, nameFee, tx,
|
|
152
|
+
}, { account, parentTxTypes, txType }) => {
|
|
153
|
+
if (account == null) return [];
|
|
154
|
+
if ((amount ?? fee ?? nameFee) === undefined) return [];
|
|
155
|
+
fee ??= 0;
|
|
106
156
|
const cost = new BigNumber(fee).plus(nameFee ?? 0).plus(amount ?? 0)
|
|
107
|
-
.plus(txType ===
|
|
108
|
-
.minus(parentTxTypes.includes(
|
|
109
|
-
if (cost.lte(account.balance.toString())) return []
|
|
157
|
+
.plus(txType === Tag.PayingForTx ? (tx.tx.encodedTx.tx).fee : 0)
|
|
158
|
+
.minus(parentTxTypes.includes(Tag.PayingForTx) ? fee : 0);
|
|
159
|
+
if (cost.lte(account.balance.toString())) return [];
|
|
110
160
|
return [{
|
|
111
161
|
message: `Account balance ${account.balance.toString()} is not enough to execute the transaction that costs ${cost.toFixed()}`,
|
|
112
162
|
key: 'InsufficientBalance',
|
|
113
|
-
checkedKeys: ['amount', 'fee', 'nameFee']
|
|
114
|
-
}]
|
|
163
|
+
checkedKeys: ['amount', 'fee', 'nameFee'],
|
|
164
|
+
}];
|
|
115
165
|
},
|
|
116
166
|
({ nonce }, { account, parentTxTypes }) => {
|
|
117
|
-
if (nonce == null || account == null || parentTxTypes.includes(
|
|
118
|
-
nonce = +nonce
|
|
119
|
-
const validNonce = account.nonce + 1
|
|
120
|
-
if (nonce === validNonce) return []
|
|
167
|
+
if (nonce == null || account == null || parentTxTypes.includes(Tag.GaMetaTx)) return [];
|
|
168
|
+
nonce = +nonce;
|
|
169
|
+
const validNonce = account.nonce + 1;
|
|
170
|
+
if (nonce === validNonce) return [];
|
|
121
171
|
return [{
|
|
122
172
|
...nonce < validNonce
|
|
123
173
|
? {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
174
|
+
message: `Nonce ${nonce} is already used, valid nonce is ${validNonce}`,
|
|
175
|
+
key: 'NonceAlreadyUsed',
|
|
176
|
+
}
|
|
127
177
|
: {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
checkedKeys: ['nonce']
|
|
132
|
-
}]
|
|
178
|
+
message: `Nonce ${nonce} is too high, valid nonce is ${validNonce}`,
|
|
179
|
+
key: 'NonceHigh',
|
|
180
|
+
},
|
|
181
|
+
checkedKeys: ['nonce'],
|
|
182
|
+
}];
|
|
133
183
|
},
|
|
134
184
|
({ ctVersion, abiVersion }, { txType, consensusProtocolVersion }) => {
|
|
135
185
|
if (!isKeyOfObject(consensusProtocolVersion, PROTOCOL_VM_ABI)) {
|
|
136
|
-
throw new UnsupportedProtocolError(`Unsupported protocol: ${consensusProtocolVersion}`)
|
|
186
|
+
throw new UnsupportedProtocolError(`Unsupported protocol: ${consensusProtocolVersion}`);
|
|
137
187
|
}
|
|
138
|
-
const protocol = PROTOCOL_VM_ABI[consensusProtocolVersion]
|
|
188
|
+
const protocol = PROTOCOL_VM_ABI[consensusProtocolVersion];
|
|
139
189
|
|
|
140
190
|
// If not contract create tx
|
|
141
|
-
if (ctVersion == null) ctVersion = { abiVersion }
|
|
142
|
-
const txProtocol = protocol[txType as keyof typeof protocol]
|
|
143
|
-
if (txProtocol == null) return []
|
|
191
|
+
if (ctVersion == null) ctVersion = { abiVersion };
|
|
192
|
+
const txProtocol = protocol[txType as keyof typeof protocol];
|
|
193
|
+
if (txProtocol == null) return [];
|
|
144
194
|
if (Object.entries(ctVersion).some(
|
|
145
195
|
([
|
|
146
196
|
key,
|
|
147
|
-
value
|
|
197
|
+
value,
|
|
148
198
|
]: [
|
|
149
199
|
key:keyof typeof txProtocol,
|
|
150
|
-
value:any]) => !(txProtocol[key].includes(+value as never))
|
|
200
|
+
value:any]) => !(txProtocol[key].includes(+value as never)),
|
|
201
|
+
)) {
|
|
151
202
|
return [{
|
|
152
203
|
message: `ABI/VM version ${JSON.stringify(ctVersion)} is wrong, supported is: ${JSON.stringify(txProtocol)}`,
|
|
153
204
|
key: 'VmAndAbiVersionMismatch',
|
|
154
|
-
checkedKeys: ['ctVersion', 'abiVersion']
|
|
155
|
-
}]
|
|
205
|
+
checkedKeys: ['ctVersion', 'abiVersion'],
|
|
206
|
+
}];
|
|
156
207
|
}
|
|
157
|
-
return []
|
|
208
|
+
return [];
|
|
158
209
|
},
|
|
159
210
|
async ({ contractId }, { txType, node }) => {
|
|
160
|
-
if (
|
|
161
|
-
contractId = contractId as
|
|
211
|
+
if (Tag.ContractCallTx !== txType) return [];
|
|
212
|
+
contractId = contractId as Encoded.ContractAddress;
|
|
162
213
|
try {
|
|
163
|
-
const { active } = await node.getContract(contractId)
|
|
164
|
-
if (active) return []
|
|
214
|
+
const { active } = await node.getContract(contractId);
|
|
215
|
+
if (active) return [];
|
|
165
216
|
return [{
|
|
166
217
|
message: `Contract ${contractId} is not active`,
|
|
167
218
|
key: 'ContractNotActive',
|
|
168
|
-
checkedKeys: ['contractId']
|
|
169
|
-
}]
|
|
219
|
+
checkedKeys: ['contractId'],
|
|
220
|
+
}];
|
|
170
221
|
} catch (error) {
|
|
171
|
-
if (error.response?.parsedBody?.reason == null) throw error
|
|
222
|
+
if (error.response?.parsedBody?.reason == null) throw error;
|
|
172
223
|
return [{
|
|
173
224
|
message: error.response.parsedBody.reason,
|
|
174
225
|
key: 'ContractNotFound',
|
|
175
|
-
checkedKeys: ['contractId']
|
|
176
|
-
}]
|
|
226
|
+
checkedKeys: ['contractId'],
|
|
227
|
+
}];
|
|
177
228
|
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const getSenderAddress = (
|
|
182
|
-
tx: TxParamsCommon | RawTxObject<TxSchema>
|
|
183
|
-
): EncodedData<'ak'> | undefined => [
|
|
184
|
-
'senderId', 'accountId', 'ownerId', 'callerId',
|
|
185
|
-
'oracleId', 'fromId', 'initiator', 'gaId', 'payerId'
|
|
186
|
-
]
|
|
187
|
-
.map((key: keyof TxSchema) => tx[key])
|
|
188
|
-
.filter(a => a)
|
|
189
|
-
.map((a) => a?.toString().replace(/^ok_/, 'ak_'))[0] as EncodedData<'ak'> | undefined
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Transaction Validator
|
|
193
|
-
* This function validates some of transaction properties,
|
|
194
|
-
* to make sure it can be posted it to the chain
|
|
195
|
-
* @param transaction - Base64Check-encoded transaction
|
|
196
|
-
* @param node - Node to validate transaction against
|
|
197
|
-
* @param parentTxTypes - Types of parent transactions
|
|
198
|
-
* @returns Array with verification errors
|
|
199
|
-
* @example const errors = await verifyTransaction(transaction, node)
|
|
200
|
-
*/
|
|
201
|
-
export default async function verifyTransaction (
|
|
202
|
-
transaction: EncodedData<'tx' | 'pi'>,
|
|
203
|
-
node: Node,
|
|
204
|
-
parentTxTypes: TX_TYPE[] = []
|
|
205
|
-
): Promise<ValidatorResult[]> {
|
|
206
|
-
const { tx, txType } = unpackTx<TX_TYPE.signed>(transaction)
|
|
207
|
-
const address = getSenderAddress(tx) ??
|
|
208
|
-
(txType === TX_TYPE.signed ? getSenderAddress(tx.encodedTx.tx) : undefined)
|
|
209
|
-
const [account, { height }, { consensusProtocolVersion, nodeNetworkId }] = await Promise.all([
|
|
210
|
-
address == null
|
|
211
|
-
? undefined
|
|
212
|
-
: node.getAccountByPubkey(address)
|
|
213
|
-
.catch(() => ({ id: address, balance: 0n, nonce: 0 }))
|
|
214
|
-
// TODO: remove after fixing https://github.com/aeternity/aepp-sdk-js/issues/1537
|
|
215
|
-
.then(acc => ({ ...acc, id: acc.id as EncodedData<'ak'> })),
|
|
216
|
-
node.getCurrentKeyBlockHeight(),
|
|
217
|
-
node.getNodeInfo()
|
|
218
|
-
])
|
|
219
|
-
|
|
220
|
-
return (await Promise.all(
|
|
221
|
-
validators.map(v => v(
|
|
222
|
-
tx as any,
|
|
223
|
-
{ txType, node, account, height, consensusProtocolVersion, nodeNetworkId, parentTxTypes }
|
|
224
|
-
))
|
|
225
|
-
)).flat()
|
|
226
|
-
}
|
|
229
|
+
},
|
|
230
|
+
);
|
|
@@ -2,32 +2,32 @@
|
|
|
2
2
|
|
|
3
3
|
declare module 'bip32-path' {
|
|
4
4
|
interface BipPath {
|
|
5
|
-
toPathArray: () => number[]
|
|
5
|
+
toPathArray: () => number[];
|
|
6
6
|
/**
|
|
7
7
|
* returns a text encoded path. Set to noRoot to true to omit the m/ prefix.
|
|
8
8
|
* Set oldStyle true to use h instead of ' for marking hardened nodes.
|
|
9
9
|
*/
|
|
10
|
-
toString: (noRoot: boolean, oldStyle: boolean) => string
|
|
10
|
+
toString: (noRoot: boolean, oldStyle: boolean) => string;
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* returns true if the input is a valid path string
|
|
14
14
|
*/
|
|
15
|
-
validateString: (path: string) => boolean
|
|
15
|
+
validateString: (path: string) => boolean;
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* returns true if the input is a valid binary path array
|
|
19
19
|
*/
|
|
20
|
-
validatePathArray: (path: string) => boolean
|
|
20
|
+
validatePathArray: (path: string) => boolean;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* creates an instance from a path written as text.
|
|
25
25
|
* Set reqRoot to true if the m/ prefix is mandatory.
|
|
26
26
|
*/
|
|
27
|
-
export const fromString: (text: string, reqRoot?: boolean) => BipPath
|
|
27
|
+
export const fromString: (text: string, reqRoot?: boolean) => BipPath;
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
30
|
* creates an instance from a binary path array
|
|
31
31
|
*/
|
|
32
|
-
export const fromPathArray: (path: string) => BipPath
|
|
32
|
+
export const fromPathArray: (path: string) => BipPath;
|
|
33
33
|
}
|
|
@@ -1,33 +1,5 @@
|
|
|
1
|
-
declare module 'blakejs' {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
t: number
|
|
6
|
-
c: number
|
|
7
|
-
outlen: number
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export type Data = Buffer | Uint8Array | string
|
|
11
|
-
|
|
12
|
-
type Key = Uint8Array | null
|
|
13
|
-
|
|
14
|
-
export const blake2b: (data: Data, key?: Key, outlen?: number) => Uint8Array
|
|
15
|
-
|
|
16
|
-
export const blake2bFinal: (context: Context) => Uint8Array
|
|
17
|
-
|
|
18
|
-
export const blake2bHex: (data: Data, key?: Key, outlen?: number) => string
|
|
19
|
-
|
|
20
|
-
export const blake2bInit: (outlen?: number, key?: Key) => Context
|
|
21
|
-
|
|
22
|
-
export const blake2bUpdate: (context: Context, data: Data) => void
|
|
23
|
-
|
|
24
|
-
export const blake2s: (data: Data, key?: Key, outlen?: number) => Uint8Array
|
|
25
|
-
|
|
26
|
-
export const blake2sFinal: (context: Context) => Uint8Array
|
|
27
|
-
|
|
28
|
-
export const blake2sHex: (data: Data, key?: Key, outlen?: number) => string
|
|
29
|
-
|
|
30
|
-
export const blake2sInit: (outlen?: number, key?: Key) => Context
|
|
31
|
-
|
|
32
|
-
export const blake2sUpdate: (context: Context, data: Data) => void
|
|
1
|
+
declare module 'blakejs/blake2b.js' {
|
|
2
|
+
export {
|
|
3
|
+
Blake2bCTX, blake2bInit, blake2bUpdate, blake2bFinal, blake2b, blake2bHex,
|
|
4
|
+
} from 'blakejs';
|
|
33
5
|
}
|
|
@@ -5,10 +5,10 @@ declare module 'tweetnacl-auth' {
|
|
|
5
5
|
* Authenticates the given message with the secret key.
|
|
6
6
|
* (In other words, returns HMAC-SHA-512-256 of the message under the key.)
|
|
7
7
|
*/
|
|
8
|
-
export default function (message: Uint8Array, key: Uint8Array): Uint8Array
|
|
8
|
+
export default function (message: Uint8Array, key: Uint8Array): Uint8Array;
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Returns HMAC-SHA-512 (without truncation) of the message under the key
|
|
12
12
|
*/
|
|
13
|
-
export const full: (message: Uint8Array, key: Uint8Array) => Uint8Array
|
|
13
|
+
export const full: (message: Uint8Array, key: Uint8Array) => Uint8Array;
|
|
14
14
|
}
|
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
15
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
16
16
|
*/
|
|
17
|
-
import BigNumber from 'bignumber.js'
|
|
18
|
-
import { isBigNumber } from './bignumber'
|
|
19
|
-
import { ArgumentError } from './errors'
|
|
17
|
+
import BigNumber from 'bignumber.js';
|
|
18
|
+
import { isBigNumber } from './bignumber';
|
|
19
|
+
import { ArgumentError } from './errors';
|
|
20
20
|
|
|
21
21
|
export enum AE_AMOUNT_FORMATS {
|
|
22
22
|
AE = 'ae',
|
|
@@ -25,7 +25,7 @@ export enum AE_AMOUNT_FORMATS {
|
|
|
25
25
|
NANO_AE = 'nanoAE',
|
|
26
26
|
PICO_AE = 'picoAE',
|
|
27
27
|
FEMTO_AE = 'femtoAE',
|
|
28
|
-
AETTOS = 'aettos'
|
|
28
|
+
AETTOS = 'aettos',
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
/**
|
|
@@ -38,77 +38,77 @@ const DENOMINATION_MAGNITUDE = {
|
|
|
38
38
|
[AE_AMOUNT_FORMATS.NANO_AE]: -9,
|
|
39
39
|
[AE_AMOUNT_FORMATS.PICO_AE]: -12,
|
|
40
40
|
[AE_AMOUNT_FORMATS.FEMTO_AE]: -15,
|
|
41
|
-
[AE_AMOUNT_FORMATS.AETTOS]: -18
|
|
42
|
-
} as const
|
|
41
|
+
[AE_AMOUNT_FORMATS.AETTOS]: -18,
|
|
42
|
+
} as const;
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
|
-
* Convert amount to
|
|
45
|
+
* Convert amount from one to other denomination
|
|
46
46
|
* @param value - amount to convert
|
|
47
47
|
* @param options - options
|
|
48
48
|
* @param options.denomination - denomination of amount, can be ['ae', 'aettos']
|
|
49
|
+
* @param options.targetDenomination - target denomination,
|
|
50
|
+
* can be ['ae', 'aettos']
|
|
49
51
|
*/
|
|
50
|
-
export const
|
|
51
|
-
value: string | number | BigNumber,
|
|
52
|
-
{ denomination = AE_AMOUNT_FORMATS.AETTOS
|
|
53
|
-
|
|
52
|
+
export const formatAmount = (
|
|
53
|
+
value: string | number | bigint | BigNumber,
|
|
54
|
+
{ denomination = AE_AMOUNT_FORMATS.AETTOS, targetDenomination = AE_AMOUNT_FORMATS.AETTOS }:
|
|
55
|
+
{ denomination?: AE_AMOUNT_FORMATS; targetDenomination?: AE_AMOUNT_FORMATS },
|
|
56
|
+
): string => {
|
|
57
|
+
if (!isBigNumber(value)) throw new ArgumentError('value', 'a number', value);
|
|
58
|
+
|
|
59
|
+
return new BigNumber(typeof value === 'bigint' ? value.toString() : value)
|
|
60
|
+
.shiftedBy(DENOMINATION_MAGNITUDE[denomination] - DENOMINATION_MAGNITUDE[targetDenomination])
|
|
61
|
+
.toFixed();
|
|
62
|
+
};
|
|
54
63
|
|
|
55
64
|
/**
|
|
56
|
-
* Convert amount to
|
|
65
|
+
* Convert amount to AE
|
|
57
66
|
* @param value - amount to convert
|
|
58
67
|
* @param options - options
|
|
59
68
|
* @param options.denomination - denomination of amount, can be ['ae', 'aettos']
|
|
60
69
|
*/
|
|
61
|
-
export const
|
|
70
|
+
export const toAe = (
|
|
62
71
|
value: string | number | BigNumber,
|
|
63
|
-
{ denomination = AE_AMOUNT_FORMATS.
|
|
64
|
-
): string => formatAmount(value, { denomination })
|
|
72
|
+
{ denomination = AE_AMOUNT_FORMATS.AETTOS }: { denomination?: AE_AMOUNT_FORMATS } = {},
|
|
73
|
+
): string => formatAmount(value, { denomination, targetDenomination: AE_AMOUNT_FORMATS.AE });
|
|
65
74
|
|
|
66
75
|
/**
|
|
67
|
-
* Convert amount
|
|
76
|
+
* Convert amount to aettos
|
|
68
77
|
* @param value - amount to convert
|
|
69
78
|
* @param options - options
|
|
70
79
|
* @param options.denomination - denomination of amount, can be ['ae', 'aettos']
|
|
71
|
-
* @param options.targetDenomination - target denomination,
|
|
72
|
-
* can be ['ae', 'aettos']
|
|
73
80
|
*/
|
|
74
|
-
export const
|
|
75
|
-
value: string | number |
|
|
76
|
-
{ denomination = AE_AMOUNT_FORMATS.
|
|
77
|
-
|
|
78
|
-
): string => {
|
|
79
|
-
if (!isBigNumber(value)) throw new ArgumentError('value', 'a number', value)
|
|
80
|
-
|
|
81
|
-
return new BigNumber(typeof value === 'bigint' ? value.toString() : value)
|
|
82
|
-
.shiftedBy(DENOMINATION_MAGNITUDE[denomination] - DENOMINATION_MAGNITUDE[targetDenomination])
|
|
83
|
-
.toFixed()
|
|
84
|
-
}
|
|
81
|
+
export const toAettos = (
|
|
82
|
+
value: string | number | BigNumber,
|
|
83
|
+
{ denomination = AE_AMOUNT_FORMATS.AE }: { denomination?: AE_AMOUNT_FORMATS } = {},
|
|
84
|
+
): string => formatAmount(value, { denomination });
|
|
85
85
|
|
|
86
86
|
interface Prefix {
|
|
87
|
-
name: string
|
|
88
|
-
magnitude: number
|
|
87
|
+
name: string;
|
|
88
|
+
magnitude: number;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
const prefixes: Prefix[] = [
|
|
92
92
|
{ name: 'exa', magnitude: 18 },
|
|
93
93
|
{ name: 'giga', magnitude: 9 },
|
|
94
94
|
{ name: '', magnitude: 0 },
|
|
95
|
-
{ name: 'pico', magnitude: -12 }
|
|
96
|
-
]
|
|
95
|
+
{ name: 'pico', magnitude: -12 },
|
|
96
|
+
];
|
|
97
97
|
|
|
98
98
|
const getNearestPrefix = (exponent: number): Prefix => prefixes.reduce((p, n) => (
|
|
99
|
-
Math.abs(n.magnitude - exponent) < Math.abs(p.magnitude - exponent) ? n : p))
|
|
99
|
+
Math.abs(n.magnitude - exponent) < Math.abs(p.magnitude - exponent) ? n : p));
|
|
100
100
|
|
|
101
101
|
const getLowerBoundPrefix = (exponent: number): Prefix => prefixes
|
|
102
|
-
.find(p => p.magnitude <= exponent) ?? prefixes[prefixes.length - 1]
|
|
102
|
+
.find((p) => p.magnitude <= exponent) ?? prefixes[prefixes.length - 1];
|
|
103
103
|
|
|
104
104
|
export default (rawValue: string | number | BigNumber): string => {
|
|
105
|
-
const value: BigNumber = new BigNumber(rawValue)
|
|
105
|
+
const value: BigNumber = new BigNumber(rawValue);
|
|
106
106
|
|
|
107
|
-
const exp = value.e ?? 0
|
|
108
|
-
const { name, magnitude } = (exp < 0 ? getNearestPrefix : getLowerBoundPrefix)(exp)
|
|
107
|
+
const exp = value.e ?? 0;
|
|
108
|
+
const { name, magnitude } = (exp < 0 ? getNearestPrefix : getLowerBoundPrefix)(exp);
|
|
109
109
|
const v = value
|
|
110
110
|
.shiftedBy(-magnitude)
|
|
111
111
|
.precision(9 + Math.min(exp - magnitude, 0))
|
|
112
|
-
.toFixed()
|
|
113
|
-
return `${v}${name !== '' ? ' ' : ''}${name}
|
|
114
|
-
}
|
|
112
|
+
.toFixed();
|
|
113
|
+
return `${v}${name !== '' ? ' ' : ''}${name}`;
|
|
114
|
+
};
|