ripple-binary-codec 1.0.4-beta.0 → 1.0.4-mpt-beta

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 (335) hide show
  1. package/README.md +3 -3
  2. package/dist/binary.d.ts +34 -20
  3. package/dist/binary.js +51 -36
  4. package/dist/binary.js.map +1 -1
  5. package/dist/coretypes.d.ts +9 -9
  6. package/dist/coretypes.js +33 -9
  7. package/dist/coretypes.js.map +1 -1
  8. package/dist/enums/bytes.d.ts +25 -0
  9. package/dist/enums/bytes.js +63 -0
  10. package/dist/enums/bytes.js.map +1 -0
  11. package/dist/enums/constants.d.ts +4 -0
  12. package/dist/enums/constants.js +8 -0
  13. package/dist/enums/constants.js.map +1 -0
  14. package/dist/enums/definitions.json +3066 -1780
  15. package/dist/enums/field.d.ts +28 -0
  16. package/dist/enums/field.js +58 -0
  17. package/dist/enums/field.js.map +1 -0
  18. package/dist/enums/index.d.ts +12 -46
  19. package/dist/enums/index.js +22 -110
  20. package/dist/enums/index.js.map +1 -1
  21. package/dist/enums/src/enums/definitions.json +3073 -0
  22. package/dist/enums/utils-renumber.js +20 -20
  23. package/dist/enums/utils-renumber.js.map +1 -1
  24. package/dist/enums/xrpl-definitions-base.d.ts +44 -0
  25. package/dist/enums/xrpl-definitions-base.js +59 -0
  26. package/dist/enums/xrpl-definitions-base.js.map +1 -0
  27. package/dist/enums/xrpl-definitions.d.ts +21 -0
  28. package/dist/enums/xrpl-definitions.js +29 -0
  29. package/dist/enums/xrpl-definitions.js.map +1 -0
  30. package/dist/hash-prefixes.d.ts +1 -2
  31. package/dist/hash-prefixes.js +7 -6
  32. package/dist/hash-prefixes.js.map +1 -1
  33. package/dist/hashes.d.ts +7 -8
  34. package/dist/hashes.js +23 -44
  35. package/dist/hashes.js.map +1 -1
  36. package/dist/index.d.ts +16 -17
  37. package/dist/index.js +65 -40
  38. package/dist/index.js.map +1 -1
  39. package/dist/ledger-hashes.d.ts +7 -5
  40. package/dist/ledger-hashes.js +40 -34
  41. package/dist/ledger-hashes.js.map +1 -1
  42. package/dist/quality.d.ts +3 -4
  43. package/dist/quality.js +20 -20
  44. package/dist/quality.js.map +1 -1
  45. package/dist/serdes/binary-parser.d.ts +8 -6
  46. package/dist/serdes/binary-parser.js +74 -64
  47. package/dist/serdes/binary-parser.js.map +1 -1
  48. package/dist/serdes/binary-serializer.d.ts +10 -11
  49. package/dist/serdes/binary-serializer.js +50 -48
  50. package/dist/serdes/binary-serializer.js.map +1 -1
  51. package/dist/shamap.d.ts +6 -7
  52. package/dist/shamap.js +74 -97
  53. package/dist/shamap.js.map +1 -1
  54. package/dist/src/binary.d.ts +90 -0
  55. package/dist/src/binary.js +128 -0
  56. package/dist/src/binary.js.map +1 -0
  57. package/dist/src/coretypes.d.ts +9 -0
  58. package/dist/src/coretypes.js +48 -0
  59. package/dist/src/coretypes.js.map +1 -0
  60. package/dist/src/enums/bytes.d.ts +25 -0
  61. package/dist/src/enums/bytes.js +63 -0
  62. package/dist/src/enums/bytes.js.map +1 -0
  63. package/dist/src/enums/constants.d.ts +4 -0
  64. package/dist/src/enums/constants.js +8 -0
  65. package/dist/src/enums/constants.js.map +1 -0
  66. package/dist/src/enums/definitions.json +3068 -0
  67. package/dist/src/enums/field.d.ts +28 -0
  68. package/dist/src/enums/field.js +58 -0
  69. package/dist/src/enums/field.js.map +1 -0
  70. package/dist/src/enums/index.d.ts +12 -0
  71. package/dist/src/enums/index.js +31 -0
  72. package/dist/src/enums/index.js.map +1 -0
  73. package/dist/src/enums/utils-renumber.d.ts +101 -0
  74. package/dist/src/enums/utils-renumber.js +127 -0
  75. package/dist/src/enums/utils-renumber.js.map +1 -0
  76. package/dist/src/enums/xrpl-definitions-base.d.ts +44 -0
  77. package/dist/src/enums/xrpl-definitions-base.js +59 -0
  78. package/dist/src/enums/xrpl-definitions-base.js.map +1 -0
  79. package/dist/src/enums/xrpl-definitions.d.ts +21 -0
  80. package/dist/src/enums/xrpl-definitions.js +29 -0
  81. package/dist/src/enums/xrpl-definitions.js.map +1 -0
  82. package/dist/src/hash-prefixes.d.ts +5 -0
  83. package/dist/src/hash-prefixes.js +41 -0
  84. package/dist/src/hash-prefixes.js.map +1 -0
  85. package/dist/src/hashes.d.ts +50 -0
  86. package/dist/src/hashes.js +76 -0
  87. package/dist/src/hashes.js.map +1 -0
  88. package/dist/src/index.d.ts +64 -0
  89. package/dist/src/index.js +123 -0
  90. package/dist/src/index.js.map +1 -0
  91. package/dist/src/ledger-hashes.d.ts +48 -0
  92. package/dist/src/ledger-hashes.js +141 -0
  93. package/dist/src/ledger-hashes.js.map +1 -0
  94. package/dist/src/quality.d.ts +21 -0
  95. package/dist/src/quality.js +42 -0
  96. package/dist/src/quality.js.map +1 -0
  97. package/dist/src/serdes/binary-parser.d.ts +100 -0
  98. package/dist/src/serdes/binary-parser.js +194 -0
  99. package/dist/src/serdes/binary-parser.js.map +1 -0
  100. package/dist/src/serdes/binary-serializer.d.ts +81 -0
  101. package/dist/src/serdes/binary-serializer.js +149 -0
  102. package/dist/src/serdes/binary-serializer.js.map +1 -0
  103. package/dist/src/shamap.d.ts +102 -0
  104. package/dist/src/shamap.js +172 -0
  105. package/dist/src/shamap.js.map +1 -0
  106. package/dist/src/types/account-id.d.ts +35 -0
  107. package/dist/src/types/account-id.js +69 -0
  108. package/dist/src/types/account-id.js.map +1 -0
  109. package/dist/src/types/amount.d.ts +91 -0
  110. package/dist/src/types/amount.js +289 -0
  111. package/dist/src/types/amount.js.map +1 -0
  112. package/dist/src/types/blob.d.ts +24 -0
  113. package/dist/src/types/blob.js +43 -0
  114. package/dist/src/types/blob.js.map +1 -0
  115. package/dist/src/types/currency.d.ts +28 -0
  116. package/dist/src/types/currency.js +127 -0
  117. package/dist/src/types/currency.js.map +1 -0
  118. package/dist/src/types/hash-128.d.ts +16 -0
  119. package/dist/src/types/hash-128.js +32 -0
  120. package/dist/src/types/hash-128.js.map +1 -0
  121. package/dist/src/types/hash-160.d.ts +10 -0
  122. package/dist/src/types/hash-160.js +19 -0
  123. package/dist/src/types/hash-160.js.map +1 -0
  124. package/dist/src/types/hash-192.d.ts +10 -0
  125. package/dist/src/types/hash-192.js +19 -0
  126. package/dist/src/types/hash-192.js.map +1 -0
  127. package/dist/src/types/hash-256.d.ts +10 -0
  128. package/dist/src/types/hash-256.js +16 -0
  129. package/dist/src/types/hash-256.js.map +1 -0
  130. package/dist/src/types/hash.d.ts +40 -0
  131. package/dist/src/types/hash.js +73 -0
  132. package/dist/src/types/hash.js.map +1 -0
  133. package/dist/src/types/index.d.ts +19 -0
  134. package/dist/src/types/index.js +64 -0
  135. package/dist/src/types/index.js.map +1 -0
  136. package/dist/src/types/issue.d.ts +38 -0
  137. package/dist/src/types/issue.js +81 -0
  138. package/dist/src/types/issue.js.map +1 -0
  139. package/dist/src/types/path-set.d.ts +36 -0
  140. package/dist/src/types/path-set.js +233 -0
  141. package/dist/src/types/path-set.js.map +1 -0
  142. package/dist/src/types/serialized-type.d.ts +70 -0
  143. package/dist/src/types/serialized-type.js +105 -0
  144. package/dist/src/types/serialized-type.js.map +1 -0
  145. package/dist/src/types/st-array.d.ts +31 -0
  146. package/dist/src/types/st-array.js +86 -0
  147. package/dist/src/types/st-array.js.map +1 -0
  148. package/dist/src/types/st-object.d.ts +32 -0
  149. package/dist/src/types/st-object.js +155 -0
  150. package/dist/src/types/st-object.js.map +1 -0
  151. package/dist/src/types/uint-16.d.ts +24 -0
  152. package/dist/src/types/uint-16.js +45 -0
  153. package/dist/src/types/uint-16.js.map +1 -0
  154. package/dist/src/types/uint-32.d.ts +24 -0
  155. package/dist/src/types/uint-32.js +50 -0
  156. package/dist/src/types/uint-32.js.map +1 -0
  157. package/dist/src/types/uint-64.d.ts +37 -0
  158. package/dist/src/types/uint-64.js +86 -0
  159. package/dist/src/types/uint-64.js.map +1 -0
  160. package/dist/src/types/uint-8.d.ts +24 -0
  161. package/dist/src/types/uint-8.js +46 -0
  162. package/dist/src/types/uint-8.js.map +1 -0
  163. package/dist/src/types/uint.d.ts +29 -0
  164. package/dist/src/types/uint.js +47 -0
  165. package/dist/src/types/uint.js.map +1 -0
  166. package/dist/src/types/vector-256.d.ts +30 -0
  167. package/dist/src/types/vector-256.js +73 -0
  168. package/dist/src/types/vector-256.js.map +1 -0
  169. package/dist/src/types/xchain-bridge.d.ts +44 -0
  170. package/dist/src/types/xchain-bridge.js +102 -0
  171. package/dist/src/types/xchain-bridge.js.map +1 -0
  172. package/dist/src/utils.d.ts +47 -0
  173. package/dist/src/utils.js +137 -0
  174. package/dist/src/utils.js.map +1 -0
  175. package/dist/tsconfig.tsbuildinfo +1 -0
  176. package/dist/types/account-id.d.ts +2 -3
  177. package/dist/types/account-id.js +29 -37
  178. package/dist/types/account-id.js.map +1 -1
  179. package/dist/types/amount.d.ts +33 -10
  180. package/dist/types/amount.js +181 -113
  181. package/dist/types/amount.js.map +1 -1
  182. package/dist/types/blob.d.ts +3 -4
  183. package/dist/types/blob.js +16 -27
  184. package/dist/types/blob.js.map +1 -1
  185. package/dist/types/currency.d.ts +5 -13
  186. package/dist/types/currency.js +53 -66
  187. package/dist/types/currency.js.map +1 -1
  188. package/dist/types/hash-128.d.ts +8 -3
  189. package/dist/types/hash-128.js +23 -22
  190. package/dist/types/hash-128.js.map +1 -1
  191. package/dist/types/hash-160.d.ts +2 -3
  192. package/dist/types/hash-160.js +7 -24
  193. package/dist/types/hash-160.js.map +1 -1
  194. package/dist/types/hash-192.d.ts +10 -0
  195. package/dist/types/hash-192.js +19 -0
  196. package/dist/types/hash-192.js.map +1 -0
  197. package/dist/types/hash-256.d.ts +2 -3
  198. package/dist/types/hash-256.js +7 -22
  199. package/dist/types/hash-256.js.map +1 -1
  200. package/dist/types/hash.d.ts +4 -5
  201. package/dist/types/hash.js +25 -39
  202. package/dist/types/hash.js.map +1 -1
  203. package/dist/types/index.d.ts +19 -33
  204. package/dist/types/index.js +44 -24
  205. package/dist/types/index.js.map +1 -1
  206. package/dist/types/issue.d.ts +38 -0
  207. package/dist/types/issue.js +81 -0
  208. package/dist/types/issue.js.map +1 -0
  209. package/dist/types/path-set.d.ts +2 -2
  210. package/dist/types/path-set.js +73 -94
  211. package/dist/types/path-set.js.map +1 -1
  212. package/dist/types/serialized-type.d.ts +27 -20
  213. package/dist/types/serialized-type.js +50 -60
  214. package/dist/types/serialized-type.js.map +1 -1
  215. package/dist/types/st-array.d.ts +7 -4
  216. package/dist/types/st-array.js +36 -47
  217. package/dist/types/st-array.js.map +1 -1
  218. package/dist/types/st-object.d.ts +8 -5
  219. package/dist/types/st-object.js +73 -74
  220. package/dist/types/st-object.js.map +1 -1
  221. package/dist/types/uint-16.d.ts +3 -4
  222. package/dist/types/uint-16.js +20 -33
  223. package/dist/types/uint-16.js.map +1 -1
  224. package/dist/types/uint-32.d.ts +3 -4
  225. package/dist/types/uint-32.js +23 -36
  226. package/dist/types/uint-32.js.map +1 -1
  227. package/dist/types/uint-64.d.ts +6 -8
  228. package/dist/types/uint-64.js +43 -57
  229. package/dist/types/uint-64.js.map +1 -1
  230. package/dist/types/uint-8.d.ts +3 -4
  231. package/dist/types/uint-8.js +21 -33
  232. package/dist/types/uint-8.js.map +1 -1
  233. package/dist/types/uint.d.ts +6 -7
  234. package/dist/types/uint.js +16 -26
  235. package/dist/types/uint.js.map +1 -1
  236. package/dist/types/vector-256.d.ts +3 -4
  237. package/dist/types/vector-256.js +28 -45
  238. package/dist/types/vector-256.js.map +1 -1
  239. package/dist/types/xchain-bridge.d.ts +44 -0
  240. package/dist/types/xchain-bridge.js +102 -0
  241. package/dist/types/xchain-bridge.js.map +1 -0
  242. package/dist/utils.d.ts +47 -0
  243. package/dist/utils.js +137 -0
  244. package/dist/utils.js.map +1 -0
  245. package/package.json +23 -34
  246. package/src/README.md +5 -0
  247. package/src/binary.ts +194 -0
  248. package/src/coretypes.ts +31 -0
  249. package/src/enums/README.md +144 -0
  250. package/src/enums/bytes.ts +74 -0
  251. package/src/enums/constants.ts +4 -0
  252. package/src/enums/definitions.json +3073 -0
  253. package/src/enums/field.ts +84 -0
  254. package/src/enums/index.ts +34 -0
  255. package/src/enums/utils-renumber.ts +134 -0
  256. package/src/enums/xrpl-definitions-base.ts +111 -0
  257. package/src/enums/xrpl-definitions.ts +32 -0
  258. package/src/hash-prefixes.ts +40 -0
  259. package/src/hashes.ts +75 -0
  260. package/src/index.ts +153 -0
  261. package/src/ledger-hashes.ts +191 -0
  262. package/src/quality.ts +38 -0
  263. package/src/serdes/binary-parser.ts +228 -0
  264. package/src/serdes/binary-serializer.ts +166 -0
  265. package/src/shamap.ts +186 -0
  266. package/src/types/account-id.ts +86 -0
  267. package/src/types/amount.ts +362 -0
  268. package/src/types/blob.ts +46 -0
  269. package/src/types/currency.ts +140 -0
  270. package/src/types/hash-128.ts +33 -0
  271. package/src/types/hash-160.ts +19 -0
  272. package/src/types/hash-192.ts +19 -0
  273. package/src/types/hash-256.ts +15 -0
  274. package/src/types/hash.ts +83 -0
  275. package/src/types/index.ts +66 -0
  276. package/src/types/issue.ts +96 -0
  277. package/src/types/path-set.ts +290 -0
  278. package/src/types/serialized-type.ts +122 -0
  279. package/src/types/st-array.ts +113 -0
  280. package/src/types/st-object.ts +192 -0
  281. package/src/types/uint-16.ts +53 -0
  282. package/src/types/uint-32.ts +59 -0
  283. package/src/types/uint-64.ts +104 -0
  284. package/src/types/uint-8.ts +52 -0
  285. package/src/types/uint.ts +60 -0
  286. package/src/types/vector-256.ts +79 -0
  287. package/src/types/xchain-bridge.ts +128 -0
  288. package/src/utils.ts +152 -0
  289. package/HISTORY.md +0 -74
  290. package/test/amount.test.js +0 -43
  291. package/test/binary-json.test.js +0 -47
  292. package/test/binary-parser.test.js +0 -397
  293. package/test/binary-serializer.test.js +0 -263
  294. package/test/fixtures/account-tx-transactions.db +0 -0
  295. package/test/fixtures/codec-fixtures.json +0 -4466
  296. package/test/fixtures/data-driven-tests.json +0 -3721
  297. package/test/fixtures/delivermin-tx-binary.json +0 -1
  298. package/test/fixtures/delivermin-tx.json +0 -98
  299. package/test/fixtures/deposit-preauth-tx-binary.json +0 -1
  300. package/test/fixtures/deposit-preauth-tx-meta-binary.json +0 -1
  301. package/test/fixtures/deposit-preauth-tx.json +0 -58
  302. package/test/fixtures/escrow-cancel-binary.json +0 -1
  303. package/test/fixtures/escrow-cancel-tx.json +0 -6
  304. package/test/fixtures/escrow-create-binary.json +0 -1
  305. package/test/fixtures/escrow-create-tx.json +0 -10
  306. package/test/fixtures/escrow-finish-binary.json +0 -1
  307. package/test/fixtures/escrow-finish-meta-binary.json +0 -1
  308. package/test/fixtures/escrow-finish-tx.json +0 -95
  309. package/test/fixtures/ledger-full-38129.json +0 -1
  310. package/test/fixtures/ledger-full-40000.json +0 -1
  311. package/test/fixtures/negative-unl.json +0 -12
  312. package/test/fixtures/payment-channel-claim-binary.json +0 -1
  313. package/test/fixtures/payment-channel-claim-tx.json +0 -8
  314. package/test/fixtures/payment-channel-create-binary.json +0 -1
  315. package/test/fixtures/payment-channel-create-tx.json +0 -11
  316. package/test/fixtures/payment-channel-fund-binary.json +0 -1
  317. package/test/fixtures/payment-channel-fund-tx.json +0 -7
  318. package/test/fixtures/signerlistset-tx-binary.json +0 -1
  319. package/test/fixtures/signerlistset-tx-meta-binary.json +0 -1
  320. package/test/fixtures/signerlistset-tx.json +0 -94
  321. package/test/fixtures/ticket-create-binary.json +0 -1
  322. package/test/fixtures/ticket-create-tx.json +0 -7
  323. package/test/fixtures/x-codec-fixtures.json +0 -188
  324. package/test/hash.test.js +0 -82
  325. package/test/ledger.test.js +0 -29
  326. package/test/lower-case-hex.test.js +0 -45
  327. package/test/pseudo-transaction.test.js +0 -38
  328. package/test/quality.test.js +0 -15
  329. package/test/shamap.test.js +0 -88
  330. package/test/signing-data-encoding.test.js +0 -129
  331. package/test/tx-encode-decode.test.js +0 -97
  332. package/test/types.test.js +0 -34
  333. package/test/uint.test.js +0 -40
  334. package/test/utils.js +0 -29
  335. package/test/x-address.test.js +0 -147
