@transia/ripple-binary-codec 1.4.4-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.
Files changed (164) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +107 -0
  3. package/dist/binary.d.ts +91 -0
  4. package/dist/binary.js +128 -0
  5. package/dist/binary.js.map +1 -0
  6. package/dist/coretypes.d.ts +9 -0
  7. package/dist/coretypes.js +48 -0
  8. package/dist/coretypes.js.map +1 -0
  9. package/dist/enums/bytes.d.ts +26 -0
  10. package/dist/enums/bytes.js +64 -0
  11. package/dist/enums/bytes.js.map +1 -0
  12. package/dist/enums/constants.d.ts +4 -0
  13. package/dist/enums/constants.js +8 -0
  14. package/dist/enums/constants.js.map +1 -0
  15. package/dist/enums/definitions.json +2476 -0
  16. package/dist/enums/field.d.ts +29 -0
  17. package/dist/enums/field.js +59 -0
  18. package/dist/enums/field.js.map +1 -0
  19. package/dist/enums/index.d.ts +13 -0
  20. package/dist/enums/index.js +53 -0
  21. package/dist/enums/index.js.map +1 -0
  22. package/dist/enums/src/enums/definitions.json +2481 -0
  23. package/dist/enums/utils-renumber.d.ts +101 -0
  24. package/dist/enums/utils-renumber.js +127 -0
  25. package/dist/enums/utils-renumber.js.map +1 -0
  26. package/dist/enums/xrpl-definitions-base.d.ts +45 -0
  27. package/dist/enums/xrpl-definitions-base.js +63 -0
  28. package/dist/enums/xrpl-definitions-base.js.map +1 -0
  29. package/dist/enums/xrpl-definitions.d.ts +21 -0
  30. package/dist/enums/xrpl-definitions.js +29 -0
  31. package/dist/enums/xrpl-definitions.js.map +1 -0
  32. package/dist/hash-prefixes.d.ts +6 -0
  33. package/dist/hash-prefixes.js +41 -0
  34. package/dist/hash-prefixes.js.map +1 -0
  35. package/dist/hashes.d.ts +51 -0
  36. package/dist/hashes.js +77 -0
  37. package/dist/hashes.js.map +1 -0
  38. package/dist/index.d.ts +64 -0
  39. package/dist/index.js +139 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/ledger-hashes.d.ts +49 -0
  42. package/dist/ledger-hashes.js +160 -0
  43. package/dist/ledger-hashes.js.map +1 -0
  44. package/dist/quality.d.ts +22 -0
  45. package/dist/quality.js +40 -0
  46. package/dist/quality.js.map +1 -0
  47. package/dist/serdes/binary-parser.d.ts +101 -0
  48. package/dist/serdes/binary-parser.js +210 -0
  49. package/dist/serdes/binary-parser.js.map +1 -0
  50. package/dist/serdes/binary-serializer.d.ts +82 -0
  51. package/dist/serdes/binary-serializer.js +172 -0
  52. package/dist/serdes/binary-serializer.js.map +1 -0
  53. package/dist/shamap.d.ts +103 -0
  54. package/dist/shamap.js +172 -0
  55. package/dist/shamap.js.map +1 -0
  56. package/dist/types/account-id.d.ts +36 -0
  57. package/dist/types/account-id.js +71 -0
  58. package/dist/types/account-id.js.map +1 -0
  59. package/dist/types/amount.d.ts +68 -0
  60. package/dist/types/amount.js +207 -0
  61. package/dist/types/amount.js.map +1 -0
  62. package/dist/types/blob.d.ts +25 -0
  63. package/dist/types/blob.js +40 -0
  64. package/dist/types/blob.js.map +1 -0
  65. package/dist/types/currency.d.ts +29 -0
  66. package/dist/types/currency.js +127 -0
  67. package/dist/types/currency.js.map +1 -0
  68. package/dist/types/hash-128.d.ts +17 -0
  69. package/dist/types/hash-128.js +32 -0
  70. package/dist/types/hash-128.js.map +1 -0
  71. package/dist/types/hash-160.d.ts +11 -0
  72. package/dist/types/hash-160.js +20 -0
  73. package/dist/types/hash-160.js.map +1 -0
  74. package/dist/types/hash-256.d.ts +11 -0
  75. package/dist/types/hash-256.js +17 -0
  76. package/dist/types/hash-256.js.map +1 -0
  77. package/dist/types/hash.d.ts +41 -0
  78. package/dist/types/hash.js +72 -0
  79. package/dist/types/hash.js.map +1 -0
  80. package/dist/types/index.d.ts +18 -0
  81. package/dist/types/index.js +57 -0
  82. package/dist/types/index.js.map +1 -0
  83. package/dist/types/issue.d.ts +39 -0
  84. package/dist/types/issue.js +81 -0
  85. package/dist/types/issue.js.map +1 -0
  86. package/dist/types/path-set.d.ts +36 -0
  87. package/dist/types/path-set.js +233 -0
  88. package/dist/types/path-set.js.map +1 -0
  89. package/dist/types/serialized-type.d.ts +63 -0
  90. package/dist/types/serialized-type.js +97 -0
  91. package/dist/types/serialized-type.js.map +1 -0
  92. package/dist/types/st-array.d.ts +28 -0
  93. package/dist/types/st-array.js +80 -0
  94. package/dist/types/st-array.js.map +1 -0
  95. package/dist/types/st-object.d.ts +32 -0
  96. package/dist/types/st-object.js +149 -0
  97. package/dist/types/st-object.js.map +1 -0
  98. package/dist/types/uint-16.d.ts +25 -0
  99. package/dist/types/uint-16.js +44 -0
  100. package/dist/types/uint-16.js.map +1 -0
  101. package/dist/types/uint-32.d.ts +25 -0
  102. package/dist/types/uint-32.js +49 -0
  103. package/dist/types/uint-32.js.map +1 -0
  104. package/dist/types/uint-64.d.ts +39 -0
  105. package/dist/types/uint-64.js +87 -0
  106. package/dist/types/uint-64.js.map +1 -0
  107. package/dist/types/uint-8.d.ts +25 -0
  108. package/dist/types/uint-8.js +44 -0
  109. package/dist/types/uint-8.js.map +1 -0
  110. package/dist/types/uint.d.ts +30 -0
  111. package/dist/types/uint.js +42 -0
  112. package/dist/types/uint.js.map +1 -0
  113. package/dist/types/vector-256.d.ts +31 -0
  114. package/dist/types/vector-256.js +75 -0
  115. package/dist/types/vector-256.js.map +1 -0
  116. package/package.json +47 -0
  117. package/test/amount.test.js +43 -0
  118. package/test/binary-json.test.js +45 -0
  119. package/test/binary-parser.test.js +396 -0
  120. package/test/binary-serializer.test.js +289 -0
  121. package/test/definitions.test.js +100 -0
  122. package/test/fixtures/account-tx-transactions.db +0 -0
  123. package/test/fixtures/codec-fixtures.json +4466 -0
  124. package/test/fixtures/data-driven-tests.json +2919 -0
  125. package/test/fixtures/delivermin-tx-binary.json +1 -0
  126. package/test/fixtures/delivermin-tx.json +98 -0
  127. package/test/fixtures/deposit-preauth-tx-binary.json +1 -0
  128. package/test/fixtures/deposit-preauth-tx-meta-binary.json +1 -0
  129. package/test/fixtures/deposit-preauth-tx.json +58 -0
  130. package/test/fixtures/escrow-cancel-binary.json +1 -0
  131. package/test/fixtures/escrow-cancel-tx.json +6 -0
  132. package/test/fixtures/escrow-create-binary.json +1 -0
  133. package/test/fixtures/escrow-create-tx.json +10 -0
  134. package/test/fixtures/escrow-finish-binary.json +1 -0
  135. package/test/fixtures/escrow-finish-meta-binary.json +1 -0
  136. package/test/fixtures/escrow-finish-tx.json +95 -0
  137. package/test/fixtures/ledger-full-38129.json +1 -0
  138. package/test/fixtures/ledger-full-40000.json +1 -0
  139. package/test/fixtures/negative-unl.json +12 -0
  140. package/test/fixtures/nf-token.json +547 -0
  141. package/test/fixtures/payment-channel-claim-binary.json +1 -0
  142. package/test/fixtures/payment-channel-claim-tx.json +8 -0
  143. package/test/fixtures/payment-channel-create-binary.json +1 -0
  144. package/test/fixtures/payment-channel-create-tx.json +11 -0
  145. package/test/fixtures/payment-channel-fund-binary.json +1 -0
  146. package/test/fixtures/payment-channel-fund-tx.json +7 -0
  147. package/test/fixtures/signerlistset-tx-binary.json +1 -0
  148. package/test/fixtures/signerlistset-tx-meta-binary.json +1 -0
  149. package/test/fixtures/signerlistset-tx.json +94 -0
  150. package/test/fixtures/ticket-create-binary.json +1 -0
  151. package/test/fixtures/ticket-create-tx.json +7 -0
  152. package/test/fixtures/x-codec-fixtures.json +188 -0
  153. package/test/hash.test.js +135 -0
  154. package/test/ledger.test.js +29 -0
  155. package/test/lower-case-hex.test.js +46 -0
  156. package/test/pseudo-transaction.test.js +38 -0
  157. package/test/quality.test.js +15 -0
  158. package/test/shamap.test.js +89 -0
  159. package/test/signing-data-encoding.test.js +242 -0
  160. package/test/tx-encode-decode.test.js +119 -0
  161. package/test/types.test.js +34 -0
  162. package/test/uint.test.js +148 -0
  163. package/test/utils.js +30 -0
  164. package/test/x-address.test.js +181 -0
