@armory-sh/client-web3 0.2.23-alpha.23.77 → 0.2.23-alpha.23.79
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +31 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -8,6 +8,7 @@ Armory x402 SDK — Payment client for Web3.js. Make payments from any Web3.js w
|
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
10
|
bun add @armory-sh/client-web3
|
|
11
|
+
bun add @armory-sh/client-hooks # optional preference/logger hooks
|
|
11
12
|
```
|
|
12
13
|
|
|
13
14
|
## Why Armory?
|
|
@@ -60,6 +61,25 @@ const response = await client.fetch('https://api.example.com/protected')
|
|
|
60
61
|
const data = await response.json()
|
|
61
62
|
```
|
|
62
63
|
|
|
64
|
+
## Hook Pipeline
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { createX402Client } from '@armory-sh/client-web3'
|
|
68
|
+
import { PaymentPreference, Logger } from '@armory-sh/client-hooks'
|
|
69
|
+
|
|
70
|
+
const client = createX402Client({
|
|
71
|
+
account,
|
|
72
|
+
hooks: [
|
|
73
|
+
PaymentPreference.chain(['base', 'polygon', 'skale']),
|
|
74
|
+
PaymentPreference.token(['USDT', 'USDC', 'WBTC']),
|
|
75
|
+
PaymentPreference.cheapest(),
|
|
76
|
+
Logger.console(),
|
|
77
|
+
],
|
|
78
|
+
})
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
`parsePaymentRequired` returns `accepts[]` (x402 v2 challenge options). Clients select from this list.
|
|
82
|
+
|
|
63
83
|
## Features
|
|
64
84
|
|
|
65
85
|
- **Auto 402 Handling**: Automatically intercepts and pays for 402 responses
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { NetworkConfig, CustomToken, PaymentPayloadV2, PaymentRequirementsV2, SettlementResponseV2, NetworkId, TokenId, ArmoryPaymentResult, ValidationError } from '@armory-sh/base';
|
|
2
|
+
import { ClientHook } from '@armory-sh/base/types/hooks';
|
|
2
3
|
import { Web3BaseWalletAccount, Web3BaseWallet } from 'web3-types';
|
|
3
4
|
|
|
4
5
|
type Web3Account = Web3BaseWalletAccount | Web3BaseWallet<Web3BaseWalletAccount>;
|
|
@@ -15,6 +16,7 @@ interface Web3ClientConfig {
|
|
|
15
16
|
domainName?: string;
|
|
16
17
|
/** Override EIP-712 domain version for custom tokens */
|
|
17
18
|
domainVersion?: string;
|
|
19
|
+
hooks?: ClientHook<Web3Account>[];
|
|
18
20
|
}
|
|
19
21
|
interface PaymentSignatureResult {
|
|
20
22
|
signature?: {
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { encodePaymentV2, validatePaymentConfig, isValidationError, resolveNetwork, resolveToken, V2_HEADERS, getNetworkConfig, getNetworkByChainId, networkToCaip2, combineSignatureV2, normalizeBase64Url, decodeBase64ToUtf8, isX402V2PaymentRequired } from '@armory-sh/base';
|
|
2
|
+
import { runOnPaymentRequiredHooks, selectRequirementWithHooks, runBeforeSignPaymentHooks, runAfterPaymentResponseHooks } from '@armory-sh/base/client-hooks-runtime';
|
|
2
3
|
import { Web3 } from 'web3';
|
|
3
4
|
|
|
4
5
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
@@ -300,6 +301,7 @@ var extractNetworkFromRequirements = (requirements) => {
|
|
|
300
301
|
};
|
|
301
302
|
var createX402Client = (config) => {
|
|
302
303
|
const state = createClientState(config);
|
|
304
|
+
const hooks = config.hooks;
|
|
303
305
|
const fetch2 = async (url, init) => {
|
|
304
306
|
let response = await fetch2(url, init);
|
|
305
307
|
if (response.status === 402) {
|
|
@@ -309,7 +311,22 @@ var createX402Client = (config) => {
|
|
|
309
311
|
throw new Error("No supported payment scheme found in requirements");
|
|
310
312
|
}
|
|
311
313
|
const from = getAddress(state.account);
|
|
312
|
-
const
|
|
314
|
+
const paymentRequiredContext = {
|
|
315
|
+
url,
|
|
316
|
+
requestInit: init,
|
|
317
|
+
accepts: parsed.requirements,
|
|
318
|
+
requirements: selectedRequirements,
|
|
319
|
+
selectedRequirement: selectedRequirements,
|
|
320
|
+
serverExtensions: void 0,
|
|
321
|
+
fromAddress: from,
|
|
322
|
+
nonce: `0x${Date.now().toString(16).padStart(64, "0")}`,
|
|
323
|
+
validBefore: Math.floor(Date.now() / 1e3) + selectedRequirements.maxTimeoutSeconds
|
|
324
|
+
};
|
|
325
|
+
await runOnPaymentRequiredHooks(hooks, paymentRequiredContext);
|
|
326
|
+
const req = await selectRequirementWithHooks(
|
|
327
|
+
hooks,
|
|
328
|
+
paymentRequiredContext
|
|
329
|
+
);
|
|
313
330
|
const to = typeof req.payTo === "string" ? req.payTo : "0x0000000000000000000000000000000000000000";
|
|
314
331
|
const network = extractNetworkFromRequirements(req);
|
|
315
332
|
const result = await signPaymentV2(state, network, {
|
|
@@ -320,12 +337,25 @@ var createX402Client = (config) => {
|
|
|
320
337
|
expiry: Math.floor(Date.now() / 1e3) + req.maxTimeoutSeconds,
|
|
321
338
|
accepted: req
|
|
322
339
|
});
|
|
340
|
+
await runBeforeSignPaymentHooks(hooks, {
|
|
341
|
+
payload: result.payload,
|
|
342
|
+
requirements: req,
|
|
343
|
+
wallet: state.account,
|
|
344
|
+
paymentContext: paymentRequiredContext
|
|
345
|
+
});
|
|
323
346
|
const paymentHeaders = new Headers(init?.headers);
|
|
324
347
|
paymentHeaders.set(
|
|
325
348
|
V2_HEADERS.PAYMENT_SIGNATURE,
|
|
326
349
|
encodePaymentV2(result.payload)
|
|
327
350
|
);
|
|
328
351
|
response = await fetch2(url, { ...init, headers: paymentHeaders });
|
|
352
|
+
await runAfterPaymentResponseHooks(hooks, {
|
|
353
|
+
payload: result.payload,
|
|
354
|
+
requirements: req,
|
|
355
|
+
wallet: state.account,
|
|
356
|
+
paymentContext: paymentRequiredContext,
|
|
357
|
+
response
|
|
358
|
+
});
|
|
329
359
|
}
|
|
330
360
|
return response;
|
|
331
361
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@armory-sh/client-web3",
|
|
3
|
-
"version": "0.2.23-alpha.23.
|
|
3
|
+
"version": "0.2.23-alpha.23.79",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Sawyer Cutler <sawyer@dirtroad.dev>",
|
|
6
6
|
"keywords": [
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"directory": "packages/client-web3"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@armory-sh/base": "0.2.27-alpha.23.
|
|
49
|
+
"@armory-sh/base": "0.2.27-alpha.23.79",
|
|
50
50
|
"web3": "4.16.0",
|
|
51
51
|
"web3-types": "1.10.0"
|
|
52
52
|
},
|