@bsvkey/x402-bsv-client 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 +10 -1
- package/index.js +13 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
# @bsvkey/x402-bsv-client
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@bsvkey/x402-bsv-client)
|
|
4
|
+
[](./LICENSE)
|
|
5
|
+
[](https://nodejs.org)
|
|
6
|
+
|
|
3
7
|
Pay an [x402](https://x402.org) `402 Payment Required` **in BSV**, in one line.
|
|
4
8
|
|
|
9
|
+
**Live gateway:** [inference.bsvkey.com/#x402](https://inference.bsvkey.com/#x402) · discovery at [`/v1/x402`](https://inference.bsvkey.com/v1/x402)
|
|
10
|
+
|
|
5
11
|
x402 ships payment builders for EVM (EIP-3009) and Solana. This is the missing
|
|
6
12
|
one for the **`bsv-p2pkh`** scheme — so an AI agent can pay a BSV-settled x402
|
|
7
13
|
endpoint (like `inference.bsvkey.com`) with true sub-cent, per-call micropayments.
|
|
@@ -47,7 +53,10 @@ const xPayment = await buildX402Payment(body402, wif);
|
|
|
47
53
|
|
|
48
54
|
## Notes
|
|
49
55
|
|
|
50
|
-
- **
|
|
56
|
+
- **Key format:** `wif` accepts either a **WIF** (starts `K`/`L`/`5`) or a **12/24-word
|
|
57
|
+
recovery phrase** (derived on the BSV path `m/44'/236'/0'/0/0`, the same one
|
|
58
|
+
inference.bsvkey.com uses) — so a wallet generated on the site works here directly.
|
|
59
|
+
- **Funding:** the key's address must hold enough BSV to cover the quoted amount + a
|
|
51
60
|
small fee. Fund it like any BSV address (the `payTo`/QR flow on inference.bsvkey.com works).
|
|
52
61
|
- **Back-to-back calls are safe:** the client chains each payment off its own change,
|
|
53
62
|
so a loop of paid calls won't double-spend while the mempool catches up.
|
package/index.js
CHANGED
|
@@ -16,7 +16,18 @@
|
|
|
16
16
|
//
|
|
17
17
|
// Peer dependency: @bsv/sdk (v1). Node 18+ (global fetch).
|
|
18
18
|
|
|
19
|
-
import { PrivateKey, P2PKH, Transaction, Utils } from '@bsv/sdk';
|
|
19
|
+
import { PrivateKey, P2PKH, Transaction, Utils, Mnemonic, HD } from '@bsv/sdk';
|
|
20
|
+
|
|
21
|
+
// Accept either a WIF (starts K/L/5) or a 12/24-word BIP-39 recovery phrase
|
|
22
|
+
// (derived on BSV path m/44'/236'/0'/0/0 — the same path inference.bsvkey.com uses),
|
|
23
|
+
// so a key generated on the site works here directly.
|
|
24
|
+
function toPrivateKey(secret) {
|
|
25
|
+
const s = String(secret || '').trim().replace(/\s+/g, ' ');
|
|
26
|
+
if (!s) throw new Error('no key: set BSV_WIF to a WIF or a 12-word recovery phrase');
|
|
27
|
+
if (Mnemonic.isValid(s)) return HD.fromSeed(Mnemonic.fromString(s).toSeed()).derive("m/44'/236'/0'/0/0").privKey;
|
|
28
|
+
try { return PrivateKey.fromWif(s); } catch {}
|
|
29
|
+
throw new Error('BSV_WIF is neither a valid WIF (starts K/L/5) nor a valid recovery phrase — you may have pasted a public address (starts with 1)');
|
|
30
|
+
}
|
|
20
31
|
|
|
21
32
|
const NET = { bsv: 'main', 'bsv-testnet': 'test', 'bsv-dev': 'main' };
|
|
22
33
|
const PREFIX = { main: [0x00], test: [0x6f] };
|
|
@@ -45,7 +56,7 @@ export async function buildX402Payment(body402, wif, { wocBase } = {}) {
|
|
|
45
56
|
const prefix = PREFIX[net];
|
|
46
57
|
const base = (wocBase || 'https://api.whatsonchain.com/v1/bsv') + '/' + net;
|
|
47
58
|
|
|
48
|
-
const priv =
|
|
59
|
+
const priv = toPrivateKey(wif);
|
|
49
60
|
const pub = priv.toPublicKey();
|
|
50
61
|
const addr = pub.toAddress(prefix);
|
|
51
62
|
const sess = session(addr);
|