-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_token.js
More file actions
120 lines (104 loc) · 3.45 KB
/
Copy pathgenerate_token.js
File metadata and controls
120 lines (104 loc) · 3.45 KB
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
import dotenv from "dotenv";
import {
createAuthenticatedClient,
isPendingGrant,
isFinalizedGrant
} from "@interledger/open-payments";
import { randomUUID } from "crypto";
import { setTimeout as wait } from "timers/promises"; // ⬅️ Needed for polling
dotenv.config();
// Load environment variables
const walletAddressUrl = process.env.OPEN_PAYMENTS_CLIENT_ADDRESS;
const privateKeyPath = process.env.OPEN_PAYMENTS_SECRET_KEY_PATH;
const keyId = process.env.OPEN_PAYMENTS_KEY_ID;
console.log("Wallet Address:", walletAddressUrl);
console.log("Key ID:", keyId);
// Validate environment variables
if (!walletAddressUrl || !privateKeyPath || !keyId) {
console.error("❌ Missing required environment variables.");
if (!walletAddressUrl) console.log("Missing: OPEN_PAYMENTS_CLIENT_ADDRESS");
if (!keyId) console.log("Missing: OPEN_PAYMENTS_KEY_ID");
if (!privateKeyPath) console.log("Missing: OPEN_PAYMENTS_SECRET_KEY_PATH");
process.exit(1);
}
// Create client
async function getAuthenticatedClient() {
let address = walletAddressUrl;
if (address.startsWith("$")) {
address = address.replace("$", "https://");
}
return await createAuthenticatedClient({
walletAddressUrl: address,
privateKey: privateKeyPath,
keyId: keyId,
});
}
// Fetch wallet info
async function getWalletAddressInfo(client, address) {
if (address.startsWith("$")) {
address = address.replace("$", "https://");
}
return await client.walletAddress.get({ url: address });
}
// Request token with polling
async function requestAccessToken(client, wallet) {
console.log("📡 Requesting grant...");
const grant = await client.grant.request(
{
url: wallet.authServer,
},
{
access_token: {
access: [
{
type: "outgoing-payment",
identifier: wallet.id,
actions: ["create", "read", "list"],
limits: {
debitAmount: {
value: "100000",
assetCode: wallet.assetCode,
assetScale: wallet.assetScale,
},
},
},
],
},
interact: {
start: ["redirect"],
},
}
);
if (isPendingGrant(grant)) {
console.log("⚠️ Grant is pending. You must authorize it in your browser:");
console.log("🔗 Redirect URL:", grant.interact?.redirect);
let continuedGrant = grant;
while (!isFinalizedGrant(continuedGrant)) {
console.log("⏳ Waiting for user authorization...");
console.log("➡️ Continue URI:", grant.continueUri);
console.log("🆔 Interact Ref:", grant.interact?.interactRef);
await wait(grant.continue.wait * 1000) // wait 20 seconds
continuedGrant = await client.grant.continue(
{
url: grant.continue.uri,
accessToken: grant.continue.access_token.value,
},
);
console.log(continuedGrant)
}
console.log("✅ Final Access Token:", continuedGrant.access_token.value);
} else {
console.log("✅ Final Access Token:", grant.access_token.value);
}
}
// Main runner
async function run() {
try {
const client = await getAuthenticatedClient();
const wallet = await getWalletAddressInfo(client, walletAddressUrl);
await requestAccessToken(client, wallet);
} catch (err) {
console.error("❌ Failed to generate token:", err);
}
}
run();