@transia/ripple-binary-codec 1.4.6-alpha.8 → 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 +1650 -964
- 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 +1652 -966
- 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 -307
- 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/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,24 @@
|
|
|
1
|
+
import { Hash } from './hash'
|
|
2
|
+
import { SerializedTypeID } from './serialized-type'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Hash with a width of 160 bits
|
|
6
|
+
*/
|
|
7
|
+
class Hash160 extends Hash {
|
|
8
|
+
static readonly width = 20
|
|
9
|
+
static readonly ZERO_160: Hash160 = new Hash160(new Uint8Array(Hash160.width))
|
|
10
|
+
|
|
11
|
+
constructor(bytes?: Uint8Array) {
|
|
12
|
+
if (bytes && bytes.byteLength === 0) {
|
|
13
|
+
bytes = Hash160.ZERO_160.bytes
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
super(bytes ?? Hash160.ZERO_160.bytes)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
getSType(): SerializedTypeID {
|
|
20
|
+
return SerializedTypeID.STI_UINT160
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { Hash160 }
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Hash } from './hash'
|
|
2
|
+
import { SerializedTypeID } from './serialized-type'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Hash with a width of 192 bits
|
|
6
|
+
*/
|
|
7
|
+
class Hash192 extends Hash {
|
|
8
|
+
static readonly width = 24
|
|
9
|
+
static readonly ZERO_192: Hash192 = new Hash192(new Uint8Array(Hash192.width))
|
|
10
|
+
|
|
11
|
+
constructor(bytes?: Uint8Array) {
|
|
12
|
+
if (bytes && bytes.byteLength === 0) {
|
|
13
|
+
bytes = Hash192.ZERO_192.bytes
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
super(bytes ?? Hash192.ZERO_192.bytes)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
getSType(): SerializedTypeID {
|
|
20
|
+
return SerializedTypeID.STI_UINT192
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { Hash192 }
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Hash } from './hash'
|
|
2
|
+
import { SerializedTypeID } from './serialized-type'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Hash with a width of 256 bits
|
|
6
|
+
*/
|
|
7
|
+
class Hash256 extends Hash {
|
|
8
|
+
static readonly width = 32
|
|
9
|
+
static readonly ZERO_256 = new Hash256(new Uint8Array(Hash256.width))
|
|
10
|
+
|
|
11
|
+
constructor(bytes: Uint8Array) {
|
|
12
|
+
super(bytes ?? Hash256.ZERO_256.bytes)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getSType(): SerializedTypeID {
|
|
16
|
+
return SerializedTypeID.STI_UINT256
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { Hash256 }
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Comparable } from './serialized-type'
|
|
2
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
+
import { HEX_REGEX, hexToBytes } from '@transia/isomorphic/utils'
|
|
4
|
+
import { compare } from '../utils'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Base class defining how to encode and decode hashes
|
|
8
|
+
*/
|
|
9
|
+
class Hash extends Comparable<Hash | string> {
|
|
10
|
+
static readonly width: number
|
|
11
|
+
|
|
12
|
+
constructor(bytes: Uint8Array) {
|
|
13
|
+
super(bytes)
|
|
14
|
+
if (this.bytes.length !== (this.constructor as typeof Hash).width) {
|
|
15
|
+
throw new Error(`Invalid Hash length ${this.bytes.byteLength}`)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Construct a Hash object from an existing Hash object or a hex-string
|
|
21
|
+
*
|
|
22
|
+
* @param value A hash object or hex-string of a hash
|
|
23
|
+
*/
|
|
24
|
+
static from<T extends Hash | string>(value: T): Hash {
|
|
25
|
+
if (value instanceof this) {
|
|
26
|
+
return value
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (typeof value === 'string') {
|
|
30
|
+
if (!HEX_REGEX.test(value)) {
|
|
31
|
+
throw new Error(`Invalid hash string ${value}`)
|
|
32
|
+
}
|
|
33
|
+
return new this(hexToBytes(value))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
throw new Error('Cannot construct Hash from given value')
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Read a Hash object from a BinaryParser
|
|
41
|
+
*
|
|
42
|
+
* @param parser BinaryParser to read the hash from
|
|
43
|
+
* @param hint length of the bytes to read, optional
|
|
44
|
+
*/
|
|
45
|
+
static fromParser(parser: BinaryParser, hint?: number): Hash {
|
|
46
|
+
return new this(parser.read(hint ?? this.width))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Overloaded operator for comparing two hash objects
|
|
51
|
+
*
|
|
52
|
+
* @param other The Hash to compare this to
|
|
53
|
+
*/
|
|
54
|
+
compareTo(other: Hash): number {
|
|
55
|
+
return compare(
|
|
56
|
+
this.bytes,
|
|
57
|
+
(this.constructor as typeof Hash).from(other).bytes,
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @returns the hex-string representation of this Hash
|
|
63
|
+
*/
|
|
64
|
+
toString(): string {
|
|
65
|
+
return this.toHex()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Returns four bits at the specified depth within a hash
|
|
70
|
+
*
|
|
71
|
+
* @param depth The depth of the four bits
|
|
72
|
+
* @returns The number represented by the four bits
|
|
73
|
+
*/
|
|
74
|
+
nibblet(depth: number): number {
|
|
75
|
+
const byteIx = depth > 0 ? (depth / 2) | 0 : 0
|
|
76
|
+
let b = this.bytes[byteIx]
|
|
77
|
+
if (depth % 2 === 0) {
|
|
78
|
+
b = (b & 0xf0) >>> 4
|
|
79
|
+
} else {
|
|
80
|
+
b = b & 0x0f
|
|
81
|
+
}
|
|
82
|
+
return b
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export { Hash }
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { AccountID } from './account-id'
|
|
2
|
+
import { Amount } from './amount'
|
|
3
|
+
import { Blob } from './blob'
|
|
4
|
+
import { Currency } from './currency'
|
|
5
|
+
import { Data } from './data'
|
|
6
|
+
import { DataType } from './dataType'
|
|
7
|
+
import { Hash128 } from './hash-128'
|
|
8
|
+
import { Hash160 } from './hash-160'
|
|
9
|
+
import { Hash192 } from './hash-192'
|
|
10
|
+
import { Hash256 } from './hash-256'
|
|
11
|
+
import { Issue } from './issue'
|
|
12
|
+
import { STNumber } from './st-number'
|
|
13
|
+
import { PathSet } from './path-set'
|
|
14
|
+
import { STArray } from './st-array'
|
|
15
|
+
import { STObject } from './st-object'
|
|
16
|
+
import { UInt16 } from './uint-16'
|
|
17
|
+
import { UInt32 } from './uint-32'
|
|
18
|
+
import { UInt64 } from './uint-64'
|
|
19
|
+
import { UInt8 } from './uint-8'
|
|
20
|
+
import { Vector256 } from './vector-256'
|
|
21
|
+
import { XChainBridge } from './xchain-bridge'
|
|
22
|
+
import { type SerializedType } from './serialized-type'
|
|
23
|
+
import { DEFAULT_DEFINITIONS } from '../enums'
|
|
24
|
+
|
|
25
|
+
const coreTypes: Record<string, typeof SerializedType> = {
|
|
26
|
+
AccountID,
|
|
27
|
+
Amount,
|
|
28
|
+
Blob,
|
|
29
|
+
Currency,
|
|
30
|
+
Data,
|
|
31
|
+
DataType,
|
|
32
|
+
Hash128,
|
|
33
|
+
Hash160,
|
|
34
|
+
Hash192,
|
|
35
|
+
Hash256,
|
|
36
|
+
Issue,
|
|
37
|
+
Number: STNumber,
|
|
38
|
+
PathSet,
|
|
39
|
+
STArray,
|
|
40
|
+
STObject,
|
|
41
|
+
UInt8,
|
|
42
|
+
UInt16,
|
|
43
|
+
UInt32,
|
|
44
|
+
UInt64,
|
|
45
|
+
Vector256,
|
|
46
|
+
XChainBridge,
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Ensures that the DEFAULT_DEFINITIONS object connects these types to fields for serializing/deserializing
|
|
50
|
+
// This is done here instead of in enums/index.ts to avoid a circular dependency
|
|
51
|
+
// because some of the above types depend on BinarySerializer which depends on enums/index.ts.
|
|
52
|
+
DEFAULT_DEFINITIONS.associateTypes(coreTypes)
|
|
53
|
+
|
|
54
|
+
export {
|
|
55
|
+
coreTypes,
|
|
56
|
+
AccountID,
|
|
57
|
+
Amount,
|
|
58
|
+
Blob,
|
|
59
|
+
Currency,
|
|
60
|
+
Data,
|
|
61
|
+
DataType,
|
|
62
|
+
Hash128,
|
|
63
|
+
Hash160,
|
|
64
|
+
Hash192,
|
|
65
|
+
Hash256,
|
|
66
|
+
PathSet,
|
|
67
|
+
STArray,
|
|
68
|
+
STObject,
|
|
69
|
+
UInt8,
|
|
70
|
+
UInt16,
|
|
71
|
+
UInt32,
|
|
72
|
+
UInt64,
|
|
73
|
+
Vector256,
|
|
74
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { concat } from '@transia/isomorphic/utils'
|
|
2
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
3
|
+
|
|
4
|
+
import { AccountID } from './account-id'
|
|
5
|
+
import { Currency } from './currency'
|
|
6
|
+
import { JsonObject, SerializedType, SerializedTypeID } from './serialized-type'
|
|
7
|
+
import { Hash192 } from './hash-192'
|
|
8
|
+
|
|
9
|
+
interface XRPIssue extends JsonObject {
|
|
10
|
+
currency: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface IOUIssue extends JsonObject {
|
|
14
|
+
currency: string
|
|
15
|
+
issuer: string
|
|
16
|
+
}
|
|
17
|
+
interface MPTIssue extends JsonObject {
|
|
18
|
+
mpt_issuance_id: string
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Interface for JSON objects that represent issues
|
|
22
|
+
*/
|
|
23
|
+
type IssueObject = XRPIssue | IOUIssue | MPTIssue
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Type guard for Issue Object
|
|
27
|
+
*/
|
|
28
|
+
function isIssueObject(arg): arg is IssueObject {
|
|
29
|
+
const keys = Object.keys(arg).sort()
|
|
30
|
+
const isXRP = keys.length === 1 && keys[0] === 'currency'
|
|
31
|
+
const isIOU =
|
|
32
|
+
keys.length === 2 && keys[0] === 'currency' && keys[1] === 'issuer'
|
|
33
|
+
const isMPT = keys.length === 1 && keys[0] === 'mpt_issuance_id'
|
|
34
|
+
|
|
35
|
+
return isXRP || isIOU || isMPT
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Class for serializing/Deserializing Amounts
|
|
40
|
+
*/
|
|
41
|
+
class Issue extends SerializedType {
|
|
42
|
+
static readonly ZERO_ISSUED_CURRENCY: Issue = new Issue(new Uint8Array(20))
|
|
43
|
+
|
|
44
|
+
constructor(bytes: Uint8Array) {
|
|
45
|
+
super(bytes ?? Issue.ZERO_ISSUED_CURRENCY.bytes)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Construct an amount from an IOU or string amount
|
|
50
|
+
*
|
|
51
|
+
* @param value An Amount, object representing an IOU, MPTAmount, or a string
|
|
52
|
+
* representing an integer amount
|
|
53
|
+
* @returns An Issue object
|
|
54
|
+
*/
|
|
55
|
+
static from<T extends Issue | IssueObject>(value: T): Issue {
|
|
56
|
+
if (value instanceof Issue) {
|
|
57
|
+
return value
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (isIssueObject(value)) {
|
|
61
|
+
if (value.currency) {
|
|
62
|
+
const currency = Currency.from(value.currency.toString()).toBytes()
|
|
63
|
+
|
|
64
|
+
//IOU case
|
|
65
|
+
if (value.issuer) {
|
|
66
|
+
const issuer = AccountID.from(value.issuer.toString()).toBytes()
|
|
67
|
+
return new Issue(concat([currency, issuer]))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
//XRP case
|
|
71
|
+
return new Issue(currency)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// MPT case
|
|
75
|
+
if (value.mpt_issuance_id) {
|
|
76
|
+
const mptIssuanceIdBytes = Hash192.from(
|
|
77
|
+
value.mpt_issuance_id.toString(),
|
|
78
|
+
).toBytes()
|
|
79
|
+
return new Issue(mptIssuanceIdBytes)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
throw new Error('Invalid type to construct an Amount')
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Read an amount from a BinaryParser
|
|
88
|
+
*
|
|
89
|
+
* @param parser BinaryParser to read the Amount from
|
|
90
|
+
* @param hint The number of bytes to consume from the parser.
|
|
91
|
+
* For an MPT amount, pass 24 (the fixed length for Hash192).
|
|
92
|
+
*
|
|
93
|
+
* @returns An Issue object
|
|
94
|
+
*/
|
|
95
|
+
static fromParser(parser: BinaryParser, hint?: number): Issue {
|
|
96
|
+
if (hint === Hash192.width) {
|
|
97
|
+
const mptBytes = parser.read(Hash192.width)
|
|
98
|
+
return new Issue(mptBytes)
|
|
99
|
+
}
|
|
100
|
+
const currency = parser.read(20)
|
|
101
|
+
if (new Currency(currency).toJSON() === 'XRP') {
|
|
102
|
+
return new Issue(currency)
|
|
103
|
+
}
|
|
104
|
+
const currencyAndIssuer = [currency, parser.read(20)]
|
|
105
|
+
return new Issue(concat(currencyAndIssuer))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get the JSON representation of this Amount
|
|
110
|
+
*
|
|
111
|
+
* @returns the JSON interpretation of this.bytes
|
|
112
|
+
*/
|
|
113
|
+
toJSON(): IssueObject {
|
|
114
|
+
// If the buffer is exactly 24 bytes, treat it as an MPT amount.
|
|
115
|
+
if (this.toBytes().length === Hash192.width) {
|
|
116
|
+
return {
|
|
117
|
+
mpt_issuance_id: this.toHex().toUpperCase(),
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const parser = new BinaryParser(this.toString())
|
|
122
|
+
|
|
123
|
+
const currency = Currency.fromParser(parser) as Currency
|
|
124
|
+
if (currency.toJSON() === 'XRP') {
|
|
125
|
+
return { currency: currency.toJSON() }
|
|
126
|
+
}
|
|
127
|
+
const issuer = AccountID.fromParser(parser) as AccountID
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
currency: currency.toJSON(),
|
|
131
|
+
issuer: issuer.toJSON(),
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
getSType(): SerializedTypeID {
|
|
136
|
+
return SerializedTypeID.STI_ISSUE
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { Issue, IssueObject }
|