bsv-x402 0.1.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/LICENSE +86 -0
- package/README.md +39 -0
- package/dist/index.cjs +56 -0
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +28 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Open BSV License Version 5 – granted by BSV Association, Grafenauweg 6, 6300
|
|
2
|
+
Zug, Switzerland (CHE-427.008.338) ("Licensor"), to you as a user (henceforth
|
|
3
|
+
"You", "User" or "Licensee").
|
|
4
|
+
|
|
5
|
+
For the purposes of this license, the definitions below have the following
|
|
6
|
+
meanings:
|
|
7
|
+
|
|
8
|
+
"Bitcoin Protocol" means the protocol implementation, cryptographic rules,
|
|
9
|
+
network protocols, and consensus mechanisms in the Bitcoin White Paper as
|
|
10
|
+
described here https://protocol.bsvblockchain.org.
|
|
11
|
+
|
|
12
|
+
"Bitcoin White Paper" means the paper entitled 'Bitcoin: A Peer-to-Peer
|
|
13
|
+
Electronic Cash System' published by 'Satoshi Nakamoto' in October 2008.
|
|
14
|
+
|
|
15
|
+
"BSV Blockchains" means:
|
|
16
|
+
(a) the Bitcoin blockchain containing block height #556767 with the hash
|
|
17
|
+
"000000000000000001d956714215d96ffc00e0afda4cd0a96c96f8d802b1662b" and
|
|
18
|
+
that contains the longest honest persistent chain of blocks which has been
|
|
19
|
+
produced in a manner which is consistent with the rules set forth in the
|
|
20
|
+
Network Access Rules; and
|
|
21
|
+
(b) the test blockchains that contain the longest honest persistent chains of
|
|
22
|
+
blocks which has been produced in a manner which is consistent with the
|
|
23
|
+
rules set forth in the Network Access Rules.
|
|
24
|
+
|
|
25
|
+
"Network Access Rules" or "Rules" means the set of rules regulating the
|
|
26
|
+
relationship between BSV Association and the nodes on BSV based on the Bitcoin
|
|
27
|
+
Protocol rules and those set out in the Bitcoin White Paper, and available here
|
|
28
|
+
https://bsvblockchain.org/network-access-rules.
|
|
29
|
+
|
|
30
|
+
"Software" means the software the subject of this licence, including any/all
|
|
31
|
+
intellectual property rights therein and associated documentation files.
|
|
32
|
+
|
|
33
|
+
BSV Association grants permission, free of charge and on a non-exclusive and
|
|
34
|
+
revocable basis, to any person obtaining a copy of the Software to deal in the
|
|
35
|
+
Software without restriction, including without limitation the rights to use,
|
|
36
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
37
|
+
Software, and to permit persons to whom the Software is furnished to do so,
|
|
38
|
+
subject to and conditioned upon the following conditions:
|
|
39
|
+
|
|
40
|
+
1 - The text "© BSV Association," and this license shall be included in all
|
|
41
|
+
copies or substantial portions of the Software.
|
|
42
|
+
2 - The Software, and any software that is derived from the Software or parts
|
|
43
|
+
thereof, must only be used on the BSV Blockchains.
|
|
44
|
+
|
|
45
|
+
For the avoidance of doubt, this license is granted subject to and conditioned
|
|
46
|
+
upon your compliance with these terms only. In the event of non-compliance, the
|
|
47
|
+
license shall extinguish and you can be enjoined from violating BSV's
|
|
48
|
+
intellectual property rights (incl. damages and similar related claims).
|
|
49
|
+
|
|
50
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
51
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES REGARDING ENTITLEMENT,
|
|
52
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
|
53
|
+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS THEREOF BE LIABLE FOR ANY CLAIM,
|
|
54
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
55
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
56
|
+
DEALINGS IN THE SOFTWARE.
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
Version 0.1.1 of the Bitcoin SV software, and prior versions of software upon
|
|
60
|
+
which it was based, were licensed under the MIT License, which is included below.
|
|
61
|
+
|
|
62
|
+
The MIT License (MIT)
|
|
63
|
+
|
|
64
|
+
Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
65
|
+
Copyright (c) 2009-2015 Bitcoin Developers
|
|
66
|
+
Copyright (c) 2009-2017 The Bitcoin Core developers
|
|
67
|
+
Copyright (c) 2017 The Bitcoin ABC developers
|
|
68
|
+
Copyright (c) 2018 Bitcoin Association for BSV
|
|
69
|
+
Copyright (c) 2023 BSV Association
|
|
70
|
+
|
|
71
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
72
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
73
|
+
the Software without restriction, including without limitation the rights to
|
|
74
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
75
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
76
|
+
subject to the following conditions:
|
|
77
|
+
|
|
78
|
+
The above copyright notice and this permission notice shall be included in all
|
|
79
|
+
copies or substantial portions of the Software.
|
|
80
|
+
|
|
81
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
82
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
83
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
84
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
85
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
86
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# @x402/client
|
|
2
|
+
|
|
3
|
+
A JavaScript client library for the [x402 payment protocol](https://x402.merkleworks.io/). Wraps `fetch()` to handle HTTP 402 payment flows transparently.
|
|
4
|
+
|
|
5
|
+
## How It Works
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
import { x402Fetch } from '@x402/client'
|
|
9
|
+
|
|
10
|
+
const response = await x402Fetch('https://api.example.com/paid-endpoint')
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
When the server responds with `402 Payment Required`, the library:
|
|
14
|
+
|
|
15
|
+
1. Parses the `X402-Challenge` header
|
|
16
|
+
2. Constructs a payment transaction via the browser wallet ([BRC-100](https://github.com/bitcoin-sv/BRCs/blob/master/wallet/0100.md) / `window.CWI`)
|
|
17
|
+
3. Broadcasts the transaction to the BSV network
|
|
18
|
+
4. Retries the original request with an `X402-Proof` header
|
|
19
|
+
|
|
20
|
+
The app developer just uses `x402Fetch` in place of `fetch`. The browser wallet handles key management and signing.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @x402/client
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Development
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install # Install dependencies
|
|
32
|
+
npm run build # Compile TypeScript → dist/
|
|
33
|
+
npm test # Run tests
|
|
34
|
+
npm run typecheck # Type-check without emitting
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Status
|
|
38
|
+
|
|
39
|
+
Early development. The fetch wrapper and challenge parsing are in place; the BRC-100 wallet integration (`constructProof`) is not yet implemented.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
parseChallenge: () => parseChallenge,
|
|
24
|
+
x402Fetch: () => x402Fetch
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/challenge.ts
|
|
29
|
+
function parseChallenge(header) {
|
|
30
|
+
return JSON.parse(header);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/x402-fetch.ts
|
|
34
|
+
async function x402Fetch(input, init) {
|
|
35
|
+
const response = await fetch(input, init);
|
|
36
|
+
if (response.status !== 402) {
|
|
37
|
+
return response;
|
|
38
|
+
}
|
|
39
|
+
const challengeHeader = response.headers.get("X402-Challenge");
|
|
40
|
+
if (!challengeHeader) {
|
|
41
|
+
return response;
|
|
42
|
+
}
|
|
43
|
+
const challenge = parseChallenge(challengeHeader);
|
|
44
|
+
const proof = await constructProof(challenge);
|
|
45
|
+
const headers = new Headers(init?.headers);
|
|
46
|
+
headers.set("X402-Proof", JSON.stringify(proof));
|
|
47
|
+
return fetch(input, { ...init, headers });
|
|
48
|
+
}
|
|
49
|
+
async function constructProof(challenge) {
|
|
50
|
+
throw new Error("Not implemented \u2014 requires BRC-100 wallet (window.CWI)");
|
|
51
|
+
}
|
|
52
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
53
|
+
0 && (module.exports = {
|
|
54
|
+
parseChallenge,
|
|
55
|
+
x402Fetch
|
|
56
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
declare function x402Fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
|
|
2
|
+
|
|
3
|
+
interface Challenge {
|
|
4
|
+
nonce: string;
|
|
5
|
+
payee: string;
|
|
6
|
+
amount: number;
|
|
7
|
+
network: string;
|
|
8
|
+
}
|
|
9
|
+
interface Proof {
|
|
10
|
+
txid: string;
|
|
11
|
+
rawTx: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare function parseChallenge(header: string): Challenge;
|
|
15
|
+
|
|
16
|
+
export { type Challenge, type Proof, parseChallenge, x402Fetch };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
declare function x402Fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
|
|
2
|
+
|
|
3
|
+
interface Challenge {
|
|
4
|
+
nonce: string;
|
|
5
|
+
payee: string;
|
|
6
|
+
amount: number;
|
|
7
|
+
network: string;
|
|
8
|
+
}
|
|
9
|
+
interface Proof {
|
|
10
|
+
txid: string;
|
|
11
|
+
rawTx: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare function parseChallenge(header: string): Challenge;
|
|
15
|
+
|
|
16
|
+
export { type Challenge, type Proof, parseChallenge, x402Fetch };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// src/challenge.ts
|
|
2
|
+
function parseChallenge(header) {
|
|
3
|
+
return JSON.parse(header);
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// src/x402-fetch.ts
|
|
7
|
+
async function x402Fetch(input, init) {
|
|
8
|
+
const response = await fetch(input, init);
|
|
9
|
+
if (response.status !== 402) {
|
|
10
|
+
return response;
|
|
11
|
+
}
|
|
12
|
+
const challengeHeader = response.headers.get("X402-Challenge");
|
|
13
|
+
if (!challengeHeader) {
|
|
14
|
+
return response;
|
|
15
|
+
}
|
|
16
|
+
const challenge = parseChallenge(challengeHeader);
|
|
17
|
+
const proof = await constructProof(challenge);
|
|
18
|
+
const headers = new Headers(init?.headers);
|
|
19
|
+
headers.set("X402-Proof", JSON.stringify(proof));
|
|
20
|
+
return fetch(input, { ...init, headers });
|
|
21
|
+
}
|
|
22
|
+
async function constructProof(challenge) {
|
|
23
|
+
throw new Error("Not implemented \u2014 requires BRC-100 wallet (window.CWI)");
|
|
24
|
+
}
|
|
25
|
+
export {
|
|
26
|
+
parseChallenge,
|
|
27
|
+
x402Fetch
|
|
28
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bsv-x402",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "x402 payment protocol client — a fetch() wrapper that handles 402 payment flows transparently",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.cjs",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsup",
|
|
21
|
+
"test": "vitest",
|
|
22
|
+
"typecheck": "tsc --noEmit"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"tsup": "^8.0.0",
|
|
26
|
+
"typescript": "^5.0.0",
|
|
27
|
+
"vitest": "^3.0.0"
|
|
28
|
+
}
|
|
29
|
+
}
|