@transia/ripple-binary-codec 1.4.6-alpha.8 → 2.5.1

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 (361) 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 +3 -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 +1650 -964
  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 +1652 -966
  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 +7 -8
  43. package/dist/serdes/binary-serializer.js +14 -37
  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 +65 -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 +3404 -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 +37 -0
  101. package/dist/src/types/account-id.js +73 -0
  102. package/dist/src/types/account-id.js.map +1 -0
  103. package/dist/src/types/amount.d.ts +92 -0
  104. package/dist/src/types/amount.js +292 -0
  105. package/dist/src/types/amount.js.map +1 -0
  106. package/dist/src/types/blob.d.ts +25 -0
  107. package/dist/src/types/blob.js +46 -0
  108. package/dist/src/types/blob.js.map +1 -0
  109. package/dist/src/types/currency.d.ts +30 -0
  110. package/dist/src/types/currency.js +131 -0
  111. package/dist/src/types/currency.js.map +1 -0
  112. package/dist/src/types/data.d.ts +104 -0
  113. package/dist/src/types/data.js +387 -0
  114. package/dist/src/types/data.js.map +1 -0
  115. package/dist/src/types/dataType.d.ts +94 -0
  116. package/dist/src/types/dataType.js +145 -0
  117. package/dist/src/types/dataType.js.map +1 -0
  118. package/dist/src/types/hash-128.d.ts +18 -0
  119. package/dist/src/types/hash-128.js +36 -0
  120. package/dist/src/types/hash-128.js.map +1 -0
  121. package/dist/src/types/hash-160.d.ts +12 -0
  122. package/dist/src/types/hash-160.js +23 -0
  123. package/dist/src/types/hash-160.js.map +1 -0
  124. package/dist/src/types/hash-192.d.ts +12 -0
  125. package/dist/src/types/hash-192.js +23 -0
  126. package/dist/src/types/hash-192.js.map +1 -0
  127. package/dist/src/types/hash-256.d.ts +12 -0
  128. package/dist/src/types/hash-256.js +20 -0
  129. package/dist/src/types/hash-256.js.map +1 -0
  130. package/dist/src/types/hash.d.ts +40 -0
  131. package/dist/src/types/hash.js +76 -0
  132. package/dist/src/types/hash.js.map +1 -0
  133. package/dist/src/types/index.d.ts +21 -0
  134. package/dist/src/types/index.js +72 -0
  135. package/dist/src/types/index.js.map +1 -0
  136. package/dist/src/types/issue.d.ts +49 -0
  137. package/dist/src/types/issue.js +107 -0
  138. package/dist/src/types/issue.js.map +1 -0
  139. package/dist/src/types/json.d.ts +169 -0
  140. package/dist/src/types/json.js +529 -0
  141. package/dist/src/types/json.js.map +1 -0
  142. package/dist/src/types/path-set.d.ts +37 -0
  143. package/dist/src/types/path-set.js +236 -0
  144. package/dist/src/types/path-set.js.map +1 -0
  145. package/dist/src/types/serialized-type.d.ts +117 -0
  146. package/dist/src/types/serialized-type.js +242 -0
  147. package/dist/src/types/serialized-type.js.map +1 -0
  148. package/dist/src/types/st-array.d.ts +32 -0
  149. package/dist/src/types/st-array.js +89 -0
  150. package/dist/src/types/st-array.js.map +1 -0
  151. package/dist/src/types/st-number.d.ts +55 -0
  152. package/dist/src/types/st-number.js +212 -0
  153. package/dist/src/types/st-number.js.map +1 -0
  154. package/dist/src/types/st-object.d.ts +33 -0
  155. package/dist/src/types/st-object.js +176 -0
  156. package/dist/src/types/st-object.js.map +1 -0
  157. package/dist/src/types/uint-16.d.ts +26 -0
  158. package/dist/src/types/uint-16.js +49 -0
  159. package/dist/src/types/uint-16.js.map +1 -0
  160. package/dist/src/types/uint-32.d.ts +26 -0
  161. package/dist/src/types/uint-32.js +54 -0
  162. package/dist/src/types/uint-32.js.map +1 -0
  163. package/dist/src/types/uint-64.d.ts +40 -0
  164. package/dist/src/types/uint-64.js +112 -0
  165. package/dist/src/types/uint-64.js.map +1 -0
  166. package/dist/src/types/uint-8.d.ts +26 -0
  167. package/dist/src/types/uint-8.js +50 -0
  168. package/dist/src/types/uint-8.js.map +1 -0
  169. package/dist/src/types/uint.d.ts +29 -0
  170. package/dist/src/types/uint.js +47 -0
  171. package/dist/src/types/uint.js.map +1 -0
  172. package/dist/src/types/vector-256.d.ts +31 -0
  173. package/dist/src/types/vector-256.js +76 -0
  174. package/dist/src/types/vector-256.js.map +1 -0
  175. package/dist/src/types/xchain-bridge.d.ts +45 -0
  176. package/dist/src/types/xchain-bridge.js +105 -0
  177. package/dist/src/types/xchain-bridge.js.map +1 -0
  178. package/dist/src/utils.d.ts +79 -0
  179. package/dist/src/utils.js +181 -0
  180. package/dist/src/utils.js.map +1 -0
  181. package/dist/tsconfig.tsbuildinfo +1 -0
  182. package/dist/types/account-id.d.ts +3 -2
  183. package/dist/types/account-id.js +8 -6
  184. package/dist/types/account-id.js.map +1 -1
  185. package/dist/types/amount.d.ts +33 -9
  186. package/dist/types/amount.js +128 -43
  187. package/dist/types/amount.js.map +1 -1
  188. package/dist/types/blob.d.ts +3 -3
  189. package/dist/types/blob.js +8 -2
  190. package/dist/types/blob.js.map +1 -1
  191. package/dist/types/currency.d.ts +3 -2
  192. package/dist/types/currency.js +17 -13
  193. package/dist/types/currency.js.map +1 -1
  194. package/dist/types/data.d.ts +104 -0
  195. package/dist/types/data.js +387 -0
  196. package/dist/types/data.js.map +1 -0
  197. package/dist/types/dataType.d.ts +94 -0
  198. package/dist/types/dataType.js +145 -0
  199. package/dist/types/dataType.js.map +1 -0
  200. package/dist/types/hash-128.d.ts +3 -2
  201. package/dist/types/hash-128.js +7 -3
  202. package/dist/types/hash-128.js.map +1 -1
  203. package/dist/types/hash-160.d.ts +3 -2
  204. package/dist/types/hash-160.js +5 -2
  205. package/dist/types/hash-160.js.map +1 -1
  206. package/dist/types/hash-192.d.ts +12 -0
  207. package/dist/types/hash-192.js +23 -0
  208. package/dist/types/hash-192.js.map +1 -0
  209. package/dist/types/hash-256.d.ts +3 -2
  210. package/dist/types/hash-256.js +5 -2
  211. package/dist/types/hash-256.js.map +1 -1
  212. package/dist/types/hash.d.ts +2 -3
  213. package/dist/types/hash.js +8 -4
  214. package/dist/types/hash.js.map +1 -1
  215. package/dist/types/index.d.ts +4 -1
  216. package/dist/types/index.js +17 -2
  217. package/dist/types/index.js.map +1 -1
  218. package/dist/types/issue.d.ts +49 -0
  219. package/dist/types/issue.js +107 -0
  220. package/dist/types/issue.js.map +1 -0
  221. package/dist/types/json.d.ts +169 -0
  222. package/dist/types/json.js +529 -0
  223. package/dist/types/json.js.map +1 -0
  224. package/dist/types/path-set.d.ts +2 -1
  225. package/dist/types/path-set.js +15 -12
  226. package/dist/types/path-set.js.map +1 -1
  227. package/dist/types/serialized-type.d.ts +70 -16
  228. package/dist/types/serialized-type.js +153 -8
  229. package/dist/types/serialized-type.js.map +1 -1
  230. package/dist/types/st-array.d.ts +7 -3
  231. package/dist/types/st-array.js +20 -11
  232. package/dist/types/st-array.js.map +1 -1
  233. package/dist/types/st-number.d.ts +55 -0
  234. package/dist/types/st-number.js +212 -0
  235. package/dist/types/st-number.js.map +1 -0
  236. package/dist/types/st-object.d.ts +2 -1
  237. package/dist/types/st-object.js +35 -8
  238. package/dist/types/st-object.js.map +1 -1
  239. package/dist/types/uint-16.d.ts +3 -2
  240. package/dist/types/uint-16.js +10 -5
  241. package/dist/types/uint-16.js.map +1 -1
  242. package/dist/types/uint-32.d.ts +3 -2
  243. package/dist/types/uint-32.js +11 -6
  244. package/dist/types/uint-32.js.map +1 -1
  245. package/dist/types/uint-64.d.ts +8 -7
  246. package/dist/types/uint-64.js +49 -24
  247. package/dist/types/uint-64.js.map +1 -1
  248. package/dist/types/uint-8.d.ts +3 -2
  249. package/dist/types/uint-8.js +11 -5
  250. package/dist/types/uint-8.js.map +1 -1
  251. package/dist/types/uint.d.ts +5 -6
  252. package/dist/types/uint.js +5 -0
  253. package/dist/types/uint.js.map +1 -1
  254. package/dist/types/vector-256.d.ts +3 -3
  255. package/dist/types/vector-256.js +5 -4
  256. package/dist/types/vector-256.js.map +1 -1
  257. package/dist/types/xchain-bridge.d.ts +45 -0
  258. package/dist/types/xchain-bridge.js +105 -0
  259. package/dist/types/xchain-bridge.js.map +1 -0
  260. package/dist/utils.d.ts +79 -0
  261. package/dist/utils.js +181 -0
  262. package/dist/utils.js.map +1 -0
  263. package/package.json +21 -17
  264. package/src/README.md +5 -0
  265. package/src/binary.ts +235 -0
  266. package/src/coretypes.ts +31 -0
  267. package/src/enums/README.md +144 -0
  268. package/src/enums/bytes.ts +80 -0
  269. package/src/enums/constants.ts +5 -0
  270. package/src/enums/definitions.json +3409 -0
  271. package/src/enums/field.ts +84 -0
  272. package/src/enums/index.ts +34 -0
  273. package/src/enums/utils-renumber.ts +134 -0
  274. package/src/enums/xrpl-definitions-base.ts +150 -0
  275. package/src/enums/xrpl-definitions.ts +33 -0
  276. package/src/hash-prefixes.ts +42 -0
  277. package/src/hashes.ts +75 -0
  278. package/src/index.ts +166 -0
  279. package/src/ledger-hashes.ts +191 -0
  280. package/src/quality.ts +38 -0
  281. package/src/serdes/binary-parser.ts +228 -0
  282. package/src/serdes/binary-serializer.ts +166 -0
  283. package/src/shamap.ts +189 -0
  284. package/src/types/account-id.ts +91 -0
  285. package/src/types/amount.ts +366 -0
  286. package/src/types/blob.ts +50 -0
  287. package/src/types/currency.ts +145 -0
  288. package/src/types/data.ts +496 -0
  289. package/src/types/dataType.ts +178 -0
  290. package/src/types/hash-128.ts +38 -0
  291. package/src/types/hash-160.ts +24 -0
  292. package/src/types/hash-192.ts +24 -0
  293. package/src/types/hash-256.ts +20 -0
  294. package/src/types/hash.ts +86 -0
  295. package/src/types/index.ts +74 -0
  296. package/src/types/issue.ts +140 -0
  297. package/src/types/json.ts +641 -0
  298. package/src/types/path-set.ts +294 -0
  299. package/src/types/serialized-type.ts +271 -0
  300. package/src/types/st-array.ts +117 -0
  301. package/src/types/st-number.ts +238 -0
  302. package/src/types/st-object.ts +219 -0
  303. package/src/types/uint-16.ts +58 -0
  304. package/src/types/uint-32.ts +64 -0
  305. package/src/types/uint-64.ts +141 -0
  306. package/src/types/uint-8.ts +57 -0
  307. package/src/types/uint.ts +60 -0
  308. package/src/types/vector-256.ts +83 -0
  309. package/src/types/xchain-bridge.ts +132 -0
  310. package/src/utils.ts +220 -0
  311. package/test/amount.test.js +0 -43
  312. package/test/binary-json.test.js +0 -45
  313. package/test/binary-parser.test.js +0 -396
  314. package/test/binary-serializer.test.js +0 -307
  315. package/test/definitions.test.js +0 -100
  316. package/test/fixtures/account-tx-transactions.db +0 -0
  317. package/test/fixtures/codec-fixtures.json +0 -4466
  318. package/test/fixtures/data-driven-tests.json +0 -2919
  319. package/test/fixtures/delivermin-tx-binary.json +0 -1
  320. package/test/fixtures/delivermin-tx.json +0 -98
  321. package/test/fixtures/deposit-preauth-tx-binary.json +0 -1
  322. package/test/fixtures/deposit-preauth-tx-meta-binary.json +0 -1
  323. package/test/fixtures/deposit-preauth-tx.json +0 -58
  324. package/test/fixtures/escrow-cancel-binary.json +0 -1
  325. package/test/fixtures/escrow-cancel-tx.json +0 -6
  326. package/test/fixtures/escrow-create-binary.json +0 -1
  327. package/test/fixtures/escrow-create-tx.json +0 -10
  328. package/test/fixtures/escrow-finish-binary.json +0 -1
  329. package/test/fixtures/escrow-finish-meta-binary.json +0 -1
  330. package/test/fixtures/escrow-finish-tx.json +0 -95
  331. package/test/fixtures/ledger-full-38129.json +0 -1
  332. package/test/fixtures/ledger-full-40000.json +0 -1
  333. package/test/fixtures/negative-unl.json +0 -12
  334. package/test/fixtures/nf-token.json +0 -547
  335. package/test/fixtures/payment-channel-claim-binary.json +0 -1
  336. package/test/fixtures/payment-channel-claim-tx.json +0 -8
  337. package/test/fixtures/payment-channel-create-binary.json +0 -1
  338. package/test/fixtures/payment-channel-create-tx.json +0 -11
  339. package/test/fixtures/payment-channel-fund-binary.json +0 -1
  340. package/test/fixtures/payment-channel-fund-tx.json +0 -7
  341. package/test/fixtures/signerlistset-tx-binary.json +0 -1
  342. package/test/fixtures/signerlistset-tx-meta-binary.json +0 -1
  343. package/test/fixtures/signerlistset-tx.json +0 -94
  344. package/test/fixtures/ticket-create-binary.json +0 -1
  345. package/test/fixtures/ticket-create-tx.json +0 -7
  346. package/test/fixtures/unl-report-binary.json +0 -1
  347. package/test/fixtures/unl-report-meta-binary.json +0 -1
  348. package/test/fixtures/unl-report.json +0 -89
  349. package/test/fixtures/x-codec-fixtures.json +0 -188
  350. package/test/hash.test.js +0 -135
  351. package/test/ledger.test.js +0 -29
  352. package/test/lower-case-hex.test.js +0 -46
  353. package/test/pseudo-transaction.test.js +0 -38
  354. package/test/quality.test.js +0 -15
  355. package/test/shamap.test.js +0 -89
  356. package/test/signing-data-encoding.test.js +0 -213
  357. package/test/tx-encode-decode.test.js +0 -119
  358. package/test/types.test.js +0 -34
  359. package/test/uint.test.js +0 -148
  360. package/test/utils.js +0 -30
  361. package/test/x-address.test.js +0 -181
