xrpl 2.10.0 → 2.11.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 (301) hide show
  1. package/build/xrpl-latest-min.js +1 -1
  2. package/build/xrpl-latest-min.js.map +1 -1
  3. package/build/xrpl-latest.js +728 -28
  4. package/build/xrpl-latest.js.map +1 -1
  5. package/dist/npm/Wallet/walletFromSecretNumbers.d.ts +7 -0
  6. package/dist/npm/Wallet/walletFromSecretNumbers.d.ts.map +1 -0
  7. package/dist/npm/Wallet/walletFromSecretNumbers.js +27 -0
  8. package/dist/npm/Wallet/walletFromSecretNumbers.js.map +1 -0
  9. package/dist/npm/client/index.d.ts +2 -1
  10. package/dist/npm/client/index.d.ts.map +1 -1
  11. package/dist/npm/client/index.js.map +1 -1
  12. package/dist/npm/index.d.ts +1 -0
  13. package/dist/npm/index.d.ts.map +1 -1
  14. package/dist/npm/index.js +3 -1
  15. package/dist/npm/index.js.map +1 -1
  16. package/dist/npm/models/common/index.d.ts +5 -0
  17. package/dist/npm/models/common/index.d.ts.map +1 -1
  18. package/dist/npm/models/ledger/AMM.d.ts +27 -0
  19. package/dist/npm/models/ledger/AMM.d.ts.map +1 -0
  20. package/dist/npm/models/ledger/AMM.js +3 -0
  21. package/dist/npm/models/ledger/AMM.js.map +1 -0
  22. package/dist/npm/models/ledger/AccountRoot.d.ts +2 -0
  23. package/dist/npm/models/ledger/AccountRoot.d.ts.map +1 -1
  24. package/dist/npm/models/ledger/AccountRoot.js +1 -0
  25. package/dist/npm/models/ledger/AccountRoot.js.map +1 -1
  26. package/dist/npm/models/ledger/LedgerEntry.d.ts +2 -1
  27. package/dist/npm/models/ledger/LedgerEntry.d.ts.map +1 -1
  28. package/dist/npm/models/methods/ammInfo.d.ts +39 -0
  29. package/dist/npm/models/methods/ammInfo.d.ts.map +1 -0
  30. package/dist/npm/models/methods/ammInfo.js +3 -0
  31. package/dist/npm/models/methods/ammInfo.js.map +1 -0
  32. package/dist/npm/models/methods/index.d.ts +4 -3
  33. package/dist/npm/models/methods/index.d.ts.map +1 -1
  34. package/dist/npm/models/methods/ledgerEntry.d.ts +10 -0
  35. package/dist/npm/models/methods/ledgerEntry.d.ts.map +1 -1
  36. package/dist/npm/models/transactions/AMMBid.d.ts +12 -0
  37. package/dist/npm/models/transactions/AMMBid.d.ts.map +1 -0
  38. package/dist/npm/models/transactions/AMMBid.js +56 -0
  39. package/dist/npm/models/transactions/AMMBid.js.map +1 -0
  40. package/dist/npm/models/transactions/AMMCreate.d.ts +11 -0
  41. package/dist/npm/models/transactions/AMMCreate.d.ts.map +1 -0
  42. package/dist/npm/models/transactions/AMMCreate.js +32 -0
  43. package/dist/npm/models/transactions/AMMCreate.js.map +1 -0
  44. package/dist/npm/models/transactions/AMMDelete.d.ts +9 -0
  45. package/dist/npm/models/transactions/AMMDelete.d.ts.map +1 -0
  46. package/dist/npm/models/transactions/AMMDelete.js +22 -0
  47. package/dist/npm/models/transactions/AMMDelete.js.map +1 -0
  48. package/dist/npm/models/transactions/AMMDeposit.d.ts +27 -0
  49. package/dist/npm/models/transactions/AMMDeposit.d.ts.map +1 -0
  50. package/dist/npm/models/transactions/AMMDeposit.js +51 -0
  51. package/dist/npm/models/transactions/AMMDeposit.js.map +1 -0
  52. package/dist/npm/models/transactions/AMMVote.d.ts +10 -0
  53. package/dist/npm/models/transactions/AMMVote.d.ts.map +1 -0
  54. package/dist/npm/models/transactions/AMMVote.js +32 -0
  55. package/dist/npm/models/transactions/AMMVote.js.map +1 -0
  56. package/dist/npm/models/transactions/AMMWithdraw.d.ts +31 -0
  57. package/dist/npm/models/transactions/AMMWithdraw.d.ts.map +1 -0
  58. package/dist/npm/models/transactions/AMMWithdraw.js +50 -0
  59. package/dist/npm/models/transactions/AMMWithdraw.js.map +1 -0
  60. package/dist/npm/models/transactions/common.d.ts +2 -1
  61. package/dist/npm/models/transactions/common.d.ts.map +1 -1
  62. package/dist/npm/models/transactions/common.js +12 -1
  63. package/dist/npm/models/transactions/common.js.map +1 -1
  64. package/dist/npm/models/transactions/index.d.ts +6 -0
  65. package/dist/npm/models/transactions/index.d.ts.map +1 -1
  66. package/dist/npm/models/transactions/index.js +5 -1
  67. package/dist/npm/models/transactions/index.js.map +1 -1
  68. package/dist/npm/models/transactions/transaction.d.ts +7 -1
  69. package/dist/npm/models/transactions/transaction.d.ts.map +1 -1
  70. package/dist/npm/models/transactions/transaction.js +24 -0
  71. package/dist/npm/models/transactions/transaction.js.map +1 -1
  72. package/dist/npm/models/utils/flags.d.ts.map +1 -1
  73. package/dist/npm/models/utils/flags.js +14 -21
  74. package/dist/npm/models/utils/flags.js.map +1 -1
  75. package/dist/npm/snippets/tsconfig.tsbuildinfo +1 -1
  76. package/dist/npm/src/Wallet/walletFromSecretNumbers.d.ts +7 -0
  77. package/dist/npm/src/Wallet/walletFromSecretNumbers.d.ts.map +1 -0
  78. package/dist/npm/src/Wallet/walletFromSecretNumbers.js +27 -0
  79. package/dist/npm/src/Wallet/walletFromSecretNumbers.js.map +1 -0
  80. package/dist/npm/src/client/index.d.ts +2 -1
  81. package/dist/npm/src/client/index.d.ts.map +1 -1
  82. package/dist/npm/src/client/index.js.map +1 -1
  83. package/dist/npm/src/index.d.ts +1 -0
  84. package/dist/npm/src/index.d.ts.map +1 -1
  85. package/dist/npm/src/index.js +3 -1
  86. package/dist/npm/src/index.js.map +1 -1
  87. package/dist/npm/src/models/common/index.d.ts +5 -0
  88. package/dist/npm/src/models/common/index.d.ts.map +1 -1
  89. package/dist/npm/src/models/ledger/AMM.d.ts +27 -0
  90. package/dist/npm/src/models/ledger/AMM.d.ts.map +1 -0
  91. package/dist/npm/src/models/ledger/AMM.js +3 -0
  92. package/dist/npm/src/models/ledger/AMM.js.map +1 -0
  93. package/dist/npm/src/models/ledger/AccountRoot.d.ts +2 -0
  94. package/dist/npm/src/models/ledger/AccountRoot.d.ts.map +1 -1
  95. package/dist/npm/src/models/ledger/AccountRoot.js +1 -0
  96. package/dist/npm/src/models/ledger/AccountRoot.js.map +1 -1
  97. package/dist/npm/src/models/ledger/LedgerEntry.d.ts +2 -1
  98. package/dist/npm/src/models/ledger/LedgerEntry.d.ts.map +1 -1
  99. package/dist/npm/src/models/methods/ammInfo.d.ts +39 -0
  100. package/dist/npm/src/models/methods/ammInfo.d.ts.map +1 -0
  101. package/dist/npm/src/models/methods/ammInfo.js +3 -0
  102. package/dist/npm/src/models/methods/ammInfo.js.map +1 -0
  103. package/dist/npm/src/models/methods/index.d.ts +4 -3
  104. package/dist/npm/src/models/methods/index.d.ts.map +1 -1
  105. package/dist/npm/src/models/methods/ledgerEntry.d.ts +10 -0
  106. package/dist/npm/src/models/methods/ledgerEntry.d.ts.map +1 -1
  107. package/dist/npm/src/models/transactions/AMMBid.d.ts +12 -0
  108. package/dist/npm/src/models/transactions/AMMBid.d.ts.map +1 -0
  109. package/dist/npm/src/models/transactions/AMMBid.js +56 -0
  110. package/dist/npm/src/models/transactions/AMMBid.js.map +1 -0
  111. package/dist/npm/src/models/transactions/AMMCreate.d.ts +11 -0
  112. package/dist/npm/src/models/transactions/AMMCreate.d.ts.map +1 -0
  113. package/dist/npm/src/models/transactions/AMMCreate.js +32 -0
  114. package/dist/npm/src/models/transactions/AMMCreate.js.map +1 -0
  115. package/dist/npm/src/models/transactions/AMMDelete.d.ts +9 -0
  116. package/dist/npm/src/models/transactions/AMMDelete.d.ts.map +1 -0
  117. package/dist/npm/src/models/transactions/AMMDelete.js +22 -0
  118. package/dist/npm/src/models/transactions/AMMDelete.js.map +1 -0
  119. package/dist/npm/src/models/transactions/AMMDeposit.d.ts +27 -0
  120. package/dist/npm/src/models/transactions/AMMDeposit.d.ts.map +1 -0
  121. package/dist/npm/src/models/transactions/AMMDeposit.js +51 -0
  122. package/dist/npm/src/models/transactions/AMMDeposit.js.map +1 -0
  123. package/dist/npm/src/models/transactions/AMMVote.d.ts +10 -0
  124. package/dist/npm/src/models/transactions/AMMVote.d.ts.map +1 -0
  125. package/dist/npm/src/models/transactions/AMMVote.js +32 -0
  126. package/dist/npm/src/models/transactions/AMMVote.js.map +1 -0
  127. package/dist/npm/src/models/transactions/AMMWithdraw.d.ts +31 -0
  128. package/dist/npm/src/models/transactions/AMMWithdraw.d.ts.map +1 -0
  129. package/dist/npm/src/models/transactions/AMMWithdraw.js +50 -0
  130. package/dist/npm/src/models/transactions/AMMWithdraw.js.map +1 -0
  131. package/dist/npm/src/models/transactions/common.d.ts +2 -1
  132. package/dist/npm/src/models/transactions/common.d.ts.map +1 -1
  133. package/dist/npm/src/models/transactions/common.js +12 -1
  134. package/dist/npm/src/models/transactions/common.js.map +1 -1
  135. package/dist/npm/src/models/transactions/index.d.ts +6 -0
  136. package/dist/npm/src/models/transactions/index.d.ts.map +1 -1
  137. package/dist/npm/src/models/transactions/index.js +5 -1
  138. package/dist/npm/src/models/transactions/index.js.map +1 -1
  139. package/dist/npm/src/models/transactions/transaction.d.ts +7 -1
  140. package/dist/npm/src/models/transactions/transaction.d.ts.map +1 -1
  141. package/dist/npm/src/models/transactions/transaction.js +24 -0
  142. package/dist/npm/src/models/transactions/transaction.js.map +1 -1
  143. package/dist/npm/src/models/utils/flags.d.ts.map +1 -1
  144. package/dist/npm/src/models/utils/flags.js +14 -21
  145. package/dist/npm/src/models/utils/flags.js.map +1 -1
  146. package/dist/npm/src/sugar/autofill.d.ts.map +1 -1
  147. package/dist/npm/src/sugar/autofill.js +4 -3
  148. package/dist/npm/src/sugar/autofill.js.map +1 -1
  149. package/dist/npm/sugar/autofill.d.ts.map +1 -1
  150. package/dist/npm/sugar/autofill.js +4 -3
  151. package/dist/npm/sugar/autofill.js.map +1 -1
  152. package/package.json +8 -6
  153. package/src/ECDSA.ts +6 -0
  154. package/src/Wallet/defaultFaucets.ts +82 -0
  155. package/src/Wallet/fundWallet.ts +344 -0
  156. package/src/Wallet/index.ts +504 -0
  157. package/src/Wallet/rfc1751.ts +190 -0
  158. package/src/Wallet/rfc1751Words.json +243 -0
  159. package/src/Wallet/signer.ts +173 -0
  160. package/src/Wallet/walletFromSecretNumbers.ts +37 -0
  161. package/src/client/BroadcastClient.ts +84 -0
  162. package/src/client/ConnectionManager.ts +40 -0
  163. package/src/client/ExponentialBackoff.ts +71 -0
  164. package/src/client/RequestManager.ts +194 -0
  165. package/src/client/WSWrapper.ts +106 -0
  166. package/src/client/connection.ts +593 -0
  167. package/src/client/index.ts +712 -0
  168. package/src/client/partialPayment.ts +153 -0
  169. package/src/errors.ts +161 -0
  170. package/src/index.ts +20 -0
  171. package/src/models/common/index.ts +149 -0
  172. package/src/models/index.ts +16 -0
  173. package/src/models/ledger/AMM.ts +78 -0
  174. package/src/models/ledger/AccountRoot.ts +217 -0
  175. package/src/models/ledger/Amendments.ts +45 -0
  176. package/src/models/ledger/BaseLedgerEntry.ts +3 -0
  177. package/src/models/ledger/Check.ts +70 -0
  178. package/src/models/ledger/DepositPreauth.ts +35 -0
  179. package/src/models/ledger/DirectoryNode.ts +46 -0
  180. package/src/models/ledger/Escrow.ts +74 -0
  181. package/src/models/ledger/FeeSettings.ts +52 -0
  182. package/src/models/ledger/Ledger.ts +65 -0
  183. package/src/models/ledger/LedgerEntry.ts +34 -0
  184. package/src/models/ledger/LedgerHashes.ts +24 -0
  185. package/src/models/ledger/NFTokenOffer.ts +16 -0
  186. package/src/models/ledger/NFTokenPage.ts +20 -0
  187. package/src/models/ledger/NegativeUNL.ts +34 -0
  188. package/src/models/ledger/Offer.ts +52 -0
  189. package/src/models/ledger/PayChannel.ts +107 -0
  190. package/src/models/ledger/RippleState.ts +88 -0
  191. package/src/models/ledger/SignerList.ts +56 -0
  192. package/src/models/ledger/Ticket.ts +36 -0
  193. package/src/models/ledger/index.ts +58 -0
  194. package/src/models/methods/accountChannels.ts +93 -0
  195. package/src/models/methods/accountCurrencies.ts +45 -0
  196. package/src/models/methods/accountInfo.ts +182 -0
  197. package/src/models/methods/accountLines.ts +137 -0
  198. package/src/models/methods/accountNFTs.ts +72 -0
  199. package/src/models/methods/accountObjects.ts +120 -0
  200. package/src/models/methods/accountOffers.ts +100 -0
  201. package/src/models/methods/accountTx.ts +109 -0
  202. package/src/models/methods/ammInfo.ts +145 -0
  203. package/src/models/methods/baseMethod.ts +58 -0
  204. package/src/models/methods/bookOffers.ts +96 -0
  205. package/src/models/methods/channelVerify.ts +41 -0
  206. package/src/models/methods/depositAuthorized.ts +56 -0
  207. package/src/models/methods/fee.ts +91 -0
  208. package/src/models/methods/gatewayBalances.ts +85 -0
  209. package/src/models/methods/index.ts +391 -0
  210. package/src/models/methods/ledger.ts +125 -0
  211. package/src/models/methods/ledgerClosed.ts +32 -0
  212. package/src/models/methods/ledgerCurrent.ts +31 -0
  213. package/src/models/methods/ledgerData.ts +78 -0
  214. package/src/models/methods/ledgerEntry.ts +177 -0
  215. package/src/models/methods/manifest.ts +54 -0
  216. package/src/models/methods/nftBuyOffers.ts +37 -0
  217. package/src/models/methods/nftHistory.ts +113 -0
  218. package/src/models/methods/nftInfo.ts +25 -0
  219. package/src/models/methods/nftSellOffers.ts +37 -0
  220. package/src/models/methods/norippleCheck.ts +82 -0
  221. package/src/models/methods/pathFind.ts +116 -0
  222. package/src/models/methods/ping.ts +21 -0
  223. package/src/models/methods/random.ts +23 -0
  224. package/src/models/methods/ripplePathFind.ts +81 -0
  225. package/src/models/methods/serverInfo.ts +257 -0
  226. package/src/models/methods/serverState.ts +77 -0
  227. package/src/models/methods/submit.ts +94 -0
  228. package/src/models/methods/submitMultisigned.ts +51 -0
  229. package/src/models/methods/subscribe.ts +435 -0
  230. package/src/models/methods/transactionEntry.ts +47 -0
  231. package/src/models/methods/tx.ts +69 -0
  232. package/src/models/methods/unsubscribe.ts +49 -0
  233. package/src/models/transactions/AMMBid.ts +140 -0
  234. package/src/models/transactions/AMMCreate.ts +80 -0
  235. package/src/models/transactions/AMMDelete.ts +55 -0
  236. package/src/models/transactions/AMMDeposit.ts +130 -0
  237. package/src/models/transactions/AMMVote.ts +71 -0
  238. package/src/models/transactions/AMMWithdraw.ts +126 -0
  239. package/src/models/transactions/NFTokenAcceptOffer.ts +104 -0
  240. package/src/models/transactions/NFTokenBurn.ts +48 -0
  241. package/src/models/transactions/NFTokenCancelOffer.ts +45 -0
  242. package/src/models/transactions/NFTokenCreateOffer.ts +145 -0
  243. package/src/models/transactions/NFTokenMint.ts +123 -0
  244. package/src/models/transactions/UNLModify.ts +20 -0
  245. package/src/models/transactions/accountDelete.ts +50 -0
  246. package/src/models/transactions/accountSet.ts +228 -0
  247. package/src/models/transactions/checkCancel.ts +34 -0
  248. package/src/models/transactions/checkCash.ts +73 -0
  249. package/src/models/transactions/checkCreate.ts +90 -0
  250. package/src/models/transactions/clawback.ts +49 -0
  251. package/src/models/transactions/common.ts +295 -0
  252. package/src/models/transactions/depositPreauth.ts +68 -0
  253. package/src/models/transactions/enableAmendment.ts +26 -0
  254. package/src/models/transactions/escrowCancel.ts +45 -0
  255. package/src/models/transactions/escrowCreate.ts +100 -0
  256. package/src/models/transactions/escrowFinish.ts +63 -0
  257. package/src/models/transactions/index.ts +66 -0
  258. package/src/models/transactions/metadata.ts +69 -0
  259. package/src/models/transactions/offerCancel.ts +37 -0
  260. package/src/models/transactions/offerCreate.ts +144 -0
  261. package/src/models/transactions/payment.ts +278 -0
  262. package/src/models/transactions/paymentChannelClaim.ts +165 -0
  263. package/src/models/transactions/paymentChannelCreate.ts +116 -0
  264. package/src/models/transactions/paymentChannelFund.ts +65 -0
  265. package/src/models/transactions/setFee.ts +48 -0
  266. package/src/models/transactions/setRegularKey.ts +33 -0
  267. package/src/models/transactions/signerListSet.ts +89 -0
  268. package/src/models/transactions/ticketCreate.ts +50 -0
  269. package/src/models/transactions/transaction.ts +303 -0
  270. package/src/models/transactions/trustSet.ts +146 -0
  271. package/src/models/utils/flags.ts +98 -0
  272. package/src/models/utils/index.ts +37 -0
  273. package/src/sugar/autofill.ts +373 -0
  274. package/src/sugar/balances.ts +123 -0
  275. package/src/sugar/getFeeXrp.ts +45 -0
  276. package/src/sugar/getLedgerIndex.ts +15 -0
  277. package/src/sugar/getOrderbook.ts +152 -0
  278. package/src/sugar/index.ts +11 -0
  279. package/src/sugar/submit.ts +305 -0
  280. package/src/sugar/utils.ts +29 -0
  281. package/src/utils/derive.ts +23 -0
  282. package/src/utils/getBalanceChanges.ts +186 -0
  283. package/src/utils/getNFTokenID.ts +97 -0
  284. package/src/utils/hashes/HashPrefix.ts +40 -0
  285. package/src/utils/hashes/README.md +65 -0
  286. package/src/utils/hashes/SHAMap/InnerNode.ts +124 -0
  287. package/src/utils/hashes/SHAMap/LeafNode.ts +69 -0
  288. package/src/utils/hashes/SHAMap/index.ts +41 -0
  289. package/src/utils/hashes/SHAMap/node.ts +14 -0
  290. package/src/utils/hashes/hashLedger.ts +236 -0
  291. package/src/utils/hashes/index.ts +187 -0
  292. package/src/utils/hashes/ledgerSpaces.ts +34 -0
  293. package/src/utils/hashes/sha512Half.ts +19 -0
  294. package/src/utils/index.ts +223 -0
  295. package/src/utils/parseNFTokenID.ts +84 -0
  296. package/src/utils/quality.ts +169 -0
  297. package/src/utils/signPaymentChannelClaim.ts +27 -0
  298. package/src/utils/stringConversion.ts +27 -0
  299. package/src/utils/timeConversion.ts +53 -0
  300. package/src/utils/verifyPaymentChannelClaim.ts +30 -0
  301. package/src/utils/xrpConversion.ts +104 -0