@@ -0,0 +1,191 @@
1
+ import { ShaMap, ShaMapNode, ShaMapLeaf } from './shamap'
2
+ import { HashPrefix } from './hash-prefixes'
3
+ import { Sha512Half } from './hashes'
4
+ import { BinarySerializer, serializeObject } from './binary'
5
+ import { Hash256 } from './types/hash-256'
6
+ import { STObject } from './types/st-object'
7
+ import { UInt64 } from './types/uint-64'
8
+ import { UInt32 } from './types/uint-32'
9
+ import { UInt8 } from './types/uint-8'
10
+ import { BinaryParser } from './serdes/binary-parser'
11
+ import { JsonObject } from './types/serialized-type'
12
+ import { XrplDefinitionsBase } from './enums'
13
+
14
+ /**
15
+ * Computes the hash of a list of objects
16
+ *
17
+ * @param itemizer Converts an item into a format that can be added to SHAMap
18
+ * @param itemsJson Array of items to add to a SHAMap
19
+ * @returns the hash of the SHAMap
20
+ */
21
+ function computeHash(
22
+ itemizer: (item: JsonObject) => [Hash256?, ShaMapNode?, ShaMapLeaf?],
23
+ itemsJson: Array<JsonObject>,
24
+ ): Hash256 {
25
+ const map = new ShaMap()
26
+ itemsJson.forEach((item) => map.addItem(...itemizer(item)))
27
+ return map.hash()
28
+ }
29
+
30
+ /**
31
+ * Interface describing a transaction item
32
+ */
33
+ interface transactionItemObject extends JsonObject {
34
+ hash: string
35
+ metaData: JsonObject
36
+ }
37
+
38
+ /**
39
+ * Convert a transaction into an index and an item
40
+ *
41
+ * @param json transaction with metadata
42
+ * @returns a tuple of index and item to be added to SHAMap
43
+ */
44
+ function transactionItemizer(
45
+ json: transactionItemObject,
46
+ ): [Hash256, ShaMapNode, undefined] {
47
+ if (!json.hash) {
48
+ throw new Error()
49
+ }
50
+ const index = Hash256.from(json.hash)
51
+ const item = {
52
+ hashPrefix() {
53
+ return HashPrefix.transaction
54
+ },
55
+ toBytesSink(sink) {
56
+ const serializer = new BinarySerializer(sink)
57
+ serializer.writeLengthEncoded(STObject.from(json))
58
+ serializer.writeLengthEncoded(STObject.from(json.metaData))
59
+ },
60
+ } as ShaMapNode
61
+ return [index, item, undefined]
62
+ }
63
+
64
+ /**
65
+ * Interface describing an entry item
66
+ */
67
+ interface entryItemObject extends JsonObject {
68
+ index: string
69
+ }
70
+
71
+ /**
72
+ * Convert an entry to a pair Hash256 and ShaMapNode
73
+ *
74
+ * @param json JSON describing a ledger entry item
75
+ * @returns a tuple of index and item to be added to SHAMap
76
+ */
77
+ function entryItemizer(
78
+ json: entryItemObject,
79
+ ): [Hash256, ShaMapNode, undefined] {
80
+ const index = Hash256.from(json.index)
81
+ const bytes = serializeObject(json)
82
+ const item = {
83
+ hashPrefix() {
84
+ return HashPrefix.accountStateEntry
85
+ },
86
+ toBytesSink(sink) {
87
+ sink.put(bytes)
88
+ },
89
+ } as ShaMapNode
90
+ return [index, item, undefined]
91
+ }
92
+
93
+ /**
94
+ * Function computing the hash of a transaction tree
95
+ *
96
+ * @param param An array of transaction objects to hash
97
+ * @returns A Hash256 object
98
+ */
99
+ function transactionTreeHash(param: Array<JsonObject>): Hash256 {
100
+ const itemizer = transactionItemizer as (
101
+ json: JsonObject,
102
+ ) => [Hash256, ShaMapNode, undefined]
103
+ return computeHash(itemizer, param)
104
+ }
105
+
106
+ /**
107
+ * Function computing the hash of accountState
108
+ *
109
+ * @param param A list of accountStates hash
110
+ * @returns A Hash256 object
111
+ */
112
+ function accountStateHash(param: Array<JsonObject>): Hash256 {
113
+ const itemizer = entryItemizer as (
114
+ json: JsonObject,
115
+ ) => [Hash256, ShaMapNode, undefined]
116
+ return computeHash(itemizer, param)
117
+ }
118
+
119
+ /**
120
+ * Interface describing a ledger header
121
+ */
122
+ interface ledgerObject {
123
+ ledger_index: number
124
+ total_coins: string | number | bigint
125
+ parent_hash: string
126
+ transaction_hash: string
127
+ account_hash: string
128
+ parent_close_time: number
129
+ close_time: number
130
+ close_time_resolution: number
131
+ close_flags: number
132
+ }
133
+
134
+ /**
135
+ * Serialize and hash a ledger header
136
+ *
137
+ * @param header a ledger header
138
+ * @returns the hash of header
139
+ */
140
+ function ledgerHash(header: ledgerObject): Hash256 {
141
+ const hash = new Sha512Half()
142
+ hash.put(HashPrefix.ledgerHeader)
143
+ if (
144
+ header.parent_close_time === undefined ||
145
+ header.close_flags === undefined
146
+ ) {
147
+ throw new Error()
148
+ }
149
+
150
+ UInt32.from<number>(header.ledger_index).toBytesSink(hash)
151
+ UInt64.from<bigint>(BigInt(String(header.total_coins))).toBytesSink(hash)
152
+ Hash256.from<string>(header.parent_hash).toBytesSink(hash)
153
+ Hash256.from<string>(header.transaction_hash).toBytesSink(hash)
154
+ Hash256.from<string>(header.account_hash).toBytesSink(hash)
155
+ UInt32.from<number>(header.parent_close_time).toBytesSink(hash)
156
+ UInt32.from<number>(header.close_time).toBytesSink(hash)
157
+ UInt8.from<number>(header.close_time_resolution).toBytesSink(hash)
158
+ UInt8.from<number>(header.close_flags).toBytesSink(hash)
159
+ return hash.finish()
160
+ }
161
+
162
+ /**
163
+ * Decodes a serialized ledger header
164
+ *
165
+ * @param binary A serialized ledger header
166
+ * @param definitions Type definitions to parse the ledger objects.
167
+ * Used if there are non-default ledger objects to decode.
168
+ * @returns A JSON object describing a ledger header
169
+ */
170
+ function decodeLedgerData(
171
+ binary: string,
172
+ definitions?: XrplDefinitionsBase,
173
+ ): object {
174
+ if (typeof binary !== 'string') {
175
+ throw new Error('binary must be a hex string')
176
+ }
177
+ const parser = new BinaryParser(binary, definitions)
178
+ return {
179
+ ledger_index: parser.readUInt32(),
180
+ total_coins: parser.readType(UInt64).valueOf().toString(),
181
+ parent_hash: parser.readType(Hash256).toHex(),
182
+ transaction_hash: parser.readType(Hash256).toHex(),
183
+ account_hash: parser.readType(Hash256).toHex(),
184
+ parent_close_time: parser.readUInt32(),
185
+ close_time: parser.readUInt32(),
186
+ close_time_resolution: parser.readUInt8(),
187
+ close_flags: parser.readUInt8(),
188
+ }
189
+ }
190
+
191
+ export { accountStateHash, transactionTreeHash, ledgerHash, decodeLedgerData }
package/src/quality.ts ADDED
@@ -0,0 +1,38 @@
1
+ import { coreTypes } from './types'
2
+ import BigNumber from 'bignumber.js'
3
+ import { bytesToHex, hexToBytes } from '@xrplf/isomorphic/utils'
4
+
5
+ /**
6
+ * class for encoding and decoding quality
7
+ */
8
+ class quality {
9
+ /**
10
+ * Encode quality amount
11
+ *
12
+ * @param arg string representation of an amount
13
+ * @returns Serialized quality
14
+ */
15
+ static encode(quality: string): Uint8Array {
16
+ const decimal = BigNumber(quality)
17
+ const exponent = (decimal?.e || 0) - 15
18
+ const qualityString = decimal.times(`1e${-exponent}`).abs().toString()
19
+ const bytes = coreTypes.UInt64.from(BigInt(qualityString)).toBytes()
20
+ bytes[0] = exponent + 100
21
+ return bytes
22
+ }
23
+
24
+ /**
25
+ * Decode quality amount
26
+ *
27
+ * @param arg hex-string denoting serialized quality
28
+ * @returns deserialized quality
29
+ */
30
+ static decode(quality: string): BigNumber {
31
+ const bytes = hexToBytes(quality).slice(-8)
32
+ const exponent = bytes[0] - 100
33
+ const mantissa = new BigNumber(`0x${bytesToHex(bytes.slice(1))}`)
34
+ return mantissa.times(`1e${exponent}`)
35
+ }
36
+ }
37
+
38
+ export { quality }
@@ -0,0 +1,228 @@
1
+ import {
2
+ XrplDefinitionsBase,
3
+ DEFAULT_DEFINITIONS,
4
+ FieldInstance,
5
+ } from '../enums'
6
+ import { type SerializedType } from '../types/serialized-type'
7
+ import { hexToBytes } from '@xrplf/isomorphic/utils'
8
+
9
+ /**
10
+ * BinaryParser is used to compute fields and values from a HexString
11
+ */
12
+ class BinaryParser {
13
+ private bytes: Uint8Array
14
+ definitions: XrplDefinitionsBase
15
+
16
+ /**
17
+ * Initialize bytes to a hex string
18
+ *
19
+ * @param hexBytes a hex string
20
+ * @param definitions Rippled definitions used to parse the values of transaction types and such.
21
+ * Can be customized for sidechains and amendments.
22
+ */
23
+ constructor(
24
+ hexBytes: string,
25
+ definitions: XrplDefinitionsBase = DEFAULT_DEFINITIONS,
26
+ ) {
27
+ this.bytes = hexToBytes(hexBytes)
28
+ this.definitions = definitions
29
+ }
30
+
31
+ /**
32
+ * Peek the first byte of the BinaryParser
33
+ *
34
+ * @returns The first byte of the BinaryParser
35
+ */
36
+ peek(): number {
37
+ if (this.bytes.byteLength === 0) {
38
+ throw new Error()
39
+ }
40
+ return this.bytes[0]
41
+ }
42
+
43
+ /**
44
+ * Consume the first n bytes of the BinaryParser
45
+ *
46
+ * @param n the number of bytes to skip
47
+ */
48
+ skip(n: number): void {
49
+ if (n > this.bytes.byteLength) {
50
+ throw new Error()
51
+ }
52
+ this.bytes = this.bytes.slice(n)
53
+ }
54
+
55
+ /**
56
+ * read the first n bytes from the BinaryParser
57
+ *
58
+ * @param n The number of bytes to read
59
+ * @return The bytes
60
+ */
61
+ read(n: number): Uint8Array {
62
+ if (n > this.bytes.byteLength) {
63
+ throw new Error()
64
+ }
65
+
66
+ const slice = this.bytes.slice(0, n)
67
+ this.skip(n)
68
+ return slice
69
+ }
70
+
71
+ /**
72
+ * Read an integer of given size
73
+ *
74
+ * @param n The number of bytes to read
75
+ * @return The number represented by those bytes
76
+ */
77
+ readUIntN(n: number): number {
78
+ if (0 >= n || n > 4) {
79
+ throw new Error('invalid n')
80
+ }
81
+ return this.read(n).reduce((a, b) => (a << 8) | b) >>> 0
82
+ }
83
+
84
+ readUInt8(): number {
85
+ return this.readUIntN(1)
86
+ }
87
+
88
+ readUInt16(): number {
89
+ return this.readUIntN(2)
90
+ }
91
+
92
+ readUInt32(): number {
93
+ return this.readUIntN(4)
94
+ }
95
+
96
+ size(): number {
97
+ return this.bytes.byteLength
98
+ }
99
+
100
+ end(customEnd?: number): boolean {
101
+ const length = this.bytes.byteLength
102
+ return length === 0 || (customEnd !== undefined && length <= customEnd)
103
+ }
104
+
105
+ /**
106
+ * Reads variable length encoded bytes
107
+ *
108
+ * @return The variable length bytes
109
+ */
110
+ readVariableLength(): Uint8Array {
111
+ return this.read(this.readVariableLengthLength())
112
+ }
113
+
114
+ /**
115
+ * Reads the length of the variable length encoded bytes
116
+ *
117
+ * @return The length of the variable length encoded bytes
118
+ */
119
+ readVariableLengthLength(): number {
120
+ const b1 = this.readUInt8()
121
+ if (b1 <= 192) {
122
+ return b1
123
+ } else if (b1 <= 240) {
124
+ const b2 = this.readUInt8()
125
+ return 193 + (b1 - 193) * 256 + b2
126
+ } else if (b1 <= 254) {
127
+ const b2 = this.readUInt8()
128
+ const b3 = this.readUInt8()
129
+ return 12481 + (b1 - 241) * 65536 + b2 * 256 + b3
130
+ }
131
+ throw new Error('Invalid variable length indicator')
132
+ }
133
+
134
+ /**
135
+ * Reads the field ordinal from the BinaryParser
136
+ *
137
+ * @return Field ordinal
138
+ */
139
+ readFieldOrdinal(): number {
140
+ let type = this.readUInt8()
141
+ let nth = type & 15
142
+ type >>= 4
143
+
144
+ if (type === 0) {
145
+ type = this.readUInt8()
146
+ if (type === 0 || type < 16) {
147
+ throw new Error(
148
+ `Cannot read FieldOrdinal, type_code ${type} out of range`,
149
+ )
150
+ }
151
+ }
152
+
153
+ if (nth === 0) {
154
+ nth = this.readUInt8()
155
+ if (nth === 0 || nth < 16) {
156
+ throw new Error(
157
+ `Cannot read FieldOrdinal, field_code ${nth} out of range`,
158
+ )
159
+ }
160
+ }
161
+
162
+ return (type << 16) | nth
163
+ }
164
+
165
+ /**
166
+ * Read the field from the BinaryParser
167
+ *
168
+ * @return The field represented by the bytes at the head of the BinaryParser
169
+ */
170
+ readField(): FieldInstance {
171
+ return this.definitions.field.fromString(this.readFieldOrdinal().toString())
172
+ }
173
+
174
+ /**
175
+ * Read a given type from the BinaryParser
176
+ *
177
+ * @param type The type that you want to read from the BinaryParser
178
+ * @return The instance of that type read from the BinaryParser
179
+ */
180
+ readType(type: typeof SerializedType): SerializedType {
181
+ return type.fromParser(this)
182
+ }
183
+
184
+ /**
185
+ * Get the type associated with a given field
186
+ *
187
+ * @param field The field that you wan to get the type of
188
+ * @return The type associated with the given field
189
+ */
190
+ typeForField(field: FieldInstance): typeof SerializedType {
191
+ return field.associatedType
192
+ }
193
+
194
+ /**
195
+ * Read value of the type specified by field from the BinaryParser
196
+ *
197
+ * @param field The field that you want to get the associated value for
198
+ * @return The value associated with the given field
199
+ */
200
+ readFieldValue(field: FieldInstance): SerializedType {
201
+ const type = this.typeForField(field)
202
+ if (!type) {
203
+ throw new Error(`unsupported: (${field.name}, ${field.type.name})`)
204
+ }
205
+ const sizeHint = field.isVariableLengthEncoded
206
+ ? this.readVariableLengthLength()
207
+ : undefined
208
+ const value = type.fromParser(this, sizeHint)
209
+ if (value === undefined) {
210
+ throw new Error(
211
+ `fromParser for (${field.name}, ${field.type.name}) -> undefined `,
212
+ )
213
+ }
214
+ return value
215
+ }
216
+
217
+ /**
218
+ * Get the next field and value from the BinaryParser
219
+ *
220
+ * @return The field and value
221
+ */
222
+ readFieldAndValue(): [FieldInstance, SerializedType] {
223
+ const field = this.readField()
224
+ return [field, this.readFieldValue(field)]
225
+ }
226
+ }
227
+
228
+ export { BinaryParser }
@@ -0,0 +1,166 @@
1
+ import { FieldInstance } from '../enums'
2
+ import { type SerializedType } from '../types/serialized-type'
3
+ import { bytesToHex, concat } from '@xrplf/isomorphic/utils'
4
+
5
+ /**
6
+ * Bytes list is a collection of Uint8Array objects
7
+ */
8
+ class BytesList {
9
+ private bytesArray: Array<Uint8Array> = []
10
+
11
+ /**
12
+ * Get the total number of bytes in the BytesList
13
+ *
14
+ * @return the number of bytes
15
+ */
16
+ public getLength(): number {
17
+ return concat(this.bytesArray).byteLength
18
+ }
19
+
20
+ /**
21
+ * Put bytes in the BytesList
22
+ *
23
+ * @param bytesArg A Uint8Array
24
+ * @return this BytesList
25
+ */
26
+ public put(bytesArg: Uint8Array): BytesList {
27
+ const bytes = Uint8Array.from(bytesArg) // Temporary, to catch instances of Uint8Array being passed in
28
+ this.bytesArray.push(bytes)
29
+ return this
30
+ }
31
+
32
+ /**
33
+ * Write this BytesList to the back of another bytes list
34
+ *
35
+ * @param list The BytesList to write to
36
+ */
37
+ public toBytesSink(list: BytesList): void {
38
+ list.put(this.toBytes())
39
+ }
40
+
41
+ public toBytes(): Uint8Array {
42
+ return concat(this.bytesArray)
43
+ }
44
+
45
+ toHex(): string {
46
+ return bytesToHex(this.toBytes())
47
+ }
48
+ }
49
+
50
+ /**
51
+ * BinarySerializer is used to write fields and values to Uint8Arrays
52
+ */
53
+ class BinarySerializer {
54
+ private sink: BytesList = new BytesList()
55
+
56
+ constructor(sink: BytesList) {
57
+ this.sink = sink
58
+ }
59
+
60
+ /**
61
+ * Write a value to this BinarySerializer
62
+ *
63
+ * @param value a SerializedType value
64
+ */
65
+ write(value: SerializedType): void {
66
+ value.toBytesSink(this.sink)
67
+ }
68
+
69
+ /**
70
+ * Write bytes to this BinarySerializer
71
+ *
72
+ * @param bytes the bytes to write
73
+ */
74
+ put(bytes: Uint8Array): void {
75
+ this.sink.put(bytes)
76
+ }
77
+
78
+ /**
79
+ * Write a value of a given type to this BinarySerializer
80
+ *
81
+ * @param type the type to write
82
+ * @param value a value of that type
83
+ */
84
+ writeType(type: typeof SerializedType, value: SerializedType): void {
85
+ this.write(type.from(value))
86
+ }
87
+
88
+ /**
89
+ * Write BytesList to this BinarySerializer
90
+ *
91
+ * @param bl BytesList to write to BinarySerializer
92
+ */
93
+ writeBytesList(bl: BytesList): void {
94
+ bl.toBytesSink(this.sink)
95
+ }
96
+
97
+ /**
98
+ * Calculate the header of Variable Length encoded bytes
99
+ *
100
+ * @param length the length of the bytes
101
+ */
102
+ private encodeVariableLength(length: number): Uint8Array {
103
+ const lenBytes = new Uint8Array(3)
104
+ if (length <= 192) {
105
+ lenBytes[0] = length
106
+ return lenBytes.slice(0, 1)
107
+ } else if (length <= 12480) {
108
+ length -= 193
109
+ lenBytes[0] = 193 + (length >>> 8)
110
+ lenBytes[1] = length & 0xff
111
+ return lenBytes.slice(0, 2)
112
+ } else if (length <= 918744) {
113
+ length -= 12481
114
+ lenBytes[0] = 241 + (length >>> 16)
115
+ lenBytes[1] = (length >> 8) & 0xff
116
+ lenBytes[2] = length & 0xff
117
+ return lenBytes.slice(0, 3)
118
+ }
119
+ throw new Error('Overflow error')
120
+ }
121
+
122
+ /**
123
+ * Write field and value to BinarySerializer
124
+ *
125
+ * @param field field to write to BinarySerializer
126
+ * @param value value to write to BinarySerializer
127
+ */
128
+ writeFieldAndValue(
129
+ field: FieldInstance,
130
+ value: SerializedType,
131
+ isUnlModifyWorkaround = false,
132
+ ): void {
133
+ const associatedValue = field.associatedType.from(value)
134
+ if (associatedValue.toBytesSink === undefined || field.name === undefined) {
135
+ throw new Error()
136
+ }
137
+
138
+ this.sink.put(field.header)
139
+
140
+ if (field.isVariableLengthEncoded) {
141
+ this.writeLengthEncoded(associatedValue, isUnlModifyWorkaround)
142
+ } else {
143
+ associatedValue.toBytesSink(this.sink)
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Write a variable length encoded value to the BinarySerializer
149
+ *
150
+ * @param value length encoded value to write to BytesList
151
+ */
152
+ public writeLengthEncoded(
153
+ value: SerializedType,
154
+ isUnlModifyWorkaround = false,
155
+ ): void {
156
+ const bytes = new BytesList()
157
+ if (!isUnlModifyWorkaround) {
158
+ // this part doesn't happen for the Account field in a UNLModify transaction
159
+ value.toBytesSink(bytes)
160
+ }
161
+ this.put(this.encodeVariableLength(bytes.getLength()))
162
+ this.writeBytesList(bytes)
163
+ }
164
+ }
165
+
166
+ export { BytesList, BinarySerializer }