ripple-binary-codec 0.2.7 → 1.0.0-mpt-beta

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 (364) hide show
  1. package/README.md +104 -2
  2. package/dist/binary.d.ts +90 -0
  3. package/dist/binary.js +128 -0
  4. package/dist/binary.js.map +1 -0
  5. package/dist/coretypes.d.ts +9 -0
  6. package/dist/coretypes.js +48 -0
  7. package/dist/coretypes.js.map +1 -0
  8. package/dist/enums/bytes.d.ts +25 -0
  9. package/dist/enums/bytes.js +63 -0
  10. package/dist/enums/bytes.js.map +1 -0
  11. package/dist/enums/constants.d.ts +4 -0
  12. package/dist/enums/constants.js +8 -0
  13. package/dist/enums/constants.js.map +1 -0
  14. package/dist/enums/definitions.json +2956 -0
  15. package/dist/enums/field.d.ts +28 -0
  16. package/dist/enums/field.js +58 -0
  17. package/dist/enums/field.js.map +1 -0
  18. package/dist/enums/index.d.ts +12 -0
  19. package/dist/enums/index.js +31 -0
  20. package/dist/enums/index.js.map +1 -0
  21. package/{distrib/npm → dist/enums/src}/enums/definitions.json +1709 -438
  22. package/dist/enums/utils-renumber.d.ts +101 -0
  23. package/dist/enums/utils-renumber.js +127 -0
  24. package/dist/enums/utils-renumber.js.map +1 -0
  25. package/dist/enums/xrpl-definitions-base.d.ts +44 -0
  26. package/dist/enums/xrpl-definitions-base.js +59 -0
  27. package/dist/enums/xrpl-definitions-base.js.map +1 -0
  28. package/dist/enums/xrpl-definitions.d.ts +21 -0
  29. package/dist/enums/xrpl-definitions.js +29 -0
  30. package/dist/enums/xrpl-definitions.js.map +1 -0
  31. package/dist/hash-prefixes.d.ts +5 -0
  32. package/dist/hash-prefixes.js +41 -0
  33. package/dist/hash-prefixes.js.map +1 -0
  34. package/dist/hashes.d.ts +50 -0
  35. package/dist/hashes.js +76 -0
  36. package/dist/hashes.js.map +1 -0
  37. package/dist/index.d.ts +64 -0
  38. package/dist/index.js +123 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/ledger-hashes.d.ts +48 -0
  41. package/dist/ledger-hashes.js +141 -0
  42. package/dist/ledger-hashes.js.map +1 -0
  43. package/dist/quality.d.ts +21 -0
  44. package/dist/quality.js +42 -0
  45. package/dist/quality.js.map +1 -0
  46. package/dist/serdes/binary-parser.d.ts +100 -0
  47. package/dist/serdes/binary-parser.js +194 -0
  48. package/dist/serdes/binary-parser.js.map +1 -0
  49. package/dist/serdes/binary-serializer.d.ts +81 -0
  50. package/dist/serdes/binary-serializer.js +149 -0
  51. package/dist/serdes/binary-serializer.js.map +1 -0
  52. package/dist/shamap.d.ts +102 -0
  53. package/dist/shamap.js +172 -0
  54. package/dist/shamap.js.map +1 -0
  55. package/dist/src/binary.d.ts +90 -0
  56. package/dist/src/binary.js +128 -0
  57. package/dist/src/binary.js.map +1 -0
  58. package/dist/src/coretypes.d.ts +9 -0
  59. package/dist/src/coretypes.js +48 -0
  60. package/dist/src/coretypes.js.map +1 -0
  61. package/dist/src/enums/bytes.d.ts +25 -0
  62. package/dist/src/enums/bytes.js +63 -0
  63. package/dist/src/enums/bytes.js.map +1 -0
  64. package/dist/src/enums/constants.d.ts +4 -0
  65. package/dist/src/enums/constants.js +8 -0
  66. package/dist/src/enums/constants.js.map +1 -0
  67. package/dist/src/enums/definitions.json +2956 -0
  68. package/dist/src/enums/field.d.ts +28 -0
  69. package/dist/src/enums/field.js +58 -0
  70. package/dist/src/enums/field.js.map +1 -0
  71. package/dist/src/enums/index.d.ts +12 -0
  72. package/dist/src/enums/index.js +31 -0
  73. package/dist/src/enums/index.js.map +1 -0
  74. package/dist/src/enums/utils-renumber.d.ts +101 -0
  75. package/dist/src/enums/utils-renumber.js +127 -0
  76. package/dist/src/enums/utils-renumber.js.map +1 -0
  77. package/dist/src/enums/xrpl-definitions-base.d.ts +44 -0
  78. package/dist/src/enums/xrpl-definitions-base.js +59 -0
  79. package/dist/src/enums/xrpl-definitions-base.js.map +1 -0
  80. package/dist/src/enums/xrpl-definitions.d.ts +21 -0
  81. package/dist/src/enums/xrpl-definitions.js +29 -0
  82. package/dist/src/enums/xrpl-definitions.js.map +1 -0
  83. package/dist/src/hash-prefixes.d.ts +5 -0
  84. package/dist/src/hash-prefixes.js +41 -0
  85. package/dist/src/hash-prefixes.js.map +1 -0
  86. package/dist/src/hashes.d.ts +50 -0
  87. package/dist/src/hashes.js +76 -0
  88. package/dist/src/hashes.js.map +1 -0
  89. package/dist/src/index.d.ts +64 -0
  90. package/dist/src/index.js +123 -0
  91. package/dist/src/index.js.map +1 -0
  92. package/dist/src/ledger-hashes.d.ts +48 -0
  93. package/dist/src/ledger-hashes.js +141 -0
  94. package/dist/src/ledger-hashes.js.map +1 -0
  95. package/dist/src/quality.d.ts +21 -0
  96. package/dist/src/quality.js +42 -0
  97. package/dist/src/quality.js.map +1 -0
  98. package/dist/src/serdes/binary-parser.d.ts +100 -0
  99. package/dist/src/serdes/binary-parser.js +194 -0
  100. package/dist/src/serdes/binary-parser.js.map +1 -0
  101. package/dist/src/serdes/binary-serializer.d.ts +81 -0
  102. package/dist/src/serdes/binary-serializer.js +149 -0
  103. package/dist/src/serdes/binary-serializer.js.map +1 -0
  104. package/dist/src/shamap.d.ts +102 -0
  105. package/dist/src/shamap.js +172 -0
  106. package/dist/src/shamap.js.map +1 -0
  107. package/dist/src/types/account-id.d.ts +35 -0
  108. package/dist/src/types/account-id.js +69 -0
  109. package/dist/src/types/account-id.js.map +1 -0
  110. package/dist/src/types/amount.d.ts +91 -0
  111. package/dist/src/types/amount.js +289 -0
  112. package/dist/src/types/amount.js.map +1 -0
  113. package/dist/src/types/blob.d.ts +24 -0
  114. package/dist/src/types/blob.js +40 -0
  115. package/dist/src/types/blob.js.map +1 -0
  116. package/dist/src/types/currency.d.ts +28 -0
  117. package/dist/src/types/currency.js +127 -0
  118. package/dist/src/types/currency.js.map +1 -0
  119. package/dist/src/types/hash-128.d.ts +16 -0
  120. package/dist/src/types/hash-128.js +32 -0
  121. package/dist/src/types/hash-128.js.map +1 -0
  122. package/dist/src/types/hash-160.d.ts +10 -0
  123. package/dist/src/types/hash-160.js +19 -0
  124. package/dist/src/types/hash-160.js.map +1 -0
  125. package/dist/src/types/hash-192.d.ts +10 -0
  126. package/dist/src/types/hash-192.js +19 -0
  127. package/dist/src/types/hash-192.js.map +1 -0
  128. package/dist/src/types/hash-256.d.ts +10 -0
  129. package/dist/src/types/hash-256.js +16 -0
  130. package/dist/src/types/hash-256.js.map +1 -0
  131. package/dist/src/types/hash.d.ts +40 -0
  132. package/dist/src/types/hash.js +73 -0
  133. package/dist/src/types/hash.js.map +1 -0
  134. package/dist/src/types/index.d.ts +19 -0
  135. package/dist/src/types/index.js +64 -0
  136. package/dist/src/types/index.js.map +1 -0
  137. package/dist/src/types/issue.d.ts +38 -0
  138. package/dist/src/types/issue.js +81 -0
  139. package/dist/src/types/issue.js.map +1 -0
  140. package/dist/src/types/path-set.d.ts +36 -0
  141. package/dist/src/types/path-set.js +233 -0
  142. package/dist/src/types/path-set.js.map +1 -0
  143. package/dist/src/types/serialized-type.d.ts +70 -0
  144. package/dist/src/types/serialized-type.js +105 -0
  145. package/dist/src/types/serialized-type.js.map +1 -0
  146. package/dist/src/types/st-array.d.ts +31 -0
  147. package/dist/src/types/st-array.js +83 -0
  148. package/dist/src/types/st-array.js.map +1 -0
  149. package/dist/src/types/st-object.d.ts +32 -0
  150. package/dist/src/types/st-object.js +155 -0
  151. package/dist/src/types/st-object.js.map +1 -0
  152. package/dist/src/types/uint-16.d.ts +24 -0
  153. package/dist/src/types/uint-16.js +45 -0
  154. package/dist/src/types/uint-16.js.map +1 -0
  155. package/dist/src/types/uint-32.d.ts +24 -0
  156. package/dist/src/types/uint-32.js +50 -0
  157. package/dist/src/types/uint-32.js.map +1 -0
  158. package/dist/src/types/uint-64.d.ts +37 -0
  159. package/dist/src/types/uint-64.js +86 -0
  160. package/dist/src/types/uint-64.js.map +1 -0
  161. package/dist/src/types/uint-8.d.ts +24 -0
  162. package/dist/src/types/uint-8.js +46 -0
  163. package/dist/src/types/uint-8.js.map +1 -0
  164. package/dist/src/types/uint.d.ts +29 -0
  165. package/dist/src/types/uint.js +47 -0
  166. package/dist/src/types/uint.js.map +1 -0
  167. package/dist/src/types/vector-256.d.ts +30 -0
  168. package/dist/src/types/vector-256.js +73 -0
  169. package/dist/src/types/vector-256.js.map +1 -0
  170. package/dist/src/types/xchain-bridge.d.ts +44 -0
  171. package/dist/src/types/xchain-bridge.js +102 -0
  172. package/dist/src/types/xchain-bridge.js.map +1 -0
  173. package/dist/src/utils.d.ts +47 -0
  174. package/dist/src/utils.js +137 -0
  175. package/dist/src/utils.js.map +1 -0
  176. package/dist/tsconfig.tsbuildinfo +1 -0
  177. package/dist/types/account-id.d.ts +35 -0
  178. package/dist/types/account-id.js +69 -0
  179. package/dist/types/account-id.js.map +1 -0
  180. package/dist/types/amount.d.ts +91 -0
  181. package/dist/types/amount.js +289 -0
  182. package/dist/types/amount.js.map +1 -0
  183. package/dist/types/blob.d.ts +24 -0
  184. package/dist/types/blob.js +40 -0
  185. package/dist/types/blob.js.map +1 -0
  186. package/dist/types/currency.d.ts +28 -0
  187. package/dist/types/currency.js +127 -0
  188. package/dist/types/currency.js.map +1 -0
  189. package/dist/types/hash-128.d.ts +16 -0
  190. package/dist/types/hash-128.js +32 -0
  191. package/dist/types/hash-128.js.map +1 -0
  192. package/dist/types/hash-160.d.ts +10 -0
  193. package/dist/types/hash-160.js +19 -0
  194. package/dist/types/hash-160.js.map +1 -0
  195. package/dist/types/hash-192.d.ts +10 -0
  196. package/dist/types/hash-192.js +19 -0
  197. package/dist/types/hash-192.js.map +1 -0
  198. package/dist/types/hash-256.d.ts +10 -0
  199. package/dist/types/hash-256.js +16 -0
  200. package/dist/types/hash-256.js.map +1 -0
  201. package/dist/types/hash.d.ts +40 -0
  202. package/dist/types/hash.js +73 -0
  203. package/dist/types/hash.js.map +1 -0
  204. package/dist/types/index.d.ts +19 -0
  205. package/dist/types/index.js +64 -0
  206. package/dist/types/index.js.map +1 -0
  207. package/dist/types/issue.d.ts +38 -0
  208. package/dist/types/issue.js +81 -0
  209. package/dist/types/issue.js.map +1 -0
  210. package/dist/types/path-set.d.ts +36 -0
  211. package/dist/types/path-set.js +233 -0
  212. package/dist/types/path-set.js.map +1 -0
  213. package/dist/types/serialized-type.d.ts +70 -0
  214. package/dist/types/serialized-type.js +105 -0
  215. package/dist/types/serialized-type.js.map +1 -0
  216. package/dist/types/st-array.d.ts +31 -0
  217. package/dist/types/st-array.js +83 -0
  218. package/dist/types/st-array.js.map +1 -0
  219. package/dist/types/st-object.d.ts +32 -0
  220. package/dist/types/st-object.js +155 -0
  221. package/dist/types/st-object.js.map +1 -0
  222. package/dist/types/uint-16.d.ts +24 -0
  223. package/dist/types/uint-16.js +45 -0
  224. package/dist/types/uint-16.js.map +1 -0
  225. package/dist/types/uint-32.d.ts +24 -0
  226. package/dist/types/uint-32.js +50 -0
  227. package/dist/types/uint-32.js.map +1 -0
  228. package/dist/types/uint-64.d.ts +37 -0
  229. package/dist/types/uint-64.js +86 -0
  230. package/dist/types/uint-64.js.map +1 -0
  231. package/dist/types/uint-8.d.ts +24 -0
  232. package/dist/types/uint-8.js +46 -0
  233. package/dist/types/uint-8.js.map +1 -0
  234. package/dist/types/uint.d.ts +29 -0
  235. package/dist/types/uint.js +47 -0
  236. package/dist/types/uint.js.map +1 -0
  237. package/dist/types/vector-256.d.ts +30 -0
  238. package/dist/types/vector-256.js +73 -0
  239. package/dist/types/vector-256.js.map +1 -0
  240. package/dist/types/xchain-bridge.d.ts +44 -0
  241. package/dist/types/xchain-bridge.js +102 -0
  242. package/dist/types/xchain-bridge.js.map +1 -0
  243. package/dist/utils.d.ts +47 -0
  244. package/dist/utils.js +137 -0
  245. package/dist/utils.js.map +1 -0
  246. package/package.json +28 -33
  247. package/src/README.md +5 -0
  248. package/src/binary.ts +194 -0
  249. package/src/coretypes.ts +31 -0
  250. package/src/enums/README.md +144 -0
  251. package/src/enums/bytes.ts +74 -0
  252. package/src/enums/constants.ts +4 -0
  253. package/src/enums/definitions.json +2961 -0
  254. package/src/enums/field.ts +84 -0
  255. package/src/enums/index.ts +34 -0
  256. package/src/enums/utils-renumber.ts +134 -0
  257. package/src/enums/xrpl-definitions-base.ts +111 -0
  258. package/src/enums/xrpl-definitions.ts +32 -0
  259. package/src/hash-prefixes.ts +40 -0
  260. package/src/hashes.ts +75 -0
  261. package/src/index.ts +153 -0
  262. package/src/ledger-hashes.ts +191 -0
  263. package/src/quality.ts +38 -0
  264. package/src/serdes/binary-parser.ts +224 -0
  265. package/src/serdes/binary-serializer.ts +166 -0
  266. package/src/shamap.ts +186 -0
  267. package/src/types/account-id.ts +86 -0
  268. package/src/types/amount.ts +362 -0
  269. package/src/types/blob.ts +43 -0
  270. package/src/types/currency.ts +140 -0
  271. package/src/types/hash-128.ts +33 -0
  272. package/src/types/hash-160.ts +19 -0
  273. package/src/types/hash-192.ts +19 -0
  274. package/src/types/hash-256.ts +15 -0
  275. package/src/types/hash.ts +83 -0
  276. package/src/types/index.ts +66 -0
  277. package/src/types/issue.ts +96 -0
  278. package/src/types/path-set.ts +290 -0
  279. package/src/types/serialized-type.ts +122 -0
  280. package/src/types/st-array.ts +107 -0
  281. package/src/types/st-object.ts +192 -0
  282. package/src/types/uint-16.ts +53 -0
  283. package/src/types/uint-32.ts +59 -0
  284. package/src/types/uint-64.ts +104 -0
  285. package/src/types/uint-8.ts +52 -0
  286. package/src/types/uint.ts +60 -0
  287. package/src/types/vector-256.ts +79 -0
  288. package/src/types/xchain-bridge.ts +128 -0
  289. package/src/utils.ts +152 -0
  290. package/HISTORY.md +0 -66
  291. package/distrib/npm/binary.js +0 -68
  292. package/distrib/npm/coretypes.js +0 -22
  293. package/distrib/npm/enums/index.js +0 -125
  294. package/distrib/npm/enums/utils-renumber.js +0 -134
  295. package/distrib/npm/hash-prefixes.js +0 -30
  296. package/distrib/npm/hashes.js +0 -42
  297. package/distrib/npm/index.js +0 -72
  298. package/distrib/npm/ledger-hashes.js +0 -71
  299. package/distrib/npm/quality.js +0 -20
  300. package/distrib/npm/serdes/binary-parser.js +0 -98
  301. package/distrib/npm/serdes/binary-serializer.js +0 -106
  302. package/distrib/npm/shamap.js +0 -107
  303. package/distrib/npm/types/account-id.js +0 -41
  304. package/distrib/npm/types/amount.js +0 -215
  305. package/distrib/npm/types/blob.js +0 -28
  306. package/distrib/npm/types/currency.js +0 -91
  307. package/distrib/npm/types/hash-128.js +0 -10
  308. package/distrib/npm/types/hash-160.js +0 -10
  309. package/distrib/npm/types/hash-256.js +0 -15
  310. package/distrib/npm/types/hash.js +0 -45
  311. package/distrib/npm/types/index.js +0 -45
  312. package/distrib/npm/types/path-set.js +0 -112
  313. package/distrib/npm/types/serialized-type.js +0 -63
  314. package/distrib/npm/types/st-array.js +0 -37
  315. package/distrib/npm/types/st-object.js +0 -65
  316. package/distrib/npm/types/uint-16.js +0 -10
  317. package/distrib/npm/types/uint-32.js +0 -10
  318. package/distrib/npm/types/uint-64.js +0 -49
  319. package/distrib/npm/types/uint-8.js +0 -10
  320. package/distrib/npm/types/uint.js +0 -60
  321. package/distrib/npm/types/vector-256.js +0 -31
  322. package/distrib/npm/utils/bytes-utils.js +0 -112
  323. package/distrib/npm/utils/make-class.js +0 -83
  324. package/test/amount-test.js +0 -43
  325. package/test/binary-json-test.js +0 -43
  326. package/test/binary-parser-test.js +0 -341
  327. package/test/binary-serializer-test.js +0 -194
  328. package/test/bytes-utils-test.js +0 -72
  329. package/test/fixtures/account-tx-transactions.db +0 -0
  330. package/test/fixtures/codec-fixtures.json +0 -4466
  331. package/test/fixtures/data-driven-tests.json +0 -3721
  332. package/test/fixtures/delivermin-tx-binary.json +0 -1
  333. package/test/fixtures/delivermin-tx.json +0 -98
  334. package/test/fixtures/deposit-preauth-tx-binary.json +0 -1
  335. package/test/fixtures/deposit-preauth-tx-meta-binary.json +0 -1
  336. package/test/fixtures/deposit-preauth-tx.json +0 -58
  337. package/test/fixtures/escrow-cancel-binary.json +0 -1
  338. package/test/fixtures/escrow-cancel-tx.json +0 -6
  339. package/test/fixtures/escrow-create-binary.json +0 -1
  340. package/test/fixtures/escrow-create-tx.json +0 -10
  341. package/test/fixtures/escrow-finish-binary.json +0 -1
  342. package/test/fixtures/escrow-finish-meta-binary.json +0 -1
  343. package/test/fixtures/escrow-finish-tx.json +0 -95
  344. package/test/fixtures/ledger-full-38129.json +0 -1
  345. package/test/fixtures/ledger-full-40000.json +0 -1
  346. package/test/fixtures/payment-channel-claim-binary.json +0 -1
  347. package/test/fixtures/payment-channel-claim-tx.json +0 -8
  348. package/test/fixtures/payment-channel-create-binary.json +0 -1
  349. package/test/fixtures/payment-channel-create-tx.json +0 -11
  350. package/test/fixtures/payment-channel-fund-binary.json +0 -1
  351. package/test/fixtures/payment-channel-fund-tx.json +0 -7
  352. package/test/fixtures/signerlistset-tx-binary.json +0 -1
  353. package/test/fixtures/signerlistset-tx-meta-binary.json +0 -1
  354. package/test/fixtures/signerlistset-tx.json +0 -94
  355. package/test/hash-test.js +0 -58
  356. package/test/ledger-test.js +0 -26
  357. package/test/mocha.opts +0 -1
  358. package/test/quality-test.js +0 -16
  359. package/test/shamap-test.js +0 -88
  360. package/test/signing-data-encoding-test.js +0 -120
  361. package/test/tx-encode-decode-test.js +0 -124
  362. package/test/types-test.js +0 -38
  363. package/test/uint-test.js +0 -38
  364. package/test/utils.js +0 -93
