@xrpay/x402 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +191 -2
- package/dist/browser.js +2 -0
- package/dist/client.cjs +1552 -0
- package/dist/client.d.cts +46 -0
- package/dist/client.d.ts +46 -0
- package/dist/client.js +1529 -0
- package/dist/commerce.cjs +1611 -0
- package/dist/commerce.d.cts +99 -0
- package/dist/commerce.d.ts +99 -0
- package/dist/commerce.js +1588 -0
- package/dist/mcp-tools.d.ts +56 -0
- package/dist/mcp.d.ts +1 -0
- package/dist/mcp.js +1749 -0
- package/dist/protocol.cjs +1447 -0
- package/dist/protocol.d.cts +64 -0
- package/dist/protocol.d.ts +64 -0
- package/dist/protocol.js +1424 -0
- package/dist/server.cjs +1772 -0
- package/dist/server.d.cts +62 -0
- package/dist/server.d.ts +62 -0
- package/dist/server.js +1723 -0
- package/examples/buy-digital-product.mjs +44 -0
- package/examples/paid-api.mjs +25 -0
- package/package.json +101 -4
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// npm install @xrpay/x402 xrpl
|
|
2
|
+
// Set XRPL_WALLET_SEED locally, never in an agent prompt or tool argument.
|
|
3
|
+
// NODE: choose an explicit network, recipient, and spending limits.
|
|
4
|
+
import { readFile, writeFile, rename } from "node:fs/promises";
|
|
5
|
+
import { Wallet, Client } from "xrpl";
|
|
6
|
+
import { XRPayCommerceClient, createRecoveryCredentials } from "@xrpay/x402/commerce";
|
|
7
|
+
|
|
8
|
+
const [listingId, customerEmail, allowedRecipient] = process.argv.slice(2);
|
|
9
|
+
if (!listingId || !customerEmail || !allowedRecipient || !process.env.XRPL_WALLET_SEED) {
|
|
10
|
+
throw new Error("Usage: node buy-digital-product.mjs LISTING_ID EMAIL MERCHANT_ADDRESS (requires XRPL_WALLET_SEED)");
|
|
11
|
+
}
|
|
12
|
+
const network = process.env.XRPL_NETWORK ?? "xrpl:1";
|
|
13
|
+
if (!["xrpl:0", "xrpl:1"].includes(network)) throw new Error("Invalid XRPL_NETWORK");
|
|
14
|
+
const wallet = Wallet.fromSeed(process.env.XRPL_WALLET_SEED);
|
|
15
|
+
const ledger = new Client(network === "xrpl:1" ? "wss://s.altnet.rippletest.net:51233" : "wss://s1.ripple.com");
|
|
16
|
+
const client = new XRPayCommerceClient({ payment: { wallet, ledgerClient: ledger, network, currency: "XRP",
|
|
17
|
+
maxAmount: process.env.XRPAY_MAX_PAYMENT ?? "1", maxTotalAmount: process.env.XRPAY_MAX_TOTAL ?? "1", allowedRecipients: [allowedRecipient] } });
|
|
18
|
+
if (!/^[A-Za-z0-9_-]+$/.test(listingId)) throw new Error("Invalid listing ID");
|
|
19
|
+
const statePath = `xrpay-purchase-${listingId}.json`;
|
|
20
|
+
let state;
|
|
21
|
+
try { state = JSON.parse(await readFile(statePath, "utf8")); }
|
|
22
|
+
catch (error) { if (error.code !== "ENOENT") throw error; state = { credentials: createRecoveryCredentials(), listingId, customerEmail }; }
|
|
23
|
+
if (state.listingId !== listingId || state.customerEmail !== customerEmail) throw new Error("Saved purchase belongs to another buyer/product.");
|
|
24
|
+
async function save() {
|
|
25
|
+
await writeFile(`${statePath}.tmp`, JSON.stringify(state, null, 2), { mode: 0o600 });
|
|
26
|
+
await rename(`${statePath}.tmp`, statePath);
|
|
27
|
+
}
|
|
28
|
+
await save(); // Keep credentials before making the first network request.
|
|
29
|
+
await ledger.connect();
|
|
30
|
+
try {
|
|
31
|
+
if (!state.handle) {
|
|
32
|
+
const result = await client.createPurchase(listingId, { customerEmail, payerAddress: wallet.address }, state.credentials);
|
|
33
|
+
state.handle = result.handle;
|
|
34
|
+
await save();
|
|
35
|
+
}
|
|
36
|
+
let purchase = await client.recover(state.handle);
|
|
37
|
+
if (purchase.status !== "paid") purchase = await client.pay(state.handle, async handle => { state.handle = handle; await save(); });
|
|
38
|
+
if (purchase.status !== "paid") throw new Error("Payment is still confirming. Run this same command later to recover it.");
|
|
39
|
+
const receipt = await client.receipt(state.handle);
|
|
40
|
+
const download = await client.download(state.handle);
|
|
41
|
+
await writeFile(`xrpay-${purchase.id}.bin`, download.bytes, { mode: 0o600 });
|
|
42
|
+
await writeFile(`xrpay-${purchase.id}-receipt.json`, JSON.stringify(receipt, null, 2), { mode: 0o600 });
|
|
43
|
+
console.log(`Order ${purchase.orderId} paid. Download SHA-256: ${download.sha256}`);
|
|
44
|
+
} finally { await ledger.disconnect(); }
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// npm install @xrpay/x402 express
|
|
2
|
+
// Uses XRP Testnet. No XRPay account, API key, or product listing is required.
|
|
3
|
+
import express from "express";
|
|
4
|
+
import { x402Middleware } from "@xrpay/x402";
|
|
5
|
+
|
|
6
|
+
const payTo = process.env.XRPL_RECIPIENT_ADDRESS;
|
|
7
|
+
const challengeSecret = process.env.X402_CHALLENGE_SECRET;
|
|
8
|
+
if (!payTo || !challengeSecret) {
|
|
9
|
+
throw new Error("Set XRPL_RECIPIENT_ADDRESS and X402_CHALLENGE_SECRET (at least 32 characters).");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const app = express();
|
|
13
|
+
app.get("/api/hello", x402Middleware({
|
|
14
|
+
payTo,
|
|
15
|
+
amount: "0.001", // XRP per request, paid directly to your wallet.
|
|
16
|
+
currency: "XRP",
|
|
17
|
+
testnet: true,
|
|
18
|
+
challengeSecret,
|
|
19
|
+
facilitatorUrl: "https://api.xrpay.it/api/x402", // Public; no API key.
|
|
20
|
+
}), (_req, res) => res.json({ message: "Hello from your paid API!" }));
|
|
21
|
+
|
|
22
|
+
const port = Number(process.env.PORT ?? 3000);
|
|
23
|
+
app.listen(port, "127.0.0.1", () => {
|
|
24
|
+
console.log(`Paid API: http://127.0.0.1:${port}/api/hello (0.001 Testnet XRP)`);
|
|
25
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,103 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xrpay/x402",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Charge for your own APIs with XRP or RLUSD\u2014no XRPay account required. Includes optional agent commerce and MCP tools.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/server.cjs",
|
|
7
|
+
"types": "./dist/server.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/server.d.ts",
|
|
12
|
+
"default": "./dist/server.js"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/server.d.cts",
|
|
16
|
+
"default": "./dist/server.cjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"./client": {
|
|
20
|
+
"import": {
|
|
21
|
+
"types": "./dist/client.d.ts",
|
|
22
|
+
"default": "./dist/client.js"
|
|
23
|
+
},
|
|
24
|
+
"require": {
|
|
25
|
+
"types": "./dist/client.d.cts",
|
|
26
|
+
"default": "./dist/client.cjs"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"./protocol": {
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./dist/protocol.d.ts",
|
|
32
|
+
"default": "./dist/protocol.js"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./dist/protocol.d.cts",
|
|
36
|
+
"default": "./dist/protocol.cjs"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"./commerce": {
|
|
40
|
+
"import": {
|
|
41
|
+
"types": "./dist/commerce.d.ts",
|
|
42
|
+
"default": "./dist/commerce.js"
|
|
43
|
+
},
|
|
44
|
+
"require": {
|
|
45
|
+
"types": "./dist/commerce.d.cts",
|
|
46
|
+
"default": "./dist/commerce.cjs"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"dist",
|
|
52
|
+
"README.md",
|
|
53
|
+
"LICENSE",
|
|
54
|
+
"examples"
|
|
55
|
+
],
|
|
56
|
+
"scripts": {
|
|
57
|
+
"build": "node build.mjs",
|
|
58
|
+
"prepack": "npm run build"
|
|
59
|
+
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=20"
|
|
62
|
+
},
|
|
63
|
+
"dependencies": {
|
|
64
|
+
"xrpl": "^4.3.0",
|
|
65
|
+
"bignumber.js": "^9.3.0",
|
|
66
|
+
"@modelcontextprotocol/sdk": "^1.29.0"
|
|
67
|
+
},
|
|
68
|
+
"license": "MIT",
|
|
69
|
+
"homepage": "https://www.xrpay.it/docs/x402",
|
|
70
|
+
"repository": {
|
|
71
|
+
"type": "git",
|
|
72
|
+
"url": "git+https://github.com/gsabbih6/ripple-pay.git",
|
|
73
|
+
"directory": "packages/x402"
|
|
74
|
+
},
|
|
75
|
+
"bugs": {
|
|
76
|
+
"url": "https://github.com/gsabbih6/ripple-pay/issues"
|
|
77
|
+
},
|
|
78
|
+
"keywords": [
|
|
79
|
+
"x402",
|
|
80
|
+
"xrpl",
|
|
81
|
+
"xrp",
|
|
82
|
+
"rlusd",
|
|
83
|
+
"payments",
|
|
84
|
+
"ai-agents",
|
|
85
|
+
"digital-products",
|
|
86
|
+
"mcp"
|
|
87
|
+
],
|
|
88
|
+
"publishConfig": {
|
|
89
|
+
"access": "public",
|
|
90
|
+
"registry": "https://registry.npmjs.org"
|
|
91
|
+
},
|
|
92
|
+
"bin": {
|
|
93
|
+
"xrpay-agent-commerce": "./dist/mcp.js"
|
|
94
|
+
},
|
|
95
|
+
"devDependencies": {
|
|
96
|
+
"esbuild": "^0.25.12",
|
|
97
|
+
"typescript": "^5.2.2",
|
|
98
|
+
"@types/node": "^22.2.0"
|
|
99
|
+
},
|
|
100
|
+
"mcpName": "io.github.gsabbih6/xrpay-agent-commerce",
|
|
101
|
+
"unpkg": "./dist/browser.js",
|
|
102
|
+
"jsdelivr": "./dist/browser.js"
|
|
103
|
+
}
|