@xrpay/x402 0.0.0-stage → 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.
@@ -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(); }
package/package.json CHANGED
@@ -1,6 +1,103 @@
1
1
  {
2
2
  "name": "@xrpay/x402",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "XRPL x402 payments and agent commerce: discover, buy, and receive digital products from XRPay merchants",
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-commerce",
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
+ }