@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,496 @@
|
|
|
1
|
+
/* eslint-disable max-lines */
|
|
2
|
+
/* eslint-disable complexity */
|
|
3
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
4
|
+
import {
|
|
5
|
+
JsonObject,
|
|
6
|
+
SerializedType,
|
|
7
|
+
SerializedTypeID,
|
|
8
|
+
TYPE_ID_TO_STRING,
|
|
9
|
+
TYPE_STRING_TO_ID,
|
|
10
|
+
TYPE_NUMBER_TO_ID,
|
|
11
|
+
} from './serialized-type'
|
|
12
|
+
import { readUInt16BE, writeUInt16BE } from '../utils'
|
|
13
|
+
import { bytesToHex, concat } from '@transia/isomorphic/utils'
|
|
14
|
+
import { Hash128 } from './hash-128'
|
|
15
|
+
import { Hash160 } from './hash-160'
|
|
16
|
+
import { Hash192 } from './hash-192'
|
|
17
|
+
import { Hash256 } from './hash-256'
|
|
18
|
+
import { AccountID } from './account-id'
|
|
19
|
+
import { Amount, AmountObject } from './amount'
|
|
20
|
+
import { Blob } from './blob'
|
|
21
|
+
import { Currency } from './currency'
|
|
22
|
+
import { STNumber } from './st-number'
|
|
23
|
+
import { Issue, IssueObject } from './issue'
|
|
24
|
+
import { UInt8 } from './uint-8'
|
|
25
|
+
import { UInt16 } from './uint-16'
|
|
26
|
+
import { UInt32 } from './uint-32'
|
|
27
|
+
import { UInt64 } from './uint-64'
|
|
28
|
+
import { BinarySerializer } from '../binary'
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Interface for Data JSON representation
|
|
32
|
+
*/
|
|
33
|
+
interface DataJSON extends JsonObject {
|
|
34
|
+
type: string
|
|
35
|
+
value: string | number | JsonObject
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Type union for all possible data values
|
|
40
|
+
*/
|
|
41
|
+
type DataValue =
|
|
42
|
+
| number
|
|
43
|
+
| string
|
|
44
|
+
| bigint
|
|
45
|
+
| Uint8Array
|
|
46
|
+
| UInt8
|
|
47
|
+
| UInt16
|
|
48
|
+
| UInt32
|
|
49
|
+
| UInt64
|
|
50
|
+
| Hash128
|
|
51
|
+
| Hash160
|
|
52
|
+
| Hash192
|
|
53
|
+
| Hash256
|
|
54
|
+
| AccountID
|
|
55
|
+
| Amount
|
|
56
|
+
| Blob
|
|
57
|
+
| Currency
|
|
58
|
+
| STNumber
|
|
59
|
+
| Issue
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* STData: Encodes XRPL's "Data" type.
|
|
63
|
+
*
|
|
64
|
+
* This type wraps both a SerializedTypeID and the actual data value.
|
|
65
|
+
* It's encoded as a 2-byte type ID followed by the serialized data.
|
|
66
|
+
*
|
|
67
|
+
* Usage:
|
|
68
|
+
* Data.from({ type: "AMOUNT", value: "1000000" })
|
|
69
|
+
* Data.from({ type: "UINT64", value: "123456789" })
|
|
70
|
+
* Data.fromParser(parser)
|
|
71
|
+
*/
|
|
72
|
+
class Data extends SerializedType {
|
|
73
|
+
static readonly ZERO_DATA: Data = new Data(
|
|
74
|
+
concat([
|
|
75
|
+
new Uint8Array([0x00, 0x01]), // Type ID for UINT16 (SerializedTypeID.STI_UINT16 = 1) as uint16
|
|
76
|
+
new Uint8Array([0x00, 0x00]), // Value: two zero bytes for UINT16
|
|
77
|
+
]),
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Construct Data from bytes
|
|
82
|
+
* @param bytes - Uint8Array containing type ID and data
|
|
83
|
+
*/
|
|
84
|
+
constructor(bytes: Uint8Array) {
|
|
85
|
+
super(bytes ?? Data.ZERO_DATA.bytes)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Create Data from various input types
|
|
90
|
+
*
|
|
91
|
+
* @param value - Can be:
|
|
92
|
+
* - Data instance (returns as-is)
|
|
93
|
+
* - DataJSON object with 'type' and 'value' fields
|
|
94
|
+
* @returns Data instance
|
|
95
|
+
* @throws Error if value type is not supported
|
|
96
|
+
*/
|
|
97
|
+
static from(value: unknown): Data {
|
|
98
|
+
if (value instanceof Data) {
|
|
99
|
+
return value
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (
|
|
103
|
+
typeof value === 'object' &&
|
|
104
|
+
value !== null &&
|
|
105
|
+
'type' in value &&
|
|
106
|
+
'value' in value
|
|
107
|
+
) {
|
|
108
|
+
const json = value as DataJSON
|
|
109
|
+
return Data.fromJSON(json)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
throw new Error('Data.from: value must be Data instance or DataJSON object')
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Create Data from JSON representation
|
|
117
|
+
*
|
|
118
|
+
* @param json - Object with 'type' and 'value' fields
|
|
119
|
+
* @returns Data instance
|
|
120
|
+
* @throws Error if type is not supported
|
|
121
|
+
*/
|
|
122
|
+
static fromJSON(json: DataJSON): Data {
|
|
123
|
+
const typeId = TYPE_STRING_TO_ID[json.type]
|
|
124
|
+
if (typeId === undefined) {
|
|
125
|
+
throw new Error(`Data: unsupported type string: ${json.type}`)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
let dataValue: DataValue
|
|
129
|
+
let dataBytes: Uint8Array
|
|
130
|
+
|
|
131
|
+
switch (typeId) {
|
|
132
|
+
case SerializedTypeID.STI_UINT8: {
|
|
133
|
+
const val =
|
|
134
|
+
typeof json.value === 'string'
|
|
135
|
+
? parseInt(json.value, 10)
|
|
136
|
+
: typeof json.value === 'number'
|
|
137
|
+
? json.value
|
|
138
|
+
: Number(json.value)
|
|
139
|
+
if (
|
|
140
|
+
typeof val !== 'number' ||
|
|
141
|
+
Number.isNaN(val) ||
|
|
142
|
+
val < 0 ||
|
|
143
|
+
val > 255
|
|
144
|
+
) {
|
|
145
|
+
throw new Error('UINT8 value out of range')
|
|
146
|
+
}
|
|
147
|
+
dataValue = UInt8.from(val)
|
|
148
|
+
dataBytes = (dataValue as UInt8).toBytes()
|
|
149
|
+
break
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
case SerializedTypeID.STI_UINT16: {
|
|
153
|
+
const val =
|
|
154
|
+
typeof json.value === 'string'
|
|
155
|
+
? parseInt(json.value, 10)
|
|
156
|
+
: typeof json.value === 'number'
|
|
157
|
+
? json.value
|
|
158
|
+
: Number(json.value)
|
|
159
|
+
if (
|
|
160
|
+
typeof val !== 'number' ||
|
|
161
|
+
Number.isNaN(val) ||
|
|
162
|
+
val < 0 ||
|
|
163
|
+
val > 65535
|
|
164
|
+
) {
|
|
165
|
+
throw new Error('UINT16 value out of range')
|
|
166
|
+
}
|
|
167
|
+
dataValue = UInt16.from(val)
|
|
168
|
+
dataBytes = (dataValue as UInt16).toBytes()
|
|
169
|
+
break
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
case SerializedTypeID.STI_UINT32: {
|
|
173
|
+
const val =
|
|
174
|
+
typeof json.value === 'string'
|
|
175
|
+
? parseInt(json.value, 10)
|
|
176
|
+
: typeof json.value === 'number'
|
|
177
|
+
? json.value
|
|
178
|
+
: Number(json.value)
|
|
179
|
+
dataValue = UInt32.from(val)
|
|
180
|
+
dataBytes = (dataValue as UInt32).toBytes()
|
|
181
|
+
break
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
case SerializedTypeID.STI_UINT64: {
|
|
185
|
+
const val =
|
|
186
|
+
typeof json.value === 'string' ? json.value : json.value.toString()
|
|
187
|
+
dataValue = UInt64.from(val)
|
|
188
|
+
dataBytes = (dataValue as UInt64).toBytes()
|
|
189
|
+
break
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
case SerializedTypeID.STI_UINT128: {
|
|
193
|
+
const val =
|
|
194
|
+
typeof json.value === 'string' ? json.value : json.value.toString()
|
|
195
|
+
dataValue = Hash128.from(val)
|
|
196
|
+
dataBytes = (dataValue as Hash128).toBytes()
|
|
197
|
+
break
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
case SerializedTypeID.STI_UINT160: {
|
|
201
|
+
const val =
|
|
202
|
+
typeof json.value === 'string' ? json.value : json.value.toString()
|
|
203
|
+
dataValue = Hash160.from(val)
|
|
204
|
+
dataBytes = (dataValue as Hash160).toBytes()
|
|
205
|
+
break
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
case SerializedTypeID.STI_UINT192: {
|
|
209
|
+
const val =
|
|
210
|
+
typeof json.value === 'string' ? json.value : json.value.toString()
|
|
211
|
+
dataValue = Hash192.from(val)
|
|
212
|
+
dataBytes = (dataValue as Hash192).toBytes()
|
|
213
|
+
break
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
case SerializedTypeID.STI_UINT256: {
|
|
217
|
+
const val =
|
|
218
|
+
typeof json.value === 'string' ? json.value : json.value.toString()
|
|
219
|
+
dataValue = Hash256.from(val)
|
|
220
|
+
dataBytes = (dataValue as Hash256).toBytes()
|
|
221
|
+
break
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
case SerializedTypeID.STI_VL: {
|
|
225
|
+
const val =
|
|
226
|
+
typeof json.value === 'string' ? json.value : json.value.toString()
|
|
227
|
+
dataValue = Blob.from(val)
|
|
228
|
+
dataBytes = dataValue.toBytes()
|
|
229
|
+
const lengthBytes = BinarySerializer.encodeVariableLength(
|
|
230
|
+
dataBytes.length,
|
|
231
|
+
)
|
|
232
|
+
dataBytes = concat([lengthBytes, dataBytes])
|
|
233
|
+
break
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
case SerializedTypeID.STI_ACCOUNT: {
|
|
237
|
+
dataValue = AccountID.from(
|
|
238
|
+
typeof json.value === 'string' ? json.value : json.value.toString(),
|
|
239
|
+
)
|
|
240
|
+
dataBytes = (dataValue as AccountID).toBytes()
|
|
241
|
+
dataBytes = concat([new Uint8Array([0x14]), dataBytes])
|
|
242
|
+
break
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
case SerializedTypeID.STI_AMOUNT: {
|
|
246
|
+
dataValue = Amount.from(json.value as AmountObject)
|
|
247
|
+
dataBytes = (dataValue as Amount).toBytes()
|
|
248
|
+
break
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
case SerializedTypeID.STI_ISSUE: {
|
|
252
|
+
dataValue = Issue.from(json.value as IssueObject)
|
|
253
|
+
dataBytes = (dataValue as Issue).toBytes()
|
|
254
|
+
break
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
case SerializedTypeID.STI_CURRENCY: {
|
|
258
|
+
const val =
|
|
259
|
+
typeof json.value === 'string' ? json.value : json.value.toString()
|
|
260
|
+
dataValue = Currency.from(val)
|
|
261
|
+
dataBytes = (dataValue as Currency).toBytes()
|
|
262
|
+
break
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
case SerializedTypeID.STI_NUMBER: {
|
|
266
|
+
dataValue = STNumber.from(json.value)
|
|
267
|
+
dataBytes = (dataValue as STNumber).toBytes()
|
|
268
|
+
break
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
default:
|
|
272
|
+
throw new Error(`Data.fromJSON(): unsupported type ID: ${typeId}`)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// Combine type header with data bytes
|
|
276
|
+
const typeBytes = new Uint8Array(2)
|
|
277
|
+
writeUInt16BE(typeBytes, typeId, 0)
|
|
278
|
+
const fullBytes = concat([typeBytes, dataBytes])
|
|
279
|
+
return new Data(fullBytes)
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Read Data from a BinaryParser stream
|
|
284
|
+
*
|
|
285
|
+
* @param parser - BinaryParser positioned at the start of Data
|
|
286
|
+
* @returns Data instance
|
|
287
|
+
*/
|
|
288
|
+
static fromParser(parser: BinaryParser): Data {
|
|
289
|
+
// Read the 2-byte type ID
|
|
290
|
+
const typeBytes = parser.read(2)
|
|
291
|
+
const typeId = TYPE_NUMBER_TO_ID[readUInt16BE(typeBytes, 0)]
|
|
292
|
+
|
|
293
|
+
let dataValue: DataValue
|
|
294
|
+
let dataBytes: Uint8Array
|
|
295
|
+
|
|
296
|
+
switch (typeId) {
|
|
297
|
+
case SerializedTypeID.STI_UINT8:
|
|
298
|
+
dataValue = UInt8.fromParser(parser)
|
|
299
|
+
dataBytes = (dataValue as UInt8).toBytes()
|
|
300
|
+
break
|
|
301
|
+
|
|
302
|
+
case SerializedTypeID.STI_UINT16:
|
|
303
|
+
dataValue = UInt16.fromParser(parser)
|
|
304
|
+
dataBytes = (dataValue as UInt16).toBytes()
|
|
305
|
+
break
|
|
306
|
+
|
|
307
|
+
case SerializedTypeID.STI_UINT32:
|
|
308
|
+
dataValue = UInt32.fromParser(parser)
|
|
309
|
+
dataBytes = (dataValue as UInt32).toBytes()
|
|
310
|
+
break
|
|
311
|
+
|
|
312
|
+
case SerializedTypeID.STI_UINT64:
|
|
313
|
+
dataValue = UInt64.fromParser(parser)
|
|
314
|
+
dataBytes = (dataValue as UInt64).toBytes()
|
|
315
|
+
break
|
|
316
|
+
|
|
317
|
+
case SerializedTypeID.STI_UINT128:
|
|
318
|
+
dataValue = Hash128.fromParser(parser)
|
|
319
|
+
dataBytes = (dataValue as Hash128).toBytes()
|
|
320
|
+
break
|
|
321
|
+
|
|
322
|
+
case SerializedTypeID.STI_UINT160:
|
|
323
|
+
dataValue = Hash160.fromParser(parser)
|
|
324
|
+
dataBytes = (dataValue as Hash160).toBytes()
|
|
325
|
+
break
|
|
326
|
+
|
|
327
|
+
case SerializedTypeID.STI_UINT192:
|
|
328
|
+
dataValue = Hash192.fromParser(parser)
|
|
329
|
+
dataBytes = (dataValue as Hash192).toBytes()
|
|
330
|
+
break
|
|
331
|
+
|
|
332
|
+
case SerializedTypeID.STI_UINT256:
|
|
333
|
+
dataValue = Hash256.fromParser(parser)
|
|
334
|
+
dataBytes = (dataValue as Hash256).toBytes()
|
|
335
|
+
break
|
|
336
|
+
|
|
337
|
+
case SerializedTypeID.STI_VL:
|
|
338
|
+
dataValue = Blob.fromParser(parser, parser.readVariableLengthLength())
|
|
339
|
+
dataBytes = dataValue.toBytes()
|
|
340
|
+
break
|
|
341
|
+
|
|
342
|
+
case SerializedTypeID.STI_ACCOUNT:
|
|
343
|
+
parser.skip(1)
|
|
344
|
+
dataValue = AccountID.fromParser(parser)
|
|
345
|
+
dataBytes = (dataValue as AccountID).toBytes()
|
|
346
|
+
break
|
|
347
|
+
|
|
348
|
+
case SerializedTypeID.STI_AMOUNT:
|
|
349
|
+
dataValue = Amount.fromParser(parser)
|
|
350
|
+
dataBytes = (dataValue as Amount).toBytes()
|
|
351
|
+
break
|
|
352
|
+
|
|
353
|
+
case SerializedTypeID.STI_ISSUE:
|
|
354
|
+
dataValue = Issue.fromParser(parser)
|
|
355
|
+
dataBytes = (dataValue as Issue).toBytes()
|
|
356
|
+
break
|
|
357
|
+
|
|
358
|
+
case SerializedTypeID.STI_CURRENCY:
|
|
359
|
+
dataValue = Currency.fromParser(parser)
|
|
360
|
+
dataBytes = (dataValue as Currency).toBytes()
|
|
361
|
+
break
|
|
362
|
+
|
|
363
|
+
case SerializedTypeID.STI_NUMBER:
|
|
364
|
+
dataValue = STNumber.fromParser(parser)
|
|
365
|
+
dataBytes = (dataValue as STNumber).toBytes()
|
|
366
|
+
break
|
|
367
|
+
|
|
368
|
+
default:
|
|
369
|
+
throw new Error(`Data: unsupported type ID when parsing: ${typeId}`)
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const fullBytes = concat([typeBytes, dataBytes])
|
|
373
|
+
return new Data(fullBytes)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Get the inner SerializedTypeID
|
|
378
|
+
*
|
|
379
|
+
* @returns The inner type ID
|
|
380
|
+
*/
|
|
381
|
+
getInnerType(): SerializedTypeID {
|
|
382
|
+
return TYPE_NUMBER_TO_ID[readUInt16BE(this.bytes, 0)]
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Get the string representation of the inner type
|
|
387
|
+
*
|
|
388
|
+
* @returns String name of the type
|
|
389
|
+
*/
|
|
390
|
+
getInnerTypeString(): string {
|
|
391
|
+
const innerType = this.getInnerType()
|
|
392
|
+
return TYPE_ID_TO_STRING[innerType] || innerType.toString()
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Get the data value
|
|
397
|
+
*
|
|
398
|
+
* @returns The stored data value
|
|
399
|
+
*/
|
|
400
|
+
getValue(): DataValue {
|
|
401
|
+
const innerType = this.getInnerType()
|
|
402
|
+
const parser = new BinaryParser(bytesToHex(this.bytes.slice(2)))
|
|
403
|
+
|
|
404
|
+
switch (innerType) {
|
|
405
|
+
case SerializedTypeID.STI_UINT8:
|
|
406
|
+
return UInt8.fromParser(parser)
|
|
407
|
+
case SerializedTypeID.STI_UINT16:
|
|
408
|
+
return UInt16.fromParser(parser)
|
|
409
|
+
case SerializedTypeID.STI_UINT32:
|
|
410
|
+
return UInt32.fromParser(parser)
|
|
411
|
+
case SerializedTypeID.STI_UINT64:
|
|
412
|
+
return UInt64.fromParser(parser)
|
|
413
|
+
case SerializedTypeID.STI_UINT128:
|
|
414
|
+
return Hash128.fromParser(parser)
|
|
415
|
+
case SerializedTypeID.STI_UINT160:
|
|
416
|
+
return Hash160.fromParser(parser)
|
|
417
|
+
case SerializedTypeID.STI_UINT192:
|
|
418
|
+
return Hash192.fromParser(parser)
|
|
419
|
+
case SerializedTypeID.STI_UINT256:
|
|
420
|
+
return Hash256.fromParser(parser)
|
|
421
|
+
case SerializedTypeID.STI_VL:
|
|
422
|
+
return Blob.fromParser(parser, parser.size())
|
|
423
|
+
case SerializedTypeID.STI_ACCOUNT:
|
|
424
|
+
return AccountID.fromParser(parser)
|
|
425
|
+
case SerializedTypeID.STI_AMOUNT:
|
|
426
|
+
return Amount.fromParser(parser)
|
|
427
|
+
case SerializedTypeID.STI_ISSUE:
|
|
428
|
+
return Issue.fromParser(parser)
|
|
429
|
+
case SerializedTypeID.STI_CURRENCY:
|
|
430
|
+
return Currency.fromParser(parser)
|
|
431
|
+
case SerializedTypeID.STI_NUMBER:
|
|
432
|
+
return STNumber.fromParser(parser)
|
|
433
|
+
default:
|
|
434
|
+
throw new Error(
|
|
435
|
+
`Data.getValue(): unsupported type ID: ${typeof innerType}`,
|
|
436
|
+
)
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Convert to JSON representation
|
|
442
|
+
*
|
|
443
|
+
* @returns JSON object with 'type' and 'value' fields
|
|
444
|
+
*/
|
|
445
|
+
toJSON(): DataJSON {
|
|
446
|
+
const data = this.getValue()
|
|
447
|
+
let jsonValue: string | number | JsonObject
|
|
448
|
+
|
|
449
|
+
// Convert the data value to its JSON representation
|
|
450
|
+
if (data instanceof SerializedType) {
|
|
451
|
+
jsonValue = data.toJSON() as JsonObject
|
|
452
|
+
} else if (data instanceof Uint8Array) {
|
|
453
|
+
jsonValue = bytesToHex(data)
|
|
454
|
+
} else if (typeof data === 'bigint') {
|
|
455
|
+
jsonValue = data.toString()
|
|
456
|
+
} else {
|
|
457
|
+
jsonValue = data
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
return {
|
|
461
|
+
type: this.getInnerTypeString(),
|
|
462
|
+
value: jsonValue,
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* Compare with another Data for equality
|
|
468
|
+
*
|
|
469
|
+
* @param other - Another Data to compare with
|
|
470
|
+
* @returns true if both have the same inner type and data
|
|
471
|
+
*/
|
|
472
|
+
equals(other: Data): boolean {
|
|
473
|
+
if (!(other instanceof Data)) {
|
|
474
|
+
return false
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Compare bytes directly
|
|
478
|
+
if (this.bytes.length !== other.bytes.length) {
|
|
479
|
+
return false
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
for (let i = 0; i < this.bytes.length; i++) {
|
|
483
|
+
if (this.bytes[i] !== other.bytes[i]) {
|
|
484
|
+
return false
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
return true
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
getSType(): SerializedTypeID {
|
|
492
|
+
return SerializedTypeID.STI_DATA
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
export { Data }
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { BinaryParser } from '../serdes/binary-parser'
|
|
2
|
+
import {
|
|
3
|
+
JsonObject,
|
|
4
|
+
SerializedType,
|
|
5
|
+
SerializedTypeID,
|
|
6
|
+
TYPE_ID_TO_STRING,
|
|
7
|
+
TYPE_STRING_TO_ID,
|
|
8
|
+
} from './serialized-type'
|
|
9
|
+
import { readUInt16BE, writeUInt16BE } from '../utils'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Interface for DataType JSON representation
|
|
13
|
+
*/
|
|
14
|
+
interface DataTypeJSON extends JsonObject {
|
|
15
|
+
type: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* STDataType: Encodes XRPL's "DataType" type.
|
|
20
|
+
*
|
|
21
|
+
* This type wraps an inner SerializedTypeID to indicate what type of data
|
|
22
|
+
* a field contains. It's encoded as a 2-byte unsigned integer representing
|
|
23
|
+
* the inner type.
|
|
24
|
+
*
|
|
25
|
+
* Usage:
|
|
26
|
+
* DataType.from({ type: "AMOUNT" })
|
|
27
|
+
* DataType.from("UINT64")
|
|
28
|
+
* DataType.fromParser(parser)
|
|
29
|
+
*/
|
|
30
|
+
class DataType extends SerializedType {
|
|
31
|
+
private innerType: SerializedTypeID
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Default bytes for DataType (STI_NOTPRESENT)
|
|
35
|
+
*/
|
|
36
|
+
static readonly defaultBytes = new Uint8Array([0x00, 0x01])
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Construct a DataType from bytes
|
|
40
|
+
* @param bytes - 2-byte Uint8Array containing the inner type ID
|
|
41
|
+
* @param innerType - Optional explicit inner type (used when constructing from value)
|
|
42
|
+
* @throws Error if bytes is not a 2-byte Uint8Array
|
|
43
|
+
*/
|
|
44
|
+
constructor(bytes?: Uint8Array, innerType?: SerializedTypeID) {
|
|
45
|
+
const used = bytes ?? DataType.defaultBytes
|
|
46
|
+
if (!(used instanceof Uint8Array) || used.length !== 2) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
`DataType must be constructed from a 2-byte Uint8Array, got ${used?.length} bytes`,
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
super(used)
|
|
52
|
+
|
|
53
|
+
// If innerType is explicitly provided, use it; otherwise read from bytes
|
|
54
|
+
if (innerType !== undefined) {
|
|
55
|
+
this.innerType = innerType
|
|
56
|
+
} else {
|
|
57
|
+
this.innerType = readUInt16BE(used, 0) as unknown as SerializedTypeID
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Construct from various input types
|
|
63
|
+
*
|
|
64
|
+
* @param value - Can be:
|
|
65
|
+
* - DataType instance (returns as-is)
|
|
66
|
+
* - DataTypeJSON object with 'type' field
|
|
67
|
+
* - String type name (e.g., "AMOUNT", "UINT64")
|
|
68
|
+
* - SerializedTypeID enum value
|
|
69
|
+
* @returns DataType instance
|
|
70
|
+
* @throws Error if value type is not supported or type string is unknown
|
|
71
|
+
*/
|
|
72
|
+
static from(value: unknown): DataType {
|
|
73
|
+
if (value instanceof DataType) {
|
|
74
|
+
return value
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (typeof value === 'object' && value !== null && 'type' in value) {
|
|
78
|
+
const json = value as DataTypeJSON
|
|
79
|
+
return DataType.fromTypeString(json.type)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (typeof value === 'string') {
|
|
83
|
+
return DataType.fromTypeString(value)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (typeof value === 'number') {
|
|
87
|
+
return DataType.fromTypeId(value as SerializedTypeID)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
throw new Error(
|
|
91
|
+
'DataType.from: value must be DataType, DataTypeJSON, string, or SerializedTypeID',
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Construct from a type string
|
|
97
|
+
*
|
|
98
|
+
* @param typeStr - Type string like "AMOUNT", "UINT64", etc.
|
|
99
|
+
* @returns DataType instance
|
|
100
|
+
* @throws Error if type string is not recognized
|
|
101
|
+
*/
|
|
102
|
+
static fromTypeString(typeStr: string): DataType {
|
|
103
|
+
const typeId = TYPE_STRING_TO_ID[typeStr]
|
|
104
|
+
if (typeId === undefined) {
|
|
105
|
+
throw new Error(`DataType: unsupported type string: ${typeStr}`)
|
|
106
|
+
}
|
|
107
|
+
return DataType.fromTypeId(typeId)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Construct from a SerializedTypeID
|
|
112
|
+
*
|
|
113
|
+
* @param typeId - The SerializedTypeID enum value
|
|
114
|
+
* @returns DataType instance
|
|
115
|
+
*/
|
|
116
|
+
static fromTypeId(typeId: SerializedTypeID): DataType {
|
|
117
|
+
const bytes = new Uint8Array(2)
|
|
118
|
+
writeUInt16BE(bytes, typeId, 0)
|
|
119
|
+
return new DataType(bytes, typeId)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Read a DataType from a BinaryParser stream (2 bytes)
|
|
124
|
+
*
|
|
125
|
+
* @param parser - BinaryParser positioned at the start of a DataType
|
|
126
|
+
* @returns DataType instance
|
|
127
|
+
*/
|
|
128
|
+
static fromParser(parser: BinaryParser): DataType {
|
|
129
|
+
const bytes = parser.read(2)
|
|
130
|
+
return new DataType(bytes)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Get the inner SerializedTypeID
|
|
135
|
+
*
|
|
136
|
+
* @returns The inner type ID
|
|
137
|
+
*/
|
|
138
|
+
getInnerType(): SerializedTypeID {
|
|
139
|
+
return this.innerType
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Set the inner SerializedTypeID
|
|
144
|
+
*
|
|
145
|
+
* @param typeId - The new inner type ID
|
|
146
|
+
*/
|
|
147
|
+
setInnerType(typeId: SerializedTypeID): void {
|
|
148
|
+
this.innerType = typeId
|
|
149
|
+
writeUInt16BE(this.bytes, typeId, 0)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Get the string representation of the inner type
|
|
154
|
+
*
|
|
155
|
+
* @returns String name of the type, or numeric string if unknown
|
|
156
|
+
*/
|
|
157
|
+
getInnerTypeString(): string {
|
|
158
|
+
return TYPE_ID_TO_STRING[this.innerType] || this.innerType.toString()
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Convert to JSON representation
|
|
163
|
+
*
|
|
164
|
+
* @returns JSON object with 'type' field
|
|
165
|
+
*/
|
|
166
|
+
toJSON(): DataTypeJSON {
|
|
167
|
+
return {
|
|
168
|
+
type: this.getInnerTypeString(),
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
getSType(): SerializedTypeID {
|
|
173
|
+
return SerializedTypeID.STI_DATATYPE
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Export the DataType class for external use
|
|
178
|
+
export { DataType }
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Hash } from './hash'
|
|
2
|
+
import { bytesToHex } from '@transia/isomorphic/utils'
|
|
3
|
+
import { SerializedTypeID } from './serialized-type'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hash with a width of 128 bits
|
|
7
|
+
*/
|
|
8
|
+
class Hash128 extends Hash {
|
|
9
|
+
static readonly width = 16
|
|
10
|
+
static readonly ZERO_128: Hash128 = new Hash128(new Uint8Array(Hash128.width))
|
|
11
|
+
|
|
12
|
+
constructor(bytes: Uint8Array) {
|
|
13
|
+
if (bytes && bytes.byteLength === 0) {
|
|
14
|
+
bytes = Hash128.ZERO_128.bytes
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
super(bytes ?? Hash128.ZERO_128.bytes)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Get the hex representation of a hash-128 bytes, allowing unset
|
|
22
|
+
*
|
|
23
|
+
* @returns hex String of this.bytes
|
|
24
|
+
*/
|
|
25
|
+
toHex(): string {
|
|
26
|
+
const hex = bytesToHex(this.toBytes())
|
|
27
|
+
if (/^0+$/.exec(hex)) {
|
|
28
|
+
return ''
|
|
29
|
+
}
|
|
30
|
+
return hex
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
getSType(): SerializedTypeID {
|
|
34
|
+
return SerializedTypeID.STI_UINT128
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { Hash128 }
|