-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
217 lines (193 loc) · 6.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* This script sets up an incoming payment on a receiving wallet address,
* and a quote on the sending wallet address (after getting grants for both of the resources).
*
* The final step is asking for an outgoing payment grant for the sending wallet address.
* Since this needs user interaction, you will need to navigate to the URL, and accept the interactive grant.
*
* To start, please add the variables for configuring the client & the wallet addresses for the payment.
*/
import {
createAuthenticatedClient,
OpenPaymentsClientError,
isFinalizedGrant,
} from "@interledger/open-payments";
import readline from "readline/promises";
(async () => {
const client = await createAuthenticatedClient({
walletAddressUrl: "", // Make sure the wallet address starts with https:// (not $), and has no trailing slashes
privateKey: "private.key",
keyId: "",
});
const sendingWalletAddress = await client.walletAddress.get({
url: "", // Make sure the wallet address starts with https:// (not $)
});
const receivingWalletAddress = await client.walletAddress.get({
url: "", // Make sure the wallet address starts with https:// (not $)
});
console.log(
"Got wallet addresses. We will set up a payment between the sending and the receiving wallet address",
{ receivingWalletAddress, sendingWalletAddress }
);
// Step 1: Get a grant for the incoming payment, so we can create the incoming payment on the receiving wallet address
const incomingPaymentGrant = await client.grant.request(
{
url: receivingWalletAddress.authServer,
},
{
access_token: {
access: [
{
type: "incoming-payment",
actions: ["read", "complete", "create"],
},
],
},
}
);
console.log(
"\nStep 1: got incoming payment grant for receiving wallet address",
incomingPaymentGrant
);
// Step 2: Create the incoming payment. This will be where funds will be received.
const incomingPayment = await client.incomingPayment.create(
{
url: receivingWalletAddress.resourceServer,
accessToken: incomingPaymentGrant.access_token.value,
},
{
walletAddress: receivingWalletAddress.id,
incomingAmount: {
assetCode: receivingWalletAddress.assetCode,
assetScale: receivingWalletAddress.assetScale,
value: "1000",
},
}
);
console.log(
"\nStep 2: created incoming payment on receiving wallet address",
incomingPayment
);
// Step 3: Get a quote grant, so we can create a quote on the sending wallet address
const quoteGrant = await client.grant.request(
{
url: sendingWalletAddress.authServer,
},
{
access_token: {
access: [
{
type: "quote",
actions: ["create", "read"],
},
],
},
}
);
console.log(
"\nStep 3: got quote grant on sending wallet address",
quoteGrant
);
// Step 4: Create a quote, this gives an indication of how much it will cost to pay into the incoming payment
const quote = await client.quote.create(
{
url: sendingWalletAddress.resourceServer,
accessToken: quoteGrant.access_token.value,
},
{
walletAddress: sendingWalletAddress.id,
receiver: incomingPayment.id,
method: "ilp",
}
);
console.log("\nStep 4: got quote on sending wallet address", quote);
// Step 5: Start the grant process for the outgoing payments.
// This is an interactive grant: the user (in this case, you) will need to accept the grant by navigating to the outputted link.
const outgoingPaymentGrant = await client.grant.request(
{
url: sendingWalletAddress.authServer,
},
{
access_token: {
access: [
{
type: "outgoing-payment",
actions: ["read", "create"],
limits: {
debitAmount: {
assetCode: quote.debitAmount.assetCode,
assetScale: quote.debitAmount.assetScale,
value: quote.debitAmount.value,
},
},
identifier: sendingWalletAddress.id,
},
],
},
interact: {
start: ["redirect"],
// finish: {
// method: "redirect",
// // This is where you can (optionally) redirect a user to after going through interaction.
// // Keep in mind, you will need to parse the interact_ref in the resulting interaction URL,
// // and pass it into the grant continuation request.
// uri: "https://example.com",
// nonce: crypto.randomUUID(),
// },
},
}
);
console.log(
"\nStep 5: got pending outgoing payment grant",
outgoingPaymentGrant
);
console.log(
"Please navigate to the following URL, to accept the interaction from the sending wallet:"
);
console.log(outgoingPaymentGrant.interact.redirect);
await readline
.createInterface({ input: process.stdin, output: process.stdout })
.question("\nPlease accept grant and press enter...");
let finalizedOutgoingPaymentGrant;
const grantContinuationErrorMessage =
"\nThere was an error continuing the grant. You probably have not accepted the grant at the url (or it has already been used up, in which case, rerun the script).";
try {
finalizedOutgoingPaymentGrant = await client.grant.continue({
url: outgoingPaymentGrant.continue.uri,
accessToken: outgoingPaymentGrant.continue.access_token.value,
});
} catch (err) {
if (err instanceof OpenPaymentsClientError) {
console.log(grantContinuationErrorMessage);
process.exit();
}
throw err;
}
if (!isFinalizedGrant(finalizedOutgoingPaymentGrant)) {
console.log(
"There was an error continuing the grant. You probably have not accepted the grant at the url."
);
process.exit();
}
console.log(
"\nStep 6: got finalized outgoing payment grant",
finalizedOutgoingPaymentGrant
);
// Step 7: Finally, create the outgoing payment on the sending wallet address.
// This will make a payment from the outgoing payment to the incoming one (over ILP)
const outgoingPayment = await client.outgoingPayment.create(
{
url: sendingWalletAddress.resourceServer,
accessToken: finalizedOutgoingPaymentGrant.access_token.value,
},
{
walletAddress: sendingWalletAddress.id,
quoteId: quote.id,
}
);
console.log(
"\nStep 7: Created outgoing payment. Funds will now move from the outgoing payment to the incoming payment.",
outgoingPayment
);
process.exit();
})();