apow-cli 0.9.1 → 0.9.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.
package/dist/preflight.js CHANGED
@@ -41,6 +41,7 @@ const viem_1 = require("viem");
41
41
  const chains_1 = require("viem/chains");
42
42
  const AgentCoin_json_1 = __importDefault(require("./abi/AgentCoin.json"));
43
43
  const config_1 = require("./config");
44
+ const grinder_http_1 = require("./grinder-http");
44
45
  const wallet_1 = require("./wallet");
45
46
  const ui = __importStar(require("./ui"));
46
47
  const agentCoinAbi = AgentCoin_json_1.default;
@@ -74,7 +75,7 @@ async function runPreflight(level) {
74
75
  results.push({
75
76
  label: "No RPC configured",
76
77
  passed: false,
77
- fix: "Run `apow setup` to configure RPC, or set RPC_URL or USE_X402=true in .env",
78
+ fix: "Set USE_X402=true in .env (auto-pay with USDC), or set RPC_URL to a Base endpoint, or run `apow setup`",
78
79
  });
79
80
  }
80
81
  // Check x402: USDC balance BEFORE RPC check (can't use x402 without USDC)
@@ -212,7 +213,7 @@ async function runPreflight(level) {
212
213
  results.push({
213
214
  label: `LLM API key not set for ${config_1.config.llmProvider}`,
214
215
  passed: false,
215
- fix: "Set LLM_API_KEY (or OPENAI_API_KEY / ANTHROPIC_API_KEY / GEMINI_API_KEY) in .env, or run `apow setup`",
216
+ fix: "Set LLM_API_KEY in .env, or switch to LLM_PROVIDER=clawrouter (zero credentials, pays with USDC)",
216
217
  });
217
218
  }
218
219
  }
