@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
package/dist/index.js ADDED
@@ -0,0 +1,139 @@
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.coreTypes = exports.DEFAULT_DEFINITIONS = exports.XrplDefinitionsBase = exports.XrplDefinitions = exports.TRANSACTION_TYPE_MAP = exports.TRANSACTION_TYPES = exports.decodeLedgerData = exports.decodeQuality = exports.encodeQuality = exports.encodeForMultisigning = exports.encodeForSigningClaim = exports.encodeForSigning = exports.encode = exports.decode = void 0;
27
+ const assert = __importStar(require("assert"));
28
+ const coretypes_1 = require("./coretypes");
29
+ const ledger_hashes_1 = require("./ledger-hashes");
30
+ Object.defineProperty(exports, "decodeLedgerData", { enumerable: true, get: function () { return ledger_hashes_1.decodeLedgerData; } });
31
+ const enums_1 = require("./enums");
32
+ Object.defineProperty(exports, "XrplDefinitionsBase", { enumerable: true, get: function () { return enums_1.XrplDefinitionsBase; } });
33
+ Object.defineProperty(exports, "TRANSACTION_TYPES", { enumerable: true, get: function () { return enums_1.TRANSACTION_TYPES; } });
34
+ Object.defineProperty(exports, "TRANSACTION_TYPE_MAP", { enumerable: true, get: function () { return enums_1.TRANSACTION_TYPE_MAP; } });
35
+ Object.defineProperty(exports, "DEFAULT_DEFINITIONS", { enumerable: true, get: function () { return enums_1.DEFAULT_DEFINITIONS; } });
36
+ const xrpl_definitions_1 = require("./enums/xrpl-definitions");
37
+ Object.defineProperty(exports, "XrplDefinitions", { enumerable: true, get: function () { return xrpl_definitions_1.XrplDefinitions; } });
38
+ const types_1 = require("./types");
39
+ Object.defineProperty(exports, "coreTypes", { enumerable: true, get: function () { return types_1.coreTypes; } });
40
+ const { signingData, signingClaimData, multiSigningData, binaryToJSON, serializeObject, } = coretypes_1.binary;
41
+ /**
42
+ * Decode a transaction
43
+ *
44
+ * @param binary hex-string of the encoded transaction
45
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
46
+ * @returns the JSON representation of the transaction
47
+ */
48
+ function decode(binary, definitions) {
49
+ assert.ok(typeof binary === 'string', 'binary must be a hex string');
50
+ return binaryToJSON(binary, definitions);
51
+ }
52
+ exports.decode = decode;
53
+ /**
54
+ * Encode a transaction
55
+ *
56
+ * @param json The JSON representation of a transaction
57
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
58
+ *
59
+ * @returns A hex-string of the encoded transaction
60
+ */
61
+ function encode(json, definitions) {
62
+ assert.ok(typeof json === 'object');
63
+ return serializeObject(json, { definitions })
64
+ .toString('hex')
65
+ .toUpperCase();
66
+ }
67
+ exports.encode = encode;
68
+ /**
69
+ * Encode a transaction and prepare for signing
70
+ *
71
+ * @param json JSON object representing the transaction
72
+ * @param signer string representing the account to sign the transaction with
73
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
74
+ * @returns a hex string of the encoded transaction
75
+ */
76
+ function encodeForSigning(json, definitions) {
77
+ assert.ok(typeof json === 'object');
78
+ return signingData(json, coretypes_1.HashPrefix.transactionSig, {
79
+ definitions,
80
+ })
81
+ .toString('hex')
82
+ .toUpperCase();
83
+ }
84
+ exports.encodeForSigning = encodeForSigning;
85
+ /**
86
+ * Encode a transaction and prepare for signing with a claim
87
+ *
88
+ * @param json JSON object representing the transaction
89
+ * @param signer string representing the account to sign the transaction with
90
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
91
+ * @returns a hex string of the encoded transaction
92
+ */
93
+ function encodeForSigningClaim(json) {
94
+ assert.ok(typeof json === 'object');
95
+ return signingClaimData(json)
96
+ .toString('hex')
97
+ .toUpperCase();
98
+ }
99
+ exports.encodeForSigningClaim = encodeForSigningClaim;
100
+ /**
101
+ * Encode a transaction and prepare for multi-signing
102
+ *
103
+ * @param json JSON object representing the transaction
104
+ * @param signer string representing the account to sign the transaction with
105
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
106
+ * @returns a hex string of the encoded transaction
107
+ */
108
+ function encodeForMultisigning(json, signer, definitions) {
109
+ assert.ok(typeof json === 'object');
110
+ assert.equal(json['SigningPubKey'], '');
111
+ const definitionsOpt = definitions ? { definitions } : undefined;
112
+ return multiSigningData(json, signer, definitionsOpt)
113
+ .toString('hex')
114
+ .toUpperCase();
115
+ }
116
+ exports.encodeForMultisigning = encodeForMultisigning;
117
+ /**
118
+ * Encode a quality value
119
+ *
120
+ * @param value string representation of a number
121
+ * @returns a hex-string representing the quality
122
+ */
123
+ function encodeQuality(value) {
124
+ assert.ok(typeof value === 'string');
125
+ return coretypes_1.quality.encode(value).toString('hex').toUpperCase();
126
+ }
127
+ exports.encodeQuality = encodeQuality;
128
+ /**
129
+ * Decode a quality value
130
+ *
131
+ * @param value hex-string of a quality
132
+ * @returns a string representing the quality
133
+ */
134
+ function decodeQuality(value) {
135
+ assert.ok(typeof value === 'string');
136
+ return coretypes_1.quality.decode(value).toString();
137
+ }
138
+ exports.decodeQuality = decodeQuality;
139
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;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Int = require('big-integer');
4
+ import { XrplDefinitionsBase } from './enums';
5
+ /**
6
+ * Function computing the hash of a transaction tree
7
+ *
8
+ * @param param An array of transaction objects to hash
9
+ * @returns A Hash256 object
10
+ */
11
+ declare function transactionTreeHash(param: Array<JsonObject>): Hash256;
12
+ /**
13
+ * Function computing the hash of accountState
14
+ *
15
+ * @param param A list of accountStates hash
16
+ * @returns A Hash256 object
17
+ */
18
+ declare function accountStateHash(param: Array<JsonObject>): Hash256;
19
+ /**
20
+ * Interface describing a ledger header
21
+ */
22
+ interface ledgerObject {
23
+ ledger_index: number;
24
+ total_coins: string | number | bigInt.BigInteger;
25
+ parent_hash: string;
26
+ transaction_hash: string;
27
+ account_hash: string;
28
+ parent_close_time: number;
29
+ close_time: number;
30
+ close_time_resolution: number;
31
+ close_flags: number;
32
+ }
33
+ /**
34
+ * Serialize and hash a ledger header
35
+ *
36
+ * @param header a ledger header
37
+ * @returns the hash of header
38
+ */
39
+ declare function ledgerHash(header: ledgerObject): Hash256;
40
+ /**
41
+ * Decodes a serialized ledger header
42
+ *
43
+ * @param binary A serialized ledger header
44
+ * @param definitions Type definitions to parse the ledger objects.
45
+ * Used if there are non-default ledger objects to decode.
46
+ * @returns A JSON object describing a ledger header
47
+ */
48
+ declare function decodeLedgerData(binary: string, definitions?: XrplDefinitionsBase): object;
49
+ export { accountStateHash, transactionTreeHash, ledgerHash, decodeLedgerData };
@@ -0,0 +1,160 @@
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.decodeLedgerData = exports.ledgerHash = exports.transactionTreeHash = exports.accountStateHash = void 0;
27
+ const assert = __importStar(require("assert"));
28
+ const shamap_1 = require("./shamap");
29
+ const hash_prefixes_1 = require("./hash-prefixes");
30
+ const hashes_1 = require("./hashes");
31
+ const binary_1 = require("./binary");
32
+ const hash_256_1 = require("./types/hash-256");
33
+ const st_object_1 = require("./types/st-object");
34
+ const uint_64_1 = require("./types/uint-64");
35
+ const uint_32_1 = require("./types/uint-32");
36
+ const uint_8_1 = require("./types/uint-8");
37
+ const binary_parser_1 = require("./serdes/binary-parser");
38
+ const bigInt = require("big-integer");
39
+ /**
40
+ * Computes the hash of a list of objects
41
+ *
42
+ * @param itemizer Converts an item into a format that can be added to SHAMap
43
+ * @param itemsJson Array of items to add to a SHAMap
44
+ * @returns the hash of the SHAMap
45
+ */
46
+ function computeHash(itemizer, itemsJson) {
47
+ const map = new shamap_1.ShaMap();
48
+ itemsJson.forEach((item) => map.addItem(...itemizer(item)));
49
+ return map.hash();
50
+ }
51
+ /**
52
+ * Convert a transaction into an index and an item
53
+ *
54
+ * @param json transaction with metadata
55
+ * @returns a tuple of index and item to be added to SHAMap
56
+ */
57
+ function transactionItemizer(json) {
58
+ assert.ok(json.hash);
59
+ const index = hash_256_1.Hash256.from(json.hash);
60
+ const item = {
61
+ hashPrefix() {
62
+ return hash_prefixes_1.HashPrefix.transaction;
63
+ },
64
+ toBytesSink(sink) {
65
+ const serializer = new binary_1.BinarySerializer(sink);
66
+ serializer.writeLengthEncoded(st_object_1.STObject.from(json));
67
+ serializer.writeLengthEncoded(st_object_1.STObject.from(json.metaData));
68
+ },
69
+ };
70
+ return [index, item, undefined];
71
+ }
72
+ /**
73
+ * Convert an entry to a pair Hash256 and ShaMapNode
74
+ *
75
+ * @param json JSON describing a ledger entry item
76
+ * @returns a tuple of index and item to be added to SHAMap
77
+ */
78
+ function entryItemizer(json) {
79
+ const index = hash_256_1.Hash256.from(json.index);
80
+ const bytes = (0, binary_1.serializeObject)(json);
81
+ const item = {
82
+ hashPrefix() {
83
+ return hash_prefixes_1.HashPrefix.accountStateEntry;
84
+ },
85
+ toBytesSink(sink) {
86
+ sink.put(bytes);
87
+ },
88
+ };
89
+ return [index, item, undefined];
90
+ }
91
+ /**
92
+ * Function computing the hash of a transaction tree
93
+ *
94
+ * @param param An array of transaction objects to hash
95
+ * @returns A Hash256 object
96
+ */
97
+ function transactionTreeHash(param) {
98
+ const itemizer = transactionItemizer;
99
+ return computeHash(itemizer, param);
100
+ }
101
+ exports.transactionTreeHash = transactionTreeHash;
102
+ /**
103
+ * Function computing the hash of accountState
104
+ *
105
+ * @param param A list of accountStates hash
106
+ * @returns A Hash256 object
107
+ */
108
+ function accountStateHash(param) {
109
+ const itemizer = entryItemizer;
110
+ return computeHash(itemizer, param);
111
+ }
112
+ exports.accountStateHash = accountStateHash;
113
+ /**
114
+ * Serialize and hash a ledger header
115
+ *
116
+ * @param header a ledger header
117
+ * @returns the hash of header
118
+ */
119
+ function ledgerHash(header) {
120
+ const hash = new hashes_1.Sha512Half();
121
+ hash.put(hash_prefixes_1.HashPrefix.ledgerHeader);
122
+ assert.ok(header.parent_close_time !== undefined);
123
+ assert.ok(header.close_flags !== undefined);
124
+ uint_32_1.UInt32.from(header.ledger_index).toBytesSink(hash);
125
+ uint_64_1.UInt64.from(bigInt(String(header.total_coins))).toBytesSink(hash);
126
+ hash_256_1.Hash256.from(header.parent_hash).toBytesSink(hash);
127
+ hash_256_1.Hash256.from(header.transaction_hash).toBytesSink(hash);
128
+ hash_256_1.Hash256.from(header.account_hash).toBytesSink(hash);
129
+ uint_32_1.UInt32.from(header.parent_close_time).toBytesSink(hash);
130
+ uint_32_1.UInt32.from(header.close_time).toBytesSink(hash);
131
+ uint_8_1.UInt8.from(header.close_time_resolution).toBytesSink(hash);
132
+ uint_8_1.UInt8.from(header.close_flags).toBytesSink(hash);
133
+ return hash.finish();
134
+ }
135
+ exports.ledgerHash = ledgerHash;
136
+ /**
137
+ * Decodes a serialized ledger header
138
+ *
139
+ * @param binary A serialized ledger header
140
+ * @param definitions Type definitions to parse the ledger objects.
141
+ * Used if there are non-default ledger objects to decode.
142
+ * @returns A JSON object describing a ledger header
143
+ */
144
+ function decodeLedgerData(binary, definitions) {
145
+ assert.ok(typeof binary === 'string', 'binary must be a hex string');
146
+ const parser = new binary_parser_1.BinaryParser(binary, definitions);
147
+ return {
148
+ ledger_index: parser.readUInt32(),
149
+ total_coins: parser.readType(uint_64_1.UInt64).valueOf().toString(),
150
+ parent_hash: parser.readType(hash_256_1.Hash256).toHex(),
151
+ transaction_hash: parser.readType(hash_256_1.Hash256).toHex(),
152
+ account_hash: parser.readType(hash_256_1.Hash256).toHex(),
153
+ parent_close_time: parser.readUInt32(),
154
+ close_time: parser.readUInt32(),
155
+ close_time_resolution: parser.readUInt8(),
156
+ close_flags: parser.readUInt8(),
157
+ };
158
+ }
159
+ exports.decodeLedgerData = decodeLedgerData;
160
+ //# sourceMappingURL=ledger-hashes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ledger-hashes.js","sourceRoot":"","sources":["../src/ledger-hashes.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAgC;AAChC,qCAAyD;AACzD,mDAA4C;AAC5C,qCAAqC;AACrC,qCAA4D;AAC5D,+CAA0C;AAC1C,iDAA4C;AAC5C,6CAAwC;AACxC,6CAAwC;AACxC,2CAAsC;AAC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MAAM,CAAC,SAAS,EAAE;QACzC,WAAW,EAAE,MAAM,CAAC,SAAS,EAAE;KAChC,CAAA;AACH,CAAC;AAE2D,4CAAgB"}
@@ -0,0 +1,22 @@
1
+ import { Decimal } from 'decimal.js';
2
+ import { Buffer } from 'buffer/';
3
+ /**
4
+ * class for encoding and decoding quality
5
+ */
6
+ declare class quality {
7
+ /**
8
+ * Encode quality amount
9
+ *
10
+ * @param arg string representation of an amount
11
+ * @returns Serialized quality
12
+ */
13
+ static encode(quality: string): Buffer;
14
+ /**
15
+ * Decode quality amount
16
+ *
17
+ * @param arg hex-string denoting serialized quality
18
+ * @returns deserialized quality
19
+ */
20
+ static decode(quality: string): Decimal;
21
+ }
22
+ export { quality };
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.quality = void 0;
4
+ const types_1 = require("./types");
5
+ const decimal_js_1 = require("decimal.js");
6
+ const bigInt = require("big-integer");
7
+ const buffer_1 = require("buffer/");
8
+ /**
9
+ * class for encoding and decoding quality
10
+ */
11
+ class quality {
12
+ /**
13
+ * Encode quality amount
14
+ *
15
+ * @param arg string representation of an amount
16
+ * @returns Serialized quality
17
+ */
18
+ static encode(quality) {
19
+ const decimal = new decimal_js_1.Decimal(quality);
20
+ const exponent = decimal.e - 15;
21
+ const qualityString = decimal.times(`1e${-exponent}`).abs().toString();
22
+ const bytes = types_1.coreTypes.UInt64.from(bigInt(qualityString)).toBytes();
23
+ bytes[0] = exponent + 100;
24
+ return bytes;
25
+ }
26
+ /**
27
+ * Decode quality amount
28
+ *
29
+ * @param arg hex-string denoting serialized quality
30
+ * @returns deserialized quality
31
+ */
32
+ static decode(quality) {
33
+ const bytes = buffer_1.Buffer.from(quality, 'hex').slice(-8);
34
+ const exponent = bytes[0] - 100;
35
+ const mantissa = new decimal_js_1.Decimal(`0x${bytes.slice(1).toString('hex')}`);
36
+ return mantissa.times(`1e${exponent}`);
37
+ }
38
+ }
39
+ exports.quality = quality;
40
+ //# sourceMappingURL=quality.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quality.js","sourceRoot":"","sources":["../src/quality.ts"],"names":[],"mappings":";;;AAAA,mCAAmC;AACnC,2CAAoC;AACpC,sCAAsC;AACtC,oCAAgC;AAEhC;;GAEG;AACH,MAAM,OAAO;IACX;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,OAAe;QAC3B,MAAM,OAAO,GAAG,IAAI,oBAAO,CAAC,OAAO,CAAC,CAAA;QACpC,MAAM,QAAQ,GAAG,OAAO,CAAC,CAAC,GAAG,EAAE,CAAA;QAC/B,MAAM,aAAa,GAAG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAA;QACtE,MAAM,KAAK,GAAG,iBAAS,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,OAAO,EAAE,CAAA;QACpE,KAAK,CAAC,CAAC,CAAC,GAAG,QAAQ,GAAG,GAAG,CAAA;QACzB,OAAO,KAAK,CAAA;IACd,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,OAAe;QAC3B,MAAM,KAAK,GAAG,eAAM,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;QACnD,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,GAAG,CAAA;QAC/B,MAAM,QAAQ,GAAG,IAAI,oBAAO,CAAC,KAAK,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;QACnE,OAAO,QAAQ,CAAC,KAAK,CAAC,KAAK,QAAQ,EAAE,CAAC,CAAA;IACxC,CAAC;CACF;AAEQ,0BAAO"}
@@ -0,0 +1,101 @@
1
+ import { XrplDefinitionsBase, FieldInstance } from '../enums';
2
+ import { type SerializedType } from '../types/serialized-type';
3
+ import { Buffer } from 'buffer/';
4
+ /**
5
+ * BinaryParser is used to compute fields and values from a HexString
6
+ */
7
+ declare class BinaryParser {
8
+ private bytes;
9
+ definitions: XrplDefinitionsBase;
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: string, definitions?: XrplDefinitionsBase);
18
+ /**
19
+ * Peek the first byte of the BinaryParser
20
+ *
21
+ * @returns The first byte of the BinaryParser
22
+ */
23
+ peek(): number;
24
+ /**
25
+ * Consume the first n bytes of the BinaryParser
26
+ *
27
+ * @param n the number of bytes to skip
28
+ */
29
+ skip(n: number): void;
30
+ /**
31
+ * read the first n bytes from the BinaryParser
32
+ *
33
+ * @param n The number of bytes to read
34
+ * @return The bytes
35
+ */
36
+ read(n: number): Buffer;
37
+ /**
38
+ * Read an integer of given size
39
+ *
40
+ * @param n The number of bytes to read
41
+ * @return The number represented by those bytes
42
+ */
43
+ readUIntN(n: number): number;
44
+ readUInt8(): number;
45
+ readUInt16(): number;
46
+ readUInt32(): number;
47
+ size(): number;
48
+ end(customEnd?: number): boolean;
49
+ /**
50
+ * Reads variable length encoded bytes
51
+ *
52
+ * @return The variable length bytes
53
+ */
54
+ readVariableLength(): Buffer;
55
+ /**
56
+ * Reads the length of the variable length encoded bytes
57
+ *
58
+ * @return The length of the variable length encoded bytes
59
+ */
60
+ readVariableLengthLength(): number;
61
+ /**
62
+ * Reads the field ordinal from the BinaryParser
63
+ *
64
+ * @return Field ordinal
65
+ */
66
+ readFieldOrdinal(): number;
67
+ /**
68
+ * Read the field from the BinaryParser
69
+ *
70
+ * @return The field represented by the bytes at the head of the BinaryParser
71
+ */
72
+ readField(): FieldInstance;
73
+ /**
74
+ * Read a given type from the BinaryParser
75
+ *
76
+ * @param type The type that you want to read from the BinaryParser
77
+ * @return The instance of that type read from the BinaryParser
78
+ */
79
+ readType(type: typeof SerializedType): SerializedType;
80
+ /**
81
+ * Get the type associated with a given field
82
+ *
83
+ * @param field The field that you wan to get the type of
84
+ * @return The type associated with the given field
85
+ */
86
+ typeForField(field: FieldInstance): typeof SerializedType;
87
+ /**
88
+ * Read value of the type specified by field from the BinaryParser
89
+ *
90
+ * @param field The field that you want to get the associated value for
91
+ * @return The value associated with the given field
92
+ */
93
+ readFieldValue(field: FieldInstance): SerializedType;
94
+ /**
95
+ * Get the next field and value from the BinaryParser
96
+ *
97
+ * @return The field and value
98
+ */
99
+ readFieldAndValue(): [FieldInstance, SerializedType];
100
+ }
101
+ export { BinaryParser };