@@ -0,0 +1,141 @@
1
+ import { UInt } from './uint'
2
+ import { BinaryParser } from '../serdes/binary-parser'
3
+ import { bytesToHex, concat, hexToBytes } from '@transia/isomorphic/utils'
4
+ import { readUInt32BE, writeUInt32BE } from '../utils'
5
+ import { DEFAULT_DEFINITIONS, XrplDefinitionsBase } from '../enums'
6
+ import { SerializedTypeID } from './serialized-type'
7
+
8
+ const HEX_REGEX = /^[a-fA-F0-9]{1,16}$/
9
+ const BASE10_REGEX = /^[0-9]{1,20}$/
10
+ const mask = BigInt(0x00000000ffffffff)
11
+
12
+ const BASE10_AMOUNT_FIELDS = new Set([
13
+ 'MaximumAmount',
14
+ 'OutstandingAmount',
15
+ 'MPTAmount',
16
+ 'LockedAmount',
17
+ ])
18
+
19
+ function isBase10(fieldName: string): boolean {
20
+ return BASE10_AMOUNT_FIELDS.has(fieldName)
21
+ }
22
+
23
+ /**
24
+ * Derived UInt class for serializing/deserializing 64 bit UInt
25
+ */
26
+ class UInt64 extends UInt {
27
+ protected static readonly width: number = 64 / 8 // 8
28
+ static readonly defaultUInt64: UInt64 = new UInt64(
29
+ new Uint8Array(UInt64.width),
30
+ )
31
+
32
+ constructor(bytes: Uint8Array) {
33
+ super(bytes ?? UInt64.defaultUInt64.bytes)
34
+ }
35
+
36
+ static fromParser(parser: BinaryParser): UInt {
37
+ return new UInt64(parser.read(UInt64.width))
38
+ }
39
+
40
+ /**
41
+ * Construct a UInt64 object
42
+ *
43
+ * @param val A UInt64, hex-string, bigInt, or number
44
+ * @returns A UInt64 object
45
+ */
46
+ // eslint-disable-next-line complexity
47
+ static from<T extends UInt64 | string | bigint | number>(
48
+ val: T,
49
+ fieldName = '',
50
+ ): UInt64 {
51
+ if (val instanceof UInt64) {
52
+ return val
53
+ }
54
+
55
+ let buf = new Uint8Array(UInt64.width)
56
+
57
+ if (typeof val === 'number') {
58
+ if (val < 0) {
59
+ throw new Error('value must be an unsigned integer')
60
+ }
61
+
62
+ const number = BigInt(val)
63
+
64
+ const intBuf = [new Uint8Array(4), new Uint8Array(4)]
65
+ writeUInt32BE(intBuf[0], Number(number >> BigInt(32)), 0)
66
+ writeUInt32BE(intBuf[1], Number(number & BigInt(mask)), 0)
67
+
68
+ return new UInt64(concat(intBuf))
69
+ }
70
+
71
+ if (typeof val === 'string') {
72
+ if (isBase10(fieldName)) {
73
+ if (!BASE10_REGEX.test(val)) {
74
+ throw new Error(`${fieldName} ${val} is not a valid base 10 string`)
75
+ }
76
+ val = BigInt(val).toString(16) as T
77
+ }
78
+
79
+ if (typeof val === 'string' && !HEX_REGEX.test(val)) {
80
+ throw new Error(`${val} is not a valid hex-string`)
81
+ }
82
+
83
+ const strBuf = (val as string).padStart(16, '0')
84
+ buf = hexToBytes(strBuf)
85
+ return new UInt64(buf)
86
+ }
87
+
88
+ if (typeof val === 'bigint') {
89
+ const intBuf = [new Uint8Array(4), new Uint8Array(4)]
90
+ writeUInt32BE(intBuf[0], Number(Number(val >> BigInt(32))), 0)
91
+ writeUInt32BE(intBuf[1], Number(val & BigInt(mask)), 0)
92
+
93
+ return new UInt64(concat(intBuf))
94
+ }
95
+
96
+ throw new Error('Cannot construct UInt64 from given value')
97
+ }
98
+
99
+ /**
100
+ * The JSON representation of a UInt64 object
101
+ *
102
+ * @returns a hex-string
103
+ */
104
+ toJSON(
105
+ _definitions: XrplDefinitionsBase = DEFAULT_DEFINITIONS,
106
+ fieldName = '',
107
+ ): string {
108
+ const hexString = bytesToHex(this.bytes)
109
+ if (isBase10(fieldName)) {
110
+ return BigInt('0x' + hexString).toString(10)
111
+ }
112
+
113
+ return hexString
114
+ }
115
+
116
+ /**
117
+ * Get the value of the UInt64
118
+ *
119
+ * @returns the number represented buy this.bytes
120
+ */
121
+ valueOf(): bigint {
122
+ const msb = BigInt(readUInt32BE(this.bytes.slice(0, 4), 0))
123
+ const lsb = BigInt(readUInt32BE(this.bytes.slice(4), 0))
124
+ return (msb << BigInt(32)) | lsb
125
+ }
126
+
127
+ /**
128
+ * Get the bytes representation of the UInt64 object
129
+ *
130
+ * @returns 8 bytes representing the UInt64
131
+ */
132
+ toBytes(): Uint8Array {
133
+ return this.bytes
134
+ }
135
+
136
+ getSType(): SerializedTypeID {
137
+ return SerializedTypeID.STI_UINT64
138
+ }
139
+ }
140
+
141
+ export { UInt64 }
@@ -0,0 +1,57 @@
1
+ import { UInt } from './uint'
2
+ import { BinaryParser } from '../serdes/binary-parser'
3
+ import { bytesToHex } from '@transia/isomorphic/utils'
4
+ import { writeUInt8 } from '../utils'
5
+ import { SerializedTypeID } from './serialized-type'
6
+
7
+ /**
8
+ * Derived UInt class for serializing/deserializing 8 bit UInt
9
+ */
10
+ class UInt8 extends UInt {
11
+ protected static readonly width: number = 8 / 8 // 1
12
+ static readonly defaultUInt8: UInt8 = new UInt8(new Uint8Array(UInt8.width))
13
+
14
+ constructor(bytes: Uint8Array) {
15
+ super(bytes ?? UInt8.defaultUInt8.bytes)
16
+ }
17
+
18
+ static fromParser(parser: BinaryParser): UInt {
19
+ return new UInt8(parser.read(UInt8.width))
20
+ }
21
+
22
+ /**
23
+ * Construct a UInt8 object from a number
24
+ *
25
+ * @param val UInt8 object or number
26
+ */
27
+ static from<T extends UInt8 | number>(val: T): UInt8 {
28
+ if (val instanceof UInt8) {
29
+ return val
30
+ }
31
+
32
+ if (typeof val === 'number') {
33
+ UInt8.checkUintRange(val, 0, 0xff)
34
+
35
+ const buf = new Uint8Array(UInt8.width)
36
+ writeUInt8(buf, val, 0)
37
+ return new UInt8(buf)
38
+ }
39
+
40
+ throw new Error('Cannot construct UInt8 from given value')
41
+ }
42
+
43
+ /**
44
+ * get the value of a UInt8 object
45
+ *
46
+ * @returns the number represented by this.bytes
47
+ */
48
+ valueOf(): number {
49
+ return parseInt(bytesToHex(this.bytes), 16)
50
+ }
51
+
52
+ getSType(): SerializedTypeID {
53
+ return SerializedTypeID.STI_UINT8
54
+ }
55
+ }
56
+
57
+ export { UInt8 }
@@ -0,0 +1,60 @@
1
+ import { Comparable } from './serialized-type'
2
+
3
+ /**
4
+ * Compare numbers and bigInts n1 and n2
5
+ *
6
+ * @param n1 First object to compare
7
+ * @param n2 Second object to compare
8
+ * @returns -1, 0, or 1, depending on how the two objects compare
9
+ */
10
+ function compare(n1: number | bigint, n2: number | bigint): number {
11
+ return n1 < n2 ? -1 : n1 == n2 ? 0 : 1
12
+ }
13
+
14
+ /**
15
+ * Base class for serializing and deserializing unsigned integers.
16
+ */
17
+ abstract class UInt extends Comparable<UInt | number> {
18
+ protected static width: number
19
+
20
+ constructor(bytes: Uint8Array) {
21
+ super(bytes)
22
+ }
23
+
24
+ /**
25
+ * Overload of compareTo for Comparable
26
+ *
27
+ * @param other other UInt to compare this to
28
+ * @returns -1, 0, or 1 depending on how the objects relate to each other
29
+ */
30
+ compareTo(other: UInt | number): number {
31
+ return compare(this.valueOf(), other.valueOf())
32
+ }
33
+
34
+ /**
35
+ * Convert a UInt object to JSON
36
+ *
37
+ * @returns number or string represented by this.bytes
38
+ */
39
+ toJSON(): number | string {
40
+ const val = this.valueOf()
41
+ return typeof val === 'number' ? val : val.toString()
42
+ }
43
+
44
+ /**
45
+ * Get the value of the UInt represented by this.bytes
46
+ *
47
+ * @returns the value
48
+ */
49
+ abstract valueOf(): number | bigint
50
+
51
+ static checkUintRange(val: number, min: number, max: number): void {
52
+ if (val < min || val > max) {
53
+ throw new Error(
54
+ `Invalid ${this.constructor.name}: ${val} must be >= ${min} and <= ${max}`,
55
+ )
56
+ }
57
+ }
58
+ }
59
+
60
+ export { UInt }
@@ -0,0 +1,83 @@
1
+ import { SerializedType, SerializedTypeID } from './serialized-type'
2
+ import { BinaryParser } from '../serdes/binary-parser'
3
+ import { Hash256 } from './hash-256'
4
+ import { BytesList } from '../serdes/binary-serializer'
5
+ import { bytesToHex } from '@transia/isomorphic/utils'
6
+
7
+ /**
8
+ * TypeGuard for Array<string>
9
+ */
10
+ function isStrings(arg): arg is Array<string> {
11
+ return Array.isArray(arg) && (arg.length === 0 || typeof arg[0] === 'string')
12
+ }
13
+
14
+ /**
15
+ * Class for serializing and deserializing vectors of Hash256
16
+ */
17
+ class Vector256 extends SerializedType {
18
+ constructor(bytes: Uint8Array) {
19
+ super(bytes)
20
+ }
21
+
22
+ /**
23
+ * Construct a Vector256 from a BinaryParser
24
+ *
25
+ * @param parser BinaryParser to
26
+ * @param hint length of the vector, in bytes, optional
27
+ * @returns a Vector256 object
28
+ */
29
+ static fromParser(parser: BinaryParser, hint?: number): Vector256 {
30
+ const bytesList = new BytesList()
31
+ const bytes = hint ?? parser.size()
32
+ const hashes = bytes / 32
33
+ for (let i = 0; i < hashes; i++) {
34
+ Hash256.fromParser(parser).toBytesSink(bytesList)
35
+ }
36
+ return new Vector256(bytesList.toBytes())
37
+ }
38
+
39
+ /**
40
+ * Construct a Vector256 object from an array of hashes
41
+ *
42
+ * @param value A Vector256 object or array of hex-strings representing Hash256's
43
+ * @returns a Vector256 object
44
+ */
45
+ static from<T extends Vector256 | Array<string>>(value: T): Vector256 {
46
+ if (value instanceof Vector256) {
47
+ return value
48
+ }
49
+
50
+ if (isStrings(value)) {
51
+ const bytesList = new BytesList()
52
+ value.forEach((hash) => {
53
+ Hash256.from(hash).toBytesSink(bytesList)
54
+ })
55
+ return new Vector256(bytesList.toBytes())
56
+ }
57
+
58
+ throw new Error('Cannot construct Vector256 from given value')
59
+ }
60
+
61
+ /**
62
+ * Return an Array of hex-strings represented by this.bytes
63
+ *
64
+ * @returns An Array of strings representing the Hash256 objects
65
+ */
66
+ toJSON(): Array<string> {
67
+ if (this.bytes.byteLength % 32 !== 0) {
68
+ throw new Error('Invalid bytes for Vector256')
69
+ }
70
+
71
+ const result: Array<string> = []
72
+ for (let i = 0; i < this.bytes.byteLength; i += 32) {
73
+ result.push(bytesToHex(this.bytes.slice(i, i + 32)))
74
+ }
75
+ return result
76
+ }
77
+
78
+ getSType(): SerializedTypeID {
79
+ return SerializedTypeID.STI_VECTOR256
80
+ }
81
+ }
82
+
83
+ export { Vector256 }
@@ -0,0 +1,132 @@
1
+ import { BinaryParser } from '../serdes/binary-parser'
2
+
3
+ import { AccountID } from './account-id'
4
+ import { JsonObject, SerializedType, SerializedTypeID } from './serialized-type'
5
+ import { Issue, IssueObject } from './issue'
6
+ import { concat } from '@transia/isomorphic/utils'
7
+
8
+ /**
9
+ * Interface for JSON objects that represent cross-chain bridges
10
+ */
11
+ interface XChainBridgeObject extends JsonObject {
12
+ LockingChainDoor: string
13
+ LockingChainIssue: IssueObject | string
14
+ IssuingChainDoor: string
15
+ IssuingChainIssue: IssueObject | string
16
+ }
17
+
18
+ /**
19
+ * Type guard for XChainBridgeObject
20
+ */
21
+ function isXChainBridgeObject(arg): arg is XChainBridgeObject {
22
+ const keys = Object.keys(arg).sort()
23
+ return (
24
+ keys.length === 4 &&
25
+ keys[0] === 'IssuingChainDoor' &&
26
+ keys[1] === 'IssuingChainIssue' &&
27
+ keys[2] === 'LockingChainDoor' &&
28
+ keys[3] === 'LockingChainIssue'
29
+ )
30
+ }
31
+
32
+ /**
33
+ * Class for serializing/deserializing XChainBridges
34
+ */
35
+ class XChainBridge extends SerializedType {
36
+ static readonly ZERO_XCHAIN_BRIDGE: XChainBridge = new XChainBridge(
37
+ concat([
38
+ Uint8Array.from([0x14]),
39
+ new Uint8Array(40),
40
+ Uint8Array.from([0x14]),
41
+ new Uint8Array(40),
42
+ ]),
43
+ )
44
+
45
+ static readonly TYPE_ORDER: { name: string; type: typeof SerializedType }[] =
46
+ [
47
+ { name: 'LockingChainDoor', type: AccountID },
48
+ { name: 'LockingChainIssue', type: Issue },
49
+ { name: 'IssuingChainDoor', type: AccountID },
50
+ { name: 'IssuingChainIssue', type: Issue },
51
+ ]
52
+
53
+ constructor(bytes: Uint8Array) {
54
+ super(bytes ?? XChainBridge.ZERO_XCHAIN_BRIDGE.bytes)
55
+ }
56
+
57
+ /**
58
+ * Construct a cross-chain bridge from a JSON
59
+ *
60
+ * @param value XChainBridge or JSON to parse into an XChainBridge
61
+ * @returns An XChainBridge object
62
+ */
63
+ static from<T extends XChainBridge | XChainBridgeObject>(
64
+ value: T,
65
+ ): XChainBridge {
66
+ if (value instanceof XChainBridge) {
67
+ return value
68
+ }
69
+
70
+ if (!isXChainBridgeObject(value)) {
71
+ throw new Error('Invalid type to construct an XChainBridge')
72
+ }
73
+
74
+ const bytes: Array<Uint8Array> = []
75
+ this.TYPE_ORDER.forEach((item) => {
76
+ const { name, type } = item
77
+ if (type === AccountID) {
78
+ bytes.push(Uint8Array.from([0x14]))
79
+ }
80
+ const object = type.from(value[name])
81
+ bytes.push(object.toBytes())
82
+ })
83
+ return new XChainBridge(concat(bytes))
84
+ }
85
+
86
+ /**
87
+ * Read an XChainBridge from a BinaryParser
88
+ *
89
+ * @param parser BinaryParser to read the XChainBridge from
90
+ * @returns An XChainBridge object
91
+ */
92
+ static fromParser(parser: BinaryParser): XChainBridge {
93
+ const bytes: Array<Uint8Array> = []
94
+
95
+ this.TYPE_ORDER.forEach((item) => {
96
+ const { type } = item
97
+ if (type === AccountID) {
98
+ parser.skip(1)
99
+ bytes.push(Uint8Array.from([0x14]))
100
+ }
101
+ const object = type.fromParser(parser)
102
+ bytes.push(object.toBytes())
103
+ })
104
+
105
+ return new XChainBridge(concat(bytes))
106
+ }
107
+
108
+ /**
109
+ * Get the JSON representation of this XChainBridge
110
+ *
111
+ * @returns the JSON interpretation of this.bytes
112
+ */
113
+ toJSON(): XChainBridgeObject {
114
+ const parser = new BinaryParser(this.toString())
115
+ const json = {}
116
+ XChainBridge.TYPE_ORDER.forEach((item) => {
117
+ const { name, type } = item
118
+ if (type === AccountID) {
119
+ parser.skip(1)
120
+ }
121
+ const object = type.fromParser(parser).toJSON()
122
+ json[name] = object
123
+ })
124
+ return json as XChainBridgeObject
125
+ }
126
+
127
+ getSType(): SerializedTypeID {
128
+ return SerializedTypeID.STI_XCHAIN_BRIDGE
129
+ }
130
+ }
131
+
132
+ export { XChainBridge, XChainBridgeObject }
package/src/utils.ts ADDED
@@ -0,0 +1,220 @@
1
+ // Even though this comes from NodeJS it is valid in the browser
2
+ import TypedArray = NodeJS.TypedArray
3
+
4
+ /**
5
+ * Writes value to array at the specified offset. The value must be a valid unsigned 8-bit integer.
6
+ * @param array Uint8Array to be written to
7
+ * @param value Number to be written to array.
8
+ * @param offset plus the number of bytes written.
9
+ */
10
+ export function writeUInt8(
11
+ array: Uint8Array,
12
+ value: number,
13
+ offset: number,
14
+ ): void {
15
+ value = Number(value)
16
+ array[offset] = value
17
+ }
18
+
19
+ /**
20
+ * Writes value to array at the specified offset as big-endian. The value must be a valid unsigned 16-bit integer.
21
+ * @param array Uint8Array to be written to
22
+ * @param value Number to be written to array.
23
+ * @param offset plus the number of bytes written.
24
+ */
25
+ export function writeUInt16BE(
26
+ array: Uint8Array,
27
+ value: number,
28
+ offset: number,
29
+ ): void {
30
+ value = Number(value)
31
+
32
+ array[offset] = value >>> 8
33
+ array[offset + 1] = value
34
+ }
35
+
36
+ /**
37
+ * Writes value to array at the specified offset as big-endian. The value must be a valid unsigned 32-bit integer.
38
+ * @param array Uint8Array to be written to
39
+ * @param value Number to be written to array.
40
+ * @param offset plus the number of bytes written.
41
+ */
42
+ export function writeUInt32BE(
43
+ array: Uint8Array,
44
+ value: number,
45
+ offset: number,
46
+ ): void {
47
+ array[offset] = (value >>> 24) & 0xff
48
+ array[offset + 1] = (value >>> 16) & 0xff
49
+ array[offset + 2] = (value >>> 8) & 0xff
50
+ array[offset + 3] = value & 0xff
51
+ }
52
+
53
+ /**
54
+ * Writes a signed 32-bit integer to a Uint8Array at the specified offset (big-endian).
55
+ *
56
+ * @param array - The Uint8Array to write to.
57
+ * @param value - The signed 32-bit integer to write.
58
+ * @param offset - The offset at which to write.
59
+ */
60
+ export function writeInt32BE(
61
+ array: Uint8Array,
62
+ value: number,
63
+ offset: number,
64
+ ): void {
65
+ new DataView(array.buffer, array.byteOffset, array.byteLength).setInt32(
66
+ offset,
67
+ value,
68
+ false,
69
+ )
70
+ }
71
+
72
+ /**
73
+ * Writes a signed 64-bit integer (BigInt) to a Uint8Array at the specified offset (big-endian).
74
+ *
75
+ * @param array - The Uint8Array to write to.
76
+ * @param value - The signed 64-bit integer (BigInt) to write.
77
+ * @param offset - The offset at which to write.
78
+ */
79
+ export function writeInt64BE(
80
+ array: Uint8Array,
81
+ value: bigint,
82
+ offset: number,
83
+ ): void {
84
+ new DataView(array.buffer, array.byteOffset, array.byteLength).setBigInt64(
85
+ offset,
86
+ value,
87
+ false,
88
+ )
89
+ }
90
+
91
+ /**
92
+ * Reads an unsigned, big-endian 16-bit integer from the array at the specified offset.
93
+ * @param array Uint8Array to read
94
+ * @param offset Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2
95
+ */
96
+ export function readUInt16BE(array: Uint8Array, offset: number): string {
97
+ return new DataView(array.buffer).getUint16(offset, false).toString(10)
98
+ }
99
+
100
+ /**
101
+ * Reads an unsigned, big-endian 16-bit integer from the array at the specified offset.
102
+ * @param array Uint8Array to read
103
+ * @param offset Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4
104
+ */
105
+ export function readUInt32BE(array: Uint8Array, offset: number): string {
106
+ return new DataView(array.buffer).getUint32(offset, false).toString(10)
107
+ }
108
+
109
+ /**
110
+ * Reads a signed 32-bit integer from a Uint8Array at the specified offset (big-endian).
111
+ *
112
+ * @param array - The Uint8Array to read from.
113
+ * @param offset - The offset at which to start reading.
114
+ * @returns The signed 32-bit integer.
115
+ */
116
+ export function readInt32BE(array: Uint8Array, offset: number): number {
117
+ return new DataView(
118
+ array.buffer,
119
+ array.byteOffset,
120
+ array.byteLength,
121
+ ).getInt32(offset, false)
122
+ }
123
+
124
+ /**
125
+ * Reads a signed 64-bit integer (BigInt) from a Uint8Array at the specified offset (big-endian).
126
+ *
127
+ * @param array - The Uint8Array to read from.
128
+ * @param offset - The offset at which to start reading.
129
+ * @returns The signed 64-bit integer (BigInt).
130
+ */
131
+ export function readInt64BE(array: Uint8Array, offset: number): bigint {
132
+ return new DataView(
133
+ array.buffer,
134
+ array.byteOffset,
135
+ array.byteLength,
136
+ ).getBigInt64(offset, false)
137
+ }
138
+
139
+ /**
140
+ * Compares two Uint8Array or ArrayBuffers
141
+ * @param a first array to compare
142
+ * @param b second array to compare
143
+ */
144
+ export function equal(
145
+ a: Uint8Array | ArrayBuffer,
146
+ b: Uint8Array | ArrayBuffer,
147
+ ): boolean {
148
+ const aUInt = a instanceof ArrayBuffer ? new Uint8Array(a, 0) : a
149
+ const bUInt = b instanceof ArrayBuffer ? new Uint8Array(b, 0) : b
150
+ if (aUInt.byteLength != bUInt.byteLength) return false
151
+ if (aligned32(aUInt) && aligned32(bUInt)) return compare32(aUInt, bUInt) === 0
152
+ if (aligned16(aUInt) && aligned16(bUInt)) return compare16(aUInt, bUInt) === 0
153
+ return compare8(aUInt, bUInt) === 0
154
+ }
155
+
156
+ /**
157
+ * Compares two 8 bit aligned arrays
158
+ * @param a first array to compare
159
+ * @param b second array to compare
160
+ */
161
+ function compare8(a, b) {
162
+ const ua = new Uint8Array(a.buffer, a.byteOffset, a.byteLength)
163
+ const ub = new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
164
+ return compare(ua, ub)
165
+ }
166
+
167
+ /**
168
+ * Compares two 16 bit aligned arrays
169
+ * @param a first array to compare
170
+ * @param b second array to compare
171
+ */
172
+ function compare16(a: Uint8Array, b: Uint8Array) {
173
+ const ua = new Uint16Array(a.buffer, a.byteOffset, a.byteLength / 2)
174
+ const ub = new Uint16Array(b.buffer, b.byteOffset, b.byteLength / 2)
175
+ return compare(ua, ub)
176
+ }
177
+
178
+ /**
179
+ * Compares two 32 bit aligned arrays
180
+ * @param a first array to compare
181
+ * @param b second array to compare
182
+ */
183
+ function compare32(a: Uint8Array, b: Uint8Array) {
184
+ const ua = new Uint32Array(a.buffer, a.byteOffset, a.byteLength / 4)
185
+ const ub = new Uint32Array(b.buffer, b.byteOffset, b.byteLength / 4)
186
+ return compare(ua, ub)
187
+ }
188
+
189
+ /**
190
+ * Compare two TypedArrays
191
+ * @param a first array to compare
192
+ * @param b second array to compare
193
+ */
194
+ export function compare(a: TypedArray, b: TypedArray): 1 | -1 | 0 {
195
+ if (a.byteLength !== b.byteLength) {
196
+ throw new Error('Cannot compare arrays of different length')
197
+ }
198
+
199
+ for (let i = 0; i < a.length - 1; i += 1) {
200
+ if (a[i] > b[i]) return 1
201
+ if (a[i] < b[i]) return -1
202
+ }
203
+ return 0
204
+ }
205
+
206
+ /**
207
+ * Determine if TypedArray is 16 bit aligned
208
+ * @param array The array to check
209
+ */
210
+ function aligned16(array: TypedArray) {
211
+ return array.byteOffset % 2 === 0 && array.byteLength % 2 === 0
212
+ }
213
+
214
+ /**
215
+ * Determine if TypedArray is 32 bit aligned
216
+ * @param array The array to check
217
+ */
218
+ function aligned32(array: TypedArray) {
219
+ return array.byteOffset % 4 === 0 && array.byteLength % 4 === 0
220
+ }