@@ -244,6 +245,30 @@ async function runPreflight(level) {
244
245
  catch {
245
246
  // Skip if contract addresses not configured (already caught above)
246
247
  }
248
+ // Check: GrindProxy reachability (mining only, non-blocking)
249
+ if (level === "mining" && (0, grinder_http_1.isHttpGrinderConfigured)()) {
250
+ const grindUrl = (0, grinder_http_1.getGrindUrl)();
251
+ const healthUrl = grindUrl.replace(/\/grind$/, "/health");
252
+ try {
253
+ const resp = await fetch(healthUrl, { signal: AbortSignal.timeout(5000) });
254
+ if (resp.ok) {
255
+ const host = new URL(grindUrl).hostname;
256
+ results.push({ label: `GrindProxy: ${host} ($0.01/grind)`, passed: true });
257
+ }
258
+ else {
259
+ results.push({
260
+ label: "GrindProxy unreachable (will use local grinders only)",
261
+ passed: true, // warn but don't fail — local grinders still work
262
+ });
263
+ }
264
+ }
265
+ catch {
266
+ results.push({
267
+ label: "GrindProxy unreachable (will use local grinders only)",
268
+ passed: true,
269
+ });
270
+ }
271
+ }
247
272
  const failures = results.filter((r) => !r.passed);
248
273
  if (failures.length > 0) {
249
274
  console.log("");
@@ -0,0 +1,39 @@
1
+ # APoW GPU Nonce Grinder — Build Targets
2
+ #
3
+ # Metal (macOS Apple Silicon): make metal
4
+ # CUDA (Vast.ai RTX 4090): make cuda
5
+ # CPU (reference/fallback): make cpu
6
+
7
+ .PHONY: all metal cuda cpu clean
8
+
9
+ # ── Metal (macOS only) ──
10
+ metal: grinder-gpu
11
+
12
+ grinder-gpu: grinder.m keccak.metal
13
+ clang -O2 -framework Metal -framework Foundation grinder.m -o grinder-gpu
14
+
15
+ # ── CUDA (Linux, requires nvcc + CUDA toolkit) ──
16
+ # sm_89 = Ada Lovelace (RTX 4090). Change for other GPUs:
17
+ # sm_86 = Ampere (RTX 3090), sm_90 = Hopper (H100)
18
+ CUDA_ARCH ?= sm_89
19
+
20
+ cuda: grinder-cuda
21
+
22
+ grinder-cuda: grinder-cuda.cu
23
+ nvcc grinder-cuda.cu -o grinder-cuda -std=c++17 -O3 -arch=$(CUDA_ARCH)
24
+
25
+ # ── CPU (portable reference) ──
26
+ cpu: grinder-cpu
27
+
28
+ grinder-cpu: grinder-cpu.c
29
+ clang -O2 -pthread grinder-cpu.c -o grinder-cpu
30
+
31
+ # ── Test harness (Metal, verifies GPU vs CPU keccak) ──
32
+ test: test_keccak
33
+
34
+ test_keccak: test_keccak.m
35
+ clang -O2 -framework Metal -framework Foundation test_keccak.m -o test_keccak
36
+
37
+ # ── Cleanup ──
38
+ clean:
39
+ rm -f grinder-gpu grinder-cuda grinder-cpu test_keccak *.metallib
@@ -0,0 +1,241 @@
1
+ /*
2
+ * APoW PoW Nonce Grinder — Multi-threaded keccak256
3
+ * Usage: ./grinder <challenge_hex> <address_hex> <target_hex> [threads]
4
+ * Output: <nonce_decimal> <attempts>
5
+ */
6
+
7
+ #include <stdio.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+ #include <stdint.h>
11
+ #include <unistd.h>
12
+ #include <pthread.h>
13
+ #include <time.h>
14
+
15
+ /* ─── Keccak-256 (compact implementation) ──────────────────── */
16
+
17
+ #define KECCAK_ROUNDS 24
18
+
19
+ static const uint64_t RC[24] = {
20
+ 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
21
+ 0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
22
+ 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
23
+ 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
24
+ 0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
25
+ 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
26
+ 0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
27
+ 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
28
+ };
29
+
30
+ static const int ROTC[24] = {
31
+ 1,3,6,10,15,21,28,36,45,55,2,14,27,41,56,8,25,43,62,18,39,61,20,44
32
+ };
33
+
34
+ static const int PILN[24] = {
35
+ 10,7,11,17,18,3,5,16,8,21,24,4,15,23,19,13,12,2,20,14,22,9,6,1
36
+ };
37
+
38
+ static inline uint64_t rotl64(uint64_t x, int n) {
39
+ return (x << n) | (x >> (64 - n));
40
+ }
41
+
42
+ static void keccak_f1600(uint64_t st[25]) {
43
+ uint64_t bc[5];
44
+ for (int r = 0; r < KECCAK_ROUNDS; r++) {
45
+ // Theta
46
+ for (int i = 0; i < 5; i++)
47
+ bc[i] = st[i] ^ st[i+5] ^ st[i+10] ^ st[i+15] ^ st[i+20];
48
+ for (int i = 0; i < 5; i++) {
49
+ uint64_t t = bc[(i+4)%5] ^ rotl64(bc[(i+1)%5], 1);
50
+ for (int j = 0; j < 25; j += 5) st[j+i] ^= t;
51
+ }
52
+ // Rho + Pi
53
+ uint64_t t = st[1];
54
+ for (int i = 0; i < 24; i++) {
55
+ int j = PILN[i];
56
+ uint64_t tmp = st[j];
57
+ st[j] = rotl64(t, ROTC[i]);
58
+ t = tmp;
59
+ }
60
+ // Chi
61
+ for (int j = 0; j < 25; j += 5) {
62
+ uint64_t tmp[5];
63
+ for (int i = 0; i < 5; i++) tmp[i] = st[j+i];
64
+ for (int i = 0; i < 5; i++)
65
+ st[j+i] = tmp[i] ^ ((~tmp[(i+1)%5]) & tmp[(i+2)%5]);
66
+ }
67
+ // Iota
68
+ st[0] ^= RC[r];
69
+ }
70
+ }
71
+
72
+ static void keccak256(const uint8_t *in, size_t inlen, uint8_t *out) {
73
+ uint64_t st[25] = {0};
74
+ const int rate = 136; // (1600 - 256*2) / 8
75
+
76
+ // Absorb
77
+ while (inlen >= (size_t)rate) {
78
+ for (int i = 0; i < rate/8; i++)
79
+ st[i] ^= ((const uint64_t*)in)[i];
80
+ keccak_f1600(st);
81
+ in += rate;
82
+ inlen -= rate;
83
+ }
84
+
85
+ // Final block (pad with aligned copy)
86
+ uint8_t tmp[136] = {0};
87
+ memcpy(tmp, in, inlen);
88
+ tmp[inlen] = 0x01; // Keccak padding (NOT SHA3 which uses 0x06)
89
+ tmp[rate - 1] |= 0x80;
90
+
91
+ for (int i = 0; i < rate/8; i++)
92
+ st[i] ^= ((uint64_t*)tmp)[i];
93
+ keccak_f1600(st);
94
+
95
+ // Squeeze (32 bytes)
96
+ memcpy(out, st, 32);
97
+ }
98
+
99
+ /* ─── Hex parsing ──────────────────────────────────────────── */
100
+
101
+ static int hex2byte(char c) {
102
+ if (c >= '0' && c <= '9') return c - '0';
103
+ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
104
+ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
105
+ return -1;
106
+ }
107
+
108
+ static int parse_hex(const char *hex, uint8_t *out, int len) {
109
+ if (hex[0] == '0' && hex[1] == 'x') hex += 2;
110
+ for (int i = 0; i < len; i++) {
111
+ int hi = hex2byte(hex[i*2]);
112
+ int lo = hex2byte(hex[i*2+1]);
113
+ if (hi < 0 || lo < 0) return -1;
114
+ out[i] = (hi << 4) | lo;
115
+ }
116
+ return 0;
117
+ }
118
+
119
+ /* ─── Worker thread ────────────────────────────────────────── */
120
+
121
+ typedef struct {
122
+ uint8_t base[84]; // challenge(32) + address(20) + nonce_template(32)
123
+ uint8_t target[32];
124
+ uint64_t start;
125
+ uint64_t range;
126
+ uint64_t found_nonce;
127
+ volatile int *global_found;
128
+ uint64_t attempts;
129
+ } WorkerArgs;
130
+
131
+ static void *grind_worker(void *arg) {
132
+ WorkerArgs *a = (WorkerArgs *)arg;
133
+ uint8_t buf[84];
134
+ uint8_t hash[32];
135
+ memcpy(buf, a->base, 52); // challenge + address
136
+ memset(buf + 52, 0, 24); // Zero first 24 bytes of nonce area
137
+
138
+ for (uint64_t n = a->start; n < a->start + a->range; n++) {
139
+ if (*a->global_found) return NULL;
140
+
141
+ // Write nonce as big-endian in last 8 bytes (buf[76..83])
142
+ buf[76] = (n >> 56) & 0xff;
143
+ buf[77] = (n >> 48) & 0xff;
144
+ buf[78] = (n >> 40) & 0xff;
145
+ buf[79] = (n >> 32) & 0xff;
146
+ buf[80] = (n >> 24) & 0xff;
147
+ buf[81] = (n >> 16) & 0xff;
148
+ buf[82] = (n >> 8) & 0xff;
149
+ buf[83] = n & 0xff;
150
+
151
+ keccak256(buf, 84, hash);
152
+
153
+ // Compare hash < target (MSB first)
154
+ int less = 0;
155
+ for (int i = 0; i < 32; i++) {
156
+ if (hash[i] < a->target[i]) { less = 1; break; }
157
+ if (hash[i] > a->target[i]) break;
158
+ }
159
+
160
+ if (less) {
161
+ a->found_nonce = n;
162
+ a->attempts = n - a->start + 1;
163
+ __sync_val_compare_and_swap(a->global_found, 0, 1);
164
+ return NULL;
165
+ }
166
+ }
167
+ a->attempts = a->range;
168
+ return NULL;
169
+ }
170
+
171
+ /* ─── Main ─────────────────────────────────────────────────── */
172
+
173
+ int main(int argc, char *argv[]) {
174
+ if (argc < 4) {
175
+ fprintf(stderr, "Usage: %s <challenge_hex> <address_hex> <target_hex> [threads]\n", argv[0]);
176
+ return 1;
177
+ }
178
+
179
+ uint8_t challenge[32], address[20], target[32];
180
+ if (parse_hex(argv[1], challenge, 32) < 0 ||
181
+ parse_hex(argv[2], address, 20) < 0 ||
182
+ parse_hex(argv[3], target, 32) < 0) {
183
+ fprintf(stderr, "Invalid hex input\n");
184
+ return 1;
185
+ }
186
+
187
+ int nthreads = argc > 4 ? atoi(argv[4]) : 8;
188
+ if (nthreads < 1) nthreads = 1;
189
+ if (nthreads > 64) nthreads = 64;
190
+
191
+ // Build base buffer
192
+ uint8_t base[84] = {0};
193
+ memcpy(base, challenge, 32);
194
+ memcpy(base + 32, address, 20);
195
+
196
+ // Random start nonce
197
+ srand((unsigned)time(NULL) ^ (unsigned)getpid());
198
+ uint64_t start = ((uint64_t)rand() << 32) | rand();
199
+ uint64_t range_per_thread = 500000000ULL; // 500M per thread max
200
+
201
+ volatile int found = 0;
202
+ pthread_t threads[64];
203
+ WorkerArgs args[64];
204
+
205
+ struct timespec t0, t1;
206
+ clock_gettime(CLOCK_MONOTONIC, &t0);
207
+
208
+ for (int i = 0; i < nthreads; i++) {
209
+ args[i] = (WorkerArgs){
210
+ .start = start + i * range_per_thread,
211
+ .range = range_per_thread,
212
+ .found_nonce = 0,
213
+ .global_found = &found,
214
+ .attempts = 0
215
+ };
216
+ memcpy(args[i].base, base, 84);
217
+ memcpy(args[i].target, target, 32);
218
+ pthread_create(&threads[i], NULL, grind_worker, &args[i]);
219
+ }
220
+
221
+ // Wait for completion
222
+ for (int i = 0; i < nthreads; i++)
223
+ pthread_join(threads[i], NULL);
224
+
225
+ clock_gettime(CLOCK_MONOTONIC, &t1);
226
+ double elapsed = (t1.tv_sec - t0.tv_sec) + (t1.tv_nsec - t0.tv_nsec) / 1e9;
227
+
228
+ // Find the winning thread
229
+ uint64_t total_attempts = 0;
230
+ for (int i = 0; i < nthreads; i++) {
231
+ total_attempts += args[i].attempts;
232
+ if (args[i].found_nonce && found) {
233
+ printf("%llu %llu %.3f\n", args[i].found_nonce, total_attempts, elapsed);
234
+ fflush(stdout);
235
+ return 0;
236
+ }
237
+ }
238
+
239
+ fprintf(stderr, "No nonce found after %llu attempts (%.1fs)\n", total_attempts, elapsed);
240
+ return 1;
241
+ }