@transia/ripple-binary-codec 1.4.6-alpha.9 → 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 (340) 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 +1535 -919
  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 +1537 -921
  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 -319
  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/remit-binary.json +0 -1
  319. package/test/fixtures/remit-tx.json +0 -39
  320. package/test/fixtures/signerlistset-tx-binary.json +0 -1
  321. package/test/fixtures/signerlistset-tx-meta-binary.json +0 -1
  322. package/test/fixtures/signerlistset-tx.json +0 -94
  323. package/test/fixtures/ticket-create-binary.json +0 -1
  324. package/test/fixtures/ticket-create-tx.json +0 -7
  325. package/test/fixtures/unl-report-binary.json +0 -1
  326. package/test/fixtures/unl-report-meta-binary.json +0 -1
  327. package/test/fixtures/unl-report.json +0 -89
  328. package/test/fixtures/x-codec-fixtures.json +0 -188
  329. package/test/hash.test.js +0 -135
  330. package/test/ledger.test.js +0 -29
  331. package/test/lower-case-hex.test.js +0 -46
  332. package/test/pseudo-transaction.test.js +0 -38
  333. package/test/quality.test.js +0 -15
  334. package/test/shamap.test.js +0 -89
  335. package/test/signing-data-encoding.test.js +0 -213
  336. package/test/tx-encode-decode.test.js +0 -119
  337. package/test/types.test.js +0 -34
  338. package/test/uint.test.js +0 -148
  339. package/test/utils.js +0 -30
  340. package/test/x-address.test.js +0 -181