@@ -0,0 +1,210 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.BinaryParser = void 0;
27
+ const assert = __importStar(require("assert"));
28
+ const enums_1 = require("../enums");
29
+ const buffer_1 = require("buffer/");
30
+ /**
31
+ * BinaryParser is used to compute fields and values from a HexString
32
+ */
33
+ class BinaryParser {
34
+ /**
35
+ * Initialize bytes to a hex string
36
+ *
37
+ * @param hexBytes a hex string
38
+ * @param definitions Rippled definitions used to parse the values of transaction types and such.
39
+ * Can be customized for sidechains and amendments.
40
+ */
41
+ constructor(hexBytes, definitions = enums_1.DEFAULT_DEFINITIONS) {
42
+ this.bytes = buffer_1.Buffer.from(hexBytes, 'hex');
43
+ this.definitions = definitions;
44
+ }
45
+ /**
46
+ * Peek the first byte of the BinaryParser
47
+ *
48
+ * @returns The first byte of the BinaryParser
49
+ */
50
+ peek() {
51
+ assert.ok(this.bytes.byteLength !== 0);
52
+ return this.bytes[0];
53
+ }
54
+ /**
55
+ * Consume the first n bytes of the BinaryParser
56
+ *
57
+ * @param n the number of bytes to skip
58
+ */
59
+ skip(n) {
60
+ assert.ok(n <= this.bytes.byteLength);
61
+ this.bytes = this.bytes.slice(n);
62
+ }
63
+ /**
64
+ * read the first n bytes from the BinaryParser
65
+ *
66
+ * @param n The number of bytes to read
67
+ * @return The bytes
68
+ */
69
+ read(n) {
70
+ assert.ok(n <= this.bytes.byteLength);
71
+ const slice = this.bytes.slice(0, n);
72
+ this.skip(n);
73
+ return slice;
74
+ }
75
+ /**
76
+ * Read an integer of given size
77
+ *
78
+ * @param n The number of bytes to read
79
+ * @return The number represented by those bytes
80
+ */
81
+ readUIntN(n) {
82
+ assert.ok(0 < n && n <= 4, 'invalid n');
83
+ return this.read(n).reduce((a, b) => (a << 8) | b) >>> 0;
84
+ }
85
+ readUInt8() {
86
+ return this.readUIntN(1);
87
+ }
88
+ readUInt16() {
89
+ return this.readUIntN(2);
90
+ }
91
+ readUInt32() {
92
+ return this.readUIntN(4);
93
+ }
94
+ size() {
95
+ return this.bytes.byteLength;
96
+ }
97
+ end(customEnd) {
98
+ const length = this.bytes.byteLength;
99
+ return length === 0 || (customEnd !== undefined && length <= customEnd);
100
+ }
101
+ /**
102
+ * Reads variable length encoded bytes
103
+ *
104
+ * @return The variable length bytes
105
+ */
106
+ readVariableLength() {
107
+ return this.read(this.readVariableLengthLength());
108
+ }
109
+ /**
110
+ * Reads the length of the variable length encoded bytes
111
+ *
112
+ * @return The length of the variable length encoded bytes
113
+ */
114
+ readVariableLengthLength() {
115
+ const b1 = this.readUInt8();
116
+ if (b1 <= 192) {
117
+ return b1;
118
+ }
119
+ else if (b1 <= 240) {
120
+ const b2 = this.readUInt8();
121
+ return 193 + (b1 - 193) * 256 + b2;
122
+ }
123
+ else if (b1 <= 254) {
124
+ const b2 = this.readUInt8();
125
+ const b3 = this.readUInt8();
126
+ return 12481 + (b1 - 241) * 65536 + b2 * 256 + b3;
127
+ }
128
+ throw new Error('Invalid variable length indicator');
129
+ }
130
+ /**
131
+ * Reads the field ordinal from the BinaryParser
132
+ *
133
+ * @return Field ordinal
134
+ */
135
+ readFieldOrdinal() {
136
+ let type = this.readUInt8();
137
+ let nth = type & 15;
138
+ type >>= 4;
139
+ if (type === 0) {
140
+ type = this.readUInt8();
141
+ if (type === 0 || type < 16) {
142
+ throw new Error('Cannot read FieldOrdinal, type_code out of range');
143
+ }
144
+ }
145
+ if (nth === 0) {
146
+ nth = this.readUInt8();
147
+ if (nth === 0 || nth < 16) {
148
+ throw new Error('Cannot read FieldOrdinal, field_code out of range');
149
+ }
150
+ }
151
+ return (type << 16) | nth;
152
+ }
153
+ /**
154
+ * Read the field from the BinaryParser
155
+ *
156
+ * @return The field represented by the bytes at the head of the BinaryParser
157
+ */
158
+ readField() {
159
+ return this.definitions.field.fromString(this.readFieldOrdinal().toString());
160
+ }
161
+ /**
162
+ * Read a given type from the BinaryParser
163
+ *
164
+ * @param type The type that you want to read from the BinaryParser
165
+ * @return The instance of that type read from the BinaryParser
166
+ */
167
+ readType(type) {
168
+ return type.fromParser(this);
169
+ }
170
+ /**
171
+ * Get the type associated with a given field
172
+ *
173
+ * @param field The field that you wan to get the type of
174
+ * @return The type associated with the given field
175
+ */
176
+ typeForField(field) {
177
+ return field.associatedType;
178
+ }
179
+ /**
180
+ * Read value of the type specified by field from the BinaryParser
181
+ *
182
+ * @param field The field that you want to get the associated value for
183
+ * @return The value associated with the given field
184
+ */
185
+ readFieldValue(field) {
186
+ const type = this.typeForField(field);
187
+ if (!type) {
188
+ throw new Error(`unsupported: (${field.name}, ${field.type.name})`);
189
+ }
190
+ const sizeHint = field.isVariableLengthEncoded
191
+ ? this.readVariableLengthLength()
192
+ : undefined;
193
+ const value = type.fromParser(this, sizeHint);
194
+ if (value === undefined) {
195
+ throw new Error(`fromParser for (${field.name}, ${field.type.name}) -> undefined `);
196
+ }
197
+ return value;
198
+ }
199
+ /**
200
+ * Get the next field and value from the BinaryParser
201
+ *
202
+ * @return The field and value
203
+ */
204
+ readFieldAndValue() {
205
+ const field = this.readField();
206
+ return [field, this.readFieldValue(field)];
207
+ }
208
+ }
209
+ exports.BinaryParser = BinaryParser;
210
+ //# 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,+CAAgC;AAChC,oCAIiB;AAEjB,oCAAgC;AAEhC;;GAEG;AACH,MAAM,YAAY;IAIhB;;;;;;OAMG;IACH,YACE,QAAgB,EAChB,cAAmC,2BAAmB;QAEtD,IAAI,CAAC,KAAK,GAAG,eAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;QACzC,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;IAChC,CAAC;IAED;;;;OAIG;IACH,IAAI;QACF,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,KAAK,CAAC,CAAC,CAAA;QACtC,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;IACtB,CAAC;IAED;;;;OAIG;IACH,IAAI,CAAC,CAAS;QACZ,MAAM,CAAC,EAAE,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAA;QACrC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;IAClC,CAAC;IAED;;;;;OAKG;IACH,IAAI,CAAC,CAAS;QACZ,MAAM,CAAC,EAAE,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,CAAA;QAErC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;QACpC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACZ,OAAO,KAAK,CAAA;IACd,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAAC,CAAS;QACjB,MAAM,CAAC,EAAE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oB;QAC/B,OAAO,KAAK,CAAC,cAAc,CAAA;IAC7B,CAAC;IAED;;;;;OAKG;IACH,cAAc,CAAC,KAAoB;QACjC,MAAM,IAAI,GAAG,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,CAAA;QACrC,IAAI,CAAC,IAAI,EAAE;YACT,MAAM,IAAI,KAAK,CAAC,iBAAiB,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAA;SACpE;QACD,MAAM,QAAQ,GAAG,KAAK,CAAC,uBAAuB;YAC5C,CAAC,CAAC,IAAI,CAAC,wBAAwB,EAAE;YACjC,CAAC,CAAC,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,82 @@
1
+ import { FieldInstance } from '../enums';
2
+ import { type SerializedType } from '../types/serialized-type';
3
+ import { Buffer } from 'buffer/';
4
+ /**
5
+ * Bytes list is a collection of buffer objects
6
+ */
7
+ declare class BytesList {
8
+ private bytesArray;
9
+ /**
10
+ * Get the total number of bytes in the BytesList
11
+ *
12
+ * @return the number of bytes
13
+ */
14
+ getLength(): number;
15
+ /**
16
+ * Put bytes in the BytesList
17
+ *
18
+ * @param bytesArg A Buffer
19
+ * @return this BytesList
20
+ */
21
+ put(bytesArg: Buffer): BytesList;
22
+ /**
23
+ * Write this BytesList to the back of another bytes list
24
+ *
25
+ * @param list The BytesList to write to
26
+ */
27
+ toBytesSink(list: BytesList): void;
28
+ toBytes(): Buffer;
29
+ toHex(): string;
30
+ }
31
+ /**
32
+ * BinarySerializer is used to write fields and values to buffers
33
+ */
34
+ declare class BinarySerializer {
35
+ private sink;
36
+ constructor(sink: BytesList);
37
+ /**
38
+ * Write a value to this BinarySerializer
39
+ *
40
+ * @param value a SerializedType value
41
+ */
42
+ write(value: SerializedType): void;
43
+ /**
44
+ * Write bytes to this BinarySerializer
45
+ *
46
+ * @param bytes the bytes to write
47
+ */
48
+ put(bytes: Buffer): void;
49
+ /**
50
+ * Write a value of a given type to this BinarySerializer
51
+ *
52
+ * @param type the type to write
53
+ * @param value a value of that type
54
+ */
55
+ writeType(type: typeof SerializedType, value: SerializedType): void;
56
+ /**
57
+ * Write BytesList to this BinarySerializer
58
+ *
59
+ * @param bl BytesList to write to BinarySerializer
60
+ */
61
+ writeBytesList(bl: BytesList): void;
62
+ /**
63
+ * Calculate the header of Variable Length encoded bytes
64
+ *
65
+ * @param length the length of the bytes
66
+ */
67
+ private encodeVariableLength;
68
+ /**
69
+ * Write field and value to BinarySerializer
70
+ *
71
+ * @param field field to write to BinarySerializer
72
+ * @param value value to write to BinarySerializer
73
+ */
74
+ writeFieldAndValue(field: FieldInstance, value: SerializedType, isUnlModifyWorkaround?: boolean): void;
75
+ /**
76
+ * Write a variable length encoded value to the BinarySerializer
77
+ *
78
+ * @param value length encoded value to write to BytesList
79
+ */
80
+ writeLengthEncoded(value: SerializedType, isUnlModifyWorkaround?: boolean): void;
81
+ }
82
+ export { BytesList, BinarySerializer };
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.BinarySerializer = exports.BytesList = void 0;
27
+ const assert = __importStar(require("assert"));
28
+ const buffer_1 = require("buffer/");
29
+ /**
30
+ * Bytes list is a collection of buffer objects
31
+ */
32
+ class BytesList {
33
+ constructor() {
34
+ this.bytesArray = [];
35
+ }
36
+ /**
37
+ * Get the total number of bytes in the BytesList
38
+ *
39
+ * @return the number of bytes
40
+ */
41
+ getLength() {
42
+ return buffer_1.Buffer.concat(this.bytesArray).byteLength;
43
+ }
44
+ /**
45
+ * Put bytes in the BytesList
46
+ *
47
+ * @param bytesArg A Buffer
48
+ * @return this BytesList
49
+ */
50
+ put(bytesArg) {
51
+ const bytes = buffer_1.Buffer.from(bytesArg); // Temporary, to catch instances of Uint8Array being passed in
52
+ this.bytesArray.push(bytes);
53
+ return this;
54
+ }
55
+ /**
56
+ * Write this BytesList to the back of another bytes list
57
+ *
58
+ * @param list The BytesList to write to
59
+ */
60
+ toBytesSink(list) {
61
+ list.put(this.toBytes());
62
+ }
63
+ toBytes() {
64
+ return buffer_1.Buffer.concat(this.bytesArray);
65
+ }
66
+ toHex() {
67
+ return this.toBytes().toString('hex').toUpperCase();
68
+ }
69
+ }
70
+ exports.BytesList = BytesList;
71
+ /**
72
+ * BinarySerializer is used to write fields and values to buffers
73
+ */
74
+ class BinarySerializer {
75
+ constructor(sink) {
76
+ this.sink = new BytesList();
77
+ this.sink = sink;
78
+ }
79
+ /**
80
+ * Write a value to this BinarySerializer
81
+ *
82
+ * @param value a SerializedType value
83
+ */
84
+ write(value) {
85
+ value.toBytesSink(this.sink);
86
+ }
87
+ /**
88
+ * Write bytes to this BinarySerializer
89
+ *
90
+ * @param bytes the bytes to write
91
+ */
92
+ put(bytes) {
93
+ this.sink.put(bytes);
94
+ }
95
+ /**
96
+ * Write a value of a given type to this BinarySerializer
97
+ *
98
+ * @param type the type to write
99
+ * @param value a value of that type
100
+ */
101
+ writeType(type, value) {
102
+ this.write(type.from(value));
103
+ }
104
+ /**
105
+ * Write BytesList to this BinarySerializer
106
+ *
107
+ * @param bl BytesList to write to BinarySerializer
108
+ */
109
+ writeBytesList(bl) {
110
+ bl.toBytesSink(this.sink);
111
+ }
112
+ /**
113
+ * Calculate the header of Variable Length encoded bytes
114
+ *
115
+ * @param length the length of the bytes
116
+ */
117
+ encodeVariableLength(length) {
118
+ const lenBytes = buffer_1.Buffer.alloc(3);
119
+ if (length <= 192) {
120
+ lenBytes[0] = length;
121
+ return lenBytes.slice(0, 1);
122
+ }
123
+ else if (length <= 12480) {
124
+ length -= 193;
125
+ lenBytes[0] = 193 + (length >>> 8);
126
+ lenBytes[1] = length & 0xff;
127
+ return lenBytes.slice(0, 2);
128
+ }
129
+ else if (length <= 918744) {
130
+ length -= 12481;
131
+ lenBytes[0] = 241 + (length >>> 16);
132
+ lenBytes[1] = (length >> 8) & 0xff;
133
+ lenBytes[2] = length & 0xff;
134
+ return lenBytes.slice(0, 3);
135
+ }
136
+ throw new Error('Overflow error');
137
+ }
138
+ /**
139
+ * Write field and value to BinarySerializer
140
+ *
141
+ * @param field field to write to BinarySerializer
142
+ * @param value value to write to BinarySerializer
143
+ */
144
+ writeFieldAndValue(field, value, isUnlModifyWorkaround = false) {
145
+ const associatedValue = field.associatedType.from(value);
146
+ assert.ok(associatedValue.toBytesSink !== undefined);
147
+ assert.ok(field.name !== undefined);
148
+ this.sink.put(field.header);
149
+ if (field.isVariableLengthEncoded) {
150
+ this.writeLengthEncoded(associatedValue, isUnlModifyWorkaround);
151
+ }
152
+ else {
153
+ associatedValue.toBytesSink(this.sink);
154
+ }
155
+ }
156
+ /**
157
+ * Write a variable length encoded value to the BinarySerializer
158
+ *
159
+ * @param value length encoded value to write to BytesList
160
+ */
161
+ writeLengthEncoded(value, isUnlModifyWorkaround = false) {
162
+ const bytes = new BytesList();
163
+ if (!isUnlModifyWorkaround) {
164
+ // this part doesn't happen for the Account field in a UNLModify transaction
165
+ value.toBytesSink(bytes);
166
+ }
167
+ this.put(this.encodeVariableLength(bytes.getLength()));
168
+ this.writeBytesList(bytes);
169
+ }
170
+ }
171
+ exports.BinarySerializer = BinarySerializer;
172
+ //# sourceMappingURL=binary-serializer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"binary-serializer.js","sourceRoot":"","sources":["../../src/serdes/binary-serializer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAgC;AAGhC,oCAAgC;AAEhC;;GAEG;AACH,MAAM,SAAS;IAAf;QACU,eAAU,GAAkB,EAAE,CAAA;IAuCxC,CAAC;IArCC;;;;OAIG;IACI,SAAS;QACd,OAAO,eAAM,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,UAAU,CAAA;IAClD,CAAC;IAED;;;;;OAKG;IACI,GAAG,CAAC,QAAgB;QACzB,MAAM,KAAK,GAAG,eAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA,CAAC,8DAA8D;QAClG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC3B,OAAO,IAAI,CAAA;IACb,CAAC;IAED;;;;OAIG;IACI,WAAW,CAAC,IAAe;QAChC,IAAI,CAAC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ACvC;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,IAAI,CAAC,oBAAoB,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC,CAAC,CAAA;QACtD,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,CAAA;IAC5B,CAAC;CACF;AAEmB,4CAAgB"}
@@ -0,0 +1,103 @@
1
+ import { Hash256 } from './types/hash-256';
2
+ import { BytesList } from './serdes/binary-serializer';
3
+ import { Buffer } from 'buffer/';
4
+ /**
5
+ * Abstract class describing a SHAMapNode
6
+ */
7
+ declare abstract class ShaMapNode {
8
+ abstract hashPrefix(): Buffer;
9
+ abstract isLeaf(): boolean;
10
+ abstract isInner(): boolean;
11
+ abstract toBytesSink(list: BytesList): void;
12
+ abstract hash(): Hash256;
13
+ }
14
+ /**
15
+ * Class describing a Leaf of SHAMap
16
+ */
17
+ declare class ShaMapLeaf extends ShaMapNode {
18
+ index: Hash256;
19
+ item?: ShaMapNode | undefined;
20
+ constructor(index: Hash256, item?: ShaMapNode | undefined);
21
+ /**
22
+ * @returns true as ShaMapLeaf is a leaf node
23
+ */
24
+ isLeaf(): boolean;
25
+ /**
26
+ * @returns false as ShaMapLeaf is not an inner node
27
+ */
28
+ isInner(): boolean;
29
+ /**
30
+ * Get the prefix of the this.item
31
+ *
32
+ * @returns The hash prefix, unless this.item is undefined, then it returns an empty Buffer
33
+ */
34
+ hashPrefix(): Buffer;
35
+ /**
36
+ * Hash the bytes representation of this
37
+ *
38
+ * @returns hash of this.item concatenated with this.index
39
+ */
40
+ hash(): Hash256;
41
+ /**
42
+ * Write the bytes representation of this to a BytesList
43
+ * @param list BytesList to write bytes to
44
+ */
45
+ toBytesSink(list: BytesList): void;
46
+ }
47
+ /**
48
+ * Class defining an Inner Node of a SHAMap
49
+ */
50
+ declare class ShaMapInner extends ShaMapNode {
51
+ private depth;
52
+ private slotBits;
53
+ private branches;
54
+ constructor(depth?: number);
55
+ /**
56
+ * @returns true as ShaMapInner is an inner node
57
+ */
58
+ isInner(): boolean;
59
+ /**
60
+ * @returns false as ShaMapInner is not a leaf node
61
+ */
62
+ isLeaf(): boolean;
63
+ /**
64
+ * Get the hash prefix for this node
65
+ *
66
+ * @returns hash prefix describing an inner node
67
+ */
68
+ hashPrefix(): Buffer;
69
+ /**
70
+ * Set a branch of this node to be another node
71
+ *
72
+ * @param slot Slot to add branch to this.branches
73
+ * @param branch Branch to add
74
+ */
75
+ setBranch(slot: number, branch: ShaMapNode): void;
76
+ /**
77
+ * @returns true if node is empty
78
+ */
79
+ empty(): boolean;
80
+ /**
81
+ * Compute the hash of this node
82
+ *
83
+ * @returns The hash of this node
84
+ */
85
+ hash(): Hash256;
86
+ /**
87
+ * Writes the bytes representation of this node to a BytesList
88
+ *
89
+ * @param list BytesList to write bytes to
90
+ */
91
+ toBytesSink(list: BytesList): void;
92
+ /**
93
+ * Add item to the SHAMap
94
+ *
95
+ * @param index Hash of the index of the item being inserted
96
+ * @param item Item to insert in the map
97
+ * @param leaf Leaf node to insert when branch doesn't exist
98
+ */
99
+ addItem(index?: Hash256, item?: ShaMapNode, leaf?: ShaMapLeaf): void;
100
+ }
101
+ declare class ShaMap extends ShaMapInner {
102
+ }
103
+ export { ShaMap, ShaMapNode, ShaMapLeaf };