@transia/ripple-binary-codec 1.4.6-alpha.9 → 2.5.2-alpha.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 (363) 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 +1604 -969
  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 +1606 -971
  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 -319
  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/remit-binary.json +0 -1
  342. package/test/fixtures/remit-tx.json +0 -39
  343. package/test/fixtures/signerlistset-tx-binary.json +0 -1
  344. package/test/fixtures/signerlistset-tx-meta-binary.json +0 -1
  345. package/test/fixtures/signerlistset-tx.json +0 -94
  346. package/test/fixtures/ticket-create-binary.json +0 -1
  347. package/test/fixtures/ticket-create-tx.json +0 -7
  348. package/test/fixtures/unl-report-binary.json +0 -1
  349. package/test/fixtures/unl-report-meta-binary.json +0 -1
  350. package/test/fixtures/unl-report.json +0 -89
  351. package/test/fixtures/x-codec-fixtures.json +0 -188
  352. package/test/hash.test.js +0 -135
  353. package/test/ledger.test.js +0 -29
  354. package/test/lower-case-hex.test.js +0 -46
  355. package/test/pseudo-transaction.test.js +0 -38
  356. package/test/quality.test.js +0 -15
  357. package/test/shamap.test.js +0 -89
  358. package/test/signing-data-encoding.test.js +0 -213
  359. package/test/tx-encode-decode.test.js +0 -119
  360. package/test/types.test.js +0 -34
  361. package/test/uint.test.js +0 -148
  362. package/test/utils.js +0 -30
  363. package/test/x-address.test.js +0 -181
