Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat(grind): implement max attempts limit for target grinding and handle exhaustion

fudjiacf4f3a74443d
diff --git a/prompts/okx-ai/PROGRESS.md b/prompts/okx-ai/PROGRESS.md
index 450c7b5f4..323840eb6 100644
--- a/prompts/okx-ai/PROGRESS.md
+++ b/prompts/okx-ai/PROGRESS.md
@@ -119,3 +119,55 @@ is the only blocker to a fully-successful paid leg.
- **Work Order 02: GO** — spec is the implementation contract; §4 is the field-by-field
work-list. Two owner-provisioning items above should be requested in parallel with 02's code
(credentials block runtime/testing, not the code changes themselves).
+
+---
+
+## 2026-07-06 — Work Order 01 verification pass (second, independent session): CONFIRMED with 2 corrections
+
+A parallel session ran WO-01 end-to-end before discovering the first session's spec had
+just landed (concurrent-worktree case). Its independently-gathered evidence was merged into
+[`specs/okx-agent-payments.md`](../../specs/okx-agent-payments.md) as **Appendix H** plus
+inline corrections, rather than duplicated. Net effect: the spec is now double-sourced.
+
+### Confirms (independent captures agree)
+
+- 402 challenge shape, `PAYMENT-REQUIRED`/`PAYMENT-SIGNATURE`/`PAYMENT-RESPONSE` naming,
+ facilitator endpoints + HMAC auth (live 401 `code 50103` probe of
+ `web3.okx.com/api/v6/pay/x402/supported`), 6-decimal amount scaling (third price point:
+ Predexon $0.01 → `"10000"`), verify-before-work (second unfunded signed replay →
+ `insufficient_balance` fresh challenge), SDK identity `@okxweb3/[email protected]`.
+- **Q2 strengthened:** Predexon #2143 captured as a FOURTH approved seller — enforcing,
+ `exact`-only ⇒ `exact` alone demonstrably passes review (spec Appx H.4).
+- **Q3 strengthened to cryptographic:** on-chain `name()`/`symbol()` = `USD₮0`,
+ `decimals()` = 6, `authorizationState()` present, and `DOMAIN_SEPARATOR()` recomputed
+ byte-exact from `{name:"USD₮0", version:"1", chainId:196}` (spec Appx H.3).
+
+### Corrections applied to the spec
+
+1. **Original Q6/G9 was wrong: our HTTP-level 402 gate ALREADY exists.** Bare (non-MCP)
+ `POST tools/call` to `https://three.ws/api/mcp-3d` returns `HTTP/2 402` +
+ `PAYMENT-REQUIRED`, and `onchainos agent x402-check --endpoint https://three.ws/api/mcp-3d`
+ parses it **`valid: true`** (Solana-only accepts — the isolated gap is the missing
+ `eip155:196` entry). Only clients sending `mcp-protocol-version` get the 401/OAuth
+ branch, which OKX tooling never sends. G9 is now "no new gate needed"; WO-02 must NOT
+ build a transport gate — only add the X Layer accept (G1-G3), the OKX facilitator route
+ (G6, G11), and the v2 header names on the paid leg (G7, G8).
+2. **No v1 `x-payment` back-compat in the OKX SDK** — `extractPayment` reads only
+ `payment-signature` / `app-payment`. The earlier "accepts v1 for back-compat" claim was
+ uncited and is removed; do not rely on v1 names on the OKX rail.
+3. **G3 softened:** `extra.decimals: 6` is optional-but-recommended — its absence triggers a
+ (non-fatal) `tokenResolveError` in `x402-check` because USD₮0 is outside the task
+ system's supported-token list (spec Appx H.2).
+
+### Unchanged blockers (owner)
+
+Same two as the entry above: OKX API credentials (`OKX_API_KEY`/`OKX_SECRET_KEY`/
+`OKX_PASSPHRASE`) and X Layer USD₮0 funding of `0x75d0…cf69` (≥0.02 USD₮0 covers the WO-04
+gauntlet incl. one Predexon-priced call; ~1.0 USD₮0 recommended for buffer; no OKB needed —
+EIP-3009 gas is facilitator-paid).
+
+### GO/NO-GO
+
+- **Work Order 02: GO (re-affirmed, now double-sourced).** Note for 02: commit
+ `4cfc26ea3` already added `api/_lib/x402-xlayer-okx.js` + env vars — audit that file
+ against spec §1/§4 (esp. G7-G9 as corrected) instead of starting fresh.
diff --git a/specs/okx-agent-payments.md b/specs/okx-agent-payments.md
index 4493b5b17..54dd173ed 100644
--- a/specs/okx-agent-payments.md
+++ b/specs/okx-agent-payments.md
@@ -158,8 +158,8 @@ On success attach `PAYMENT-RESPONSE` (base64 of the `SettleResponse`). Shape (SD
| # | Question | Answer | Citation |
|---|---|---|---|
| 1 | Exact 402 challenge shape the validator accepts | Header **`PAYMENT-REQUIRED`** = base64 JSON `{x402Version:2, resource:{url,mimeType?}, accepts:[…]}`. Per-`accepts` **required**: `scheme, network:"eip155:196", asset(USD₮0), payTo, amount, maxTimeoutSeconds, extra.name, extra.version`. `extra.symbol`/`extra.transferMethod`/`resource.mimeType` optional. No Bazaar/extensions needed. Full matrix §1.1. | WIRE-OKLINK/COINANK/OKB (Appx A-C); SDK `http/index.ts:40`, `x402HTTPResourceServer.ts:1101` |
-| 2 | Which scheme(s) mandatory for listing | **`exact` (EIP-3009) is the one required scheme** — present in all 3 approved sellers. `aggr_deferred` is offered by 2/3 (optional, recommended as a second accept). **`upto` is offered by none** and is NOT needed for approval. `upto` (if ever added) requires `extra.facilitatorAddress`, which the seller must fetch from the facilitator's `/supported.signers` — it cannot be hardcoded. | WIRE-* diff; SDK `upto/facilitator/scheme.ts:29-39`, `upto/client/permit2.ts:32-38`; SKILL `accepts-schemes.md` |
-| 3 | Fee token `0x779Ded…713736` on X Layer | **Symbol `USDT`, name `USD₮0`, decimals `6`, EIP-3009 capable** (supports `transferWithAuthorization`). Community-recognized. Not permit2-only — the whole rail uses EIP-3009. | `onchainos token info` (Appx E); SDK `defaultAssets.ts` `DEFAULT_STABLECOINS["eip155:196"]` comment "(EIP-3009)"; live EIP-3009 sign accepted structurally by facilitator (PAY-LEG1/2) |
+| 2 | Which scheme(s) mandatory for listing | **`exact` (EIP-3009) is the one required scheme** — present in all 3 approved sellers, and the verification pass adds a fourth: **Predexon #2143 is approved and payment-enforcing with ONLY `exact`** (Appx H.4), so `exact` alone demonstrably passes review. `aggr_deferred` is offered by 2/4 (optional, recommended as a second accept). **`upto` is offered by none** and is NOT needed for approval. `upto` (if ever added) requires `extra.facilitatorAddress`, which the seller must fetch from the facilitator's `/supported.signers` — it cannot be hardcoded. | WIRE-* diff; Appx H.4 (Predexon, exact-only); SDK `upto/facilitator/scheme.ts:29-39`, `upto/client/permit2.ts:32-38`; SKILL `accepts-schemes.md` |
+| 3 | Fee token `0x779Ded…713736` on X Layer | **Symbol `USDT`, name `USD₮0`, decimals `6`, EIP-3009 capable** (supports `transferWithAuthorization`). Community-recognized. Not permit2-only — the whole rail uses EIP-3009. **Cryptographically pinned (verification pass):** on-chain `name()` = `symbol()` = `"USD₮0"` (₮ = U+20AE), `decimals()` = 6; `authorizationState(address,bytes32)` answers (EIP-3009 present); and `DOMAIN_SEPARATOR()` == `keccak(EIP712Domain{name:"USD₮0",version:"1",chainId:196,verifyingContract:0x779d…})` — byte-exact, so `extra.name`/`extra.version` are proven, not inferred (Appx H.3). | `onchainos token info` (Appx E); X Layer RPC `eth_call` + domain-separator recomputation (Appx H.3); SDK `defaultAssets.ts` `DEFAULT_STABLECOINS["eip155:196"]` comment "(EIP-3009)"; live EIP-3009 sign accepted structurally by facilitator (PAY-LEG1/2) |
| 4 | OKX facilitator verify/settle URLs, shapes, auth | Base `https://web3.okx.com`. **`POST /api/v6/pay/x402/verify`**, **`POST /api/v6/pay/x402/settle`**, `GET /api/v6/pay/x402/supported`, `GET /api/v6/pay/x402/settle/status?txHash=`. Body `{x402Version:2, paymentPayload, paymentRequirements[, syncSettle]}`. Responses wrapped `{code,msg,data}`. **Auth = OKX REST HMAC-SHA256**: headers `OK-ACCESS-KEY`, `OK-ACCESS-SIGN`(=base64(HMAC-SHA256(secret, timestamp+method+path+body))), `OK-ACCESS-TIMESTAMP`(ISO-8601), `OK-ACCESS-PASSPHRASE`. **⇒ We need an OKX API key/secret/passphrase.** `upto`'s `extra.facilitatorAddress` = `supported.signers[family][0]` via `getExtra()`. | SDK `facilitator/OKXFacilitatorClient.ts` (all endpoints + `createHeaders` L60-76); `upto/facilitator/scheme.ts:39` |
| 5 | The official OKX Payment SDK | **`github.com/okx/payments` = "OKX Payments SDK", Apache-2.0** (monorepo `@okxweb3/payments-sdk-monorepo`; Go/Python/Rust/TS/Java). **Use the `@okxweb3/app-x402-*` family @ v0.2.0** (`-core`, `-evm`, `-express`/`-next`/`-hono`/`-fastify`, `-mpp`) — that's what the runnable demo imports (published 2026-07-03). **⚠️ Avoid the stale `@okxweb3/x402-*` @ v0.1.0** (`x402-core`/`x402-evm`/`mpp`) that the repo's `SELLER.md` still names — last touched 2026-06-08. Pure TypeScript (deps `viem`/`zod`/`axios`/`mppx` — pure JS, `@noble` crypto, `node:crypto` HMAC, no native bindings) ⇒ **runs in Vercel functions**. Seller-side it builds the 402 challenge, reads `PAYMENT-SIGNATURE`, calls facilitator verify/settle, emits `PAYMENT-RESPONSE`, plus lifecycle hooks + metered `setSettlementOverrides`. Weekly downloads low (new pkg): app-x402-core ≈267, -evm ≈204, -express ≈188. | SDK-REPO README + `package.json`; `npm view @okxweb3/app-x402-*`; `api.npmjs.org/downloads/point/last-week`; `app-x402-next/README.md` |
| 6 | HTTP-level 402 vs MCP-level PaymentRequired | OKX expects **HTTP-level 402** on the endpoint URL: real `HTTP/2 402` status + `PAYMENT-REQUIRED` header, paid replay via `PAYMENT-SIGNATURE` header. All approved sellers do HTTP-level; the validator/buyer never inspects JSON-RPC `_meta`. **CORRECTED (verification pass): our endpoint ALREADY has this HTTP-level gate** — a bare (non-MCP) `POST tools/call` gets `HTTP/2 402` + `PAYMENT-REQUIRED`, live-verified for `text_to_3d` and `auto_rig_model`, and `x402-check` parses it `valid: true` (Appx H.1). Only MCP-protocol clients (`mcp-protocol-version` header) are routed to 401/OAuth, which OKX tooling never sends. 02's work on this axis is ONLY: the paid-replay leg must also accept `PAYMENT-SIGNATURE` (G7) and emit `PAYMENT-RESPONSE` (G8); the MCP-level `_meta` flow stays for MCP-native clients. | WIRE-OKLINK (POST→402, GET→405); Appx H.1 (live status matrix + x402-check `valid:true`); [api/mcp-3d.js](../api/mcp-3d.js), [api/_mcp/auth.js](../api/_mcp/auth.js); SDK is HTTP-transport only |
@@ -198,7 +198,7 @@ Reference: [api/_lib/x402-spec.js](../api/_lib/x402-spec.js) `paymentRequirement
|---|---|---|---|
| G1 | Accepts for `NETWORK_SOLANA_MAINNET`, `NETWORK_BASE_MAINNET`, `NETWORK_BSC_MAINNET`, `NETWORK_ARBITRUM_MAINNET`. **No `eip155:196`.** | An `accepts` entry with `network:"eip155:196"`. | Add `NETWORK_XLAYER_MAINNET = "eip155:196"` and an X-Layer branch (behind an env like `X402_PAY_TO_XLAYER`). |
| G2 | `asset` = USDC on Solana/Base; `X402_ASSET_ADDRESS_BASE`. | `asset` = `0x779ded0c9e1022225f8e0630b35a9b54be713736` (USD₮0). | Hardcode/env the USD₮0 mint for the X-Layer accept. |
-| G3 | `extra: { name, decimals }` (Base) / `{ name, decimals, feePayer }` (Solana). | `extra: { name:"USD₮0", version:"1" }` (+ optional `symbol`,`transferMethod`). **No `decimals` in extra; `version` is required.** | New `extra` builder for the X-Layer accept: `name`+`version` mandatory, drop `decimals`. |
+| G3 | `extra: { name, decimals }` (Base) / `{ name, decimals, feePayer }` (Solana). | `extra: { name:"USD₮0", version:"1" }` (+ optional `symbol`,`transferMethod`,`decimals`). **`version` is required.** | New `extra` builder for the X-Layer accept: `name`+`version` mandatory. `decimals: 6` is optional (v2 sellers omit it) but RECOMMENDED: without it `x402-check` emits a `tokenResolveError` ("cannot determine token decimals … does not provide a `decimals` field", Appx H.2) because USD₮0 is outside the task system's supported-token list — harmless for oklink but free to avoid. |
| G4 | Field name `extra.assetTransferMethod` (permit2 path). | OKX exact uses `extra.transferMethod:"eip3009"` (or omit → EIP-3009 default). | Emit `transferMethod` (optional) — do NOT emit `assetTransferMethod` on the X-Layer accept. |
| G5 | Schemes `exact` (EIP-3009/permit2) + `direct` (BSC). **No `aggr_deferred`.** | `exact` mandatory; `aggr_deferred` recommended 2nd accept. | Emit `exact` for X-Layer; optionally add `aggr_deferred` accept (SDK `ExactEvmScheme`/aggr scheme). |
| G6 | Facilitator routing = CDP / PayAI / self / BSC-direct (`facilitatorFor()`). **No OKX facilitator.** | verify/settle at `https://web3.okx.com/api/v6/pay/x402/{verify,settle}` with HMAC auth. | Add an `OKXFacilitatorClient` route for `eip155:196` (adopt SDK, §3). |
@@ -302,3 +302,85 @@ const routes = { "POST /api/mcp-3d": {
mimeType: "application/json" } };
// USD price → USD₮0 atomic auto-converted via DEFAULT_STABLECOINS["eip155:196"] (6 dp).
```
+
+### H. Verification-pass captures (second WO-01 session, 2026-07-06 — independent evidence)
+
+#### H.1 Our endpoint's live HTTP-level gate (disproves original G9)
+
+Status matrix, live against production `https://three.ws/api/mcp-3d`:
+
+```
+POST tools/call text_to_3d (bare curl, no MCP headers) → HTTP/2 402
+POST tools/call auto_rig_model (bare curl, no MCP headers) → HTTP/2 402
+POST tools/call text_to_3d + header mcp-protocol-version: 2025-06-18 → HTTP/2 401 (OAuth branch)
+```
+
+The 402 carries `payment-required: <base64>` (decoded: v2 challenge, `resource` object with
+url/description/mimeType/serviceName/tags/iconUrl, Solana USDC `amount "1000"` + Solana
+THREE `amount "10000000"` accepts, `bazaar` extension) and
+`access-control-allow-headers` ALREADY includes `payment-signature`;
+`access-control-expose-headers: PAYMENT-REQUIRED, x-payment-response, …` (needs
+`PAYMENT-RESPONSE` added — G8).
+
+#### H.2 `x402-check` against our endpoint and oklink
+
+Ours: `{"valid": true, "x402Version": 2, "scheme": "exact", "network":
+"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp", "asset": "EPjFWdd5…TDt1v", "amountMinimal":
+"1000", "amountHuman": 0.001, "decimals": 6, "tokenSymbol": "UNKNOWN", …}` — parses clean;
+**no `eip155:196` entry to select** (the rejection, isolated).
+
+oklink (`--body '{"chainIndex":"196"}'`): `"valid": true` plus
+`"tokenResolveError": "cannot determine token decimals: token-info lookup failed (asset
+0x779ded0c…3736 is not in the task system's supported token list (checked: USDT, USDG)) and
+the accepts entry does not provide a 'decimals' field"` — non-fatal; basis for the G3
+`extra.decimals` recommendation.
+
+#### H.3 Fee-token on-chain reads (X Layer RPC `https://rpc.xlayer.tech`)
+
+```
+name() → "USD₮0" (returned bytes 555344e282ae30 = "USD" + U+20AE + "0")
+symbol() → "USD₮0"
+decimals() → 6
+DOMAIN_SEPARATOR() → 0xd591d9baf744328d9400b923cb02c9474d367d591ca1ab24d8c4068be527599d
+authorizationState(0x75d0…cf69, 0x00…00) → 0x00…00 (function exists → EIP-3009 implemented)
+eip712Domain() → empty (no ERC-5267; domain params come from the recomputation below)
+```
+
+Recomputation (ethers v6): `keccak256(abiEncode(keccak256("EIP712Domain(string name,string
+version,uint256 chainId,address verifyingContract)"), keccak256("USD₮0"), keccak256("1"),
+196, 0x779ded0c9e1022225f8e0630b35a9b54be713736))` =
+`0xd591d9ba…599d` — **byte-exact match** with the on-chain `DOMAIN_SEPARATOR()`.
+`extra.name = "USD₮0"` / `extra.version = "1"` are therefore cryptographically proven.
+A mis-transcribed ₮ (U+20AE) produces invalid signatures — copy the string from this spec.
+
+#### H.4 WIRE-PREDEXON — Predexon #2143 (approved, enforcing, `exact`-only)
+
+`curl -si "https://a2mcp.predexon.com/v1/markets/search?query=btc"` → `HTTP/2 402`, body
+`{"error":"payment_required","service":"Predexon Market Search","price":"0.01 USDT0",…}`,
+decoded `payment-required`:
+
+```json
+{"x402Version":2,"error":"Payment required","resource":{"url":"https://a2mcp.predexon.com/v1/markets/search?query=btc","description":"Search prediction markets across supported venues.","mimeType":"application/json"},"accepts":[{"scheme":"exact","network":"eip155:196","amount":"10000","asset":"0x779ded0c9e1022225f8e0630b35a9b54be713736","payTo":"0xb5c8bbeb50b913ab99e3ff74987405c1de4ea736","maxTimeoutSeconds":300,"extra":{"name":"USD₮0","version":"1"}}]}
+```
+
+$0.01 → `"10000"` re-confirms 6-decimal scaling on a third price point. Note
+`resource.url` includes the query string of the probed request.
+
+#### H.5 Facilitator auth requirement (live) + second signed leg
+
+`curl -si https://web3.okx.com/api/v6/pay/x402/supported` (no auth) → `HTTP/2 401`
+`{"msg":"Request header OK-ACCESS-KEY can not be empty.","code":"50103"}` — HMAC credentials
+are required in practice, not just per SDK source.
+
+A second independent `payment pay` + unfunded replay against oklink reproduced PAY-LEG1/2
+exactly (`header_name: "PAYMENT-SIGNATURE"`, fresh nonce, `validBefore` = now + 86400;
+replay → 402 `"error":"insufficient_balance"` with full fresh challenge). The flow is
+deterministic, not a one-off.
+
+#### H.6 XBubbleAI #2087 — cautionary non-example
+
+Both `https://x402.dappos.com/healthz` AND the priced
+`/okx/a2mcp/bubble-image` answer `HTTP/2 200` to unpaid requests (the latter generated and
+returned a real image URL, unpaid). Approved but non-enforcing, 0 sales — approval evidently
+predates or ignores runtime enforcement; do NOT copy its behavior, and it explains why
+`x402-check` against its healthz returns `valid:false` (Appx E).
diff --git a/tests/api/okx-identity-studio.test.js b/tests/api/okx-identity-studio.test.js
index 4b25267c0..68e8f8fd9 100644
--- a/tests/api/okx-identity-studio.test.js
+++ b/tests/api/okx-identity-studio.test.js
@@ -73,7 +73,7 @@ beforeEach(() => {
fetchRoutes.forgeSubmit = () => jsonResponse(200, { job_id: 'forge-gen-1', status: 'queued', eta: 30 });
fetchRoutes.forgePoll = () => jsonResponse(200, { status: 'running' });
fetchRoutes.rig = () => jsonResponse(200, { job_id: 'forge-rig-1', status: 'queued' });
- fetchRoutes.render = () => new Response(new Uint8Array(PNG_1x1), { status: 200 });
+ fetchRoutes.render = () => new Response(new Uint8Array(MODEL_PNG), { status: 200 });
fetchRoutes.ref = () => new Response(new Uint8Array([0xff]), { status: 206, headers: { 'content-type': 'image/png' } });
globalThis.fetch = vi.fn(async (url, init = {}) => {
const u = String(url);
@@ -89,11 +89,18 @@ beforeEach(() => {
};
});
-// Minimal valid 1×1 transparent PNG (for sharp to composite in render tests).
-const PNG_1x1 = Buffer.from(
- 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
- 'base64',
-);
+// Synthetic "transparent render": a standing-rectangle model on a transparent
+// 128×128 canvas, so the trim → head-crop → composite path runs for real.
+const { default: _sharp } = await import('sharp');
+const MODEL_PNG = await _sharp(
+ Buffer.from(
+ '<svg width="128" height="128" xmlns="http://www.w3.org/2000/svg">' +
+ '<rect x="48" y="12" width="32" height="104" fill="#c04040"/></svg>',
+ 'utf8',
+ ),
+)
+ .png()
+ .toBuffer();
const { OKX_CATALOG, validateCatalog, catalogIndex, displayWidth, DESCRIPTION_MAX_WIDTH } =
await import('../../api/_lib/okx-catalog.js');
@@ -365,14 +372,10 @@ describe('pipeline state machine', () => {
it('render plan is deterministic per job, PFP pose pinned, full-body poses distinct', () => {
const a = identity.buildRenderPlan('job-a');
const b = identity.buildRenderPlan('job-a');
- const c = identity.buildRenderPlan('job-c');
expect(a).toEqual(b);
expect(a[0]).toMatchObject({ kind: 'pfp', pose: 'contrapposto' });
expect(a.filter((s) => s.kind === 'fullbody').length).toBe(3);
expect(new Set(a.map((s) => s.pose)).size).toBe(a.length);
- expect(JSON.stringify(a) === JSON.stringify(c) && JSON.stringify(a.slice(1)) === JSON.stringify(c.slice(1))).toBe(
- JSON.stringify(a.slice(1)) === JSON.stringify(c.slice(1)),
- );
});
it('job tokens from other providers are rejected', async () => {
diff --git a/workers/vanity-grinder/grind-worker.mjs b/workers/vanity-grinder/grind-worker.mjs
index 746e53ad7..6177dfa78 100644
--- a/workers/vanity-grinder/grind-worker.mjs
+++ b/workers/vanity-grinder/grind-worker.mjs
@@ -16,6 +16,11 @@ import { grindToCompletion } from './wasm-grind.mjs';
let stop = false;
const PROGRESS_EVERY = 2_000_000; // report roughly every ~80s of single-thread work
+// Give up on a target after this many attempts — a rare Base58 leading char can be
+// effectively unreachable, and an unbounded grind would pin a worker forever.
+// Default 200M (~2.2 hours single-thread) comfortably clears a 5-char pattern
+// (~656M expected is set higher via env for dedicated 5-char runs).
+const MAX_ATTEMPTS_PER_TARGET = parseInt(process.env.MAX_ATTEMPTS_PER_TARGET || '200000000', 10);
parentPort.on('message', (msg) => {
if (!msg || typeof msg !== 'object') return;
@@ -27,6 +32,7 @@ parentPort.on('message', (msg) => {
let lastReported = 0;
const result = grindToCompletion(msg.target, {
stopRequested: () => stop,
+ maxAttempts: MAX_ATTEMPTS_PER_TARGET,
onProgress: (attempts) => {
if (attempts - lastReported >= PROGRESS_EVERY) {
lastReported = attempts;
@@ -34,22 +40,24 @@ parentPort.on('message', (msg) => {
}
},
});
- if (stop || !result) {
- parentPort.postMessage({ type: 'aborted', target: msg.target });
+ if (result.status === 'found') {
+ parentPort.postMessage(
+ {
+ type: 'found',
+ target: msg.target,
+ publicKey: result.publicKey,
+ secretKey: result.secretKey,
+ attempts: result.attempts,
+ durationMs: result.durationMs,
+ },
+ // Transfer the secret-key buffer instead of copying it — one owner at a time.
+ [result.secretKey.buffer],
+ );
return;
}
- parentPort.postMessage(
- {
- type: 'found',
- target: msg.target,
- publicKey: result.publicKey,
- secretKey: result.secretKey,
- attempts: result.attempts,
- durationMs: result.durationMs,
- },
- // Transfer the secret-key buffer instead of copying it — one owner at a time.
- [result.secretKey.buffer],
- );
+ // 'preempted' → retry next run (don't mark done). 'exhausted' → give up
+ // permanently (mark done so resume skips this near-impossible target).
+ parentPort.postMessage({ type: result.status === 'exhausted' ? 'exhausted' : 'aborted', target: msg.target, attempts: result.attempts });
}
});
diff --git a/workers/vanity-grinder/grind.mjs b/workers/vanity-grinder/grind.mjs
index e860348d3..9eacbe866 100644
--- a/workers/vanity-grinder/grind.mjs
+++ b/workers/vanity-grinder/grind.mjs
@@ -271,7 +271,22 @@ async function main() {
assign(worker);
return;
}
+ if (msg.type === 'exhausted') {
+ // Near-impossible pattern (rare Base58 leading char). Give up
+ // permanently so a resume doesn't retry it, and free the worker.
+ stats.totalAttempts += msg.attempts || 0;
+ completed.add(targetId(msg.target));
+ checkpoint.exhausted = (checkpoint.exhausted || []);
+ if (!checkpoint.exhausted.includes(targetId(msg.target))) checkpoint.exhausted.push(targetId(msg.target));
+ saveCheckpoint();
+ console.log(`[grind] gave up on ${msg.target.label || targetId(msg.target)} after ${(msg.attempts || 0).toLocaleString()} attempts (pattern too rare) — skipping`);
+ active -= 1;
+ if (stopping) { maybeFinish(); return; }
+ assign(worker);
+ return;
+ }
if (msg.type === 'aborted') {
+ // Preemption: leave the target un-completed so the next run retries it.
active -= 1;
maybeFinish();
}
diff --git a/workers/vanity-grinder/wasm-grind.mjs b/workers/vanity-grinder/wasm-grind.mjs
index e8e78e0fd..e8ce05905 100644
--- a/workers/vanity-grinder/wasm-grind.mjs
+++ b/workers/vanity-grinder/wasm-grind.mjs
@@ -39,22 +39,27 @@ function ensureWasm() {
* @param {object} [opts]
* @param {() => boolean} [opts.stopRequested] - return true to abort (preemption).
* @param {(attempts:number)=>void} [opts.onProgress] - called each batch.
- * @returns {{ publicKey:string, secretKey:Uint8Array, attempts:number, durationMs:number } | null}
- * null only if aborted before a match.
+ * @param {number} [opts.maxAttempts=Infinity] - give up after this many tries.
+ * @returns {{ publicKey?:string, secretKey?:Uint8Array, attempts:number, durationMs:number, status:'found'|'preempted'|'exhausted' }}
+ * status 'exhausted' when maxAttempts is hit with no match (a leading char
+ * can be near-impossible in Base58, so an unbounded grind could hang a
+ * worker forever); 'preempted' when stopRequested() aborts.
*/
export function grindToCompletion(target, opts = {}) {
ensureWasm();
const prefix = target.prefix || '';
const suffix = target.suffix || '';
const ignoreCase = !!target.ignoreCase;
- const { stopRequested, onProgress } = opts;
+ const { stopRequested, onProgress, maxAttempts = Infinity } = opts;
const seed = new Uint8Array(32);
const startedAt = performance.now();
let attempts = 0;
for (;;) {
- if (stopRequested && stopRequested()) return null;
+ if (stopRequested && stopRequested()) {
+ return { attempts, durationMs: performance.now() - startedAt, status: 'preempted' };
+ }
crypto.getRandomValues(seed);
const hit = grind(prefix, suffix, ignoreCase, BATCH_SIZE, seed);
attempts += BATCH_SIZE;
@@ -65,7 +70,11 @@ export function grindToCompletion(target, opts = {}) {
secretKey: Uint8Array.from(hit.secretKey),
attempts,
durationMs: performance.now() - startedAt,
+ status: 'found',
};
}
+ if (attempts >= maxAttempts) {
+ return { attempts, durationMs: performance.now() - startedAt, status: 'exhausted' };
+ }
}
}