@armory-sh/client-ethers 0.2.12 → 0.2.14
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 +68 -38
- package/dist/index.js +54 -184
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,63 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { Address, BalanceOfParams, CAIP2ChainId, CAIPAssetId, EIP712_TYPES, ERC20_ABI, Extensions, NETWORKS, NetworkConfig, PayToV2, PaymentPayload,
|
|
1
|
+
import { PaymentRequirementsV2, Address, PaymentPayloadV2, CustomToken } from '@armory-sh/base';
|
|
2
|
+
export { Address, BalanceOfParams, CAIP2ChainId, CAIPAssetId, EIP712_TYPES, ERC20_ABI, Extensions, NETWORKS, NetworkConfig, PayToV2, PaymentPayload, PaymentPayloadV2, PaymentRequirements, PaymentRequirementsV2, SettlementResponse, SettlementResponseV2, Signature, TransferWithAuthorizationParams, V2_HEADERS, createEIP712Domain, createTransferWithAuthorization, decodePayment, decodePaymentV2, decodeSettlementV2, detectPaymentVersion, encodePaymentV2, encodeSettlementV2, getMainnets, getNetworkByChainId, getNetworkConfig, getTestnets, getTxHash, isCAIP2ChainId, isCAIPAssetId, isPaymentV2, isSettlementSuccessful, isSettlementV2, isX402V2PaymentRequired, safeBase64Decode, validateTransferWithAuthorization } from '@armory-sh/base';
|
|
3
3
|
import { Signer, Provider } from 'ethers';
|
|
4
4
|
export { Provider, Signer } from 'ethers';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* X402 Protocol Implementation for Ethers Client
|
|
7
|
+
* X402 Protocol Implementation for Ethers Client (V2 Only)
|
|
8
8
|
*
|
|
9
|
-
* Handles parsing x402
|
|
10
|
-
* and generating x402
|
|
9
|
+
* Handles parsing x402 V2 PAYMENT-REQUIRED headers
|
|
10
|
+
* and generating x402 V2 PAYMENT-SIGNATURE payloads
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Detect x402 protocol version from response headers
|
|
15
|
+
* V2-only: Always returns 2
|
|
15
16
|
*/
|
|
16
|
-
declare function detectX402Version(
|
|
17
|
+
declare function detectX402Version(_response: Response): 2;
|
|
17
18
|
/**
|
|
18
|
-
*
|
|
19
|
+
* Get payment header name for protocol version
|
|
19
20
|
*/
|
|
21
|
+
declare function getPaymentHeaderName(_version: 2): string;
|
|
20
22
|
interface ParsedPaymentRequirements {
|
|
21
|
-
version:
|
|
22
|
-
requirements:
|
|
23
|
+
version: 2;
|
|
24
|
+
requirements: PaymentRequirementsV2;
|
|
23
25
|
}
|
|
24
26
|
/**
|
|
25
27
|
* Parse x402 PAYMENT-REQUIRED header from response
|
|
26
|
-
*
|
|
28
|
+
* V2 only
|
|
27
29
|
*/
|
|
28
30
|
declare function parsePaymentRequired(response: Response): ParsedPaymentRequirements;
|
|
29
31
|
/**
|
|
30
|
-
* Create x402
|
|
32
|
+
* Create x402 payment payload (V2-only wrapper)
|
|
31
33
|
*/
|
|
32
|
-
declare function
|
|
33
|
-
/**
|
|
34
|
-
* Create x402 V2 payment payload
|
|
35
|
-
*/
|
|
36
|
-
declare function createX402V2Payment(signer: Signer, requirements: PaymentRequirementsV2, fromAddress: Address, nonce: `0x${string}`, validBefore: number, domainName?: string, domainVersion?: string): Promise<PaymentPayloadV2>;
|
|
37
|
-
/**
|
|
38
|
-
* Create x402 payment payload (auto-detects version)
|
|
39
|
-
*/
|
|
40
|
-
declare function createX402Payment(signer: Signer, parsed: ParsedPaymentRequirements, fromAddress: Address, nonce?: `0x${string}`, validBefore?: number, domainName?: string, domainVersion?: string): Promise<X402PaymentPayloadV1 | PaymentPayloadV2>;
|
|
34
|
+
declare function createX402Payment(signer: Signer, parsed: ParsedPaymentRequirements, fromAddress: Address, nonce?: `0x${string}`, validBefore?: number, domainName?: string, domainVersion?: string): Promise<PaymentPayloadV2>;
|
|
41
35
|
/**
|
|
42
36
|
* Encode x402 payment payload to Base64 for transport
|
|
43
37
|
*/
|
|
44
|
-
declare function encodeX402Payment(payload:
|
|
45
|
-
/**
|
|
46
|
-
* Get the correct header name for payment based on version
|
|
47
|
-
*/
|
|
48
|
-
declare function getPaymentHeaderName(version: 1 | 2): string;
|
|
49
|
-
/**
|
|
50
|
-
* @deprecated Use createX402Payment instead
|
|
51
|
-
*/
|
|
52
|
-
declare function createPaymentPayload(requirements: unknown, signer: Signer, from: string): Promise<[string, string]>;
|
|
53
|
-
/**
|
|
54
|
-
* @deprecated Use parsePaymentRequired instead
|
|
55
|
-
*/
|
|
56
|
-
declare function parsePaymentRequirements(response: Response): Promise<unknown>;
|
|
57
|
-
/**
|
|
58
|
-
* @deprecated Use detectX402Version instead
|
|
59
|
-
*/
|
|
60
|
-
declare function detectProtocolVersion(requirements: unknown): 1 | 2;
|
|
38
|
+
declare function encodeX402Payment(payload: PaymentPayloadV2): string;
|
|
61
39
|
|
|
62
40
|
declare class X402ClientError extends Error {
|
|
63
41
|
readonly cause?: unknown;
|
|
@@ -168,4 +146,56 @@ declare function recoverEIP3009Signer(params: TransferWithAuthorizationParams, s
|
|
|
168
146
|
s: string;
|
|
169
147
|
}, domain: EIP712Domain): Promise<`0x${string}`>;
|
|
170
148
|
|
|
171
|
-
|
|
149
|
+
/**
|
|
150
|
+
* X402 Client Factory for Ethers
|
|
151
|
+
*
|
|
152
|
+
* Creates a configured client instance for making X-402 payments
|
|
153
|
+
*/
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* X402 Client instance
|
|
157
|
+
* Provides a high-level API for making X-402 authenticated requests
|
|
158
|
+
*/
|
|
159
|
+
interface X402Client {
|
|
160
|
+
/**
|
|
161
|
+
* Make a GET request
|
|
162
|
+
*/
|
|
163
|
+
get(url: string, init?: X402RequestInit): Promise<Response>;
|
|
164
|
+
/**
|
|
165
|
+
* Make a POST request
|
|
166
|
+
*/
|
|
167
|
+
post(url: string, body?: unknown, init?: X402RequestInit): Promise<Response>;
|
|
168
|
+
/**
|
|
169
|
+
* Make a PUT request
|
|
170
|
+
*/
|
|
171
|
+
put(url: string, body?: unknown, init?: X402RequestInit): Promise<Response>;
|
|
172
|
+
/**
|
|
173
|
+
* Make a DELETE request
|
|
174
|
+
*/
|
|
175
|
+
del(url: string, init?: X402RequestInit): Promise<Response>;
|
|
176
|
+
/**
|
|
177
|
+
* Make a PATCH request
|
|
178
|
+
*/
|
|
179
|
+
patch(url: string, body?: unknown, init?: X402RequestInit): Promise<Response>;
|
|
180
|
+
/**
|
|
181
|
+
* Set the signer for payment signing
|
|
182
|
+
*/
|
|
183
|
+
setSigner(signer: Signer): void;
|
|
184
|
+
/**
|
|
185
|
+
* Get the current signer
|
|
186
|
+
*/
|
|
187
|
+
getSigner(): Signer | undefined;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Create an X402 client
|
|
192
|
+
*
|
|
193
|
+
* @param config - Client configuration
|
|
194
|
+
* @returns Configured X402 client
|
|
195
|
+
*/
|
|
196
|
+
declare function createX402Client(config: X402ClientConfig & SignerClientConfig & {
|
|
197
|
+
signer: Signer;
|
|
198
|
+
}): X402Client;
|
|
199
|
+
declare function createX402Client(config: X402ClientConfig & ProviderClientConfig): X402Client;
|
|
200
|
+
|
|
201
|
+
export { AuthorizationError, type ClientConfig, type ParsedPaymentRequirements, PaymentError, ProviderRequiredError, SignerRequiredError, SigningError, type X402Client, type X402ClientConfig, X402ClientError, type X402RequestInit, type X402Transport, type X402TransportConfig, createX402Client, createX402Payment, createX402Transport, detectX402Version, encodeX402Payment, getPaymentHeaderName, parsePaymentRequired, recoverEIP3009Signer, signEIP3009, signEIP3009WithDomain, signPayment };
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import {
|
|
3
|
-
V1_HEADERS as V1_HEADERS3,
|
|
4
3
|
V2_HEADERS as V2_HEADERS3,
|
|
5
|
-
isV1,
|
|
6
|
-
isV2,
|
|
7
|
-
getPaymentVersion,
|
|
8
|
-
getRequirementsVersion,
|
|
9
|
-
getSettlementVersion,
|
|
10
|
-
getPaymentHeaderName as getPaymentHeaderName2,
|
|
11
|
-
getPaymentResponseHeaderName,
|
|
12
|
-
getPaymentRequiredHeaderName,
|
|
13
4
|
isSettlementSuccessful as isSettlementSuccessful2,
|
|
14
5
|
getTxHash,
|
|
15
6
|
NETWORKS,
|
|
@@ -18,41 +9,28 @@ import {
|
|
|
18
9
|
getMainnets,
|
|
19
10
|
getTestnets,
|
|
20
11
|
ERC20_ABI,
|
|
21
|
-
encodePaymentV1,
|
|
22
|
-
decodePaymentV1,
|
|
23
|
-
encodeSettlementV1,
|
|
24
|
-
decodeSettlementV1,
|
|
25
12
|
encodePaymentV2,
|
|
26
13
|
decodePaymentV2,
|
|
27
14
|
encodeSettlementV2,
|
|
28
|
-
decodeSettlementV2,
|
|
15
|
+
decodeSettlementV2 as decodeSettlementV22,
|
|
29
16
|
detectPaymentVersion,
|
|
30
17
|
decodePayment,
|
|
31
|
-
decodeSettlementLegacy as decodeSettlementLegacy2,
|
|
32
|
-
isPaymentV1,
|
|
33
18
|
isPaymentV2,
|
|
34
|
-
isSettlementV1,
|
|
35
19
|
isSettlementV2,
|
|
36
|
-
isX402V1PaymentRequired as isX402V1PaymentRequired2,
|
|
37
20
|
isX402V2PaymentRequired as isX402V2PaymentRequired2,
|
|
38
|
-
isX402V1Requirements as isX402V1Requirements2,
|
|
39
21
|
EIP712_TYPES as EIP712_TYPES2,
|
|
40
22
|
createEIP712Domain as createEIP712Domain3,
|
|
41
23
|
createTransferWithAuthorization as createTransferWithAuthorization2,
|
|
42
24
|
validateTransferWithAuthorization,
|
|
43
25
|
isCAIP2ChainId,
|
|
44
26
|
isCAIPAssetId,
|
|
45
|
-
safeBase64Decode
|
|
27
|
+
safeBase64Decode
|
|
46
28
|
} from "@armory-sh/base";
|
|
47
29
|
|
|
48
30
|
// src/protocol.ts
|
|
49
31
|
import {
|
|
50
|
-
V1_HEADERS,
|
|
51
32
|
V2_HEADERS,
|
|
52
|
-
safeBase64Decode,
|
|
53
|
-
isX402V1PaymentRequired,
|
|
54
33
|
isX402V2PaymentRequired,
|
|
55
|
-
isX402V1Requirements,
|
|
56
34
|
getNetworkByChainId,
|
|
57
35
|
getNetworkConfig,
|
|
58
36
|
createEIP712Domain as createEIP712Domain2,
|
|
@@ -173,71 +151,32 @@ async function recoverEIP3009Signer(params, signature, domain) {
|
|
|
173
151
|
}
|
|
174
152
|
|
|
175
153
|
// src/protocol.ts
|
|
176
|
-
function detectX402Version(
|
|
177
|
-
const v2Header = response.headers.get(V2_HEADERS.PAYMENT_REQUIRED);
|
|
178
|
-
if (v2Header) {
|
|
179
|
-
try {
|
|
180
|
-
const parsed = JSON.parse(v2Header);
|
|
181
|
-
if (parsed.x402Version === 2) return 2;
|
|
182
|
-
} catch {
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
const v1Header = response.headers.get(V1_HEADERS.PAYMENT_REQUIRED);
|
|
186
|
-
if (v1Header) {
|
|
187
|
-
try {
|
|
188
|
-
const decoded = safeBase64Decode(v1Header);
|
|
189
|
-
const parsed = JSON.parse(decoded);
|
|
190
|
-
if (parsed.x402Version === 1) return 1;
|
|
191
|
-
return 1;
|
|
192
|
-
} catch {
|
|
193
|
-
}
|
|
194
|
-
}
|
|
154
|
+
function detectX402Version(_response) {
|
|
195
155
|
return 2;
|
|
196
156
|
}
|
|
157
|
+
function getPaymentHeaderName(_version) {
|
|
158
|
+
return V2_HEADERS.PAYMENT_SIGNATURE;
|
|
159
|
+
}
|
|
197
160
|
function parsePaymentRequired(response) {
|
|
198
|
-
const
|
|
199
|
-
if (
|
|
200
|
-
|
|
201
|
-
if (!v2Header) {
|
|
202
|
-
throw new PaymentError("No PAYMENT-REQUIRED header found in 402 response");
|
|
203
|
-
}
|
|
204
|
-
try {
|
|
205
|
-
const parsed = JSON.parse(v2Header);
|
|
206
|
-
if (!isX402V2PaymentRequired(parsed)) {
|
|
207
|
-
throw new PaymentError("Invalid x402 V2 payment required format");
|
|
208
|
-
}
|
|
209
|
-
if (!parsed.accepts || parsed.accepts.length === 0) {
|
|
210
|
-
throw new PaymentError("No payment requirements found in accepts array");
|
|
211
|
-
}
|
|
212
|
-
return {
|
|
213
|
-
version: 2,
|
|
214
|
-
requirements: parsed.accepts[0]
|
|
215
|
-
};
|
|
216
|
-
} catch (error) {
|
|
217
|
-
if (error instanceof PaymentError) throw error;
|
|
218
|
-
throw new PaymentError(`Failed to parse V2 PAYMENT-REQUIRED header: ${error}`);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
const v1Header = response.headers.get(V1_HEADERS.PAYMENT_REQUIRED);
|
|
222
|
-
if (!v1Header) {
|
|
223
|
-
throw new PaymentError("No X-PAYMENT-REQUIRED header found in 402 response");
|
|
161
|
+
const v2Header = response.headers.get(V2_HEADERS.PAYMENT_REQUIRED);
|
|
162
|
+
if (!v2Header) {
|
|
163
|
+
throw new PaymentError("No PAYMENT-REQUIRED header found in V2 response");
|
|
224
164
|
}
|
|
225
165
|
try {
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
throw new PaymentError("Invalid x402 V1 payment required format");
|
|
166
|
+
const parsed = JSON.parse(v2Header);
|
|
167
|
+
if (!isX402V2PaymentRequired(parsed)) {
|
|
168
|
+
throw new PaymentError("Invalid x402 V2 payment required format");
|
|
230
169
|
}
|
|
231
170
|
if (!parsed.accepts || parsed.accepts.length === 0) {
|
|
232
171
|
throw new PaymentError("No payment requirements found in accepts array");
|
|
233
172
|
}
|
|
234
173
|
return {
|
|
235
|
-
version:
|
|
174
|
+
version: 2,
|
|
236
175
|
requirements: parsed.accepts[0]
|
|
237
176
|
};
|
|
238
177
|
} catch (error) {
|
|
239
178
|
if (error instanceof PaymentError) throw error;
|
|
240
|
-
throw new PaymentError(`Failed to parse
|
|
179
|
+
throw new PaymentError(`Failed to parse V2 PAYMENT-REQUIRED header: ${error}`);
|
|
241
180
|
}
|
|
242
181
|
}
|
|
243
182
|
function toAtomicUnits(amount) {
|
|
@@ -253,56 +192,10 @@ function extractChainId(network) {
|
|
|
253
192
|
}
|
|
254
193
|
throw new PaymentError(`Unsupported network: ${network}`);
|
|
255
194
|
}
|
|
256
|
-
function getNetworkSlug(network) {
|
|
257
|
-
if (network.startsWith("eip155:")) {
|
|
258
|
-
const chainId = parseInt(network.split(":")[1], 10);
|
|
259
|
-
const net = getNetworkByChainId(chainId);
|
|
260
|
-
if (!net) {
|
|
261
|
-
throw new PaymentError(`No network config found for chainId: ${chainId}`);
|
|
262
|
-
}
|
|
263
|
-
return normalizeNetworkName(net.name);
|
|
264
|
-
}
|
|
265
|
-
return normalizeNetworkName(network);
|
|
266
|
-
}
|
|
267
195
|
function createNonce() {
|
|
268
196
|
const now = Math.floor(Date.now() / 1e3);
|
|
269
197
|
return `0x${(now * 1e3).toString(16).padStart(64, "0")}`;
|
|
270
198
|
}
|
|
271
|
-
async function createX402V1Payment(signer, requirements, fromAddress, nonce, validBefore, domainName, domainVersion) {
|
|
272
|
-
const network = getNetworkSlug(requirements.network);
|
|
273
|
-
const contractAddress = requirements.asset;
|
|
274
|
-
const chainId = extractChainId(requirements.network);
|
|
275
|
-
const domain = createEIP712Domain2(chainId, contractAddress);
|
|
276
|
-
const customDomain = domainName || domainVersion ? { ...domain, name: domainName ?? domain.name, version: domainVersion ?? domain.version } : domain;
|
|
277
|
-
const authorization = {
|
|
278
|
-
from: fromAddress,
|
|
279
|
-
to: requirements.payTo,
|
|
280
|
-
value: toAtomicUnits(requirements.maxAmountRequired),
|
|
281
|
-
validAfter: "0",
|
|
282
|
-
validBefore: validBefore.toString(),
|
|
283
|
-
nonce
|
|
284
|
-
};
|
|
285
|
-
const authParams = {
|
|
286
|
-
from: authorization.from,
|
|
287
|
-
to: authorization.to,
|
|
288
|
-
value: BigInt(authorization.value),
|
|
289
|
-
validAfter: BigInt(authorization.validAfter),
|
|
290
|
-
validBefore: BigInt(authorization.validBefore),
|
|
291
|
-
nonce: BigInt(authorization.nonce)
|
|
292
|
-
};
|
|
293
|
-
const signature = await signEIP3009(signer, authParams, customDomain);
|
|
294
|
-
const combinedSignature = `0x${signature.r.slice(2)}${signature.s.slice(2)}${signature.v.toString(16).padStart(2, "0")}`;
|
|
295
|
-
const payload = {
|
|
296
|
-
signature: combinedSignature,
|
|
297
|
-
authorization
|
|
298
|
-
};
|
|
299
|
-
return {
|
|
300
|
-
x402Version: 1,
|
|
301
|
-
scheme: "exact",
|
|
302
|
-
network,
|
|
303
|
-
payload
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
199
|
async function createX402V2Payment(signer, requirements, fromAddress, nonce, validBefore, domainName, domainVersion) {
|
|
307
200
|
const contractAddress = requirements.asset;
|
|
308
201
|
const chainId = extractChainId(requirements.network);
|
|
@@ -332,24 +225,14 @@ async function createX402V2Payment(signer, requirements, fromAddress, nonce, val
|
|
|
332
225
|
};
|
|
333
226
|
return {
|
|
334
227
|
x402Version: 2,
|
|
335
|
-
|
|
228
|
+
scheme: requirements.scheme,
|
|
229
|
+
network: requirements.network,
|
|
336
230
|
payload
|
|
337
231
|
};
|
|
338
232
|
}
|
|
339
233
|
async function createX402Payment(signer, parsed, fromAddress, nonce, validBefore, domainName, domainVersion) {
|
|
340
234
|
const effectiveNonce = nonce ?? createNonce();
|
|
341
235
|
const effectiveValidBefore = validBefore ?? Math.floor(Date.now() / 1e3) + 3600;
|
|
342
|
-
if (parsed.version === 1) {
|
|
343
|
-
return createX402V1Payment(
|
|
344
|
-
signer,
|
|
345
|
-
parsed.requirements,
|
|
346
|
-
fromAddress,
|
|
347
|
-
effectiveNonce,
|
|
348
|
-
effectiveValidBefore,
|
|
349
|
-
domainName,
|
|
350
|
-
domainVersion
|
|
351
|
-
);
|
|
352
|
-
}
|
|
353
236
|
return createX402V2Payment(
|
|
354
237
|
signer,
|
|
355
238
|
parsed.requirements,
|
|
@@ -363,34 +246,11 @@ async function createX402Payment(signer, parsed, fromAddress, nonce, validBefore
|
|
|
363
246
|
function encodeX402Payment(payload) {
|
|
364
247
|
return Buffer.from(JSON.stringify(payload)).toString("base64");
|
|
365
248
|
}
|
|
366
|
-
function getPaymentHeaderName(version) {
|
|
367
|
-
return version === 1 ? V1_HEADERS.PAYMENT : V2_HEADERS.PAYMENT_SIGNATURE;
|
|
368
|
-
}
|
|
369
|
-
async function createPaymentPayload(requirements, signer, from) {
|
|
370
|
-
const parsed = isX402V1Requirements(requirements) ? { version: 1, requirements } : { version: 2, requirements };
|
|
371
|
-
const payload = await createX402Payment(signer, parsed, from);
|
|
372
|
-
const encoded = encodeX402Payment(payload);
|
|
373
|
-
return [encoded, getPaymentHeaderName(parsed.version)];
|
|
374
|
-
}
|
|
375
|
-
async function parsePaymentRequirements(response) {
|
|
376
|
-
const parsed = parsePaymentRequired(response);
|
|
377
|
-
return parsed.requirements;
|
|
378
|
-
}
|
|
379
|
-
function detectProtocolVersion(requirements) {
|
|
380
|
-
if (typeof requirements === "object" && requirements !== null) {
|
|
381
|
-
if ("x402Version" in requirements && requirements.x402Version === 2) {
|
|
382
|
-
return 2;
|
|
383
|
-
}
|
|
384
|
-
if ("chainId" in requirements && "assetId" in requirements && !("contractAddress" in requirements)) {
|
|
385
|
-
return 2;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
return 1;
|
|
389
|
-
}
|
|
390
249
|
|
|
391
250
|
// src/transport.ts
|
|
392
251
|
import {
|
|
393
|
-
|
|
252
|
+
decodeSettlementV2,
|
|
253
|
+
V2_HEADERS as V2_HEADERS2
|
|
394
254
|
} from "@armory-sh/base";
|
|
395
255
|
var defaultConfig = {
|
|
396
256
|
baseURL: "",
|
|
@@ -442,7 +302,7 @@ var handlePaymentRequired = async (state, response) => {
|
|
|
442
302
|
},
|
|
443
303
|
state.config.timeout
|
|
444
304
|
);
|
|
445
|
-
const settlement =
|
|
305
|
+
const settlement = decodeSettlementV2(paymentResponse.headers.get(V2_HEADERS2.PAYMENT_RESPONSE) || "");
|
|
446
306
|
return { success: true, settlement };
|
|
447
307
|
} catch (error) {
|
|
448
308
|
return { success: false, error: error instanceof Error ? error : new Error(String(error)) };
|
|
@@ -502,6 +362,38 @@ var createX402Transport = (config) => {
|
|
|
502
362
|
getSigner: () => state.signer
|
|
503
363
|
};
|
|
504
364
|
};
|
|
365
|
+
|
|
366
|
+
// src/client.ts
|
|
367
|
+
function createX402Client(config) {
|
|
368
|
+
const transport = createX402Transport(void 0);
|
|
369
|
+
if ("signer" in config && config.signer) {
|
|
370
|
+
transport.setSigner(config.signer);
|
|
371
|
+
} else if ("provider" in config && config.provider) {
|
|
372
|
+
const provider = config.provider;
|
|
373
|
+
if (provider.getSigner) {
|
|
374
|
+
try {
|
|
375
|
+
const signer = provider.getSigner();
|
|
376
|
+
if (signer) {
|
|
377
|
+
transport.setSigner(signer);
|
|
378
|
+
}
|
|
379
|
+
} catch {
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
} else {
|
|
383
|
+
throw new SignerRequiredError(
|
|
384
|
+
"Either 'signer' or 'provider' with getSigner() must be provided"
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
return {
|
|
388
|
+
get: (url, init) => transport.get(url, init),
|
|
389
|
+
post: (url, body, init) => transport.post(url, body, init),
|
|
390
|
+
put: (url, body, init) => transport.put(url, body, init),
|
|
391
|
+
del: (url, init) => transport.del(url, init),
|
|
392
|
+
patch: (url, body, init) => transport.patch(url, body, init),
|
|
393
|
+
setSigner: (signer) => transport.setSigner(signer),
|
|
394
|
+
getSigner: () => transport.getSigner()
|
|
395
|
+
};
|
|
396
|
+
}
|
|
505
397
|
export {
|
|
506
398
|
AuthorizationError,
|
|
507
399
|
EIP712_TYPES2 as EIP712_TYPES,
|
|
@@ -511,58 +403,36 @@ export {
|
|
|
511
403
|
ProviderRequiredError,
|
|
512
404
|
SignerRequiredError,
|
|
513
405
|
SigningError,
|
|
514
|
-
V1_HEADERS3 as V1_HEADERS,
|
|
515
406
|
V2_HEADERS3 as V2_HEADERS,
|
|
516
407
|
X402ClientError,
|
|
517
408
|
createEIP712Domain3 as createEIP712Domain,
|
|
518
|
-
createPaymentPayload,
|
|
519
409
|
createTransferWithAuthorization2 as createTransferWithAuthorization,
|
|
410
|
+
createX402Client,
|
|
520
411
|
createX402Payment,
|
|
521
412
|
createX402Transport,
|
|
522
|
-
createX402V1Payment,
|
|
523
|
-
createX402V2Payment,
|
|
524
413
|
decodePayment,
|
|
525
|
-
decodePaymentV1,
|
|
526
414
|
decodePaymentV2,
|
|
527
|
-
|
|
528
|
-
decodeSettlementV1,
|
|
529
|
-
decodeSettlementV2,
|
|
415
|
+
decodeSettlementV22 as decodeSettlementV2,
|
|
530
416
|
detectPaymentVersion,
|
|
531
|
-
detectProtocolVersion,
|
|
532
417
|
detectX402Version,
|
|
533
|
-
encodePaymentV1,
|
|
534
418
|
encodePaymentV2,
|
|
535
|
-
encodeSettlementV1,
|
|
536
419
|
encodeSettlementV2,
|
|
537
420
|
encodeX402Payment,
|
|
538
421
|
getMainnets,
|
|
539
422
|
getNetworkByChainId2 as getNetworkByChainId,
|
|
540
423
|
getNetworkConfig2 as getNetworkConfig,
|
|
541
424
|
getPaymentHeaderName,
|
|
542
|
-
getPaymentHeaderName2 as getPaymentHeaderNameBase,
|
|
543
|
-
getPaymentRequiredHeaderName,
|
|
544
|
-
getPaymentResponseHeaderName,
|
|
545
|
-
getPaymentVersion,
|
|
546
|
-
getRequirementsVersion,
|
|
547
|
-
getSettlementVersion,
|
|
548
425
|
getTestnets,
|
|
549
426
|
getTxHash,
|
|
550
427
|
isCAIP2ChainId,
|
|
551
428
|
isCAIPAssetId,
|
|
552
|
-
isPaymentV1,
|
|
553
429
|
isPaymentV2,
|
|
554
430
|
isSettlementSuccessful2 as isSettlementSuccessful,
|
|
555
|
-
isSettlementV1,
|
|
556
431
|
isSettlementV2,
|
|
557
|
-
isV1,
|
|
558
|
-
isV2,
|
|
559
|
-
isX402V1PaymentRequired2 as isX402V1PaymentRequired,
|
|
560
|
-
isX402V1Requirements2 as isX402V1Requirements,
|
|
561
432
|
isX402V2PaymentRequired2 as isX402V2PaymentRequired,
|
|
562
433
|
parsePaymentRequired,
|
|
563
|
-
parsePaymentRequirements,
|
|
564
434
|
recoverEIP3009Signer,
|
|
565
|
-
|
|
435
|
+
safeBase64Decode,
|
|
566
436
|
signEIP3009,
|
|
567
437
|
signEIP3009WithDomain,
|
|
568
438
|
signPayment,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@armory-sh/client-ethers",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.14",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Sawyer Cutler <sawyer@dirtroad.dev>",
|
|
6
6
|
"type": "module",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"directory": "packages/client-ethers"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@armory-sh/base": "^0.2.
|
|
30
|
+
"@armory-sh/base": "^0.2.14",
|
|
31
31
|
"ethers": "6.16.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"bun-types": "latest"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
|
-
"build": "tsup",
|
|
38
|
+
"build": "rm -rf dist && tsup",
|
|
39
39
|
"test": "bun test",
|
|
40
40
|
"example": "bun run examples/"
|
|
41
41
|
}
|