@x402/extensions 2.2.0 → 2.3.0
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 +322 -93
- package/dist/cjs/bazaar/index.d.ts +3 -562
- package/dist/cjs/bazaar/index.js +12 -0
- package/dist/cjs/bazaar/index.js.map +1 -1
- package/dist/cjs/index-DvDlinmy.d.ts +575 -0
- package/dist/cjs/index.d.ts +4 -1
- package/dist/cjs/index.js +1008 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/payment-identifier/index.d.ts +345 -0
- package/dist/cjs/payment-identifier/index.js +285 -0
- package/dist/cjs/payment-identifier/index.js.map +1 -0
- package/dist/cjs/sign-in-with-x/index.d.ts +1054 -1
- package/dist/cjs/sign-in-with-x/index.js +766 -0
- package/dist/cjs/sign-in-with-x/index.js.map +1 -1
- package/dist/esm/bazaar/index.d.mts +3 -562
- package/dist/esm/bazaar/index.mjs +1 -1
- package/dist/esm/chunk-73HCOE6N.mjs +233 -0
- package/dist/esm/chunk-73HCOE6N.mjs.map +1 -0
- package/dist/esm/{chunk-WB72GLC2.mjs → chunk-DFJ4ZQFO.mjs} +13 -1
- package/dist/esm/chunk-DFJ4ZQFO.mjs.map +1 -0
- package/dist/esm/chunk-E3F2XHTI.mjs +719 -0
- package/dist/esm/chunk-E3F2XHTI.mjs.map +1 -0
- package/dist/esm/index-DvDlinmy.d.mts +575 -0
- package/dist/esm/index.d.mts +4 -1
- package/dist/esm/index.mjs +102 -3
- package/dist/esm/payment-identifier/index.d.mts +345 -0
- package/dist/esm/payment-identifier/index.mjs +39 -0
- package/dist/esm/sign-in-with-x/index.d.mts +1054 -1
- package/dist/esm/sign-in-with-x/index.mjs +66 -1
- package/package.json +16 -2
- package/dist/esm/chunk-MKFJ5AA3.mjs +0 -1
- package/dist/esm/chunk-WB72GLC2.mjs.map +0 -1
- /package/dist/esm/{chunk-MKFJ5AA3.mjs.map → payment-identifier/index.mjs.map} +0 -0
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
6
12
|
var __copyProps = (to, from, except, desc) => {
|
|
7
13
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
14
|
for (let key of __getOwnPropNames(from))
|
|
@@ -11,9 +17,769 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
11
17
|
}
|
|
12
18
|
return to;
|
|
13
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
14
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
29
|
|
|
16
30
|
// src/sign-in-with-x/index.ts
|
|
17
31
|
var sign_in_with_x_exports = {};
|
|
32
|
+
__export(sign_in_with_x_exports, {
|
|
33
|
+
InMemorySIWxStorage: () => InMemorySIWxStorage,
|
|
34
|
+
SIGN_IN_WITH_X: () => SIGN_IN_WITH_X,
|
|
35
|
+
SIWxPayloadSchema: () => SIWxPayloadSchema,
|
|
36
|
+
SOLANA_DEVNET: () => SOLANA_DEVNET,
|
|
37
|
+
SOLANA_MAINNET: () => SOLANA_MAINNET,
|
|
38
|
+
SOLANA_TESTNET: () => SOLANA_TESTNET,
|
|
39
|
+
buildSIWxSchema: () => buildSIWxSchema,
|
|
40
|
+
createSIWxClientHook: () => createSIWxClientHook,
|
|
41
|
+
createSIWxMessage: () => createSIWxMessage,
|
|
42
|
+
createSIWxPayload: () => createSIWxPayload,
|
|
43
|
+
createSIWxRequestHook: () => createSIWxRequestHook,
|
|
44
|
+
createSIWxSettleHook: () => createSIWxSettleHook,
|
|
45
|
+
declareSIWxExtension: () => declareSIWxExtension,
|
|
46
|
+
decodeBase58: () => decodeBase58,
|
|
47
|
+
encodeBase58: () => encodeBase58,
|
|
48
|
+
encodeSIWxHeader: () => encodeSIWxHeader,
|
|
49
|
+
extractEVMChainId: () => extractEVMChainId,
|
|
50
|
+
extractSolanaChainReference: () => extractSolanaChainReference,
|
|
51
|
+
formatSIWEMessage: () => formatSIWEMessage,
|
|
52
|
+
formatSIWSMessage: () => formatSIWSMessage,
|
|
53
|
+
getEVMAddress: () => getEVMAddress,
|
|
54
|
+
getSolanaAddress: () => getSolanaAddress,
|
|
55
|
+
parseSIWxHeader: () => parseSIWxHeader,
|
|
56
|
+
signEVMMessage: () => signEVMMessage,
|
|
57
|
+
signSolanaMessage: () => signSolanaMessage,
|
|
58
|
+
siwxResourceServerExtension: () => siwxResourceServerExtension,
|
|
59
|
+
validateSIWxMessage: () => validateSIWxMessage,
|
|
60
|
+
verifyEVMSignature: () => verifyEVMSignature,
|
|
61
|
+
verifySIWxSignature: () => verifySIWxSignature,
|
|
62
|
+
verifySolanaSignature: () => verifySolanaSignature,
|
|
63
|
+
wrapFetchWithSIWx: () => wrapFetchWithSIWx
|
|
64
|
+
});
|
|
18
65
|
module.exports = __toCommonJS(sign_in_with_x_exports);
|
|
66
|
+
|
|
67
|
+
// src/sign-in-with-x/types.ts
|
|
68
|
+
var import_zod = require("zod");
|
|
69
|
+
var SIGN_IN_WITH_X = "sign-in-with-x";
|
|
70
|
+
var SIWxPayloadSchema = import_zod.z.object({
|
|
71
|
+
domain: import_zod.z.string(),
|
|
72
|
+
address: import_zod.z.string(),
|
|
73
|
+
statement: import_zod.z.string().optional(),
|
|
74
|
+
uri: import_zod.z.string(),
|
|
75
|
+
version: import_zod.z.string(),
|
|
76
|
+
chainId: import_zod.z.string(),
|
|
77
|
+
type: import_zod.z.enum(["eip191", "ed25519"]),
|
|
78
|
+
nonce: import_zod.z.string(),
|
|
79
|
+
issuedAt: import_zod.z.string(),
|
|
80
|
+
expirationTime: import_zod.z.string().optional(),
|
|
81
|
+
notBefore: import_zod.z.string().optional(),
|
|
82
|
+
requestId: import_zod.z.string().optional(),
|
|
83
|
+
resources: import_zod.z.array(import_zod.z.string()).optional(),
|
|
84
|
+
signatureScheme: import_zod.z.enum(["eip191", "eip1271", "eip6492", "siws"]).optional(),
|
|
85
|
+
signature: import_zod.z.string()
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// src/sign-in-with-x/solana.ts
|
|
89
|
+
var import_base = require("@scure/base");
|
|
90
|
+
var import_tweetnacl = __toESM(require("tweetnacl"));
|
|
91
|
+
var SOLANA_MAINNET = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp";
|
|
92
|
+
var SOLANA_DEVNET = "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1";
|
|
93
|
+
var SOLANA_TESTNET = "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z";
|
|
94
|
+
function extractSolanaChainReference(chainId) {
|
|
95
|
+
const [, reference] = chainId.split(":");
|
|
96
|
+
return reference;
|
|
97
|
+
}
|
|
98
|
+
function formatSIWSMessage(info, address) {
|
|
99
|
+
const lines = [
|
|
100
|
+
`${info.domain} wants you to sign in with your Solana account:`,
|
|
101
|
+
address,
|
|
102
|
+
""
|
|
103
|
+
];
|
|
104
|
+
if (info.statement) {
|
|
105
|
+
lines.push(info.statement, "");
|
|
106
|
+
}
|
|
107
|
+
lines.push(
|
|
108
|
+
`URI: ${info.uri}`,
|
|
109
|
+
`Version: ${info.version}`,
|
|
110
|
+
`Chain ID: ${extractSolanaChainReference(info.chainId)}`,
|
|
111
|
+
`Nonce: ${info.nonce}`,
|
|
112
|
+
`Issued At: ${info.issuedAt}`
|
|
113
|
+
);
|
|
114
|
+
if (info.expirationTime) {
|
|
115
|
+
lines.push(`Expiration Time: ${info.expirationTime}`);
|
|
116
|
+
}
|
|
117
|
+
if (info.notBefore) {
|
|
118
|
+
lines.push(`Not Before: ${info.notBefore}`);
|
|
119
|
+
}
|
|
120
|
+
if (info.requestId) {
|
|
121
|
+
lines.push(`Request ID: ${info.requestId}`);
|
|
122
|
+
}
|
|
123
|
+
if (info.resources && info.resources.length > 0) {
|
|
124
|
+
lines.push("Resources:");
|
|
125
|
+
for (const resource of info.resources) {
|
|
126
|
+
lines.push(`- ${resource}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return lines.join("\n");
|
|
130
|
+
}
|
|
131
|
+
function verifySolanaSignature(message, signature, publicKey) {
|
|
132
|
+
const messageBytes = new TextEncoder().encode(message);
|
|
133
|
+
return import_tweetnacl.default.sign.detached.verify(messageBytes, signature, publicKey);
|
|
134
|
+
}
|
|
135
|
+
function decodeBase58(encoded) {
|
|
136
|
+
return import_base.base58.decode(encoded);
|
|
137
|
+
}
|
|
138
|
+
function encodeBase58(bytes) {
|
|
139
|
+
return import_base.base58.encode(bytes);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/sign-in-with-x/schema.ts
|
|
143
|
+
function buildSIWxSchema() {
|
|
144
|
+
return {
|
|
145
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
146
|
+
type: "object",
|
|
147
|
+
properties: {
|
|
148
|
+
domain: { type: "string" },
|
|
149
|
+
address: { type: "string" },
|
|
150
|
+
statement: { type: "string" },
|
|
151
|
+
uri: { type: "string", format: "uri" },
|
|
152
|
+
version: { type: "string" },
|
|
153
|
+
chainId: { type: "string" },
|
|
154
|
+
type: { type: "string" },
|
|
155
|
+
nonce: { type: "string" },
|
|
156
|
+
issuedAt: { type: "string", format: "date-time" },
|
|
157
|
+
expirationTime: { type: "string", format: "date-time" },
|
|
158
|
+
notBefore: { type: "string", format: "date-time" },
|
|
159
|
+
requestId: { type: "string" },
|
|
160
|
+
resources: { type: "array", items: { type: "string", format: "uri" } },
|
|
161
|
+
signature: { type: "string" }
|
|
162
|
+
},
|
|
163
|
+
required: [
|
|
164
|
+
"domain",
|
|
165
|
+
"address",
|
|
166
|
+
"uri",
|
|
167
|
+
"version",
|
|
168
|
+
"chainId",
|
|
169
|
+
"type",
|
|
170
|
+
"nonce",
|
|
171
|
+
"issuedAt",
|
|
172
|
+
"signature"
|
|
173
|
+
]
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// src/sign-in-with-x/declare.ts
|
|
178
|
+
function getSignatureType(network) {
|
|
179
|
+
return network.startsWith("solana:") ? "ed25519" : "eip191";
|
|
180
|
+
}
|
|
181
|
+
function declareSIWxExtension(options = {}) {
|
|
182
|
+
const info = {
|
|
183
|
+
version: options.version ?? "1"
|
|
184
|
+
};
|
|
185
|
+
if (options.domain) {
|
|
186
|
+
info.domain = options.domain;
|
|
187
|
+
}
|
|
188
|
+
if (options.resourceUri) {
|
|
189
|
+
info.uri = options.resourceUri;
|
|
190
|
+
info.resources = [options.resourceUri];
|
|
191
|
+
}
|
|
192
|
+
if (options.statement) {
|
|
193
|
+
info.statement = options.statement;
|
|
194
|
+
}
|
|
195
|
+
let supportedChains = [];
|
|
196
|
+
if (options.network) {
|
|
197
|
+
const networks = Array.isArray(options.network) ? options.network : [options.network];
|
|
198
|
+
supportedChains = networks.map((network) => ({
|
|
199
|
+
chainId: network,
|
|
200
|
+
type: getSignatureType(network)
|
|
201
|
+
}));
|
|
202
|
+
}
|
|
203
|
+
const declaration = {
|
|
204
|
+
info,
|
|
205
|
+
supportedChains,
|
|
206
|
+
schema: buildSIWxSchema(),
|
|
207
|
+
_options: options
|
|
208
|
+
};
|
|
209
|
+
return { [SIGN_IN_WITH_X]: declaration };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// src/sign-in-with-x/server.ts
|
|
213
|
+
var import_crypto = require("crypto");
|
|
214
|
+
var siwxResourceServerExtension = {
|
|
215
|
+
key: SIGN_IN_WITH_X,
|
|
216
|
+
enrichPaymentRequiredResponse: async (declaration, context) => {
|
|
217
|
+
const decl = declaration;
|
|
218
|
+
const opts = decl._options ?? {};
|
|
219
|
+
const resourceUri = opts.resourceUri ?? context.resourceInfo.url;
|
|
220
|
+
let domain = opts.domain;
|
|
221
|
+
if (!domain && resourceUri) {
|
|
222
|
+
try {
|
|
223
|
+
domain = new URL(resourceUri).hostname;
|
|
224
|
+
} catch {
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
let networks;
|
|
228
|
+
if (opts.network) {
|
|
229
|
+
networks = Array.isArray(opts.network) ? opts.network : [opts.network];
|
|
230
|
+
} else {
|
|
231
|
+
networks = [...new Set(context.requirements.map((r) => r.network))];
|
|
232
|
+
}
|
|
233
|
+
const nonce = (0, import_crypto.randomBytes)(16).toString("hex");
|
|
234
|
+
const issuedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
235
|
+
const expirationSeconds = opts.expirationSeconds;
|
|
236
|
+
const expirationTime = expirationSeconds !== void 0 ? new Date(Date.now() + expirationSeconds * 1e3).toISOString() : void 0;
|
|
237
|
+
const info = {
|
|
238
|
+
domain: domain ?? "",
|
|
239
|
+
uri: resourceUri,
|
|
240
|
+
version: opts.version ?? "1",
|
|
241
|
+
nonce,
|
|
242
|
+
issuedAt,
|
|
243
|
+
resources: [resourceUri]
|
|
244
|
+
};
|
|
245
|
+
if (expirationTime) {
|
|
246
|
+
info.expirationTime = expirationTime;
|
|
247
|
+
}
|
|
248
|
+
if (opts.statement) {
|
|
249
|
+
info.statement = opts.statement;
|
|
250
|
+
}
|
|
251
|
+
const supportedChains = networks.map((network) => ({
|
|
252
|
+
chainId: network,
|
|
253
|
+
type: getSignatureType(network)
|
|
254
|
+
}));
|
|
255
|
+
return {
|
|
256
|
+
info,
|
|
257
|
+
supportedChains,
|
|
258
|
+
schema: buildSIWxSchema()
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// src/sign-in-with-x/parse.ts
|
|
264
|
+
var import_utils = require("@x402/core/utils");
|
|
265
|
+
function parseSIWxHeader(header) {
|
|
266
|
+
if (!import_utils.Base64EncodedRegex.test(header)) {
|
|
267
|
+
throw new Error("Invalid SIWX header: not valid base64");
|
|
268
|
+
}
|
|
269
|
+
const jsonStr = (0, import_utils.safeBase64Decode)(header);
|
|
270
|
+
let rawPayload;
|
|
271
|
+
try {
|
|
272
|
+
rawPayload = JSON.parse(jsonStr);
|
|
273
|
+
} catch (error) {
|
|
274
|
+
if (error instanceof SyntaxError) {
|
|
275
|
+
throw new Error("Invalid SIWX header: not valid JSON");
|
|
276
|
+
}
|
|
277
|
+
throw error;
|
|
278
|
+
}
|
|
279
|
+
const parsed = SIWxPayloadSchema.safeParse(rawPayload);
|
|
280
|
+
if (!parsed.success) {
|
|
281
|
+
const issues = parsed.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join(", ");
|
|
282
|
+
throw new Error(`Invalid SIWX header: ${issues}`);
|
|
283
|
+
}
|
|
284
|
+
return parsed.data;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// src/sign-in-with-x/validate.ts
|
|
288
|
+
var DEFAULT_MAX_AGE_MS = 5 * 60 * 1e3;
|
|
289
|
+
async function validateSIWxMessage(message, expectedResourceUri, options = {}) {
|
|
290
|
+
const expectedUrl = new URL(expectedResourceUri);
|
|
291
|
+
const maxAge = options.maxAge ?? DEFAULT_MAX_AGE_MS;
|
|
292
|
+
if (message.domain !== expectedUrl.hostname) {
|
|
293
|
+
return {
|
|
294
|
+
valid: false,
|
|
295
|
+
error: `Domain mismatch: expected "${expectedUrl.hostname}", got "${message.domain}"`
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
if (!message.uri.startsWith(expectedUrl.origin)) {
|
|
299
|
+
return {
|
|
300
|
+
valid: false,
|
|
301
|
+
error: `URI mismatch: expected origin "${expectedUrl.origin}", got "${message.uri}"`
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
const issuedAt = new Date(message.issuedAt);
|
|
305
|
+
if (isNaN(issuedAt.getTime())) {
|
|
306
|
+
return {
|
|
307
|
+
valid: false,
|
|
308
|
+
error: "Invalid issuedAt timestamp"
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
const age = Date.now() - issuedAt.getTime();
|
|
312
|
+
if (age > maxAge) {
|
|
313
|
+
return {
|
|
314
|
+
valid: false,
|
|
315
|
+
error: `Message too old: ${Math.round(age / 1e3)}s exceeds ${maxAge / 1e3}s limit`
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
if (age < 0) {
|
|
319
|
+
return {
|
|
320
|
+
valid: false,
|
|
321
|
+
error: "issuedAt is in the future"
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
if (message.expirationTime) {
|
|
325
|
+
const expiration = new Date(message.expirationTime);
|
|
326
|
+
if (isNaN(expiration.getTime())) {
|
|
327
|
+
return {
|
|
328
|
+
valid: false,
|
|
329
|
+
error: "Invalid expirationTime timestamp"
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
if (expiration < /* @__PURE__ */ new Date()) {
|
|
333
|
+
return {
|
|
334
|
+
valid: false,
|
|
335
|
+
error: "Message expired"
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (message.notBefore) {
|
|
340
|
+
const notBefore = new Date(message.notBefore);
|
|
341
|
+
if (isNaN(notBefore.getTime())) {
|
|
342
|
+
return {
|
|
343
|
+
valid: false,
|
|
344
|
+
error: "Invalid notBefore timestamp"
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
if (/* @__PURE__ */ new Date() < notBefore) {
|
|
348
|
+
return {
|
|
349
|
+
valid: false,
|
|
350
|
+
error: "Message not yet valid (notBefore is in the future)"
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (options.checkNonce) {
|
|
355
|
+
const nonceValid = await options.checkNonce(message.nonce);
|
|
356
|
+
if (!nonceValid) {
|
|
357
|
+
return {
|
|
358
|
+
valid: false,
|
|
359
|
+
error: "Nonce validation failed (possible replay attack)"
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return { valid: true };
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// src/sign-in-with-x/evm.ts
|
|
367
|
+
var import_viem = require("viem");
|
|
368
|
+
var import_siwe = require("siwe");
|
|
369
|
+
function extractEVMChainId(chainId) {
|
|
370
|
+
const match = /^eip155:(\d+)$/.exec(chainId);
|
|
371
|
+
if (!match) {
|
|
372
|
+
throw new Error(`Invalid EVM chainId format: ${chainId}. Expected eip155:<number>`);
|
|
373
|
+
}
|
|
374
|
+
return parseInt(match[1], 10);
|
|
375
|
+
}
|
|
376
|
+
function formatSIWEMessage(info, address) {
|
|
377
|
+
const numericChainId = extractEVMChainId(info.chainId);
|
|
378
|
+
const siweMessage = new import_siwe.SiweMessage({
|
|
379
|
+
domain: info.domain,
|
|
380
|
+
address,
|
|
381
|
+
statement: info.statement,
|
|
382
|
+
uri: info.uri,
|
|
383
|
+
version: info.version,
|
|
384
|
+
chainId: numericChainId,
|
|
385
|
+
nonce: info.nonce,
|
|
386
|
+
issuedAt: info.issuedAt,
|
|
387
|
+
expirationTime: info.expirationTime,
|
|
388
|
+
notBefore: info.notBefore,
|
|
389
|
+
requestId: info.requestId,
|
|
390
|
+
resources: info.resources
|
|
391
|
+
});
|
|
392
|
+
return siweMessage.prepareMessage();
|
|
393
|
+
}
|
|
394
|
+
async function verifyEVMSignature(message, address, signature, verifier) {
|
|
395
|
+
const args = {
|
|
396
|
+
address,
|
|
397
|
+
message,
|
|
398
|
+
signature
|
|
399
|
+
};
|
|
400
|
+
if (verifier) {
|
|
401
|
+
return verifier(args);
|
|
402
|
+
}
|
|
403
|
+
return (0, import_viem.verifyMessage)(args);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// src/sign-in-with-x/verify.ts
|
|
407
|
+
async function verifySIWxSignature(payload, options) {
|
|
408
|
+
try {
|
|
409
|
+
if (payload.chainId.startsWith("eip155:")) {
|
|
410
|
+
return verifyEVMPayload(payload, options?.evmVerifier);
|
|
411
|
+
}
|
|
412
|
+
if (payload.chainId.startsWith("solana:")) {
|
|
413
|
+
return verifySolanaPayload(payload);
|
|
414
|
+
}
|
|
415
|
+
return {
|
|
416
|
+
valid: false,
|
|
417
|
+
error: `Unsupported chain namespace: ${payload.chainId}. Supported: eip155:* (EVM), solana:* (Solana)`
|
|
418
|
+
};
|
|
419
|
+
} catch (error) {
|
|
420
|
+
return {
|
|
421
|
+
valid: false,
|
|
422
|
+
error: error instanceof Error ? error.message : "Verification failed"
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
async function verifyEVMPayload(payload, verifier) {
|
|
427
|
+
const message = formatSIWEMessage(
|
|
428
|
+
{
|
|
429
|
+
domain: payload.domain,
|
|
430
|
+
uri: payload.uri,
|
|
431
|
+
statement: payload.statement,
|
|
432
|
+
version: payload.version,
|
|
433
|
+
chainId: payload.chainId,
|
|
434
|
+
type: payload.type,
|
|
435
|
+
nonce: payload.nonce,
|
|
436
|
+
issuedAt: payload.issuedAt,
|
|
437
|
+
expirationTime: payload.expirationTime,
|
|
438
|
+
notBefore: payload.notBefore,
|
|
439
|
+
requestId: payload.requestId,
|
|
440
|
+
resources: payload.resources
|
|
441
|
+
},
|
|
442
|
+
payload.address
|
|
443
|
+
);
|
|
444
|
+
try {
|
|
445
|
+
const valid = await verifyEVMSignature(message, payload.address, payload.signature, verifier);
|
|
446
|
+
if (!valid) {
|
|
447
|
+
return {
|
|
448
|
+
valid: false,
|
|
449
|
+
error: "Signature verification failed"
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
return {
|
|
453
|
+
valid: true,
|
|
454
|
+
address: payload.address
|
|
455
|
+
};
|
|
456
|
+
} catch (error) {
|
|
457
|
+
return {
|
|
458
|
+
valid: false,
|
|
459
|
+
error: error instanceof Error ? error.message : "Signature verification failed"
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
function verifySolanaPayload(payload) {
|
|
464
|
+
const message = formatSIWSMessage(
|
|
465
|
+
{
|
|
466
|
+
domain: payload.domain,
|
|
467
|
+
uri: payload.uri,
|
|
468
|
+
statement: payload.statement,
|
|
469
|
+
version: payload.version,
|
|
470
|
+
chainId: payload.chainId,
|
|
471
|
+
type: payload.type,
|
|
472
|
+
nonce: payload.nonce,
|
|
473
|
+
issuedAt: payload.issuedAt,
|
|
474
|
+
expirationTime: payload.expirationTime,
|
|
475
|
+
notBefore: payload.notBefore,
|
|
476
|
+
requestId: payload.requestId,
|
|
477
|
+
resources: payload.resources
|
|
478
|
+
},
|
|
479
|
+
payload.address
|
|
480
|
+
);
|
|
481
|
+
let signature;
|
|
482
|
+
let publicKey;
|
|
483
|
+
try {
|
|
484
|
+
signature = decodeBase58(payload.signature);
|
|
485
|
+
publicKey = decodeBase58(payload.address);
|
|
486
|
+
} catch (error) {
|
|
487
|
+
return {
|
|
488
|
+
valid: false,
|
|
489
|
+
error: `Invalid Base58 encoding: ${error instanceof Error ? error.message : "decode failed"}`
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
if (signature.length !== 64) {
|
|
493
|
+
return {
|
|
494
|
+
valid: false,
|
|
495
|
+
error: `Invalid signature length: expected 64 bytes, got ${signature.length}`
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
if (publicKey.length !== 32) {
|
|
499
|
+
return {
|
|
500
|
+
valid: false,
|
|
501
|
+
error: `Invalid public key length: expected 32 bytes, got ${publicKey.length}`
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
const valid = verifySolanaSignature(message, signature, publicKey);
|
|
505
|
+
if (!valid) {
|
|
506
|
+
return {
|
|
507
|
+
valid: false,
|
|
508
|
+
error: "Solana signature verification failed"
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
return {
|
|
512
|
+
valid: true,
|
|
513
|
+
address: payload.address
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// src/sign-in-with-x/message.ts
|
|
518
|
+
function createSIWxMessage(serverInfo, address) {
|
|
519
|
+
if (serverInfo.chainId.startsWith("eip155:")) {
|
|
520
|
+
return formatSIWEMessage(serverInfo, address);
|
|
521
|
+
}
|
|
522
|
+
if (serverInfo.chainId.startsWith("solana:")) {
|
|
523
|
+
return formatSIWSMessage(serverInfo, address);
|
|
524
|
+
}
|
|
525
|
+
throw new Error(
|
|
526
|
+
`Unsupported chain namespace: ${serverInfo.chainId}. Supported: eip155:* (EVM), solana:* (Solana)`
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// src/sign-in-with-x/sign.ts
|
|
531
|
+
function getEVMAddress(signer) {
|
|
532
|
+
if (signer.account?.address) {
|
|
533
|
+
return signer.account.address;
|
|
534
|
+
}
|
|
535
|
+
if (signer.address) {
|
|
536
|
+
return signer.address;
|
|
537
|
+
}
|
|
538
|
+
throw new Error("EVM signer missing address");
|
|
539
|
+
}
|
|
540
|
+
function getSolanaAddress(signer) {
|
|
541
|
+
const pk = signer.publicKey;
|
|
542
|
+
return typeof pk === "string" ? pk : pk.toBase58();
|
|
543
|
+
}
|
|
544
|
+
async function signEVMMessage(message, signer) {
|
|
545
|
+
if (signer.account) {
|
|
546
|
+
return signer.signMessage({ message, account: signer.account });
|
|
547
|
+
}
|
|
548
|
+
return signer.signMessage({ message });
|
|
549
|
+
}
|
|
550
|
+
async function signSolanaMessage(message, signer) {
|
|
551
|
+
const messageBytes = new TextEncoder().encode(message);
|
|
552
|
+
const signatureBytes = await signer.signMessage(messageBytes);
|
|
553
|
+
return encodeBase58(signatureBytes);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// src/sign-in-with-x/client.ts
|
|
557
|
+
async function createSIWxPayload(serverExtension, signer) {
|
|
558
|
+
const isSolana = serverExtension.chainId.startsWith("solana:");
|
|
559
|
+
const address = isSolana ? getSolanaAddress(signer) : getEVMAddress(signer);
|
|
560
|
+
const message = createSIWxMessage(serverExtension, address);
|
|
561
|
+
const signature = isSolana ? await signSolanaMessage(message, signer) : await signEVMMessage(message, signer);
|
|
562
|
+
return {
|
|
563
|
+
domain: serverExtension.domain,
|
|
564
|
+
address,
|
|
565
|
+
statement: serverExtension.statement,
|
|
566
|
+
uri: serverExtension.uri,
|
|
567
|
+
version: serverExtension.version,
|
|
568
|
+
chainId: serverExtension.chainId,
|
|
569
|
+
type: serverExtension.type,
|
|
570
|
+
nonce: serverExtension.nonce,
|
|
571
|
+
issuedAt: serverExtension.issuedAt,
|
|
572
|
+
expirationTime: serverExtension.expirationTime,
|
|
573
|
+
notBefore: serverExtension.notBefore,
|
|
574
|
+
requestId: serverExtension.requestId,
|
|
575
|
+
resources: serverExtension.resources,
|
|
576
|
+
signatureScheme: serverExtension.signatureScheme,
|
|
577
|
+
signature
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// src/sign-in-with-x/encode.ts
|
|
582
|
+
var import_utils2 = require("@x402/core/utils");
|
|
583
|
+
function encodeSIWxHeader(payload) {
|
|
584
|
+
return (0, import_utils2.safeBase64Encode)(JSON.stringify(payload));
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// src/sign-in-with-x/fetch.ts
|
|
588
|
+
var import_http = require("@x402/core/http");
|
|
589
|
+
function wrapFetchWithSIWx(fetch, signer) {
|
|
590
|
+
return async (input, init) => {
|
|
591
|
+
const request = new Request(input, init);
|
|
592
|
+
const clonedRequest = request.clone();
|
|
593
|
+
const response = await fetch(request);
|
|
594
|
+
if (response.status !== 402) {
|
|
595
|
+
return response;
|
|
596
|
+
}
|
|
597
|
+
const paymentRequiredHeader = response.headers.get("PAYMENT-REQUIRED");
|
|
598
|
+
if (!paymentRequiredHeader) {
|
|
599
|
+
return response;
|
|
600
|
+
}
|
|
601
|
+
const paymentRequired = (0, import_http.decodePaymentRequiredHeader)(paymentRequiredHeader);
|
|
602
|
+
const siwxExtension = paymentRequired.extensions?.[SIGN_IN_WITH_X];
|
|
603
|
+
if (!siwxExtension?.supportedChains) {
|
|
604
|
+
return response;
|
|
605
|
+
}
|
|
606
|
+
if (clonedRequest.headers.has(SIGN_IN_WITH_X)) {
|
|
607
|
+
throw new Error("SIWX authentication already attempted");
|
|
608
|
+
}
|
|
609
|
+
const paymentNetwork = paymentRequired.accepts?.[0]?.network;
|
|
610
|
+
if (!paymentNetwork) {
|
|
611
|
+
return response;
|
|
612
|
+
}
|
|
613
|
+
const matchingChain = siwxExtension.supportedChains.find(
|
|
614
|
+
(chain) => chain.chainId === paymentNetwork
|
|
615
|
+
);
|
|
616
|
+
if (!matchingChain) {
|
|
617
|
+
return response;
|
|
618
|
+
}
|
|
619
|
+
const completeInfo = {
|
|
620
|
+
...siwxExtension.info,
|
|
621
|
+
chainId: matchingChain.chainId,
|
|
622
|
+
type: matchingChain.type
|
|
623
|
+
};
|
|
624
|
+
const payload = await createSIWxPayload(completeInfo, signer);
|
|
625
|
+
const siwxHeader = encodeSIWxHeader(payload);
|
|
626
|
+
clonedRequest.headers.set(SIGN_IN_WITH_X, siwxHeader);
|
|
627
|
+
return fetch(clonedRequest);
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// src/sign-in-with-x/storage.ts
|
|
632
|
+
var InMemorySIWxStorage = class {
|
|
633
|
+
constructor() {
|
|
634
|
+
this.paidAddresses = /* @__PURE__ */ new Map();
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Check if an address has paid for a resource.
|
|
638
|
+
*
|
|
639
|
+
* @param resource - The resource path
|
|
640
|
+
* @param address - The wallet address to check
|
|
641
|
+
* @returns True if the address has paid
|
|
642
|
+
*/
|
|
643
|
+
hasPaid(resource, address) {
|
|
644
|
+
return this.paidAddresses.get(resource)?.has(address.toLowerCase()) ?? false;
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* Record that an address has paid for a resource.
|
|
648
|
+
*
|
|
649
|
+
* @param resource - The resource path
|
|
650
|
+
* @param address - The wallet address that paid
|
|
651
|
+
*/
|
|
652
|
+
recordPayment(resource, address) {
|
|
653
|
+
if (!this.paidAddresses.has(resource)) {
|
|
654
|
+
this.paidAddresses.set(resource, /* @__PURE__ */ new Set());
|
|
655
|
+
}
|
|
656
|
+
this.paidAddresses.get(resource).add(address.toLowerCase());
|
|
657
|
+
}
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
// src/sign-in-with-x/hooks.ts
|
|
661
|
+
function createSIWxSettleHook(options) {
|
|
662
|
+
const { storage, onEvent } = options;
|
|
663
|
+
return async (ctx) => {
|
|
664
|
+
if (!ctx.result.success) return;
|
|
665
|
+
const address = ctx.result.payer;
|
|
666
|
+
if (!address) return;
|
|
667
|
+
const resource = new URL(ctx.paymentPayload.resource.url).pathname;
|
|
668
|
+
await storage.recordPayment(resource, address);
|
|
669
|
+
onEvent?.({ type: "payment_recorded", resource, address });
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
function createSIWxRequestHook(options) {
|
|
673
|
+
const { storage, verifyOptions, onEvent } = options;
|
|
674
|
+
const hasUsedNonce = typeof storage.hasUsedNonce === "function";
|
|
675
|
+
const hasRecordNonce = typeof storage.recordNonce === "function";
|
|
676
|
+
if (hasUsedNonce !== hasRecordNonce) {
|
|
677
|
+
throw new Error(
|
|
678
|
+
"SIWxStorage nonce tracking requires both hasUsedNonce and recordNonce to be implemented"
|
|
679
|
+
);
|
|
680
|
+
}
|
|
681
|
+
return async (context) => {
|
|
682
|
+
const header = context.adapter.getHeader(SIGN_IN_WITH_X) || context.adapter.getHeader(SIGN_IN_WITH_X.toLowerCase());
|
|
683
|
+
if (!header) return;
|
|
684
|
+
try {
|
|
685
|
+
const payload = parseSIWxHeader(header);
|
|
686
|
+
const resourceUri = context.adapter.getUrl();
|
|
687
|
+
const validation = await validateSIWxMessage(payload, resourceUri);
|
|
688
|
+
if (!validation.valid) {
|
|
689
|
+
onEvent?.({ type: "validation_failed", resource: context.path, error: validation.error });
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
692
|
+
const verification = await verifySIWxSignature(payload, verifyOptions);
|
|
693
|
+
if (!verification.valid || !verification.address) {
|
|
694
|
+
onEvent?.({ type: "validation_failed", resource: context.path, error: verification.error });
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
if (storage.hasUsedNonce) {
|
|
698
|
+
const nonceUsed = await storage.hasUsedNonce(payload.nonce);
|
|
699
|
+
if (nonceUsed) {
|
|
700
|
+
onEvent?.({ type: "nonce_reused", resource: context.path, nonce: payload.nonce });
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
const hasPaid = await storage.hasPaid(context.path, verification.address);
|
|
705
|
+
if (hasPaid) {
|
|
706
|
+
if (storage.recordNonce) {
|
|
707
|
+
await storage.recordNonce(payload.nonce);
|
|
708
|
+
}
|
|
709
|
+
onEvent?.({
|
|
710
|
+
type: "access_granted",
|
|
711
|
+
resource: context.path,
|
|
712
|
+
address: verification.address
|
|
713
|
+
});
|
|
714
|
+
return { grantAccess: true };
|
|
715
|
+
}
|
|
716
|
+
} catch (err) {
|
|
717
|
+
onEvent?.({
|
|
718
|
+
type: "validation_failed",
|
|
719
|
+
resource: context.path,
|
|
720
|
+
error: err instanceof Error ? err.message : "Unknown error"
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
function createSIWxClientHook(signer) {
|
|
726
|
+
return async (context) => {
|
|
727
|
+
const extensions = context.paymentRequired.extensions ?? {};
|
|
728
|
+
const siwxExtension = extensions[SIGN_IN_WITH_X];
|
|
729
|
+
if (!siwxExtension?.supportedChains) return;
|
|
730
|
+
try {
|
|
731
|
+
const paymentNetwork = context.paymentRequired.accepts?.[0]?.network;
|
|
732
|
+
if (!paymentNetwork) return;
|
|
733
|
+
const matchingChain = siwxExtension.supportedChains.find(
|
|
734
|
+
(chain) => chain.chainId === paymentNetwork
|
|
735
|
+
);
|
|
736
|
+
if (!matchingChain) {
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
const completeInfo = {
|
|
740
|
+
...siwxExtension.info,
|
|
741
|
+
chainId: matchingChain.chainId,
|
|
742
|
+
type: matchingChain.type
|
|
743
|
+
};
|
|
744
|
+
const payload = await createSIWxPayload(completeInfo, signer);
|
|
745
|
+
const header = encodeSIWxHeader(payload);
|
|
746
|
+
return { headers: { [SIGN_IN_WITH_X]: header } };
|
|
747
|
+
} catch {
|
|
748
|
+
}
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
752
|
+
0 && (module.exports = {
|
|
753
|
+
InMemorySIWxStorage,
|
|
754
|
+
SIGN_IN_WITH_X,
|
|
755
|
+
SIWxPayloadSchema,
|
|
756
|
+
SOLANA_DEVNET,
|
|
757
|
+
SOLANA_MAINNET,
|
|
758
|
+
SOLANA_TESTNET,
|
|
759
|
+
buildSIWxSchema,
|
|
760
|
+
createSIWxClientHook,
|
|
761
|
+
createSIWxMessage,
|
|
762
|
+
createSIWxPayload,
|
|
763
|
+
createSIWxRequestHook,
|
|
764
|
+
createSIWxSettleHook,
|
|
765
|
+
declareSIWxExtension,
|
|
766
|
+
decodeBase58,
|
|
767
|
+
encodeBase58,
|
|
768
|
+
encodeSIWxHeader,
|
|
769
|
+
extractEVMChainId,
|
|
770
|
+
extractSolanaChainReference,
|
|
771
|
+
formatSIWEMessage,
|
|
772
|
+
formatSIWSMessage,
|
|
773
|
+
getEVMAddress,
|
|
774
|
+
getSolanaAddress,
|
|
775
|
+
parseSIWxHeader,
|
|
776
|
+
signEVMMessage,
|
|
777
|
+
signSolanaMessage,
|
|
778
|
+
siwxResourceServerExtension,
|
|
779
|
+
validateSIWxMessage,
|
|
780
|
+
verifyEVMSignature,
|
|
781
|
+
verifySIWxSignature,
|
|
782
|
+
verifySolanaSignature,
|
|
783
|
+
wrapFetchWithSIWx
|
|
784
|
+
});
|
|
19
785
|
//# sourceMappingURL=index.js.map
|