@@ -0,0 +1,84 @@
1
+ /* eslint-disable @typescript-eslint/no-magic-numbers -- Doing hex string parsing. */
2
+ import BigNumber from 'bignumber.js'
3
+ import { encodeAccountID } from 'ripple-address-codec'
4
+
5
+ import { XrplError } from '../errors'
6
+
7
+ /**
8
+ * An issuer may issue several NFTs with the same taxon; to ensure that NFTs are
9
+ * spread across multiple pages we lightly mix the taxon up by using the sequence
10
+ * (which is not under the issuer's direct control) as the seed for a simple linear
11
+ * congruential generator.
12
+ *
13
+ * From the Hull-Dobell theorem we know that f(x)=(m*x+c) mod n will yield a
14
+ * permutation of [0, n) when n is a power of 2 if m is congruent to 1 mod 4 and
15
+ * c is odd. By doing a bitwise XOR with this permutation we can scramble/unscramble
16
+ * the taxon.
17
+ *
18
+ * The XLS-20d proposal fixes m = 384160001 and c = 2459.
19
+ * We then take the modulus of 2^32 which is 4294967296.
20
+ *
21
+ * @param taxon - The scrambled or unscrambled taxon (The XOR is both the encoding and decoding)
22
+ * @param tokenSeq - The account sequence when the token was minted. Used as a psuedorandom seed.
23
+ * @returns the opposite taxon. If the taxon was scrambled it becomes unscrambled, and vice versa.
24
+ */
25
+ function unscrambleTaxon(taxon: number, tokenSeq: number): number {
26
+ /* eslint-disable no-bitwise -- XOR is part of the encode/decode scheme. */
27
+ return (taxon ^ (384160001 * tokenSeq + 2459)) % 4294967296
28
+ /* eslint-enable no-bitwise */
29
+ }
30
+
31
+ /**
32
+ * Parses an NFTokenID into the information it is encoding.
33
+ *
34
+ * Example decoding:
35
+ *
36
+ * 000B 0539 C35B55AA096BA6D87A6E6C965A6534150DC56E5E 12C5D09E 0000000C
37
+ * +--- +--- +--------------------------------------- +------- +-------
38
+ * | | | | |
39
+ * | | | | `---> Sequence: 12
40
+ * | | | |
41
+ * | | | `---> Scrambled Taxon: 314,953,886
42
+ * | | | Unscrambled Taxon: 1337
43
+ * | | |
44
+ * | | `---> Issuer: rJoxBSzpXhPtAuqFmqxQtGKjA13jUJWthE
45
+ * | |
46
+ * | `---> TransferFee: 1337.0 bps or 13.37%
47
+ * |
48
+ * `---> Flags: 11 -> lsfBurnable, lsfOnlyXRP and lsfTransferable
49
+ *
50
+ * @param nftokenID - A hex string which identifies an NFToken on the ledger.
51
+ * @throws XrplError when given an invalid nftokenID.
52
+ * @returns a decoded nftokenID with all fields encoded within.
53
+ */
54
+ export default function parseNFTokenID(nftokenID: string): {
55
+ NFTokenID: string
56
+ Flags: number
57
+ TransferFee: number
58
+ Issuer: string
59
+ Taxon: number
60
+ Sequence: number
61
+ } {
62
+ const expectedLength = 64
63
+ if (nftokenID.length !== expectedLength) {
64
+ throw new XrplError(`Attempting to parse a nftokenID with length ${nftokenID.length}
65
+ , but expected a token with length ${expectedLength}`)
66
+ }
67
+
68
+ const scrambledTaxon = new BigNumber(
69
+ nftokenID.substring(48, 56),
70
+ 16,
71
+ ).toNumber()
72
+ const sequence = new BigNumber(nftokenID.substring(56, 64), 16).toNumber()
73
+
74
+ const NFTokenIDData = {
75
+ NFTokenID: nftokenID,
76
+ Flags: new BigNumber(nftokenID.substring(0, 4), 16).toNumber(),
77
+ TransferFee: new BigNumber(nftokenID.substring(4, 8), 16).toNumber(),
78
+ Issuer: encodeAccountID(Buffer.from(nftokenID.substring(8, 48), 'hex')),
79
+ Taxon: unscrambleTaxon(scrambledTaxon, sequence),
80
+ Sequence: sequence,
81
+ }
82
+
83
+ return NFTokenIDData
84
+ }
@@ -0,0 +1,169 @@
1
+ import BigNumber from 'bignumber.js'
2
+
3
+ import { ValidationError } from '../errors'
4
+
5
+ const BASE_TEN = 10
6
+ const ONE_BILLION = '1000000000'
7
+ const TWO_BILLION = '2000000000'
8
+
9
+ function percentToDecimal(percent: string): string {
10
+ if (!percent.endsWith('%')) {
11
+ throw new ValidationError(`Value ${percent} must end with %`)
12
+ }
13
+
14
+ // Split the string on % and filter out any empty strings
15
+ const split = percent.split('%').filter((str) => str !== '')
16
+ if (split.length !== 1) {
17
+ throw new ValidationError(`Value ${percent} contains too many % signs`)
18
+ }
19
+
20
+ return new BigNumber(split[0]).dividedBy('100').toString(BASE_TEN)
21
+ }
22
+
23
+ /**
24
+ * Converts a string decimal to "billionths" format for use with TransferRate.
25
+ *
26
+ * @param decimal - A string decimal between 0 and 1.00
27
+ * @returns A number in the "billionths" format.
28
+ * @throws ValidationError when the parameter is not convertible to
29
+ * "billionths" format.
30
+ * @category Utilities
31
+ */
32
+ export function decimalToTransferRate(decimal: string): number {
33
+ const rate = new BigNumber(decimal).times(ONE_BILLION).plus(ONE_BILLION)
34
+
35
+ if (rate.isLessThan(ONE_BILLION) || rate.isGreaterThan(TWO_BILLION)) {
36
+ throw new ValidationError(`Decimal value must be between 0 and 1.00.`)
37
+ }
38
+
39
+ const billionths = rate.toString(BASE_TEN)
40
+
41
+ if (billionths === ONE_BILLION) {
42
+ return 0
43
+ }
44
+
45
+ if (billionths === 'NaN') {
46
+ throw new ValidationError(`Value is not a number`)
47
+ }
48
+
49
+ if (billionths.includes('.')) {
50
+ throw new ValidationError(`Decimal exceeds maximum precision.`)
51
+ }
52
+
53
+ return Number(billionths)
54
+ }
55
+
56
+ /**
57
+ * Converts a string percent to "billionths" format for use with TransferRate.
58
+ *
59
+ * @param percent - A string percent between 0% and 100%.
60
+ * @returns A number in the "billionths" format.
61
+ * @throws ValidationError when the percent parameter is not convertible to
62
+ * "billionths" format.
63
+ * @category Utilities
64
+ */
65
+ export function percentToTransferRate(percent: string): number {
66
+ return decimalToTransferRate(percentToDecimal(percent))
67
+ }
68
+
69
+ /**
70
+ * Converts a string decimal to the "billionths" format for use with QualityIn/
71
+ * QualityOut
72
+ *
73
+ * @param decimal - A string decimal (i.e. ".00034").
74
+ * @returns A number in the "billionths" format.
75
+ * @throws ValidationError when the parameter is not convertible to
76
+ * "billionths" format.
77
+ * @category Utilities
78
+ */
79
+ export function decimalToQuality(decimal: string): number {
80
+ const rate = new BigNumber(decimal).times(ONE_BILLION)
81
+
82
+ const billionths = rate.toString(BASE_TEN)
83
+
84
+ if (billionths === 'NaN') {
85
+ throw new ValidationError(`Value is not a number`)
86
+ }
87
+
88
+ if (billionths.includes('-')) {
89
+ throw new ValidationError('Cannot have negative Quality')
90
+ }
91
+
92
+ if (billionths === ONE_BILLION) {
93
+ return 0
94
+ }
95
+
96
+ if (billionths.includes('.')) {
97
+ throw new ValidationError(`Decimal exceeds maximum precision.`)
98
+ }
99
+
100
+ return Number(billionths)
101
+ }
102
+
103
+ /**
104
+ * Converts a quality in "billionths" format to a decimal.
105
+ *
106
+ * @param quality - Quality to convert to decimal.
107
+ * @returns decimal representation of quality.
108
+ * @throws ValidationError when quality is not convertible to decimal format.
109
+ * @category Utilities
110
+ */
111
+ export function qualityToDecimal(quality: number): string {
112
+ if (!Number.isInteger(quality)) {
113
+ throw new ValidationError('Quality must be an integer')
114
+ }
115
+
116
+ if (quality < 0) {
117
+ throw new ValidationError('Negative quality not allowed')
118
+ }
119
+
120
+ if (quality === 0) {
121
+ return '1'
122
+ }
123
+
124
+ const decimal = new BigNumber(quality).dividedBy(ONE_BILLION)
125
+
126
+ return decimal.toString(BASE_TEN)
127
+ }
128
+
129
+ /**
130
+ * Converts a transfer rate in "billionths" format to a decimal.
131
+ *
132
+ * @param rate - TransferRate to convert to decimal.
133
+ * @returns decimal representation of transfer Rate.
134
+ * @throws ValidationError when it cannot convert from billionths format.
135
+ * @category Utilities
136
+ */
137
+ export function transferRateToDecimal(rate: number): string {
138
+ if (!Number.isInteger(rate)) {
139
+ throw new ValidationError(
140
+ 'Error decoding, transfer Rate must be an integer',
141
+ )
142
+ }
143
+
144
+ if (rate === 0) {
145
+ return '0'
146
+ }
147
+
148
+ const decimal = new BigNumber(rate).minus(ONE_BILLION).dividedBy(ONE_BILLION)
149
+
150
+ if (decimal.isLessThan(0)) {
151
+ throw new ValidationError('Error decoding, negative transfer rate')
152
+ }
153
+
154
+ return decimal.toString(BASE_TEN)
155
+ }
156
+
157
+ /**
158
+ * Converts a string percent to the "billionths" format for use with QualityIn/
159
+ * QualityOut
160
+ *
161
+ * @param percent - A string percent (i.e. ".034%").
162
+ * @returns A number in the "billionths" format.
163
+ * @throws ValidationError when the percent parameter is not convertible to
164
+ * "billionths" format.
165
+ * @category Utilities
166
+ */
167
+ export function percentToQuality(percent: string): number {
168
+ return decimalToQuality(percentToDecimal(percent))
169
+ }
@@ -0,0 +1,27 @@
1
+ import { encodeForSigningClaim } from 'ripple-binary-codec'
2
+ import { sign } from 'ripple-keypairs'
3
+
4
+ import { xrpToDrops } from './xrpConversion'
5
+
6
+ /**
7
+ * Sign a payment channel claim.
8
+ *
9
+ * @param channel - Channel identifier specified by the paymentChannelClaim.
10
+ * @param amount - Amount specified by the paymentChannelClaim.
11
+ * @param privateKey - Private Key to sign paymentChannelClaim with.
12
+ * @returns True if the channel is valid.
13
+ * @category Utilities
14
+ */
15
+ function signPaymentChannelClaim(
16
+ channel: string,
17
+ amount: string,
18
+ privateKey: string,
19
+ ): string {
20
+ const signingData = encodeForSigningClaim({
21
+ channel,
22
+ amount: xrpToDrops(amount),
23
+ })
24
+ return sign(signingData, privateKey)
25
+ }
26
+
27
+ export default signPaymentChannelClaim
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Converts a string to its hex equivalent. Useful for Memos.
3
+ *
4
+ * @param string - The string to convert to Hex.
5
+ * @returns The Hex equivalent of the string.
6
+ * @category Utilities
7
+ */
8
+ function convertStringToHex(string: string): string {
9
+ return Buffer.from(string, 'utf8').toString('hex').toUpperCase()
10
+ }
11
+
12
+ /**
13
+ * Converts hex to its string equivalent. Useful to read the Domain field and some Memos.
14
+ *
15
+ * @param hex - The hex to convert to a string.
16
+ * @param encoding - The encoding to use. Defaults to 'utf8' (UTF-8). 'ascii' is also allowed.
17
+ * @returns The converted string.
18
+ * @category Utilities
19
+ */
20
+ function convertHexToString(
21
+ hex: string,
22
+ encoding: BufferEncoding = 'utf8',
23
+ ): string {
24
+ return Buffer.from(hex, 'hex').toString(encoding)
25
+ }
26
+
27
+ export { convertHexToString, convertStringToHex }
@@ -0,0 +1,53 @@
1
+ const RIPPLE_EPOCH_DIFF = 0x386d4380
2
+
3
+ /**
4
+ * Convert a ripple timestamp to a unix timestamp.
5
+ *
6
+ * @param rpepoch - (seconds since 1/1/2000 GMT).
7
+ * @returns Milliseconds since unix epoch.
8
+ * @category Utilities
9
+ */
10
+ function rippleTimeToUnixTime(rpepoch: number): number {
11
+ return (rpepoch + RIPPLE_EPOCH_DIFF) * 1000
12
+ }
13
+
14
+ /**
15
+ * Convert a unix timestamp to a ripple timestamp.
16
+ *
17
+ * @param timestamp - (ms since unix epoch).
18
+ * @returns Seconds since Ripple Epoch (1/1/2000 GMT).
19
+ * @category Utilities
20
+ */
21
+ function unixTimeToRippleTime(timestamp: number): number {
22
+ return Math.round(timestamp / 1000) - RIPPLE_EPOCH_DIFF
23
+ }
24
+
25
+ /**
26
+ * Convert a ripple timestamp to an Iso8601 timestamp.
27
+ *
28
+ * @param rippleTime - Is the number of seconds since Ripple Epoch (1/1/2000 GMT).
29
+ * @returns Iso8601 international standard date format.
30
+ * @category Utilities
31
+ */
32
+ function rippleTimeToISOTime(rippleTime: number): string {
33
+ return new Date(rippleTimeToUnixTime(rippleTime)).toISOString()
34
+ }
35
+
36
+ /**
37
+ * Convert an ISO8601 timestmap to a ripple timestamp.
38
+ *
39
+ * @param iso8601 - International standard date format.
40
+ * @returns Seconds since ripple epoch (1/1/2000 GMT).
41
+ * @category Utilities
42
+ */
43
+ function isoTimeToRippleTime(iso8601: string | Date): number {
44
+ const isoDate = typeof iso8601 === 'string' ? new Date(iso8601) : iso8601
45
+ return unixTimeToRippleTime(isoDate.getTime())
46
+ }
47
+
48
+ export {
49
+ rippleTimeToUnixTime,
50
+ unixTimeToRippleTime,
51
+ rippleTimeToISOTime,
52
+ isoTimeToRippleTime,
53
+ }
@@ -0,0 +1,30 @@
1
+ import { encodeForSigningClaim } from 'ripple-binary-codec'
2
+ import { verify } from 'ripple-keypairs'
3
+
4
+ import { xrpToDrops } from './xrpConversion'
5
+
6
+ /**
7
+ * Verify the signature of a payment channel claim.
8
+ *
9
+ * @param channel - Channel identifier specified by the paymentChannelClaim.
10
+ * @param amount - Amount specified by the paymentChannelClaim.
11
+ * @param signature - Signature produced from signing paymentChannelClaim.
12
+ * @param publicKey - Public key that signed the paymentChannelClaim.
13
+ * @returns True if the channel is valid.
14
+ * @category Utilities
15
+ */
16
+ // eslint-disable-next-line max-params -- Needs 4 params
17
+ function verifyPaymentChannelClaim(
18
+ channel: string,
19
+ amount: string,
20
+ signature: string,
21
+ publicKey: string,
22
+ ): boolean {
23
+ const signingData = encodeForSigningClaim({
24
+ channel,
25
+ amount: xrpToDrops(amount),
26
+ })
27
+ return verify(signingData, signature, publicKey)
28
+ }
29
+
30
+ export default verifyPaymentChannelClaim
@@ -0,0 +1,104 @@
1
+ import BigNumber from 'bignumber.js'
2
+
3
+ import { ValidationError } from '../errors'
4
+
5
+ const DROPS_PER_XRP = 1000000.0
6
+ const MAX_FRACTION_LENGTH = 6
7
+ const BASE_TEN = 10
8
+ const SANITY_CHECK = /^-?[0-9.]+$/u
9
+
10
+ /**
11
+ * Convert Drops to XRP.
12
+ *
13
+ * @param dropsToConvert - Drops to convert to XRP. This can be a string, number, or BigNumber.
14
+ * @returns Amount in XRP.
15
+ * @throws When drops amount is invalid.
16
+ * @category Utilities
17
+ */
18
+ export function dropsToXrp(dropsToConvert: BigNumber.Value): string {
19
+ /*
20
+ * Converting to BigNumber and then back to string should remove any
21
+ * decimal point followed by zeros, e.g. '1.00'.
22
+ * Important: specify base BASE_10 to avoid exponential notation, e.g. '1e-7'.
23
+ */
24
+ const drops = new BigNumber(dropsToConvert).toString(BASE_TEN)
25
+
26
+ // check that the value is valid and actually a number
27
+ if (typeof dropsToConvert === 'string' && drops === 'NaN') {
28
+ throw new ValidationError(
29
+ `dropsToXrp: invalid value '${dropsToConvert}', should be a BigNumber or string-encoded number.`,
30
+ )
31
+ }
32
+
33
+ // drops are only whole units
34
+ if (drops.includes('.')) {
35
+ throw new ValidationError(
36
+ `dropsToXrp: value '${drops}' has too many decimal places.`,
37
+ )
38
+ }
39
+
40
+ /*
41
+ * This should never happen; the value has already been
42
+ * validated above. This just ensures BigNumber did not do
43
+ * something unexpected.
44
+ */
45
+ if (!SANITY_CHECK.exec(drops)) {
46
+ throw new ValidationError(
47
+ `dropsToXrp: failed sanity check -` +
48
+ ` value '${drops}',` +
49
+ ` does not match (^-?[0-9]+$).`,
50
+ )
51
+ }
52
+
53
+ return new BigNumber(drops).dividedBy(DROPS_PER_XRP).toString(BASE_TEN)
54
+ }
55
+
56
+ /**
57
+ * Convert an amount in XRP to an amount in drops.
58
+ *
59
+ * @param xrpToConvert - Amount in XRP.
60
+ * @returns Amount in drops.
61
+ * @throws When amount in xrp is invalid.
62
+ * @category Utilities
63
+ */
64
+ export function xrpToDrops(xrpToConvert: BigNumber.Value): string {
65
+ // Important: specify base BASE_TEN to avoid exponential notation, e.g. '1e-7'.
66
+ const xrp = new BigNumber(xrpToConvert).toString(BASE_TEN)
67
+
68
+ // check that the value is valid and actually a number
69
+ if (typeof xrpToConvert === 'string' && xrp === 'NaN') {
70
+ throw new ValidationError(
71
+ `xrpToDrops: invalid value '${xrpToConvert}', should be a BigNumber or string-encoded number.`,
72
+ )
73
+ }
74
+
75
+ /*
76
+ * This should never happen; the value has already been
77
+ * validated above. This just ensures BigNumber did not do
78
+ * something unexpected.
79
+ */
80
+ if (!SANITY_CHECK.exec(xrp)) {
81
+ throw new ValidationError(
82
+ `xrpToDrops: failed sanity check - value '${xrp}', does not match (^-?[0-9.]+$).`,
83
+ )
84
+ }
85
+
86
+ const components = xrp.split('.')
87
+ if (components.length > 2) {
88
+ throw new ValidationError(
89
+ `xrpToDrops: failed sanity check - value '${xrp}' has too many decimal points.`,
90
+ )
91
+ }
92
+
93
+ const fraction = components[1] || '0'
94
+ if (fraction.length > MAX_FRACTION_LENGTH) {
95
+ throw new ValidationError(
96
+ `xrpToDrops: value '${xrp}' has too many decimal places.`,
97
+ )
98
+ }
99
+
100
+ return new BigNumber(xrp)
101
+ .times(DROPS_PER_XRP)
102
+ .integerValue(BigNumber.ROUND_FLOOR)
103
+ .toString(BASE_TEN)
104
+ }