crowdcode-mcp 0.1.2 → 0.2.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/chunk-SKCQBRTB.js +134 -0
- package/dist/chunk-SKCQBRTB.js.map +1 -0
- package/dist/cli.js +2 -2
- package/dist/server-GI4ZADCU.js +620 -0
- package/dist/server-GI4ZADCU.js.map +1 -0
- package/package.json +2 -1
- package/dist/chunk-ZOKWC7AU.js +0 -18
- package/dist/chunk-ZOKWC7AU.js.map +0 -1
- package/dist/server-6AVZDCL4.js +0 -391
- package/dist/server-6AVZDCL4.js.map +0 -1
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// src/wallet.ts
|
|
2
|
+
import { chmod, mkdir, readFile, writeFile } from "fs/promises";
|
|
3
|
+
import { homedir } from "os";
|
|
4
|
+
import { join } from "path";
|
|
5
|
+
import {
|
|
6
|
+
generatePrivateKey,
|
|
7
|
+
privateKeyToAccount
|
|
8
|
+
} from "viem/accounts";
|
|
9
|
+
import { getAddress } from "viem/utils";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
var PRIVATE_KEY_RE = /^0x[a-fA-F0-9]{64}$/;
|
|
12
|
+
var storedWalletSchema = z.object({
|
|
13
|
+
privateKey: z.string().regex(PRIVATE_KEY_RE),
|
|
14
|
+
address: z.string().regex(/^0x[a-fA-F0-9]{40}$/),
|
|
15
|
+
createdAt: z.string()
|
|
16
|
+
});
|
|
17
|
+
function defaultWalletDir() {
|
|
18
|
+
return join(homedir(), ".agentcash");
|
|
19
|
+
}
|
|
20
|
+
var memo = null;
|
|
21
|
+
async function loadWallet(opts = {}) {
|
|
22
|
+
const isDefault = opts.env === void 0 && opts.walletDir === void 0;
|
|
23
|
+
if (isDefault && memo) return memo;
|
|
24
|
+
const result = await load(
|
|
25
|
+
opts.env ?? process.env,
|
|
26
|
+
opts.walletDir ?? defaultWalletDir(),
|
|
27
|
+
opts.autoCreate ?? false
|
|
28
|
+
);
|
|
29
|
+
if (isDefault && result.source !== "none") {
|
|
30
|
+
memo = { ...result, created: void 0 };
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
async function load(env, dir, autoCreate) {
|
|
35
|
+
const envKey = env.X402_PRIVATE_KEY?.trim();
|
|
36
|
+
if (envKey) {
|
|
37
|
+
if (!PRIVATE_KEY_RE.test(envKey)) {
|
|
38
|
+
return none(
|
|
39
|
+
"X402_PRIVATE_KEY is set but is not a 0x-prefixed 32-byte hex key"
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
const account2 = privateKeyToAccount(envKey);
|
|
43
|
+
return { source: "env", account: account2, address: account2.address };
|
|
44
|
+
}
|
|
45
|
+
const file = join(dir, "wallet.json");
|
|
46
|
+
let raw = null;
|
|
47
|
+
try {
|
|
48
|
+
raw = await readFile(file, "utf8");
|
|
49
|
+
} catch (err) {
|
|
50
|
+
if (err.code !== "ENOENT") {
|
|
51
|
+
return none(`wallet file is unreadable: ${err.message}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (raw !== null) {
|
|
55
|
+
return parseStored(raw, file);
|
|
56
|
+
}
|
|
57
|
+
if (!autoCreate) {
|
|
58
|
+
return none();
|
|
59
|
+
}
|
|
60
|
+
const privateKey = generatePrivateKey();
|
|
61
|
+
const account = privateKeyToAccount(privateKey);
|
|
62
|
+
const stored = {
|
|
63
|
+
privateKey,
|
|
64
|
+
address: account.address,
|
|
65
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
66
|
+
};
|
|
67
|
+
try {
|
|
68
|
+
await mkdir(dir, { recursive: true, mode: 448 });
|
|
69
|
+
await writeFile(file, JSON.stringify(stored, null, 2), {
|
|
70
|
+
mode: 384,
|
|
71
|
+
flag: "wx"
|
|
72
|
+
});
|
|
73
|
+
await chmod(file, 384);
|
|
74
|
+
} catch (err) {
|
|
75
|
+
if (err.code === "EEXIST") {
|
|
76
|
+
try {
|
|
77
|
+
return parseStored(await readFile(file, "utf8"), file);
|
|
78
|
+
} catch (readErr) {
|
|
79
|
+
return none(`wallet file is unreadable: ${readErr.message}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return none(`could not create wallet file: ${err.message}`);
|
|
83
|
+
}
|
|
84
|
+
return { source: "agentcash", account, address: account.address, created: true };
|
|
85
|
+
}
|
|
86
|
+
function parseStored(raw, file) {
|
|
87
|
+
let parsedJson;
|
|
88
|
+
try {
|
|
89
|
+
parsedJson = JSON.parse(raw);
|
|
90
|
+
} catch {
|
|
91
|
+
return none(`wallet file is not valid JSON: ${file} \u2014 fix or remove it`);
|
|
92
|
+
}
|
|
93
|
+
const parsed = storedWalletSchema.safeParse(parsedJson);
|
|
94
|
+
if (!parsed.success) {
|
|
95
|
+
return none(
|
|
96
|
+
`wallet file does not match the agentcash wallet shape: ${file} \u2014 fix or remove it`
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
const account = privateKeyToAccount(parsed.data.privateKey);
|
|
100
|
+
if (getAddress(account.address) !== getAddress(parsed.data.address)) {
|
|
101
|
+
return none(
|
|
102
|
+
`wallet file address does not match its private key: ${file} \u2014 fix or remove it`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
return { source: "agentcash", account, address: getAddress(parsed.data.address) };
|
|
106
|
+
}
|
|
107
|
+
function none(error) {
|
|
108
|
+
return { source: "none", account: null, address: null, error };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// src/config.ts
|
|
112
|
+
import { homedir as homedir2 } from "os";
|
|
113
|
+
import { join as join2 } from "path";
|
|
114
|
+
var TRUTHY = /* @__PURE__ */ new Set(["1", "true", "yes"]);
|
|
115
|
+
function getConfig(env = process.env) {
|
|
116
|
+
return {
|
|
117
|
+
backendUrl: env.CROWDCODE_BACKEND_URL ?? "https://crowdcode-backend.onrender.com/mcp",
|
|
118
|
+
cacheDir: env.CROWDCODE_CACHE_DIR ?? join2(homedir2(), ".cache", "crowdcode-mcp"),
|
|
119
|
+
disableModel: TRUTHY.has((env.CROWDCODE_DISABLE_MODEL ?? "").toLowerCase()),
|
|
120
|
+
// Generous default: the free-tier Render backend cold-starts slowly.
|
|
121
|
+
upstreamTimeoutMs: Number(env.CROWDCODE_UPSTREAM_TIMEOUT_MS ?? 6e4),
|
|
122
|
+
// Signing wallet shared with agentcash (~/.agentcash/wallet.json).
|
|
123
|
+
walletDir: env.CROWDCODE_WALLET_DIR ?? defaultWalletDir(),
|
|
124
|
+
walletAutoCreate: !TRUTHY.has(
|
|
125
|
+
(env.CROWDCODE_DISABLE_WALLET_CREATE ?? "").toLowerCase()
|
|
126
|
+
)
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export {
|
|
131
|
+
loadWallet,
|
|
132
|
+
getConfig
|
|
133
|
+
};
|
|
134
|
+
//# sourceMappingURL=chunk-SKCQBRTB.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/wallet.ts","../src/config.ts"],"sourcesContent":["/**\n * Local signing wallet, shared with agentcash.\n *\n * Reads (and lazily creates) the same wallet file agentcash uses —\n * ~/.agentcash/wallet.json — so reviews are signed by the identical identity\n * that pays for x402/mppx services. Semantics deliberately mirror agentcash\n * v0.17.0 exactly:\n * 1. X402_PRIVATE_KEY env var wins entirely — the file is never read or\n * written when it is set (a malformed value is an error, not a\n * fallthrough).\n * 2. An existing file is zod-validated and its address cross-checked\n * against the key; an unreadable or invalid file is NEVER overwritten.\n * 3. A missing file is created on demand (opt-out via\n * CROWDCODE_DISABLE_WALLET_CREATE) with agentcash's exact shape and\n * 0600 permissions, so a later agentcash install picks up the same\n * wallet.\n */\n\nimport { chmod, mkdir, readFile, writeFile } from \"node:fs/promises\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\nimport {\n generatePrivateKey,\n privateKeyToAccount,\n type PrivateKeyAccount,\n} from \"viem/accounts\";\nimport { getAddress } from \"viem/utils\";\nimport { z } from \"zod\";\n\nexport type WalletSource = \"env\" | \"agentcash\" | \"none\";\n\nexport interface LoadedWallet {\n source: WalletSource;\n account: PrivateKeyAccount | null;\n address: string | null;\n error?: string;\n created?: boolean;\n}\n\nexport interface LoadWalletOptions {\n env?: NodeJS.ProcessEnv;\n walletDir?: string;\n autoCreate?: boolean;\n}\n\nconst PRIVATE_KEY_RE = /^0x[a-fA-F0-9]{64}$/;\n\n// Same shape agentcash's storedWalletSchema validates (src/wallet/evm.ts).\nconst storedWalletSchema = z.object({\n privateKey: z.string().regex(PRIVATE_KEY_RE),\n address: z.string().regex(/^0x[a-fA-F0-9]{40}$/),\n createdAt: z.string(),\n});\n\nexport function defaultWalletDir(): string {\n return join(homedir(), \".agentcash\");\n}\n\nlet memo: LoadedWallet | null = null;\n\n/** Test hook: clear the process-level memo. */\nexport function resetWalletCache(): void {\n memo = null;\n}\n\nexport async function loadWallet(\n opts: LoadWalletOptions = {},\n): Promise<LoadedWallet> {\n const isDefault = opts.env === undefined && opts.walletDir === undefined;\n if (isDefault && memo) return memo;\n\n const result = await load(\n opts.env ?? process.env,\n opts.walletDir ?? defaultWalletDir(),\n opts.autoCreate ?? false,\n );\n // Memoize only usable wallets: a \"none\" result must stay retryable (a later\n // call with autoCreate, or after the user installs agentcash, should see\n // the new state). `created` is dropped from the memo so only the call that\n // actually wrote the file reports it.\n if (isDefault && result.source !== \"none\") {\n memo = { ...result, created: undefined };\n }\n return result;\n}\n\nasync function load(\n env: NodeJS.ProcessEnv,\n dir: string,\n autoCreate: boolean,\n): Promise<LoadedWallet> {\n const envKey = env.X402_PRIVATE_KEY?.trim();\n if (envKey) {\n if (!PRIVATE_KEY_RE.test(envKey)) {\n return none(\n \"X402_PRIVATE_KEY is set but is not a 0x-prefixed 32-byte hex key\",\n );\n }\n const account = privateKeyToAccount(envKey as `0x${string}`);\n return { source: \"env\", account, address: account.address };\n }\n\n const file = join(dir, \"wallet.json\");\n let raw: string | null = null;\n try {\n raw = await readFile(file, \"utf8\");\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code !== \"ENOENT\") {\n return none(`wallet file is unreadable: ${(err as Error).message}`);\n }\n }\n\n if (raw !== null) {\n return parseStored(raw, file);\n }\n\n if (!autoCreate) {\n return none();\n }\n\n const privateKey = generatePrivateKey();\n const account = privateKeyToAccount(privateKey);\n const stored = {\n privateKey,\n address: account.address,\n createdAt: new Date().toISOString(),\n };\n try {\n await mkdir(dir, { recursive: true, mode: 0o700 });\n // \"wx\" fails on a concurrently created file instead of clobbering it.\n await writeFile(file, JSON.stringify(stored, null, 2), {\n mode: 0o600,\n flag: \"wx\",\n });\n await chmod(file, 0o600);\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === \"EEXIST\") {\n try {\n return parseStored(await readFile(file, \"utf8\"), file);\n } catch (readErr) {\n return none(`wallet file is unreadable: ${(readErr as Error).message}`);\n }\n }\n return none(`could not create wallet file: ${(err as Error).message}`);\n }\n return { source: \"agentcash\", account, address: account.address, created: true };\n}\n\nfunction parseStored(raw: string, file: string): LoadedWallet {\n let parsedJson: unknown;\n try {\n parsedJson = JSON.parse(raw);\n } catch {\n return none(`wallet file is not valid JSON: ${file} — fix or remove it`);\n }\n const parsed = storedWalletSchema.safeParse(parsedJson);\n if (!parsed.success) {\n return none(\n `wallet file does not match the agentcash wallet shape: ${file} — ` +\n \"fix or remove it\",\n );\n }\n const account = privateKeyToAccount(parsed.data.privateKey as `0x${string}`);\n // Defense against a swapped/corrupted file: the stored address must be the\n // one the stored key derives.\n if (getAddress(account.address) !== getAddress(parsed.data.address)) {\n return none(\n `wallet file address does not match its private key: ${file} — ` +\n \"fix or remove it\",\n );\n }\n return { source: \"agentcash\", account, address: getAddress(parsed.data.address) };\n}\n\nfunction none(error?: string): LoadedWallet {\n return { source: \"none\", account: null, address: null, error };\n}\n","import { homedir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { defaultWalletDir } from \"./wallet.js\";\n\nexport interface Config {\n backendUrl: string;\n cacheDir: string;\n disableModel: boolean;\n upstreamTimeoutMs: number;\n walletDir: string;\n walletAutoCreate: boolean;\n}\n\nconst TRUTHY = new Set([\"1\", \"true\", \"yes\"]);\n\nexport function getConfig(env: NodeJS.ProcessEnv = process.env): Config {\n return {\n backendUrl:\n env.CROWDCODE_BACKEND_URL ?? \"https://crowdcode-backend.onrender.com/mcp\",\n cacheDir: env.CROWDCODE_CACHE_DIR ?? join(homedir(), \".cache\", \"crowdcode-mcp\"),\n disableModel: TRUTHY.has((env.CROWDCODE_DISABLE_MODEL ?? \"\").toLowerCase()),\n // Generous default: the free-tier Render backend cold-starts slowly.\n upstreamTimeoutMs: Number(env.CROWDCODE_UPSTREAM_TIMEOUT_MS ?? 60_000),\n // Signing wallet shared with agentcash (~/.agentcash/wallet.json).\n walletDir: env.CROWDCODE_WALLET_DIR ?? defaultWalletDir(),\n walletAutoCreate: !TRUTHY.has(\n (env.CROWDCODE_DISABLE_WALLET_CREATE ?? \"\").toLowerCase(),\n ),\n };\n}\n"],"mappings":";AAkBA,SAAS,OAAO,OAAO,UAAU,iBAAiB;AAClD,SAAS,eAAe;AACxB,SAAS,YAAY;AACrB;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAS,kBAAkB;AAC3B,SAAS,SAAS;AAkBlB,IAAM,iBAAiB;AAGvB,IAAM,qBAAqB,EAAE,OAAO;AAAA,EAClC,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc;AAAA,EAC3C,SAAS,EAAE,OAAO,EAAE,MAAM,qBAAqB;AAAA,EAC/C,WAAW,EAAE,OAAO;AACtB,CAAC;AAEM,SAAS,mBAA2B;AACzC,SAAO,KAAK,QAAQ,GAAG,YAAY;AACrC;AAEA,IAAI,OAA4B;AAOhC,eAAsB,WACpB,OAA0B,CAAC,GACJ;AACvB,QAAM,YAAY,KAAK,QAAQ,UAAa,KAAK,cAAc;AAC/D,MAAI,aAAa,KAAM,QAAO;AAE9B,QAAM,SAAS,MAAM;AAAA,IACnB,KAAK,OAAO,QAAQ;AAAA,I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AAI,2BAA2B,IAAI,YAAY,CAAC;AAAA;AAAA,IAE1E,mBAAmB,OAAO,IAAI,iCAAiC,GAAM;AAAA;AAAA,IAErE,WAAW,IAAI,wBAAwB,iBAAiB;AAAA,IACxD,kBAAkB,CAAC,OAAO;AAAA,OACvB,IAAI,mCAAmC,IAAI,YAAY;AAAA,IAC1D;AAAA,EACF;AACF;","names":["account","homedir","join","join","homedir"]}
|
package/dist/cli.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
getConfig
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-SKCQBRTB.js";
|
|
5
5
|
|
|
6
6
|
// src/cli.ts
|
|
7
7
|
import { rm, stat } from "fs/promises";
|
|
@@ -65,7 +65,7 @@ async function main() {
|
|
|
65
65
|
case void 0:
|
|
66
66
|
case "serve": {
|
|
67
67
|
routeStdoutToStderr();
|
|
68
|
-
const { startServer } = await import("./server-
|
|
68
|
+
const { startServer } = await import("./server-GI4ZADCU.js");
|
|
69
69
|
await startServer();
|
|
70
70
|
break;
|
|
71
71
|
}
|