@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,100 @@
|
|
|
1
|
+
import { XrplDefinitionsBase, FieldInstance } from '../enums';
|
|
2
|
+
import { type SerializedType } from '../types/serialized-type';
|
|
3
|
+
/**
|
|
4
|
+
* BinaryParser is used to compute fields and values from a HexString
|
|
5
|
+
*/
|
|
6
|
+
declare class BinaryParser {
|
|
7
|
+
private bytes;
|
|
8
|
+
definitions: XrplDefinitionsBase;
|
|
9
|
+
/**
|
|
10
|
+
* Initialize bytes to a hex string
|
|
11
|
+
*
|
|
12
|
+
* @param hexBytes a hex string
|
|
13
|
+
* @param definitions Rippled definitions used to parse the values of transaction types and such.
|
|
14
|
+
* Can be customized for sidechains and amendments.
|
|
15
|
+
*/
|
|
16
|
+
constructor(hexBytes: string, definitions?: XrplDefinitionsBase);
|
|
17
|
+
/**
|
|
18
|
+
* Peek the first byte of the BinaryParser
|
|
19
|
+
*
|
|
20
|
+
* @returns The first byte of the BinaryParser
|
|
21
|
+
*/
|
|
22
|
+
peek(): number;
|
|
23
|
+
/**
|
|
24
|
+
* Consume the first n bytes of the BinaryParser
|
|
25
|
+
*
|
|
26
|
+
* @param n the number of bytes to skip
|
|
27
|
+
*/
|
|
28
|
+
skip(n: number): void;
|
|
29
|
+
/**
|
|
30
|
+
* read the first n bytes from the BinaryParser
|
|
31
|
+
*
|
|
32
|
+
* @param n The number of bytes to read
|
|
33
|
+
* @return The bytes
|
|
34
|
+
*/
|
|
35
|
+
read(n: number): Uint8Array;
|
|
36
|
+
/**
|
|
37
|
+
* Read an integer of given size
|
|
38
|
+
*
|
|
39
|
+
* @param n The number of bytes to read
|
|
40
|
+
* @return The number represented by those bytes
|
|
41
|
+
*/
|
|
42
|
+
readUIntN(n: number): number;
|
|
43
|
+
readUInt8(): number;
|
|
44
|
+
readUInt16(): number;
|
|
45
|
+
readUInt32(): number;
|
|
46
|
+
size(): number;
|
|
47
|
+
end(customEnd?: number): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Reads variable length encoded bytes
|
|
50
|
+
*
|
|
51
|
+
* @return The variable length bytes
|
|
52
|
+
*/
|
|
53
|
+
readVariableLength(): Uint8Array;
|
|
54
|
+
/**
|
|
55
|
+
* Reads the length of the variable length encoded bytes
|
|
56
|
+
*
|
|
57
|
+
* @return The length of the variable length encoded bytes
|
|
58
|
+
*/
|
|
59
|
+
readVariableLengthLength(): number;
|
|
60
|
+
/**
|
|
61
|
+
* Reads the field ordinal from the BinaryParser
|
|
62
|
+
*
|
|
63
|
+
* @return Field ordinal
|
|
64
|
+
*/
|
|
65
|
+
readFieldOrdinal(): number;
|
|
66
|
+
/**
|
|
67
|
+
* Read the field from the BinaryParser
|
|
68
|
+
*
|
|
69
|
+
* @return The field represented by the bytes at the head of the BinaryParser
|
|
70
|
+
*/
|
|
71
|
+
readField(): FieldInstance;
|
|
72
|
+
/**
|
|
73
|
+
* Read a given type from the BinaryParser
|
|
74
|
+
*
|
|
75
|
+
* @param type The type that you want to read from the BinaryParser
|
|
76
|
+
* @return The instance of that type read from the BinaryParser
|
|
77
|
+
*/
|
|
78
|
+
readType(type: typeof SerializedType): SerializedType;
|
|
79
|
+
/**
|
|
80
|
+
* Get the type associated with a given field
|
|
81
|
+
*
|
|
82
|
+
* @param field The field that you wan to get the type of
|
|
83
|
+
* @return The type associated with the given field
|
|
84
|
+
*/
|
|
85
|
+
typeForField(field: FieldInstance): typeof SerializedType;
|
|
86
|
+
/**
|
|
87
|
+
* Read value of the type specified by field from the BinaryParser
|
|
88
|
+
*
|
|
89
|
+
* @param field The field that you want to get the associated value for
|
|
90
|
+
* @return The value associated with the given field
|
|
91
|
+
*/
|
|
92
|
+
readFieldValue(field: FieldInstance): SerializedType;
|
|
93
|
+
/**
|
|
94
|
+
* Get the next field and value from the BinaryParser
|
|
95
|
+
*
|
|
96
|
+
* @return The field and value
|
|
97
|
+
*/
|
|
98
|
+
readFieldAndValue(): [FieldInstance, SerializedType];
|
|
99
|
+
}
|
|
100
|
+
export { BinaryParser };
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BinaryParser = void 0;
|
|
4
|
+
const enums_1 = require("../enums");
|
|
5
|
+
const utils_1 = require("@transia/isomorphic/utils");
|
|
6
|
+
/**
|
|
7
|
+
* BinaryParser is used to compute fields and values from a HexString
|
|
8
|
+
*/
|
|
9
|
+
class BinaryParser {
|
|
10
|
+
/**
|
|
11
|
+
* Initialize bytes to a hex string
|
|
12
|
+
*
|
|
13
|
+
* @param hexBytes a hex string
|
|
14
|
+
* @param definitions Rippled definitions used to parse the values of transaction types and such.
|
|
15
|
+
* Can be customized for sidechains and amendments.
|
|
16
|
+
*/
|
|
17
|
+
constructor(hexBytes, definitions = enums_1.DEFAULT_DEFINITIONS) {
|
|
18
|
+
this.bytes = (0, utils_1.hexToBytes)(hexBytes);
|
|
19
|
+
this.definitions = definitions;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Peek the first byte of the BinaryParser
|
|
23
|
+
*
|
|
24
|
+
* @returns The first byte of the BinaryParser
|
|
25
|
+
*/
|
|
26
|
+
peek() {
|
|
27
|
+
if (this.bytes.byteLength === 0) {
|
|
28
|
+
throw new Error();
|
|
29
|
+
}
|
|
30
|
+
return this.bytes[0];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Consume the first n bytes of the BinaryParser
|
|
34
|
+
*
|
|
35
|
+
* @param n the number of bytes to skip
|
|
36
|
+
*/
|
|
37
|
+
skip(n) {
|
|
38
|
+
if (n > this.bytes.byteLength) {
|
|
39
|
+
throw new Error();
|
|
40
|
+
}
|
|
41
|
+
this.bytes = this.bytes.slice(n);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* read the first n bytes from the BinaryParser
|
|
45
|
+
*
|
|
46
|
+
* @param n The number of bytes to read
|
|
47
|
+
* @return The bytes
|
|
48
|
+
*/
|
|
49
|
+
read(n) {
|
|
50
|
+
if (n > this.bytes.byteLength) {
|
|
51
|
+
throw new Error();
|
|
52
|
+
}
|
|
53
|
+
const slice = this.bytes.slice(0, n);
|
|
54
|
+
this.skip(n);
|
|
55
|
+
return slice;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Read an integer of given size
|
|
59
|
+
*
|
|
60
|
+
* @param n The number of bytes to read
|
|
61
|
+
* @return The number represented by those bytes
|
|
62
|
+
*/
|
|
63
|
+
readUIntN(n) {
|
|
64
|
+
if (0 >= n || n > 4) {
|
|
65
|
+
throw new Error('invalid n');
|
|
66
|
+
}
|
|
67
|
+
return this.read(n).reduce((a, b) => (a << 8) | b) >>> 0;
|
|
68
|
+
}
|
|
69
|
+
readUInt8() {
|
|
70
|
+
return this.readUIntN(1);
|
|
71
|
+
}
|
|
72
|
+
readUInt16() {
|
|
73
|
+
return this.readUIntN(2);
|
|
74
|
+
}
|
|
75
|
+
readUInt32() {
|
|
76
|
+
return this.readUIntN(4);
|
|
77
|
+
}
|
|
78
|
+
size() {
|
|
79
|
+
return this.bytes.byteLength;
|
|
80
|
+
}
|
|
81
|
+
end(customEnd) {
|
|
82
|
+
const length = this.bytes.byteLength;
|
|
83
|
+
return length === 0 || (customEnd !== undefined && length <= customEnd);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Reads variable length encoded bytes
|
|
87
|
+
*
|
|
88
|
+
* @return The variable length bytes
|
|
89
|
+
*/
|
|
90
|
+
readVariableLength() {
|
|
91
|
+
return this.read(this.readVariableLengthLength());
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Reads the length of the variable length encoded bytes
|
|
95
|
+
*
|
|
96
|
+
* @return The length of the variable length encoded bytes
|
|
97
|
+
*/
|
|
98
|
+
readVariableLengthLength() {
|
|
99
|
+
const b1 = this.readUInt8();
|
|
100
|
+
if (b1 <= 192) {
|
|
101
|
+
return b1;
|
|
102
|
+
}
|
|
103
|
+
else if (b1 <= 240) {
|
|
104
|
+
const b2 = this.readUInt8();
|
|
105
|
+
return 193 + (b1 - 193) * 256 + b2;
|
|
106
|
+
}
|
|
107
|
+
else if (b1 <= 254) {
|
|
108
|
+
const b2 = this.readUInt8();
|
|
109
|
+
const b3 = this.readUInt8();
|
|
110
|
+
return 12481 + (b1 - 241) * 65536 + b2 * 256 + b3;
|
|
111
|
+
}
|
|
112
|
+
throw new Error('Invalid variable length indicator');
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Reads the field ordinal from the BinaryParser
|
|
116
|
+
*
|
|
117
|
+
* @return Field ordinal
|
|
118
|
+
*/
|
|
119
|
+
readFieldOrdinal() {
|
|
120
|
+
let type = this.readUInt8();
|
|
121
|
+
let nth = type & 15;
|
|
122
|
+
type >>= 4;
|
|
123
|
+
if (type === 0) {
|
|
124
|
+
type = this.readUInt8();
|
|
125
|
+
if (type === 0 || type < 16) {
|
|
126
|
+
throw new Error(`Cannot read FieldOrdinal, type_code ${type} out of range`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (nth === 0) {
|
|
130
|
+
nth = this.readUInt8();
|
|
131
|
+
if (nth === 0 || nth < 16) {
|
|
132
|
+
throw new Error(`Cannot read FieldOrdinal, field_code ${nth} out of range`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return (type << 16) | nth;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Read the field from the BinaryParser
|
|
139
|
+
*
|
|
140
|
+
* @return The field represented by the bytes at the head of the BinaryParser
|
|
141
|
+
*/
|
|
142
|
+
readField() {
|
|
143
|
+
return this.definitions.field.fromString(this.readFieldOrdinal().toString());
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Read a given type from the BinaryParser
|
|
147
|
+
*
|
|
148
|
+
* @param type The type that you want to read from the BinaryParser
|
|
149
|
+
* @return The instance of that type read from the BinaryParser
|
|
150
|
+
*/
|
|
151
|
+
readType(type) {
|
|
152
|
+
return type.fromParser(this);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Get the type associated with a given field
|
|
156
|
+
*
|
|
157
|
+
* @param field The field that you wan to get the type of
|
|
158
|
+
* @return The type associated with the given field
|
|
159
|
+
*/
|
|
160
|
+
typeForField(field) {
|
|
161
|
+
return field.associatedType;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Read value of the type specified by field from the BinaryParser
|
|
165
|
+
*
|
|
166
|
+
* @param field The field that you want to get the associated value for
|
|
167
|
+
* @return The value associated with the given field
|
|
168
|
+
*/
|
|
169
|
+
readFieldValue(field) {
|
|
170
|
+
const type = this.typeForField(field);
|
|
171
|
+
if (!type) {
|
|
172
|
+
throw new Error(`unsupported: (${field.name}, ${field.type.name})`);
|
|
173
|
+
}
|
|
174
|
+
const sizeHint = field.isVariableLengthEncoded
|
|
175
|
+
? this.readVariableLengthLength()
|
|
176
|
+
: undefined;
|
|
177
|
+
const value = type.fromParser(this, sizeHint);
|
|
178
|
+
if (value === undefined) {
|
|
179
|
+
throw new Error(`fromParser for (${field.name}, ${field.type.name}) -> undefined `);
|
|
180
|
+
}
|
|
181
|
+
return value;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Get the next field and value from the BinaryParser
|
|
185
|
+
*
|
|
186
|
+
* @return The field and value
|
|
187
|
+
*/
|
|
188
|
+
readFieldAndValue() {
|
|
189
|
+
const field = this.readField();
|
|
190
|
+
return [field, this.readFieldValue(field)];
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
exports.BinaryParser = BinaryParser;
|
|
194
|
+
//# sourceMappingURL=binary-parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary-parser.js","sourceRoot":"","sources":["../../../src/serdes/binary-parser.ts"],"names":[],"mappings":";;;AAAA,oCAIiB;AAEjB,qDAAsD;AAEtD;;GAEG;AACH,MAAM,YAAY;IAIhB;;;;;;OAMG;IACH,YACE,QAAgB,EAChB,cAAmC,2BAAmB;QAEtD,IAAI,CAAC,KAAK,GAAG,IAAA,kBAAU,EAAC,QAAQ,CAAC,CAAA;QACjC,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;IAChC,CAAC;IAED;;;;OAIG;IACH,I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AAC,SAAS,CAAA;QACb,MAAM,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAA;QAC7C,IAAI,KAAK,KAAK,SAAS,EAAE;YACvB,MAAM,IAAI,KAAK,CACb,mBAAmB,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,CAAC,IAAI,iBAAiB,CACnE,CAAA;SACF;QACD,OAAO,KAAK,CAAA;IACd,CAAC;IAED;;;;OAIG;IACH,iBAAiB;QACf,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,EAAE,CAAA;QAC9B,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAA;IAC5C,CAAC;CACF;AAEQ,oCAAY"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { FieldInstance } from '../enums';
|
|
2
|
+
import { type SerializedType } from '../types/serialized-type';
|
|
3
|
+
/**
|
|
4
|
+
* Bytes list is a collection of Uint8Array objects
|
|
5
|
+
*/
|
|
6
|
+
declare class BytesList {
|
|
7
|
+
private bytesArray;
|
|
8
|
+
/**
|
|
9
|
+
* Get the total number of bytes in the BytesList
|
|
10
|
+
*
|
|
11
|
+
* @return the number of bytes
|
|
12
|
+
*/
|
|
13
|
+
getLength(): number;
|
|
14
|
+
/**
|
|
15
|
+
* Put bytes in the BytesList
|
|
16
|
+
*
|
|
17
|
+
* @param bytesArg A Uint8Array
|
|
18
|
+
* @return this BytesList
|
|
19
|
+
*/
|
|
20
|
+
put(bytesArg: Uint8Array): BytesList;
|
|
21
|
+
/**
|
|
22
|
+
* Write this BytesList to the back of another bytes list
|
|
23
|
+
*
|
|
24
|
+
* @param list The BytesList to write to
|
|
25
|
+
*/
|
|
26
|
+
toBytesSink(list: BytesList): void;
|
|
27
|
+
toBytes(): Uint8Array;
|
|
28
|
+
toHex(): string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* BinarySerializer is used to write fields and values to Uint8Arrays
|
|
32
|
+
*/
|
|
33
|
+
declare class BinarySerializer {
|
|
34
|
+
private sink;
|
|
35
|
+
constructor(sink: BytesList);
|
|
36
|
+
/**
|
|
37
|
+
* Write a value to this BinarySerializer
|
|
38
|
+
*
|
|
39
|
+
* @param value a SerializedType value
|
|
40
|
+
*/
|
|
41
|
+
write(value: SerializedType): void;
|
|
42
|
+
/**
|
|
43
|
+
* Write bytes to this BinarySerializer
|
|
44
|
+
*
|
|
45
|
+
* @param bytes the bytes to write
|
|
46
|
+
*/
|
|
47
|
+
put(bytes: Uint8Array): void;
|
|
48
|
+
/**
|
|
49
|
+
* Write a value of a given type to this BinarySerializer
|
|
50
|
+
*
|
|
51
|
+
* @param type the type to write
|
|
52
|
+
* @param value a value of that type
|
|
53
|
+
*/
|
|
54
|
+
writeType(type: typeof SerializedType, value: SerializedType): void;
|
|
55
|
+
/**
|
|
56
|
+
* Write BytesList to this BinarySerializer
|
|
57
|
+
*
|
|
58
|
+
* @param bl BytesList to write to BinarySerializer
|
|
59
|
+
*/
|
|
60
|
+
writeBytesList(bl: BytesList): void;
|
|
61
|
+
/**
|
|
62
|
+
* Calculate the header of Variable Length encoded bytes
|
|
63
|
+
*
|
|
64
|
+
* @param length the length of the bytes
|
|
65
|
+
*/
|
|
66
|
+
static encodeVariableLength(length: number): Uint8Array;
|
|
67
|
+
/**
|
|
68
|
+
* Write field and value to BinarySerializer
|
|
69
|
+
*
|
|
70
|
+
* @param field field to write to BinarySerializer
|
|
71
|
+
* @param value value to write to BinarySerializer
|
|
72
|
+
*/
|
|
73
|
+
writeFieldAndValue(field: FieldInstance, value: SerializedType, isUnlModifyWorkaround?: boolean): void;
|
|
74
|
+
/**
|
|
75
|
+
* Write a variable length encoded value to the BinarySerializer
|
|
76
|
+
*
|
|
77
|
+
* @param value length encoded value to write to BytesList
|
|
78
|
+
*/
|
|
79
|
+
writeLengthEncoded(value: SerializedType, isUnlModifyWorkaround?: boolean): void;
|
|
80
|
+
}
|
|
81
|
+
export { BytesList, BinarySerializer };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BinarySerializer = exports.BytesList = void 0;
|
|
4
|
+
const utils_1 = require("@transia/isomorphic/utils");
|
|
5
|
+
/**
|
|
6
|
+
* Bytes list is a collection of Uint8Array objects
|
|
7
|
+
*/
|
|
8
|
+
class BytesList {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.bytesArray = [];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Get the total number of bytes in the BytesList
|
|
14
|
+
*
|
|
15
|
+
* @return the number of bytes
|
|
16
|
+
*/
|
|
17
|
+
getLength() {
|
|
18
|
+
return (0, utils_1.concat)(this.bytesArray).byteLength;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Put bytes in the BytesList
|
|
22
|
+
*
|
|
23
|
+
* @param bytesArg A Uint8Array
|
|
24
|
+
* @return this BytesList
|
|
25
|
+
*/
|
|
26
|
+
put(bytesArg) {
|
|
27
|
+
const bytes = Uint8Array.from(bytesArg); // Temporary, to catch instances of Uint8Array being passed in
|
|
28
|
+
this.bytesArray.push(bytes);
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Write this BytesList to the back of another bytes list
|
|
33
|
+
*
|
|
34
|
+
* @param list The BytesList to write to
|
|
35
|
+
*/
|
|
36
|
+
toBytesSink(list) {
|
|
37
|
+
list.put(this.toBytes());
|
|
38
|
+
}
|
|
39
|
+
toBytes() {
|
|
40
|
+
return (0, utils_1.concat)(this.bytesArray);
|
|
41
|
+
}
|
|
42
|
+
toHex() {
|
|
43
|
+
return (0, utils_1.bytesToHex)(this.toBytes());
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.BytesList = BytesList;
|
|
47
|
+
/**
|
|
48
|
+
* BinarySerializer is used to write fields and values to Uint8Arrays
|
|
49
|
+
*/
|
|
50
|
+
class BinarySerializer {
|
|
51
|
+
constructor(sink) {
|
|
52
|
+
this.sink = new BytesList();
|
|
53
|
+
this.sink = sink;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Write a value to this BinarySerializer
|
|
57
|
+
*
|
|
58
|
+
* @param value a SerializedType value
|
|
59
|
+
*/
|
|
60
|
+
write(value) {
|
|
61
|
+
value.toBytesSink(this.sink);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Write bytes to this BinarySerializer
|
|
65
|
+
*
|
|
66
|
+
* @param bytes the bytes to write
|
|
67
|
+
*/
|
|
68
|
+
put(bytes) {
|
|
69
|
+
this.sink.put(bytes);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Write a value of a given type to this BinarySerializer
|
|
73
|
+
*
|
|
74
|
+
* @param type the type to write
|
|
75
|
+
* @param value a value of that type
|
|
76
|
+
*/
|
|
77
|
+
writeType(type, value) {
|
|
78
|
+
this.write(type.from(value));
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Write BytesList to this BinarySerializer
|
|
82
|
+
*
|
|
83
|
+
* @param bl BytesList to write to BinarySerializer
|
|
84
|
+
*/
|
|
85
|
+
writeBytesList(bl) {
|
|
86
|
+
bl.toBytesSink(this.sink);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Calculate the header of Variable Length encoded bytes
|
|
90
|
+
*
|
|
91
|
+
* @param length the length of the bytes
|
|
92
|
+
*/
|
|
93
|
+
static encodeVariableLength(length) {
|
|
94
|
+
const lenBytes = new Uint8Array(3);
|
|
95
|
+
if (length <= 192) {
|
|
96
|
+
lenBytes[0] = length;
|
|
97
|
+
return lenBytes.slice(0, 1);
|
|
98
|
+
}
|
|
99
|
+
else if (length <= 12480) {
|
|
100
|
+
length -= 193;
|
|
101
|
+
lenBytes[0] = 193 + (length >>> 8);
|
|
102
|
+
lenBytes[1] = length & 0xff;
|
|
103
|
+
return lenBytes.slice(0, 2);
|
|
104
|
+
}
|
|
105
|
+
else if (length <= 918744) {
|
|
106
|
+
length -= 12481;
|
|
107
|
+
lenBytes[0] = 241 + (length >>> 16);
|
|
108
|
+
lenBytes[1] = (length >> 8) & 0xff;
|
|
109
|
+
lenBytes[2] = length & 0xff;
|
|
110
|
+
return lenBytes.slice(0, 3);
|
|
111
|
+
}
|
|
112
|
+
throw new Error('Overflow error');
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Write field and value to BinarySerializer
|
|
116
|
+
*
|
|
117
|
+
* @param field field to write to BinarySerializer
|
|
118
|
+
* @param value value to write to BinarySerializer
|
|
119
|
+
*/
|
|
120
|
+
writeFieldAndValue(field, value, isUnlModifyWorkaround = false) {
|
|
121
|
+
const associatedValue = field.associatedType.from(value);
|
|
122
|
+
if (associatedValue.toBytesSink === undefined || field.name === undefined) {
|
|
123
|
+
throw new Error();
|
|
124
|
+
}
|
|
125
|
+
this.sink.put(field.header);
|
|
126
|
+
if (field.isVariableLengthEncoded) {
|
|
127
|
+
this.writeLengthEncoded(associatedValue, isUnlModifyWorkaround);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
associatedValue.toBytesSink(this.sink);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Write a variable length encoded value to the BinarySerializer
|
|
135
|
+
*
|
|
136
|
+
* @param value length encoded value to write to BytesList
|
|
137
|
+
*/
|
|
138
|
+
writeLengthEncoded(value, isUnlModifyWorkaround = false) {
|
|
139
|
+
const bytes = new BytesList();
|
|
140
|
+
if (!isUnlModifyWorkaround) {
|
|
141
|
+
// this part doesn't happen for the Account field in a UNLModify transaction
|
|
142
|
+
value.toBytesSink(bytes);
|
|
143
|
+
}
|
|
144
|
+
this.put(BinarySerializer.encodeVariableLength(bytes.getLength()));
|
|
145
|
+
this.writeBytesList(bytes);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
exports.BinarySerializer = BinarySerializer;
|
|
149
|
+
//# sourceMappingURL=binary-serializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary-serializer.js","sourceRoot":"","sources":["../../../src/serdes/binary-serializer.ts"],"names":[],"mappings":";;;AAEA,qDAA8D;AAE9D;;GAEG;AACH,MAAM,SAAS;IAAf;QACU,eAAU,GAAsB,EAAE,CAAA;IAuC5C,CAAC;IArCC;;;;OAIG;IACI,SAAS;QACd,OAAO,IAAA,cAAM,EAAC,IAAI,CAAC,UAAU,CAAC,CAAC,UAAU,CAAA;IAC3C,CAAC;IAED;;;;;OAKG;IACI,GAAG,CAAC,QAAoB;QAC7B,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA,CAAC,8DAA8D;QACtG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC3B,OAAO,IAAI,CAAA;IACb,CAAC;IAED;;;;OAIG;IACI,WAAW,CAAC,IAAe;QAChC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAA;IAC1B,CAAC;IAEM,OAAO;QACZ,OAAO,IAAA,cAAM,EAAC,IAAI,CAAC,UAAU,CAAC,CAAA;IAChC,CAAC;IAED,KAAK;QACH,OAAO,IAAA,kBAAU,EAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAA;IACnC,CAAC;CACF;AAsHQ,8BAAS;AApHlB;;GAEG;AACH,MAAM,gBAAgB;IAGpB,YAAY,IAAe;QAFnB,SAAI,GAAc,IAAI,SAAS,EAAE,CAAA;QAGvC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,KAAqB;QACzB,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC9B,CAAC;IAED;;;;OAIG;IACH,GAAG,CAAC,KAAiB;QACnB,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAA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AAI,KAAK,CAAC,gBAAgB,CAAC,CAAA;IACnC,CAAC;IAED;;;;;OAKG;IACH,kBAAkB,CAChB,KAAoB,EACpB,KAAqB,EACrB,qBAAqB,GAAG,KAAK;QAE7B,MAAM,eAAe,GAAG,KAAK,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QACxD,IAAI,eAAe,CAAC,WAAW,KAAK,SAAS,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,EAAE;YACzE,MAAM,IAAI,KAAK,EAAE,CAAA;SAClB;QAED,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;QAE3B,IAAI,KAAK,CAAC,uBAAuB,EAAE;YACjC,IAAI,CAAC,kBAAkB,CAAC,eAAe,EAAE,qBAAqB,CAAC,CAAA;SAChE;aAAM;YACL,eAAe,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;SACvC;IACH,CAAC;IAED;;;;OAIG;IACI,kBAAkB,CACvB,KAAqB,EACrB,qBAAqB,GAAG,KAAK;QAE7B,MAAM,KAAK,GAAG,IAAI,SAAS,EAAE,CAAA;QAC7B,IAAI,CAAC,qBAAqB,EAAE;YAC1B,4EAA4E;YAC5E,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,CAAA;SACzB;QACD,IAAI,CAAC,GAAG,CAAC,gBAAgB,CAAC,oBAAoB,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC,CAAC,CAAA;QAClE,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,CAAA;IAC5B,CAAC;CACF;AAEmB,4CAAgB"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { Hash256 } from './types/hash-256';
|
|
2
|
+
import { BytesList } from './serdes/binary-serializer';
|
|
3
|
+
/**
|
|
4
|
+
* Abstract class describing a SHAMapNode
|
|
5
|
+
*/
|
|
6
|
+
declare abstract class ShaMapNode {
|
|
7
|
+
abstract hashPrefix(): Uint8Array;
|
|
8
|
+
abstract isLeaf(): boolean;
|
|
9
|
+
abstract isInner(): boolean;
|
|
10
|
+
abstract toBytesSink(list: BytesList): void;
|
|
11
|
+
abstract hash(): Hash256;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Class describing a Leaf of SHAMap
|
|
15
|
+
*/
|
|
16
|
+
declare class ShaMapLeaf extends ShaMapNode {
|
|
17
|
+
index: Hash256;
|
|
18
|
+
item?: ShaMapNode | undefined;
|
|
19
|
+
constructor(index: Hash256, item?: ShaMapNode | undefined);
|
|
20
|
+
/**
|
|
21
|
+
* @returns true as ShaMapLeaf is a leaf node
|
|
22
|
+
*/
|
|
23
|
+
isLeaf(): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* @returns false as ShaMapLeaf is not an inner node
|
|
26
|
+
*/
|
|
27
|
+
isInner(): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Get the prefix of the this.item
|
|
30
|
+
*
|
|
31
|
+
* @returns The hash prefix, unless this.item is undefined, then it returns an empty Uint8Array
|
|
32
|
+
*/
|
|
33
|
+
hashPrefix(): Uint8Array;
|
|
34
|
+
/**
|
|
35
|
+
* Hash the bytes representation of this
|
|
36
|
+
*
|
|
37
|
+
* @returns hash of this.item concatenated with this.index
|
|
38
|
+
*/
|
|
39
|
+
hash(): Hash256;
|
|
40
|
+
/**
|
|
41
|
+
* Write the bytes representation of this to a BytesList
|
|
42
|
+
* @param list BytesList to write bytes to
|
|
43
|
+
*/
|
|
44
|
+
toBytesSink(list: BytesList): void;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Class defining an Inner Node of a SHAMap
|
|
48
|
+
*/
|
|
49
|
+
declare class ShaMapInner extends ShaMapNode {
|
|
50
|
+
private depth;
|
|
51
|
+
private slotBits;
|
|
52
|
+
private branches;
|
|
53
|
+
constructor(depth?: number);
|
|
54
|
+
/**
|
|
55
|
+
* @returns true as ShaMapInner is an inner node
|
|
56
|
+
*/
|
|
57
|
+
isInner(): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* @returns false as ShaMapInner is not a leaf node
|
|
60
|
+
*/
|
|
61
|
+
isLeaf(): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Get the hash prefix for this node
|
|
64
|
+
*
|
|
65
|
+
* @returns hash prefix describing an inner node
|
|
66
|
+
*/
|
|
67
|
+
hashPrefix(): Uint8Array;
|
|
68
|
+
/**
|
|
69
|
+
* Set a branch of this node to be another node
|
|
70
|
+
*
|
|
71
|
+
* @param slot Slot to add branch to this.branches
|
|
72
|
+
* @param branch Branch to add
|
|
73
|
+
*/
|
|
74
|
+
setBranch(slot: number, branch: ShaMapNode): void;
|
|
75
|
+
/**
|
|
76
|
+
* @returns true if node is empty
|
|
77
|
+
*/
|
|
78
|
+
empty(): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Compute the hash of this node
|
|
81
|
+
*
|
|
82
|
+
* @returns The hash of this node
|
|
83
|
+
*/
|
|
84
|
+
hash(): Hash256;
|
|
85
|
+
/**
|
|
86
|
+
* Writes the bytes representation of this node to a BytesList
|
|
87
|
+
*
|
|
88
|
+
* @param list BytesList to write bytes to
|
|
89
|
+
*/
|
|
90
|
+
toBytesSink(list: BytesList): void;
|
|
91
|
+
/**
|
|
92
|
+
* Add item to the SHAMap
|
|
93
|
+
*
|
|
94
|
+
* @param index Hash of the index of the item being inserted
|
|
95
|
+
* @param item Item to insert in the map
|
|
96
|
+
* @param leaf Leaf node to insert when branch doesn't exist
|
|
97
|
+
*/
|
|
98
|
+
addItem(index?: Hash256, item?: ShaMapNode, leaf?: ShaMapLeaf): void;
|
|
99
|
+
}
|
|
100
|
+
declare class ShaMap extends ShaMapInner {
|
|
101
|
+
}
|
|
102
|
+
export { ShaMap, ShaMapNode, ShaMapLeaf };
|