forked from Mahlomola2002/PaymentBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
282 lines (213 loc) · 7.04 KB
/
server.ts
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { AuthenticatedClient, Grant, PendingGrant, Quote, WalletAddress, createAuthenticatedClient,isFinalizedGrant,isPendingGrant,} from "@interledger/open-payments";
import { randomUUID } from "crypto";
import * as net from 'net';
export async function getAuthenticatedClient() {
const client = await createAuthenticatedClient({
walletAddressUrl: "https://ilp.rafiki.money/ffc52473",
privateKey: "./private-2.key",
keyId:"bde57813-19cb-437f-9e51-731aa9ebf333",
validateResponses: false
});
return client;
}
async function getWalletAddressInfo(client: AuthenticatedClient,
walletAddress: string,): Promise<WalletAddress | null> {
if (walletAddress.startsWith("$")){
walletAddress = walletAddress.replace("$", "https://");}
const walletAddressDetails = await client.walletAddress.get({
url: walletAddress,
});
return walletAddressDetails;
}
export async function createIncomingPayment(
client: AuthenticatedClient,
value: string,
walletAddressDetails: WalletAddress,
) {
console.log("** creating incoming payment grant req");
console.log(walletAddressDetails);
// Request IP grant
const grant = await client.grant.request(
{
url: walletAddressDetails.authServer,
},
{
access_token: {
access: [
{
type: "incoming-payment",
actions: ["read", "create", "complete"],
},
],
},
},
);
if (isPendingGrant(grant)) {
throw new Error("Expected non-interactive grant");
}
// create incoming payment
const incomingPayment = await client.incomingPayment.create(
{
url: new URL(walletAddressDetails.id).origin,
accessToken: grant.access_token.value,
},
{
walletAddress: walletAddressDetails.id,
incomingAmount: {
value: value,
assetCode: walletAddressDetails.assetCode,
assetScale: walletAddressDetails.assetScale,
},
// expiresAt: new Date(Date.now() + 60_000 * 10).toISOString(),
},
);
console.log("** inc");
console.log(incomingPayment);
return incomingPayment;
}
export async function createQuote(
client: AuthenticatedClient,
incomingPaymentUrl: string,
walletAddressDetails: WalletAddress,
) {
console.log("** 2 req");
console.log(walletAddressDetails);
// Request Qoute grant
const grant = await client.grant.request(
{
url: walletAddressDetails.authServer,
},
{
access_token: {
access: [
{
type: "quote",
actions: ["create", "read", "read-all"],
},
],
},
},
);
if (isPendingGrant(grant)) {
throw new Error("Expected non-interactive grant");
}
// create qoute
const qoute = await client.quote.create(
{
url: walletAddressDetails.resourceServer,
accessToken: grant.access_token.value,
},
{
method: "ilp",
walletAddress: walletAddressDetails.id,
receiver: incomingPaymentUrl,
},
);
console.log("** qoute");
console.log(qoute);
return qoute;
}
export async function getOutgoingPaymentAuthorization(
client: AuthenticatedClient,
walletAddressDetails: WalletAddress,
): Promise<PendingGrant> {
const grant = await client.grant.request(
{
url:walletAddressDetails.authServer,
},
{
access_token: {
access: [
{
identifier: walletAddressDetails.id,
type: "outgoing-payment",
actions: ["list", "list-all", "read", "read-all", "create"],
limits: {
},
},
],
},
interact: {
start: ["redirect"],
},
},
);
if (!isPendingGrant(grant)) {
throw new Error("Expected interactive grant");
}
return grant;
}
export async function createOutgoingPayment(client: AuthenticatedClient,walletAddressUrl:WalletAddress,grant:PendingGrant,qoute:Quote){
let grant2;
while(true){
console.log("trying to get payment.......");
grant2 = await client.grant.continue(
{
accessToken: grant.continue.access_token.value,
url: grant.continue.uri,
},
);
console.log(grant2);
if (isFinalizedGrant(grant2)){
// sleep
break;
}
else{
console.log("sleeping....");
await new Promise((r)=>setTimeout(r,2000));
}
}
const outgoingPayment = await client.outgoingPayment.create(
{
url: walletAddressUrl.resourceServer,
accessToken: grant2.access_token.value, //OUTGOING_PAYMENT_ACCESS_TOKEN,
},
{
walletAddress: walletAddressUrl.id,
quoteId: qoute.id, //QUOTE_URL,
},
);
return outgoingPayment;
}
// Create a TCP server
const server = net.createServer((socket: net.Socket) => {
console.log('Client connected');
// Handle incoming data from clients
socket.on('data', async (data: Buffer) => {
const myClient= await getAuthenticatedClient();
console.log(`Received from client: ${data.toString()}`);
const args=data.toString().split(" ");
const sendWallet=args[0];
const receiverWallet=args[1];
const amountMoney=args[2];
//get sender wallet address details
const senderWalletsDetails=await getWalletAddressInfo( myClient,sendWallet);
//get receiver wallet address details
const receiverWalletsDetails=await getWalletAddressInfo(myClient,receiverWallet);
if(!receiverWalletsDetails){
throw new Error();
}
const incomingPayment=await createIncomingPayment(myClient,amountMoney,receiverWalletsDetails);
console.log(incomingPayment);
if(!senderWalletsDetails){
throw new Error();
}
const myQuote=await createQuote(myClient,incomingPayment.id,senderWalletsDetails);
//get outgoing payment
const outPayment=await getOutgoingPaymentAuthorization(myClient,senderWalletsDetails);
if(!isPendingGrant(outPayment)){
throw new Error();
}
// Echo back to the client
socket.write(`Server received: ${outPayment.interact.redirect}`);
const completePayment= await createOutgoingPayment(myClient,senderWalletsDetails,outPayment,myQuote)
});
// Handle client disconnection
socket.on('end', () => {
console.log('Client disconnected');
});
});
// Listen for connections on port 3000
server.listen(3003, () => {
console.log('Server is listening on port 3002');
});