@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,86 @@
1
+ import { Comparable } from './serialized-type'
2
+ import { BinaryParser } from '../serdes/binary-parser'
3
+ import { HEX_REGEX, hexToBytes } from '@transia/isomorphic/utils'
4
+ import { compare } from '../utils'
5
+
6
+ /**
7
+ * Base class defining how to encode and decode hashes
8
+ */
9
+ class Hash extends Comparable<Hash | string> {
10
+ static readonly width: number
11
+
12
+ constructor(bytes: Uint8Array) {
13
+ super(bytes)
14
+ if (this.bytes.length !== (this.constructor as typeof Hash).width) {
15
+ throw new Error(`Invalid Hash length ${this.bytes.byteLength}`)
16
+ }
17
+ }
18
+
19
+ /**
20
+ * Construct a Hash object from an existing Hash object or a hex-string
21
+ *
22
+ * @param value A hash object or hex-string of a hash
23
+ */
24
+ static from<T extends Hash | string>(value: T): Hash {
25
+ if (value instanceof this) {
26
+ return value
27
+ }
28
+
29
+ if (typeof value === 'string') {
30
+ if (!HEX_REGEX.test(value)) {
31
+ throw new Error(`Invalid hash string ${value}`)
32
+ }
33
+ return new this(hexToBytes(value))
34
+ }
35
+
36
+ throw new Error('Cannot construct Hash from given value')
37
+ }
38
+
39
+ /**
40
+ * Read a Hash object from a BinaryParser
41
+ *
42
+ * @param parser BinaryParser to read the hash from
43
+ * @param hint length of the bytes to read, optional
44
+ */
45
+ static fromParser(parser: BinaryParser, hint?: number): Hash {
46
+ return new this(parser.read(hint ?? this.width))
47
+ }
48
+
49
+ /**
50
+ * Overloaded operator for comparing two hash objects
51
+ *
52
+ * @param other The Hash to compare this to
53
+ */
54
+ compareTo(other: Hash): number {
55
+ return compare(
56
+ this.bytes,
57
+ (this.constructor as typeof Hash).from(other).bytes,
58
+ )
59
+ }
60
+
61
+ /**
62
+ * @returns the hex-string representation of this Hash
63
+ */
64
+ toString(): string {
65
+ return this.toHex()
66
+ }
67
+
68
+ /**
69
+ * Returns four bits at the specified depth within a hash
70
+ *
71
+ * @param depth The depth of the four bits
72
+ * @returns The number represented by the four bits
73
+ */
74
+ nibblet(depth: number): number {
75
+ const byteIx = depth > 0 ? (depth / 2) | 0 : 0
76
+ let b = this.bytes[byteIx]
77
+ if (depth % 2 === 0) {
78
+ b = (b & 0xf0) >>> 4
79
+ } else {
80
+ b = b & 0x0f
81
+ }
82
+ return b
83
+ }
84
+ }
85
+
86
+ export { Hash }
@@ -0,0 +1,68 @@
1
+ import { AccountID } from './account-id'
2
+ import { Amount } from './amount'
3
+ import { Blob } from './blob'
4
+ import { Currency } from './currency'
5
+ import { Hash128 } from './hash-128'
6
+ import { Hash160 } from './hash-160'
7
+ import { Hash192 } from './hash-192'
8
+ import { Hash256 } from './hash-256'
9
+ import { Issue } from './issue'
10
+ import { STNumber } from './st-number'
11
+ import { PathSet } from './path-set'
12
+ import { STArray } from './st-array'
13
+ import { STObject } from './st-object'
14
+ import { UInt16 } from './uint-16'
15
+ import { UInt32 } from './uint-32'
16
+ import { UInt64 } from './uint-64'
17
+ import { UInt8 } from './uint-8'
18
+ import { Vector256 } from './vector-256'
19
+ import { XChainBridge } from './xchain-bridge'
20
+ import { type SerializedType } from './serialized-type'
21
+ import { DEFAULT_DEFINITIONS } from '../enums'
22
+
23
+ const coreTypes: Record<string, typeof SerializedType> = {
24
+ AccountID,
25
+ Amount,
26
+ Blob,
27
+ Currency,
28
+ Hash128,
29
+ Hash160,
30
+ Hash192,
31
+ Hash256,
32
+ Issue,
33
+ Number: STNumber,
34
+ PathSet,
35
+ STArray,
36
+ STObject,
37
+ UInt8,
38
+ UInt16,
39
+ UInt32,
40
+ UInt64,
41
+ Vector256,
42
+ XChainBridge,
43
+ }
44
+
45
+ // Ensures that the DEFAULT_DEFINITIONS object connects these types to fields for serializing/deserializing
46
+ // This is done here instead of in enums/index.ts to avoid a circular dependency
47
+ // because some of the above types depend on BinarySerializer which depends on enums/index.ts.
48
+ DEFAULT_DEFINITIONS.associateTypes(coreTypes)
49
+
50
+ export {
51
+ coreTypes,
52
+ AccountID,
53
+ Amount,
54
+ Blob,
55
+ Currency,
56
+ Hash128,
57
+ Hash160,
58
+ Hash192,
59
+ Hash256,
60
+ PathSet,
61
+ STArray,
62
+ STObject,
63
+ UInt8,
64
+ UInt16,
65
+ UInt32,
66
+ UInt64,
67
+ Vector256,
68
+ }
@@ -0,0 +1,136 @@
1
+ import { concat } from '@transia/isomorphic/utils'
2
+ import { BinaryParser } from '../serdes/binary-parser'
3
+
4
+ import { AccountID } from './account-id'
5
+ import { Currency } from './currency'
6
+ import { JsonObject, SerializedType } from './serialized-type'
7
+ import { Hash192 } from './hash-192'
8
+
9
+ interface XRPIssue extends JsonObject {
10
+ currency: string
11
+ }
12
+
13
+ interface IOUIssue extends JsonObject {
14
+ currency: string
15
+ issuer: string
16
+ }
17
+ interface MPTIssue extends JsonObject {
18
+ mpt_issuance_id: string
19
+ }
20
+ /**
21
+ * Interface for JSON objects that represent issues
22
+ */
23
+ type IssueObject = XRPIssue | IOUIssue | MPTIssue
24
+
25
+ /**
26
+ * Type guard for Issue Object
27
+ */
28
+ function isIssueObject(arg): arg is IssueObject {
29
+ const keys = Object.keys(arg).sort()
30
+ const isXRP = keys.length === 1 && keys[0] === 'currency'
31
+ const isIOU =
32
+ keys.length === 2 && keys[0] === 'currency' && keys[1] === 'issuer'
33
+ const isMPT = keys.length === 1 && keys[0] === 'mpt_issuance_id'
34
+
35
+ return isXRP || isIOU || isMPT
36
+ }
37
+
38
+ /**
39
+ * Class for serializing/Deserializing Amounts
40
+ */
41
+ class Issue extends SerializedType {
42
+ static readonly ZERO_ISSUED_CURRENCY: Issue = new Issue(new Uint8Array(20))
43
+
44
+ constructor(bytes: Uint8Array) {
45
+ super(bytes ?? Issue.ZERO_ISSUED_CURRENCY.bytes)
46
+ }
47
+
48
+ /**
49
+ * Construct an amount from an IOU or string amount
50
+ *
51
+ * @param value An Amount, object representing an IOU, MPTAmount, or a string
52
+ * representing an integer amount
53
+ * @returns An Issue object
54
+ */
55
+ static from<T extends Issue | IssueObject>(value: T): Issue {
56
+ if (value instanceof Issue) {
57
+ return value
58
+ }
59
+
60
+ if (isIssueObject(value)) {
61
+ if (value.currency) {
62
+ const currency = Currency.from(value.currency.toString()).toBytes()
63
+
64
+ //IOU case
65
+ if (value.issuer) {
66
+ const issuer = AccountID.from(value.issuer.toString()).toBytes()
67
+ return new Issue(concat([currency, issuer]))
68
+ }
69
+
70
+ //XRP case
71
+ return new Issue(currency)
72
+ }
73
+
74
+ // MPT case
75
+ if (value.mpt_issuance_id) {
76
+ const mptIssuanceIdBytes = Hash192.from(
77
+ value.mpt_issuance_id.toString(),
78
+ ).toBytes()
79
+ return new Issue(mptIssuanceIdBytes)
80
+ }
81
+ }
82
+
83
+ throw new Error('Invalid type to construct an Amount')
84
+ }
85
+
86
+ /**
87
+ * Read an amount from a BinaryParser
88
+ *
89
+ * @param parser BinaryParser to read the Amount from
90
+ * @param hint The number of bytes to consume from the parser.
91
+ * For an MPT amount, pass 24 (the fixed length for Hash192).
92
+ *
93
+ * @returns An Issue object
94
+ */
95
+ static fromParser(parser: BinaryParser, hint?: number): Issue {
96
+ if (hint === Hash192.width) {
97
+ const mptBytes = parser.read(Hash192.width)
98
+ return new Issue(mptBytes)
99
+ }
100
+ const currency = parser.read(20)
101
+ if (new Currency(currency).toJSON() === 'XRP') {
102
+ return new Issue(currency)
103
+ }
104
+ const currencyAndIssuer = [currency, parser.read(20)]
105
+ return new Issue(concat(currencyAndIssuer))
106
+ }
107
+
108
+ /**
109
+ * Get the JSON representation of this Amount
110
+ *
111
+ * @returns the JSON interpretation of this.bytes
112
+ */
113
+ toJSON(): IssueObject {
114
+ // If the buffer is exactly 24 bytes, treat it as an MPT amount.
115
+ if (this.toBytes().length === Hash192.width) {
116
+ return {
117
+ mpt_issuance_id: this.toHex().toUpperCase(),
118
+ }
119
+ }
120
+
121
+ const parser = new BinaryParser(this.toString())
122
+
123
+ const currency = Currency.fromParser(parser) as Currency
124
+ if (currency.toJSON() === 'XRP') {
125
+ return { currency: currency.toJSON() }
126
+ }
127
+ const issuer = AccountID.fromParser(parser) as AccountID
128
+
129
+ return {
130
+ currency: currency.toJSON(),
131
+ issuer: issuer.toJSON(),
132
+ }
133
+ }
134
+ }
135
+
136
+ export { Issue, IssueObject }
@@ -0,0 +1,290 @@
1
+ import { AccountID } from './account-id'
2
+ import { Currency } from './currency'
3
+ import { BinaryParser } from '../serdes/binary-parser'
4
+ import { SerializedType, JsonObject } from './serialized-type'
5
+ import { bytesToHex, concat } from '@transia/isomorphic/utils'
6
+
7
+ /**
8
+ * Constants for separating Paths in a PathSet
9
+ */
10
+ const PATHSET_END_BYTE = 0x00
11
+ const PATH_SEPARATOR_BYTE = 0xff
12
+
13
+ /**
14
+ * Constant for masking types of a Hop
15
+ */
16
+ const TYPE_ACCOUNT = 0x01
17
+ const TYPE_CURRENCY = 0x10
18
+ const TYPE_ISSUER = 0x20
19
+
20
+ /**
21
+ * The object representation of a Hop, an issuer AccountID, an account AccountID, and a Currency
22
+ */
23
+ interface HopObject extends JsonObject {
24
+ issuer?: string
25
+ account?: string
26
+ currency?: string
27
+ }
28
+
29
+ /**
30
+ * TypeGuard for HopObject
31
+ */
32
+ function isHopObject(arg): arg is HopObject {
33
+ return (
34
+ arg.issuer !== undefined ||
35
+ arg.account !== undefined ||
36
+ arg.currency !== undefined
37
+ )
38
+ }
39
+
40
+ /**
41
+ * TypeGuard for PathSet
42
+ */
43
+ function isPathSet(arg): arg is Array<Array<HopObject>> {
44
+ return (
45
+ (Array.isArray(arg) && arg.length === 0) ||
46
+ (Array.isArray(arg) && Array.isArray(arg[0]) && arg[0].length === 0) ||
47
+ (Array.isArray(arg) && Array.isArray(arg[0]) && isHopObject(arg[0][0]))
48
+ )
49
+ }
50
+
51
+ /**
52
+ * Serialize and Deserialize a Hop
53
+ */
54
+ class Hop extends SerializedType {
55
+ /**
56
+ * Create a Hop from a HopObject
57
+ *
58
+ * @param value Either a hop or HopObject to create a hop with
59
+ * @returns a Hop
60
+ */
61
+ static from(value: Hop | HopObject): Hop {
62
+ if (value instanceof Hop) {
63
+ return value
64
+ }
65
+
66
+ const bytes: Array<Uint8Array> = [Uint8Array.from([0])]
67
+
68
+ if (value.account) {
69
+ bytes.push(AccountID.from(value.account).toBytes())
70
+ bytes[0][0] |= TYPE_ACCOUNT
71
+ }
72
+
73
+ if (value.currency) {
74
+ bytes.push(Currency.from(value.currency).toBytes())
75
+ bytes[0][0] |= TYPE_CURRENCY
76
+ }
77
+
78
+ if (value.issuer) {
79
+ bytes.push(AccountID.from(value.issuer).toBytes())
80
+ bytes[0][0] |= TYPE_ISSUER
81
+ }
82
+
83
+ return new Hop(concat(bytes))
84
+ }
85
+
86
+ /**
87
+ * Construct a Hop from a BinaryParser
88
+ *
89
+ * @param parser BinaryParser to read the Hop from
90
+ * @returns a Hop
91
+ */
92
+ static fromParser(parser: BinaryParser): Hop {
93
+ const type = parser.readUInt8()
94
+ const bytes: Array<Uint8Array> = [Uint8Array.from([type])]
95
+
96
+ if (type & TYPE_ACCOUNT) {
97
+ bytes.push(parser.read(AccountID.width))
98
+ }
99
+
100
+ if (type & TYPE_CURRENCY) {
101
+ bytes.push(parser.read(Currency.width))
102
+ }
103
+
104
+ if (type & TYPE_ISSUER) {
105
+ bytes.push(parser.read(AccountID.width))
106
+ }
107
+
108
+ return new Hop(concat(bytes))
109
+ }
110
+
111
+ /**
112
+ * Get the JSON interpretation of this hop
113
+ *
114
+ * @returns a HopObject, an JS object with optional account, issuer, and currency
115
+ */
116
+ toJSON(): HopObject {
117
+ const hopParser = new BinaryParser(bytesToHex(this.bytes))
118
+ const type = hopParser.readUInt8()
119
+
120
+ let account, currency, issuer
121
+ if (type & TYPE_ACCOUNT) {
122
+ account = (AccountID.fromParser(hopParser) as AccountID).toJSON()
123
+ }
124
+
125
+ if (type & TYPE_CURRENCY) {
126
+ currency = (Currency.fromParser(hopParser) as Currency).toJSON()
127
+ }
128
+
129
+ if (type & TYPE_ISSUER) {
130
+ issuer = (AccountID.fromParser(hopParser) as AccountID).toJSON()
131
+ }
132
+
133
+ const result: HopObject = {}
134
+ if (account) {
135
+ result.account = account
136
+ }
137
+
138
+ if (issuer) {
139
+ result.issuer = issuer
140
+ }
141
+
142
+ if (currency) {
143
+ result.currency = currency
144
+ }
145
+
146
+ return result
147
+ }
148
+
149
+ /**
150
+ * get a number representing the type of this hop
151
+ *
152
+ * @returns a number to be bitwise and-ed with TYPE_ constants to describe the types in the hop
153
+ */
154
+ type(): number {
155
+ return this.bytes[0]
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Class for serializing/deserializing Paths
161
+ */
162
+ class Path extends SerializedType {
163
+ /**
164
+ * construct a Path from an array of Hops
165
+ *
166
+ * @param value Path or array of HopObjects to construct a Path
167
+ * @returns the Path
168
+ */
169
+ static from(value: Path | Array<HopObject>): Path {
170
+ if (value instanceof Path) {
171
+ return value
172
+ }
173
+
174
+ const bytes: Array<Uint8Array> = []
175
+ value.forEach((hop: HopObject) => {
176
+ bytes.push(Hop.from(hop).toBytes())
177
+ })
178
+
179
+ return new Path(concat(bytes))
180
+ }
181
+
182
+ /**
183
+ * Read a Path from a BinaryParser
184
+ *
185
+ * @param parser BinaryParser to read Path from
186
+ * @returns the Path represented by the bytes read from the BinaryParser
187
+ */
188
+ static fromParser(parser: BinaryParser): Path {
189
+ const bytes: Array<Uint8Array> = []
190
+ while (!parser.end()) {
191
+ bytes.push(Hop.fromParser(parser).toBytes())
192
+
193
+ if (
194
+ parser.peek() === PATHSET_END_BYTE ||
195
+ parser.peek() === PATH_SEPARATOR_BYTE
196
+ ) {
197
+ break
198
+ }
199
+ }
200
+ return new Path(concat(bytes))
201
+ }
202
+
203
+ /**
204
+ * Get the JSON representation of this Path
205
+ *
206
+ * @returns an Array of HopObject constructed from this.bytes
207
+ */
208
+ toJSON(): Array<HopObject> {
209
+ const json: Array<HopObject> = []
210
+ const pathParser = new BinaryParser(this.toString())
211
+
212
+ while (!pathParser.end()) {
213
+ json.push(Hop.fromParser(pathParser).toJSON())
214
+ }
215
+
216
+ return json
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Deserialize and Serialize the PathSet type
222
+ */
223
+ class PathSet extends SerializedType {
224
+ /**
225
+ * Construct a PathSet from an Array of Arrays representing paths
226
+ *
227
+ * @param value A PathSet or Array of Array of HopObjects
228
+ * @returns the PathSet constructed from value
229
+ */
230
+ static from<T extends PathSet | Array<Array<HopObject>>>(value: T): PathSet {
231
+ if (value instanceof PathSet) {
232
+ return value
233
+ }
234
+
235
+ if (isPathSet(value)) {
236
+ const bytes: Array<Uint8Array> = []
237
+
238
+ value.forEach((path: Array<HopObject>) => {
239
+ bytes.push(Path.from(path).toBytes())
240
+ bytes.push(Uint8Array.from([PATH_SEPARATOR_BYTE]))
241
+ })
242
+
243
+ bytes[bytes.length - 1] = Uint8Array.from([PATHSET_END_BYTE])
244
+
245
+ return new PathSet(concat(bytes))
246
+ }
247
+
248
+ throw new Error('Cannot construct PathSet from given value')
249
+ }
250
+
251
+ /**
252
+ * Construct a PathSet from a BinaryParser
253
+ *
254
+ * @param parser A BinaryParser to read PathSet from
255
+ * @returns the PathSet read from parser
256
+ */
257
+ static fromParser(parser: BinaryParser): PathSet {
258
+ const bytes: Array<Uint8Array> = []
259
+
260
+ while (!parser.end()) {
261
+ bytes.push(Path.fromParser(parser).toBytes())
262
+ bytes.push(parser.read(1))
263
+
264
+ if (bytes[bytes.length - 1][0] == PATHSET_END_BYTE) {
265
+ break
266
+ }
267
+ }
268
+
269
+ return new PathSet(concat(bytes))
270
+ }
271
+
272
+ /**
273
+ * Get the JSON representation of this PathSet
274
+ *
275
+ * @returns an Array of Array of HopObjects, representing this PathSet
276
+ */
277
+ toJSON(): Array<Array<HopObject>> {
278
+ const json: Array<Array<HopObject>> = []
279
+ const pathParser = new BinaryParser(this.toString())
280
+
281
+ while (!pathParser.end()) {
282
+ json.push(Path.fromParser(pathParser).toJSON())
283
+ pathParser.skip(1)
284
+ }
285
+
286
+ return json
287
+ }
288
+ }
289
+
290
+ export { PathSet }
@@ -0,0 +1,122 @@
1
+ import { BytesList } from '../serdes/binary-serializer'
2
+ import { BinaryParser } from '../serdes/binary-parser'
3
+ import { XrplDefinitionsBase } from '../enums'
4
+ import { bytesToHex } from '@transia/isomorphic/utils'
5
+
6
+ type JSON = string | number | boolean | null | undefined | JSON[] | JsonObject
7
+
8
+ type JsonObject = { [key: string]: JSON }
9
+
10
+ /**
11
+ * The base class for all binary-codec types
12
+ */
13
+ class SerializedType {
14
+ protected readonly bytes: Uint8Array = new Uint8Array(0)
15
+
16
+ constructor(bytes?: Uint8Array) {
17
+ this.bytes = bytes ?? new Uint8Array(0)
18
+ }
19
+
20
+ static fromParser(parser: BinaryParser, hint?: number): SerializedType {
21
+ throw new Error('fromParser not implemented')
22
+ return this.fromParser(parser, hint)
23
+ }
24
+
25
+ static from(value: SerializedType | JSON | bigint): SerializedType {
26
+ throw new Error('from not implemented')
27
+ return this.from(value)
28
+ }
29
+
30
+ /**
31
+ * Write the bytes representation of a SerializedType to a BytesList
32
+ *
33
+ * @param list The BytesList to write SerializedType bytes to
34
+ */
35
+ toBytesSink(list: BytesList): void {
36
+ list.put(this.bytes)
37
+ }
38
+
39
+ /**
40
+ * Get the hex representation of a SerializedType's bytes
41
+ *
42
+ * @returns hex String of this.bytes
43
+ */
44
+ toHex(): string {
45
+ return bytesToHex(this.toBytes())
46
+ }
47
+
48
+ /**
49
+ * Get the bytes representation of a SerializedType
50
+ *
51
+ * @returns A Uint8Array of the bytes
52
+ */
53
+ toBytes(): Uint8Array {
54
+ if (this.bytes) {
55
+ return this.bytes
56
+ }
57
+ const bytes = new BytesList()
58
+ this.toBytesSink(bytes)
59
+ return bytes.toBytes()
60
+ }
61
+
62
+ /**
63
+ * Return the JSON representation of a SerializedType
64
+ *
65
+ * @param _definitions rippled definitions used to parse the values of transaction types and such.
66
+ * Unused in default, but used in STObject, STArray
67
+ * Can be customized for sidechains and amendments.
68
+ * @returns any type, if not overloaded returns hexString representation of bytes
69
+ */
70
+ toJSON(_definitions?: XrplDefinitionsBase, _fieldName?: string): JSON {
71
+ return this.toHex()
72
+ }
73
+
74
+ /**
75
+ * @returns hexString representation of this.bytes
76
+ */
77
+ toString(): string {
78
+ return this.toHex()
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Base class for SerializedTypes that are comparable.
84
+ *
85
+ * @template T - What types you want to allow comparisons between. You must specify all types. Primarily used to allow
86
+ * comparisons between built-in types (like `string`) and SerializedType subclasses (like `Hash`).
87
+ *
88
+ * Ex. `class Hash extends Comparable<Hash | string>`
89
+ */
90
+ class Comparable<T extends Object> extends SerializedType {
91
+ lt(other: T): boolean {
92
+ return this.compareTo(other) < 0
93
+ }
94
+
95
+ eq(other: T): boolean {
96
+ return this.compareTo(other) === 0
97
+ }
98
+
99
+ gt(other: T): boolean {
100
+ return this.compareTo(other) > 0
101
+ }
102
+
103
+ gte(other: T): boolean {
104
+ return this.compareTo(other) > -1
105
+ }
106
+
107
+ lte(other: T): boolean {
108
+ return this.compareTo(other) < 1
109
+ }
110
+
111
+ /**
112
+ * Overload this method to define how two Comparable SerializedTypes are compared
113
+ *
114
+ * @param other The comparable object to compare this to
115
+ * @returns A number denoting the relationship of this and other
116
+ */
117
+ compareTo(other: T): number {
118
+ throw new Error(`cannot compare ${this.toString()} and ${other.toString()}`)
119
+ }
120
+ }
121
+
122
+ export { SerializedType, Comparable, JSON, JsonObject }