@transia/ripple-binary-codec 1.4.6-alpha.8 → 2.5.1-quantum.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 (338) hide show
  1. package/dist/binary.d.ts +30 -16
  2. package/dist/binary.js +36 -16
  3. package/dist/binary.js.map +1 -1
  4. package/dist/enums/bytes.d.ts +1 -2
  5. package/dist/enums/bytes.js +1 -2
  6. package/dist/enums/bytes.js.map +1 -1
  7. package/dist/enums/constants.d.ts +1 -0
  8. package/dist/enums/constants.js +6 -5
  9. package/dist/enums/constants.js.map +1 -1
  10. package/dist/enums/definitions.json +1565 -898
  11. package/dist/enums/field.d.ts +1 -2
  12. package/dist/enums/field.js +1 -2
  13. package/dist/enums/field.js.map +1 -1
  14. package/dist/enums/index.d.ts +1 -2
  15. package/dist/enums/index.js +5 -27
  16. package/dist/enums/index.js.map +1 -1
  17. package/dist/enums/src/enums/definitions.json +1566 -899
  18. package/dist/enums/xrpl-definitions-base.d.ts +5 -3
  19. package/dist/enums/xrpl-definitions-base.js +26 -6
  20. package/dist/enums/xrpl-definitions-base.js.map +1 -1
  21. package/dist/enums/xrpl-definitions.d.ts +2 -1
  22. package/dist/enums/xrpl-definitions.js +2 -1
  23. package/dist/enums/xrpl-definitions.js.map +1 -1
  24. package/dist/hash-prefixes.d.ts +1 -2
  25. package/dist/hash-prefixes.js +8 -6
  26. package/dist/hash-prefixes.js.map +1 -1
  27. package/dist/hashes.d.ts +6 -7
  28. package/dist/hashes.js +6 -7
  29. package/dist/hashes.js.map +1 -1
  30. package/dist/index.d.ts +16 -11
  31. package/dist/index.js +53 -58
  32. package/dist/index.js.map +1 -1
  33. package/dist/ledger-hashes.d.ts +1 -2
  34. package/dist/ledger-hashes.js +11 -30
  35. package/dist/ledger-hashes.js.map +1 -1
  36. package/dist/quality.d.ts +3 -4
  37. package/dist/quality.js +10 -8
  38. package/dist/quality.js.map +1 -1
  39. package/dist/serdes/binary-parser.d.ts +2 -3
  40. package/dist/serdes/binary-parser.js +16 -32
  41. package/dist/serdes/binary-parser.js.map +1 -1
  42. package/dist/serdes/binary-serializer.d.ts +6 -7
  43. package/dist/serdes/binary-serializer.js +12 -35
  44. package/dist/serdes/binary-serializer.js.map +1 -1
  45. package/dist/shamap.d.ts +4 -5
  46. package/dist/shamap.js +5 -5
  47. package/dist/shamap.js.map +1 -1
  48. package/dist/src/binary.d.ts +106 -0
  49. package/dist/src/binary.js +154 -0
  50. package/dist/src/binary.js.map +1 -0
  51. package/dist/src/coretypes.d.ts +9 -0
  52. package/dist/src/coretypes.js +48 -0
  53. package/dist/src/coretypes.js.map +1 -0
  54. package/dist/src/enums/bytes.d.ts +25 -0
  55. package/dist/src/enums/bytes.js +63 -0
  56. package/dist/src/enums/bytes.js.map +1 -0
  57. package/dist/src/enums/constants.d.ts +5 -0
  58. package/dist/src/enums/constants.js +9 -0
  59. package/dist/src/enums/constants.js.map +1 -0
  60. package/dist/src/enums/definitions.json +3385 -0
  61. package/dist/src/enums/field.d.ts +28 -0
  62. package/dist/src/enums/field.js +58 -0
  63. package/dist/src/enums/field.js.map +1 -0
  64. package/dist/src/enums/index.d.ts +12 -0
  65. package/dist/src/enums/index.js +31 -0
  66. package/dist/src/enums/index.js.map +1 -0
  67. package/dist/src/enums/utils-renumber.d.ts +101 -0
  68. package/dist/src/enums/utils-renumber.js +127 -0
  69. package/dist/src/enums/utils-renumber.js.map +1 -0
  70. package/dist/src/enums/xrpl-definitions-base.d.ts +47 -0
  71. package/dist/src/enums/xrpl-definitions-base.js +83 -0
  72. package/dist/src/enums/xrpl-definitions-base.js.map +1 -0
  73. package/dist/src/enums/xrpl-definitions.d.ts +22 -0
  74. package/dist/src/enums/xrpl-definitions.js +30 -0
  75. package/dist/src/enums/xrpl-definitions.js.map +1 -0
  76. package/dist/src/hash-prefixes.d.ts +5 -0
  77. package/dist/src/hash-prefixes.js +43 -0
  78. package/dist/src/hash-prefixes.js.map +1 -0
  79. package/dist/src/hashes.d.ts +50 -0
  80. package/dist/src/hashes.js +76 -0
  81. package/dist/src/hashes.js.map +1 -0
  82. package/dist/src/index.d.ts +69 -0
  83. package/dist/src/index.js +134 -0
  84. package/dist/src/index.js.map +1 -0
  85. package/dist/src/ledger-hashes.d.ts +48 -0
  86. package/dist/src/ledger-hashes.js +141 -0
  87. package/dist/src/ledger-hashes.js.map +1 -0
  88. package/dist/src/quality.d.ts +21 -0
  89. package/dist/src/quality.js +42 -0
  90. package/dist/src/quality.js.map +1 -0
  91. package/dist/src/serdes/binary-parser.d.ts +100 -0
  92. package/dist/src/serdes/binary-parser.js +194 -0
  93. package/dist/src/serdes/binary-parser.js.map +1 -0
  94. package/dist/src/serdes/binary-serializer.d.ts +81 -0
  95. package/dist/src/serdes/binary-serializer.js +149 -0
  96. package/dist/src/serdes/binary-serializer.js.map +1 -0
  97. package/dist/src/shamap.d.ts +102 -0
  98. package/dist/src/shamap.js +172 -0
  99. package/dist/src/shamap.js.map +1 -0
  100. package/dist/src/types/account-id.d.ts +35 -0
  101. package/dist/src/types/account-id.js +69 -0
  102. package/dist/src/types/account-id.js.map +1 -0
  103. package/dist/src/types/amount.d.ts +91 -0
  104. package/dist/src/types/amount.js +289 -0
  105. package/dist/src/types/amount.js.map +1 -0
  106. package/dist/src/types/blob.d.ts +24 -0
  107. package/dist/src/types/blob.js +43 -0
  108. package/dist/src/types/blob.js.map +1 -0
  109. package/dist/src/types/currency.d.ts +28 -0
  110. package/dist/src/types/currency.js +127 -0
  111. package/dist/src/types/currency.js.map +1 -0
  112. package/dist/src/types/hash-128.d.ts +16 -0
  113. package/dist/src/types/hash-128.js +32 -0
  114. package/dist/src/types/hash-128.js.map +1 -0
  115. package/dist/src/types/hash-160.d.ts +10 -0
  116. package/dist/src/types/hash-160.js +19 -0
  117. package/dist/src/types/hash-160.js.map +1 -0
  118. package/dist/src/types/hash-192.d.ts +10 -0
  119. package/dist/src/types/hash-192.js +19 -0
  120. package/dist/src/types/hash-192.js.map +1 -0
  121. package/dist/src/types/hash-256.d.ts +10 -0
  122. package/dist/src/types/hash-256.js +16 -0
  123. package/dist/src/types/hash-256.js.map +1 -0
  124. package/dist/src/types/hash.d.ts +40 -0
  125. package/dist/src/types/hash.js +76 -0
  126. package/dist/src/types/hash.js.map +1 -0
  127. package/dist/src/types/index.d.ts +19 -0
  128. package/dist/src/types/index.js +66 -0
  129. package/dist/src/types/index.js.map +1 -0
  130. package/dist/src/types/issue.d.ts +48 -0
  131. package/dist/src/types/issue.js +104 -0
  132. package/dist/src/types/issue.js.map +1 -0
  133. package/dist/src/types/path-set.d.ts +36 -0
  134. package/dist/src/types/path-set.js +233 -0
  135. package/dist/src/types/path-set.js.map +1 -0
  136. package/dist/src/types/serialized-type.d.ts +70 -0
  137. package/dist/src/types/serialized-type.js +105 -0
  138. package/dist/src/types/serialized-type.js.map +1 -0
  139. package/dist/src/types/st-array.d.ts +31 -0
  140. package/dist/src/types/st-array.js +86 -0
  141. package/dist/src/types/st-array.js.map +1 -0
  142. package/dist/src/types/st-number.d.ts +54 -0
  143. package/dist/src/types/st-number.js +208 -0
  144. package/dist/src/types/st-number.js.map +1 -0
  145. package/dist/src/types/st-object.d.ts +32 -0
  146. package/dist/src/types/st-object.js +173 -0
  147. package/dist/src/types/st-object.js.map +1 -0
  148. package/dist/src/types/uint-16.d.ts +24 -0
  149. package/dist/src/types/uint-16.js +45 -0
  150. package/dist/src/types/uint-16.js.map +1 -0
  151. package/dist/src/types/uint-32.d.ts +24 -0
  152. package/dist/src/types/uint-32.js +50 -0
  153. package/dist/src/types/uint-32.js.map +1 -0
  154. package/dist/src/types/uint-64.d.ts +38 -0
  155. package/dist/src/types/uint-64.js +107 -0
  156. package/dist/src/types/uint-64.js.map +1 -0
  157. package/dist/src/types/uint-8.d.ts +24 -0
  158. package/dist/src/types/uint-8.js +46 -0
  159. package/dist/src/types/uint-8.js.map +1 -0
  160. package/dist/src/types/uint.d.ts +29 -0
  161. package/dist/src/types/uint.js +47 -0
  162. package/dist/src/types/uint.js.map +1 -0
  163. package/dist/src/types/vector-256.d.ts +30 -0
  164. package/dist/src/types/vector-256.js +73 -0
  165. package/dist/src/types/vector-256.js.map +1 -0
  166. package/dist/src/types/xchain-bridge.d.ts +44 -0
  167. package/dist/src/types/xchain-bridge.js +102 -0
  168. package/dist/src/types/xchain-bridge.js.map +1 -0
  169. package/dist/src/utils.d.ts +79 -0
  170. package/dist/src/utils.js +181 -0
  171. package/dist/src/utils.js.map +1 -0
  172. package/dist/tsconfig.tsbuildinfo +1 -0
  173. package/dist/types/account-id.d.ts +1 -2
  174. package/dist/types/account-id.js +4 -6
  175. package/dist/types/account-id.js.map +1 -1
  176. package/dist/types/amount.d.ts +31 -8
  177. package/dist/types/amount.js +125 -43
  178. package/dist/types/amount.js.map +1 -1
  179. package/dist/types/blob.d.ts +1 -2
  180. package/dist/types/blob.js +5 -2
  181. package/dist/types/blob.js.map +1 -1
  182. package/dist/types/currency.d.ts +1 -2
  183. package/dist/types/currency.js +13 -13
  184. package/dist/types/currency.js.map +1 -1
  185. package/dist/types/hash-128.d.ts +1 -2
  186. package/dist/types/hash-128.js +3 -3
  187. package/dist/types/hash-128.js.map +1 -1
  188. package/dist/types/hash-160.d.ts +1 -2
  189. package/dist/types/hash-160.js +1 -2
  190. package/dist/types/hash-160.js.map +1 -1
  191. package/dist/types/hash-192.d.ts +10 -0
  192. package/dist/types/hash-192.js +19 -0
  193. package/dist/types/hash-192.js.map +1 -0
  194. package/dist/types/hash-256.d.ts +1 -2
  195. package/dist/types/hash-256.js +1 -2
  196. package/dist/types/hash-256.js.map +1 -1
  197. package/dist/types/hash.d.ts +2 -3
  198. package/dist/types/hash.js +8 -4
  199. package/dist/types/hash.js.map +1 -1
  200. package/dist/types/index.d.ts +2 -1
  201. package/dist/types/index.js +11 -2
  202. package/dist/types/index.js.map +1 -1
  203. package/dist/types/issue.d.ts +48 -0
  204. package/dist/types/issue.js +104 -0
  205. package/dist/types/issue.js.map +1 -0
  206. package/dist/types/path-set.js +12 -12
  207. package/dist/types/path-set.js.map +1 -1
  208. package/dist/types/serialized-type.d.ts +23 -16
  209. package/dist/types/serialized-type.js +15 -7
  210. package/dist/types/serialized-type.js.map +1 -1
  211. package/dist/types/st-array.d.ts +5 -2
  212. package/dist/types/st-array.js +17 -11
  213. package/dist/types/st-array.js.map +1 -1
  214. package/dist/types/st-number.d.ts +54 -0
  215. package/dist/types/st-number.js +208 -0
  216. package/dist/types/st-number.js.map +1 -0
  217. package/dist/types/st-object.js +32 -8
  218. package/dist/types/st-object.js.map +1 -1
  219. package/dist/types/uint-16.d.ts +1 -2
  220. package/dist/types/uint-16.js +6 -5
  221. package/dist/types/uint-16.js.map +1 -1
  222. package/dist/types/uint-32.d.ts +1 -2
  223. package/dist/types/uint-32.js +7 -6
  224. package/dist/types/uint-32.js.map +1 -1
  225. package/dist/types/uint-64.d.ts +6 -7
  226. package/dist/types/uint-64.js +44 -24
  227. package/dist/types/uint-64.js.map +1 -1
  228. package/dist/types/uint-8.d.ts +1 -2
  229. package/dist/types/uint-8.js +7 -5
  230. package/dist/types/uint-8.js.map +1 -1
  231. package/dist/types/uint.d.ts +5 -6
  232. package/dist/types/uint.js +5 -0
  233. package/dist/types/uint.js.map +1 -1
  234. package/dist/types/vector-256.d.ts +1 -2
  235. package/dist/types/vector-256.js +2 -4
  236. package/dist/types/vector-256.js.map +1 -1
  237. package/dist/types/xchain-bridge.d.ts +44 -0
  238. package/dist/types/xchain-bridge.js +102 -0
  239. package/dist/types/xchain-bridge.js.map +1 -0
  240. package/dist/utils.d.ts +79 -0
  241. package/dist/utils.js +181 -0
  242. package/dist/utils.js.map +1 -0
  243. package/package.json +21 -17
  244. package/src/README.md +5 -0
  245. package/src/binary.ts +235 -0
  246. package/src/coretypes.ts +31 -0
  247. package/src/enums/README.md +144 -0
  248. package/src/enums/bytes.ts +74 -0
  249. package/src/enums/constants.ts +5 -0
  250. package/src/enums/definitions.json +3390 -0
  251. package/src/enums/field.ts +84 -0
  252. package/src/enums/index.ts +34 -0
  253. package/src/enums/utils-renumber.ts +134 -0
  254. package/src/enums/xrpl-definitions-base.ts +150 -0
  255. package/src/enums/xrpl-definitions.ts +33 -0
  256. package/src/hash-prefixes.ts +42 -0
  257. package/src/hashes.ts +75 -0
  258. package/src/index.ts +166 -0
  259. package/src/ledger-hashes.ts +191 -0
  260. package/src/quality.ts +38 -0
  261. package/src/serdes/binary-parser.ts +228 -0
  262. package/src/serdes/binary-serializer.ts +166 -0
  263. package/src/shamap.ts +186 -0
  264. package/src/types/account-id.ts +86 -0
  265. package/src/types/amount.ts +362 -0
  266. package/src/types/blob.ts +46 -0
  267. package/src/types/currency.ts +140 -0
  268. package/src/types/hash-128.ts +33 -0
  269. package/src/types/hash-160.ts +19 -0
  270. package/src/types/hash-192.ts +19 -0
  271. package/src/types/hash-256.ts +15 -0
  272. package/src/types/hash.ts +86 -0
  273. package/src/types/index.ts +68 -0
  274. package/src/types/issue.ts +136 -0
  275. package/src/types/path-set.ts +290 -0
  276. package/src/types/serialized-type.ts +122 -0
  277. package/src/types/st-array.ts +113 -0
  278. package/src/types/st-number.ts +233 -0
  279. package/src/types/st-object.ts +215 -0
  280. package/src/types/uint-16.ts +53 -0
  281. package/src/types/uint-32.ts +59 -0
  282. package/src/types/uint-64.ts +135 -0
  283. package/src/types/uint-8.ts +52 -0
  284. package/src/types/uint.ts +60 -0
  285. package/src/types/vector-256.ts +79 -0
  286. package/src/types/xchain-bridge.ts +128 -0
  287. package/src/utils.ts +220 -0
  288. package/test/amount.test.js +0 -43
  289. package/test/binary-json.test.js +0 -45
  290. package/test/binary-parser.test.js +0 -396
  291. package/test/binary-serializer.test.js +0 -307
  292. package/test/definitions.test.js +0 -100
  293. package/test/fixtures/account-tx-transactions.db +0 -0
  294. package/test/fixtures/codec-fixtures.json +0 -4466
  295. package/test/fixtures/data-driven-tests.json +0 -2919
  296. package/test/fixtures/delivermin-tx-binary.json +0 -1
  297. package/test/fixtures/delivermin-tx.json +0 -98
  298. package/test/fixtures/deposit-preauth-tx-binary.json +0 -1
  299. package/test/fixtures/deposit-preauth-tx-meta-binary.json +0 -1
  300. package/test/fixtures/deposit-preauth-tx.json +0 -58
  301. package/test/fixtures/escrow-cancel-binary.json +0 -1
  302. package/test/fixtures/escrow-cancel-tx.json +0 -6
  303. package/test/fixtures/escrow-create-binary.json +0 -1
  304. package/test/fixtures/escrow-create-tx.json +0 -10
  305. package/test/fixtures/escrow-finish-binary.json +0 -1
  306. package/test/fixtures/escrow-finish-meta-binary.json +0 -1
  307. package/test/fixtures/escrow-finish-tx.json +0 -95
  308. package/test/fixtures/ledger-full-38129.json +0 -1
  309. package/test/fixtures/ledger-full-40000.json +0 -1
  310. package/test/fixtures/negative-unl.json +0 -12
  311. package/test/fixtures/nf-token.json +0 -547
  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/unl-report-binary.json +0 -1
  324. package/test/fixtures/unl-report-meta-binary.json +0 -1
  325. package/test/fixtures/unl-report.json +0 -89
  326. package/test/fixtures/x-codec-fixtures.json +0 -188
  327. package/test/hash.test.js +0 -135
  328. package/test/ledger.test.js +0 -29
  329. package/test/lower-case-hex.test.js +0 -46
  330. package/test/pseudo-transaction.test.js +0 -38
  331. package/test/quality.test.js +0 -15
  332. package/test/shamap.test.js +0 -89
  333. package/test/signing-data-encoding.test.js +0 -213
  334. package/test/tx-encode-decode.test.js +0 -119
  335. package/test/types.test.js +0 -34
  336. package/test/uint.test.js +0 -148
  337. package/test/utils.js +0 -30
  338. package/test/x-address.test.js +0 -181
