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,71 @@
1
+ /*
2
+ * Original code based on "backo" - https://github.com/segmentio/backo
3
+ * MIT License - Copyright 2014 Segment.io
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
5
+ * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
6
+ * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
7
+ * is furnished to do so, subject to the following conditions:
8
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
10
+ * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
11
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
12
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13
+ */
14
+
15
+ interface ExponentialBackoffOptions {
16
+ // The min backoff duration.
17
+ min?: number
18
+ // The max backoff duration.
19
+ max?: number
20
+ }
21
+
22
+ const DEFAULT_MIN = 100
23
+ const DEFAULT_MAX = 1000
24
+
25
+ /**
26
+ * A Back off strategy that increases exponentially. Useful with repeated
27
+ * setTimeout calls over a network (where the destination may be down).
28
+ */
29
+ export default class ExponentialBackoff {
30
+ private readonly ms: number
31
+ private readonly max: number
32
+ private readonly factor: number = 2
33
+ private numAttempts = 0
34
+
35
+ /**
36
+ * Constructs an ExponentialBackoff object.
37
+ *
38
+ * @param opts - The options for the object.
39
+ */
40
+ public constructor(opts: ExponentialBackoffOptions = {}) {
41
+ this.ms = opts.min ?? DEFAULT_MIN
42
+ this.max = opts.max ?? DEFAULT_MAX
43
+ }
44
+
45
+ /**
46
+ * Number of attempts for backoff so far.
47
+ *
48
+ * @returns Number of attempts.
49
+ */
50
+ public get attempts(): number {
51
+ return this.numAttempts
52
+ }
53
+
54
+ /**
55
+ * Return the backoff duration.
56
+ *
57
+ * @returns The backoff duration in milliseconds.
58
+ */
59
+ public duration(): number {
60
+ const ms = this.ms * this.factor ** this.numAttempts
61
+ this.numAttempts += 1
62
+ return Math.floor(Math.min(ms, this.max))
63
+ }
64
+
65
+ /**
66
+ * Reset the number of attempts.
67
+ */
68
+ public reset(): void {
69
+ this.numAttempts = 0
70
+ }
71
+ }
@@ -0,0 +1,194 @@
1
+ import {
2
+ ResponseFormatError,
3
+ RippledError,
4
+ TimeoutError,
5
+ XrplError,
6
+ } from '../errors'
7
+ import { Response } from '../models/methods'
8
+ import { BaseRequest, ErrorResponse } from '../models/methods/baseMethod'
9
+
10
+ /**
11
+ * Manage all the requests made to the websocket, and their async responses
12
+ * that come in from the WebSocket. Responses come in over the WS connection
13
+ * after-the-fact, so this manager will tie that response to resolve the
14
+ * original request.
15
+ */
16
+ export default class RequestManager {
17
+ private nextId = 0
18
+ private readonly promisesAwaitingResponse = new Map<
19
+ string | number,
20
+ {
21
+ resolve: (value: Response | PromiseLike<Response>) => void
22
+ reject: (value: Error) => void
23
+ timer: ReturnType<typeof setTimeout>
24
+ }
25
+ >()
26
+
27
+ /**
28
+ * Successfully resolves a request.
29
+ *
30
+ * @param id - ID of the request.
31
+ * @param response - Response to return.
32
+ * @throws Error if no existing promise with the given ID.
33
+ */
34
+ public resolve(id: string | number, response: Response): void {
35
+ const promise = this.promisesAwaitingResponse.get(id)
36
+ if (promise == null) {
37
+ throw new XrplError(`No existing promise with id ${id}`, {
38
+ type: 'resolve',
39
+ response,
40
+ })
41
+ }
42
+ clearTimeout(promise.timer)
43
+ promise.resolve(response)
44
+ this.deletePromise(id)
45
+ }
46
+
47
+ /**
48
+ * Rejects a request.
49
+ *
50
+ * @param id - ID of the request.
51
+ * @param error - Error to throw with the reject.
52
+ * @throws Error if no existing promise with the given ID.
53
+ */
54
+ public reject(id: string | number, error: Error): void {
55
+ const promise = this.promisesAwaitingResponse.get(id)
56
+ if (promise == null) {
57
+ throw new XrplError(`No existing promise with id ${id}`, {
58
+ type: 'reject',
59
+ error,
60
+ })
61
+ }
62
+ clearTimeout(promise.timer)
63
+ // TODO: figure out how to have a better stack trace for an error
64
+ promise.reject(error)
65
+ this.deletePromise(id)
66
+ }
67
+
68
+ /**
69
+ * Reject all pending requests.
70
+ *
71
+ * @param error - Error to throw with the reject.
72
+ */
73
+ public rejectAll(error: Error): void {
74
+ this.promisesAwaitingResponse.forEach((_promise, id, _map) => {
75
+ this.reject(id, error)
76
+ this.deletePromise(id)
77
+ })
78
+ }
79
+
80
+ /**
81
+ * Creates a new WebSocket request. This sets up a timeout timer to catch
82
+ * hung responses, and a promise that will resolve with the response once
83
+ * the response is seen & handled.
84
+ *
85
+ * @param request - Request to create.
86
+ * @param timeout - Timeout length to catch hung responses.
87
+ * @returns Request ID, new request form, and the promise for resolving the request.
88
+ * @throws XrplError if request with the same ID is already pending.
89
+ */
90
+ public createRequest<T extends BaseRequest>(
91
+ request: T,
92
+ timeout: number,
93
+ ): [string | number, string, Promise<Response>] {
94
+ let newId: string | number
95
+ if (request.id == null) {
96
+ newId = this.nextId
97
+ this.nextId += 1
98
+ } else {
99
+ newId = request.id
100
+ }
101
+ const newRequest = JSON.stringify({ ...request, id: newId })
102
+ // Typing required for Jest running in browser
103
+ const timer: ReturnType<typeof setTimeout> = setTimeout(() => {
104
+ this.reject(
105
+ newId,
106
+ new TimeoutError(
107
+ `Timeout for request: ${JSON.stringify(request)} with id ${newId}`,
108
+ request,
109
+ ),
110
+ )
111
+ }, timeout)
112
+ /*
113
+ * Node.js won't exit if a timer is still running, so we tell Node to ignore.
114
+ * (Node will still wait for the request to complete).
115
+ */
116
+ // The following type assertions are required to get this code to pass in browser environments
117
+ // where setTimeout has a different type
118
+ // eslint-disable-next-line max-len -- Necessary to disable both rules.
119
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access -- Reason above.
120
+ if ((timer as unknown as any).unref) {
121
+ // eslint-disable-next-line max-len -- Necessary to disable both rules.
122
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call -- Reason above.
123
+ ;(timer as unknown as any).unref()
124
+ }
125
+ if (this.promisesAwaitingResponse.has(newId)) {
126
+ clearTimeout(timer)
127
+ throw new XrplError(
128
+ `Response with id '${newId}' is already pending`,
129
+ request,
130
+ )
131
+ }
132
+ const newPromise = new Promise<Response>(
133
+ (resolve: (value: Response | PromiseLike<Response>) => void, reject) => {
134
+ this.promisesAwaitingResponse.set(newId, { resolve, reject, timer })
135
+ },
136
+ )
137
+
138
+ return [newId, newRequest, newPromise]
139
+ }
140
+
141
+ /**
142
+ * Handle a "response". Responses match to the earlier request handlers,
143
+ * and resolve/reject based on the data received.
144
+ *
145
+ * @param response - The response to handle.
146
+ * @throws ResponseFormatError if the response format is invalid, RippledError if rippled returns an error.
147
+ */
148
+ public handleResponse(response: Partial<Response | ErrorResponse>): void {
149
+ if (
150
+ response.id == null ||
151
+ !(typeof response.id === 'string' || typeof response.id === 'number')
152
+ ) {
153
+ throw new ResponseFormatError('valid id not found in response', response)
154
+ }
155
+ if (!this.promisesAwaitingResponse.has(response.id)) {
156
+ return
157
+ }
158
+ if (response.status == null) {
159
+ const error = new ResponseFormatError('Response has no status')
160
+ this.reject(response.id, error)
161
+ }
162
+ if (response.status === 'error') {
163
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- We know this must be true
164
+ const errorResponse = response as Partial<ErrorResponse>
165
+ const error = new RippledError(
166
+ errorResponse.error_message ?? errorResponse.error,
167
+ errorResponse,
168
+ )
169
+ this.reject(response.id, error)
170
+ return
171
+ }
172
+ if (response.status !== 'success') {
173
+ const error = new ResponseFormatError(
174
+ `unrecognized response.status: ${response.status ?? ''}`,
175
+ response,
176
+ )
177
+ this.reject(response.id, error)
178
+ return
179
+ }
180
+ // status no longer needed because error is thrown if status is not "success"
181
+ delete response.status
182
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Must be a valid Response here
183
+ this.resolve(response.id, response as unknown as Response)
184
+ }
185
+
186
+ /**
187
+ * Delete a promise after it has been returned.
188
+ *
189
+ * @param id - ID of the request.
190
+ */
191
+ private deletePromise(id: string | number): void {
192
+ this.promisesAwaitingResponse.delete(id)
193
+ }
194
+ }
@@ -0,0 +1,106 @@
1
+ /* eslint-disable max-classes-per-file -- Needs to be a wrapper for ws */
2
+ import { EventEmitter } from 'events'
3
+
4
+ // Define the global WebSocket class found on the native browser
5
+ declare class WebSocket {
6
+ public onclose?: (closeEvent: CloseEvent) => void
7
+ public onopen?: (openEvent: Event) => void
8
+ public onerror?: (error: Error) => void
9
+ public onmessage?: (message: MessageEvent) => void
10
+ public readyState: number
11
+ public constructor(url: string)
12
+ public close(code?: number, reason?: Buffer): void
13
+ public send(message: string): void
14
+ }
15
+
16
+ interface WSWrapperOptions {
17
+ perMessageDeflate: boolean
18
+ handshakeTimeout: number
19
+ protocolVersion: number
20
+ origin: string
21
+ maxPayload: number
22
+ followRedirects: boolean
23
+ maxRedirects: number
24
+ }
25
+
26
+ /**
27
+ * Provides `EventEmitter` interface for native browser `WebSocket`,
28
+ * same, as `ws` package provides.
29
+ */
30
+ export default class WSWrapper extends EventEmitter {
31
+ public static CONNECTING = 0
32
+ public static OPEN = 1
33
+ public static CLOSING = 2
34
+ // eslint-disable-next-line @typescript-eslint/no-magic-numbers -- magic number is being defined here
35
+ public static CLOSED = 3
36
+ private readonly ws: WebSocket
37
+
38
+ /**
39
+ * Constructs a browser-safe websocket.
40
+ *
41
+ * @param url - URL to connect to.
42
+ * @param _protocols - Not used.
43
+ * @param _websocketOptions - Not used.
44
+ */
45
+ public constructor(
46
+ url: string,
47
+ _protocols: string | string[] | WSWrapperOptions | undefined,
48
+ _websocketOptions: WSWrapperOptions,
49
+ ) {
50
+ super()
51
+ this.setMaxListeners(Infinity)
52
+
53
+ this.ws = new WebSocket(url)
54
+
55
+ this.ws.onclose = (closeEvent: CloseEvent): void => {
56
+ let reason: Uint8Array | undefined
57
+ if (closeEvent.reason) {
58
+ const enc = new TextEncoder()
59
+ reason = enc.encode(closeEvent.reason)
60
+ }
61
+ this.emit('close', closeEvent.code, reason)
62
+ }
63
+
64
+ this.ws.onopen = (): void => {
65
+ this.emit('open')
66
+ }
67
+
68
+ this.ws.onerror = (error): void => {
69
+ this.emit('error', error)
70
+ }
71
+
72
+ this.ws.onmessage = (message: MessageEvent): void => {
73
+ this.emit('message', message.data)
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Get the ready state of the websocket.
79
+ *
80
+ * @returns The Websocket's ready state.
81
+ */
82
+ public get readyState(): number {
83
+ return this.ws.readyState
84
+ }
85
+
86
+ /**
87
+ * Closes the websocket.
88
+ *
89
+ * @param code - Close code.
90
+ * @param reason - Close reason.
91
+ */
92
+ public close(code?: number, reason?: Buffer): void {
93
+ if (this.readyState === 1) {
94
+ this.ws.close(code, reason)
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Sends a message over the Websocket connection.
100
+ *
101
+ * @param message - Message to send.
102
+ */
103
+ public send(message: string): void {
104
+ this.ws.send(message)
105
+ }
106
+ }