@transia/ripple-binary-codec 1.4.6-alpha.9 → 2.5.2-alpha.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/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,294 @@
|
|
|
1
|
+
import { AccountID } from './account-id'
|
|
2
|
+
import { Currency } from './currency'
|
|
3
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
4
|
+
import { SerializedType, JsonObject, SerializedTypeID } from './serialized-type'
|
|
5
|
+
import { bytesToHex, concat } from '@transia/isomorphic/utils'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Constants for separating Paths in a PathSet
|
|
9
|
+
*/
|
|
10
|
+
const PATHSET_END_BYTE = 0x00
|
|
11
|
+
const PATH_SEPARATOR_BYTE = 0xff
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Constant for masking types of a Hop
|
|
15
|
+
*/
|
|
16
|
+
const TYPE_ACCOUNT = 0x01
|
|
17
|
+
const TYPE_CURRENCY = 0x10
|
|
18
|
+
const TYPE_ISSUER = 0x20
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The object representation of a Hop, an issuer AccountID, an account AccountID, and a Currency
|
|
22
|
+
*/
|
|
23
|
+
interface HopObject extends JsonObject {
|
|
24
|
+
issuer?: string
|
|
25
|
+
account?: string
|
|
26
|
+
currency?: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* TypeGuard for HopObject
|
|
31
|
+
*/
|
|
32
|
+
function isHopObject(arg): arg is HopObject {
|
|
33
|
+
return (
|
|
34
|
+
arg.issuer !== undefined ||
|
|
35
|
+
arg.account !== undefined ||
|
|
36
|
+
arg.currency !== undefined
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* TypeGuard for PathSet
|
|
42
|
+
*/
|
|
43
|
+
function isPathSet(arg): arg is Array<Array<HopObject>> {
|
|
44
|
+
return (
|
|
45
|
+
(Array.isArray(arg) && arg.length === 0) ||
|
|
46
|
+
(Array.isArray(arg) && Array.isArray(arg[0]) && arg[0].length === 0) ||
|
|
47
|
+
(Array.isArray(arg) && Array.isArray(arg[0]) && isHopObject(arg[0][0]))
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Serialize and Deserialize a Hop
|
|
53
|
+
*/
|
|
54
|
+
class Hop extends SerializedType {
|
|
55
|
+
/**
|
|
56
|
+
* Create a Hop from a HopObject
|
|
57
|
+
*
|
|
58
|
+
* @param value Either a hop or HopObject to create a hop with
|
|
59
|
+
* @returns a Hop
|
|
60
|
+
*/
|
|
61
|
+
static from(value: Hop | HopObject): Hop {
|
|
62
|
+
if (value instanceof Hop) {
|
|
63
|
+
return value
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const bytes: Array<Uint8Array> = [Uint8Array.from([0])]
|
|
67
|
+
|
|
68
|
+
if (value.account) {
|
|
69
|
+
bytes.push(AccountID.from(value.account).toBytes())
|
|
70
|
+
bytes[0][0] |= TYPE_ACCOUNT
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (value.currency) {
|
|
74
|
+
bytes.push(Currency.from(value.currency).toBytes())
|
|
75
|
+
bytes[0][0] |= TYPE_CURRENCY
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (value.issuer) {
|
|
79
|
+
bytes.push(AccountID.from(value.issuer).toBytes())
|
|
80
|
+
bytes[0][0] |= TYPE_ISSUER
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return new Hop(concat(bytes))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Construct a Hop from a BinaryParser
|
|
88
|
+
*
|
|
89
|
+
* @param parser BinaryParser to read the Hop from
|
|
90
|
+
* @returns a Hop
|
|
91
|
+
*/
|
|
92
|
+
static fromParser(parser: BinaryParser): Hop {
|
|
93
|
+
const type = parser.readUInt8()
|
|
94
|
+
const bytes: Array<Uint8Array> = [Uint8Array.from([type])]
|
|
95
|
+
|
|
96
|
+
if (type & TYPE_ACCOUNT) {
|
|
97
|
+
bytes.push(parser.read(AccountID.width))
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (type & TYPE_CURRENCY) {
|
|
101
|
+
bytes.push(parser.read(Currency.width))
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (type & TYPE_ISSUER) {
|
|
105
|
+
bytes.push(parser.read(AccountID.width))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return new Hop(concat(bytes))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Get the JSON interpretation of this hop
|
|
113
|
+
*
|
|
114
|
+
* @returns a HopObject, an JS object with optional account, issuer, and currency
|
|
115
|
+
*/
|
|
116
|
+
toJSON(): HopObject {
|
|
117
|
+
const hopParser = new BinaryParser(bytesToHex(this.bytes))
|
|
118
|
+
const type = hopParser.readUInt8()
|
|
119
|
+
|
|
120
|
+
let account, currency, issuer
|
|
121
|
+
if (type & TYPE_ACCOUNT) {
|
|
122
|
+
account = (AccountID.fromParser(hopParser) as AccountID).toJSON()
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (type & TYPE_CURRENCY) {
|
|
126
|
+
currency = (Currency.fromParser(hopParser) as Currency).toJSON()
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (type & TYPE_ISSUER) {
|
|
130
|
+
issuer = (AccountID.fromParser(hopParser) as AccountID).toJSON()
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const result: HopObject = {}
|
|
134
|
+
if (account) {
|
|
135
|
+
result.account = account
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (issuer) {
|
|
139
|
+
result.issuer = issuer
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (currency) {
|
|
143
|
+
result.currency = currency
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return result
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* get a number representing the type of this hop
|
|
151
|
+
*
|
|
152
|
+
* @returns a number to be bitwise and-ed with TYPE_ constants to describe the types in the hop
|
|
153
|
+
*/
|
|
154
|
+
type(): number {
|
|
155
|
+
return this.bytes[0]
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Class for serializing/deserializing Paths
|
|
161
|
+
*/
|
|
162
|
+
class Path extends SerializedType {
|
|
163
|
+
/**
|
|
164
|
+
* construct a Path from an array of Hops
|
|
165
|
+
*
|
|
166
|
+
* @param value Path or array of HopObjects to construct a Path
|
|
167
|
+
* @returns the Path
|
|
168
|
+
*/
|
|
169
|
+
static from(value: Path | Array<HopObject>): Path {
|
|
170
|
+
if (value instanceof Path) {
|
|
171
|
+
return value
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const bytes: Array<Uint8Array> = []
|
|
175
|
+
value.forEach((hop: HopObject) => {
|
|
176
|
+
bytes.push(Hop.from(hop).toBytes())
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
return new Path(concat(bytes))
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Read a Path from a BinaryParser
|
|
184
|
+
*
|
|
185
|
+
* @param parser BinaryParser to read Path from
|
|
186
|
+
* @returns the Path represented by the bytes read from the BinaryParser
|
|
187
|
+
*/
|
|
188
|
+
static fromParser(parser: BinaryParser): Path {
|
|
189
|
+
const bytes: Array<Uint8Array> = []
|
|
190
|
+
while (!parser.end()) {
|
|
191
|
+
bytes.push(Hop.fromParser(parser).toBytes())
|
|
192
|
+
|
|
193
|
+
if (
|
|
194
|
+
parser.peek() === PATHSET_END_BYTE ||
|
|
195
|
+
parser.peek() === PATH_SEPARATOR_BYTE
|
|
196
|
+
) {
|
|
197
|
+
break
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return new Path(concat(bytes))
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Get the JSON representation of this Path
|
|
205
|
+
*
|
|
206
|
+
* @returns an Array of HopObject constructed from this.bytes
|
|
207
|
+
*/
|
|
208
|
+
toJSON(): Array<HopObject> {
|
|
209
|
+
const json: Array<HopObject> = []
|
|
210
|
+
const pathParser = new BinaryParser(this.toString())
|
|
211
|
+
|
|
212
|
+
while (!pathParser.end()) {
|
|
213
|
+
json.push(Hop.fromParser(pathParser).toJSON())
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return json
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Deserialize and Serialize the PathSet type
|
|
222
|
+
*/
|
|
223
|
+
class PathSet extends SerializedType {
|
|
224
|
+
/**
|
|
225
|
+
* Construct a PathSet from an Array of Arrays representing paths
|
|
226
|
+
*
|
|
227
|
+
* @param value A PathSet or Array of Array of HopObjects
|
|
228
|
+
* @returns the PathSet constructed from value
|
|
229
|
+
*/
|
|
230
|
+
static from<T extends PathSet | Array<Array<HopObject>>>(value: T): PathSet {
|
|
231
|
+
if (value instanceof PathSet) {
|
|
232
|
+
return value
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (isPathSet(value)) {
|
|
236
|
+
const bytes: Array<Uint8Array> = []
|
|
237
|
+
|
|
238
|
+
value.forEach((path: Array<HopObject>) => {
|
|
239
|
+
bytes.push(Path.from(path).toBytes())
|
|
240
|
+
bytes.push(Uint8Array.from([PATH_SEPARATOR_BYTE]))
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
bytes[bytes.length - 1] = Uint8Array.from([PATHSET_END_BYTE])
|
|
244
|
+
|
|
245
|
+
return new PathSet(concat(bytes))
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
throw new Error('Cannot construct PathSet from given value')
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Construct a PathSet from a BinaryParser
|
|
253
|
+
*
|
|
254
|
+
* @param parser A BinaryParser to read PathSet from
|
|
255
|
+
* @returns the PathSet read from parser
|
|
256
|
+
*/
|
|
257
|
+
static fromParser(parser: BinaryParser): PathSet {
|
|
258
|
+
const bytes: Array<Uint8Array> = []
|
|
259
|
+
|
|
260
|
+
while (!parser.end()) {
|
|
261
|
+
bytes.push(Path.fromParser(parser).toBytes())
|
|
262
|
+
bytes.push(parser.read(1))
|
|
263
|
+
|
|
264
|
+
if (bytes[bytes.length - 1][0] == PATHSET_END_BYTE) {
|
|
265
|
+
break
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return new PathSet(concat(bytes))
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Get the JSON representation of this PathSet
|
|
274
|
+
*
|
|
275
|
+
* @returns an Array of Array of HopObjects, representing this PathSet
|
|
276
|
+
*/
|
|
277
|
+
toJSON(): Array<Array<HopObject>> {
|
|
278
|
+
const json: Array<Array<HopObject>> = []
|
|
279
|
+
const pathParser = new BinaryParser(this.toString())
|
|
280
|
+
|
|
281
|
+
while (!pathParser.end()) {
|
|
282
|
+
json.push(Path.fromParser(pathParser).toJSON())
|
|
283
|
+
pathParser.skip(1)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return json
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
getSType(): SerializedTypeID {
|
|
290
|
+
return SerializedTypeID.STI_PATHSET
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export { PathSet }
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { BytesList } from '../serdes/binary-serializer'
|
|
2
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
+
import { XrplDefinitionsBase } from '../enums'
|
|
4
|
+
import { bytesToHex } from '@transia/isomorphic/utils'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Enum for SerializedTypeID values used in XRPL
|
|
8
|
+
* These match the C++ implementation's STI_ constants
|
|
9
|
+
*/
|
|
10
|
+
export enum SerializedTypeID {
|
|
11
|
+
STI_NOTPRESENT = 0,
|
|
12
|
+
STI_UINT16 = 1,
|
|
13
|
+
STI_UINT32 = 2,
|
|
14
|
+
STI_UINT64 = 3,
|
|
15
|
+
STI_UINT128 = 4,
|
|
16
|
+
STI_UINT256 = 5,
|
|
17
|
+
STI_AMOUNT = 6,
|
|
18
|
+
STI_VL = 7,
|
|
19
|
+
STI_ACCOUNT = 8,
|
|
20
|
+
STI_NUMBER = 9,
|
|
21
|
+
STI_INT32 = 10,
|
|
22
|
+
STI_INT64 = 11,
|
|
23
|
+
|
|
24
|
+
STI_OBJECT = 14,
|
|
25
|
+
STI_ARRAY = 15,
|
|
26
|
+
|
|
27
|
+
STI_UINT8 = 16,
|
|
28
|
+
STI_UINT160 = 17,
|
|
29
|
+
STI_PATHSET = 18,
|
|
30
|
+
STI_VECTOR256 = 19,
|
|
31
|
+
STI_UINT96 = 20,
|
|
32
|
+
STI_UINT192 = 21,
|
|
33
|
+
STI_UINT384 = 22,
|
|
34
|
+
STI_UINT512 = 23,
|
|
35
|
+
STI_ISSUE = 24,
|
|
36
|
+
STI_XCHAIN_BRIDGE = 25,
|
|
37
|
+
STI_CURRENCY = 26,
|
|
38
|
+
STI_DATA = 27,
|
|
39
|
+
STI_DATATYPE = 28,
|
|
40
|
+
STI_JSON = 29,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Map of type strings to SerializedTypeID values
|
|
45
|
+
*/
|
|
46
|
+
export const TYPE_STRING_TO_ID: Record<string, SerializedTypeID> = {
|
|
47
|
+
NOTPRESENT: SerializedTypeID.STI_NOTPRESENT,
|
|
48
|
+
UINT16: SerializedTypeID.STI_UINT16,
|
|
49
|
+
UINT32: SerializedTypeID.STI_UINT32,
|
|
50
|
+
UINT64: SerializedTypeID.STI_UINT64,
|
|
51
|
+
UINT128: SerializedTypeID.STI_UINT128,
|
|
52
|
+
UINT256: SerializedTypeID.STI_UINT256,
|
|
53
|
+
AMOUNT: SerializedTypeID.STI_AMOUNT,
|
|
54
|
+
VL: SerializedTypeID.STI_VL,
|
|
55
|
+
ACCOUNT: SerializedTypeID.STI_ACCOUNT,
|
|
56
|
+
NUMBER: SerializedTypeID.STI_NUMBER,
|
|
57
|
+
INT32: SerializedTypeID.STI_INT32,
|
|
58
|
+
INT64: SerializedTypeID.STI_INT64,
|
|
59
|
+
|
|
60
|
+
OBJECT: SerializedTypeID.STI_OBJECT,
|
|
61
|
+
ARRAY: SerializedTypeID.STI_ARRAY,
|
|
62
|
+
|
|
63
|
+
UINT8: SerializedTypeID.STI_UINT8,
|
|
64
|
+
UINT160: SerializedTypeID.STI_UINT160,
|
|
65
|
+
PATHSET: SerializedTypeID.STI_PATHSET,
|
|
66
|
+
VECTOR256: SerializedTypeID.STI_VECTOR256,
|
|
67
|
+
UINT96: SerializedTypeID.STI_UINT96,
|
|
68
|
+
UINT192: SerializedTypeID.STI_UINT192,
|
|
69
|
+
UINT384: SerializedTypeID.STI_UINT384,
|
|
70
|
+
UINT512: SerializedTypeID.STI_UINT512,
|
|
71
|
+
ISSUE: SerializedTypeID.STI_ISSUE,
|
|
72
|
+
XCHAIN_BRIDGE: SerializedTypeID.STI_XCHAIN_BRIDGE,
|
|
73
|
+
CURRENCY: SerializedTypeID.STI_CURRENCY,
|
|
74
|
+
DATA: SerializedTypeID.STI_DATA,
|
|
75
|
+
DATATYPE: SerializedTypeID.STI_DATATYPE,
|
|
76
|
+
JSON: SerializedTypeID.STI_JSON,
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Map of type strings to SerializedTypeID values
|
|
81
|
+
*/
|
|
82
|
+
export const TYPE_NUMBER_TO_ID: Record<number, SerializedTypeID> = {
|
|
83
|
+
0: SerializedTypeID.STI_NOTPRESENT,
|
|
84
|
+
1: SerializedTypeID.STI_UINT16,
|
|
85
|
+
2: SerializedTypeID.STI_UINT32,
|
|
86
|
+
3: SerializedTypeID.STI_UINT64,
|
|
87
|
+
4: SerializedTypeID.STI_UINT128,
|
|
88
|
+
5: SerializedTypeID.STI_UINT256,
|
|
89
|
+
6: SerializedTypeID.STI_AMOUNT,
|
|
90
|
+
7: SerializedTypeID.STI_VL,
|
|
91
|
+
8: SerializedTypeID.STI_ACCOUNT,
|
|
92
|
+
9: SerializedTypeID.STI_NUMBER,
|
|
93
|
+
10: SerializedTypeID.STI_INT32,
|
|
94
|
+
11: SerializedTypeID.STI_INT64,
|
|
95
|
+
|
|
96
|
+
14: SerializedTypeID.STI_OBJECT,
|
|
97
|
+
15: SerializedTypeID.STI_ARRAY,
|
|
98
|
+
|
|
99
|
+
16: SerializedTypeID.STI_UINT8,
|
|
100
|
+
17: SerializedTypeID.STI_UINT160,
|
|
101
|
+
18: SerializedTypeID.STI_PATHSET,
|
|
102
|
+
19: SerializedTypeID.STI_VECTOR256,
|
|
103
|
+
20: SerializedTypeID.STI_UINT96,
|
|
104
|
+
21: SerializedTypeID.STI_UINT192,
|
|
105
|
+
22: SerializedTypeID.STI_UINT384,
|
|
106
|
+
23: SerializedTypeID.STI_UINT512,
|
|
107
|
+
24: SerializedTypeID.STI_ISSUE,
|
|
108
|
+
25: SerializedTypeID.STI_XCHAIN_BRIDGE,
|
|
109
|
+
26: SerializedTypeID.STI_CURRENCY,
|
|
110
|
+
27: SerializedTypeID.STI_DATA,
|
|
111
|
+
28: SerializedTypeID.STI_DATATYPE,
|
|
112
|
+
29: SerializedTypeID.STI_JSON,
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Map of SerializedTypeID values to type strings
|
|
117
|
+
*/
|
|
118
|
+
export const TYPE_ID_TO_STRING: Record<SerializedTypeID, string> = {
|
|
119
|
+
[SerializedTypeID.STI_NOTPRESENT]: '',
|
|
120
|
+
[SerializedTypeID.STI_UINT16]: 'UINT16',
|
|
121
|
+
[SerializedTypeID.STI_UINT32]: 'UINT32',
|
|
122
|
+
[SerializedTypeID.STI_UINT64]: 'UINT64',
|
|
123
|
+
[SerializedTypeID.STI_UINT128]: 'UINT128',
|
|
124
|
+
[SerializedTypeID.STI_UINT256]: 'UINT256',
|
|
125
|
+
[SerializedTypeID.STI_AMOUNT]: 'AMOUNT',
|
|
126
|
+
[SerializedTypeID.STI_VL]: 'VL',
|
|
127
|
+
[SerializedTypeID.STI_ACCOUNT]: 'ACCOUNT',
|
|
128
|
+
[SerializedTypeID.STI_NUMBER]: 'NUMBER',
|
|
129
|
+
[SerializedTypeID.STI_INT32]: 'INT32',
|
|
130
|
+
[SerializedTypeID.STI_INT64]: 'INT64',
|
|
131
|
+
|
|
132
|
+
[SerializedTypeID.STI_OBJECT]: 'OBJECT',
|
|
133
|
+
[SerializedTypeID.STI_ARRAY]: 'ARRAY',
|
|
134
|
+
|
|
135
|
+
[SerializedTypeID.STI_UINT8]: 'UINT8',
|
|
136
|
+
[SerializedTypeID.STI_UINT160]: 'UINT160',
|
|
137
|
+
[SerializedTypeID.STI_PATHSET]: 'PATHSET',
|
|
138
|
+
[SerializedTypeID.STI_VECTOR256]: 'VECTOR256',
|
|
139
|
+
[SerializedTypeID.STI_UINT96]: 'UINT96',
|
|
140
|
+
[SerializedTypeID.STI_UINT192]: 'UINT192',
|
|
141
|
+
[SerializedTypeID.STI_UINT384]: 'UINT384',
|
|
142
|
+
[SerializedTypeID.STI_UINT512]: 'UINT512',
|
|
143
|
+
[SerializedTypeID.STI_ISSUE]: 'ISSUE',
|
|
144
|
+
[SerializedTypeID.STI_XCHAIN_BRIDGE]: 'XCHAIN_BRIDGE',
|
|
145
|
+
[SerializedTypeID.STI_CURRENCY]: 'CURRENCY',
|
|
146
|
+
[SerializedTypeID.STI_DATA]: 'DATA',
|
|
147
|
+
[SerializedTypeID.STI_DATATYPE]: 'DATATYPE',
|
|
148
|
+
[SerializedTypeID.STI_JSON]: 'JSON',
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
type JSON = string | number | boolean | null | undefined | JSON[] | JsonObject
|
|
152
|
+
|
|
153
|
+
type JsonObject = { [key: string]: JSON }
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The base class for all binary-codec types
|
|
157
|
+
*/
|
|
158
|
+
class SerializedType {
|
|
159
|
+
protected readonly bytes: Uint8Array = new Uint8Array(0)
|
|
160
|
+
|
|
161
|
+
constructor(bytes?: Uint8Array) {
|
|
162
|
+
this.bytes = bytes ?? new Uint8Array(0)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
static fromParser(parser: BinaryParser, hint?: number): SerializedType {
|
|
166
|
+
throw new Error('fromParser not implemented')
|
|
167
|
+
return this.fromParser(parser, hint)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
static from(value: SerializedType | JSON | bigint): SerializedType {
|
|
171
|
+
throw new Error('from not implemented')
|
|
172
|
+
return this.from(value)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Write the bytes representation of a SerializedType to a BytesList
|
|
177
|
+
*
|
|
178
|
+
* @param list The BytesList to write SerializedType bytes to
|
|
179
|
+
*/
|
|
180
|
+
toBytesSink(list: BytesList): void {
|
|
181
|
+
list.put(this.bytes)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Get the hex representation of a SerializedType's bytes
|
|
186
|
+
*
|
|
187
|
+
* @returns hex String of this.bytes
|
|
188
|
+
*/
|
|
189
|
+
toHex(): string {
|
|
190
|
+
return bytesToHex(this.toBytes())
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Get the bytes representation of a SerializedType
|
|
195
|
+
*
|
|
196
|
+
* @returns A Uint8Array of the bytes
|
|
197
|
+
*/
|
|
198
|
+
toBytes(): Uint8Array {
|
|
199
|
+
if (this.bytes) {
|
|
200
|
+
return this.bytes
|
|
201
|
+
}
|
|
202
|
+
const bytes = new BytesList()
|
|
203
|
+
this.toBytesSink(bytes)
|
|
204
|
+
return bytes.toBytes()
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Return the JSON representation of a SerializedType
|
|
209
|
+
*
|
|
210
|
+
* @param _definitions rippled definitions used to parse the values of transaction types and such.
|
|
211
|
+
* Unused in default, but used in STObject, STArray
|
|
212
|
+
* Can be customized for sidechains and amendments.
|
|
213
|
+
* @returns any type, if not overloaded returns hexString representation of bytes
|
|
214
|
+
*/
|
|
215
|
+
toJSON(_definitions?: XrplDefinitionsBase, _fieldName?: string): JSON {
|
|
216
|
+
return this.toHex()
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* @returns hexString representation of this.bytes
|
|
221
|
+
*/
|
|
222
|
+
toString(): string {
|
|
223
|
+
return this.toHex()
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
getSType(): SerializedTypeID {
|
|
227
|
+
return this.getSType()
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Base class for SerializedTypes that are comparable.
|
|
233
|
+
*
|
|
234
|
+
* @template T - What types you want to allow comparisons between. You must specify all types. Primarily used to allow
|
|
235
|
+
* comparisons between built-in types (like `string`) and SerializedType subclasses (like `Hash`).
|
|
236
|
+
*
|
|
237
|
+
* Ex. `class Hash extends Comparable<Hash | string>`
|
|
238
|
+
*/
|
|
239
|
+
class Comparable<T extends Object> extends SerializedType {
|
|
240
|
+
lt(other: T): boolean {
|
|
241
|
+
return this.compareTo(other) < 0
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
eq(other: T): boolean {
|
|
245
|
+
return this.compareTo(other) === 0
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
gt(other: T): boolean {
|
|
249
|
+
return this.compareTo(other) > 0
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
gte(other: T): boolean {
|
|
253
|
+
return this.compareTo(other) > -1
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
lte(other: T): boolean {
|
|
257
|
+
return this.compareTo(other) < 1
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Overload this method to define how two Comparable SerializedTypes are compared
|
|
262
|
+
*
|
|
263
|
+
* @param other The comparable object to compare this to
|
|
264
|
+
* @returns A number denoting the relationship of this and other
|
|
265
|
+
*/
|
|
266
|
+
compareTo(other: T): number {
|
|
267
|
+
throw new Error(`cannot compare ${this.toString()} and ${other.toString()}`)
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export { SerializedType, Comparable, JSON, JsonObject }
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { DEFAULT_DEFINITIONS, XrplDefinitionsBase } from '../enums'
|
|
2
|
+
import { SerializedType, JsonObject, SerializedTypeID } from './serialized-type'
|
|
3
|
+
import { STObject } from './st-object'
|
|
4
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
5
|
+
import { concat } from '@transia/isomorphic/utils'
|
|
6
|
+
|
|
7
|
+
const ARRAY_END_MARKER = Uint8Array.from([0xf1])
|
|
8
|
+
const ARRAY_END_MARKER_NAME = 'ArrayEndMarker'
|
|
9
|
+
|
|
10
|
+
const OBJECT_END_MARKER = Uint8Array.from([0xe1])
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* TypeGuard for Array<JsonObject>
|
|
14
|
+
*/
|
|
15
|
+
function isObjects(args): args is Array<JsonObject> {
|
|
16
|
+
return (
|
|
17
|
+
Array.isArray(args) &&
|
|
18
|
+
args.every(
|
|
19
|
+
(arg) =>
|
|
20
|
+
typeof arg === 'object' &&
|
|
21
|
+
Object.keys(arg).length === 1 &&
|
|
22
|
+
typeof Object.values(arg)[0] === 'object',
|
|
23
|
+
)
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Class for serializing and deserializing Arrays of Objects
|
|
29
|
+
*/
|
|
30
|
+
class STArray extends SerializedType {
|
|
31
|
+
/**
|
|
32
|
+
* Construct an STArray from a BinaryParser
|
|
33
|
+
*
|
|
34
|
+
* @param parser BinaryParser to parse an STArray from
|
|
35
|
+
* @returns An STArray Object
|
|
36
|
+
*/
|
|
37
|
+
static fromParser(parser: BinaryParser): STArray {
|
|
38
|
+
const bytes: Array<Uint8Array> = []
|
|
39
|
+
|
|
40
|
+
while (!parser.end()) {
|
|
41
|
+
const field = parser.readField()
|
|
42
|
+
if (field.name === ARRAY_END_MARKER_NAME) {
|
|
43
|
+
break
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
bytes.push(
|
|
47
|
+
field.header,
|
|
48
|
+
parser.readFieldValue(field).toBytes(),
|
|
49
|
+
OBJECT_END_MARKER,
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
bytes.push(ARRAY_END_MARKER)
|
|
54
|
+
return new STArray(concat(bytes))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Construct an STArray from an Array of JSON Objects
|
|
59
|
+
*
|
|
60
|
+
* @param value STArray or Array of Objects to parse into an STArray
|
|
61
|
+
* @param definitions optional, types and values to use to encode/decode a transaction
|
|
62
|
+
* @returns An STArray object
|
|
63
|
+
*/
|
|
64
|
+
static from<T extends STArray | Array<JsonObject>>(
|
|
65
|
+
value: T,
|
|
66
|
+
definitions: XrplDefinitionsBase = DEFAULT_DEFINITIONS,
|
|
67
|
+
): STArray {
|
|
68
|
+
if (value instanceof STArray) {
|
|
69
|
+
return value
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (isObjects(value)) {
|
|
73
|
+
const bytes: Array<Uint8Array> = []
|
|
74
|
+
value.forEach((obj) => {
|
|
75
|
+
bytes.push(STObject.from(obj, undefined, definitions).toBytes())
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
bytes.push(ARRAY_END_MARKER)
|
|
79
|
+
return new STArray(concat(bytes))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
throw new Error('Cannot construct STArray from value given')
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Return the JSON representation of this.bytes
|
|
87
|
+
*
|
|
88
|
+
* @param definitions optional, types and values to use to encode/decode a transaction
|
|
89
|
+
* @returns An Array of JSON objects
|
|
90
|
+
*/
|
|
91
|
+
toJSON(
|
|
92
|
+
definitions: XrplDefinitionsBase = DEFAULT_DEFINITIONS,
|
|
93
|
+
): Array<JsonObject> {
|
|
94
|
+
const result: Array<JsonObject> = []
|
|
95
|
+
|
|
96
|
+
const arrayParser = new BinaryParser(this.toString(), definitions)
|
|
97
|
+
|
|
98
|
+
while (!arrayParser.end()) {
|
|
99
|
+
const field = arrayParser.readField()
|
|
100
|
+
if (field.name === ARRAY_END_MARKER_NAME) {
|
|
101
|
+
break
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const outer = {}
|
|
105
|
+
outer[field.name] = STObject.fromParser(arrayParser).toJSON(definitions)
|
|
106
|
+
result.push(outer)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return result
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
getSType(): SerializedTypeID {
|
|
113
|
+
return SerializedTypeID.STI_ARRAY
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export { STArray }
|