apow-cli 0.11.19 → 0.12.3

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.
@@ -50,7 +50,8 @@ exports.getGrindUrl = getGrindUrl;
50
50
  exports.grindNonceHttp = grindNonceHttp;
51
51
  const config_1 = require("./config");
52
52
  const DEFAULT_GRIND_URL = "https://grind.apow.io/grind";
53
- const GRIND_HTTP_TIMEOUT_MS = 60_000;
53
+ // The service allows 300 seconds including cold startup. Leave transport margin.
54
+ const GRIND_HTTP_TIMEOUT_MS = 330_000;
54
55
  // Lazy singleton — created on first grind, reused across calls
55
56
  let _fetchWithPayment = null;
56
57
  function resetPaymentFetch() {
@@ -73,13 +74,11 @@ function extractErrorDetail(body) {
73
74
  }
74
75
  return body.trim();
75
76
  }
76
- async function getPaymentFetch(privateKey) {
77
+ async function getPaymentFetch(signer) {
77
78
  if (_fetchWithPayment)
78
79
  return _fetchWithPayment;
79
- const { privateKeyToAccount } = await Promise.resolve().then(() => __importStar(require("viem/accounts")));
80
80
  const { x402Client, wrapFetchWithPayment } = await Promise.resolve().then(() => __importStar(require("@x402/fetch")));
81
81
  const { registerExactEvmScheme } = await Promise.resolve().then(() => __importStar(require("@x402/evm/exact/client")));
82
- const signer = privateKeyToAccount(privateKey);
83
82
  const client = new x402Client();
84
83
  registerExactEvmScheme(client, { signer });
85
84
  _fetchWithPayment = wrapFetchWithPayment(fetch, client);
@@ -91,7 +90,7 @@ function isHttpGrinderConfigured() {
91
90
  function getGrindUrl() {
92
91
  return config_1.config.grindUrl ?? DEFAULT_GRIND_URL;
93
92
  }
94
- async function grindNonceHttp(challengeNumber, target, minerAddress, grindUrl, privateKey, signal) {
93
+ async function grindNonceHttp(challengeNumber, target, minerAddress, grindUrl, signer, signal) {
95
94
  const start = process.hrtime();
96
95
  const timeoutSignal = AbortSignal.timeout(GRIND_HTTP_TIMEOUT_MS);
97
96
  const requestSignal = signal ? AbortSignal.any([signal, timeoutSignal]) : timeoutSignal;
@@ -107,7 +106,7 @@ async function grindNonceHttp(challengeNumber, target, minerAddress, grindUrl, p
107
106
  };
108
107
  let response = null;
109
108
  for (let attempt = 1; attempt <= 2; attempt += 1) {
110
- const paidFetch = await getPaymentFetch(privateKey);
109
+ const paidFetch = await getPaymentFetch(signer);
111
110
  try {
112
111
  response = await paidFetch(grindUrl, requestInit);
113
112
  }
@@ -117,13 +116,11 @@ async function grindNonceHttp(challengeNumber, target, minerAddress, grindUrl, p
117
116
  }
118
117
  if (timeoutSignal.aborted) {
119
118
  resetPaymentFetch();
120
- throw new Error(`Remote GPU grind timed out (${GRIND_HTTP_TIMEOUT_MS / 1000}s)`);
119
+ throw new Error(`Remote GPU grind timed out (${GRIND_HTTP_TIMEOUT_MS / 1000}s); payment outcome is unknown`);
121
120
  }
122
121
  resetPaymentFetch();
123
- if (attempt === 1) {
124
- continue;
125
- }
126
- throw new Error(`Remote GPU grind request failed: ${err instanceof Error ? err.message : String(err)}`);
122
+ // A lost response can follow a settled payment. Do not replay an unknown paid request.
123
+ throw new Error(`Remote GPU grind request failed; payment outcome is unknown: ${err instanceof Error ? err.message : String(err)}`);
127
124
  }
128
125
  if (response.ok) {
129
126
  break;
@@ -141,7 +138,7 @@ async function grindNonceHttp(challengeNumber, target, minerAddress, grindUrl, p
141
138
  }
142
139
  const combinedError = `${reason} ${bodyDetail}`.trim();
143
140
  const stalePaymentSession = /No matching payment requirements|facilitator|payment session|authorization/i.test(combinedError);
144
- if (attempt === 1 && (stalePaymentSession || response.status >= 500)) {
141
+ if (attempt === 1 && response.status === 402 && stalePaymentSession) {
145
142
  resetPaymentFetch();
146
143
  continue;
147
144
  }
@@ -151,7 +148,7 @@ async function grindNonceHttp(challengeNumber, target, minerAddress, grindUrl, p
151
148
  throw new Error("x402 GPU payment failed: insufficient USDC balance");
152
149
  }
153
150
  else if (reason.includes("simulation_failed")) {
154
- throw new Error(`x402 GPU payment failed: EVM simulation failed (USDC approval issue?) [${reason}]`);
151
+ throw new Error(`x402 GPU payment failed: EVM simulation failed; check balance, policy, and service settlement RPC [${reason}]`);
155
152
  }
156
153
  else if (attempt === 1) {
157
154
  continue;
@@ -161,8 +158,10 @@ async function grindNonceHttp(challengeNumber, target, minerAddress, grindUrl, p
161
158
  }
162
159
  }
163
160
  if (response.status === 504) {
164
- throw new Error(`Remote GPU grind timed out (${GRIND_HTTP_TIMEOUT_MS / 1000}s)`);
161
+ throw new Error("Remote GPU service timed out; payment outcome is unknown");
165
162
  }
163
+ if (response.status >= 500)
164
+ throw new Error(`GrindProxy HTTP ${response.status}; payment outcome is unknown`);
166
165
  throw new Error(`GrindProxy HTTP ${response.status}: ${(bodyDetail || body).slice(0, 200)}`);
167
166
  }
168
167
  if (!response || !response.ok) {
@@ -32,6 +32,7 @@ const node_fs_1 = require("node:fs");
32
32
  const node_path_1 = require("node:path");
33
33
  const node_os_1 = __importDefault(require("node:os"));
34
34
  const config_1 = require("./config");
35
+ const secure_env_1 = require("./secure-env");
35
36
  /**
36
37
  * Detect available native grinder binaries.
37
38
  * Checks: explicit env var > ./gpu/ > ./ > ~/.apow/
@@ -156,7 +157,7 @@ async function grindNonceNative(challengeNumber, target, minerAddress, info, sig
156
157
  "-p", config_1.config.vastPort,
157
158
  `root@${config_1.config.vastIp}`,
158
159
  "pkill -9 grinder-cuda 2>/dev/null",
159
- ]);
160
+ ], { env: (0, secure_env_1.childEnv)() });
160
161
  setTimeout(() => { try {
161
162
  kp.kill();
162
163
  }
@@ -198,7 +199,7 @@ async function grindNonceNative(challengeNumber, target, minerAddress, info, sig
198
199
  if (info.gpu) {
199
200
  totalGrinders++;
200
201
  // Set CWD to binary's directory so Metal grinder finds keccak.metal shader
201
- const proc = (0, node_child_process_1.spawn)(info.gpu, [challengeNumber, minerAddress, targetHex], { cwd: (0, node_path_1.dirname)(info.gpu) });
202
+ const proc = (0, node_child_process_1.spawn)(info.gpu, [challengeNumber, minerAddress, targetHex], { cwd: (0, node_path_1.dirname)(info.gpu), env: (0, secure_env_1.childEnv)() });
202
203
  processes.push(proc);
203
204
  let stdout = "";
204
205
  proc.stdout.on("data", (d) => { stdout += d.toString(); });
@@ -227,7 +228,7 @@ async function grindNonceNative(challengeNumber, target, minerAddress, info, sig
227
228
  if (info.cuda) {
228
229
  totalGrinders++;
229
230
  // CUDA arg order: <challenge> <target> <address>
230
- const proc = (0, node_child_process_1.spawn)(info.cuda, [challengeNumber, targetHex, minerAddress]);
231
+ const proc = (0, node_child_process_1.spawn)(info.cuda, [challengeNumber, targetHex, minerAddress], { env: (0, secure_env_1.childEnv)() });
231
232
  processes.push(proc);
232
233
  let buf = "";
233
234
  proc.stdout.on("data", (d) => {
@@ -265,7 +266,7 @@ async function grindNonceNative(challengeNumber, target, minerAddress, info, sig
265
266
  const proc = (0, node_child_process_1.spawn)(info.cpu, [
266
267
  challengeNumber, minerAddress, targetHex,
267
268
  String(config_1.config.cpuGrinderThreads),
268
- ]);
269
+ ], { env: (0, secure_env_1.childEnv)() });
269
270
  processes.push(proc);
270
271
  let stdout = "";
271
272
  proc.stdout.on("data", (d) => { stdout += d.toString(); });
@@ -301,7 +302,7 @@ async function grindNonceNative(challengeNumber, target, minerAddress, info, sig
301
302
  "-p", config_1.config.vastPort,
302
303
  `root@${config_1.config.vastIp}`,
303
304
  `${config_1.config.remoteGrinderPath} ${challengeNumber} ${targetHex} ${minerAddress}`,
304
- ]);
305
+ ], { env: (0, secure_env_1.childEnv)() });
305
306
  processes.push(proc);
306
307
  let buf = "";
307
308
  proc.stdout.on("data", (d) => {