@x402/extensions 2.5.0 → 2.7.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/dist/cjs/bazaar/index.d.ts +1 -1
- package/dist/cjs/{index-G8RNfr6X.d.ts → index-CtOzXcjN.d.ts} +2 -2
- package/dist/cjs/index.d.ts +33 -23
- package/dist/cjs/index.js +874 -10
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/offer-receipt/index.d.ts +702 -0
- package/dist/cjs/offer-receipt/index.js +909 -0
- package/dist/cjs/offer-receipt/index.js.map +1 -0
- package/dist/cjs/sign-in-with-x/index.js +1 -2
- package/dist/cjs/sign-in-with-x/index.js.map +1 -1
- package/dist/esm/bazaar/index.d.mts +1 -1
- package/dist/esm/{chunk-O34SGKEP.mjs → chunk-QVNCC7CH.mjs} +2 -3
- package/dist/esm/chunk-QVNCC7CH.mjs.map +1 -0
- package/dist/esm/chunk-TYR4QHVX.mjs +828 -0
- package/dist/esm/chunk-TYR4QHVX.mjs.map +1 -0
- package/dist/esm/{index-G8RNfr6X.d.mts → index-CtOzXcjN.d.mts} +2 -2
- package/dist/esm/index.d.mts +33 -23
- package/dist/esm/index.mjs +97 -9
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/offer-receipt/index.d.mts +702 -0
- package/dist/esm/offer-receipt/index.mjs +97 -0
- package/dist/esm/offer-receipt/index.mjs.map +1 -0
- package/dist/esm/sign-in-with-x/index.mjs +1 -1
- package/package.json +14 -2
- package/dist/esm/chunk-O34SGKEP.mjs.map +0 -1
|
@@ -0,0 +1,702 @@
|
|
|
1
|
+
import * as jose from 'jose';
|
|
2
|
+
import { TypedDataDomain, Hex } from 'viem';
|
|
3
|
+
import { ResourceServerExtension, PaymentRequired, PaymentRequirements } from '@x402/core/types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Type definitions for the x402 Offer/Receipt Extension
|
|
7
|
+
*
|
|
8
|
+
* Based on: x402/specs/extensions/extension-offer-and-receipt.md (v1.0)
|
|
9
|
+
*
|
|
10
|
+
* Offers prove payment requirements originated from a resource server.
|
|
11
|
+
* Receipts prove service was delivered after payment.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Extension identifier constant
|
|
15
|
+
*/
|
|
16
|
+
declare const OFFER_RECEIPT = "offer-receipt";
|
|
17
|
+
/**
|
|
18
|
+
* Supported signature formats (§3.1)
|
|
19
|
+
*/
|
|
20
|
+
type SignatureFormat = "jws" | "eip712";
|
|
21
|
+
/**
|
|
22
|
+
* Base signer interface for pluggable signing backends
|
|
23
|
+
*/
|
|
24
|
+
interface Signer {
|
|
25
|
+
/** Key identifier DID (e.g., did:web:api.example.com#key-1) */
|
|
26
|
+
kid: string;
|
|
27
|
+
/** Sign payload and return signature string */
|
|
28
|
+
sign: (payload: Uint8Array) => Promise<string>;
|
|
29
|
+
/** Signature format */
|
|
30
|
+
format: SignatureFormat;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* JWS-specific signer with algorithm info
|
|
34
|
+
*/
|
|
35
|
+
interface JWSSigner extends Signer {
|
|
36
|
+
format: "jws";
|
|
37
|
+
/** JWS algorithm (e.g., ES256K, EdDSA) */
|
|
38
|
+
algorithm: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* EIP-712 specific signer
|
|
42
|
+
*/
|
|
43
|
+
interface EIP712Signer extends Signer {
|
|
44
|
+
format: "eip712";
|
|
45
|
+
/** Chain ID for EIP-712 domain */
|
|
46
|
+
chainId: number;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Offer payload fields (§4.2)
|
|
50
|
+
*
|
|
51
|
+
* Required: version, resourceUrl, scheme, network, asset, payTo, amount
|
|
52
|
+
* Optional: validUntil
|
|
53
|
+
*/
|
|
54
|
+
interface OfferPayload {
|
|
55
|
+
/** Offer payload schema version (currently 1) */
|
|
56
|
+
version: number;
|
|
57
|
+
/** The paid resource URL */
|
|
58
|
+
resourceUrl: string;
|
|
59
|
+
/** Payment scheme identifier (e.g., "exact") */
|
|
60
|
+
scheme: string;
|
|
61
|
+
/** Blockchain network identifier (CAIP-2 format, e.g., "eip155:8453") */
|
|
62
|
+
network: string;
|
|
63
|
+
/** Token contract address or "native" */
|
|
64
|
+
asset: string;
|
|
65
|
+
/** Recipient wallet address */
|
|
66
|
+
payTo: string;
|
|
67
|
+
/** Required payment amount */
|
|
68
|
+
amount: string;
|
|
69
|
+
/** Unix timestamp (seconds) when the offer expires (optional) */
|
|
70
|
+
validUntil: number;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Signed offer in JWS format (§3.1.1)
|
|
74
|
+
*
|
|
75
|
+
* "When format = 'jws': payload MUST be omitted"
|
|
76
|
+
*/
|
|
77
|
+
interface JWSSignedOffer {
|
|
78
|
+
format: "jws";
|
|
79
|
+
/** Index into accepts[] array (unsigned envelope field, §4.1.1) */
|
|
80
|
+
acceptIndex?: number;
|
|
81
|
+
/** JWS Compact Serialization string (header.payload.signature) */
|
|
82
|
+
signature: string;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Signed offer in EIP-712 format (§3.1.1)
|
|
86
|
+
*
|
|
87
|
+
* "When format = 'eip712': payload is REQUIRED"
|
|
88
|
+
*/
|
|
89
|
+
interface EIP712SignedOffer {
|
|
90
|
+
format: "eip712";
|
|
91
|
+
/** Index into accepts[] array (unsigned envelope field, §4.1.1) */
|
|
92
|
+
acceptIndex?: number;
|
|
93
|
+
/** The canonical payload fields */
|
|
94
|
+
payload: OfferPayload;
|
|
95
|
+
/** Hex-encoded ECDSA signature (0x-prefixed, 65 bytes: r+s+v) */
|
|
96
|
+
signature: string;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Union type for signed offers
|
|
100
|
+
*/
|
|
101
|
+
type SignedOffer = JWSSignedOffer | EIP712SignedOffer;
|
|
102
|
+
/**
|
|
103
|
+
* Receipt payload fields (§5.2)
|
|
104
|
+
*
|
|
105
|
+
* Required: version, network, resourceUrl, payer, issuedAt
|
|
106
|
+
* Optional: transaction (for verifiability over privacy)
|
|
107
|
+
*/
|
|
108
|
+
interface ReceiptPayload {
|
|
109
|
+
/** Receipt payload schema version (currently 1) */
|
|
110
|
+
version: number;
|
|
111
|
+
/** Blockchain network identifier (CAIP-2 format, e.g., "eip155:8453") */
|
|
112
|
+
network: string;
|
|
113
|
+
/** The paid resource URL */
|
|
114
|
+
resourceUrl: string;
|
|
115
|
+
/** Payer identifier (commonly a wallet address) */
|
|
116
|
+
payer: string;
|
|
117
|
+
/** Unix timestamp (seconds) when receipt was issued */
|
|
118
|
+
issuedAt: number;
|
|
119
|
+
/** Blockchain transaction hash (optional - for verifiability over privacy) */
|
|
120
|
+
transaction: string;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Signed receipt in JWS format (§3.1.1)
|
|
124
|
+
*/
|
|
125
|
+
interface JWSSignedReceipt {
|
|
126
|
+
format: "jws";
|
|
127
|
+
/** JWS Compact Serialization string */
|
|
128
|
+
signature: string;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Signed receipt in EIP-712 format (§3.1.1)
|
|
132
|
+
*/
|
|
133
|
+
interface EIP712SignedReceipt {
|
|
134
|
+
format: "eip712";
|
|
135
|
+
/** The receipt payload */
|
|
136
|
+
payload: ReceiptPayload;
|
|
137
|
+
/** Hex-encoded ECDSA signature */
|
|
138
|
+
signature: string;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Union type for signed receipts
|
|
142
|
+
*/
|
|
143
|
+
type SignedReceipt = JWSSignedReceipt | EIP712SignedReceipt;
|
|
144
|
+
/**
|
|
145
|
+
* Declaration for the offer-receipt extension in route config
|
|
146
|
+
* Used by servers to declare that a route uses offer-receipt
|
|
147
|
+
*/
|
|
148
|
+
interface OfferReceiptDeclaration {
|
|
149
|
+
/** Include transaction hash in receipt (default: false for privacy). Set to true for verifiability. */
|
|
150
|
+
includeTxHash?: boolean;
|
|
151
|
+
/** Offer validity duration in seconds. Default: 300 (see x402ResourceServer.ts) */
|
|
152
|
+
offerValiditySeconds?: number;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Input for creating an offer (derived from PaymentRequirements)
|
|
156
|
+
*/
|
|
157
|
+
interface OfferInput {
|
|
158
|
+
/** Index into accepts[] array this offer corresponds to (0-based) */
|
|
159
|
+
acceptIndex: number;
|
|
160
|
+
/** Payment scheme identifier */
|
|
161
|
+
scheme: string;
|
|
162
|
+
/** Blockchain network identifier (CAIP-2 format) */
|
|
163
|
+
network: string;
|
|
164
|
+
/** Token contract address or "native" */
|
|
165
|
+
asset: string;
|
|
166
|
+
/** Recipient wallet address */
|
|
167
|
+
payTo: string;
|
|
168
|
+
/** Required payment amount */
|
|
169
|
+
amount: string;
|
|
170
|
+
/** Offer validity duration in seconds. Default: 300 (see x402ResourceServer.ts) */
|
|
171
|
+
offerValiditySeconds?: number;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* High-level issuer interface for the offer-receipt extension.
|
|
175
|
+
* Creates and signs offers and receipts.
|
|
176
|
+
* Used by createOfferReceiptExtension()
|
|
177
|
+
*/
|
|
178
|
+
interface OfferReceiptIssuer {
|
|
179
|
+
/** Key identifier DID */
|
|
180
|
+
kid: string;
|
|
181
|
+
/** Signature format */
|
|
182
|
+
format: SignatureFormat;
|
|
183
|
+
/** Create and sign an offer for a resource */
|
|
184
|
+
issueOffer(resourceUrl: string, input: OfferInput): Promise<SignedOffer>;
|
|
185
|
+
/** Create and sign a receipt for a completed payment */
|
|
186
|
+
issueReceipt(resourceUrl: string, payer: string, network: string, transaction?: string): Promise<SignedReceipt>;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Check if an offer is JWS format
|
|
190
|
+
*
|
|
191
|
+
* @param offer - The signed offer to check
|
|
192
|
+
* @returns True if the offer uses JWS format
|
|
193
|
+
*/
|
|
194
|
+
declare function isJWSSignedOffer(offer: SignedOffer): offer is JWSSignedOffer;
|
|
195
|
+
/**
|
|
196
|
+
* Check if an offer is EIP-712 format
|
|
197
|
+
*
|
|
198
|
+
* @param offer - The signed offer to check
|
|
199
|
+
* @returns True if the offer uses EIP-712 format
|
|
200
|
+
*/
|
|
201
|
+
declare function isEIP712SignedOffer(offer: SignedOffer): offer is EIP712SignedOffer;
|
|
202
|
+
/**
|
|
203
|
+
* Check if a receipt is JWS format
|
|
204
|
+
*
|
|
205
|
+
* @param receipt - The signed receipt to check
|
|
206
|
+
* @returns True if the receipt uses JWS format
|
|
207
|
+
*/
|
|
208
|
+
declare function isJWSSignedReceipt(receipt: SignedReceipt): receipt is JWSSignedReceipt;
|
|
209
|
+
/**
|
|
210
|
+
* Check if a receipt is EIP-712 format
|
|
211
|
+
*
|
|
212
|
+
* @param receipt - The signed receipt to check
|
|
213
|
+
* @returns True if the receipt uses EIP-712 format
|
|
214
|
+
*/
|
|
215
|
+
declare function isEIP712SignedReceipt(receipt: SignedReceipt): receipt is EIP712SignedReceipt;
|
|
216
|
+
/**
|
|
217
|
+
* Check if a signer is JWS format
|
|
218
|
+
*
|
|
219
|
+
* @param signer - The signer to check
|
|
220
|
+
* @returns True if the signer uses JWS format
|
|
221
|
+
*/
|
|
222
|
+
declare function isJWSSigner(signer: Signer): signer is JWSSigner;
|
|
223
|
+
/**
|
|
224
|
+
* Check if a signer is EIP-712 format
|
|
225
|
+
*
|
|
226
|
+
* @param signer - The signer to check
|
|
227
|
+
* @returns True if the signer uses EIP-712 format
|
|
228
|
+
*/
|
|
229
|
+
declare function isEIP712Signer(signer: Signer): signer is EIP712Signer;
|
|
230
|
+
/**
|
|
231
|
+
* Input for creating a receipt
|
|
232
|
+
*/
|
|
233
|
+
interface ReceiptInput {
|
|
234
|
+
/** The resource URL that was paid for */
|
|
235
|
+
resourceUrl: string;
|
|
236
|
+
/** The payer identifier (wallet address) */
|
|
237
|
+
payer: string;
|
|
238
|
+
/** The blockchain network (CAIP-2 format) */
|
|
239
|
+
network: string;
|
|
240
|
+
/** The transaction hash (optional, for verifiability) */
|
|
241
|
+
transaction?: string;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Signing utilities for x402 Offer/Receipt Extension
|
|
246
|
+
*
|
|
247
|
+
* This module provides:
|
|
248
|
+
* - JCS (JSON Canonicalization Scheme) per RFC 8785
|
|
249
|
+
* - JWS (JSON Web Signature) signing and extraction
|
|
250
|
+
* - EIP-712 typed data signing
|
|
251
|
+
* - Offer/Receipt creation utilities
|
|
252
|
+
* - Signature verification utilities
|
|
253
|
+
*
|
|
254
|
+
* Based on: x402/specs/extensions/extension-offer-and-receipt.md (v1.0) §3
|
|
255
|
+
*/
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Canonicalize a JSON object using JCS (RFC 8785)
|
|
259
|
+
*
|
|
260
|
+
* Rules:
|
|
261
|
+
* 1. Object keys are sorted lexicographically by UTF-16 code units
|
|
262
|
+
* 2. No whitespace between tokens
|
|
263
|
+
* 3. Numbers use shortest representation (no trailing zeros)
|
|
264
|
+
* 4. Strings use minimal escaping
|
|
265
|
+
* 5. null, true, false are lowercase literals
|
|
266
|
+
*
|
|
267
|
+
* @param value - The object to canonicalize
|
|
268
|
+
* @returns The canonicalized JSON string
|
|
269
|
+
*/
|
|
270
|
+
declare function canonicalize(value: unknown): string;
|
|
271
|
+
/**
|
|
272
|
+
* Hash a canonicalized object using SHA-256
|
|
273
|
+
*
|
|
274
|
+
* @param obj - The object to hash
|
|
275
|
+
* @returns The SHA-256 hash as Uint8Array
|
|
276
|
+
*/
|
|
277
|
+
declare function hashCanonical(obj: unknown): Promise<Uint8Array>;
|
|
278
|
+
/**
|
|
279
|
+
* Get canonical bytes of an object (UTF-8 encoded)
|
|
280
|
+
*
|
|
281
|
+
* @param obj - The object to encode
|
|
282
|
+
* @returns The UTF-8 encoded canonical JSON
|
|
283
|
+
*/
|
|
284
|
+
declare function getCanonicalBytes(obj: unknown): Uint8Array;
|
|
285
|
+
/**
|
|
286
|
+
* Create a JWS Compact Serialization from a payload
|
|
287
|
+
*
|
|
288
|
+
* Assembles the full JWS structure (header.payload.signature) using the
|
|
289
|
+
* signer's algorithm and kid. The signer only needs to sign bytes and
|
|
290
|
+
* return the base64url-encoded signature.
|
|
291
|
+
*
|
|
292
|
+
* @param payload - The payload object to sign
|
|
293
|
+
* @param signer - The JWS signer
|
|
294
|
+
* @returns The JWS compact serialization string
|
|
295
|
+
*/
|
|
296
|
+
declare function createJWS<T extends object>(payload: T, signer: JWSSigner): Promise<string>;
|
|
297
|
+
/**
|
|
298
|
+
* Extract JWS header without verification
|
|
299
|
+
*
|
|
300
|
+
* @param jws - The JWS compact serialization string
|
|
301
|
+
* @returns The decoded header object
|
|
302
|
+
*/
|
|
303
|
+
declare function extractJWSHeader(jws: string): {
|
|
304
|
+
alg: string;
|
|
305
|
+
kid?: string;
|
|
306
|
+
};
|
|
307
|
+
/**
|
|
308
|
+
* Extract JWS payload
|
|
309
|
+
*
|
|
310
|
+
* Note: This extracts the payload without verifying the signature or
|
|
311
|
+
* checking signer authorization. Signature verification requires resolving
|
|
312
|
+
* key bindings (did:web documents, attestations, etc.) which is outside
|
|
313
|
+
* the scope of x402 client utilities.
|
|
314
|
+
*
|
|
315
|
+
* @param jws - The JWS compact serialization string
|
|
316
|
+
* @returns The decoded payload
|
|
317
|
+
*/
|
|
318
|
+
declare function extractJWSPayload<T>(jws: string): T;
|
|
319
|
+
/**
|
|
320
|
+
* Create EIP-712 domain for offer signing
|
|
321
|
+
*
|
|
322
|
+
* @returns The EIP-712 domain object
|
|
323
|
+
*/
|
|
324
|
+
declare function createOfferDomain(): TypedDataDomain;
|
|
325
|
+
/**
|
|
326
|
+
* Create EIP-712 domain for receipt signing
|
|
327
|
+
*
|
|
328
|
+
* @returns The EIP-712 domain object
|
|
329
|
+
*/
|
|
330
|
+
declare function createReceiptDomain(): TypedDataDomain;
|
|
331
|
+
/**
|
|
332
|
+
* EIP-712 types for Offer (§4.3)
|
|
333
|
+
*/
|
|
334
|
+
declare const OFFER_TYPES: {
|
|
335
|
+
Offer: {
|
|
336
|
+
name: string;
|
|
337
|
+
type: string;
|
|
338
|
+
}[];
|
|
339
|
+
};
|
|
340
|
+
/**
|
|
341
|
+
* EIP-712 types for Receipt (§5.3)
|
|
342
|
+
*/
|
|
343
|
+
declare const RECEIPT_TYPES: {
|
|
344
|
+
Receipt: {
|
|
345
|
+
name: string;
|
|
346
|
+
type: string;
|
|
347
|
+
}[];
|
|
348
|
+
};
|
|
349
|
+
/**
|
|
350
|
+
* Prepare offer payload for EIP-712 signing
|
|
351
|
+
*
|
|
352
|
+
* @param payload - The offer payload
|
|
353
|
+
* @returns The prepared message object for EIP-712
|
|
354
|
+
*/
|
|
355
|
+
declare function prepareOfferForEIP712(payload: OfferPayload): {
|
|
356
|
+
version: bigint;
|
|
357
|
+
resourceUrl: string;
|
|
358
|
+
scheme: string;
|
|
359
|
+
network: string;
|
|
360
|
+
asset: string;
|
|
361
|
+
payTo: string;
|
|
362
|
+
amount: string;
|
|
363
|
+
validUntil: bigint;
|
|
364
|
+
};
|
|
365
|
+
/**
|
|
366
|
+
* Prepare receipt payload for EIP-712 signing
|
|
367
|
+
*
|
|
368
|
+
* @param payload - The receipt payload
|
|
369
|
+
* @returns The prepared message object for EIP-712
|
|
370
|
+
*/
|
|
371
|
+
declare function prepareReceiptForEIP712(payload: ReceiptPayload): {
|
|
372
|
+
version: bigint;
|
|
373
|
+
network: string;
|
|
374
|
+
resourceUrl: string;
|
|
375
|
+
payer: string;
|
|
376
|
+
issuedAt: bigint;
|
|
377
|
+
transaction: string;
|
|
378
|
+
};
|
|
379
|
+
/**
|
|
380
|
+
* Hash offer typed data for EIP-712
|
|
381
|
+
*
|
|
382
|
+
* @param payload - The offer payload
|
|
383
|
+
* @returns The EIP-712 hash
|
|
384
|
+
*/
|
|
385
|
+
declare function hashOfferTypedData(payload: OfferPayload): Hex;
|
|
386
|
+
/**
|
|
387
|
+
* Hash receipt typed data for EIP-712
|
|
388
|
+
*
|
|
389
|
+
* @param payload - The receipt payload
|
|
390
|
+
* @returns The EIP-712 hash
|
|
391
|
+
*/
|
|
392
|
+
declare function hashReceiptTypedData(payload: ReceiptPayload): Hex;
|
|
393
|
+
/**
|
|
394
|
+
* Function type for signing EIP-712 typed data
|
|
395
|
+
*/
|
|
396
|
+
type SignTypedDataFn = (params: {
|
|
397
|
+
domain: TypedDataDomain;
|
|
398
|
+
types: Record<string, Array<{
|
|
399
|
+
name: string;
|
|
400
|
+
type: string;
|
|
401
|
+
}>>;
|
|
402
|
+
primaryType: string;
|
|
403
|
+
message: Record<string, unknown>;
|
|
404
|
+
}) => Promise<Hex>;
|
|
405
|
+
/**
|
|
406
|
+
* Sign an offer using EIP-712
|
|
407
|
+
*
|
|
408
|
+
* @param payload - The offer payload
|
|
409
|
+
* @param signTypedData - The signing function
|
|
410
|
+
* @returns The signature hex string
|
|
411
|
+
*/
|
|
412
|
+
declare function signOfferEIP712(payload: OfferPayload, signTypedData: SignTypedDataFn): Promise<Hex>;
|
|
413
|
+
/**
|
|
414
|
+
* Sign a receipt using EIP-712
|
|
415
|
+
*
|
|
416
|
+
* @param payload - The receipt payload
|
|
417
|
+
* @param signTypedData - The signing function
|
|
418
|
+
* @returns The signature hex string
|
|
419
|
+
*/
|
|
420
|
+
declare function signReceiptEIP712(payload: ReceiptPayload, signTypedData: SignTypedDataFn): Promise<Hex>;
|
|
421
|
+
/**
|
|
422
|
+
* Extract chain ID from an EIP-155 network string (strict format)
|
|
423
|
+
*
|
|
424
|
+
* @param network - The network string in "eip155:<chainId>" format
|
|
425
|
+
* @returns The chain ID number
|
|
426
|
+
* @throws Error if network is not in "eip155:<chainId>" format
|
|
427
|
+
*/
|
|
428
|
+
declare function extractEIP155ChainId(network: string): number;
|
|
429
|
+
/**
|
|
430
|
+
* Convert a network string to CAIP-2 format
|
|
431
|
+
*
|
|
432
|
+
* Handles both CAIP-2 format and legacy x402 v1 network strings:
|
|
433
|
+
* - CAIP-2: "eip155:8453" → "eip155:8453" (passed through)
|
|
434
|
+
* - V1 EVM: "base" → "eip155:8453", "base-sepolia" → "eip155:84532"
|
|
435
|
+
* - V1 Solana: "solana" → "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"
|
|
436
|
+
*
|
|
437
|
+
* @param network - The network string to convert
|
|
438
|
+
* @returns The CAIP-2 formatted network string
|
|
439
|
+
* @throws Error if network is not a recognized v1 identifier or CAIP-2 format
|
|
440
|
+
*/
|
|
441
|
+
declare function convertNetworkStringToCAIP2(network: string): string;
|
|
442
|
+
/**
|
|
443
|
+
* Extract chain ID from a CAIP-2 network string (EVM only)
|
|
444
|
+
*
|
|
445
|
+
* @param network - The CAIP-2 network string
|
|
446
|
+
* @returns Chain ID number, or undefined for non-EVM networks
|
|
447
|
+
*/
|
|
448
|
+
declare function extractChainIdFromCAIP2(network: string): number | undefined;
|
|
449
|
+
/**
|
|
450
|
+
* Create a signed offer using JWS
|
|
451
|
+
*
|
|
452
|
+
* @param resourceUrl - The resource URL being paid for
|
|
453
|
+
* @param input - The offer input parameters
|
|
454
|
+
* @param signer - The JWS signer
|
|
455
|
+
* @returns The signed offer with JWS format
|
|
456
|
+
*/
|
|
457
|
+
declare function createOfferJWS(resourceUrl: string, input: OfferInput, signer: JWSSigner): Promise<JWSSignedOffer>;
|
|
458
|
+
/**
|
|
459
|
+
* Create a signed offer using EIP-712
|
|
460
|
+
*
|
|
461
|
+
* @param resourceUrl - The resource URL being paid for
|
|
462
|
+
* @param input - The offer input parameters
|
|
463
|
+
* @param signTypedData - The signing function
|
|
464
|
+
* @returns The signed offer with EIP-712 format
|
|
465
|
+
*/
|
|
466
|
+
declare function createOfferEIP712(resourceUrl: string, input: OfferInput, signTypedData: SignTypedDataFn): Promise<EIP712SignedOffer>;
|
|
467
|
+
/**
|
|
468
|
+
* Extract offer payload
|
|
469
|
+
*
|
|
470
|
+
* Note: This extracts the payload without verifying the signature or
|
|
471
|
+
* checking signer authorization. Signer authorization requires resolving
|
|
472
|
+
* key bindings (did:web documents, attestations, etc.) which is outside
|
|
473
|
+
* the scope of x402 client utilities. See spec §4.5.1.
|
|
474
|
+
*
|
|
475
|
+
* @param offer - The signed offer
|
|
476
|
+
* @returns The offer payload
|
|
477
|
+
*/
|
|
478
|
+
declare function extractOfferPayload(offer: SignedOffer): OfferPayload;
|
|
479
|
+
/**
|
|
480
|
+
* Create a signed receipt using JWS
|
|
481
|
+
*
|
|
482
|
+
* @param input - The receipt input parameters
|
|
483
|
+
* @param signer - The JWS signer
|
|
484
|
+
* @returns The signed receipt with JWS format
|
|
485
|
+
*/
|
|
486
|
+
declare function createReceiptJWS(input: ReceiptInput, signer: JWSSigner): Promise<JWSSignedReceipt>;
|
|
487
|
+
/**
|
|
488
|
+
* Create a signed receipt using EIP-712
|
|
489
|
+
*
|
|
490
|
+
* @param input - The receipt input parameters
|
|
491
|
+
* @param signTypedData - The signing function
|
|
492
|
+
* @returns The signed receipt with EIP-712 format
|
|
493
|
+
*/
|
|
494
|
+
declare function createReceiptEIP712(input: ReceiptInput, signTypedData: SignTypedDataFn): Promise<EIP712SignedReceipt>;
|
|
495
|
+
/**
|
|
496
|
+
* Extract receipt payload
|
|
497
|
+
*
|
|
498
|
+
* Note: This extracts the payload without verifying the signature or
|
|
499
|
+
* checking signer authorization. Signer authorization requires resolving
|
|
500
|
+
* key bindings (did:web documents, attestations, etc.) which is outside
|
|
501
|
+
* the scope of x402 client utilities. See spec §5.5.
|
|
502
|
+
*
|
|
503
|
+
* @param receipt - The signed receipt
|
|
504
|
+
* @returns The receipt payload
|
|
505
|
+
*/
|
|
506
|
+
declare function extractReceiptPayload(receipt: SignedReceipt): ReceiptPayload;
|
|
507
|
+
/**
|
|
508
|
+
* Result of EIP-712 signature verification
|
|
509
|
+
*/
|
|
510
|
+
interface EIP712VerificationResult<T> {
|
|
511
|
+
signer: Hex;
|
|
512
|
+
payload: T;
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Verify an EIP-712 signed offer and recover the signer address.
|
|
516
|
+
* Does NOT verify signer authorization for the resourceUrl - see spec §4.5.1.
|
|
517
|
+
*
|
|
518
|
+
* @param offer - The EIP-712 signed offer
|
|
519
|
+
* @returns The recovered signer address and payload
|
|
520
|
+
*/
|
|
521
|
+
declare function verifyOfferSignatureEIP712(offer: EIP712SignedOffer): Promise<EIP712VerificationResult<OfferPayload>>;
|
|
522
|
+
/**
|
|
523
|
+
* Verify an EIP-712 signed receipt and recover the signer address.
|
|
524
|
+
* Does NOT verify signer authorization for the resourceUrl - see spec §4.5.1.
|
|
525
|
+
*
|
|
526
|
+
* @param receipt - The EIP-712 signed receipt
|
|
527
|
+
* @returns The recovered signer address and payload
|
|
528
|
+
*/
|
|
529
|
+
declare function verifyReceiptSignatureEIP712(receipt: EIP712SignedReceipt): Promise<EIP712VerificationResult<ReceiptPayload>>;
|
|
530
|
+
/**
|
|
531
|
+
* Verify a JWS signed offer.
|
|
532
|
+
* Does NOT verify signer authorization for the resourceUrl - see spec §4.5.1.
|
|
533
|
+
* If no publicKey provided, extracts from kid (supports did:key, did:jwk, did:web).
|
|
534
|
+
*
|
|
535
|
+
* @param offer - The JWS signed offer
|
|
536
|
+
* @param publicKey - Optional public key (JWK or KeyLike). If not provided, extracted from kid.
|
|
537
|
+
* @returns The verified payload
|
|
538
|
+
*/
|
|
539
|
+
declare function verifyOfferSignatureJWS(offer: JWSSignedOffer, publicKey?: jose.KeyLike | jose.JWK): Promise<OfferPayload>;
|
|
540
|
+
/**
|
|
541
|
+
* Verify a JWS signed receipt.
|
|
542
|
+
* Does NOT verify signer authorization for the resourceUrl - see spec §4.5.1.
|
|
543
|
+
* If no publicKey provided, extracts from kid (supports did:key, did:jwk, did:web).
|
|
544
|
+
*
|
|
545
|
+
* @param receipt - The JWS signed receipt
|
|
546
|
+
* @param publicKey - Optional public key (JWK or KeyLike). If not provided, extracted from kid.
|
|
547
|
+
* @returns The verified payload
|
|
548
|
+
*/
|
|
549
|
+
declare function verifyReceiptSignatureJWS(receipt: JWSSignedReceipt, publicKey?: jose.KeyLike | jose.JWK): Promise<ReceiptPayload>;
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Offer-Receipt Extension for x402ResourceServer
|
|
553
|
+
*
|
|
554
|
+
* This module provides the ResourceServerExtension implementation that uses
|
|
555
|
+
* the extension hooks (enrichPaymentRequiredResponse, enrichSettlementResponse)
|
|
556
|
+
* to add signed offers and receipts to x402 payment flows.
|
|
557
|
+
*
|
|
558
|
+
* Based on: x402/specs/extensions/extension-offer-and-receipt.md (v1.0)
|
|
559
|
+
*/
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Creates an offer-receipt extension for use with x402ResourceServer.
|
|
563
|
+
*
|
|
564
|
+
* The extension uses the hook system to:
|
|
565
|
+
* 1. Add signed offers to each PaymentRequirements in 402 responses
|
|
566
|
+
* 2. Add signed receipts to settlement responses after successful payment
|
|
567
|
+
*
|
|
568
|
+
* @param issuer - The issuer to use for creating and signing offers and receipts
|
|
569
|
+
* @returns ResourceServerExtension that can be registered with x402ResourceServer
|
|
570
|
+
*/
|
|
571
|
+
declare function createOfferReceiptExtension(issuer: OfferReceiptIssuer): ResourceServerExtension;
|
|
572
|
+
/**
|
|
573
|
+
* Declare offer-receipt extension for a route
|
|
574
|
+
*
|
|
575
|
+
* Use this in route configuration to enable offer-receipt for a specific endpoint.
|
|
576
|
+
*
|
|
577
|
+
* @param config - Optional configuration for the extension
|
|
578
|
+
* @returns Extension declaration object to spread into route config
|
|
579
|
+
*/
|
|
580
|
+
declare function declareOfferReceiptExtension(config?: OfferReceiptDeclaration): Record<string, OfferReceiptDeclaration>;
|
|
581
|
+
/**
|
|
582
|
+
* Create an OfferReceiptIssuer that uses JWS format
|
|
583
|
+
*
|
|
584
|
+
* @param kid - Key identifier DID (e.g., did:web:api.example.com#key-1)
|
|
585
|
+
* @param jwsSigner - JWS signer with sign() function and algorithm
|
|
586
|
+
* @returns OfferReceiptIssuer for use with createOfferReceiptExtension
|
|
587
|
+
*/
|
|
588
|
+
declare function createJWSOfferReceiptIssuer(kid: string, jwsSigner: JWSSigner): OfferReceiptIssuer;
|
|
589
|
+
/**
|
|
590
|
+
* Create an OfferReceiptIssuer that uses EIP-712 format
|
|
591
|
+
*
|
|
592
|
+
* @param kid - Key identifier DID (e.g., did:pkh:eip155:1:0x...)
|
|
593
|
+
* @param signTypedData - Function to sign EIP-712 typed data
|
|
594
|
+
* @returns OfferReceiptIssuer for use with createOfferReceiptExtension
|
|
595
|
+
*/
|
|
596
|
+
declare function createEIP712OfferReceiptIssuer(kid: string, signTypedData: SignTypedDataFn): OfferReceiptIssuer;
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* Client-side utilities for extracting offers and receipts from x402 responses
|
|
600
|
+
*
|
|
601
|
+
* Provides utilities for clients who want to access signed offers and receipts
|
|
602
|
+
* from x402 payment flows. Useful for verified reviews, audit trails, and dispute resolution.
|
|
603
|
+
*
|
|
604
|
+
* @see README.md for usage examples (raw and wrapper flows)
|
|
605
|
+
* @see examples/typescript/clients/offer-receipt/ for complete example
|
|
606
|
+
*/
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* A signed offer with its decoded payload fields at the top level.
|
|
610
|
+
* Combines the signed offer metadata with the decoded payload for easy access.
|
|
611
|
+
*/
|
|
612
|
+
interface DecodedOffer extends OfferPayload {
|
|
613
|
+
/** The original signed offer (for passing to other functions or downstream systems) */
|
|
614
|
+
signedOffer: SignedOffer;
|
|
615
|
+
/** The signature format used */
|
|
616
|
+
format: "jws" | "eip712";
|
|
617
|
+
/** Index into accepts[] array (hint for matching), may be undefined */
|
|
618
|
+
acceptIndex?: number;
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Verify that a receipt's payload matches the offer and payer.
|
|
622
|
+
*
|
|
623
|
+
* This performs basic payload field verification:
|
|
624
|
+
* - resourceUrl matches the offer
|
|
625
|
+
* - network matches the offer
|
|
626
|
+
* - payer matches one of the client's wallet addresses
|
|
627
|
+
* - issuedAt is recent (within maxAgeSeconds)
|
|
628
|
+
*
|
|
629
|
+
* NOTE: This does NOT verify the signature or key binding. See the comment
|
|
630
|
+
* in the offer-receipt example for guidance on full verification.
|
|
631
|
+
*
|
|
632
|
+
* @param receipt - The signed receipt from the server
|
|
633
|
+
* @param offer - The decoded offer that was accepted
|
|
634
|
+
* @param payerAddresses - Array of the client's wallet addresses (EVM, SVM, etc.)
|
|
635
|
+
* @param maxAgeSeconds - Maximum age of receipt in seconds (default: 3600 = 1 hour)
|
|
636
|
+
* @returns true if all checks pass, false otherwise
|
|
637
|
+
*/
|
|
638
|
+
declare function verifyReceiptMatchesOffer(receipt: SignedReceipt, offer: DecodedOffer, payerAddresses: string[], maxAgeSeconds?: number): boolean;
|
|
639
|
+
/**
|
|
640
|
+
* Extract signed offers from a PaymentRequired response.
|
|
641
|
+
*
|
|
642
|
+
* Call this immediately after receiving a 402 response to save the offers.
|
|
643
|
+
* If the settlement response doesn't include a receipt, you'll still have
|
|
644
|
+
* the offers for attestation purposes.
|
|
645
|
+
*
|
|
646
|
+
* @param paymentRequired - The PaymentRequired object from the 402 response
|
|
647
|
+
* @returns Array of signed offers, or empty array if none present
|
|
648
|
+
*/
|
|
649
|
+
declare function extractOffersFromPaymentRequired(paymentRequired: PaymentRequired): SignedOffer[];
|
|
650
|
+
/**
|
|
651
|
+
* Decode all signed offers and return them with payload fields at the top level.
|
|
652
|
+
*
|
|
653
|
+
* Use this to inspect offer details (network, amount, etc.) for selection.
|
|
654
|
+
* JWS decoding is cheap (base64 decode, no crypto), so decoding all offers
|
|
655
|
+
* upfront is fine even with multiple offers.
|
|
656
|
+
*
|
|
657
|
+
* @param offers - Array of signed offers from extractOffersFromPaymentRequired
|
|
658
|
+
* @returns Array of decoded offers with payload fields at top level
|
|
659
|
+
*/
|
|
660
|
+
declare function decodeSignedOffers(offers: SignedOffer[]): DecodedOffer[];
|
|
661
|
+
/**
|
|
662
|
+
* Find the accepts[] entry that matches a signed or decoded offer.
|
|
663
|
+
*
|
|
664
|
+
* Use this after selecting an offer to get the PaymentRequirements
|
|
665
|
+
* object needed for createPaymentPayload.
|
|
666
|
+
*
|
|
667
|
+
* Uses the offer's acceptIndex as a hint for faster lookup, but verifies
|
|
668
|
+
* the payload matches in case indices got out of sync.
|
|
669
|
+
*
|
|
670
|
+
* @param offer - A DecodedOffer (from decodeSignedOffers) or SignedOffer
|
|
671
|
+
* @param accepts - Array of payment requirements from paymentRequired.accepts
|
|
672
|
+
* @returns The matching PaymentRequirements, or undefined if not found
|
|
673
|
+
*/
|
|
674
|
+
declare function findAcceptsObjectFromSignedOffer(offer: DecodedOffer | SignedOffer, accepts: PaymentRequirements[]): PaymentRequirements | undefined;
|
|
675
|
+
/**
|
|
676
|
+
* Extract signed receipt from a successful payment response.
|
|
677
|
+
*
|
|
678
|
+
* Call this after a successful payment to get the server's signed receipt.
|
|
679
|
+
* The receipt proves the service was delivered after payment.
|
|
680
|
+
*
|
|
681
|
+
* @param response - The Response object from the successful request
|
|
682
|
+
* @returns The signed receipt, or undefined if not present
|
|
683
|
+
*/
|
|
684
|
+
declare function extractReceiptFromResponse(response: Response): SignedReceipt | undefined;
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* DID Resolution Utilities
|
|
688
|
+
*
|
|
689
|
+
* Extracts public keys from DID key identifiers. Supports did:key, did:jwk, did:web.
|
|
690
|
+
* Uses @noble/curves and @scure/base for cryptographic operations.
|
|
691
|
+
*/
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Extract a public key from a DID key identifier (kid).
|
|
695
|
+
* Supports did:key, did:jwk, did:web.
|
|
696
|
+
*
|
|
697
|
+
* @param kid - The key identifier (DID URL, e.g., did:key:z6Mk..., did:web:example.com#key-1)
|
|
698
|
+
* @returns The extracted public key
|
|
699
|
+
*/
|
|
700
|
+
declare function extractPublicKeyFromKid(kid: string): Promise<jose.KeyLike>;
|
|
701
|
+
|
|
702
|
+
export { type DecodedOffer, type EIP712SignedOffer, type EIP712SignedReceipt, type EIP712Signer, type EIP712VerificationResult, type JWSSignedOffer, type JWSSignedReceipt, type JWSSigner, OFFER_RECEIPT, OFFER_TYPES, type OfferInput, type OfferPayload, type OfferReceiptDeclaration, type OfferReceiptIssuer, RECEIPT_TYPES, type ReceiptInput, type ReceiptPayload, type SignTypedDataFn, type SignatureFormat, type SignedOffer, type SignedReceipt, type Signer, canonicalize, convertNetworkStringToCAIP2, createEIP712OfferReceiptIssuer, createJWS, createJWSOfferReceiptIssuer, createOfferDomain, createOfferEIP712, createOfferJWS, createOfferReceiptExtension, createReceiptDomain, createReceiptEIP712, createReceiptJWS, declareOfferReceiptExtension, decodeSignedOffers, extractChainIdFromCAIP2, extractEIP155ChainId, extractJWSHeader, extractJWSPayload, extractOfferPayload, extractOffersFromPaymentRequired, extractPublicKeyFromKid, extractReceiptFromResponse, extractReceiptPayload, findAcceptsObjectFromSignedOffer, getCanonicalBytes, hashCanonical, hashOfferTypedData, hashReceiptTypedData, isEIP712SignedOffer, isEIP712SignedReceipt, isEIP712Signer, isJWSSignedOffer, isJWSSignedReceipt, isJWSSigner, prepareOfferForEIP712, prepareReceiptForEIP712, signOfferEIP712, signReceiptEIP712, verifyOfferSignatureEIP712, verifyOfferSignatureJWS, verifyReceiptMatchesOffer, verifyReceiptSignatureEIP712, verifyReceiptSignatureJWS };
|