n8n-nodes-agnicwallet 1.0.11 → 1.0.13
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 +51 -6
- package/dist/credentials/AgnicWalletApi.credentials.js +70 -41
- package/dist/credentials/AgnicWalletOAuth2Api.credentials.js +120 -91
- package/dist/nodes/AgnicAI/AgnicAI.node.js +1160 -411
- package/dist/nodes/AgnicAILanguageModel/AgnicAILanguageModel.node.js +1016 -355
- package/dist/nodes/AgnicMCPTool/AgnicMCPTool.node.js +12330 -336
- package/dist/nodes/X402HttpRequest/X402HttpRequest.node.js +345 -310
- package/package.json +9 -8
|
@@ -1,327 +1,362 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// nodes/X402HttpRequest/X402HttpRequest.node.ts
|
|
22
|
+
var X402HttpRequest_node_exports = {};
|
|
23
|
+
__export(X402HttpRequest_node_exports, {
|
|
24
|
+
X402HttpRequest: () => X402HttpRequest
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(X402HttpRequest_node_exports);
|
|
27
|
+
var import_n8n_workflow = require("n8n-workflow");
|
|
28
|
+
var X402HttpRequest = class {
|
|
29
|
+
constructor() {
|
|
30
|
+
this.description = {
|
|
31
|
+
displayName: "AgnicWallet X402 Request",
|
|
32
|
+
name: "agnicWalletX402Request",
|
|
33
|
+
group: ["transform"],
|
|
34
|
+
version: 1,
|
|
35
|
+
description: "Make HTTP requests to X402-enabled APIs with automatic payment via AgnicWallet",
|
|
36
|
+
defaults: {
|
|
37
|
+
name: "AgnicWallet X402"
|
|
38
|
+
},
|
|
39
|
+
icon: "file:X402HttpRequest.png",
|
|
40
|
+
usableAsTool: true,
|
|
41
|
+
inputs: ["main"],
|
|
42
|
+
outputs: ["main"],
|
|
43
|
+
credentials: [
|
|
44
|
+
{
|
|
45
|
+
name: "agnicWalletOAuth2Api",
|
|
46
|
+
required: false,
|
|
47
|
+
displayOptions: {
|
|
48
|
+
show: {
|
|
49
|
+
authentication: ["oAuth2"]
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: "agnicWalletApi",
|
|
55
|
+
required: false,
|
|
56
|
+
displayOptions: {
|
|
57
|
+
show: {
|
|
58
|
+
authentication: ["apiKey"]
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
],
|
|
63
|
+
properties: [
|
|
64
|
+
{
|
|
65
|
+
displayName: "Authentication",
|
|
66
|
+
name: "authentication",
|
|
67
|
+
type: "options",
|
|
68
|
+
options: [
|
|
69
|
+
{
|
|
70
|
+
name: "OAuth2",
|
|
71
|
+
value: "oAuth2",
|
|
72
|
+
description: "Recommended: Connect your account"
|
|
15
73
|
},
|
|
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
|
-
name: "PUT",
|
|
75
|
-
value: "PUT",
|
|
76
|
-
},
|
|
77
|
-
{
|
|
78
|
-
name: "DELETE",
|
|
79
|
-
value: "DELETE",
|
|
80
|
-
},
|
|
81
|
-
],
|
|
82
|
-
default: "GET",
|
|
83
|
-
description: "The HTTP method to use",
|
|
84
|
-
},
|
|
85
|
-
{
|
|
86
|
-
displayName: "URL",
|
|
87
|
-
name: "url",
|
|
88
|
-
type: "string",
|
|
89
|
-
default: "",
|
|
90
|
-
required: true,
|
|
91
|
-
placeholder: "https://api.example.com/endpoint",
|
|
92
|
-
description: "The URL of the X402-enabled API",
|
|
93
|
-
},
|
|
74
|
+
{
|
|
75
|
+
name: "API Key",
|
|
76
|
+
value: "apiKey",
|
|
77
|
+
description: "For CI/CD or programmatic access"
|
|
78
|
+
}
|
|
79
|
+
],
|
|
80
|
+
default: "oAuth2",
|
|
81
|
+
description: "How to authenticate with AgnicWallet"
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
displayName: "Method",
|
|
85
|
+
name: "method",
|
|
86
|
+
type: "options",
|
|
87
|
+
options: [
|
|
88
|
+
{
|
|
89
|
+
name: "GET",
|
|
90
|
+
value: "GET"
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
name: "POST",
|
|
94
|
+
value: "POST"
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: "PUT",
|
|
98
|
+
value: "PUT"
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
name: "DELETE",
|
|
102
|
+
value: "DELETE"
|
|
103
|
+
}
|
|
104
|
+
],
|
|
105
|
+
default: "GET",
|
|
106
|
+
description: "The HTTP method to use"
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
displayName: "URL",
|
|
110
|
+
name: "url",
|
|
111
|
+
type: "string",
|
|
112
|
+
default: "",
|
|
113
|
+
required: true,
|
|
114
|
+
placeholder: "https://api.example.com/endpoint",
|
|
115
|
+
description: "The URL of the X402-enabled API"
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
displayName: "Headers",
|
|
119
|
+
name: "headers",
|
|
120
|
+
type: "fixedCollection",
|
|
121
|
+
typeOptions: {
|
|
122
|
+
multipleValues: true
|
|
123
|
+
},
|
|
124
|
+
default: {},
|
|
125
|
+
options: [
|
|
126
|
+
{
|
|
127
|
+
name: "header",
|
|
128
|
+
displayName: "Header",
|
|
129
|
+
values: [
|
|
94
130
|
{
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
},
|
|
101
|
-
default: {},
|
|
102
|
-
options: [
|
|
103
|
-
{
|
|
104
|
-
name: "header",
|
|
105
|
-
displayName: "Header",
|
|
106
|
-
values: [
|
|
107
|
-
{
|
|
108
|
-
displayName: "Name",
|
|
109
|
-
name: "name",
|
|
110
|
-
type: "string",
|
|
111
|
-
default: "",
|
|
112
|
-
description: "Name of the header",
|
|
113
|
-
},
|
|
114
|
-
{
|
|
115
|
-
displayName: "Value",
|
|
116
|
-
name: "value",
|
|
117
|
-
type: "string",
|
|
118
|
-
default: "",
|
|
119
|
-
description: "Value of the header",
|
|
120
|
-
},
|
|
121
|
-
],
|
|
122
|
-
},
|
|
123
|
-
],
|
|
124
|
-
description: "Additional headers to send with the request",
|
|
131
|
+
displayName: "Name",
|
|
132
|
+
name: "name",
|
|
133
|
+
type: "string",
|
|
134
|
+
default: "",
|
|
135
|
+
description: "Name of the header"
|
|
125
136
|
},
|
|
126
137
|
{
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
show: {
|
|
133
|
-
method: ["POST", "PUT"],
|
|
134
|
-
},
|
|
135
|
-
},
|
|
136
|
-
description: "JSON body to send with the request",
|
|
137
|
-
},
|
|
138
|
-
],
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
async execute() {
|
|
142
|
-
const items = this.getInputData();
|
|
143
|
-
const returnData = [];
|
|
144
|
-
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
|
145
|
-
try {
|
|
146
|
-
// Get authentication type
|
|
147
|
-
const authentication = this.getNodeParameter("authentication", itemIndex);
|
|
148
|
-
let apiBaseUrl;
|
|
149
|
-
let authHeader;
|
|
150
|
-
// Use AgnicWallet backend API endpoint (production cloud)
|
|
151
|
-
apiBaseUrl = "https://api.agnicpay.xyz";
|
|
152
|
-
if (authentication === "oAuth2") {
|
|
153
|
-
// OAuth2 authentication
|
|
154
|
-
const credentials = (await this.getCredentials("agnicWalletOAuth2Api", itemIndex));
|
|
155
|
-
authHeader = `Bearer ${credentials.oauthTokenData.access_token}`;
|
|
138
|
+
displayName: "Value",
|
|
139
|
+
name: "value",
|
|
140
|
+
type: "string",
|
|
141
|
+
default: "",
|
|
142
|
+
description: "Value of the header"
|
|
156
143
|
}
|
|
157
|
-
|
|
158
|
-
// API Key authentication
|
|
159
|
-
const credentials = await this.getCredentials("agnicWalletApi", itemIndex);
|
|
160
|
-
const { apiToken } = credentials;
|
|
161
|
-
authHeader = apiToken;
|
|
162
|
-
}
|
|
163
|
-
// Get parameters
|
|
164
|
-
const method = this.getNodeParameter("method", itemIndex);
|
|
165
|
-
const url = this.getNodeParameter("url", itemIndex);
|
|
166
|
-
const headersConfig = this.getNodeParameter("headers", itemIndex, {});
|
|
167
|
-
const body = this.getNodeParameter("body", itemIndex, "{}");
|
|
168
|
-
// Build headers
|
|
169
|
-
const headers = {
|
|
170
|
-
"Content-Type": "application/json",
|
|
171
|
-
};
|
|
172
|
-
if (headersConfig.header) {
|
|
173
|
-
headersConfig.header.forEach((h) => {
|
|
174
|
-
headers[h.name] = h.value;
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
// Make request with X402 payment handling
|
|
178
|
-
const response = await makeX402Request(this, {
|
|
179
|
-
url,
|
|
180
|
-
method,
|
|
181
|
-
headers,
|
|
182
|
-
body: method === "POST" || method === "PUT"
|
|
183
|
-
? JSON.parse(body)
|
|
184
|
-
: undefined,
|
|
185
|
-
agnicWalletApi: apiBaseUrl,
|
|
186
|
-
authHeader,
|
|
187
|
-
isOAuth: authentication === "oAuth2",
|
|
188
|
-
});
|
|
189
|
-
returnData.push({
|
|
190
|
-
json: response,
|
|
191
|
-
pairedItem: {
|
|
192
|
-
item: itemIndex,
|
|
193
|
-
},
|
|
194
|
-
});
|
|
144
|
+
]
|
|
195
145
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
continue;
|
|
208
|
-
}
|
|
209
|
-
throw new n8n_workflow_1.NodeOperationError(this.getNode(), errorMessage, {
|
|
210
|
-
itemIndex,
|
|
211
|
-
});
|
|
146
|
+
],
|
|
147
|
+
description: "Additional headers to send with the request"
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
displayName: "Body",
|
|
151
|
+
name: "body",
|
|
152
|
+
type: "json",
|
|
153
|
+
default: "{}",
|
|
154
|
+
displayOptions: {
|
|
155
|
+
show: {
|
|
156
|
+
method: ["POST", "PUT"]
|
|
212
157
|
}
|
|
158
|
+
},
|
|
159
|
+
description: "JSON body to send with the request"
|
|
213
160
|
}
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
161
|
+
]
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
static {
|
|
165
|
+
__name(this, "X402HttpRequest");
|
|
166
|
+
}
|
|
167
|
+
async execute() {
|
|
168
|
+
const items = this.getInputData();
|
|
169
|
+
const returnData = [];
|
|
170
|
+
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
|
171
|
+
try {
|
|
172
|
+
const authentication = this.getNodeParameter(
|
|
173
|
+
"authentication",
|
|
174
|
+
itemIndex
|
|
175
|
+
);
|
|
176
|
+
let apiBaseUrl;
|
|
177
|
+
let authHeader;
|
|
178
|
+
apiBaseUrl = "https://api.agnicpay.xyz";
|
|
179
|
+
if (authentication === "oAuth2") {
|
|
180
|
+
const credentials = await this.getCredentials(
|
|
181
|
+
"agnicWalletOAuth2Api",
|
|
182
|
+
itemIndex
|
|
183
|
+
);
|
|
184
|
+
authHeader = `Bearer ${credentials.oauthTokenData.access_token}`;
|
|
185
|
+
} else {
|
|
186
|
+
const credentials = await this.getCredentials(
|
|
187
|
+
"agnicWalletApi",
|
|
188
|
+
itemIndex
|
|
189
|
+
);
|
|
190
|
+
const { apiToken } = credentials;
|
|
191
|
+
authHeader = apiToken;
|
|
192
|
+
}
|
|
193
|
+
const method = this.getNodeParameter("method", itemIndex);
|
|
194
|
+
const url = this.getNodeParameter("url", itemIndex);
|
|
195
|
+
const headersConfig = this.getNodeParameter(
|
|
196
|
+
"headers",
|
|
197
|
+
itemIndex,
|
|
198
|
+
{}
|
|
199
|
+
);
|
|
200
|
+
const body = this.getNodeParameter("body", itemIndex, "{}");
|
|
201
|
+
const headers = {
|
|
202
|
+
"Content-Type": "application/json"
|
|
203
|
+
};
|
|
204
|
+
if (headersConfig.header) {
|
|
205
|
+
headersConfig.header.forEach((h) => {
|
|
206
|
+
headers[h.name] = h.value;
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
const response = await makeX402Request(this, {
|
|
210
|
+
url,
|
|
211
|
+
method,
|
|
212
|
+
headers,
|
|
213
|
+
body: method === "POST" || method === "PUT" ? JSON.parse(body) : void 0,
|
|
214
|
+
agnicWalletApi: apiBaseUrl,
|
|
215
|
+
authHeader,
|
|
216
|
+
isOAuth: authentication === "oAuth2"
|
|
228
217
|
});
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
// 3. Call AgnicWallet signing API
|
|
245
|
-
context.logger.info(`Calling AgnicWallet API at: ${agnicWalletApi}/api/sign-payment`);
|
|
246
|
-
// Build auth headers based on auth type
|
|
247
|
-
const authHeaders = {
|
|
248
|
-
"Content-Type": "application/json",
|
|
249
|
-
};
|
|
250
|
-
if (isOAuth) {
|
|
251
|
-
// OAuth: Use Authorization header with Bearer token
|
|
252
|
-
authHeaders["Authorization"] = authHeader;
|
|
253
|
-
}
|
|
254
|
-
else {
|
|
255
|
-
// API Key: Use X-Agnic-Token header
|
|
256
|
-
authHeaders["X-Agnic-Token"] = authHeader;
|
|
257
|
-
}
|
|
258
|
-
const signingResponse = await fetch(`${agnicWalletApi}/api/sign-payment`, {
|
|
259
|
-
method: "POST",
|
|
260
|
-
headers: authHeaders,
|
|
261
|
-
body: JSON.stringify({
|
|
262
|
-
paymentRequirements,
|
|
263
|
-
requestData: { url, method, body },
|
|
264
|
-
}),
|
|
265
|
-
});
|
|
266
|
-
if (!signingResponse.ok) {
|
|
267
|
-
const errorText = await signingResponse.text();
|
|
268
|
-
context.logger.error(`AgnicWallet signing failed: ${errorText}`);
|
|
269
|
-
throw new Error(`Payment signing failed (${signingResponse.status}): ${errorText}`);
|
|
270
|
-
}
|
|
271
|
-
const signingResult = (await signingResponse.json());
|
|
272
|
-
const { paymentHeader, amountPaid } = signingResult;
|
|
273
|
-
context.logger.info(`Payment signed successfully: $${amountPaid}`);
|
|
274
|
-
context.logger.info(`Payment header (Base64): ${paymentHeader}`);
|
|
275
|
-
// 4. Retry original request with payment header
|
|
276
|
-
// The paymentHeader is already Base64-encoded and X402-compliant
|
|
277
|
-
context.logger.info("Retrying request with X402 payment header...");
|
|
278
|
-
response = await fetch(url, {
|
|
279
|
-
method,
|
|
280
|
-
headers: {
|
|
281
|
-
...headers,
|
|
282
|
-
"X-PAYMENT": paymentHeader,
|
|
283
|
-
},
|
|
284
|
-
body: body ? JSON.stringify(body) : undefined,
|
|
285
|
-
});
|
|
286
|
-
context.logger.info(`Retry response status: ${response.status}`);
|
|
287
|
-
if (!response.ok) {
|
|
288
|
-
const errorText = await response.text();
|
|
289
|
-
throw new Error(`Request failed after payment (${response.status}): ${errorText}`);
|
|
290
|
-
}
|
|
291
|
-
const resultText = await response.text();
|
|
292
|
-
let result;
|
|
293
|
-
try {
|
|
294
|
-
result = JSON.parse(resultText);
|
|
295
|
-
}
|
|
296
|
-
catch (parseError) {
|
|
297
|
-
context.logger.warn(`Response is not JSON: ${resultText}`);
|
|
298
|
-
result = { data: resultText };
|
|
218
|
+
returnData.push({
|
|
219
|
+
json: response,
|
|
220
|
+
pairedItem: {
|
|
221
|
+
item: itemIndex
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
} catch (error) {
|
|
225
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
|
226
|
+
if (this.continueOnFail()) {
|
|
227
|
+
returnData.push({
|
|
228
|
+
json: {
|
|
229
|
+
error: errorMessage
|
|
230
|
+
},
|
|
231
|
+
pairedItem: {
|
|
232
|
+
item: itemIndex
|
|
299
233
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
_agnicWallet: {
|
|
303
|
-
paymentMade: true,
|
|
304
|
-
amountPaid,
|
|
305
|
-
timestamp: new Date().toISOString(),
|
|
306
|
-
},
|
|
307
|
-
};
|
|
234
|
+
});
|
|
235
|
+
continue;
|
|
308
236
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
237
|
+
throw new import_n8n_workflow.NodeOperationError(this.getNode(), errorMessage, {
|
|
238
|
+
itemIndex
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return [returnData];
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
async function makeX402Request(context, config) {
|
|
246
|
+
const { url, method, headers, body, agnicWalletApi, authHeader, isOAuth } = config;
|
|
247
|
+
try {
|
|
248
|
+
context.logger.info(`Making initial request to: ${url}`);
|
|
249
|
+
let response = await fetch(url, {
|
|
250
|
+
method,
|
|
251
|
+
headers,
|
|
252
|
+
body: body ? JSON.stringify(body) : void 0
|
|
253
|
+
});
|
|
254
|
+
context.logger.info(`Response status: ${response.status}`);
|
|
255
|
+
if (response.status === 402) {
|
|
256
|
+
context.logger.info(
|
|
257
|
+
"402 Payment Required detected, parsing payment requirements..."
|
|
258
|
+
);
|
|
259
|
+
const responseText = await response.text();
|
|
260
|
+
context.logger.info(`Payment requirements response: ${responseText}`);
|
|
261
|
+
let paymentRequirements;
|
|
262
|
+
try {
|
|
263
|
+
paymentRequirements = JSON.parse(responseText);
|
|
264
|
+
} catch (parseError) {
|
|
265
|
+
throw new Error(
|
|
266
|
+
`Failed to parse payment requirements: ${parseError instanceof Error ? parseError.message : "Unknown error"}. Response: ${responseText}`
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
context.logger.info(
|
|
270
|
+
`Payment required: ${JSON.stringify(paymentRequirements)}`
|
|
271
|
+
);
|
|
272
|
+
context.logger.info(
|
|
273
|
+
`Calling AgnicWallet API at: ${agnicWalletApi}/api/sign-payment`
|
|
274
|
+
);
|
|
275
|
+
const authHeaders = {
|
|
276
|
+
"Content-Type": "application/json"
|
|
277
|
+
};
|
|
278
|
+
if (isOAuth) {
|
|
279
|
+
authHeaders["Authorization"] = authHeader;
|
|
280
|
+
} else {
|
|
281
|
+
authHeaders["X-Agnic-Token"] = authHeader;
|
|
282
|
+
}
|
|
283
|
+
const signingResponse = await fetch(
|
|
284
|
+
`${agnicWalletApi}/api/sign-payment`,
|
|
285
|
+
{
|
|
286
|
+
method: "POST",
|
|
287
|
+
headers: authHeaders,
|
|
288
|
+
body: JSON.stringify({
|
|
289
|
+
paymentRequirements,
|
|
290
|
+
requestData: { url, method, body }
|
|
291
|
+
})
|
|
317
292
|
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
293
|
+
);
|
|
294
|
+
if (!signingResponse.ok) {
|
|
295
|
+
const errorText = await signingResponse.text();
|
|
296
|
+
context.logger.error(`AgnicWallet signing failed: ${errorText}`);
|
|
297
|
+
throw new Error(
|
|
298
|
+
`Payment signing failed (${signingResponse.status}): ${errorText}`
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
const signingResult = await signingResponse.json();
|
|
302
|
+
const { paymentHeader, amountPaid } = signingResult;
|
|
303
|
+
context.logger.info(`Payment signed successfully: $${amountPaid}`);
|
|
304
|
+
context.logger.info(`Payment header (Base64): ${paymentHeader}`);
|
|
305
|
+
context.logger.info("Retrying request with X402 payment header...");
|
|
306
|
+
response = await fetch(url, {
|
|
307
|
+
method,
|
|
308
|
+
headers: {
|
|
309
|
+
...headers,
|
|
310
|
+
"X-PAYMENT": paymentHeader
|
|
311
|
+
},
|
|
312
|
+
body: body ? JSON.stringify(body) : void 0
|
|
313
|
+
});
|
|
314
|
+
context.logger.info(`Retry response status: ${response.status}`);
|
|
315
|
+
if (!response.ok) {
|
|
316
|
+
const errorText = await response.text();
|
|
317
|
+
throw new Error(
|
|
318
|
+
`Request failed after payment (${response.status}): ${errorText}`
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
const resultText2 = await response.text();
|
|
322
|
+
let result;
|
|
323
|
+
try {
|
|
324
|
+
result = JSON.parse(resultText2);
|
|
325
|
+
} catch (parseError) {
|
|
326
|
+
context.logger.warn(`Response is not JSON: ${resultText2}`);
|
|
327
|
+
result = { data: resultText2 };
|
|
328
|
+
}
|
|
329
|
+
return {
|
|
330
|
+
...typeof result === "object" && result !== null ? result : {},
|
|
331
|
+
_agnicWallet: {
|
|
332
|
+
paymentMade: true,
|
|
333
|
+
amountPaid,
|
|
334
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
321
335
|
}
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
if (!response.ok) {
|
|
339
|
+
const errorText = await response.text();
|
|
340
|
+
throw new Error(
|
|
341
|
+
`Request failed (${response.status} ${response.statusText}): ${errorText}`
|
|
342
|
+
);
|
|
322
343
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
344
|
+
const resultText = await response.text();
|
|
345
|
+
try {
|
|
346
|
+
return JSON.parse(resultText);
|
|
347
|
+
} catch (parseError) {
|
|
348
|
+
context.logger.warn(`Response is not JSON: ${resultText}`);
|
|
349
|
+
return { data: resultText };
|
|
326
350
|
}
|
|
351
|
+
} catch (error) {
|
|
352
|
+
context.logger.error(
|
|
353
|
+
`Error in makeX402Request: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
354
|
+
);
|
|
355
|
+
throw error;
|
|
356
|
+
}
|
|
327
357
|
}
|
|
358
|
+
__name(makeX402Request, "makeX402Request");
|
|
359
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
360
|
+
0 && (module.exports = {
|
|
361
|
+
X402HttpRequest
|
|
362
|
+
});
|