@@ -0,0 +1,166 @@
1
+ import { FieldInstance } from '../enums'
2
+ import { type SerializedType } from '../types/serialized-type'
3
+ import { bytesToHex, concat } from '@transia/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 }
package/src/shamap.ts ADDED
@@ -0,0 +1,186 @@
1
+ import { coreTypes } from './types'
2
+ import { HashPrefix } from './hash-prefixes'
3
+ import { Sha512Half } from './hashes'
4
+ import { Hash256 } from './types/hash-256'
5
+ import { BytesList } from './serdes/binary-serializer'
6
+
7
+ /**
8
+ * Abstract class describing a SHAMapNode
9
+ */
10
+ abstract class ShaMapNode {
11
+ abstract hashPrefix(): Uint8Array
12
+ abstract isLeaf(): boolean
13
+ abstract isInner(): boolean
14
+ abstract toBytesSink(list: BytesList): void
15
+ abstract hash(): Hash256
16
+ }
17
+
18
+ /**
19
+ * Class describing a Leaf of SHAMap
20
+ */
21
+ class ShaMapLeaf extends ShaMapNode {
22
+ constructor(public index: Hash256, public item?: ShaMapNode) {
23
+ super()
24
+ }
25
+
26
+ /**
27
+ * @returns true as ShaMapLeaf is a leaf node
28
+ */
29
+ isLeaf(): boolean {
30
+ return true
31
+ }
32
+
33
+ /**
34
+ * @returns false as ShaMapLeaf is not an inner node
35
+ */
36
+ isInner(): boolean {
37
+ return false
38
+ }
39
+
40
+ /**
41
+ * Get the prefix of the this.item
42
+ *
43
+ * @returns The hash prefix, unless this.item is undefined, then it returns an empty Uint8Array
44
+ */
45
+ hashPrefix(): Uint8Array {
46
+ return this.item === undefined ? new Uint8Array(0) : this.item.hashPrefix()
47
+ }
48
+
49
+ /**
50
+ * Hash the bytes representation of this
51
+ *
52
+ * @returns hash of this.item concatenated with this.index
53
+ */
54
+ hash(): Hash256 {
55
+ const hash = Sha512Half.put(this.hashPrefix())
56
+ this.toBytesSink(hash)
57
+ return hash.finish()
58
+ }
59
+
60
+ /**
61
+ * Write the bytes representation of this to a BytesList
62
+ * @param list BytesList to write bytes to
63
+ */
64
+ toBytesSink(list: BytesList): void {
65
+ if (this.item !== undefined) {
66
+ this.item.toBytesSink(list)
67
+ }
68
+ this.index.toBytesSink(list)
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Class defining an Inner Node of a SHAMap
74
+ */
75
+ class ShaMapInner extends ShaMapNode {
76
+ private slotBits = 0
77
+ private branches: Array<ShaMapNode> = Array(16)
78
+
79
+ constructor(private depth: number = 0) {
80
+ super()
81
+ }
82
+
83
+ /**
84
+ * @returns true as ShaMapInner is an inner node
85
+ */
86
+ isInner(): boolean {
87
+ return true
88
+ }
89
+
90
+ /**
91
+ * @returns false as ShaMapInner is not a leaf node
92
+ */
93
+ isLeaf(): boolean {
94
+ return false
95
+ }
96
+
97
+ /**
98
+ * Get the hash prefix for this node
99
+ *
100
+ * @returns hash prefix describing an inner node
101
+ */
102
+ hashPrefix(): Uint8Array {
103
+ return HashPrefix.innerNode
104
+ }
105
+
106
+ /**
107
+ * Set a branch of this node to be another node
108
+ *
109
+ * @param slot Slot to add branch to this.branches
110
+ * @param branch Branch to add
111
+ */
112
+ setBranch(slot: number, branch: ShaMapNode): void {
113
+ this.slotBits = this.slotBits | (1 << slot)
114
+ this.branches[slot] = branch
115
+ }
116
+
117
+ /**
118
+ * @returns true if node is empty
119
+ */
120
+ empty(): boolean {
121
+ return this.slotBits === 0
122
+ }
123
+
124
+ /**
125
+ * Compute the hash of this node
126
+ *
127
+ * @returns The hash of this node
128
+ */
129
+ hash(): Hash256 {
130
+ if (this.empty()) {
131
+ return (coreTypes.Hash256 as typeof Hash256).ZERO_256
132
+ }
133
+ const hash = Sha512Half.put(this.hashPrefix())
134
+ this.toBytesSink(hash)
135
+ return hash.finish()
136
+ }
137
+
138
+ /**
139
+ * Writes the bytes representation of this node to a BytesList
140
+ *
141
+ * @param list BytesList to write bytes to
142
+ */
143
+ toBytesSink(list: BytesList): void {
144
+ for (let i = 0; i < this.branches.length; i++) {
145
+ const branch = this.branches[i]
146
+ const hash = branch
147
+ ? branch.hash()
148
+ : (coreTypes.Hash256 as typeof Hash256).ZERO_256
149
+ hash.toBytesSink(list)
150
+ }
151
+ }
152
+
153
+ /**
154
+ * Add item to the SHAMap
155
+ *
156
+ * @param index Hash of the index of the item being inserted
157
+ * @param item Item to insert in the map
158
+ * @param leaf Leaf node to insert when branch doesn't exist
159
+ */
160
+ addItem(index?: Hash256, item?: ShaMapNode, leaf?: ShaMapLeaf): void {
161
+ if (index === undefined) {
162
+ throw new Error()
163
+ }
164
+ if (index !== undefined) {
165
+ const nibble = index.nibblet(this.depth)
166
+ const existing = this.branches[nibble]
167
+
168
+ if (existing === undefined) {
169
+ this.setBranch(nibble, leaf || new ShaMapLeaf(index, item))
170
+ } else if (existing instanceof ShaMapLeaf) {
171
+ const newInner = new ShaMapInner(this.depth + 1)
172
+ newInner.addItem(existing.index, undefined, existing)
173
+ newInner.addItem(index, item, leaf)
174
+ this.setBranch(nibble, newInner)
175
+ } else if (existing instanceof ShaMapInner) {
176
+ existing.addItem(index, item, leaf)
177
+ } else {
178
+ throw new Error('invalid ShaMap.addItem call')
179
+ }
180
+ }
181
+ }
182
+ }
183
+
184
+ class ShaMap extends ShaMapInner {}
185
+
186
+ export { ShaMap, ShaMapNode, ShaMapLeaf }
@@ -0,0 +1,86 @@
1
+ import {
2
+ decodeAccountID,
3
+ encodeAccountID,
4
+ isValidXAddress,
5
+ xAddressToClassicAddress,
6
+ } from '@transia/ripple-address-codec'
7
+ import { Hash160 } from './hash-160'
8
+ import { hexToBytes } from '@transia/isomorphic/utils'
9
+
10
+ const HEX_REGEX = /^[A-F0-9]{40}$/
11
+
12
+ /**
13
+ * Class defining how to encode and decode an AccountID
14
+ */
15
+ class AccountID extends Hash160 {
16
+ static readonly defaultAccountID: AccountID = new AccountID(
17
+ new Uint8Array(20),
18
+ )
19
+
20
+ constructor(bytes?: Uint8Array) {
21
+ super(bytes ?? AccountID.defaultAccountID.bytes)
22
+ }
23
+
24
+ /**
25
+ * Defines how to construct an AccountID
26
+ *
27
+ * @param value either an existing AccountID, a hex-string, or a base58 r-Address
28
+ * @returns an AccountID object
29
+ */
30
+ static from<T extends Hash160 | string>(value: T): AccountID {
31
+ if (value instanceof AccountID) {
32
+ return value
33
+ }
34
+
35
+ if (typeof value === 'string') {
36
+ if (value === '') {
37
+ return new AccountID()
38
+ }
39
+
40
+ return HEX_REGEX.test(value)
41
+ ? new AccountID(hexToBytes(value))
42
+ : this.fromBase58(value)
43
+ }
44
+
45
+ throw new Error('Cannot construct AccountID from value given')
46
+ }
47
+
48
+ /**
49
+ * Defines how to build an AccountID from a base58 r-Address
50
+ *
51
+ * @param value a base58 r-Address
52
+ * @returns an AccountID object
53
+ */
54
+ static fromBase58(value: string): AccountID {
55
+ if (isValidXAddress(value)) {
56
+ const classic = xAddressToClassicAddress(value)
57
+
58
+ if (classic.tag !== false)
59
+ throw new Error('Only allowed to have tag on Account or Destination')
60
+
61
+ value = classic.classicAddress
62
+ }
63
+
64
+ return new AccountID(Uint8Array.from(decodeAccountID(value)))
65
+ }
66
+
67
+ /**
68
+ * Overload of toJSON
69
+ *
70
+ * @returns the base58 string for this AccountID
71
+ */
72
+ toJSON(): string {
73
+ return this.toBase58()
74
+ }
75
+
76
+ /**
77
+ * Defines how to encode AccountID into a base58 address
78
+ *
79
+ * @returns the base58 string defined by this.bytes
80
+ */
81
+ toBase58(): string {
82
+ return encodeAccountID(this.bytes)
83
+ }
84
+ }
85
+
86
+ export { AccountID }