@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,149 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.STObject = void 0;
4
+ const enums_1 = require("../enums");
5
+ const serialized_type_1 = require("./serialized-type");
6
+ const ripple_address_codec_1 = require("ripple-address-codec");
7
+ const binary_parser_1 = require("../serdes/binary-parser");
8
+ const binary_serializer_1 = require("../serdes/binary-serializer");
9
+ const buffer_1 = require("buffer/");
10
+ const OBJECT_END_MARKER_BYTE = buffer_1.Buffer.from([0xe1]);
11
+ const OBJECT_END_MARKER = 'ObjectEndMarker';
12
+ const ST_OBJECT = 'STObject';
13
+ const DESTINATION = 'Destination';
14
+ const ACCOUNT = 'Account';
15
+ const SOURCE_TAG = 'SourceTag';
16
+ const DEST_TAG = 'DestinationTag';
17
+ /**
18
+ * Break down an X-Address into an account and a tag
19
+ *
20
+ * @param field Name of field
21
+ * @param xAddress X-Address corresponding to the field
22
+ */
23
+ function handleXAddress(field, xAddress) {
24
+ const decoded = (0, ripple_address_codec_1.xAddressToClassicAddress)(xAddress);
25
+ let tagName;
26
+ if (field === DESTINATION)
27
+ tagName = DEST_TAG;
28
+ else if (field === ACCOUNT)
29
+ tagName = SOURCE_TAG;
30
+ else if (decoded.tag !== false)
31
+ throw new Error(`${field} cannot have an associated tag`);
32
+ return decoded.tag !== false
33
+ ? { [field]: decoded.classicAddress, [tagName]: decoded.tag }
34
+ : { [field]: decoded.classicAddress };
35
+ }
36
+ /**
37
+ * Validate that two objects don't both have the same tag fields
38
+ *
39
+ * @param obj1 First object to check for tags
40
+ * @param obj2 Second object to check for tags
41
+ * @throws When both objects have SourceTag or DestinationTag
42
+ */
43
+ function checkForDuplicateTags(obj1, obj2) {
44
+ if (!(obj1[SOURCE_TAG] === undefined || obj2[SOURCE_TAG] === undefined))
45
+ throw new Error('Cannot have Account X-Address and SourceTag');
46
+ if (!(obj1[DEST_TAG] === undefined || obj2[DEST_TAG] === undefined))
47
+ throw new Error('Cannot have Destination X-Address and DestinationTag');
48
+ }
49
+ /**
50
+ * Class for Serializing/Deserializing objects
51
+ */
52
+ class STObject extends serialized_type_1.SerializedType {
53
+ /**
54
+ * Construct a STObject from a BinaryParser
55
+ *
56
+ * @param parser BinaryParser to read STObject from
57
+ * @returns A STObject object
58
+ */
59
+ static fromParser(parser) {
60
+ const list = new binary_serializer_1.BytesList();
61
+ const bytes = new binary_serializer_1.BinarySerializer(list);
62
+ while (!parser.end()) {
63
+ const field = parser.readField();
64
+ if (field.name === OBJECT_END_MARKER) {
65
+ break;
66
+ }
67
+ const associatedValue = parser.readFieldValue(field);
68
+ bytes.writeFieldAndValue(field, associatedValue);
69
+ if (field.type.name === ST_OBJECT) {
70
+ bytes.put(OBJECT_END_MARKER_BYTE);
71
+ }
72
+ }
73
+ return new STObject(list.toBytes());
74
+ }
75
+ /**
76
+ * Construct a STObject from a JSON object
77
+ *
78
+ * @param value An object to include
79
+ * @param filter optional, denote which field to include in serialized object
80
+ * @param definitions optional, types and values to use to encode/decode a transaction
81
+ * @returns a STObject object
82
+ */
83
+ static from(value, filter, definitions = enums_1.DEFAULT_DEFINITIONS) {
84
+ if (value instanceof STObject) {
85
+ return value;
86
+ }
87
+ const list = new binary_serializer_1.BytesList();
88
+ const bytes = new binary_serializer_1.BinarySerializer(list);
89
+ let isUnlModify = false;
90
+ const xAddressDecoded = Object.entries(value).reduce((acc, [key, val]) => {
91
+ let handled = undefined;
92
+ if (val && (0, ripple_address_codec_1.isValidXAddress)(val.toString())) {
93
+ handled = handleXAddress(key, val.toString());
94
+ checkForDuplicateTags(handled, value);
95
+ }
96
+ return Object.assign(acc, handled !== null && handled !== void 0 ? handled : { [key]: val });
97
+ }, {});
98
+ let sorted = Object.keys(xAddressDecoded)
99
+ .map((f) => definitions.field[f])
100
+ .filter((f) => f !== undefined &&
101
+ xAddressDecoded[f.name] !== undefined &&
102
+ f.isSerialized)
103
+ .sort((a, b) => {
104
+ return a.ordinal - b.ordinal;
105
+ });
106
+ if (filter !== undefined) {
107
+ sorted = sorted.filter(filter);
108
+ }
109
+ sorted.forEach((field) => {
110
+ const associatedValue = field.associatedType.from(xAddressDecoded[field.name]);
111
+ if (associatedValue == undefined) {
112
+ throw new TypeError(`Unable to interpret "${field.name}: ${xAddressDecoded[field.name]}".`);
113
+ }
114
+ if (associatedValue.name === 'UNLModify') {
115
+ // triggered when the TransactionType field has a value of 'UNLModify'
116
+ isUnlModify = true;
117
+ }
118
+ // true when in the UNLModify pseudotransaction (after the transaction type has been processed) and working with the
119
+ // Account field
120
+ // The Account field must not be a part of the UNLModify pseudotransaction encoding, due to a bug in rippled
121
+ const isUnlModifyWorkaround = field.name == 'Account' && isUnlModify;
122
+ bytes.writeFieldAndValue(field, associatedValue, isUnlModifyWorkaround);
123
+ if (field.type.name === ST_OBJECT) {
124
+ bytes.put(OBJECT_END_MARKER_BYTE);
125
+ }
126
+ });
127
+ return new STObject(list.toBytes());
128
+ }
129
+ /**
130
+ * Get the JSON interpretation of this.bytes
131
+ * @param definitions rippled definitions used to parse the values of transaction types and such.
132
+ * Can be customized for sidechains and amendments.
133
+ * @returns a JSON object
134
+ */
135
+ toJSON(definitions) {
136
+ const objectParser = new binary_parser_1.BinaryParser(this.toString(), definitions);
137
+ const accumulator = {};
138
+ while (!objectParser.end()) {
139
+ const field = objectParser.readField();
140
+ if (field.name === OBJECT_END_MARKER) {
141
+ break;
142
+ }
143
+ accumulator[field.name] = objectParser.readFieldValue(field).toJSON();
144
+ }
145
+ return accumulator;
146
+ }
147
+ }
148
+ exports.STObject = STObject;
149
+ //# sourceMappingURL=st-object.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"st-object.js","sourceRoot":"","sources":["../../src/types/st-object.ts"],"names":[],"mappings":";;;AAAA,oCAKiB;AACjB,uDAA8D;AAC9D,+DAAgF;AAChF,2DAAsD;AACtD,mEAAyE;AACzE,oCAAgC;AAEhC,MAAM,sBAAsB,GAAG,eAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAA;AAClD,MAAM,iBAAiB,GAAG,iBAAiB,CAAA;AAC3C,MAAM,SAAS,GAAG,UAAU,CA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EAAsE;gBACtE,WAAW,GAAG,IAAI,CAAA;aACnB;YACD,oHAAoH;YACpH,gBAAgB;YAChB,4GAA4G;YAC5G,MAAM,qBAAqB,GAAG,KAAK,CAAC,IAAI,IAAI,SAAS,IAAI,WAAW,CAAA;YACpE,KAAK,CAAC,kBAAkB,CAAC,KAAK,EAAE,eAAe,EAAE,qBAAqB,CAAC,CAAA;YACvE,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE;gBACjC,KAAK,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAA;aAClC;QACH,CAAC,CAAC,CAAA;QAEF,OAAO,IAAI,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAA;IACrC,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,WAAiC;QACtC,MAAM,YAAY,GAAG,IAAI,4BAAY,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,WAAW,CAAC,CAAA;QACnE,MAAM,WAAW,GAAG,EAAE,CAAA;QAEtB,OAAO,CAAC,YAAY,CAAC,GAAG,EAAE,EAAE;YAC1B,MAAM,KAAK,GAAG,YAAY,CAAC,SAAS,EAAE,CAAA;YACtC,IAAI,KAAK,CAAC,IAAI,KAAK,iBAAiB,EAAE;gBACpC,MAAK;aACN;YACD,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,YAAY,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAA;SACtE;QAED,OAAO,WAAW,CAAA;IACpB,CAAC;CACF;AAEQ,4BAAQ"}
@@ -0,0 +1,25 @@
1
+ import { UInt } from './uint';
2
+ import { BinaryParser } from '../serdes/binary-parser';
3
+ import { Buffer } from 'buffer/';
4
+ /**
5
+ * Derived UInt class for serializing/deserializing 16 bit UInt
6
+ */
7
+ declare class UInt16 extends UInt {
8
+ protected static readonly width: number;
9
+ static readonly defaultUInt16: UInt16;
10
+ constructor(bytes: Buffer);
11
+ static fromParser(parser: BinaryParser): UInt;
12
+ /**
13
+ * Construct a UInt16 object from a number
14
+ *
15
+ * @param val UInt16 object or number
16
+ */
17
+ static from<T extends UInt16 | number>(val: T): UInt16;
18
+ /**
19
+ * get the value of a UInt16 object
20
+ *
21
+ * @returns the number represented by this.bytes
22
+ */
23
+ valueOf(): number;
24
+ }
25
+ export { UInt16 };
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UInt16 = void 0;
4
+ const uint_1 = require("./uint");
5
+ const buffer_1 = require("buffer/");
6
+ /**
7
+ * Derived UInt class for serializing/deserializing 16 bit UInt
8
+ */
9
+ class UInt16 extends uint_1.UInt {
10
+ constructor(bytes) {
11
+ super(bytes !== null && bytes !== void 0 ? bytes : UInt16.defaultUInt16.bytes);
12
+ }
13
+ static fromParser(parser) {
14
+ return new UInt16(parser.read(UInt16.width));
15
+ }
16
+ /**
17
+ * Construct a UInt16 object from a number
18
+ *
19
+ * @param val UInt16 object or number
20
+ */
21
+ static from(val) {
22
+ if (val instanceof UInt16) {
23
+ return val;
24
+ }
25
+ if (typeof val === 'number') {
26
+ const buf = buffer_1.Buffer.alloc(UInt16.width);
27
+ buf.writeUInt16BE(val, 0);
28
+ return new UInt16(buf);
29
+ }
30
+ throw new Error('Can not construct UInt16 with given value');
31
+ }
32
+ /**
33
+ * get the value of a UInt16 object
34
+ *
35
+ * @returns the number represented by this.bytes
36
+ */
37
+ valueOf() {
38
+ return this.bytes.readUInt16BE(0);
39
+ }
40
+ }
41
+ exports.UInt16 = UInt16;
42
+ UInt16.width = 16 / 8; // 2
43
+ UInt16.defaultUInt16 = new UInt16(buffer_1.Buffer.alloc(UInt16.width));
44
+ //# sourceMappingURL=uint-16.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uint-16.js","sourceRoot":"","sources":["../../src/types/uint-16.ts"],"names":[],"mappings":";;;AAAA,iCAA6B;AAE7B,oCAAgC;AAEhC;;GAEG;AACH,MAAM,MAAO,SAAQ,WAAI;IAIvB,YAAY,KAAa;QACvB,KAAK,CAAC,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,CAAA;IAC5C,CAAC;IAED,MAAM,CAAC,UAAU,CAAC,MAAoB;QACpC,OAAO,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA;IAC9C,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,IAAI,CAA4B,GAAM;QAC3C,IAAI,GAAG,YAAY,MAAM,EAAE;YACzB,OAAO,GAAG,CAAA;SACX;QAED,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC3B,MAAM,GAAG,GAAG,eAAM,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;YACtC,GAAG,CAAC,aAAa,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;YACzB,OAAO,IAAI,MAAM,CAAC,GAAG,CAAC,CAAA;SACvB;QAED,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAA;IAC9D,CAAC;IAED;;;;OAIG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAA;IACnC,CAAC;;AAGM,wBAAM;AAxCa,YAAK,GAAW,EAAE,GAAG,CAAC,CAAA,CAAC,IAAI;AACrC,oBAAa,GAAW,IAAI,MAAM,CAAC,eAAM,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA"}
@@ -0,0 +1,25 @@
1
+ import { UInt } from './uint';
2
+ import { BinaryParser } from '../serdes/binary-parser';
3
+ import { Buffer } from 'buffer/';
4
+ /**
5
+ * Derived UInt class for serializing/deserializing 32 bit UInt
6
+ */
7
+ declare class UInt32 extends UInt {
8
+ protected static readonly width: number;
9
+ static readonly defaultUInt32: UInt32;
10
+ constructor(bytes: Buffer);
11
+ static fromParser(parser: BinaryParser): UInt;
12
+ /**
13
+ * Construct a UInt32 object from a number
14
+ *
15
+ * @param val UInt32 object or number
16
+ */
17
+ static from<T extends UInt32 | number | string>(val: T): UInt32;
18
+ /**
19
+ * get the value of a UInt32 object
20
+ *
21
+ * @returns the number represented by this.bytes
22
+ */
23
+ valueOf(): number;
24
+ }
25
+ export { UInt32 };
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UInt32 = void 0;
4
+ const uint_1 = require("./uint");
5
+ const buffer_1 = require("buffer/");
6
+ /**
7
+ * Derived UInt class for serializing/deserializing 32 bit UInt
8
+ */
9
+ class UInt32 extends uint_1.UInt {
10
+ constructor(bytes) {
11
+ super(bytes !== null && bytes !== void 0 ? bytes : UInt32.defaultUInt32.bytes);
12
+ }
13
+ static fromParser(parser) {
14
+ return new UInt32(parser.read(UInt32.width));
15
+ }
16
+ /**
17
+ * Construct a UInt32 object from a number
18
+ *
19
+ * @param val UInt32 object or number
20
+ */
21
+ static from(val) {
22
+ if (val instanceof UInt32) {
23
+ return val;
24
+ }
25
+ const buf = buffer_1.Buffer.alloc(UInt32.width);
26
+ if (typeof val === 'string') {
27
+ const num = Number.parseInt(val);
28
+ buf.writeUInt32BE(num, 0);
29
+ return new UInt32(buf);
30
+ }
31
+ if (typeof val === 'number') {
32
+ buf.writeUInt32BE(val, 0);
33
+ return new UInt32(buf);
34
+ }
35
+ throw new Error('Cannot construct UInt32 from given value');
36
+ }
37
+ /**
38
+ * get the value of a UInt32 object
39
+ *
40
+ * @returns the number represented by this.bytes
41
+ */
42
+ valueOf() {
43
+ return this.bytes.readUInt32BE(0);
44
+ }
45
+ }
46
+ exports.UInt32 = UInt32;
47
+ UInt32.width = 32 / 8; // 4
48
+ UInt32.defaultUInt32 = new UInt32(buffer_1.Buffer.alloc(UInt32.width));
49
+ //# sourceMappingURL=uint-32.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uint-32.js","sourceRoot":"","sources":["../../src/types/u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AAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA"}
@@ -0,0 +1,39 @@
1
+ import { UInt } from './uint';
2
+ import { BinaryParser } from '../serdes/binary-parser';
3
+ import bigInt = require('big-integer');
4
+ import { Buffer } from 'buffer/';
5
+ /**
6
+ * Derived UInt class for serializing/deserializing 64 bit UInt
7
+ */
8
+ declare class UInt64 extends UInt {
9
+ protected static readonly width: number;
10
+ static readonly defaultUInt64: UInt64;
11
+ constructor(bytes: Buffer);
12
+ static fromParser(parser: BinaryParser): UInt;
13
+ /**
14
+ * Construct a UInt64 object
15
+ *
16
+ * @param val A UInt64, hex-string, bigInt, or number
17
+ * @returns A UInt64 object
18
+ */
19
+ static from<T extends UInt64 | string | bigInt.BigInteger | number>(val: T): UInt64;
20
+ /**
21
+ * The JSON representation of a UInt64 object
22
+ *
23
+ * @returns a hex-string
24
+ */
25
+ toJSON(): string;
26
+ /**
27
+ * Get the value of the UInt64
28
+ *
29
+ * @returns the number represented buy this.bytes
30
+ */
31
+ valueOf(): bigInt.BigInteger;
32
+ /**
33
+ * Get the bytes representation of the UInt64 object
34
+ *
35
+ * @returns 8 bytes representing the UInt64
36
+ */
37
+ toBytes(): Buffer;
38
+ }
39
+ export { UInt64 };
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UInt64 = void 0;
4
+ const uint_1 = require("./uint");
5
+ const bigInt = require("big-integer");
6
+ const big_integer_1 = require("big-integer");
7
+ const buffer_1 = require("buffer/");
8
+ const HEX_REGEX = /^[a-fA-F0-9]{1,16}$/;
9
+ const mask = bigInt(0x00000000ffffffff);
10
+ /**
11
+ * Derived UInt class for serializing/deserializing 64 bit UInt
12
+ */
13
+ class UInt64 extends uint_1.UInt {
14
+ constructor(bytes) {
15
+ super(bytes !== null && bytes !== void 0 ? bytes : UInt64.defaultUInt64.bytes);
16
+ }
17
+ static fromParser(parser) {
18
+ return new UInt64(parser.read(UInt64.width));
19
+ }
20
+ /**
21
+ * Construct a UInt64 object
22
+ *
23
+ * @param val A UInt64, hex-string, bigInt, or number
24
+ * @returns A UInt64 object
25
+ */
26
+ static from(val) {
27
+ if (val instanceof UInt64) {
28
+ return val;
29
+ }
30
+ let buf = buffer_1.Buffer.alloc(UInt64.width);
31
+ if (typeof val === 'number') {
32
+ if (val < 0) {
33
+ throw new Error('value must be an unsigned integer');
34
+ }
35
+ const number = bigInt(val);
36
+ const intBuf = [buffer_1.Buffer.alloc(4), buffer_1.Buffer.alloc(4)];
37
+ intBuf[0].writeUInt32BE(Number(number.shiftRight(32)), 0);
38
+ intBuf[1].writeUInt32BE(Number(number.and(mask)), 0);
39
+ return new UInt64(buffer_1.Buffer.concat(intBuf));
40
+ }
41
+ if (typeof val === 'string') {
42
+ if (!HEX_REGEX.test(val)) {
43
+ throw new Error(`${val} is not a valid hex-string`);
44
+ }
45
+ const strBuf = val.padStart(16, '0');
46
+ buf = buffer_1.Buffer.from(strBuf, 'hex');
47
+ return new UInt64(buf);
48
+ }
49
+ if ((0, big_integer_1.isInstance)(val)) {
50
+ const intBuf = [buffer_1.Buffer.alloc(4), buffer_1.Buffer.alloc(4)];
51
+ intBuf[0].writeUInt32BE(Number(val.shiftRight(bigInt(32))), 0);
52
+ intBuf[1].writeUInt32BE(Number(val.and(mask)), 0);
53
+ return new UInt64(buffer_1.Buffer.concat(intBuf));
54
+ }
55
+ throw new Error('Cannot construct UInt64 from given value');
56
+ }
57
+ /**
58
+ * The JSON representation of a UInt64 object
59
+ *
60
+ * @returns a hex-string
61
+ */
62
+ toJSON() {
63
+ return this.bytes.toString('hex').toUpperCase();
64
+ }
65
+ /**
66
+ * Get the value of the UInt64
67
+ *
68
+ * @returns the number represented buy this.bytes
69
+ */
70
+ valueOf() {
71
+ const msb = bigInt(this.bytes.slice(0, 4).readUInt32BE(0));
72
+ const lsb = bigInt(this.bytes.slice(4).readUInt32BE(0));
73
+ return msb.shiftLeft(bigInt(32)).or(lsb);
74
+ }
75
+ /**
76
+ * Get the bytes representation of the UInt64 object
77
+ *
78
+ * @returns 8 bytes representing the UInt64
79
+ */
80
+ toBytes() {
81
+ return this.bytes;
82
+ }
83
+ }
84
+ exports.UInt64 = UInt64;
85
+ UInt64.width = 64 / 8; // 8
86
+ UInt64.defaultUInt64 = new UInt64(buffer_1.Buffer.alloc(UInt64.width));
87
+ //# sourceMappingURL=uint-64.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uint-64.js","sourceRoot":"","sources":["../../src/types/uint-64.ts"],"names":[],"mappings":";;;AAAA,iCAA6B;AAE7B,sCAAsC;AACtC,6CAAwC;AACxC,oCAAgC;AAEhC,MAAM,SAAS,GAAG,qBAAqB,CAAA;AACvC,MAAM,IAAI,GAAG,MAAM,CAAC,kBAAkB,CAAC,CAAA;AAEvC;;GAEG;AACH,MAAM,MAAO,SAAQ,WAAI;IAIvB,YAAY,KAAa;QACvB,KAAK,CAAC,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,CAAA;IAC5C,CAAC;IAED,MAAM,CAAC,UAAU,CAAC,MAAoB;QACpC,OAAO,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA;IAC9C,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,IAAI,CACT,GAAM;QAEN,IAAI,GAAG,YAAY,MAAM,EAAE;YACzB,OAAO,GAAG,CAAA;SACX;QAED,IAAI,GAAG,GAAG,eAAM,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;QAEpC,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC3B,IAAI,GAAG,GAAG,CAAC,EAAE;gBACX,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAA;aACrD;YAED,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,CAAA;YAE1B,MAAM,MAAM,GAAG,CAAC,eAAM,C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,CAAA;YAEjD,OAAO,IAAI,MAAM,CAAC,eAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAA;SACzC;QAED,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAA;IAC7D,CAAC;IAED;;;;OAIG;IACH,MAAM;QACJ,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,CAAA;IACjD,CAAC;IAED;;;;OAIG;IACH,OAAO;QACL,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAA;QAC1D,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAA;QACvD,OAAO,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAA;IAC1C,CAAC;IAED;;;;OAIG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;;AAGM,wBAAM;AA3Fa,YAAK,GAAW,EAAE,GAAG,CAAC,CAAA,CAAC,IAAI;AACrC,oBAAa,GAAW,IAAI,MAAM,CAAC,eAAM,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA"}
@@ -0,0 +1,25 @@
1
+ import { UInt } from './uint';
2
+ import { BinaryParser } from '../serdes/binary-parser';
3
+ import { Buffer } from 'buffer/';
4
+ /**
5
+ * Derived UInt class for serializing/deserializing 8 bit UInt
6
+ */
7
+ declare class UInt8 extends UInt {
8
+ protected static readonly width: number;
9
+ static readonly defaultUInt8: UInt8;
10
+ constructor(bytes: Buffer);
11
+ static fromParser(parser: BinaryParser): UInt;
12
+ /**
13
+ * Construct a UInt8 object from a number
14
+ *
15
+ * @param val UInt8 object or number
16
+ */
17
+ static from<T extends UInt8 | number>(val: T): UInt8;
18
+ /**
19
+ * get the value of a UInt8 object
20
+ *
21
+ * @returns the number represented by this.bytes
22
+ */
23
+ valueOf(): number;
24
+ }
25
+ export { UInt8 };
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UInt8 = void 0;
4
+ const uint_1 = require("./uint");
5
+ const buffer_1 = require("buffer/");
6
+ /**
7
+ * Derived UInt class for serializing/deserializing 8 bit UInt
8
+ */
9
+ class UInt8 extends uint_1.UInt {
10
+ constructor(bytes) {
11
+ super(bytes !== null && bytes !== void 0 ? bytes : UInt8.defaultUInt8.bytes);
12
+ }
13
+ static fromParser(parser) {
14
+ return new UInt8(parser.read(UInt8.width));
15
+ }
16
+ /**
17
+ * Construct a UInt8 object from a number
18
+ *
19
+ * @param val UInt8 object or number
20
+ */
21
+ static from(val) {
22
+ if (val instanceof UInt8) {
23
+ return val;
24
+ }
25
+ if (typeof val === 'number') {
26
+ const buf = buffer_1.Buffer.alloc(UInt8.width);
27
+ buf.writeUInt8(val, 0);
28
+ return new UInt8(buf);
29
+ }
30
+ throw new Error('Cannot construct UInt8 from given value');
31
+ }
32
+ /**
33
+ * get the value of a UInt8 object
34
+ *
35
+ * @returns the number represented by this.bytes
36
+ */
37
+ valueOf() {
38
+ return this.bytes.readUInt8(0);
39
+ }
40
+ }
41
+ exports.UInt8 = UInt8;
42
+ UInt8.width = 8 / 8; // 1
43
+ UInt8.defaultUInt8 = new UInt8(buffer_1.Buffer.alloc(UInt8.width));
44
+ //# sourceMappingURL=uint-8.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uint-8.js","sourceRoot":"","sources":["../../src/types/uint-8.ts"],"names":[],"mappings":";;;AAAA,iCAA6B;AAE7B,oCAAgC;AAEhC;;GAEG;AACH,MAAM,KAAM,SAAQ,WAAI;IAItB,YAAY,KAAa;QACvB,KAAK,CAAC,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,CAAA;IAC1C,CAAC;IAED,MAAM,CAAC,UAAU,CAAC,MAAoB;QACpC,OAAO,IAAI,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAA;IAC5C,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,IAAI,CAA2B,GAAM;QAC1C,IAAI,GAAG,YAAY,KAAK,EAAE;YACxB,OAAO,GAAG,CAAA;SACX;QAED,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC3B,MAAM,GAAG,GAAG,eAAM,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;YACrC,GAAG,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;YACtB,OAAO,IAAI,KAAK,CAAC,GAAG,CAAC,CAAA;SACtB;QAED,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAA;IAC5D,CAAC;IAED;;;;OAIG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAA;IAChC,CAAC;;AAGM,sBAAK;AAxCc,WAAK,GAAW,CAAC,GAAG,CAAC,CAAA,CAAC,IAAI;AACpC,kBAAY,GAAU,IAAI,KAAK,CAAC,eAAM,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAA"}
@@ -0,0 +1,30 @@
1
+ import bigInt = require('big-integer');
2
+ import { Comparable } from './serialized-type';
3
+ import { Buffer } from 'buffer/';
4
+ /**
5
+ * Base class for serializing and deserializing unsigned integers.
6
+ */
7
+ declare abstract class UInt extends Comparable {
8
+ protected static width: number;
9
+ constructor(bytes: Buffer);
10
+ /**
11
+ * Overload of compareTo for Comparable
12
+ *
13
+ * @param other other UInt to compare this to
14
+ * @returns -1, 0, or 1 depending on how the objects relate to each other
15
+ */
16
+ compareTo(other: UInt): number;
17
+ /**
18
+ * Convert a UInt object to JSON
19
+ *
20
+ * @returns number or string represented by this.bytes
21
+ */
22
+ toJSON(): number | string;
23
+ /**
24
+ * Get the value of the UInt represented by this.bytes
25
+ *
26
+ * @returns the value
27
+ */
28
+ abstract valueOf(): number | bigInt.BigInteger;
29
+ }
30
+ export { UInt };
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UInt = void 0;
4
+ const serialized_type_1 = require("./serialized-type");
5
+ /**
6
+ * Compare numbers and bigInts n1 and n2
7
+ *
8
+ * @param n1 First object to compare
9
+ * @param n2 Second object to compare
10
+ * @returns -1, 0, or 1, depending on how the two objects compare
11
+ */
12
+ function compare(n1, n2) {
13
+ return n1 < n2 ? -1 : n1 == n2 ? 0 : 1;
14
+ }
15
+ /**
16
+ * Base class for serializing and deserializing unsigned integers.
17
+ */
18
+ class UInt extends serialized_type_1.Comparable {
19
+ constructor(bytes) {
20
+ super(bytes);
21
+ }
22
+ /**
23
+ * Overload of compareTo for Comparable
24
+ *
25
+ * @param other other UInt to compare this to
26
+ * @returns -1, 0, or 1 depending on how the objects relate to each other
27
+ */
28
+ compareTo(other) {
29
+ return compare(this.valueOf(), other.valueOf());
30
+ }
31
+ /**
32
+ * Convert a UInt object to JSON
33
+ *
34
+ * @returns number or string represented by this.bytes
35
+ */
36
+ toJSON() {
37
+ const val = this.valueOf();
38
+ return typeof val === 'number' ? val : val.toString();
39
+ }
40
+ }
41
+ exports.UInt = UInt;
42
+ //# sourceMappingURL=uint.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uint.js","sourceRoot":"","sources":["../../src/types/uint.ts"],"names":[],"mappings":";;;AACA,uDAA8C;AAG9C;;;;;;GAMG;AACH,SAAS,OAAO,CACd,EAA8B,EAC9B,EAA8B;IAE9B,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AACxC,CAAC;AAED;;GAEG;AACH,MAAe,IAAK,SAAQ,4BAAU;IAGpC,YAAY,KAAa;QACvB,KAAK,CAAC,KAAK,CAAC,CAAA;IACd,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAAC,KAAW;QACnB,OAAO,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,CAAA;IACjD,CAAC;IAED;;;;OAIG;IACH,MAAM;QACJ,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,EAAE,CAAA;QAC1B,OAAO,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAA;IACvD,CAAC;CAQF;AAEQ,oBAAI"}
@@ -0,0 +1,31 @@
1
+ import { SerializedType } from './serialized-type';
2
+ import { BinaryParser } from '../serdes/binary-parser';
3
+ import { Buffer } from 'buffer/';
4
+ /**
5
+ * Class for serializing and deserializing vectors of Hash256
6
+ */
7
+ declare class Vector256 extends SerializedType {
8
+ constructor(bytes: Buffer);
9
+ /**
10
+ * Construct a Vector256 from a BinaryParser
11
+ *
12
+ * @param parser BinaryParser to
13
+ * @param hint length of the vector, in bytes, optional
14
+ * @returns a Vector256 object
15
+ */
16
+ static fromParser(parser: BinaryParser, hint?: number): Vector256;
17
+ /**
18
+ * Construct a Vector256 object from an array of hashes
19
+ *
20
+ * @param value A Vector256 object or array of hex-strings representing Hash256's
21
+ * @returns a Vector256 object
22
+ */
23
+ static from<T extends Vector256 | Array<string>>(value: T): Vector256;
24
+ /**
25
+ * Return an Array of hex-strings represented by this.bytes
26
+ *
27
+ * @returns An Array of strings representing the Hash256 objects
28
+ */
29
+ toJSON(): Array<string>;
30
+ }
31
+ export { Vector256 };