x402check 0.1.0 → 0.1.1
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 +135 -0
- package/package.json +12 -13
package/README.md
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
# x402check
|
|
2
|
+
|
|
3
|
+
Validate [x402](https://www.x402.org/) payment configurations. Works in Node, browsers, and edge runtimes — zero dependencies.
|
|
4
|
+
|
|
5
|
+
**[x402check.com](https://www.x402check.com)** — try it in the browser
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm i x402check
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { validate } from 'x402check'
|
|
17
|
+
|
|
18
|
+
const result = validate({
|
|
19
|
+
x402Version: 2,
|
|
20
|
+
accepts: [{
|
|
21
|
+
scheme: 'exact',
|
|
22
|
+
network: 'base',
|
|
23
|
+
payTo: '0x1234567890abcdef1234567890abcdef12345678',
|
|
24
|
+
asset: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
|
|
25
|
+
amount: '10000',
|
|
26
|
+
maxTimeoutSeconds: 300
|
|
27
|
+
}]
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
result.valid // true | false
|
|
31
|
+
result.errors // ValidationIssue[]
|
|
32
|
+
result.warnings // ValidationIssue[]
|
|
33
|
+
result.normalized // canonical v2 config
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## API
|
|
37
|
+
|
|
38
|
+
### `validate(input, options?)`
|
|
39
|
+
|
|
40
|
+
Validates a config object or JSON string. Returns errors, warnings, and a normalized v2 config.
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
validate(configOrJson)
|
|
44
|
+
validate(configOrJson, { strict: true }) // promotes warnings to errors
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**Returns:** `ValidationResult`
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
{
|
|
51
|
+
valid: boolean
|
|
52
|
+
version: 'v2' | 'v1' | 'unknown'
|
|
53
|
+
errors: ValidationIssue[]
|
|
54
|
+
warnings: ValidationIssue[]
|
|
55
|
+
normalized: NormalizedConfig | null
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Each issue includes a machine-readable `code`, a `field` path, a human-readable `message`, and an optional `fix` suggestion.
|
|
60
|
+
|
|
61
|
+
### `extractConfig(response)`
|
|
62
|
+
|
|
63
|
+
Extracts an x402 config from an HTTP 402 response. Checks the JSON body first, then falls back to the `PAYMENT-REQUIRED` header (base64 or raw JSON).
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
const res = await fetch(url)
|
|
67
|
+
const { config, source, error } = extractConfig({
|
|
68
|
+
body: await res.json(),
|
|
69
|
+
headers: res.headers
|
|
70
|
+
})
|
|
71
|
+
// source: 'body' | 'header' | null
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### `detect(input)`
|
|
75
|
+
|
|
76
|
+
Returns the config format: `'v2'`, `'v1'`, or `'unknown'`.
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
detect({ x402Version: 2, accepts: [...] }) // 'v2'
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### `normalize(input)`
|
|
83
|
+
|
|
84
|
+
Converts any supported config to canonical v2 shape. Returns `null` if the format is unrecognized.
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
const v2Config = normalize(v1Config)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Address validation
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
import { validateAddress, validateEvmAddress, validateSolanaAddress } from 'x402check'
|
|
94
|
+
|
|
95
|
+
validateAddress(addr, 'eip155:8453', 'payTo') // dispatches by network
|
|
96
|
+
validateEvmAddress(addr, 'payTo') // EIP-55 checksum verification
|
|
97
|
+
validateSolanaAddress(addr, 'payTo') // base58, 32-byte decode check
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Network & asset registry
|
|
101
|
+
|
|
102
|
+
```js
|
|
103
|
+
import {
|
|
104
|
+
isKnownNetwork, getNetworkInfo, getCanonicalNetwork,
|
|
105
|
+
isKnownAsset, getAssetInfo, isValidCaip2
|
|
106
|
+
} from 'x402check'
|
|
107
|
+
|
|
108
|
+
isValidCaip2('eip155:8453') // true
|
|
109
|
+
getCanonicalNetwork('base') // 'eip155:8453'
|
|
110
|
+
getNetworkInfo('eip155:8453') // { name: 'Base', type: 'evm', testnet: false }
|
|
111
|
+
isKnownAsset('eip155:8453', '0x833…') // true
|
|
112
|
+
getAssetInfo('eip155:8453', '0x833…') // { symbol: 'USDC', name: 'USD Coin', decimals: 6 }
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Supported formats
|
|
116
|
+
|
|
117
|
+
| Format | `x402Version` | Status |
|
|
118
|
+
|--------|---------------|--------|
|
|
119
|
+
| v2 | `2` | Recommended |
|
|
120
|
+
| v1 | `1` | Supported, auto-normalized to v2 |
|
|
121
|
+
|
|
122
|
+
## Validation checks
|
|
123
|
+
|
|
124
|
+
- Required fields (`scheme`, `network`, `amount`, `asset`, `payTo`)
|
|
125
|
+
- Amount is a numeric string > 0
|
|
126
|
+
- Network is valid [CAIP-2](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md)
|
|
127
|
+
- EVM addresses: `0x`-prefixed, 40 hex chars, EIP-55 checksum
|
|
128
|
+
- Solana addresses: base58, decodes to 32 bytes
|
|
129
|
+
- Known network and asset registry warnings
|
|
130
|
+
- `maxTimeoutSeconds` is a positive integer (if present)
|
|
131
|
+
- Resource URL format (if present)
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "x402check",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Validate x402 payment configurations",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -22,14 +22,6 @@
|
|
|
22
22
|
"dist/"
|
|
23
23
|
],
|
|
24
24
|
"sideEffects": false,
|
|
25
|
-
"scripts": {
|
|
26
|
-
"build": "tsdown",
|
|
27
|
-
"test": "vitest run",
|
|
28
|
-
"test:watch": "vitest",
|
|
29
|
-
"lint": "tsc --noEmit",
|
|
30
|
-
"typecheck": "tsc --noEmit",
|
|
31
|
-
"prepublishOnly": "pnpm test && pnpm build && publint"
|
|
32
|
-
},
|
|
33
25
|
"keywords": [
|
|
34
26
|
"x402",
|
|
35
27
|
"payment",
|
|
@@ -38,11 +30,11 @@
|
|
|
38
30
|
"license": "MIT",
|
|
39
31
|
"repository": {
|
|
40
32
|
"type": "git",
|
|
41
|
-
"url": "git+https://github.com/
|
|
33
|
+
"url": "git+https://github.com/rawgroundbeef/x402check.com.git"
|
|
42
34
|
},
|
|
43
|
-
"homepage": "https://
|
|
35
|
+
"homepage": "https://www.x402check.com",
|
|
44
36
|
"bugs": {
|
|
45
|
-
"url": "https://github.com/
|
|
37
|
+
"url": "https://github.com/rawgroundbeef/x402check.com/issues"
|
|
46
38
|
},
|
|
47
39
|
"devDependencies": {
|
|
48
40
|
"@noble/hashes": "^2.0.1",
|
|
@@ -51,5 +43,12 @@
|
|
|
51
43
|
"tsdown": "^0.20.1",
|
|
52
44
|
"typescript": "^5.9.3",
|
|
53
45
|
"vitest": "^4.0.18"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsdown",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"test:watch": "vitest",
|
|
51
|
+
"lint": "tsc --noEmit",
|
|
52
|
+
"typecheck": "tsc --noEmit"
|
|
54
53
|
}
|
|
55
|
-
}
|
|
54
|
+
}
|