dhali-js 1.0.2 → 1.0.4
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.
- package/README.md +56 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
[](https://github.com/Dhali-org/Dhali-js/actions/workflows/tests.yaml)
|
|
2
|
+
[](https://github.com/Dhali-org/Dhali-js/actions/workflows/publish.yaml)
|
|
3
|
+
|
|
4
|
+
|
|
1
5
|
# dhali-js
|
|
2
6
|
|
|
3
7
|
A JavaScript library for managing XRPL payment channels and generating auth tokens for use with [Dhali](https://dhali.io) APIs.
|
|
@@ -9,55 +13,76 @@ Leverages [xrpl.js](https://github.com/XRPLF/xrpl.js) and **only ever performs l
|
|
|
9
13
|
|
|
10
14
|
```bash
|
|
11
15
|
npm install dhali-js
|
|
12
|
-
|
|
16
|
+
```
|
|
13
17
|
|
|
14
18
|
---
|
|
15
19
|
|
|
16
20
|
## Quick Start
|
|
17
21
|
|
|
18
22
|
```js
|
|
23
|
+
// ==== 0. Common setup ====
|
|
19
24
|
const { Wallet } = require('xrpl')
|
|
20
25
|
const { DhaliChannelManager, ChannelNotFound } = require('dhali-js')
|
|
21
26
|
|
|
22
|
-
const seed
|
|
27
|
+
const seed = "sXXX"
|
|
28
|
+
const wallet = Wallet.fromSeed(seed)
|
|
29
|
+
const manager = new DhaliChannelManager(wallet)
|
|
30
|
+
```
|
|
23
31
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
32
|
+
|
|
33
|
+
### 1. Create a Payment Claim
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
let token
|
|
37
|
+
try {
|
|
38
|
+
token = await manager.getAuthToken()
|
|
39
|
+
} catch (err) {
|
|
40
|
+
if (err instanceof ChannelNotFound) {
|
|
41
|
+
await manager.deposit(1_000_000) // deposit 1 XRP
|
|
42
|
+
token = await manager.getAuthToken() // 🔑 regenerate after deposit
|
|
43
|
+
} else throw err
|
|
44
|
+
}
|
|
45
|
+
console.log('New channel token:', token)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
### 2. Top Up Later (and Regenerate)
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
await manager.deposit(2_000_000) // add 2 XRP
|
|
54
|
+
const updatedToken = await manager.getAuthToken()
|
|
55
|
+
console.log('Updated token:', updatedToken)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
### 3. Using APIs and Handling 402 "Payment Required" Errors
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
const fetchWithClaim = async (maxRetries = 5) => {
|
|
64
|
+
for (let i = 1; i <= maxRetries; i++) {
|
|
65
|
+
const token = await manager.getAuthToken()
|
|
66
|
+
const url = `https://xrplcluster.dhali.io?payment-claim=${token}`
|
|
67
|
+
const resp = await fetch(url, { /* …RPC call… */ })
|
|
68
|
+
|
|
69
|
+
if (resp.status !== 402) return resp.json()
|
|
70
|
+
|
|
71
|
+
console.warn(`Attempt ${i}: topping up…`)
|
|
72
|
+
await manager.deposit(1_000_000) // deposit 1 XRP
|
|
40
73
|
}
|
|
74
|
+
throw new Error(`402 after ${maxRetries} retries`)
|
|
75
|
+
}
|
|
41
76
|
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
method: 'POST',
|
|
45
|
-
headers: { 'Content-Type': 'application/json' },
|
|
46
|
-
body: JSON.stringify({
|
|
47
|
-
method: 'account_info',
|
|
48
|
-
params: [{ account: wallet.classicAddress, ledger_index: 'validated' }],
|
|
49
|
-
id: 1,
|
|
50
|
-
}),
|
|
51
|
-
})
|
|
52
|
-
const result = await resp.json()
|
|
77
|
+
;(async () => {
|
|
78
|
+
const result = await fetchWithClaim()
|
|
53
79
|
console.log(result)
|
|
54
80
|
})()
|
|
55
|
-
|
|
56
81
|
```
|
|
57
82
|
|
|
58
83
|
---
|
|
59
84
|
|
|
60
|
-
##
|
|
85
|
+
## Class reference
|
|
61
86
|
|
|
62
87
|
### `new DhaliChannelManager(wallet: xrpl.Wallet)`
|
|
63
88
|
|