@raycashxyz/mpp-pay 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.
Files changed (3) hide show
  1. package/README.md +44 -0
  2. package/dist/cli.js +103 -0
  3. package/package.json +28 -0
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # @raycashxyz/mpp-pay
2
+
3
+ CLI for making paid API requests via the [Machine Payments Protocol](https://mpp.dev) with [Raycash](https://raycash.xyz) private payments.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install -g @raycashxyz/mpp-pay
9
+ ```
10
+
11
+ Or use directly with `npx`:
12
+
13
+ ```bash
14
+ npx @raycashxyz/mpp-pay <url>
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```bash
20
+ # Create a signing wallet (first time only — no ETH needed)
21
+ mpp-pay --create-wallet
22
+
23
+ # Make a paid request
24
+ mpp-pay "https://your-api.com/endpoint"
25
+
26
+ # POST with body
27
+ mpp-pay "https://your-api.com/endpoint" --method POST --body '{"input":"hello"}'
28
+
29
+ # Check wallet
30
+ mpp-pay --wallet
31
+ ```
32
+
33
+ ## How It Works
34
+
35
+ 1. You request a paid endpoint → gets `402 Payment Required`
36
+ 2. `mpp-pay` reads the payment challenge, creates a channel (first time), signs a voucher
37
+ 3. Retries with payment → returns the response
38
+ 4. If the channel needs funding, shows the address to send tokens to
39
+
40
+ The wallet is a local signing key (`~/.mpp-wallet.json`). It never needs gas or on-chain transactions — it only signs payment vouchers off-chain.
41
+
42
+ ## License
43
+
44
+ MIT
package/dist/cli.js ADDED
@@ -0,0 +1,103 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/cli.ts
4
+ import { existsSync, readFileSync, writeFileSync } from "fs";
5
+ import { resolve } from "path";
6
+ import { homedir } from "os";
7
+ import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
8
+ import { Mppx } from "mppx/client";
9
+ import { raycash } from "@raycashxyz/mpp/client";
10
+ var WALLET_PATH = resolve(homedir(), ".mpp-wallet.json");
11
+ function loadWallet() {
12
+ if (!existsSync(WALLET_PATH)) return null;
13
+ return JSON.parse(readFileSync(WALLET_PATH, "utf-8"));
14
+ }
15
+ function createWallet() {
16
+ const existing = loadWallet();
17
+ if (existing) return existing;
18
+ const wallet2 = {
19
+ privateKey: generatePrivateKey(),
20
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
21
+ };
22
+ writeFileSync(WALLET_PATH, JSON.stringify(wallet2, null, 2));
23
+ return wallet2;
24
+ }
25
+ var args = process.argv.slice(2);
26
+ if (args.includes("--create-wallet")) {
27
+ const wallet2 = createWallet();
28
+ const account2 = privateKeyToAccount(wallet2.privateKey);
29
+ console.log(`[mpp-pay] Wallet created`);
30
+ console.log(`[mpp-pay] Signer: ${account2.address}`);
31
+ console.log(`[mpp-pay] File: ${WALLET_PATH}`);
32
+ console.log(`[mpp-pay]`);
33
+ console.log(`[mpp-pay] This is a signing-only key. It does not hold tokens.`);
34
+ console.log(`[mpp-pay] When you make a paid request, the channel address to fund will be shown.`);
35
+ process.exit(0);
36
+ }
37
+ if (args.includes("--wallet")) {
38
+ const wallet2 = loadWallet();
39
+ if (!wallet2) {
40
+ console.log("[mpp-pay] No wallet found. Run: mpp-pay --create-wallet");
41
+ process.exit(0);
42
+ }
43
+ const account2 = privateKeyToAccount(wallet2.privateKey);
44
+ console.log(`[mpp-pay] Signer: ${account2.address}`);
45
+ console.log(`[mpp-pay] File: ${WALLET_PATH}`);
46
+ process.exit(0);
47
+ }
48
+ var url = args.find((a) => !a.startsWith("--"));
49
+ if (!url) {
50
+ console.log("Usage:");
51
+ console.log(" mpp-pay <url> Make a paid request");
52
+ console.log(" mpp-pay <url> --method POST --body '{}' POST with body");
53
+ console.log(" mpp-pay --create-wallet Create signing wallet");
54
+ console.log(" mpp-pay --wallet Show wallet info");
55
+ process.exit(0);
56
+ }
57
+ var wallet = loadWallet();
58
+ if (!wallet) {
59
+ console.error("[mpp-pay] No wallet found. Create one first:");
60
+ console.error("[mpp-pay] mpp-pay --create-wallet");
61
+ process.exit(1);
62
+ }
63
+ var account = privateKeyToAccount(wallet.privateKey);
64
+ var methodIdx = args.indexOf("--method");
65
+ var method = methodIdx !== -1 ? args[methodIdx + 1] ?? "GET" : "GET";
66
+ var bodyIdx = args.indexOf("--body");
67
+ var body = bodyIdx !== -1 ? args[bodyIdx + 1] : void 0;
68
+ console.log(`[mpp-pay] Signer: ${account.address}`);
69
+ console.log(`[mpp-pay] Request: ${method} ${url}`);
70
+ var mppx = Mppx.create({
71
+ polyfill: false,
72
+ methods: [
73
+ raycash({
74
+ account,
75
+ serviceOrigin: new URL(url).origin,
76
+ onFundChannel: async (params) => {
77
+ console.log(`[mpp-pay] Payment channel created: ${params.channelAddress}`);
78
+ console.log(`[mpp-pay]`);
79
+ console.log(`[mpp-pay] Fund this channel to start paying:`);
80
+ console.log(`[mpp-pay] Send at least ${params.minDeposit} smallest units to:`);
81
+ console.log(`[mpp-pay] Address: ${params.channelAddress}`);
82
+ console.log(`[mpp-pay] Token: ${params.underlying}`);
83
+ console.log(`[mpp-pay] Chain ID: ${params.chainId}`);
84
+ console.log(`[mpp-pay]`);
85
+ console.log(`[mpp-pay] After funding, re-run the same command.`);
86
+ process.exit(1);
87
+ }
88
+ })
89
+ ]
90
+ });
91
+ var init = { method };
92
+ if (body) {
93
+ init.body = body;
94
+ init.headers = { "Content-Type": "application/json" };
95
+ }
96
+ var response = await mppx.fetch(url, init);
97
+ console.log(`[mpp-pay] Status: ${response.status}`);
98
+ var contentType = response.headers.get("content-type") ?? "";
99
+ if (contentType.includes("json")) {
100
+ console.log(JSON.stringify(await response.json(), null, 2));
101
+ } else {
102
+ console.log(await response.text());
103
+ }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@raycashxyz/mpp-pay",
3
+ "version": "0.1.0",
4
+ "description": "CLI for making paid API requests via the Machine Payments Protocol",
5
+ "type": "module",
6
+ "bin": {
7
+ "mpp-pay": "./dist/cli.js"
8
+ },
9
+ "files": ["dist"],
10
+ "license": "MIT",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/raycashxyz/mpp-pay"
14
+ },
15
+ "scripts": {
16
+ "build": "tsup",
17
+ "prepublishOnly": "npm run build"
18
+ },
19
+ "dependencies": {
20
+ "@raycashxyz/mpp": "^0.1.1",
21
+ "mppx": "^0.4.7",
22
+ "viem": "^2.0.0"
23
+ },
24
+ "devDependencies": {
25
+ "tsup": "^8.5.1",
26
+ "typescript": "^5.8.3"
27
+ }
28
+ }