@@ -0,0 +1,84 @@
1
+ import { Bytes } from './bytes'
2
+ import { SerializedType } from '../types/serialized-type'
3
+ import { TYPE_WIDTH } from './constants'
4
+
5
+ /**
6
+ * Encoding information for a rippled field, often used in transactions.
7
+ * See the enums [README.md](https://github.com/XRPLF/xrpl.js/tree/main/packages/ripple-binary-codec/src/enums) for more details on what each means.
8
+ */
9
+ export interface FieldInfo {
10
+ nth: number
11
+ isVLEncoded: boolean
12
+ isSerialized: boolean
13
+ isSigningField: boolean
14
+ type: string
15
+ }
16
+
17
+ export interface FieldInstance {
18
+ readonly nth: number
19
+ readonly isVariableLengthEncoded: boolean
20
+ readonly isSerialized: boolean
21
+ readonly isSigningField: boolean
22
+ readonly type: Bytes
23
+ readonly ordinal: number
24
+ readonly name: string
25
+ readonly header: Uint8Array
26
+ readonly associatedType: typeof SerializedType
27
+ }
28
+
29
+ /*
30
+ * @brief: Serialize a field based on type_code and Field.nth
31
+ */
32
+ function fieldHeader(type: number, nth: number): Uint8Array {
33
+ const header: Array<number> = []
34
+ if (type < 16) {
35
+ if (nth < 16) {
36
+ header.push((type << 4) | nth)
37
+ } else {
38
+ header.push(type << 4, nth)
39
+ }
40
+ } else if (nth < 16) {
41
+ header.push(nth, type)
42
+ } else {
43
+ header.push(0, type, nth)
44
+ }
45
+ return Uint8Array.from(header)
46
+ }
47
+
48
+ function buildField(
49
+ [name, info]: [string, FieldInfo],
50
+ typeOrdinal: number,
51
+ ): FieldInstance {
52
+ const field = fieldHeader(typeOrdinal, info.nth)
53
+ return {
54
+ name: name,
55
+ nth: info.nth,
56
+ isVariableLengthEncoded: info.isVLEncoded,
57
+ isSerialized: info.isSerialized,
58
+ isSigningField: info.isSigningField,
59
+ ordinal: (typeOrdinal << 16) | info.nth,
60
+ type: new Bytes(info.type, typeOrdinal, TYPE_WIDTH),
61
+ header: field,
62
+ associatedType: SerializedType, // For later assignment in ./types/index.js or Definitions.updateAll(...)
63
+ }
64
+ }
65
+
66
+ /*
67
+ * @brief: The collection of all fields as defined in definitions.json
68
+ */
69
+ export class FieldLookup {
70
+ constructor(
71
+ fields: Array<[string, FieldInfo]>,
72
+ types: Record<string, number>,
73
+ ) {
74
+ fields.forEach(([name, field_info]) => {
75
+ const typeOrdinal = types[field_info.type]
76
+ this[name] = buildField([name, field_info], typeOrdinal)
77
+ this[this[name].ordinal.toString()] = this[name]
78
+ })
79
+ }
80
+
81
+ fromString(value: string): FieldInstance {
82
+ return this[value] as FieldInstance
83
+ }
84
+ }
@@ -0,0 +1,34 @@
1
+ import enums from './definitions.json'
2
+ import {
3
+ XrplDefinitionsBase,
4
+ FieldInstance,
5
+ Bytes,
6
+ } from './xrpl-definitions-base'
7
+ /**
8
+ * By default, coreTypes from the `types` folder is where known type definitions are initialized to avoid import cycles.
9
+ */
10
+ const DEFAULT_DEFINITIONS = new XrplDefinitionsBase(enums, {})
11
+
12
+ const Type = DEFAULT_DEFINITIONS.type
13
+ const LedgerEntryType = DEFAULT_DEFINITIONS.ledgerEntryType
14
+ const TransactionType = DEFAULT_DEFINITIONS.transactionType
15
+ const TransactionResult = DEFAULT_DEFINITIONS.transactionResult
16
+ const Field = DEFAULT_DEFINITIONS.field
17
+
18
+ /*
19
+ * @brief: All valid transaction types
20
+ */
21
+ const TRANSACTION_TYPES = DEFAULT_DEFINITIONS.transactionNames
22
+
23
+ export {
24
+ Bytes,
25
+ XrplDefinitionsBase,
26
+ DEFAULT_DEFINITIONS,
27
+ Field,
28
+ FieldInstance,
29
+ Type,
30
+ LedgerEntryType,
31
+ TransactionResult,
32
+ TransactionType,
33
+ TRANSACTION_TYPES,
34
+ }
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Quick script to re-number values
3
+ */
4
+
5
+ const input = {
6
+ temBAD_SEND_XRP_PATHS: -283,
7
+ temBAD_SEQUENCE: -282,
8
+ temBAD_SIGNATURE: -281,
9
+ temBAD_SRC_ACCOUNT: -280,
10
+ temBAD_TRANSFER_RATE: -279,
11
+ temDST_IS_SRC: -278,
12
+ temDST_NEEDED: -277,
13
+ temINVALID: -276,
14
+ temINVALID_FLAG: -275,
15
+ temREDUNDANT: -274,
16
+ temRIPPLE_EMPTY: -273,
17
+ temDISABLED: -272,
18
+ temBAD_SIGNER: -271,
19
+ temBAD_QUORUM: -270,
20
+ temBAD_WEIGHT: -269,
21
+ temBAD_TICK_SIZE: -268,
22
+ temINVALID_ACCOUNT_ID: -267,
23
+ temCANNOT_PREAUTH_SELF: -266,
24
+
25
+ temUNCERTAIN: -265,
26
+ temUNKNOWN: -264,
27
+
28
+ tefFAILURE: -199,
29
+ tefALREADY: -198,
30
+ tefBAD_ADD_AUTH: -197,
31
+ tefBAD_AUTH: -196,
32
+ tefBAD_LEDGER: -195,
33
+ tefCREATED: -194,
34
+ tefEXCEPTION: -193,
35
+ tefINTERNAL: -192,
36
+ tefNO_AUTH_REQUIRED: -191,
37
+ tefPAST_SEQ: -190,
38
+ tefWRONG_PRIOR: -189,
39
+ tefMASTER_DISABLED: -188,
40
+ tefMAX_LEDGER: -187,
41
+ tefBAD_SIGNATURE: -186,
42
+ tefBAD_QUORUM: -185,
43
+ tefNOT_MULTI_SIGNING: -184,
44
+ tefBAD_AUTH_MASTER: -183,
45
+ tefINVARIANT_FAILED: -182,
46
+ tefTOO_BIG: -181,
47
+
48
+ terRETRY: -99,
49
+ terFUNDS_SPENT: -98,
50
+ terINSUF_FEE_B: -97,
51
+ terNO_ACCOUNT: -96,
52
+ terNO_AUTH: -95,
53
+ terNO_LINE: -94,
54
+ terOWNERS: -93,
55
+ terPRE_SEQ: -92,
56
+ terLAST: -91,
57
+ terNO_RIPPLE: -90,
58
+ terQUEUED: -89,
59
+
60
+ tesSUCCESS: 0,
61
+
62
+ tecCLAIM: 100,
63
+ tecPATH_PARTIAL: 101,
64
+ tecUNFUNDED_ADD: 102,
65
+ tecUNFUNDED_OFFER: 103,
66
+ tecUNFUNDED_PAYMENT: 104,
67
+ tecFAILED_PROCESSING: 105,
68
+ tecDIR_FULL: 121,
69
+ tecINSUF_RESERVE_LINE: 122,
70
+ tecINSUF_RESERVE_OFFER: 123,
71
+ tecNO_DST: 124,
72
+ tecNO_DST_INSUF_XRP: 125,
73
+ tecNO_LINE_INSUF_RESERVE: 126,
74
+ tecNO_LINE_REDUNDANT: 127,
75
+ tecPATH_DRY: 128,
76
+ tecUNFUNDED: 129,
77
+ tecNO_ALTERNATIVE_KEY: 130,
78
+ tecNO_REGULAR_KEY: 131,
79
+ tecOWNERS: 132,
80
+ tecNO_ISSUER: 133,
81
+ tecNO_AUTH: 134,
82
+ tecNO_LINE: 135,
83
+ tecINSUFF_FEE: 136,
84
+ tecFROZEN: 137,
85
+ tecNO_TARGET: 138,
86
+ tecNO_PERMISSION: 139,
87
+ tecNO_ENTRY: 140,
88
+ tecINSUFFICIENT_RESERVE: 141,
89
+ tecNEED_MASTER_KEY: 142,
90
+ tecDST_TAG_NEEDED: 143,
91
+ tecINTERNAL: 144,
92
+ tecOVERSIZE: 145,
93
+ tecCRYPTOCONDITION_ERROR: 146,
94
+ tecINVARIANT_FAILED: 147,
95
+ tecEXPIRED: 148,
96
+ tecDUPLICATE: 149,
97
+ tecKILLED: 150,
98
+ tecHAS_OBLIGATIONS: 151,
99
+ tecTOO_SOON: 152,
100
+ }
101
+
102
+ let startingFromTemBADSENDXRPPATHS = -284
103
+
104
+ let startingFromTefFAILURE = -199
105
+
106
+ let startingFromTerRETRY = -99
107
+
108
+ const tesSUCCESS = 0
109
+
110
+ let startingFromTecCLAIM = 100
111
+
112
+ const startingFromTecDIRFULL = 121
113
+
114
+ let previousKey = 'tem'
115
+ Object.keys(input).forEach((key) => {
116
+ if (key.substring(0, 3) !== previousKey.substring(0, 3)) {
117
+ console.log()
118
+ previousKey = key
119
+ }
120
+ if (key.substring(0, 3) === 'tem') {
121
+ console.log(` "${key}": ${startingFromTemBADSENDXRPPATHS++},`)
122
+ } else if (key.substring(0, 3) === 'tef') {
123
+ console.log(` "${key}": ${startingFromTefFAILURE++},`)
124
+ } else if (key.substring(0, 3) === 'ter') {
125
+ console.log(` "${key}": ${startingFromTerRETRY++},`)
126
+ } else if (key.substring(0, 3) === 'tes') {
127
+ console.log(` "${key}": ${tesSUCCESS},`)
128
+ } else if (key.substring(0, 3) === 'tec') {
129
+ if (key === 'tecDIR_FULL') {
130
+ startingFromTecCLAIM = startingFromTecDIRFULL
131
+ }
132
+ console.log(` "${key}": ${startingFromTecCLAIM++},`)
133
+ }
134
+ })
@@ -0,0 +1,111 @@
1
+ import { SerializedType } from '../types/serialized-type'
2
+ import { Bytes, BytesLookup } from './bytes'
3
+ import { FieldInfo, FieldLookup, FieldInstance } from './field'
4
+ import {
5
+ TYPE_WIDTH,
6
+ LEDGER_ENTRY_WIDTH,
7
+ TRANSACTION_TYPE_WIDTH,
8
+ TRANSACTION_RESULT_WIDTH,
9
+ } from './constants'
10
+
11
+ interface DefinitionsData {
12
+ TYPES: Record<string, number>
13
+ LEDGER_ENTRY_TYPES: Record<string, number>
14
+ FIELDS: (string | FieldInfo)[][]
15
+ TRANSACTION_RESULTS: Record<string, number>
16
+ TRANSACTION_TYPES: Record<string, number>
17
+ }
18
+
19
+ /**
20
+ * Stores the various types and fields for rippled to be used to encode/decode information later on.
21
+ * XrplDefinitions should be instantiated instead of this class.
22
+ */
23
+ class XrplDefinitionsBase {
24
+ // A collection of fields that can be included in transactions
25
+ field: FieldLookup
26
+ // A collection of ids corresponding to types of ledger objects
27
+ ledgerEntryType: BytesLookup
28
+ // A collection of type flags used to determine how to serialize a field's data
29
+ type: BytesLookup
30
+ // Errors and result codes for transactions
31
+ transactionResult: BytesLookup
32
+ // Defined transactions that can be submitted to the ledger
33
+ transactionType: BytesLookup
34
+ // Valid transaction names
35
+ transactionNames: string[]
36
+ // Maps serializable types to their TypeScript class implementation
37
+ dataTypes: Record<string, typeof SerializedType>
38
+
39
+ /**
40
+ * Present rippled types in a typed and updatable format.
41
+ * For an example of the input format see `definitions.json`
42
+ * To generate a new definitions file from rippled source code, use this tool: https://github.com/RichardAH/xrpl-codec-gen
43
+ *
44
+ * See the definitions.test.js file for examples of how to create your own updated definitions.json.
45
+ *
46
+ * @param enums - A json encoding of the core types, transaction types, transaction results, transaction names, and fields.
47
+ * @param types - A list of type objects with the same name as the fields defined.
48
+ * You can use the coreTypes object if you are not adding new types.
49
+ */
50
+ constructor(
51
+ enums: DefinitionsData,
52
+ types: Record<string, typeof SerializedType>,
53
+ ) {
54
+ this.type = new BytesLookup(enums.TYPES, TYPE_WIDTH)
55
+ this.ledgerEntryType = new BytesLookup(
56
+ enums.LEDGER_ENTRY_TYPES,
57
+ LEDGER_ENTRY_WIDTH,
58
+ )
59
+ this.transactionType = new BytesLookup(
60
+ enums.TRANSACTION_TYPES,
61
+ TRANSACTION_TYPE_WIDTH,
62
+ )
63
+ this.transactionResult = new BytesLookup(
64
+ enums.TRANSACTION_RESULTS,
65
+ TRANSACTION_RESULT_WIDTH,
66
+ )
67
+ this.field = new FieldLookup(
68
+ enums.FIELDS as Array<[string, FieldInfo]>,
69
+ enums.TYPES,
70
+ )
71
+ this.transactionNames = Object.entries(enums.TRANSACTION_TYPES)
72
+ .filter(([_key, value]) => value >= 0)
73
+ .map(([key, _value]) => key)
74
+
75
+ this.dataTypes = {} // Filled in via associateTypes
76
+ this.associateTypes(types)
77
+ }
78
+
79
+ /**
80
+ * Associates each Field to a corresponding class that TypeScript can recognize.
81
+ *
82
+ * @param types a list of type objects with the same name as the fields defined.
83
+ * Defaults to xrpl.js's core type definitions.
84
+ */
85
+ public associateTypes(types: Record<string, typeof SerializedType>): void {
86
+ // Overwrite any existing type definitions with the given types
87
+ this.dataTypes = Object.assign({}, this.dataTypes, types)
88
+
89
+ Object.values(this.field).forEach((field) => {
90
+ field.associatedType = this.dataTypes[field.type.name]
91
+ })
92
+
93
+ this.field['TransactionType'].associatedType = this.transactionType
94
+ this.field['TransactionResult'].associatedType = this.transactionResult
95
+ this.field['LedgerEntryType'].associatedType = this.ledgerEntryType
96
+ }
97
+
98
+ public getAssociatedTypes(): Record<string, typeof SerializedType> {
99
+ return this.dataTypes
100
+ }
101
+ }
102
+
103
+ export {
104
+ DefinitionsData,
105
+ XrplDefinitionsBase,
106
+ FieldLookup,
107
+ FieldInfo,
108
+ FieldInstance,
109
+ Bytes,
110
+ BytesLookup,
111
+ }
@@ -0,0 +1,32 @@
1
+ import {
2
+ type DefinitionsData,
3
+ XrplDefinitionsBase,
4
+ } from './xrpl-definitions-base'
5
+ import { coreTypes } from '../types'
6
+ import { SerializedType } from '../types/serialized-type'
7
+
8
+ /**
9
+ * Stores the various types and fields for rippled to be used to encode/decode information later on.
10
+ * Should be used instead of XrplDefinitionsBase since this defines default `types` for serializing/deserializing
11
+ * ledger data.
12
+ */
13
+ export class XrplDefinitions extends XrplDefinitionsBase {
14
+ /**
15
+ * Present rippled types in a typed and updatable format.
16
+ * For an example of the input format see `definitions.json`
17
+ * To generate a new definitions file from rippled source code, use this tool: https://github.com/RichardAH/xrpl-codec-gen
18
+ *
19
+ * See the definitions.test.js file for examples of how to create your own updated definitions.json.
20
+ *
21
+ * @param enums - A json encoding of the core types, transaction types, transaction results, transaction names, and fields.
22
+ * @param additionalTypes - A list of SerializedType objects with the same name as the fields defined.
23
+ * These types will be included in addition to the coreTypes used on mainnet.
24
+ */
25
+ constructor(
26
+ enums: DefinitionsData,
27
+ additionalTypes?: Record<string, typeof SerializedType>,
28
+ ) {
29
+ const types = Object.assign({}, coreTypes, additionalTypes)
30
+ super(enums, types)
31
+ }
32
+ }
@@ -0,0 +1,40 @@
1
+ import { writeUInt32BE } from './utils'
2
+
3
+ /**
4
+ * Write a 32 bit integer to a Uint8Array
5
+ *
6
+ * @param uint32 32 bit integer to write to Uint8Array
7
+ * @returns a Uint8Array with the bytes representation of uint32
8
+ */
9
+ function bytes(uint32: number): Uint8Array {
10
+ const result = new Uint8Array(4)
11
+ writeUInt32BE(result, uint32, 0)
12
+ return result
13
+ }
14
+
15
+ /**
16
+ * Maps HashPrefix names to their byte representation
17
+ */
18
+ const HashPrefix: Record<string, Uint8Array> = {
19
+ transactionID: bytes(0x54584e00),
20
+ // transaction plus metadata
21
+ transaction: bytes(0x534e4400),
22
+ // account state
23
+ accountStateEntry: bytes(0x4d4c4e00),
24
+ // inner node in tree
25
+ innerNode: bytes(0x4d494e00),
26
+ // ledger master data for signing
27
+ ledgerHeader: bytes(0x4c575200),
28
+ // inner transaction to sign
29
+ transactionSig: bytes(0x53545800),
30
+ // inner transaction to sign
31
+ transactionMultiSig: bytes(0x534d5400),
32
+ // validation for signing
33
+ validation: bytes(0x56414c00),
34
+ // proposal for signing
35
+ proposal: bytes(0x50525000),
36
+ // payment channel claim
37
+ paymentChannelClaim: bytes(0x434c4d00),
38
+ }
39
+
40
+ export { HashPrefix }
package/src/hashes.ts ADDED
@@ -0,0 +1,75 @@
1
+ import { HashPrefix } from './hash-prefixes'
2
+ import { Hash256 } from './types'
3
+ import { BytesList } from './serdes/binary-serializer'
4
+ import { sha512 } from '@xrplf/isomorphic/sha512'
5
+
6
+ /**
7
+ * Class for hashing with SHA512
8
+ * @extends BytesList So SerializedTypes can write bytes to a Sha512Half
9
+ */
10
+ class Sha512Half extends BytesList {
11
+ private hash = sha512.create()
12
+
13
+ /**
14
+ * Construct a new Sha512Hash and write bytes this.hash
15
+ *
16
+ * @param bytes bytes to write to this.hash
17
+ * @returns the new Sha512Hash object
18
+ */
19
+ static put(bytes: Uint8Array): Sha512Half {
20
+ return new Sha512Half().put(bytes)
21
+ }
22
+
23
+ /**
24
+ * Write bytes to an existing Sha512Hash
25
+ *
26
+ * @param bytes bytes to write to object
27
+ * @returns the Sha512 object
28
+ */
29
+ put(bytes: Uint8Array): Sha512Half {
30
+ this.hash.update(bytes)
31
+ return this
32
+ }
33
+
34
+ /**
35
+ * Compute SHA512 hash and slice in half
36
+ *
37
+ * @returns half of a SHA512 hash
38
+ */
39
+ finish256(): Uint8Array {
40
+ return Uint8Array.from(this.hash.digest().slice(0, 32))
41
+ }
42
+
43
+ /**
44
+ * Constructs a Hash256 from the Sha512Half object
45
+ *
46
+ * @returns a Hash256 object
47
+ */
48
+ finish(): Hash256 {
49
+ return new Hash256(this.finish256())
50
+ }
51
+ }
52
+
53
+ /**
54
+ * compute SHA512 hash of a list of bytes
55
+ *
56
+ * @param args zero or more arguments to hash
57
+ * @returns the sha512half hash of the arguments.
58
+ */
59
+ function sha512Half(...args: Uint8Array[]): Uint8Array {
60
+ const hash = new Sha512Half()
61
+ args.forEach((a) => hash.put(a))
62
+ return hash.finish256()
63
+ }
64
+
65
+ /**
66
+ * Construct a transactionID from a Serialized Transaction
67
+ *
68
+ * @param serialized bytes to hash
69
+ * @returns a Hash256 object
70
+ */
71
+ function transactionID(serialized: Uint8Array): Hash256 {
72
+ return new Hash256(sha512Half(HashPrefix.transactionID, serialized))
73
+ }
74
+
75
+ export { Sha512Half, sha512Half, transactionID }
package/src/index.ts ADDED
@@ -0,0 +1,153 @@
1
+ import { quality, binary, HashPrefix } from './coretypes'
2
+ import { decodeLedgerData } from './ledger-hashes'
3
+ import { ClaimObject } from './binary'
4
+ import { JsonObject } from './types/serialized-type'
5
+ import {
6
+ XrplDefinitionsBase,
7
+ TRANSACTION_TYPES,
8
+ DEFAULT_DEFINITIONS,
9
+ } from './enums'
10
+ import { XrplDefinitions } from './enums/xrpl-definitions'
11
+ import { coreTypes } from './types'
12
+ import { bytesToHex } from '@xrplf/isomorphic/utils'
13
+
14
+ const {
15
+ signingData,
16
+ signingClaimData,
17
+ multiSigningData,
18
+ binaryToJSON,
19
+ serializeObject,
20
+ } = binary
21
+
22
+ /**
23
+ * Decode a transaction
24
+ *
25
+ * @param binary hex-string of the encoded transaction
26
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
27
+ * @returns the JSON representation of the transaction
28
+ */
29
+ function decode(binary: string, definitions?: XrplDefinitionsBase): JsonObject {
30
+ if (typeof binary !== 'string') {
31
+ throw new Error('binary must be a hex string')
32
+ }
33
+ return binaryToJSON(binary, definitions)
34
+ }
35
+
36
+ /**
37
+ * Encode a transaction
38
+ *
39
+ * @param json The JSON representation of a transaction
40
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
41
+ *
42
+ * @returns A hex-string of the encoded transaction
43
+ */
44
+ function encode(json: object, definitions?: XrplDefinitionsBase): string {
45
+ if (typeof json !== 'object') {
46
+ throw new Error()
47
+ }
48
+ return bytesToHex(serializeObject(json as JsonObject, { definitions }))
49
+ }
50
+
51
+ /**
52
+ * Encode a transaction and prepare for signing
53
+ *
54
+ * @param json JSON object representing the transaction
55
+ * @param signer string representing the account to sign the transaction with
56
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
57
+ * @returns a hex string of the encoded transaction
58
+ */
59
+ function encodeForSigning(
60
+ json: object,
61
+ definitions?: XrplDefinitionsBase,
62
+ ): string {
63
+ if (typeof json !== 'object') {
64
+ throw new Error()
65
+ }
66
+ return bytesToHex(
67
+ signingData(json as JsonObject, HashPrefix.transactionSig, {
68
+ definitions,
69
+ }),
70
+ )
71
+ }
72
+
73
+ /**
74
+ * Encode a transaction and prepare for signing with a claim
75
+ *
76
+ * @param json JSON object representing the transaction
77
+ * @param signer string representing the account to sign the transaction with
78
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
79
+ * @returns a hex string of the encoded transaction
80
+ */
81
+ function encodeForSigningClaim(json: object): string {
82
+ if (typeof json !== 'object') {
83
+ throw new Error()
84
+ }
85
+ return bytesToHex(signingClaimData(json as ClaimObject))
86
+ }
87
+
88
+ /**
89
+ * Encode a transaction and prepare for multi-signing
90
+ *
91
+ * @param json JSON object representing the transaction
92
+ * @param signer string representing the account to sign the transaction with
93
+ * @param definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
94
+ * @returns a hex string of the encoded transaction
95
+ */
96
+ function encodeForMultisigning(
97
+ json: object,
98
+ signer: string,
99
+ definitions?: XrplDefinitionsBase,
100
+ ): string {
101
+ if (typeof json !== 'object') {
102
+ throw new Error()
103
+ }
104
+ if (json['SigningPubKey'] !== '') {
105
+ throw new Error()
106
+ }
107
+ const definitionsOpt = definitions ? { definitions } : undefined
108
+ return bytesToHex(
109
+ multiSigningData(json as JsonObject, signer, definitionsOpt),
110
+ )
111
+ }
112
+
113
+ /**
114
+ * Encode a quality value
115
+ *
116
+ * @param value string representation of a number
117
+ * @returns a hex-string representing the quality
118
+ */
119
+ function encodeQuality(value: string): string {
120
+ if (typeof value !== 'string') {
121
+ throw new Error()
122
+ }
123
+ return bytesToHex(quality.encode(value))
124
+ }
125
+
126
+ /**
127
+ * Decode a quality value
128
+ *
129
+ * @param value hex-string of a quality
130
+ * @returns a string representing the quality
131
+ */
132
+ function decodeQuality(value: string): string {
133
+ if (typeof value !== 'string') {
134
+ throw new Error()
135
+ }
136
+ return quality.decode(value).toString()
137
+ }
138
+
139
+ export {
140
+ decode,
141
+ encode,
142
+ encodeForSigning,
143
+ encodeForSigningClaim,
144
+ encodeForMultisigning,
145
+ encodeQuality,
146
+ decodeQuality,
147
+ decodeLedgerData,
148
+ TRANSACTION_TYPES,
149
+ XrplDefinitions,
150
+ XrplDefinitionsBase,
151
+ DEFAULT_DEFINITIONS,
152
+ coreTypes,
153
+ }