@transia/ripple-binary-codec 1.4.6-alpha.9 → 2.5.1
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/binary.d.ts +30 -16
- package/dist/binary.js +36 -16
- package/dist/binary.js.map +1 -1
- package/dist/enums/bytes.d.ts +1 -2
- package/dist/enums/bytes.js +3 -2
- package/dist/enums/bytes.js.map +1 -1
- package/dist/enums/constants.d.ts +1 -0
- package/dist/enums/constants.js +6 -5
- package/dist/enums/constants.js.map +1 -1
- package/dist/enums/definitions.json +1604 -969
- package/dist/enums/field.d.ts +1 -2
- package/dist/enums/field.js +1 -2
- package/dist/enums/field.js.map +1 -1
- package/dist/enums/index.d.ts +1 -2
- package/dist/enums/index.js +5 -27
- package/dist/enums/index.js.map +1 -1
- package/dist/enums/src/enums/definitions.json +1606 -971
- package/dist/enums/xrpl-definitions-base.d.ts +5 -3
- package/dist/enums/xrpl-definitions-base.js +26 -6
- package/dist/enums/xrpl-definitions-base.js.map +1 -1
- package/dist/enums/xrpl-definitions.d.ts +2 -1
- package/dist/enums/xrpl-definitions.js +2 -1
- package/dist/enums/xrpl-definitions.js.map +1 -1
- package/dist/hash-prefixes.d.ts +1 -2
- package/dist/hash-prefixes.js +8 -6
- package/dist/hash-prefixes.js.map +1 -1
- package/dist/hashes.d.ts +6 -7
- package/dist/hashes.js +6 -7
- package/dist/hashes.js.map +1 -1
- package/dist/index.d.ts +16 -11
- package/dist/index.js +53 -58
- package/dist/index.js.map +1 -1
- package/dist/ledger-hashes.d.ts +1 -2
- package/dist/ledger-hashes.js +11 -30
- package/dist/ledger-hashes.js.map +1 -1
- package/dist/quality.d.ts +3 -4
- package/dist/quality.js +10 -8
- package/dist/quality.js.map +1 -1
- package/dist/serdes/binary-parser.d.ts +2 -3
- package/dist/serdes/binary-parser.js +16 -32
- package/dist/serdes/binary-parser.js.map +1 -1
- package/dist/serdes/binary-serializer.d.ts +7 -8
- package/dist/serdes/binary-serializer.js +14 -37
- package/dist/serdes/binary-serializer.js.map +1 -1
- package/dist/shamap.d.ts +4 -5
- package/dist/shamap.js +5 -5
- package/dist/shamap.js.map +1 -1
- package/dist/src/binary.d.ts +106 -0
- package/dist/src/binary.js +154 -0
- package/dist/src/binary.js.map +1 -0
- package/dist/src/coretypes.d.ts +9 -0
- package/dist/src/coretypes.js +48 -0
- package/dist/src/coretypes.js.map +1 -0
- package/dist/src/enums/bytes.d.ts +25 -0
- package/dist/src/enums/bytes.js +65 -0
- package/dist/src/enums/bytes.js.map +1 -0
- package/dist/src/enums/constants.d.ts +5 -0
- package/dist/src/enums/constants.js +9 -0
- package/dist/src/enums/constants.js.map +1 -0
- package/dist/src/enums/definitions.json +3404 -0
- package/dist/src/enums/field.d.ts +28 -0
- package/dist/src/enums/field.js +58 -0
- package/dist/src/enums/field.js.map +1 -0
- package/dist/src/enums/index.d.ts +12 -0
- package/dist/src/enums/index.js +31 -0
- package/dist/src/enums/index.js.map +1 -0
- package/dist/src/enums/utils-renumber.d.ts +101 -0
- package/dist/src/enums/utils-renumber.js +127 -0
- package/dist/src/enums/utils-renumber.js.map +1 -0
- package/dist/src/enums/xrpl-definitions-base.d.ts +47 -0
- package/dist/src/enums/xrpl-definitions-base.js +83 -0
- package/dist/src/enums/xrpl-definitions-base.js.map +1 -0
- package/dist/src/enums/xrpl-definitions.d.ts +22 -0
- package/dist/src/enums/xrpl-definitions.js +30 -0
- package/dist/src/enums/xrpl-definitions.js.map +1 -0
- package/dist/src/hash-prefixes.d.ts +5 -0
- package/dist/src/hash-prefixes.js +43 -0
- package/dist/src/hash-prefixes.js.map +1 -0
- package/dist/src/hashes.d.ts +50 -0
- package/dist/src/hashes.js +76 -0
- package/dist/src/hashes.js.map +1 -0
- package/dist/src/index.d.ts +69 -0
- package/dist/src/index.js +134 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/ledger-hashes.d.ts +48 -0
- package/dist/src/ledger-hashes.js +141 -0
- package/dist/src/ledger-hashes.js.map +1 -0
- package/dist/src/quality.d.ts +21 -0
- package/dist/src/quality.js +42 -0
- package/dist/src/quality.js.map +1 -0
- package/dist/src/serdes/binary-parser.d.ts +100 -0
- package/dist/src/serdes/binary-parser.js +194 -0
- package/dist/src/serdes/binary-parser.js.map +1 -0
- package/dist/src/serdes/binary-serializer.d.ts +81 -0
- package/dist/src/serdes/binary-serializer.js +149 -0
- package/dist/src/serdes/binary-serializer.js.map +1 -0
- package/dist/src/shamap.d.ts +102 -0
- package/dist/src/shamap.js +172 -0
- package/dist/src/shamap.js.map +1 -0
- package/dist/src/types/account-id.d.ts +37 -0
- package/dist/src/types/account-id.js +73 -0
- package/dist/src/types/account-id.js.map +1 -0
- package/dist/src/types/amount.d.ts +92 -0
- package/dist/src/types/amount.js +292 -0
- package/dist/src/types/amount.js.map +1 -0
- package/dist/src/types/blob.d.ts +25 -0
- package/dist/src/types/blob.js +46 -0
- package/dist/src/types/blob.js.map +1 -0
- package/dist/src/types/currency.d.ts +30 -0
- package/dist/src/types/currency.js +131 -0
- package/dist/src/types/currency.js.map +1 -0
- package/dist/src/types/data.d.ts +104 -0
- package/dist/src/types/data.js +387 -0
- package/dist/src/types/data.js.map +1 -0
- package/dist/src/types/dataType.d.ts +94 -0
- package/dist/src/types/dataType.js +145 -0
- package/dist/src/types/dataType.js.map +1 -0
- package/dist/src/types/hash-128.d.ts +18 -0
- package/dist/src/types/hash-128.js +36 -0
- package/dist/src/types/hash-128.js.map +1 -0
- package/dist/src/types/hash-160.d.ts +12 -0
- package/dist/src/types/hash-160.js +23 -0
- package/dist/src/types/hash-160.js.map +1 -0
- package/dist/src/types/hash-192.d.ts +12 -0
- package/dist/src/types/hash-192.js +23 -0
- package/dist/src/types/hash-192.js.map +1 -0
- package/dist/src/types/hash-256.d.ts +12 -0
- package/dist/src/types/hash-256.js +20 -0
- package/dist/src/types/hash-256.js.map +1 -0
- package/dist/src/types/hash.d.ts +40 -0
- package/dist/src/types/hash.js +76 -0
- package/dist/src/types/hash.js.map +1 -0
- package/dist/src/types/index.d.ts +21 -0
- package/dist/src/types/index.js +72 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/src/types/issue.d.ts +49 -0
- package/dist/src/types/issue.js +107 -0
- package/dist/src/types/issue.js.map +1 -0
- package/dist/src/types/json.d.ts +169 -0
- package/dist/src/types/json.js +529 -0
- package/dist/src/types/json.js.map +1 -0
- package/dist/src/types/path-set.d.ts +37 -0
- package/dist/src/types/path-set.js +236 -0
- package/dist/src/types/path-set.js.map +1 -0
- package/dist/src/types/serialized-type.d.ts +117 -0
- package/dist/src/types/serialized-type.js +242 -0
- package/dist/src/types/serialized-type.js.map +1 -0
- package/dist/src/types/st-array.d.ts +32 -0
- package/dist/src/types/st-array.js +89 -0
- package/dist/src/types/st-array.js.map +1 -0
- package/dist/src/types/st-number.d.ts +55 -0
- package/dist/src/types/st-number.js +212 -0
- package/dist/src/types/st-number.js.map +1 -0
- package/dist/src/types/st-object.d.ts +33 -0
- package/dist/src/types/st-object.js +176 -0
- package/dist/src/types/st-object.js.map +1 -0
- package/dist/src/types/uint-16.d.ts +26 -0
- package/dist/src/types/uint-16.js +49 -0
- package/dist/src/types/uint-16.js.map +1 -0
- package/dist/src/types/uint-32.d.ts +26 -0
- package/dist/src/types/uint-32.js +54 -0
- package/dist/src/types/uint-32.js.map +1 -0
- package/dist/src/types/uint-64.d.ts +40 -0
- package/dist/src/types/uint-64.js +112 -0
- package/dist/src/types/uint-64.js.map +1 -0
- package/dist/src/types/uint-8.d.ts +26 -0
- package/dist/src/types/uint-8.js +50 -0
- package/dist/src/types/uint-8.js.map +1 -0
- package/dist/src/types/uint.d.ts +29 -0
- package/dist/src/types/uint.js +47 -0
- package/dist/src/types/uint.js.map +1 -0
- package/dist/src/types/vector-256.d.ts +31 -0
- package/dist/src/types/vector-256.js +76 -0
- package/dist/src/types/vector-256.js.map +1 -0
- package/dist/src/types/xchain-bridge.d.ts +45 -0
- package/dist/src/types/xchain-bridge.js +105 -0
- package/dist/src/types/xchain-bridge.js.map +1 -0
- package/dist/src/utils.d.ts +79 -0
- package/dist/src/utils.js +181 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/account-id.d.ts +3 -2
- package/dist/types/account-id.js +8 -6
- package/dist/types/account-id.js.map +1 -1
- package/dist/types/amount.d.ts +33 -9
- package/dist/types/amount.js +128 -43
- package/dist/types/amount.js.map +1 -1
- package/dist/types/blob.d.ts +3 -3
- package/dist/types/blob.js +8 -2
- package/dist/types/blob.js.map +1 -1
- package/dist/types/currency.d.ts +3 -2
- package/dist/types/currency.js +17 -13
- package/dist/types/currency.js.map +1 -1
- package/dist/types/data.d.ts +104 -0
- package/dist/types/data.js +387 -0
- package/dist/types/data.js.map +1 -0
- package/dist/types/dataType.d.ts +94 -0
- package/dist/types/dataType.js +145 -0
- package/dist/types/dataType.js.map +1 -0
- package/dist/types/hash-128.d.ts +3 -2
- package/dist/types/hash-128.js +7 -3
- package/dist/types/hash-128.js.map +1 -1
- package/dist/types/hash-160.d.ts +3 -2
- package/dist/types/hash-160.js +5 -2
- package/dist/types/hash-160.js.map +1 -1
- package/dist/types/hash-192.d.ts +12 -0
- package/dist/types/hash-192.js +23 -0
- package/dist/types/hash-192.js.map +1 -0
- package/dist/types/hash-256.d.ts +3 -2
- package/dist/types/hash-256.js +5 -2
- package/dist/types/hash-256.js.map +1 -1
- package/dist/types/hash.d.ts +2 -3
- package/dist/types/hash.js +8 -4
- package/dist/types/hash.js.map +1 -1
- package/dist/types/index.d.ts +4 -1
- package/dist/types/index.js +17 -2
- package/dist/types/index.js.map +1 -1
- package/dist/types/issue.d.ts +49 -0
- package/dist/types/issue.js +107 -0
- package/dist/types/issue.js.map +1 -0
- package/dist/types/json.d.ts +169 -0
- package/dist/types/json.js +529 -0
- package/dist/types/json.js.map +1 -0
- package/dist/types/path-set.d.ts +2 -1
- package/dist/types/path-set.js +15 -12
- package/dist/types/path-set.js.map +1 -1
- package/dist/types/serialized-type.d.ts +70 -16
- package/dist/types/serialized-type.js +153 -8
- package/dist/types/serialized-type.js.map +1 -1
- package/dist/types/st-array.d.ts +7 -3
- package/dist/types/st-array.js +20 -11
- package/dist/types/st-array.js.map +1 -1
- package/dist/types/st-number.d.ts +55 -0
- package/dist/types/st-number.js +212 -0
- package/dist/types/st-number.js.map +1 -0
- package/dist/types/st-object.d.ts +2 -1
- package/dist/types/st-object.js +35 -8
- package/dist/types/st-object.js.map +1 -1
- package/dist/types/uint-16.d.ts +3 -2
- package/dist/types/uint-16.js +10 -5
- package/dist/types/uint-16.js.map +1 -1
- package/dist/types/uint-32.d.ts +3 -2
- package/dist/types/uint-32.js +11 -6
- package/dist/types/uint-32.js.map +1 -1
- package/dist/types/uint-64.d.ts +8 -7
- package/dist/types/uint-64.js +49 -24
- package/dist/types/uint-64.js.map +1 -1
- package/dist/types/uint-8.d.ts +3 -2
- package/dist/types/uint-8.js +11 -5
- package/dist/types/uint-8.js.map +1 -1
- package/dist/types/uint.d.ts +5 -6
- package/dist/types/uint.js +5 -0
- package/dist/types/uint.js.map +1 -1
- package/dist/types/vector-256.d.ts +3 -3
- package/dist/types/vector-256.js +5 -4
- package/dist/types/vector-256.js.map +1 -1
- package/dist/types/xchain-bridge.d.ts +45 -0
- package/dist/types/xchain-bridge.js +105 -0
- package/dist/types/xchain-bridge.js.map +1 -0
- package/dist/utils.d.ts +79 -0
- package/dist/utils.js +181 -0
- package/dist/utils.js.map +1 -0
- package/package.json +21 -17
- package/src/README.md +5 -0
- package/src/binary.ts +235 -0
- package/src/coretypes.ts +31 -0
- package/src/enums/README.md +144 -0
- package/src/enums/bytes.ts +80 -0
- package/src/enums/constants.ts +5 -0
- package/src/enums/definitions.json +3409 -0
- package/src/enums/field.ts +84 -0
- package/src/enums/index.ts +34 -0
- package/src/enums/utils-renumber.ts +134 -0
- package/src/enums/xrpl-definitions-base.ts +150 -0
- package/src/enums/xrpl-definitions.ts +33 -0
- package/src/hash-prefixes.ts +42 -0
- package/src/hashes.ts +75 -0
- package/src/index.ts +166 -0
- package/src/ledger-hashes.ts +191 -0
- package/src/quality.ts +38 -0
- package/src/serdes/binary-parser.ts +228 -0
- package/src/serdes/binary-serializer.ts +166 -0
- package/src/shamap.ts +189 -0
- package/src/types/account-id.ts +91 -0
- package/src/types/amount.ts +366 -0
- package/src/types/blob.ts +50 -0
- package/src/types/currency.ts +145 -0
- package/src/types/data.ts +496 -0
- package/src/types/dataType.ts +178 -0
- package/src/types/hash-128.ts +38 -0
- package/src/types/hash-160.ts +24 -0
- package/src/types/hash-192.ts +24 -0
- package/src/types/hash-256.ts +20 -0
- package/src/types/hash.ts +86 -0
- package/src/types/index.ts +74 -0
- package/src/types/issue.ts +140 -0
- package/src/types/json.ts +641 -0
- package/src/types/path-set.ts +294 -0
- package/src/types/serialized-type.ts +271 -0
- package/src/types/st-array.ts +117 -0
- package/src/types/st-number.ts +238 -0
- package/src/types/st-object.ts +219 -0
- package/src/types/uint-16.ts +58 -0
- package/src/types/uint-32.ts +64 -0
- package/src/types/uint-64.ts +141 -0
- package/src/types/uint-8.ts +57 -0
- package/src/types/uint.ts +60 -0
- package/src/types/vector-256.ts +83 -0
- package/src/types/xchain-bridge.ts +132 -0
- package/src/utils.ts +220 -0
- package/test/amount.test.js +0 -43
- package/test/binary-json.test.js +0 -45
- package/test/binary-parser.test.js +0 -396
- package/test/binary-serializer.test.js +0 -319
- package/test/definitions.test.js +0 -100
- package/test/fixtures/account-tx-transactions.db +0 -0
- package/test/fixtures/codec-fixtures.json +0 -4466
- package/test/fixtures/data-driven-tests.json +0 -2919
- package/test/fixtures/delivermin-tx-binary.json +0 -1
- package/test/fixtures/delivermin-tx.json +0 -98
- package/test/fixtures/deposit-preauth-tx-binary.json +0 -1
- package/test/fixtures/deposit-preauth-tx-meta-binary.json +0 -1
- package/test/fixtures/deposit-preauth-tx.json +0 -58
- package/test/fixtures/escrow-cancel-binary.json +0 -1
- package/test/fixtures/escrow-cancel-tx.json +0 -6
- package/test/fixtures/escrow-create-binary.json +0 -1
- package/test/fixtures/escrow-create-tx.json +0 -10
- package/test/fixtures/escrow-finish-binary.json +0 -1
- package/test/fixtures/escrow-finish-meta-binary.json +0 -1
- package/test/fixtures/escrow-finish-tx.json +0 -95
- package/test/fixtures/ledger-full-38129.json +0 -1
- package/test/fixtures/ledger-full-40000.json +0 -1
- package/test/fixtures/negative-unl.json +0 -12
- package/test/fixtures/nf-token.json +0 -547
- package/test/fixtures/payment-channel-claim-binary.json +0 -1
- package/test/fixtures/payment-channel-claim-tx.json +0 -8
- package/test/fixtures/payment-channel-create-binary.json +0 -1
- package/test/fixtures/payment-channel-create-tx.json +0 -11
- package/test/fixtures/payment-channel-fund-binary.json +0 -1
- package/test/fixtures/payment-channel-fund-tx.json +0 -7
- package/test/fixtures/remit-binary.json +0 -1
- package/test/fixtures/remit-tx.json +0 -39
- package/test/fixtures/signerlistset-tx-binary.json +0 -1
- package/test/fixtures/signerlistset-tx-meta-binary.json +0 -1
- package/test/fixtures/signerlistset-tx.json +0 -94
- package/test/fixtures/ticket-create-binary.json +0 -1
- package/test/fixtures/ticket-create-tx.json +0 -7
- package/test/fixtures/unl-report-binary.json +0 -1
- package/test/fixtures/unl-report-meta-binary.json +0 -1
- package/test/fixtures/unl-report.json +0 -89
- package/test/fixtures/x-codec-fixtures.json +0 -188
- package/test/hash.test.js +0 -135
- package/test/ledger.test.js +0 -29
- package/test/lower-case-hex.test.js +0 -46
- package/test/pseudo-transaction.test.js +0 -38
- package/test/quality.test.js +0 -15
- package/test/shamap.test.js +0 -89
- package/test/signing-data-encoding.test.js +0 -213
- package/test/tx-encode-decode.test.js +0 -119
- package/test/types.test.js +0 -34
- package/test/uint.test.js +0 -148
- package/test/utils.js +0 -30
- package/test/x-address.test.js +0 -181
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ShaMapLeaf = exports.ShaMapNode = exports.ShaMap = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
const hash_prefixes_1 = require("./hash-prefixes");
|
|
6
|
+
const hashes_1 = require("./hashes");
|
|
7
|
+
/**
|
|
8
|
+
* Abstract class describing a SHAMapNode
|
|
9
|
+
*/
|
|
10
|
+
class ShaMapNode {
|
|
11
|
+
}
|
|
12
|
+
exports.ShaMapNode = ShaMapNode;
|
|
13
|
+
/**
|
|
14
|
+
* Class describing a Leaf of SHAMap
|
|
15
|
+
*/
|
|
16
|
+
class ShaMapLeaf extends ShaMapNode {
|
|
17
|
+
constructor(index, item) {
|
|
18
|
+
super();
|
|
19
|
+
this.index = index;
|
|
20
|
+
this.item = item;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* @returns true as ShaMapLeaf is a leaf node
|
|
24
|
+
*/
|
|
25
|
+
isLeaf() {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* @returns false as ShaMapLeaf is not an inner node
|
|
30
|
+
*/
|
|
31
|
+
isInner() {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Get the prefix of the this.item
|
|
36
|
+
*
|
|
37
|
+
* @returns The hash prefix, unless this.item is undefined, then it returns an empty Uint8Array
|
|
38
|
+
*/
|
|
39
|
+
hashPrefix() {
|
|
40
|
+
return this.item === undefined ? new Uint8Array(0) : this.item.hashPrefix();
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Hash the bytes representation of this
|
|
44
|
+
*
|
|
45
|
+
* @returns hash of this.item concatenated with this.index
|
|
46
|
+
*/
|
|
47
|
+
hash() {
|
|
48
|
+
const hash = hashes_1.Sha512Half.put(this.hashPrefix());
|
|
49
|
+
this.toBytesSink(hash);
|
|
50
|
+
return hash.finish();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Write the bytes representation of this to a BytesList
|
|
54
|
+
* @param list BytesList to write bytes to
|
|
55
|
+
*/
|
|
56
|
+
toBytesSink(list) {
|
|
57
|
+
if (this.item !== undefined) {
|
|
58
|
+
this.item.toBytesSink(list);
|
|
59
|
+
}
|
|
60
|
+
this.index.toBytesSink(list);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.ShaMapLeaf = ShaMapLeaf;
|
|
64
|
+
/**
|
|
65
|
+
* Class defining an Inner Node of a SHAMap
|
|
66
|
+
*/
|
|
67
|
+
class ShaMapInner extends ShaMapNode {
|
|
68
|
+
constructor(depth = 0) {
|
|
69
|
+
super();
|
|
70
|
+
this.depth = depth;
|
|
71
|
+
this.slotBits = 0;
|
|
72
|
+
this.branches = Array(16);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* @returns true as ShaMapInner is an inner node
|
|
76
|
+
*/
|
|
77
|
+
isInner() {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* @returns false as ShaMapInner is not a leaf node
|
|
82
|
+
*/
|
|
83
|
+
isLeaf() {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Get the hash prefix for this node
|
|
88
|
+
*
|
|
89
|
+
* @returns hash prefix describing an inner node
|
|
90
|
+
*/
|
|
91
|
+
hashPrefix() {
|
|
92
|
+
return hash_prefixes_1.HashPrefix.innerNode;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Set a branch of this node to be another node
|
|
96
|
+
*
|
|
97
|
+
* @param slot Slot to add branch to this.branches
|
|
98
|
+
* @param branch Branch to add
|
|
99
|
+
*/
|
|
100
|
+
setBranch(slot, branch) {
|
|
101
|
+
this.slotBits = this.slotBits | (1 << slot);
|
|
102
|
+
this.branches[slot] = branch;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* @returns true if node is empty
|
|
106
|
+
*/
|
|
107
|
+
empty() {
|
|
108
|
+
return this.slotBits === 0;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Compute the hash of this node
|
|
112
|
+
*
|
|
113
|
+
* @returns The hash of this node
|
|
114
|
+
*/
|
|
115
|
+
hash() {
|
|
116
|
+
if (this.empty()) {
|
|
117
|
+
return types_1.coreTypes.Hash256.ZERO_256;
|
|
118
|
+
}
|
|
119
|
+
const hash = hashes_1.Sha512Half.put(this.hashPrefix());
|
|
120
|
+
this.toBytesSink(hash);
|
|
121
|
+
return hash.finish();
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Writes the bytes representation of this node to a BytesList
|
|
125
|
+
*
|
|
126
|
+
* @param list BytesList to write bytes to
|
|
127
|
+
*/
|
|
128
|
+
toBytesSink(list) {
|
|
129
|
+
for (let i = 0; i < this.branches.length; i++) {
|
|
130
|
+
const branch = this.branches[i];
|
|
131
|
+
const hash = branch
|
|
132
|
+
? branch.hash()
|
|
133
|
+
: types_1.coreTypes.Hash256.ZERO_256;
|
|
134
|
+
hash.toBytesSink(list);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Add item to the SHAMap
|
|
139
|
+
*
|
|
140
|
+
* @param index Hash of the index of the item being inserted
|
|
141
|
+
* @param item Item to insert in the map
|
|
142
|
+
* @param leaf Leaf node to insert when branch doesn't exist
|
|
143
|
+
*/
|
|
144
|
+
addItem(index, item, leaf) {
|
|
145
|
+
if (index === undefined) {
|
|
146
|
+
throw new Error();
|
|
147
|
+
}
|
|
148
|
+
if (index !== undefined) {
|
|
149
|
+
const nibble = index.nibblet(this.depth);
|
|
150
|
+
const existing = this.branches[nibble];
|
|
151
|
+
if (existing === undefined) {
|
|
152
|
+
this.setBranch(nibble, leaf || new ShaMapLeaf(index, item));
|
|
153
|
+
}
|
|
154
|
+
else if (existing instanceof ShaMapLeaf) {
|
|
155
|
+
const newInner = new ShaMapInner(this.depth + 1);
|
|
156
|
+
newInner.addItem(existing.index, undefined, existing);
|
|
157
|
+
newInner.addItem(index, item, leaf);
|
|
158
|
+
this.setBranch(nibble, newInner);
|
|
159
|
+
}
|
|
160
|
+
else if (existing instanceof ShaMapInner) {
|
|
161
|
+
existing.addItem(index, item, leaf);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
throw new Error('invalid ShaMap.addItem call');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
class ShaMap extends ShaMapInner {
|
|
170
|
+
}
|
|
171
|
+
exports.ShaMap = ShaMap;
|
|
172
|
+
//# sourceMappingURL=shamap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shamap.js","sourceRoot":"","sources":["../../src/shamap.ts"],"names":[],"mappings":";;;AAAA,mCAAmC;AACnC,mDAA4C;AAC5C,qCAAqC;AAIrC;;GAEG;AACH,MAAe,UAAU;CAMxB;AA6KgB,gCAAU;AA3K3B;;GAEG;AACH,MAAM,UAAW,SAAQ,UAAU;IACjC,YACS,KAAc,EACd,IAAiB;QAExB,KAAK,EAAE,CAAA;QAHA,UAAK,GAAL,KAAK,CAAS;QACd,SAAI,GAAJ,IAAI,CAAa;IAG1B,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,OAAO,IAAI,CAAA;IACb,CAAC;IAED;;OAEG;IACH,OAAO;QACL,OAAO,KAAK,CAAA;IACd,CAAC;IAED;;;;OAIG;IACH,UAAU;QACR,OAAO,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAA;IAC7E,CAAC;IAED;;;;OAIG;IACH,IAAI;QACF,MAAM,IAAI,GAAG,mBAAU,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC,CAAA;QAC9C,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAA;QACtB,OAAO,IAAI,CAAC,MAAM,EAAE,CAAA;IACtB,CAAC;IAED;;;OAGG;IACH,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AS,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;aACjC;iBAAM,IAAI,QAAQ,YAAY,WAAW,EAAE;gBAC1C,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,CAAA;aACpC;iBAAM;gBACL,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAA;aAC/C;SACF;IACH,CAAC;CACF;AAED,MAAM,MAAO,SAAQ,WAAW;CAAG;AAE1B,wBAAM"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Hash160 } from './hash-160';
|
|
2
|
+
import { SerializedTypeID } from './serialized-type';
|
|
3
|
+
/**
|
|
4
|
+
* Class defining how to encode and decode an AccountID
|
|
5
|
+
*/
|
|
6
|
+
declare class AccountID extends Hash160 {
|
|
7
|
+
static readonly defaultAccountID: AccountID;
|
|
8
|
+
constructor(bytes?: Uint8Array);
|
|
9
|
+
/**
|
|
10
|
+
* Defines how to construct an AccountID
|
|
11
|
+
*
|
|
12
|
+
* @param value either an existing AccountID, a hex-string, or a base58 r-Address
|
|
13
|
+
* @returns an AccountID object
|
|
14
|
+
*/
|
|
15
|
+
static from<T extends Hash160 | string>(value: T): AccountID;
|
|
16
|
+
/**
|
|
17
|
+
* Defines how to build an AccountID from a base58 r-Address
|
|
18
|
+
*
|
|
19
|
+
* @param value a base58 r-Address
|
|
20
|
+
* @returns an AccountID object
|
|
21
|
+
*/
|
|
22
|
+
static fromBase58(value: string): AccountID;
|
|
23
|
+
/**
|
|
24
|
+
* Overload of toJSON
|
|
25
|
+
*
|
|
26
|
+
* @returns the base58 string for this AccountID
|
|
27
|
+
*/
|
|
28
|
+
toJSON(): string;
|
|
29
|
+
/**
|
|
30
|
+
* Defines how to encode AccountID into a base58 address
|
|
31
|
+
*
|
|
32
|
+
* @returns the base58 string defined by this.bytes
|
|
33
|
+
*/
|
|
34
|
+
toBase58(): string;
|
|
35
|
+
getSType(): SerializedTypeID;
|
|
36
|
+
}
|
|
37
|
+
export { AccountID };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AccountID = void 0;
|
|
4
|
+
const ripple_address_codec_1 = require("@transia/ripple-address-codec");
|
|
5
|
+
const hash_160_1 = require("./hash-160");
|
|
6
|
+
const utils_1 = require("@transia/isomorphic/utils");
|
|
7
|
+
const serialized_type_1 = require("./serialized-type");
|
|
8
|
+
const HEX_REGEX = /^[A-F0-9]{40}$/;
|
|
9
|
+
/**
|
|
10
|
+
* Class defining how to encode and decode an AccountID
|
|
11
|
+
*/
|
|
12
|
+
class AccountID extends hash_160_1.Hash160 {
|
|
13
|
+
constructor(bytes) {
|
|
14
|
+
super(bytes !== null && bytes !== void 0 ? bytes : AccountID.defaultAccountID.bytes);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Defines how to construct an AccountID
|
|
18
|
+
*
|
|
19
|
+
* @param value either an existing AccountID, a hex-string, or a base58 r-Address
|
|
20
|
+
* @returns an AccountID object
|
|
21
|
+
*/
|
|
22
|
+
static from(value) {
|
|
23
|
+
if (value instanceof AccountID) {
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
if (typeof value === 'string') {
|
|
27
|
+
if (value === '') {
|
|
28
|
+
return new AccountID();
|
|
29
|
+
}
|
|
30
|
+
return HEX_REGEX.test(value)
|
|
31
|
+
? new AccountID((0, utils_1.hexToBytes)(value))
|
|
32
|
+
: this.fromBase58(value);
|
|
33
|
+
}
|
|
34
|
+
throw new Error('Cannot construct AccountID from value given');
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Defines how to build an AccountID from a base58 r-Address
|
|
38
|
+
*
|
|
39
|
+
* @param value a base58 r-Address
|
|
40
|
+
* @returns an AccountID object
|
|
41
|
+
*/
|
|
42
|
+
static fromBase58(value) {
|
|
43
|
+
if ((0, ripple_address_codec_1.isValidXAddress)(value)) {
|
|
44
|
+
const classic = (0, ripple_address_codec_1.xAddressToClassicAddress)(value);
|
|
45
|
+
if (classic.tag !== false)
|
|
46
|
+
throw new Error('Only allowed to have tag on Account or Destination');
|
|
47
|
+
value = classic.classicAddress;
|
|
48
|
+
}
|
|
49
|
+
return new AccountID(Uint8Array.from((0, ripple_address_codec_1.decodeAccountID)(value)));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Overload of toJSON
|
|
53
|
+
*
|
|
54
|
+
* @returns the base58 string for this AccountID
|
|
55
|
+
*/
|
|
56
|
+
toJSON() {
|
|
57
|
+
return this.toBase58();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Defines how to encode AccountID into a base58 address
|
|
61
|
+
*
|
|
62
|
+
* @returns the base58 string defined by this.bytes
|
|
63
|
+
*/
|
|
64
|
+
toBase58() {
|
|
65
|
+
return (0, ripple_address_codec_1.encodeAccountID)(this.bytes);
|
|
66
|
+
}
|
|
67
|
+
getSType() {
|
|
68
|
+
return serialized_type_1.SerializedTypeID.STI_ACCOUNT;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.AccountID = AccountID;
|
|
72
|
+
AccountID.defaultAccountID = new AccountID(new Uint8Array(20));
|
|
73
|
+
//# sourceMappingURL=account-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-id.js","sourceRoot":"","sources":["../../../src/types/account-id.ts"],"names":[],"mappings":";;;AAAA,wEAKsC;AACtC,yCAAoC;AACpC,qDAAsD;AACtD,uDAAoD;AAEpD,MAAM,SAAS,GAAG,gBAAgB,CAAA;AAElC;;GAEG;AACH,MAAM,SAAU,SAAQ,kBAAO;IAK7B,YAAY,KAAkB;QAC5B,KAAK,CAAC,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,SAAS,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAA;IAClD,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,IAAI,CAA6B,KAAQ;QAC9C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
1
|
+
import { BinaryParser } from '../serdes/binary-parser';
|
|
2
|
+
import { JsonObject, SerializedType, SerializedTypeID } from './serialized-type';
|
|
3
|
+
interface AmountObjectIOU extends JsonObject {
|
|
4
|
+
value: string;
|
|
5
|
+
currency: string;
|
|
6
|
+
issuer: string;
|
|
7
|
+
}
|
|
8
|
+
interface AmountObjectMPT extends JsonObject {
|
|
9
|
+
value: string;
|
|
10
|
+
mpt_issuance_id: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Interface for JSON objects that represent amounts
|
|
14
|
+
*/
|
|
15
|
+
type AmountObject = AmountObjectIOU | AmountObjectMPT;
|
|
16
|
+
/**
|
|
17
|
+
* Class for serializing/Deserializing Amounts
|
|
18
|
+
*/
|
|
19
|
+
declare class Amount extends SerializedType {
|
|
20
|
+
static defaultAmount: Amount;
|
|
21
|
+
constructor(bytes: Uint8Array);
|
|
22
|
+
/**
|
|
23
|
+
* Construct an amount from an IOU, MPT or string amount
|
|
24
|
+
*
|
|
25
|
+
* @param value An Amount, object representing an IOU, or a string
|
|
26
|
+
* representing an integer amount
|
|
27
|
+
* @returns An Amount object
|
|
28
|
+
*/
|
|
29
|
+
static from<T extends Amount | AmountObject | string>(value: T): Amount;
|
|
30
|
+
/**
|
|
31
|
+
* Read an amount from a BinaryParser
|
|
32
|
+
*
|
|
33
|
+
* @param parser BinaryParser to read the Amount from
|
|
34
|
+
* @returns An Amount object
|
|
35
|
+
*/
|
|
36
|
+
static fromParser(parser: BinaryParser): Amount;
|
|
37
|
+
/**
|
|
38
|
+
* Get the JSON representation of this Amount
|
|
39
|
+
*
|
|
40
|
+
* @returns the JSON interpretation of this.bytes
|
|
41
|
+
*/
|
|
42
|
+
toJSON(): AmountObject | string;
|
|
43
|
+
/**
|
|
44
|
+
* Validate XRP amount
|
|
45
|
+
*
|
|
46
|
+
* @param amount String representing XRP amount
|
|
47
|
+
* @returns void, but will throw if invalid amount
|
|
48
|
+
*/
|
|
49
|
+
private static assertXrpIsValid;
|
|
50
|
+
/**
|
|
51
|
+
* Validate IOU.value amount
|
|
52
|
+
*
|
|
53
|
+
* @param decimal BigNumber object representing IOU.value
|
|
54
|
+
* @returns void, but will throw if invalid amount
|
|
55
|
+
*/
|
|
56
|
+
private static assertIouIsValid;
|
|
57
|
+
/**
|
|
58
|
+
* Validate MPT.value amount
|
|
59
|
+
*
|
|
60
|
+
* @param decimal BigNumber object representing MPT.value
|
|
61
|
+
* @returns void, but will throw if invalid amount
|
|
62
|
+
*/
|
|
63
|
+
private static assertMptIsValid;
|
|
64
|
+
/**
|
|
65
|
+
* Ensure that the value after being multiplied by the exponent does not
|
|
66
|
+
* contain a decimal.
|
|
67
|
+
*
|
|
68
|
+
* @param decimal a Decimal object
|
|
69
|
+
* @returns a string of the object without a decimal
|
|
70
|
+
*/
|
|
71
|
+
private static verifyNoDecimal;
|
|
72
|
+
/**
|
|
73
|
+
* Test if this amount is in units of Native Currency(XRP)
|
|
74
|
+
*
|
|
75
|
+
* @returns true if Native (XRP)
|
|
76
|
+
*/
|
|
77
|
+
private isNative;
|
|
78
|
+
/**
|
|
79
|
+
* Test if this amount is in units of MPT
|
|
80
|
+
*
|
|
81
|
+
* @returns true if MPT
|
|
82
|
+
*/
|
|
83
|
+
private isMPT;
|
|
84
|
+
/**
|
|
85
|
+
* Test if this amount is in units of IOU
|
|
86
|
+
*
|
|
87
|
+
* @returns true if IOU
|
|
88
|
+
*/
|
|
89
|
+
private isIOU;
|
|
90
|
+
getSType(): SerializedTypeID;
|
|
91
|
+
}
|
|
92
|
+
export { Amount, AmountObject };
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Amount = void 0;
|
|
7
|
+
const binary_parser_1 = require("../serdes/binary-parser");
|
|
8
|
+
const account_id_1 = require("./account-id");
|
|
9
|
+
const currency_1 = require("./currency");
|
|
10
|
+
const serialized_type_1 = require("./serialized-type");
|
|
11
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
12
|
+
const utils_1 = require("@transia/isomorphic/utils");
|
|
13
|
+
const utils_2 = require("../utils");
|
|
14
|
+
const hash_192_1 = require("./hash-192");
|
|
15
|
+
/**
|
|
16
|
+
* Constants for validating amounts
|
|
17
|
+
*/
|
|
18
|
+
const MIN_IOU_EXPONENT = -96;
|
|
19
|
+
const MAX_IOU_EXPONENT = 80;
|
|
20
|
+
const MAX_IOU_PRECISION = 16;
|
|
21
|
+
const MAX_DROPS = new bignumber_js_1.default('1e17');
|
|
22
|
+
const MIN_XRP = new bignumber_js_1.default('1e-6');
|
|
23
|
+
const mask = BigInt(0x00000000ffffffff);
|
|
24
|
+
const mptMask = BigInt(0x8000000000000000);
|
|
25
|
+
/**
|
|
26
|
+
* BigNumber configuration for Amount IOUs
|
|
27
|
+
*/
|
|
28
|
+
bignumber_js_1.default.config({
|
|
29
|
+
EXPONENTIAL_AT: [
|
|
30
|
+
MIN_IOU_EXPONENT - MAX_IOU_PRECISION,
|
|
31
|
+
MAX_IOU_EXPONENT + MAX_IOU_PRECISION,
|
|
32
|
+
],
|
|
33
|
+
});
|
|
34
|
+
/**
|
|
35
|
+
* Type guard for AmountObjectIOU
|
|
36
|
+
*/
|
|
37
|
+
function isAmountObjectIOU(arg) {
|
|
38
|
+
const keys = Object.keys(arg).sort();
|
|
39
|
+
return (keys.length === 3 &&
|
|
40
|
+
keys[0] === 'currency' &&
|
|
41
|
+
keys[1] === 'issuer' &&
|
|
42
|
+
keys[2] === 'value');
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Type guard for AmountObjectMPT
|
|
46
|
+
*/
|
|
47
|
+
function isAmountObjectMPT(arg) {
|
|
48
|
+
const keys = Object.keys(arg).sort();
|
|
49
|
+
return (keys.length === 2 && keys[0] === 'mpt_issuance_id' && keys[1] === 'value');
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Class for serializing/Deserializing Amounts
|
|
53
|
+
*/
|
|
54
|
+
class Amount extends serialized_type_1.SerializedType {
|
|
55
|
+
constructor(bytes) {
|
|
56
|
+
super(bytes !== null && bytes !== void 0 ? bytes : Amount.defaultAmount.bytes);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Construct an amount from an IOU, MPT or string amount
|
|
60
|
+
*
|
|
61
|
+
* @param value An Amount, object representing an IOU, or a string
|
|
62
|
+
* representing an integer amount
|
|
63
|
+
* @returns An Amount object
|
|
64
|
+
*/
|
|
65
|
+
static from(value) {
|
|
66
|
+
if (value instanceof Amount) {
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
let amount = new Uint8Array(8);
|
|
70
|
+
if (typeof value === 'string') {
|
|
71
|
+
Amount.assertXrpIsValid(value);
|
|
72
|
+
const number = BigInt(value);
|
|
73
|
+
const intBuf = [new Uint8Array(4), new Uint8Array(4)];
|
|
74
|
+
(0, utils_2.writeUInt32BE)(intBuf[0], Number(number >> BigInt(32)), 0);
|
|
75
|
+
(0, utils_2.writeUInt32BE)(intBuf[1], Number(number & BigInt(mask)), 0);
|
|
76
|
+
amount = (0, utils_1.concat)(intBuf);
|
|
77
|
+
amount[0] |= 0x40;
|
|
78
|
+
return new Amount(amount);
|
|
79
|
+
}
|
|
80
|
+
if (isAmountObjectIOU(value)) {
|
|
81
|
+
const number = new bignumber_js_1.default(value.value);
|
|
82
|
+
Amount.assertIouIsValid(number);
|
|
83
|
+
if (number.isZero()) {
|
|
84
|
+
amount[0] |= 0x80;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const integerNumberString = number
|
|
88
|
+
.times(`1e${-((number.e || 0) - 15)}`)
|
|
89
|
+
.abs()
|
|
90
|
+
.toString();
|
|
91
|
+
const num = BigInt(integerNumberString);
|
|
92
|
+
const intBuf = [new Uint8Array(4), new Uint8Array(4)];
|
|
93
|
+
(0, utils_2.writeUInt32BE)(intBuf[0], Number(num >> BigInt(32)), 0);
|
|
94
|
+
(0, utils_2.writeUInt32BE)(intBuf[1], Number(num & BigInt(mask)), 0);
|
|
95
|
+
amount = (0, utils_1.concat)(intBuf);
|
|
96
|
+
amount[0] |= 0x80;
|
|
97
|
+
if (number.gt(new bignumber_js_1.default(0))) {
|
|
98
|
+
amount[0] |= 0x40;
|
|
99
|
+
}
|
|
100
|
+
const exponent = (number.e || 0) - 15;
|
|
101
|
+
const exponentByte = 97 + exponent;
|
|
102
|
+
amount[0] |= exponentByte >>> 2;
|
|
103
|
+
amount[1] |= (exponentByte & 0x03) << 6;
|
|
104
|
+
}
|
|
105
|
+
const currency = currency_1.Currency.from(value.currency).toBytes();
|
|
106
|
+
const issuer = account_id_1.AccountID.from(value.issuer).toBytes();
|
|
107
|
+
return new Amount((0, utils_1.concat)([amount, currency, issuer]));
|
|
108
|
+
}
|
|
109
|
+
if (isAmountObjectMPT(value)) {
|
|
110
|
+
Amount.assertMptIsValid(value.value);
|
|
111
|
+
let leadingByte = new Uint8Array(1);
|
|
112
|
+
leadingByte[0] |= 0x60;
|
|
113
|
+
const num = BigInt(value.value);
|
|
114
|
+
const intBuf = [new Uint8Array(4), new Uint8Array(4)];
|
|
115
|
+
(0, utils_2.writeUInt32BE)(intBuf[0], Number(num >> BigInt(32)), 0);
|
|
116
|
+
(0, utils_2.writeUInt32BE)(intBuf[1], Number(num & BigInt(mask)), 0);
|
|
117
|
+
amount = (0, utils_1.concat)(intBuf);
|
|
118
|
+
const mptIssuanceID = hash_192_1.Hash192.from(value.mpt_issuance_id).toBytes();
|
|
119
|
+
return new Amount((0, utils_1.concat)([leadingByte, amount, mptIssuanceID]));
|
|
120
|
+
}
|
|
121
|
+
throw new Error('Invalid type to construct an Amount');
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Read an amount from a BinaryParser
|
|
125
|
+
*
|
|
126
|
+
* @param parser BinaryParser to read the Amount from
|
|
127
|
+
* @returns An Amount object
|
|
128
|
+
*/
|
|
129
|
+
static fromParser(parser) {
|
|
130
|
+
const isIOU = parser.peek() & 0x80;
|
|
131
|
+
if (isIOU)
|
|
132
|
+
return new Amount(parser.read(48));
|
|
133
|
+
// the amount can be either MPT or XRP at this point
|
|
134
|
+
const isMPT = parser.peek() & 0x20;
|
|
135
|
+
const numBytes = isMPT ? 33 : 8;
|
|
136
|
+
return new Amount(parser.read(numBytes));
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Get the JSON representation of this Amount
|
|
140
|
+
*
|
|
141
|
+
* @returns the JSON interpretation of this.bytes
|
|
142
|
+
*/
|
|
143
|
+
toJSON() {
|
|
144
|
+
if (this.isNative()) {
|
|
145
|
+
const bytes = this.bytes;
|
|
146
|
+
const isPositive = bytes[0] & 0x40;
|
|
147
|
+
const sign = isPositive ? '' : '-';
|
|
148
|
+
bytes[0] &= 0x3f;
|
|
149
|
+
const msb = BigInt((0, utils_2.readUInt32BE)(bytes.slice(0, 4), 0));
|
|
150
|
+
const lsb = BigInt((0, utils_2.readUInt32BE)(bytes.slice(4), 0));
|
|
151
|
+
const num = (msb << BigInt(32)) | lsb;
|
|
152
|
+
return `${sign}${num.toString()}`;
|
|
153
|
+
}
|
|
154
|
+
if (this.isIOU()) {
|
|
155
|
+
const parser = new binary_parser_1.BinaryParser(this.toString());
|
|
156
|
+
const mantissa = parser.read(8);
|
|
157
|
+
const currency = currency_1.Currency.fromParser(parser);
|
|
158
|
+
const issuer = account_id_1.AccountID.fromParser(parser);
|
|
159
|
+
const b1 = mantissa[0];
|
|
160
|
+
const b2 = mantissa[1];
|
|
161
|
+
const isPositive = b1 & 0x40;
|
|
162
|
+
const sign = isPositive ? '' : '-';
|
|
163
|
+
const exponent = ((b1 & 0x3f) << 2) + ((b2 & 0xff) >> 6) - 97;
|
|
164
|
+
mantissa[0] = 0;
|
|
165
|
+
mantissa[1] &= 0x3f;
|
|
166
|
+
const value = new bignumber_js_1.default(`${sign}0x${(0, utils_1.bytesToHex)(mantissa)}`).times(`1e${exponent}`);
|
|
167
|
+
Amount.assertIouIsValid(value);
|
|
168
|
+
return {
|
|
169
|
+
value: value.toString(),
|
|
170
|
+
currency: currency.toJSON(),
|
|
171
|
+
issuer: issuer.toJSON(),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
if (this.isMPT()) {
|
|
175
|
+
const parser = new binary_parser_1.BinaryParser(this.toString());
|
|
176
|
+
const leadingByte = parser.read(1);
|
|
177
|
+
const amount = parser.read(8);
|
|
178
|
+
const mptID = hash_192_1.Hash192.fromParser(parser);
|
|
179
|
+
const isPositive = leadingByte[0] & 0x40;
|
|
180
|
+
const sign = isPositive ? '' : '-';
|
|
181
|
+
const msb = BigInt((0, utils_2.readUInt32BE)(amount.slice(0, 4), 0));
|
|
182
|
+
const lsb = BigInt((0, utils_2.readUInt32BE)(amount.slice(4), 0));
|
|
183
|
+
const num = (msb << BigInt(32)) | lsb;
|
|
184
|
+
return {
|
|
185
|
+
value: `${sign}${num.toString()}`,
|
|
186
|
+
mpt_issuance_id: mptID.toString(),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
throw new Error('Invalid amount to construct JSON');
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Validate XRP amount
|
|
193
|
+
*
|
|
194
|
+
* @param amount String representing XRP amount
|
|
195
|
+
* @returns void, but will throw if invalid amount
|
|
196
|
+
*/
|
|
197
|
+
static assertXrpIsValid(amount) {
|
|
198
|
+
if (amount.indexOf('.') !== -1) {
|
|
199
|
+
throw new Error(`${amount.toString()} is an illegal amount`);
|
|
200
|
+
}
|
|
201
|
+
const decimal = new bignumber_js_1.default(amount);
|
|
202
|
+
if (!decimal.isZero()) {
|
|
203
|
+
if (decimal.lt(MIN_XRP) || decimal.gt(MAX_DROPS)) {
|
|
204
|
+
throw new Error(`${amount.toString()} is an illegal amount`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Validate IOU.value amount
|
|
210
|
+
*
|
|
211
|
+
* @param decimal BigNumber object representing IOU.value
|
|
212
|
+
* @returns void, but will throw if invalid amount
|
|
213
|
+
*/
|
|
214
|
+
static assertIouIsValid(decimal) {
|
|
215
|
+
if (!decimal.isZero()) {
|
|
216
|
+
const p = decimal.precision();
|
|
217
|
+
const e = (decimal.e || 0) - 15;
|
|
218
|
+
if (p > MAX_IOU_PRECISION ||
|
|
219
|
+
e > MAX_IOU_EXPONENT ||
|
|
220
|
+
e < MIN_IOU_EXPONENT) {
|
|
221
|
+
throw new Error('Decimal precision out of range');
|
|
222
|
+
}
|
|
223
|
+
this.verifyNoDecimal(decimal);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Validate MPT.value amount
|
|
228
|
+
*
|
|
229
|
+
* @param decimal BigNumber object representing MPT.value
|
|
230
|
+
* @returns void, but will throw if invalid amount
|
|
231
|
+
*/
|
|
232
|
+
static assertMptIsValid(amount) {
|
|
233
|
+
if (amount.indexOf('.') !== -1) {
|
|
234
|
+
throw new Error(`${amount.toString()} is an illegal amount`);
|
|
235
|
+
}
|
|
236
|
+
const decimal = new bignumber_js_1.default(amount);
|
|
237
|
+
if (!decimal.isZero()) {
|
|
238
|
+
if (decimal < (0, bignumber_js_1.default)(0)) {
|
|
239
|
+
throw new Error(`${amount.toString()} is an illegal amount`);
|
|
240
|
+
}
|
|
241
|
+
if (Number(BigInt(amount) & BigInt(mptMask)) != 0) {
|
|
242
|
+
throw new Error(`${amount.toString()} is an illegal amount`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Ensure that the value after being multiplied by the exponent does not
|
|
248
|
+
* contain a decimal.
|
|
249
|
+
*
|
|
250
|
+
* @param decimal a Decimal object
|
|
251
|
+
* @returns a string of the object without a decimal
|
|
252
|
+
*/
|
|
253
|
+
static verifyNoDecimal(decimal) {
|
|
254
|
+
const integerNumberString = decimal
|
|
255
|
+
.times(`1e${-((decimal.e || 0) - 15)}`)
|
|
256
|
+
.abs()
|
|
257
|
+
.toString();
|
|
258
|
+
if (integerNumberString.indexOf('.') !== -1) {
|
|
259
|
+
throw new Error('Decimal place found in integerNumberString');
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Test if this amount is in units of Native Currency(XRP)
|
|
264
|
+
*
|
|
265
|
+
* @returns true if Native (XRP)
|
|
266
|
+
*/
|
|
267
|
+
isNative() {
|
|
268
|
+
return (this.bytes[0] & 0x80) === 0 && (this.bytes[0] & 0x20) === 0;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Test if this amount is in units of MPT
|
|
272
|
+
*
|
|
273
|
+
* @returns true if MPT
|
|
274
|
+
*/
|
|
275
|
+
isMPT() {
|
|
276
|
+
return (this.bytes[0] & 0x80) === 0 && (this.bytes[0] & 0x20) !== 0;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Test if this amount is in units of IOU
|
|
280
|
+
*
|
|
281
|
+
* @returns true if IOU
|
|
282
|
+
*/
|
|
283
|
+
isIOU() {
|
|
284
|
+
return (this.bytes[0] & 0x80) !== 0;
|
|
285
|
+
}
|
|
286
|
+
getSType() {
|
|
287
|
+
return serialized_type_1.SerializedTypeID.STI_AMOUNT;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
exports.Amount = Amount;
|
|
291
|
+
Amount.defaultAmount = new Amount((0, utils_1.hexToBytes)('4000000000000000'));
|
|
292
|
+
//# sourceMappingURL=amount.js.map
|