@ic-pay/icpay-sdk 1.3.96 → 1.4.12
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/index.d.ts +47 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +790 -325
- package/dist/index.js.map +1 -1
- package/dist/protected.d.ts.map +1 -1
- package/dist/protected.js.map +1 -1
- package/dist/types/index.d.ts +48 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/x402/builders.d.ts +60 -0
- package/dist/x402/builders.d.ts.map +1 -0
- package/dist/x402/builders.js +210 -0
- package/dist/x402/builders.js.map +1 -0
- package/dist/x402/common.d.ts +23 -0
- package/dist/x402/common.d.ts.map +1 -0
- package/dist/x402/common.js +108 -0
- package/dist/x402/common.js.map +1 -0
- package/dist/x402/encode.d.ts +23 -0
- package/dist/x402/encode.d.ts.map +1 -0
- package/dist/x402/encode.js +71 -0
- package/dist/x402/encode.js.map +1 -0
- package/dist/x402/facilitator.d.ts +88 -0
- package/dist/x402/facilitator.d.ts.map +1 -0
- package/dist/x402/facilitator.js +214 -0
- package/dist/x402/facilitator.js.map +1 -0
- package/dist/x402/fetchWithPayment.d.ts +43 -0
- package/dist/x402/fetchWithPayment.d.ts.map +1 -0
- package/dist/x402/fetchWithPayment.js +117 -0
- package/dist/x402/fetchWithPayment.js.map +1 -0
- package/dist/x402/schemas.d.ts +34 -0
- package/dist/x402/schemas.d.ts.map +1 -0
- package/dist/x402/schemas.js +126 -0
- package/dist/x402/schemas.js.map +1 -0
- package/dist/x402/settle-payment.d.ts +120 -0
- package/dist/x402/settle-payment.d.ts.map +1 -0
- package/dist/x402/settle-payment.js +177 -0
- package/dist/x402/settle-payment.js.map +1 -0
- package/dist/x402/sign.d.ts +13 -0
- package/dist/x402/sign.d.ts.map +1 -0
- package/dist/x402/sign.js +221 -0
- package/dist/x402/sign.js.map +1 -0
- package/dist/x402/types.d.ts +58 -0
- package/dist/x402/types.d.ts.map +1 -0
- package/dist/x402/types.js +3 -0
- package/dist/x402/types.js.map +1 -0
- package/dist/x402/verify-payment.d.ts +70 -0
- package/dist/x402/verify-payment.d.ts.map +1 -0
- package/dist/x402/verify-payment.js +123 -0
- package/dist/x402/verify-payment.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Eip712Domain, TransferWithAuthorizationMessage, PermitMessage, X402Acceptance, X402Header } from './types';
|
|
2
|
+
export declare function buildTransferWithAuthorizationTypedData(domain: Eip712Domain, message: TransferWithAuthorizationMessage): {
|
|
3
|
+
types: {
|
|
4
|
+
EIP712Domain: {
|
|
5
|
+
name: string;
|
|
6
|
+
type: string;
|
|
7
|
+
}[];
|
|
8
|
+
TransferWithAuthorization: {
|
|
9
|
+
name: string;
|
|
10
|
+
type: string;
|
|
11
|
+
}[];
|
|
12
|
+
};
|
|
13
|
+
domain: Eip712Domain;
|
|
14
|
+
primaryType: string;
|
|
15
|
+
message: TransferWithAuthorizationMessage;
|
|
16
|
+
};
|
|
17
|
+
export declare function buildPermitTypedData(domain: Eip712Domain, message: PermitMessage): {
|
|
18
|
+
types: {
|
|
19
|
+
EIP712Domain: {
|
|
20
|
+
name: string;
|
|
21
|
+
type: string;
|
|
22
|
+
}[];
|
|
23
|
+
Permit: {
|
|
24
|
+
name: string;
|
|
25
|
+
type: string;
|
|
26
|
+
}[];
|
|
27
|
+
};
|
|
28
|
+
domain: Eip712Domain;
|
|
29
|
+
primaryType: string;
|
|
30
|
+
message: PermitMessage;
|
|
31
|
+
};
|
|
32
|
+
export declare function encodeX402Header(header: X402Header): string;
|
|
33
|
+
export declare function makeEip712Domain(params: {
|
|
34
|
+
name: string;
|
|
35
|
+
version: string;
|
|
36
|
+
chainId?: number;
|
|
37
|
+
verifyingContract: string;
|
|
38
|
+
}): Eip712Domain;
|
|
39
|
+
export declare function buildX402HeaderFromAuthorization(input: {
|
|
40
|
+
x402Version: number;
|
|
41
|
+
scheme: string;
|
|
42
|
+
network: string;
|
|
43
|
+
from: string;
|
|
44
|
+
to: string;
|
|
45
|
+
value: string;
|
|
46
|
+
validAfter: string;
|
|
47
|
+
validBefore: string;
|
|
48
|
+
nonce: string;
|
|
49
|
+
signature: string;
|
|
50
|
+
}): X402Header;
|
|
51
|
+
/**
|
|
52
|
+
* Build and sign an X402 payment header using an EVM wallet (eth_signTypedData_v4).
|
|
53
|
+
* Returns a base64-encoded header string suitable for the X402 settle endpoint.
|
|
54
|
+
*/
|
|
55
|
+
export declare function buildAndSignX402PaymentHeader(requirement: X402Acceptance, context?: {
|
|
56
|
+
x402Version?: number;
|
|
57
|
+
debug?: boolean;
|
|
58
|
+
provider?: any;
|
|
59
|
+
}): Promise<string>;
|
|
60
|
+
//# sourceMappingURL=builders.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builders.d.ts","sourceRoot":"","sources":["../../src/x402/builders.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,gCAAgC,EAAE,aAAa,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEpH,wBAAgB,uCAAuC,CACrD,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,gCAAgC;;;;;;;;;;;;;;EAwB1C;AAED,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,aAAa;;;;;;;;;;;;;;EAsBhF;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAO3D;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iBAAiB,EAAE,MAAM,CAAC;CAC3B,GAAG,YAAY,CAOf;AAED,wBAAgB,gCAAgC,CAAC,KAAK,EAAE;IACtD,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;CACnB,GAAG,UAAU,CAiBb;AAED;;;GAGG;AACH,wBAAsB,6BAA6B,CACjD,WAAW,EAAE,cAAc,EAC3B,OAAO,CAAC,EAAE;IAAE,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,EAAE,GAAG,CAAA;CAAE,GAClE,OAAO,CAAC,MAAM,CAAC,CAmHjB"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildTransferWithAuthorizationTypedData = buildTransferWithAuthorizationTypedData;
|
|
4
|
+
exports.buildPermitTypedData = buildPermitTypedData;
|
|
5
|
+
exports.encodeX402Header = encodeX402Header;
|
|
6
|
+
exports.makeEip712Domain = makeEip712Domain;
|
|
7
|
+
exports.buildX402HeaderFromAuthorization = buildX402HeaderFromAuthorization;
|
|
8
|
+
exports.buildAndSignX402PaymentHeader = buildAndSignX402PaymentHeader;
|
|
9
|
+
function buildTransferWithAuthorizationTypedData(domain, message) {
|
|
10
|
+
const types = {
|
|
11
|
+
EIP712Domain: [
|
|
12
|
+
{ name: 'name', type: 'string' },
|
|
13
|
+
{ name: 'version', type: 'string' },
|
|
14
|
+
{ name: 'chainId', type: 'uint256' },
|
|
15
|
+
{ name: 'verifyingContract', type: 'address' },
|
|
16
|
+
],
|
|
17
|
+
TransferWithAuthorization: [
|
|
18
|
+
{ name: 'from', type: 'address' },
|
|
19
|
+
{ name: 'to', type: 'address' },
|
|
20
|
+
{ name: 'value', type: 'uint256' },
|
|
21
|
+
{ name: 'validAfter', type: 'uint256' },
|
|
22
|
+
{ name: 'validBefore', type: 'uint256' },
|
|
23
|
+
{ name: 'nonce', type: 'bytes32' },
|
|
24
|
+
],
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
types,
|
|
28
|
+
domain,
|
|
29
|
+
primaryType: 'TransferWithAuthorization',
|
|
30
|
+
message,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function buildPermitTypedData(domain, message) {
|
|
34
|
+
const types = {
|
|
35
|
+
EIP712Domain: [
|
|
36
|
+
{ name: 'name', type: 'string' },
|
|
37
|
+
{ name: 'version', type: 'string' },
|
|
38
|
+
{ name: 'chainId', type: 'uint256' },
|
|
39
|
+
{ name: 'verifyingContract', type: 'address' },
|
|
40
|
+
],
|
|
41
|
+
Permit: [
|
|
42
|
+
{ name: 'owner', type: 'address' },
|
|
43
|
+
{ name: 'spender', type: 'address' },
|
|
44
|
+
{ name: 'value', type: 'uint256' },
|
|
45
|
+
{ name: 'nonce', type: 'uint256' },
|
|
46
|
+
{ name: 'deadline', type: 'uint256' },
|
|
47
|
+
],
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
types,
|
|
51
|
+
domain,
|
|
52
|
+
primaryType: 'Permit',
|
|
53
|
+
message,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function encodeX402Header(header) {
|
|
57
|
+
const json = JSON.stringify(header);
|
|
58
|
+
try {
|
|
59
|
+
return btoa(json);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return Buffer.from(json, 'utf8').toString('base64');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function makeEip712Domain(params) {
|
|
66
|
+
return {
|
|
67
|
+
name: String(params.name),
|
|
68
|
+
version: String(params.version),
|
|
69
|
+
chainId: typeof params.chainId === 'number' ? params.chainId : undefined,
|
|
70
|
+
verifyingContract: String(params.verifyingContract),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function buildX402HeaderFromAuthorization(input) {
|
|
74
|
+
return {
|
|
75
|
+
x402Version: Number(input.x402Version || 1),
|
|
76
|
+
scheme: String(input.scheme || 'exact'),
|
|
77
|
+
network: String(input.network || ''),
|
|
78
|
+
payload: {
|
|
79
|
+
authorization: {
|
|
80
|
+
from: input.from,
|
|
81
|
+
to: input.to,
|
|
82
|
+
value: input.value,
|
|
83
|
+
validAfter: input.validAfter,
|
|
84
|
+
validBefore: input.validBefore,
|
|
85
|
+
nonce: input.nonce,
|
|
86
|
+
},
|
|
87
|
+
signature: input.signature,
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Build and sign an X402 payment header using an EVM wallet (eth_signTypedData_v4).
|
|
93
|
+
* Returns a base64-encoded header string suitable for the X402 settle endpoint.
|
|
94
|
+
*/
|
|
95
|
+
async function buildAndSignX402PaymentHeader(requirement, context) {
|
|
96
|
+
const w = globalThis?.window || globalThis;
|
|
97
|
+
const eth = context?.provider || w?.ethereum;
|
|
98
|
+
if (!eth || !eth.request) {
|
|
99
|
+
throw new Error('No EVM wallet available for X402');
|
|
100
|
+
}
|
|
101
|
+
// Ensure wallet is on the intended chain for reliable eth_call (token name) and consistent UX
|
|
102
|
+
try {
|
|
103
|
+
const netStr = requirement?.network || '';
|
|
104
|
+
const chainIdDec = typeof netStr === 'string' ? parseInt(netStr, 10) : Number(netStr || 0);
|
|
105
|
+
if (Number.isFinite(chainIdDec) && chainIdDec > 0) {
|
|
106
|
+
const hex = '0x' + chainIdDec.toString(16);
|
|
107
|
+
try {
|
|
108
|
+
const currentHex = await eth.request({ method: 'eth_chainId' });
|
|
109
|
+
if (typeof currentHex !== 'string' || currentHex.toLowerCase() !== hex.toLowerCase()) {
|
|
110
|
+
await eth.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: hex }] });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
// Best-effort: if switch fails, continue; signing does not require chain match,
|
|
115
|
+
// but eth_call for token name may fail. We'll handle that gracefully.
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
catch { }
|
|
120
|
+
const accounts = await eth.request({ method: 'eth_requestAccounts' });
|
|
121
|
+
const from = (accounts && accounts[0]) || '';
|
|
122
|
+
if (!from)
|
|
123
|
+
throw new Error('No wallet account available for X402');
|
|
124
|
+
const nowSec = Math.floor(Date.now() / 1000);
|
|
125
|
+
const validAfter = (nowSec - 86400).toString();
|
|
126
|
+
const validBefore = (nowSec + Number(requirement?.maxTimeoutSeconds || 300)).toString();
|
|
127
|
+
// Generate 32-byte nonce (prefer secure sources)
|
|
128
|
+
let nonceBytes;
|
|
129
|
+
try {
|
|
130
|
+
if (w?.crypto?.getRandomValues) {
|
|
131
|
+
nonceBytes = w.crypto.getRandomValues(new Uint8Array(32));
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
// Try Node crypto for non-browser environments
|
|
135
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
136
|
+
const nodeCrypto = typeof require === 'function' ? require('crypto') : null;
|
|
137
|
+
nonceBytes = nodeCrypto?.randomBytes ? nodeCrypto.randomBytes(32) : new Uint8Array(32);
|
|
138
|
+
if (!nodeCrypto?.randomBytes) {
|
|
139
|
+
for (let i = 0; i < nonceBytes.length; i++) {
|
|
140
|
+
nonceBytes[i] = Math.floor(Math.random() * 256);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
nonceBytes = new Uint8Array(32);
|
|
147
|
+
for (let i = 0; i < nonceBytes.length; i++) {
|
|
148
|
+
nonceBytes[i] = Math.floor(Math.random() * 256);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const bytesArr = Array.from(nonceBytes);
|
|
152
|
+
const nonce = '0x' + bytesArr.map(b => b.toString(16).padStart(2, '0')).join('');
|
|
153
|
+
const extra = (requirement?.extra || {});
|
|
154
|
+
const primaryType = String(extra?.primaryType || 'TransferWithAuthorization');
|
|
155
|
+
const domain = makeEip712Domain({
|
|
156
|
+
name: String(extra?.name || 'Token'),
|
|
157
|
+
version: String(extra?.eip3009Version || '1'),
|
|
158
|
+
chainId: Number(requirement?.network || 0) || undefined,
|
|
159
|
+
verifyingContract: String(requirement?.asset || requirement?.payTo || ''),
|
|
160
|
+
});
|
|
161
|
+
const typedData = primaryType === 'Permit'
|
|
162
|
+
? buildPermitTypedData(domain, {
|
|
163
|
+
owner: from,
|
|
164
|
+
spender: String(requirement?.payTo || ''),
|
|
165
|
+
value: String(requirement?.maxAmountRequired || '0'),
|
|
166
|
+
nonce: String(BigInt(nonce)),
|
|
167
|
+
deadline: String(validBefore),
|
|
168
|
+
})
|
|
169
|
+
: buildTransferWithAuthorizationTypedData(domain, {
|
|
170
|
+
from,
|
|
171
|
+
to: String(requirement?.payTo || ''),
|
|
172
|
+
value: String(requirement?.maxAmountRequired || '0'),
|
|
173
|
+
validAfter: String(validAfter),
|
|
174
|
+
validBefore: String(validBefore),
|
|
175
|
+
nonce,
|
|
176
|
+
});
|
|
177
|
+
if (context?.debug) {
|
|
178
|
+
try {
|
|
179
|
+
// eslint-disable-next-line no-console
|
|
180
|
+
console.debug('X402 EIP-712 typedData (pre-sign)', {
|
|
181
|
+
domain,
|
|
182
|
+
primaryType: typedData.primaryType,
|
|
183
|
+
verifyingContract: domain.verifyingContract,
|
|
184
|
+
chainId: domain.chainId,
|
|
185
|
+
types: Object.keys((typedData.types || {})),
|
|
186
|
+
message: typedData.message,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
catch { }
|
|
190
|
+
}
|
|
191
|
+
const payload = JSON.stringify(typedData);
|
|
192
|
+
const signature = await eth.request({
|
|
193
|
+
method: 'eth_signTypedData_v4',
|
|
194
|
+
params: [from, payload],
|
|
195
|
+
});
|
|
196
|
+
const headerObj = buildX402HeaderFromAuthorization({
|
|
197
|
+
x402Version: Number(context?.x402Version || 1),
|
|
198
|
+
scheme: String(requirement?.scheme || 'exact'),
|
|
199
|
+
network: String(requirement?.network || ''),
|
|
200
|
+
from,
|
|
201
|
+
to: String(requirement?.payTo || ''),
|
|
202
|
+
value: String(requirement?.maxAmountRequired || '0'),
|
|
203
|
+
validAfter: String(validAfter),
|
|
204
|
+
validBefore: String(validBefore),
|
|
205
|
+
nonce,
|
|
206
|
+
signature,
|
|
207
|
+
});
|
|
208
|
+
return encodeX402Header(headerObj);
|
|
209
|
+
}
|
|
210
|
+
//# sourceMappingURL=builders.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builders.js","sourceRoot":"","sources":["../../src/x402/builders.ts"],"names":[],"mappings":";;AAEA,0FA0BC;AAED,oDAsBC;AAED,4CAOC;AAED,4CAYC;AAED,4EA4BC;AAMD,sEAsHC;AAnOD,SAAgB,uCAAuC,CACrD,MAAoB,EACpB,OAAyC;IAEzC,MAAM,KAAK,GAAG;QACZ,YAAY,EAAE;YACZ,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE;YAChC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE;YACnC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;YACpC,EAAE,IAAI,EAAE,mBAAmB,EAAE,IAAI,EAAE,SAAS,EAAE;SAC/C;QACD,yBAAyB,EAAE;YACzB,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE;YACjC,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE;YAC/B,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;YAClC,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE;YACvC,EAAE,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,SAAS,EAAE;YACxC,EAAE,IAAI,EAAE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WAAW,EAAE,MAAM,IAAI,OAAO,CAAC;QAC9C,OAAO,EAAE,MAAM,CAAC,WAAW,EAAE,OAAO,IAAI,EAAE,CAAC;QAC3C,IAAI;QACJ,EAAE,EAAE,MAAM,CAAC,WAAW,EAAE,KAAK,IAAI,EAAE,CAAC;QACpC,KAAK,EAAE,MAAM,CAAC,WAAW,EAAE,iBAAiB,IAAI,GAAG,CAAC;QACpD,UAAU,EAAE,MAAM,CAAC,UAAU,CAAC;QAC9B,WAAW,EAAE,MAAM,CAAC,WAAW,CAAC;QAChC,KAAK;QACL,SAAS;KACV,CAAC,CAAC;IACH,OAAO,gBAAgB,CAAC,SAAS,CAAC,CAAC;AACrC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ThirdwebClient } from "../client/client.js";
|
|
2
|
+
import { type RequestedPaymentPayload, type RequestedPaymentRequirements } from "./schemas.js";
|
|
3
|
+
import { type ERC20TokenAmount, type PaymentArgs, type PaymentRequiredResult, type SupportedSignatureType } from "./types.js";
|
|
4
|
+
type GetPaymentRequirementsResult = {
|
|
5
|
+
status: 200;
|
|
6
|
+
paymentRequirements: RequestedPaymentRequirements[];
|
|
7
|
+
selectedPaymentRequirements: RequestedPaymentRequirements;
|
|
8
|
+
decodedPayment: RequestedPaymentPayload;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Decodes a payment request and returns the payment requirements, selected payment requirements, and decoded payment
|
|
12
|
+
* @param args
|
|
13
|
+
* @returns The payment requirements, selected payment requirements, and decoded payment
|
|
14
|
+
*/
|
|
15
|
+
export declare function decodePaymentRequest(args: PaymentArgs): Promise<GetPaymentRequirementsResult | PaymentRequiredResult>;
|
|
16
|
+
export declare function getSupportedSignatureType(args: {
|
|
17
|
+
client: ThirdwebClient;
|
|
18
|
+
asset: string;
|
|
19
|
+
chainId: number;
|
|
20
|
+
eip712Extras: ERC20TokenAmount["asset"]["eip712"] | undefined;
|
|
21
|
+
}): Promise<SupportedSignatureType | undefined>;
|
|
22
|
+
export {};
|
|
23
|
+
//# sourceMappingURL=common.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/x402/common.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAM1D,OAAO,EAEL,KAAK,uBAAuB,EAC5B,KAAK,4BAA4B,EAClC,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,KAAK,qBAAqB,EAC1B,KAAK,sBAAsB,EAE5B,MAAM,YAAY,CAAC;AAEpB,KAAK,4BAA4B,GAAG;IAClC,MAAM,EAAE,GAAG,CAAC;IACZ,mBAAmB,EAAE,4BAA4B,EAAE,CAAC;IACpD,2BAA2B,EAAE,4BAA4B,CAAC;IAC1D,cAAc,EAAE,uBAAuB,CAAC;CACzC,CAAC;AAEF;;;;GAIG;AACH,wBAAsB,oBAAoB,CACxC,IAAI,EAAE,WAAW,GAChB,OAAO,CAAC,4BAA4B,GAAG,qBAAqB,CAAC,CAgF/D;AAED,wBAAsB,yBAAyB,CAAC,IAAI,EAAE;IACpD,MAAM,EAAE,cAAc,CAAC;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,GAAG,SAAS,CAAC;CAC/D,GAAG,OAAO,CAAC,sBAAsB,GAAG,SAAS,CAAC,CAiC9C"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decodePaymentRequest = decodePaymentRequest;
|
|
4
|
+
exports.getSupportedSignatureType = getSupportedSignatureType;
|
|
5
|
+
const utils_1 = require("viem/utils");
|
|
6
|
+
const utils_js_1 = require("../chains/utils.js");
|
|
7
|
+
const resolve_abi_js_1 = require("../contract/actions/resolve-abi.js");
|
|
8
|
+
const contract_js_1 = require("../contract/contract.js");
|
|
9
|
+
const permit_js_1 = require("../extensions/erc20/__generated__/IERC20Permit/write/permit.js");
|
|
10
|
+
const transferWithAuthorization_js_1 = require("../extensions/erc20/__generated__/USDC/write/transferWithAuthorization.js");
|
|
11
|
+
const encode_js_1 = require("./encode.js");
|
|
12
|
+
const schemas_js_1 = require("./schemas.js");
|
|
13
|
+
const types_js_1 = require("./types.js");
|
|
14
|
+
/**
|
|
15
|
+
* Decodes a payment request and returns the payment requirements, selected payment requirements, and decoded payment
|
|
16
|
+
* @param args
|
|
17
|
+
* @returns The payment requirements, selected payment requirements, and decoded payment
|
|
18
|
+
*/
|
|
19
|
+
async function decodePaymentRequest(args) {
|
|
20
|
+
const { price, network, facilitator, payTo, resourceUrl, routeConfig = {}, method, paymentData, extraMetadata, } = args;
|
|
21
|
+
const { errorMessages } = routeConfig;
|
|
22
|
+
const paymentRequirementsResult = await facilitator.accepts({
|
|
23
|
+
resourceUrl,
|
|
24
|
+
method,
|
|
25
|
+
network,
|
|
26
|
+
price,
|
|
27
|
+
routeConfig,
|
|
28
|
+
payTo,
|
|
29
|
+
extraMetadata,
|
|
30
|
+
});
|
|
31
|
+
// Check for payment header, if none, return the payment requirements
|
|
32
|
+
if (!paymentData) {
|
|
33
|
+
return paymentRequirementsResult;
|
|
34
|
+
}
|
|
35
|
+
const paymentRequirements = paymentRequirementsResult.responseBody.accepts;
|
|
36
|
+
// decode b64 payment
|
|
37
|
+
let decodedPayment;
|
|
38
|
+
try {
|
|
39
|
+
decodedPayment = (0, encode_js_1.decodePayment)(paymentData);
|
|
40
|
+
decodedPayment.x402Version = types_js_1.x402Version;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
return {
|
|
44
|
+
status: 402,
|
|
45
|
+
responseHeaders: {
|
|
46
|
+
"Content-Type": "application/json",
|
|
47
|
+
},
|
|
48
|
+
responseBody: {
|
|
49
|
+
x402Version: types_js_1.x402Version,
|
|
50
|
+
error: errorMessages?.invalidPayment ||
|
|
51
|
+
(error instanceof Error ? error.message : "Invalid payment"),
|
|
52
|
+
accepts: paymentRequirements,
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const selectedPaymentRequirements = paymentRequirements.find((value) => value.scheme === decodedPayment.scheme &&
|
|
57
|
+
(0, schemas_js_1.networkToCaip2ChainId)(value.network) ===
|
|
58
|
+
(0, schemas_js_1.networkToCaip2ChainId)(decodedPayment.network));
|
|
59
|
+
if (!selectedPaymentRequirements) {
|
|
60
|
+
return {
|
|
61
|
+
status: 402,
|
|
62
|
+
responseHeaders: {
|
|
63
|
+
"Content-Type": "application/json",
|
|
64
|
+
},
|
|
65
|
+
responseBody: {
|
|
66
|
+
x402Version: types_js_1.x402Version,
|
|
67
|
+
error: errorMessages?.noMatchingRequirements ||
|
|
68
|
+
"Unable to find matching payment requirements",
|
|
69
|
+
accepts: paymentRequirements,
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
status: 200,
|
|
75
|
+
paymentRequirements,
|
|
76
|
+
decodedPayment,
|
|
77
|
+
selectedPaymentRequirements,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
async function getSupportedSignatureType(args) {
|
|
81
|
+
const primaryType = args.eip712Extras?.primaryType;
|
|
82
|
+
if (primaryType === "Permit" || primaryType === "TransferWithAuthorization") {
|
|
83
|
+
return primaryType;
|
|
84
|
+
}
|
|
85
|
+
// not specified, so we need to detect it
|
|
86
|
+
const abi = await (0, resolve_abi_js_1.resolveContractAbi)((0, contract_js_1.getContract)({
|
|
87
|
+
client: args.client,
|
|
88
|
+
address: args.asset,
|
|
89
|
+
chain: (0, utils_js_1.getCachedChain)(args.chainId),
|
|
90
|
+
})).catch((error) => {
|
|
91
|
+
console.error("Error resolving contract ABI", error);
|
|
92
|
+
return [];
|
|
93
|
+
});
|
|
94
|
+
const selectors = abi
|
|
95
|
+
.filter((f) => f.type === "function")
|
|
96
|
+
.map((f) => (0, utils_1.toFunctionSelector)(f));
|
|
97
|
+
const hasPermit = (0, permit_js_1.isPermitSupported)(selectors);
|
|
98
|
+
const hasTransferWithAuthorization = (0, transferWithAuthorization_js_1.isTransferWithAuthorizationSupported)(selectors);
|
|
99
|
+
// prefer transferWithAuthorization over permit
|
|
100
|
+
if (hasTransferWithAuthorization) {
|
|
101
|
+
return "TransferWithAuthorization";
|
|
102
|
+
}
|
|
103
|
+
if (hasPermit) {
|
|
104
|
+
return "Permit";
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/x402/common.ts"],"names":[],"mappings":";;AAkCA,oDAkFC;AAED,8DAsCC;AA3JD,sCAAgD;AAChD,iDAAoD;AAEpD,uEAAwE;AACxE,yDAAsD;AACtD,8FAAmG;AACnG,4HAAiI;AACjI,2CAA4C;AAC5C,6CAIsB;AACtB,yCAMoB;AASpB;;;;GAIG;AACI,KAAK,UAAU,oBAAoB,CACxC,IAAiB;IAEjB,MAAM,EACJ,KAAK,EACL,OAAO,EACP,WAAW,EACX,KAAK,EACL,WAAW,EACX,WAAW,GAAG,EAAE,EAChB,MAAM,EACN,WAAW,EACX,aAAa,GACd,GAAG,IAAI,CAAC;IACT,MAAM,EAAE,aAAa,EAAE,GAAG,WAAW,CAAC;IAEtC,MAAM,yBAAyB,GAAG,MAAM,WAAW,CAAC,OAAO,CAAC;QAC1D,WAAW;QACX,MAAM;QACN,OAAO;QACP,KAAK;QACL,WAAW;QACX,KAAK;QACL,aAAa;KACd,CAAC,CAAC;IAEH,qEAAqE;IACrE,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,OAAO,yBAAyB,CAAC;IACnC,CAAC;IAED,MAAM,mBAAmB,GAAG,yB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IAAI,CAAC,YAAY,EAAE,WAAW,CAAC;IAEnD,IAAI,WAAW,KAAK,QAAQ,IAAI,WAAW,KAAK,2BAA2B,EAAE,CAAC;QAC5E,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,yCAAyC;IACzC,MAAM,GAAG,GAAG,MAAM,IAAA,mCAAkB,EAClC,IAAA,yBAAW,EAAC;QACV,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,OAAO,EAAE,IAAI,CAAC,KAAK;QACnB,KAAK,EAAE,IAAA,yBAAc,EAAC,IAAI,CAAC,OAAO,CAAC;KACpC,CAAC,CACH,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QAChB,OAAO,CAAC,KAAK,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAC;QACrD,OAAO,EAAS,CAAC;IACnB,CAAC,CAAC,CAAC;IACH,MAAM,SAAS,GAAG,GAAG;SAClB,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,UAAU,CAAC;SACpC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAA,0BAAkB,EAAC,CAAC,CAAC,CAAC,CAAC;IACrC,MAAM,SAAS,GAAG,IAAA,6BAAiB,EAAC,SAAS,CAAC,CAAC;IAC/C,MAAM,4BAA4B,GAChC,IAAA,mEAAoC,EAAC,SAAS,CAAC,CAAC;IAElD,+CAA+C;IAC/C,IAAI,4BAA4B,EAAE,CAAC;QACjC,OAAO,2BAA2B,CAAC;IACrC,CAAC;IACD,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,QAAQ,CAAC;IAClB,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type RequestedPaymentPayload } from "./schemas.js";
|
|
2
|
+
/**
|
|
3
|
+
* Encodes a payment payload into a base64 string, ensuring bigint values are properly stringified
|
|
4
|
+
*
|
|
5
|
+
* @param payment - The payment payload to encode
|
|
6
|
+
* @returns A base64 encoded string representation of the payment payload
|
|
7
|
+
*/
|
|
8
|
+
export declare function encodePayment(payment: RequestedPaymentPayload): string;
|
|
9
|
+
/**
|
|
10
|
+
* Decodes a base64 encoded payment string back into a PaymentPayload object
|
|
11
|
+
*
|
|
12
|
+
* @param payment - The base64 encoded payment string to decode
|
|
13
|
+
* @returns The decoded and validated PaymentPayload object
|
|
14
|
+
*/
|
|
15
|
+
export declare function decodePayment(payment: string): RequestedPaymentPayload;
|
|
16
|
+
/**
|
|
17
|
+
* Encodes a string to base64 format
|
|
18
|
+
*
|
|
19
|
+
* @param data - The string to be encoded to base64
|
|
20
|
+
* @returns The base64 encoded string
|
|
21
|
+
*/
|
|
22
|
+
export declare function safeBase64Encode(data: string): string;
|
|
23
|
+
//# sourceMappingURL=encode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../src/x402/encode.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,uBAAuB,EAE7B,MAAM,cAAc,CAAC;AAEtB;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,uBAAuB,GAAG,MAAM,CAkBtE;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,uBAAuB,CAUtE;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAQrD"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodePayment = encodePayment;
|
|
4
|
+
exports.decodePayment = decodePayment;
|
|
5
|
+
exports.safeBase64Encode = safeBase64Encode;
|
|
6
|
+
const schemas_js_1 = require("./schemas.js");
|
|
7
|
+
/**
|
|
8
|
+
* Encodes a payment payload into a base64 string, ensuring bigint values are properly stringified
|
|
9
|
+
*
|
|
10
|
+
* @param payment - The payment payload to encode
|
|
11
|
+
* @returns A base64 encoded string representation of the payment payload
|
|
12
|
+
*/
|
|
13
|
+
function encodePayment(payment) {
|
|
14
|
+
let safe;
|
|
15
|
+
// evm
|
|
16
|
+
const evmPayload = payment.payload;
|
|
17
|
+
safe = {
|
|
18
|
+
...payment,
|
|
19
|
+
payload: {
|
|
20
|
+
...evmPayload,
|
|
21
|
+
authorization: Object.fromEntries(Object.entries(evmPayload.authorization).map(([key, value]) => [
|
|
22
|
+
key,
|
|
23
|
+
typeof value === "bigint" ? value.toString() : value,
|
|
24
|
+
])),
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
return safeBase64Encode(JSON.stringify(safe));
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Decodes a base64 encoded payment string back into a PaymentPayload object
|
|
31
|
+
*
|
|
32
|
+
* @param payment - The base64 encoded payment string to decode
|
|
33
|
+
* @returns The decoded and validated PaymentPayload object
|
|
34
|
+
*/
|
|
35
|
+
function decodePayment(payment) {
|
|
36
|
+
const decoded = safeBase64Decode(payment);
|
|
37
|
+
const parsed = JSON.parse(decoded);
|
|
38
|
+
const obj = {
|
|
39
|
+
...parsed,
|
|
40
|
+
payload: parsed.payload,
|
|
41
|
+
};
|
|
42
|
+
const validated = schemas_js_1.RequestedPaymentPayloadSchema.parse(obj);
|
|
43
|
+
return validated;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Encodes a string to base64 format
|
|
47
|
+
*
|
|
48
|
+
* @param data - The string to be encoded to base64
|
|
49
|
+
* @returns The base64 encoded string
|
|
50
|
+
*/
|
|
51
|
+
function safeBase64Encode(data) {
|
|
52
|
+
if (typeof globalThis !== "undefined" &&
|
|
53
|
+
typeof globalThis.btoa === "function") {
|
|
54
|
+
return globalThis.btoa(data);
|
|
55
|
+
}
|
|
56
|
+
return Buffer.from(data).toString("base64");
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Decodes a base64 string back to its original format
|
|
60
|
+
*
|
|
61
|
+
* @param data - The base64 encoded string to be decoded
|
|
62
|
+
* @returns The decoded string in UTF-8 format
|
|
63
|
+
*/
|
|
64
|
+
function safeBase64Decode(data) {
|
|
65
|
+
if (typeof globalThis !== "undefined" &&
|
|
66
|
+
typeof globalThis.atob === "function") {
|
|
67
|
+
return globalThis.atob(data);
|
|
68
|
+
}
|
|
69
|
+
return Buffer.from(data, "base64").toString("utf-8");
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=encode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode.js","sourceRoot":"","sources":["../../src/x402/encode.ts"],"names":[],"mappings":";;AAYA,sCAkBC;AAQD,sCAUC;AAQD,4CAQC;AA/DD,6CAGsB;AAEtB;;;;;GAKG;AACH,SAAgB,aAAa,CAAC,OAAgC;IAC5D,IAAI,IAA6B,CAAC;IAElC,MAAM;IACN,MAAM,UAAU,GAAG,OAAO,CAAC,OAA0B,CAAC;IACtD,IAAI,GAAG;QACL,GAAG,OAAO;QACV,OAAO,EAAE;YACP,GAAG,UAAU;YACb,aAAa,EAAE,MAAM,CAAC,WAAW,CAC/B,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,aAAa,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC;gBAC7D,GAAG;gBACH,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAE,KAAgB,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,KAAK;aACjE,CAAC,CACiC;SACtC;KACF,CAAC;IACF,OAAO,gBAAgB,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;AAChD,CAAC;AAED;;;;;GAKG;AACH,SAAgB,aAAa,CAAC,OAAe;IAC3C,MAAM,OAAO,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAC1C,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAEnC,MAAM,GAAG,GAA4B;QACnC,GAAG,MAAM;QACT,OAAO,EAAE,MAAM,CAAC,OAA0B;KAC3C,CAAC;IACF,MAAM,SAAS,GAAG,0CAA6B,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC3D,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;GAKG;AACH,SAAgB,gBAAgB,CAAC,IAAY;IAC3C,IACE,OAAO,UAAU,KAAK,WAAW;QACjC,OAAO,UAAU,CAAC,IAAI,KAAK,UAAU,EACrC,CAAC;QACD,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAC9C,CAAC;AAED;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,IAAY;IACpC,IACE,OAAO,UAAU,KAAK,WAAW;QACjC,OAAO,UAAU,CAAC,IAAI,KAAK,UAAU,EACrC,CAAC;QACD,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;AACvD,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ThirdwebClient } from "../client/client.js";
|
|
2
|
+
import { type FacilitatorSettleResponse, type FacilitatorSupportedResponse, type FacilitatorVerifyResponse, type RequestedPaymentPayload, type RequestedPaymentRequirements } from "./schemas.js";
|
|
3
|
+
import type { PaymentArgs, PaymentRequiredResult } from "./types.js";
|
|
4
|
+
export type WaitUntil = "simulated" | "submitted" | "confirmed";
|
|
5
|
+
export type ThirdwebX402FacilitatorConfig = {
|
|
6
|
+
client: ThirdwebClient;
|
|
7
|
+
serverWalletAddress: string;
|
|
8
|
+
waitUntil?: WaitUntil;
|
|
9
|
+
vaultAccessToken?: string;
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* facilitator for the x402 payment protocol.
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export type ThirdwebX402Facilitator = {
|
|
17
|
+
url: `${string}://${string}`;
|
|
18
|
+
address: string;
|
|
19
|
+
createAuthHeaders: () => Promise<{
|
|
20
|
+
verify: Record<string, string>;
|
|
21
|
+
settle: Record<string, string>;
|
|
22
|
+
supported: Record<string, string>;
|
|
23
|
+
list: Record<string, string>;
|
|
24
|
+
}>;
|
|
25
|
+
verify: (payload: RequestedPaymentPayload, paymentRequirements: RequestedPaymentRequirements) => Promise<FacilitatorVerifyResponse>;
|
|
26
|
+
settle: (payload: RequestedPaymentPayload, paymentRequirements: RequestedPaymentRequirements, waitUntil?: WaitUntil) => Promise<FacilitatorSettleResponse>;
|
|
27
|
+
supported: (filters?: {
|
|
28
|
+
chainId: number;
|
|
29
|
+
tokenAddress?: string;
|
|
30
|
+
}) => Promise<FacilitatorSupportedResponse>;
|
|
31
|
+
accepts: (args: Omit<PaymentArgs, "facilitator">) => Promise<PaymentRequiredResult>;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Creates a facilitator for the x402 payment protocol.
|
|
35
|
+
* You can use this with `settlePayment` or with any x402 middleware to enable settling transactions with your thirdweb server wallet.
|
|
36
|
+
*
|
|
37
|
+
* @param config - The configuration for the facilitator
|
|
38
|
+
* @returns a x402 compatible FacilitatorConfig
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { facilitator } from "thirdweb/x402";
|
|
43
|
+
* import { createThirdwebClient } from "thirdweb";
|
|
44
|
+
* import { paymentMiddleware } from 'x402-hono'
|
|
45
|
+
*
|
|
46
|
+
* const client = createThirdwebClient({
|
|
47
|
+
* secretKey: "your-secret-key",
|
|
48
|
+
* });
|
|
49
|
+
* const thirdwebX402Facilitator = facilitator({
|
|
50
|
+
* client: client,
|
|
51
|
+
* serverWalletAddress: "0x1234567890123456789012345678901234567890",
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* // add the facilitator to any x402 payment middleware
|
|
55
|
+
* const middleware = paymentMiddleware(
|
|
56
|
+
* "0x1234567890123456789012345678901234567890",
|
|
57
|
+
* {
|
|
58
|
+
* "/api/paywall": {
|
|
59
|
+
* price: "$0.01",
|
|
60
|
+
* network: "base-sepolia",
|
|
61
|
+
* config: {
|
|
62
|
+
* description: "Access to paid content",
|
|
63
|
+
* },
|
|
64
|
+
* },
|
|
65
|
+
* },
|
|
66
|
+
* thirdwebX402Facilitator,
|
|
67
|
+
* );
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* #### Configuration Options
|
|
71
|
+
*
|
|
72
|
+
* ```ts
|
|
73
|
+
* const thirdwebX402Facilitator = facilitator({
|
|
74
|
+
* client: client,
|
|
75
|
+
* serverWalletAddress: "0x1234567890123456789012345678901234567890",
|
|
76
|
+
* // Optional: Wait behavior for settlements
|
|
77
|
+
* // - "simulated": Only simulate the transaction (fastest)
|
|
78
|
+
* // - "submitted": Wait until transaction is submitted
|
|
79
|
+
* // - "confirmed": Wait for full on-chain confirmation (slowest, default)
|
|
80
|
+
* waitUntil: "confirmed",
|
|
81
|
+
* });
|
|
82
|
+
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @bridge x402
|
|
86
|
+
*/
|
|
87
|
+
export declare function facilitator(config: ThirdwebX402FacilitatorConfig): ThirdwebX402Facilitator;
|
|
88
|
+
//# sourceMappingURL=facilitator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"facilitator.d.ts","sourceRoot":"","sources":["../../src/x402/facilitator.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAG1D,OAAO,EACL,KAAK,yBAAyB,EAC9B,KAAK,4BAA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