@@ -1,43 +0,0 @@
1
- const { loadFixture } = require('./utils')
2
- const { coreTypes } = require('../src/types')
3
- const { Amount } = coreTypes
4
- const fixtures = loadFixture('data-driven-tests.json')
5
-
6
- function amountErrorTests() {
7
- fixtures.values_tests
8
- .filter((obj) => obj.type === 'Amount')
9
- .forEach((f) => {
10
- // We only want these with errors
11
- if (!f.error) {
12
- return
13
- }
14
- const testName =
15
- `${JSON.stringify(f.test_json)}\n\tis invalid ` + `because: ${f.error}`
16
- it(testName, () => {
17
- expect(() => {
18
- Amount.from(f.test_json)
19
- JSON.stringify(f.test_json)
20
- }).toThrow()
21
- })
22
- })
23
- }
24
-
25
- describe('Amount', function () {
26
- it('can be parsed from', function () {
27
- expect(Amount.from('1000000') instanceof Amount).toBe(true)
28
- expect(Amount.from('1000000').toJSON()).toEqual('1000000')
29
- const fixture = {
30
- value: '1',
31
- issuer: '0000000000000000000000000000000000000000',
32
- currency: 'USD',
33
- }
34
- const amt = Amount.from(fixture)
35
- const rewritten = {
36
- value: '1',
37
- issuer: 'rrrrrrrrrrrrrrrrrrrrrhoLvTp',
38
- currency: 'USD',
39
- }
40
- expect(amt.toJSON()).toEqual(rewritten)
41
- })
42
- amountErrorTests()
43
- })
@@ -1,45 +0,0 @@
1
- const fixtures = require('./fixtures/codec-fixtures.json')
2
- const { decode, encode, decodeLedgerData } = require('../src')
3
-
4
- function json(object) {
5
- return JSON.stringify(object)
6
- }
7
-
8
- function truncateForDisplay(longStr) {
9
- return `${longStr.slice(0, 10)} ... ${longStr.slice(-10)}`
10
- }
11
-
12
- describe('ripple-binary-codec', function () {
13
- function makeSuite(name, entries) {
14
- describe(name, function () {
15
- entries.forEach((t, testN) => {
16
- test(`${name}[${testN}] can encode ${truncateForDisplay(
17
- json(t.json),
18
- )} to ${truncateForDisplay(t.binary)}`, () => {
19
- expect(encode(t.json)).toEqual(t.binary)
20
- })
21
- test(`${name}[${testN}] can decode ${truncateForDisplay(
22
- t.binary,
23
- )} to ${truncateForDisplay(json(t.json))}`, () => {
24
- const decoded = decode(t.binary)
25
- expect(decoded).toEqual(t.json)
26
- })
27
- })
28
- })
29
- }
30
- makeSuite('transactions', fixtures.transactions)
31
- makeSuite('accountState', fixtures.accountState)
32
-
33
- describe('ledgerData', function () {
34
- if (fixtures.ledgerData) {
35
- fixtures.ledgerData.forEach((t, testN) => {
36
- test(`ledgerData[${testN}] can decode ${t.binary} to ${json(
37
- t.json,
38
- )}`, () => {
39
- const decoded = decodeLedgerData(t.binary)
40
- expect(t.json).toEqual(decoded)
41
- })
42
- })
43
- }
44
- })
45
- })
@@ -1,396 +0,0 @@
1
- const { coreTypes } = require('../src/types')
2
- const Decimal = require('decimal.js')
3
-
4
- const { encodeAccountID } = require('@transia/ripple-address-codec')
5
- const { binary } = require('../src/coretypes')
6
- const { Amount, Hash160 } = coreTypes
7
- const { makeParser, readJSON } = binary
8
- const { Field, TransactionType } = require('./../src/enums')
9
- const { parseHexOnly, hexOnly, loadFixture } = require('./utils')
10
- const fixtures = loadFixture('data-driven-tests.json')
11
- const { BytesList } = require('../src/serdes/binary-serializer')
12
- const { Buffer } = require('buffer/')
13
-
14
- const __ = hexOnly
15
- function toJSON(v) {
16
- return v.toJSON ? v.toJSON() : v
17
- }
18
-
19
- function assertEqualAmountJSON(actual, expected) {
20
- expect(typeof actual === typeof expected).toBe(true)
21
- if (typeof actual === 'string') {
22
- expect(actual).toEqual(expected)
23
- return
24
- }
25
- expect(actual.currency).toEqual(expected.currency)
26
- expect(actual.issuer).toEqual(expected.issuer)
27
- expect(
28
- actual.value === expected.value ||
29
- new Decimal(actual.value).equals(new Decimal(expected.value)),
30
- ).toBe(true)
31
- }
32
-
33
- function basicApiTests() {
34
- const bytes = parseHexOnly('00,01020304,0506', Uint8Array)
35
- test('can read slices of bytes', () => {
36
- const parser = makeParser(bytes)
37
- expect(parser.bytes instanceof Buffer).toBe(true)
38
- const read1 = parser.read(1)
39
- expect(read1 instanceof Buffer).toBe(true)
40
- expect(read1).toEqual(Buffer.from([0]))
41
- expect(parser.read(4)).toEqual(Buffer.from([1, 2, 3, 4]))
42
- expect(parser.read(2)).toEqual(Buffer.from([5, 6]))
43
- expect(() => parser.read(1)).toThrow()
44
- })
45
- test('can read a Uint32 at full', () => {
46
- const parser = makeParser('FFFFFFFF')
47
- expect(parser.readUInt32()).toEqual(0xffffffff)
48
- })
49
- }
50
-
51
- function transactionParsingTests() {
52
- const transaction = {
53
- json: {
54
- Account: 'raD5qJMAShLeHZXf9wjUmo6vRK4arj9cF3',
55
- Fee: '10',
56
- Flags: 0,
57
- Sequence: 103929,
58
- SigningPubKey:
59
- '028472865AF4CB32AA285834B57576B7290AA8C31B459047DB27E16F418D6A7166',
60
- TakerGets: {
61
- currency: 'ILS',
62
- issuer: 'rNPRNzBB92BVpAhhZr4iXDTveCgV5Pofm9',
63
- value: '1694.768',
64
- },
65
- TakerPays: '98957503520',
66
- TransactionType: 'OfferCreate',
67
- TxnSignature: __(`
68
- 304502202ABE08D5E78D1E74A4C18F2714F64E87B8BD57444AF
69
- A5733109EB3C077077520022100DB335EE97386E4C0591CAC02
70
- 4D50E9230D8F171EEB901B5E5E4BD6D1E0AEF98C`),
71
- },
72
- binary: __(`
73
- 120007220000000024000195F964400000170A53AC2065D5460561E
74
- C9DE000000000000000000000000000494C53000000000092D70596
75
- 8936C419CE614BF264B5EEB1CEA47FF468400000000000000A73210
76
- 28472865AF4CB32AA285834B57576B7290AA8C31B459047DB27E16F
77
- 418D6A71667447304502202ABE08D5E78D1E74A4C18F2714F64E87B
78
- 8BD57444AFA5733109EB3C077077520022100DB335EE97386E4C059
79
- 1CAC024D50E9230D8F171EEB901B5E5E4BD6D1E0AEF98C811439408
80
- A69F0895E62149CFCC006FB89FA7D1E6E5D`),
81
- }
82
-
83
- const tx_json = transaction.json
84
- // These tests are basically development logs
85
-
86
- test('can be done with low level apis', () => {
87
- const parser = makeParser(transaction.binary)
88
-
89
- expect(parser.readField()).toEqual(Field.TransactionType)
90
- expect(parser.readUInt16()).toEqual(7)
91
- expect(parser.readField()).toEqual(Field.Flags)
92
- expect(parser.readUInt32()).toEqual(0)
93
- expect(parser.readField()).toEqual(Field.Sequence)
94
- expect(parser.readUInt32()).toEqual(103929)
95
- expect(parser.readField()).toEqual(Field.TakerPays)
96
- parser.read(8)
97
- expect(parser.readField()).toEqual(Field.TakerGets)
98
- // amount value
99
- expect(parser.read(8)).not.toBe([])
100
- // amount currency
101
- expect(Hash160.fromParser(parser)).not.toBe([])
102
- expect(encodeAccountID(parser.read(20))).toEqual(tx_json.TakerGets.issuer)
103
- expect(parser.readField()).toEqual(Field.Fee)
104
- expect(parser.read(8)).not.toEqual([])
105
- expect(parser.readField()).toEqual(Field.SigningPubKey)
106
- expect(parser.readVariableLengthLength()).toBe(33)
107
- expect(parser.read(33).toString('hex').toUpperCase()).toEqual(
108
- tx_json.SigningPubKey,
109
- )
110
- expect(parser.readField()).toEqual(Field.TxnSignature)
111
- expect(parser.readVariableLength().toString('hex').toUpperCase()).toEqual(
112
- tx_json.TxnSignature,
113
- )
114
- expect(parser.readField()).toEqual(Field.Account)
115
- expect(encodeAccountID(parser.readVariableLength())).toEqual(
116
- tx_json.Account,
117
- )
118
- expect(parser.end()).toBe(true)
119
- })
120
-
121
- test('can be done with high level apis', () => {
122
- const parser = makeParser(transaction.binary)
123
- function readField() {
124
- return parser.readFieldAndValue()
125
- }
126
- {
127
- const [field, value] = readField()
128
- expect(field).toEqual(Field.TransactionType)
129
- expect(value).toEqual(TransactionType.OfferCreate)
130
- }
131
- {
132
- const [field, value] = readField()
133
- expect(field).toEqual(Field.Flags)
134
- expect(value.valueOf()).toEqual(0)
135
- }
136
- {
137
- const [field, value] = readField()
138
- expect(field).toEqual(Field.Sequence)
139
- expect(value.valueOf()).toEqual(103929)
140
- }
141
- {
142
- const [field, value] = readField()
143
- expect(field).toEqual(Field.TakerPays)
144
- expect(value.isNative()).toEqual(true)
145
- expect(value.toJSON()).toEqual('98957503520')
146
- }
147
- {
148
- const [field, value] = readField()
149
- expect(field).toEqual(Field.TakerGets)
150
- expect(value.isNative()).toEqual(false)
151
- expect(value.toJSON().issuer).toEqual(tx_json.TakerGets.issuer)
152
- }
153
- {
154
- const [field, value] = readField()
155
- expect(field).toEqual(Field.Fee)
156
- expect(value.isNative()).toEqual(true)
157
- }
158
- {
159
- const [field, value] = readField()
160
- expect(field).toEqual(Field.SigningPubKey)
161
- expect(value.toJSON()).toEqual(tx_json.SigningPubKey)
162
- }
163
- {
164
- const [field, value] = readField()
165
- expect(field).toEqual(Field.TxnSignature)
166
- expect(value.toJSON()).toEqual(tx_json.TxnSignature)
167
- }
168
- {
169
- const [field, value] = readField()
170
- expect(field).toEqual(Field.Account)
171
- expect(value.toJSON()).toEqual(tx_json.Account)
172
- }
173
- expect(parser.end()).toBe(true)
174
- })
175
-
176
- test('can be done with higher level apis', () => {
177
- const parser = makeParser(transaction.binary)
178
- const jsonFromBinary = readJSON(parser)
179
- expect(jsonFromBinary).toEqual(tx_json)
180
- })
181
-
182
- test('readJSON (binary.decode) does not return STObject ', () => {
183
- const parser = makeParser(transaction.binary)
184
- const jsonFromBinary = readJSON(parser)
185
- expect(jsonFromBinary instanceof coreTypes.STObject).toBe(false)
186
- expect(jsonFromBinary instanceof Object).toBe(true)
187
- expect(jsonFromBinary.prototype).toBe(undefined)
188
- })
189
- }
190
-
191
- function amountParsingTests() {
192
- fixtures.values_tests
193
- .filter((obj) => obj.type === 'Amount')
194
- .forEach((f, i) => {
195
- if (f.error) {
196
- return
197
- }
198
- const parser = makeParser(f.expected_hex)
199
- const testName = `values_tests[${i}] parses ${f.expected_hex.slice(
200
- 0,
201
- 16,
202
- )}...
203
- as ${JSON.stringify(f.test_json)}`
204
- test(testName, () => {
205
- const value = parser.readType(Amount)
206
- // May not actually be in canonical form. The fixtures are to be used
207
- // also for json -> binary;
208
- const json = toJSON(value)
209
- assertEqualAmountJSON(json, f.test_json)
210
- if (f.exponent) {
211
- const exponent = new Decimal(json.value)
212
- expect(exponent.e - 15).toEqual(f.exponent)
213
- }
214
- })
215
- })
216
- }
217
-
218
- function fieldParsingTests() {
219
- fixtures.fields_tests.forEach((f, i) => {
220
- const parser = makeParser(f.expected_hex)
221
- test(`fields[${i}]: parses ${f.expected_hex} as ${f.name}`, () => {
222
- const field = parser.readField()
223
- expect(field.name).toEqual(f.name)
224
- expect(field.type.name).toEqual(f.type_name)
225
- })
226
- })
227
- test('Field throws when type code out of range', () => {
228
- const parser = makeParser('0101')
229
- expect(() => parser.readField()).toThrow(
230
- new Error('Cannot read FieldOrdinal, type_code out of range'),
231
- )
232
- })
233
- test('Field throws when field code out of range', () => {
234
- const parser = makeParser('1001')
235
- expect(() => parser.readFieldOrdinal()).toThrowError(
236
- new Error('Cannot read FieldOrdinal, field_code out of range'),
237
- )
238
- })
239
- test('Field throws when both type and field code out of range', () => {
240
- const parser = makeParser('000101')
241
- expect(() => parser.readFieldOrdinal()).toThrowError(
242
- new Error('Cannot read FieldOrdinal, type_code out of range'),
243
- )
244
- })
245
- }
246
-
247
- function assertRecyclable(json, forField) {
248
- const Type = forField.associatedType
249
- const recycled = Type.from(json).toJSON()
250
- expect(recycled).toEqual(json)
251
- const sink = new BytesList()
252
- Type.from(recycled).toBytesSink(sink)
253
- const recycledAgain = makeParser(sink.toHex()).readType(Type).toJSON()
254
- expect(recycledAgain).toEqual(json)
255
- }
256
-
257
- function nestedObjectTests() {
258
- fixtures.whole_objects.forEach((f, i) => {
259
- test(`whole_objects[${i}]: can parse blob into
260
- ${JSON.stringify(
261
- f.tx_json,
262
- )}`, /* */ () => {
263
- const parser = makeParser(f.blob_with_no_signing)
264
- let ix = 0
265
- while (!parser.end()) {
266
- const [field, value] = parser.readFieldAndValue()
267
- const expected = f.fields[ix]
268
- const expectedJSON = expected[1].json
269
- const expectedField = expected[0]
270
- const actual = toJSON(value)
271
-
272
- try {
273
- expect(actual).toEqual(expectedJSON)
274
- } catch (e) {
275
- throw new Error(`${e} ${field} a: ${actual} e: ${expectedJSON}`)
276
- }
277
- expect(field.name).toEqual(expectedField)
278
- assertRecyclable(actual, field)
279
- ix++
280
- }
281
- })
282
- })
283
- }
284
-
285
- function pathSetBinaryTests() {
286
- const bytes = __(
287
- `1200002200000000240000002E2E00004BF161D4C71AFD498D00000000000000
288
- 0000000000000055534400000000000A20B3C85F482532A9578DBB3950B85CA0
289
- 6594D168400000000000000A69D446F8038585E9400000000000000000000000
290
- 00425443000000000078CA21A6014541AB7B26C3929B9E0CD8C284D61C732103
291
- A4665B1F0B7AE2BCA12E2DB80A192125BBEA660F80E9CEE137BA444C1B0769EC
292
- 7447304502205A964536805E35785C659D1F9670D057749AE39668175D6AA75D
293
- 25B218FE682E0221009252C0E5DDD5F2712A48F211669DE17B54113918E0D2C2
294
- 66F818095E9339D7D3811478CA21A6014541AB7B26C3929B9E0CD8C284D61C83
295
- 140A20B3C85F482532A9578DBB3950B85CA06594D1011231585E1F3BD02A15D6
296
- 185F8BB9B57CC60DEDDB37C10000000000000000000000004254430000000000
297
- 585E1F3BD02A15D6185F8BB9B57CC60DEDDB37C131E4FE687C90257D3D2D694C
298
- 8531CDEECBE84F33670000000000000000000000004254430000000000E4FE68
299
- 7C90257D3D2D694C8531CDEECBE84F3367310A20B3C85F482532A9578DBB3950
300
- B85CA06594D100000000000000000000000042544300000000000A20B3C85F48
301
- 2532A9578DBB3950B85CA06594D1300000000000000000000000005553440000
302
- 0000000A20B3C85F482532A9578DBB3950B85CA06594D1FF31585E1F3BD02A15
303
- D6185F8BB9B57CC60DEDDB37C100000000000000000000000042544300000000
304
- 00585E1F3BD02A15D6185F8BB9B57CC60DEDDB37C131E4FE687C90257D3D2D69
305
- 4C8531CDEECBE84F33670000000000000000000000004254430000000000E4FE
306
- 687C90257D3D2D694C8531CDEECBE84F33673115036E2D3F5437A83E5AC3CAEE
307
- 34FF2C21DEB618000000000000000000000000425443000000000015036E2D3F
308
- 5437A83E5AC3CAEE34FF2C21DEB6183000000000000000000000000055534400
309
- 000000000A20B3C85F482532A9578DBB3950B85CA06594D1FF31585E1F3BD02A
310
- 15D6185F8BB9B57CC60DEDDB37C1000000000000000000000000425443000000
311
- 0000585E1F3BD02A15D6185F8BB9B57CC60DEDDB37C13157180C769B66D942EE
312
- 69E6DCC940CA48D82337AD000000000000000000000000425443000000000057
313
- 180C769B66D942EE69E6DCC940CA48D82337AD10000000000000000000000000
314
- 00000000000000003000000000000000000000000055534400000000000A20B3
315
- C85F482532A9578DBB3950B85CA06594D100`,
316
- )
317
-
318
- const expectedJSON = [
319
- [
320
- {
321
- account: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
322
- currency: 'BTC',
323
- issuer: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
324
- },
325
- {
326
- account: 'rM1oqKtfh1zgjdAgbFmaRm3btfGBX25xVo',
327
- currency: 'BTC',
328
- issuer: 'rM1oqKtfh1zgjdAgbFmaRm3btfGBX25xVo',
329
- },
330
- {
331
- account: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
332
- currency: 'BTC',
333
- issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
334
- },
335
- {
336
- currency: 'USD',
337
- issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
338
- },
339
- ],
340
- [
341
- {
342
- account: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
343
- currency: 'BTC',
344
- issuer: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
345
- },
346
- {
347
- account: 'rM1oqKtfh1zgjdAgbFmaRm3btfGBX25xVo',
348
- currency: 'BTC',
349
- issuer: 'rM1oqKtfh1zgjdAgbFmaRm3btfGBX25xVo',
350
- },
351
- {
352
- account: 'rpvfJ4mR6QQAeogpXEKnuyGBx8mYCSnYZi',
353
- currency: 'BTC',
354
- issuer: 'rpvfJ4mR6QQAeogpXEKnuyGBx8mYCSnYZi',
355
- },
356
- {
357
- currency: 'USD',
358
- issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
359
- },
360
- ],
361
- [
362
- {
363
- account: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
364
- currency: 'BTC',
365
- issuer: 'r9hEDb4xBGRfBCcX3E4FirDWQBAYtpxC8K',
366
- },
367
- {
368
- account: 'r3AWbdp2jQLXLywJypdoNwVSvr81xs3uhn',
369
- currency: 'BTC',
370
- issuer: 'r3AWbdp2jQLXLywJypdoNwVSvr81xs3uhn',
371
- },
372
- { currency: 'XAH' },
373
- {
374
- currency: 'USD',
375
- issuer: 'rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B',
376
- },
377
- ],
378
- ]
379
-
380
- test('works with long paths', () => {
381
- const parser = makeParser(bytes)
382
- const txn = readJSON(parser)
383
- expect(txn.Paths).toEqual(expectedJSON)
384
- // TODO: this should go elsewhere
385
- expect(coreTypes.PathSet.from(txn.Paths).toJSON()).toEqual(expectedJSON)
386
- })
387
- }
388
-
389
- describe('Binary Parser', function () {
390
- describe('pathSetBinaryTests', () => pathSetBinaryTests())
391
- describe('nestedObjectTests', () => nestedObjectTests())
392
- describe('fieldParsingTests', () => fieldParsingTests())
393
- describe('amountParsingTests', () => amountParsingTests())
394
- describe('transactionParsingTests', () => transactionParsingTests())
395
- describe('basicApiTests', () => basicApiTests())
396
- })