package/src/index.ts ADDED
@@ -0,0 +1,166 @@
1
+ import { quality, binary, HashPrefix } from './coretypes'
2
+ import { decodeLedgerData } from './ledger-hashes'
3
+ import { ClaimObject, BatchObject } from './binary'
4
+ import { JsonObject } from './types/serialized-type'
5
+ import {
6
+ XrplDefinitionsBase,
7
+ TRANSACTION_TYPES,
8
+ DEFAULT_DEFINITIONS,
9
+ } from './enums'
10
+ import { XrplDefinitions } from './enums/xrpl-definitions'
11
+ import { coreTypes } from './types'
12
+ import { bytesToHex } from '@transia/isomorphic/utils'
13
+
14
+ const {
15
+ signingData,
16
+ signingClaimData,
17
+ multiSigningData,
18
+ signingBatchData,
19
+ binaryToJSON,
20
+ serializeObject,
21
+ } = binary
22
+
23
+ /**
24
+ * Decode a transaction
25
+ *
26
+ * @param binary hex-string of the encoded transaction
27
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
28
+ * @returns the JSON representation of the transaction
29
+ */
30
+ function decode(binary: string, definitions?: XrplDefinitionsBase): JsonObject {
31
+ if (typeof binary !== 'string') {
32
+ throw new Error('binary must be a hex string')
33
+ }
34
+ return binaryToJSON(binary, definitions)
35
+ }
36
+
37
+ /**
38
+ * Encode a transaction
39
+ *
40
+ * @param json The JSON representation of a transaction
41
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
42
+ *
43
+ * @returns A hex-string of the encoded transaction
44
+ */
45
+ function encode(json: object, definitions?: XrplDefinitionsBase): string {
46
+ if (typeof json !== 'object') {
47
+ throw new Error()
48
+ }
49
+ return bytesToHex(serializeObject(json as JsonObject, { definitions }))
50
+ }
51
+
52
+ /**
53
+ * Encode a transaction and prepare for signing
54
+ *
55
+ * @param json JSON object representing the transaction
56
+ * @param signer string representing the account to sign the transaction with
57
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
58
+ * @returns a hex string of the encoded transaction
59
+ */
60
+ function encodeForSigning(
61
+ json: object,
62
+ definitions?: XrplDefinitionsBase,
63
+ ): string {
64
+ if (typeof json !== 'object') {
65
+ throw new Error()
66
+ }
67
+ return bytesToHex(
68
+ signingData(json as JsonObject, HashPrefix.transactionSig, {
69
+ definitions,
70
+ }),
71
+ )
72
+ }
73
+
74
+ /**
75
+ * Encode a payment channel claim for signing.
76
+ *
77
+ * @param json JSON object representing the claim.
78
+ * @returns a hex string of the encoded claim.
79
+ */
80
+ function encodeForSigningClaim(json: object): string {
81
+ if (typeof json !== 'object') {
82
+ throw new Error()
83
+ }
84
+ return bytesToHex(signingClaimData(json as ClaimObject))
85
+ }
86
+
87
+ /**
88
+ * Encode a transaction and prepare for multi-signing.
89
+ *
90
+ * @param json JSON object representing the transaction.
91
+ * @param signer string representing the account to sign the transaction with.
92
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
93
+ * @returns a hex string of the encoded transaction.
94
+ */
95
+ function encodeForMultisigning(
96
+ json: object,
97
+ signer: string,
98
+ definitions?: XrplDefinitionsBase,
99
+ ): string {
100
+ if (typeof json !== 'object') {
101
+ throw new Error()
102
+ }
103
+ if (json['SigningPubKey'] !== '') {
104
+ throw new Error()
105
+ }
106
+ const definitionsOpt = definitions ? { definitions } : undefined
107
+ return bytesToHex(
108
+ multiSigningData(json as JsonObject, signer, definitionsOpt),
109
+ )
110
+ }
111
+
112
+ /**
113
+ * Encode a Batch transaction for signing.
114
+ *
115
+ * @param json JSON object representing the transaction.
116
+ * @returns a hex string of the encoded transaction.
117
+ */
118
+ function encodeForSigningBatch(json: object): string {
119
+ if (typeof json !== 'object') {
120
+ throw new Error('Need an object to encode a Batch transaction')
121
+ }
122
+ return bytesToHex(signingBatchData(json as BatchObject))
123
+ }
124
+
125
+ /**
126
+ * Encode a quality value
127
+ *
128
+ * @param value string representation of a number
129
+ * @returns a hex-string representing the quality
130
+ */
131
+ function encodeQuality(value: string): string {
132
+ if (typeof value !== 'string') {
133
+ throw new Error()
134
+ }
135
+ return bytesToHex(quality.encode(value))
136
+ }
137
+
138
+ /**
139
+ * Decode a quality value
140
+ *
141
+ * @param value hex-string of a quality
142
+ * @returns a string representing the quality
143
+ */
144
+ function decodeQuality(value: string): string {
145
+ if (typeof value !== 'string') {
146
+ throw new Error()
147
+ }
148
+ return quality.decode(value).toString()
149
+ }
150
+
151
+ export {
152
+ decode,
153
+ encode,
154
+ encodeForSigning,
155
+ encodeForSigningClaim,
156
+ encodeForMultisigning,
157
+ encodeForSigningBatch,
158
+ encodeQuality,
159
+ decodeQuality,
160
+ decodeLedgerData,
161
+ TRANSACTION_TYPES,
162
+ XrplDefinitions,
163
+ XrplDefinitionsBase,
164
+ DEFAULT_DEFINITIONS,
165
+ coreTypes,
166
+ }
@@ -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 '@transia/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 '@transia/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 }