moltspay 0.4.4 → 0.5.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/dist/cli.js +152 -0
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +152 -0
- package/dist/cli.mjs.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -30983,6 +30983,86 @@ var init_cdp = __esm({
|
|
|
30983
30983
|
}
|
|
30984
30984
|
});
|
|
30985
30985
|
|
|
30986
|
+
// src/x402/client.ts
|
|
30987
|
+
var client_exports = {};
|
|
30988
|
+
__export(client_exports, {
|
|
30989
|
+
createX402Client: () => createX402Client,
|
|
30990
|
+
isX402Available: () => isX402Available,
|
|
30991
|
+
x402Fetch: () => x402Fetch
|
|
30992
|
+
});
|
|
30993
|
+
function isX402Available() {
|
|
30994
|
+
try {
|
|
30995
|
+
require.resolve("@x402/fetch");
|
|
30996
|
+
require.resolve("@x402/evm");
|
|
30997
|
+
return true;
|
|
30998
|
+
} catch {
|
|
30999
|
+
return false;
|
|
31000
|
+
}
|
|
31001
|
+
}
|
|
31002
|
+
async function createLocalX402Client(config) {
|
|
31003
|
+
const { AgentWallet: AgentWallet2 } = await Promise.resolve().then(() => (init_AgentWallet(), AgentWallet_exports));
|
|
31004
|
+
const { ethers: ethers11 } = await import("ethers");
|
|
31005
|
+
const wallet = new AgentWallet2({
|
|
31006
|
+
chain: config.chain,
|
|
31007
|
+
storageDir: config.storageDir
|
|
31008
|
+
});
|
|
31009
|
+
const fs4 = await import("fs");
|
|
31010
|
+
const path4 = await import("path");
|
|
31011
|
+
const storageDir = config.storageDir || path4.join(process.env.HOME || ".", ".moltspay");
|
|
31012
|
+
const walletPath = path4.join(storageDir, "wallet.json");
|
|
31013
|
+
const walletData = JSON.parse(fs4.readFileSync(walletPath, "utf-8"));
|
|
31014
|
+
const privateKey = config.privateKey || walletData.privateKey;
|
|
31015
|
+
const { privateKeyToAccount: privateKeyToAccount2 } = await Promise.resolve().then(() => (init_accounts(), accounts_exports));
|
|
31016
|
+
const signer = privateKeyToAccount2(privateKey);
|
|
31017
|
+
const { x402Client, wrapFetchWithPayment } = await import("@x402/fetch");
|
|
31018
|
+
const { registerExactEvmScheme } = await import("@x402/evm/exact/client");
|
|
31019
|
+
const client = new x402Client();
|
|
31020
|
+
registerExactEvmScheme(client, { signer });
|
|
31021
|
+
const fetchWithPayment = wrapFetchWithPayment(fetch, client);
|
|
31022
|
+
return {
|
|
31023
|
+
fetch: fetchWithPayment,
|
|
31024
|
+
address: wallet.address,
|
|
31025
|
+
chain: config.chain || "base"
|
|
31026
|
+
};
|
|
31027
|
+
}
|
|
31028
|
+
async function createCDPX402Client(config) {
|
|
31029
|
+
const { CDPWallet: CDPWallet2 } = await Promise.resolve().then(() => (init_cdp(), cdp_exports));
|
|
31030
|
+
const wallet = new CDPWallet2({
|
|
31031
|
+
chain: config.chain,
|
|
31032
|
+
storageDir: config.storageDir
|
|
31033
|
+
});
|
|
31034
|
+
const signer = await wallet.getViemAccount();
|
|
31035
|
+
const { x402Client, wrapFetchWithPayment } = await import("@x402/fetch");
|
|
31036
|
+
const { registerExactEvmScheme } = await import("@x402/evm/exact/client");
|
|
31037
|
+
const client = new x402Client();
|
|
31038
|
+
registerExactEvmScheme(client, { signer });
|
|
31039
|
+
const fetchWithPayment = wrapFetchWithPayment(fetch, client);
|
|
31040
|
+
return {
|
|
31041
|
+
fetch: fetchWithPayment,
|
|
31042
|
+
address: wallet.address,
|
|
31043
|
+
chain: config.chain || "base"
|
|
31044
|
+
};
|
|
31045
|
+
}
|
|
31046
|
+
async function createX402Client(config = {}) {
|
|
31047
|
+
if (!isX402Available()) {
|
|
31048
|
+
throw new Error("x402 packages not installed. Run: npm install @x402/fetch @x402/evm");
|
|
31049
|
+
}
|
|
31050
|
+
if (config.useCDP) {
|
|
31051
|
+
return createCDPX402Client(config);
|
|
31052
|
+
} else {
|
|
31053
|
+
return createLocalX402Client(config);
|
|
31054
|
+
}
|
|
31055
|
+
}
|
|
31056
|
+
async function x402Fetch(input, init, config) {
|
|
31057
|
+
const client = await createX402Client(config);
|
|
31058
|
+
return client.fetch(input, init);
|
|
31059
|
+
}
|
|
31060
|
+
var init_client = __esm({
|
|
31061
|
+
"src/x402/client.ts"() {
|
|
31062
|
+
"use strict";
|
|
31063
|
+
}
|
|
31064
|
+
});
|
|
31065
|
+
|
|
30986
31066
|
// src/cli.ts
|
|
30987
31067
|
var import_commander = require("commander");
|
|
30988
31068
|
|
|
@@ -31742,6 +31822,7 @@ init_chains();
|
|
|
31742
31822
|
init_chains();
|
|
31743
31823
|
|
|
31744
31824
|
// src/x402/index.ts
|
|
31825
|
+
init_client();
|
|
31745
31826
|
var import_ethers10 = require("ethers");
|
|
31746
31827
|
init_chains();
|
|
31747
31828
|
|
|
@@ -31969,6 +32050,77 @@ program.command("spend").description("Spend USDC from Owner wallet (requires per
|
|
|
31969
32050
|
console.log(JSON.stringify(result, null, 2));
|
|
31970
32051
|
process.exit(result.success ? 0 : 1);
|
|
31971
32052
|
});
|
|
32053
|
+
program.command("x402").description("Make HTTP request with automatic x402 payment").argument("<url>", "URL to request").option("-X, --method <method>", "HTTP method", "GET").option("-d, --data <json>", "Request body (JSON)").option("-H, --header <header...>", "Additional headers (key:value)").option("-c, --chain <chain>", "Chain name", "base").option("--cdp", "Use CDP wallet").option("-o, --output <file>", "Save response to file").option("-v, --verbose", "Show payment details").action(async (url, options) => {
|
|
32054
|
+
const { createX402Client: createX402Client2, isX402Available: isX402Available2 } = await Promise.resolve().then(() => (init_client(), client_exports));
|
|
32055
|
+
if (!isX402Available2()) {
|
|
32056
|
+
console.error("\u274C x402 packages not installed.");
|
|
32057
|
+
console.error("");
|
|
32058
|
+
console.error("Install them with:");
|
|
32059
|
+
console.error(" npm install @x402/fetch @x402/evm viem");
|
|
32060
|
+
process.exit(1);
|
|
32061
|
+
}
|
|
32062
|
+
try {
|
|
32063
|
+
if (options.verbose) {
|
|
32064
|
+
console.error(`\u{1F504} Initializing x402 client (chain: ${options.chain})...`);
|
|
32065
|
+
}
|
|
32066
|
+
const client = await createX402Client2({
|
|
32067
|
+
chain: options.chain,
|
|
32068
|
+
useCDP: options.cdp
|
|
32069
|
+
});
|
|
32070
|
+
if (options.verbose) {
|
|
32071
|
+
console.error(` Wallet: ${client.address}`);
|
|
32072
|
+
console.error(` Chain: ${client.chain}`);
|
|
32073
|
+
console.error("");
|
|
32074
|
+
}
|
|
32075
|
+
const headers = {
|
|
32076
|
+
"Content-Type": "application/json"
|
|
32077
|
+
};
|
|
32078
|
+
if (options.header) {
|
|
32079
|
+
for (const h of options.header) {
|
|
32080
|
+
const [key, ...valueParts] = h.split(":");
|
|
32081
|
+
headers[key.trim()] = valueParts.join(":").trim();
|
|
32082
|
+
}
|
|
32083
|
+
}
|
|
32084
|
+
const init = {
|
|
32085
|
+
method: options.method.toUpperCase(),
|
|
32086
|
+
headers
|
|
32087
|
+
};
|
|
32088
|
+
if (options.data && ["POST", "PUT", "PATCH"].includes(init.method)) {
|
|
32089
|
+
init.body = options.data;
|
|
32090
|
+
}
|
|
32091
|
+
if (options.verbose) {
|
|
32092
|
+
console.error(`\u{1F4E4} ${init.method} ${url}`);
|
|
32093
|
+
if (options.data) {
|
|
32094
|
+
console.error(` Body: ${options.data.substring(0, 100)}${options.data.length > 100 ? "..." : ""}`);
|
|
32095
|
+
}
|
|
32096
|
+
console.error("");
|
|
32097
|
+
}
|
|
32098
|
+
const response = await client.fetch(url, init);
|
|
32099
|
+
if (options.verbose) {
|
|
32100
|
+
console.error(`\u{1F4E5} Response: ${response.status} ${response.statusText}`);
|
|
32101
|
+
console.error("");
|
|
32102
|
+
}
|
|
32103
|
+
const contentType = response.headers.get("content-type") || "";
|
|
32104
|
+
let body;
|
|
32105
|
+
if (contentType.includes("application/json")) {
|
|
32106
|
+
const json = await response.json();
|
|
32107
|
+
body = JSON.stringify(json, null, 2);
|
|
32108
|
+
} else {
|
|
32109
|
+
body = await response.text();
|
|
32110
|
+
}
|
|
32111
|
+
if (options.output) {
|
|
32112
|
+
const fs4 = await import("fs");
|
|
32113
|
+
fs4.writeFileSync(options.output, body);
|
|
32114
|
+
console.error(`\u2705 Saved to ${options.output}`);
|
|
32115
|
+
} else {
|
|
32116
|
+
console.log(body);
|
|
32117
|
+
}
|
|
32118
|
+
process.exit(response.ok ? 0 : 1);
|
|
32119
|
+
} catch (error) {
|
|
32120
|
+
console.error("\u274C Error:", error.message);
|
|
32121
|
+
process.exit(1);
|
|
32122
|
+
}
|
|
32123
|
+
});
|
|
31972
32124
|
program.command("status").description("Show agent wallet status").option("-c, --chain <chain>", "Chain name", "base").option("-o, --owner <address>", "Check allowance from specific owner").action(async (options) => {
|
|
31973
32125
|
const { AgentWallet: AgentWallet2 } = await Promise.resolve().then(() => (init_AgentWallet(), AgentWallet_exports));
|
|
31974
32126
|
const wallet = new AgentWallet2({ chain: options.chain });
|