zeldhash-miner 0.2.3 → 0.2.4

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/index.js CHANGED
@@ -1,22 +1,22 @@
1
- var o = /* @__PURE__ */ ((s) => (s.INVALID_ADDRESS = "INVALID_ADDRESS", s.UNSUPPORTED_ADDRESS_TYPE = "UNSUPPORTED_ADDRESS_TYPE", s.INSUFFICIENT_FUNDS = "INSUFFICIENT_FUNDS", s.NO_CHANGE_OUTPUT = "NO_CHANGE_OUTPUT", s.MULTIPLE_CHANGE_OUTPUTS = "MULTIPLE_CHANGE_OUTPUTS", s.INVALID_INPUT = "INVALID_INPUT", s.WEBGPU_NOT_AVAILABLE = "WEBGPU_NOT_AVAILABLE", s.WORKER_ERROR = "WORKER_ERROR", s.MINING_ABORTED = "MINING_ABORTED", s.DUST_OUTPUT = "DUST_OUTPUT", s))(o || {});
2
- const P = (s) => s instanceof Error ? s.message : String(s);
3
- class w extends Error {
1
+ import { Z as I, l as H, c as F, n as K, s as v, a as f } from "./nonce.js";
2
+ const S = (o) => o instanceof Error ? o.message : String(o);
3
+ class N extends Error {
4
4
  code;
5
5
  details;
6
- constructor(t, e, n) {
7
- super(e), this.name = "ZeldMinerError", this.code = t, this.details = n;
6
+ constructor(t, e, s) {
7
+ super(e), this.name = "ZeldMinerError", this.code = t, this.details = s;
8
8
  }
9
9
  }
10
- const v = (s, t = o.WORKER_ERROR, e) => {
11
- if (s instanceof w)
12
- return s;
13
- const n = P(s);
14
- return new w(t, n, e);
15
- }, r = (s, t, e) => new w(s, t, e), X = (s) => {
10
+ const Y = (o, t = I.WORKER_ERROR, e) => {
11
+ if (o instanceof N)
12
+ return o;
13
+ const s = S(o);
14
+ return new N(t, s, e);
15
+ }, i = (o, t, e) => new N(o, t, e), B = (o) => {
16
16
  const t = BigInt(Number.MAX_SAFE_INTEGER);
17
- return s > t ? Number.MAX_SAFE_INTEGER : s < -t ? -Number.MAX_SAFE_INTEGER : Number(s);
17
+ return o > t ? Number.MAX_SAFE_INTEGER : o < -t ? -Number.MAX_SAFE_INTEGER : Number(o);
18
18
  };
19
- class O {
19
+ class T {
20
20
  mode;
21
21
  batchSize;
22
22
  workerCount;
@@ -57,7 +57,7 @@ class O {
57
57
  this.listeners[t].delete(e);
58
58
  }
59
59
  emit(t, e) {
60
- this.listeners[t].forEach((n) => n(e));
60
+ this.listeners[t].forEach((s) => s(e));
61
61
  }
62
62
  resolveWorkerUrl() {
63
63
  const t = typeof location < "u" && location.origin ? location.origin : typeof self < "u" && self.location?.origin ? self.location.origin : void 0;
@@ -65,31 +65,31 @@ class O {
65
65
  }
66
66
  async spawnWorkers() {
67
67
  const t = [], e = this.resolveWorkerUrl();
68
- for (let n = 0; n < this.workerCount; n += 1) {
69
- const i = new Worker(e, {
68
+ for (let s = 0; s < this.workerCount; s += 1) {
69
+ const g = new Worker(e, {
70
70
  type: "module",
71
71
  /* @vite-ignore */
72
- name: `zeldminer-worker-${n}`
73
- }), g = {
74
- worker: i,
72
+ name: `zeldminer-worker-${s}`
73
+ }), n = {
74
+ worker: g,
75
75
  hashesProcessed: 0n,
76
76
  hashRate: 0,
77
77
  nextNonce: 0n,
78
78
  processedBase: 0n
79
79
  };
80
- this.workers.push(g), t.push(
81
- new Promise((c) => {
82
- const a = (A) => {
83
- A.data.type === "ready" && (i.removeEventListener("message", a), c());
80
+ this.workers.push(n), t.push(
81
+ new Promise((C) => {
82
+ const c = (d) => {
83
+ d.data.type === "ready" && (g.removeEventListener("message", c), C());
84
84
  };
85
- i.addEventListener("message", a);
85
+ g.addEventListener("message", c);
86
86
  })
87
- ), i.addEventListener(
87
+ ), g.addEventListener(
88
88
  "message",
89
- (c) => this.handleWorkerMessage(g, c.data)
89
+ (C) => this.handleWorkerMessage(n, C.data)
90
90
  );
91
- const I = { type: "init", mode: this.mode };
92
- i.postMessage(I);
91
+ const r = { type: "init", mode: this.mode };
92
+ g.postMessage(r);
93
93
  }
94
94
  await Promise.all(t), this.emit("ready", void 0);
95
95
  }
@@ -105,22 +105,22 @@ class O {
105
105
  break;
106
106
  case "found":
107
107
  t.hashesProcessed = t.processedBase + (e.hashesProcessed ?? t.hashesProcessed), t.hashRate = e.hashRate ?? t.hashRate, t.lastNonce = e.lastNonce ?? t.lastNonce;
108
- const n = this.workers.reduce(
109
- (c, a) => c + a.hashesProcessed,
108
+ const s = this.workers.reduce(
109
+ (C, c) => C + c.hashesProcessed,
110
110
  0n
111
- ), i = this.startedAt ? performance.now() - this.startedAt : 0, g = i > 0 ? X(n) / (i / 1e3) : 0, I = {
111
+ ), g = this.startedAt ? performance.now() - this.startedAt : 0, n = g > 0 ? B(s) / (g / 1e3) : 0, r = {
112
112
  ...e.result,
113
- attempts: n,
114
- duration: i,
115
- hashRate: g
113
+ attempts: s,
114
+ duration: g,
115
+ hashRate: n
116
116
  };
117
- this.running = !1, this.paused = !1, this.terminateWorkers(), this.cleanupExternalAbort(), this.emit("found", I);
117
+ this.running = !1, this.paused = !1, this.terminateWorkers(), this.cleanupExternalAbort(), this.emit("found", r);
118
118
  break;
119
119
  case "error":
120
120
  this.running = !1, this.paused = !1, this.terminateWorkers(), this.cleanupExternalAbort(), this.emit(
121
121
  "error",
122
- new w(
123
- e.code ?? o.WORKER_ERROR,
122
+ new N(
123
+ e.code ?? I.WORKER_ERROR,
124
124
  e.message,
125
125
  {
126
126
  workerId: e.workerId,
@@ -134,14 +134,14 @@ class O {
134
134
  emitProgress() {
135
135
  if (!this.running) return;
136
136
  const t = this.workers.reduce(
137
- (g, I) => g + I.hashesProcessed,
137
+ (n, r) => n + r.hashesProcessed,
138
138
  0n
139
- ), e = this.startedAt ? performance.now() - this.startedAt : 0, n = e > 0 ? X(t) / (e / 1e3) : 0, i = {
139
+ ), e = this.startedAt ? performance.now() - this.startedAt : 0, s = e > 0 ? B(t) / (e / 1e3) : 0, g = {
140
140
  hashesProcessed: t,
141
- hashRate: n,
141
+ hashRate: s,
142
142
  elapsedMs: e
143
143
  };
144
- this.emit("progress", i);
144
+ this.emit("progress", g);
145
145
  }
146
146
  computeNextNonce(t) {
147
147
  const e = this.stride - BigInt(this.batchSize);
@@ -163,25 +163,25 @@ class O {
163
163
  async start(t) {
164
164
  if (await this.readyPromise, this.stopWorkers(), this.running = !0, this.paused = !1, this.startedAt = performance.now(), this.txInputs = t.inputs, this.txOutputs = t.outputs, this.txNetwork = t.network === "signet" ? "testnet" : t.network, this.satsPerVbyte = t.satsPerVbyte, this.template = t.template, this.targetZeros = t.targetZeros, this.startNonce = t.startNonce ?? 0n, this.txDistribution = t.distribution, this.externalAbort && this.externalAbort !== t.signal && this.cleanupExternalAbort(), t.signal && (this.externalAbort = t.signal, t.signal.addEventListener("abort", this.abortHandler, { once: !0 })), !this.template || !this.txInputs || !this.txOutputs || this.targetZeros === void 0 || this.satsPerVbyte === void 0 || !this.txNetwork)
165
165
  throw new Error("Mining parameters are missing");
166
- const e = this.txInputs, n = this.txOutputs, i = this.txNetwork, g = this.satsPerVbyte, I = this.template, c = this.targetZeros, a = this.txDistribution, A = this.stride;
167
- this.workers.forEach((C, l) => {
168
- C.processedBase = 0n, C.hashesProcessed = 0n, C.hashRate = 0, C.lastNonce = void 0;
166
+ const e = this.txInputs, s = this.txOutputs, g = this.txNetwork, n = this.satsPerVbyte, r = this.template, C = this.targetZeros, c = this.txDistribution, d = this.stride;
167
+ this.workers.forEach((A, l) => {
168
+ A.processedBase = 0n, A.hashesProcessed = 0n, A.hashRate = 0, A.lastNonce = void 0;
169
169
  const u = this.startNonce + BigInt(l) * BigInt(this.batchSize);
170
- C.nextNonce = u;
170
+ A.nextNonce = u;
171
171
  const h = {
172
172
  type: "mine",
173
173
  inputs: e,
174
- outputs: n,
175
- network: i,
176
- satsPerVbyte: g,
177
- template: I,
174
+ outputs: s,
175
+ network: g,
176
+ satsPerVbyte: n,
177
+ template: r,
178
178
  startNonce: u,
179
179
  batchSize: this.batchSize,
180
- targetZeros: c,
181
- nonceStep: A,
182
- distribution: a
180
+ targetZeros: C,
181
+ nonceStep: d,
182
+ distribution: c
183
183
  };
184
- C.worker.postMessage(h);
184
+ A.worker.postMessage(h);
185
185
  });
186
186
  }
187
187
  pause() {
@@ -191,236 +191,89 @@ class O {
191
191
  if (!this.paused || !this.template || !this.txInputs || !this.txOutputs || this.targetZeros === void 0 || this.satsPerVbyte === void 0 || !this.txNetwork)
192
192
  return;
193
193
  await this.readyPromise, this.running = !0, this.paused = !1, this.startedAt || (this.startedAt = performance.now());
194
- const t = this.txInputs, e = this.txOutputs, n = this.txNetwork, i = this.satsPerVbyte, g = this.template, I = this.targetZeros, c = this.txDistribution, a = this.stride;
195
- this.workers.forEach((A, C) => {
196
- A.processedBase = A.hashesProcessed, A.hashRate = 0;
197
- const l = A.nextNonce || this.startNonce + BigInt(C) * BigInt(this.batchSize), u = {
194
+ const t = this.txInputs, e = this.txOutputs, s = this.txNetwork, g = this.satsPerVbyte, n = this.template, r = this.targetZeros, C = this.txDistribution, c = this.stride;
195
+ this.workers.forEach((d, A) => {
196
+ d.processedBase = d.hashesProcessed, d.hashRate = 0;
197
+ const l = d.nextNonce || this.startNonce + BigInt(A) * BigInt(this.batchSize), u = {
198
198
  type: "mine",
199
199
  inputs: t,
200
200
  outputs: e,
201
- network: n,
202
- satsPerVbyte: i,
203
- template: g,
201
+ network: s,
202
+ satsPerVbyte: g,
203
+ template: n,
204
204
  startNonce: l,
205
205
  batchSize: this.batchSize,
206
- targetZeros: I,
207
- nonceStep: a,
208
- distribution: c
206
+ targetZeros: r,
207
+ nonceStep: c,
208
+ distribution: C
209
209
  };
210
- A.worker.postMessage(u);
210
+ d.worker.postMessage(u);
211
211
  });
212
212
  }
213
213
  stop() {
214
214
  !this.running && !this.paused || (this.running = !1, this.paused = !1, this.terminateWorkers(), this.cleanupExternalAbort(), this.emit("stopped", void 0));
215
215
  }
216
216
  }
217
- const L = {};
218
- if (typeof globalThis.__ZELDMINER_WASM_BASE__ > "u")
219
- try {
220
- const s = typeof window < "u" && window.location?.origin ? window.location.origin : typeof self < "u" && self.location?.origin ? self.location.origin : "http://localhost";
221
- globalThis.__ZELDMINER_WASM_BASE__ = new URL("/wasm/", s).href;
222
- } catch {
223
- globalThis.__ZELDMINER_WASM_BASE__ = "/wasm/";
224
- }
225
- let B = null, N = null, Y = !1;
226
- const D = () => {
227
- if (Y) return;
228
- Y = !0;
229
- const s = globalThis.GPUAdapter?.prototype, t = s?.requestDevice;
230
- !s || typeof t != "function" || (s.requestDevice = function(n) {
231
- if (n?.requiredLimits && typeof this.limits == "object") {
232
- const i = n.requiredLimits, g = this.limits;
233
- for (const I of Object.keys(i))
234
- (!(I in g) || g[I] === void 0) && delete i[I];
235
- }
236
- return t.call(this, n);
237
- });
238
- }, R = (s) => s.endsWith("/") ? s : `${s}/`, p = (s) => {
239
- const t = s.trim();
240
- return t && (typeof window < "u" && typeof window.location?.origin == "string" ? R(new URL(t, window.location.origin).href) : R(new URL(t, import.meta.url).href));
241
- }, Q = () => {
242
- const s = globalThis.__ZELDMINER_WASM_BASE__;
243
- if (typeof s == "string" && s.trim())
244
- return p(s);
245
- const t = L?.VITE_ZELDMINER_WASM_BASE;
246
- if (typeof t == "string" && t.trim())
247
- return p(t);
248
- const e = "./";
249
- return e.trim() ? p(`${R(e.trim())}wasm/`) : p("/wasm/");
250
- }, K = Q(), S = `${K}zeldhash_miner_wasm.js`, _ = `${K}zeldhash_miner_wasm_bg.wasm`, x = async (s) => (0, eval)("s => import(s)")(s), T = (s) => s instanceof Error ? s.message : String(s), j = async () => {
251
- D();
252
- let s;
253
- try {
254
- s = await x(
255
- /* @vite-ignore */
256
- S
257
- );
258
- } catch (n) {
259
- throw new Error(
260
- `Failed to import WASM bundle (${S}). Did you run ./scripts/build-wasm.sh? (${T(n)})`
261
- );
262
- }
263
- const t = s.default;
264
- if (typeof t != "function")
265
- throw new Error("WASM init function is missing from the bundle.");
266
- try {
267
- const n = new URL(_, import.meta.url);
268
- await t({ module_or_path: n });
269
- } catch (n) {
270
- throw new Error(
271
- `Failed to initialize WASM bundle: ${T(n)}`
272
- );
273
- }
274
- const e = s;
275
- try {
276
- e.init_panic_hook?.();
277
- } catch {
278
- }
279
- return e;
280
- }, M = async () => B || (N || (N = j().then((s) => (B = s, s)).catch((s) => {
281
- throw N = null, s;
282
- })), N), W = (1n << 64n) - 1n, k = (s) => {
283
- if (s < 0n)
284
- throw new Error("nonce must be non-negative");
285
- if (s === 0n) return 1;
286
- let t = 0, e = s;
287
- for (; e > 0n; )
288
- t += 1, e >>= 8n;
289
- return t;
290
- }, z = (s) => {
291
- if (s < 0n)
292
- throw new Error("nonce must be non-negative");
293
- if (s <= 23n) return 1;
294
- if (s <= 0xffn) return 2;
295
- if (s <= 0xffffn) return 3;
296
- if (s <= 0xffffffffn) return 5;
297
- if (s <= W) return 9;
298
- throw new Error("nonce range exceeds u64");
299
- }, $ = (s) => {
300
- if (!Number.isInteger(s) || s <= 0 || s > 8)
301
- throw new Error("nonceLength must be between 1 and 8");
302
- return (1n << BigInt(s * 8)) - 1n;
303
- }, q = (s) => {
304
- switch (s) {
305
- case 1:
306
- return 23n;
307
- case 2:
308
- return 0xffn;
309
- case 3:
310
- return 0xffffn;
311
- case 5:
312
- return 0xffffffffn;
313
- case 9:
314
- return W;
315
- default:
316
- throw new Error("cbor nonceLength must be one of 1, 2, 3, 5, 9");
317
- }
318
- }, tt = (s, t) => {
319
- if (s < 0n)
320
- throw new Error("startNonce must be non-negative");
321
- if (!Number.isInteger(t) || t <= 0)
322
- throw new Error("batchSize must be a positive integer");
323
- const e = s + BigInt(t - 1);
324
- if (e > W)
325
- throw new Error("nonce range exceeds u64");
326
- const n = [];
327
- let i = s;
328
- for (; i <= e; ) {
329
- const g = k(i), I = $(g), c = e < I ? e : I, a = c - i + 1n;
330
- if (a > BigInt(Number.MAX_SAFE_INTEGER))
331
- throw new Error("segment size exceeds safe integer range");
332
- if (n.push({
333
- start: i,
334
- size: Number(a),
335
- nonceLength: g
336
- }), c === e)
337
- break;
338
- i = c + 1n;
339
- }
340
- return n;
341
- }, et = (s, t) => {
342
- if (s < 0n)
343
- throw new Error("startNonce must be non-negative");
344
- if (!Number.isInteger(t) || t <= 0)
345
- throw new Error("batchSize must be a positive integer");
346
- const e = s + BigInt(t - 1);
347
- if (e > W)
348
- throw new Error("nonce range exceeds u64");
349
- const n = [];
350
- let i = s;
351
- for (; i <= e; ) {
352
- const g = z(i), I = q(g), c = e < I ? e : I, a = c - i + 1n;
353
- if (a > BigInt(Number.MAX_SAFE_INTEGER))
354
- throw new Error("segment size exceeds safe integer range");
355
- if (n.push({
356
- start: i,
357
- size: Number(a),
358
- nonceLength: g
359
- }), c === e)
360
- break;
361
- i = c + 1n;
362
- }
363
- return n;
364
- }, st = /^[0-9a-fA-F]{64}$/, nt = /^[0-9a-fA-F]+$/, G = (1n << 64n) - 1n, it = 4294967295, gt = (s) => s === "p2tr" ? 330 : s === "p2wpkh" ? 310 : 546, ot = (s) => s instanceof Error ? s.message : String(s), F = (s, t) => t.some((e) => s.includes(e)), J = (s, t, e) => {
365
- const n = ot(s), i = n.toLowerCase();
366
- throw F(i, ["insufficient funds", "insufficient_funds"]) ? r(
367
- o.INSUFFICIENT_FUNDS,
217
+ const U = /^[0-9a-fA-F]{64}$/, k = /^[0-9a-fA-F]+$/, p = (1n << 64n) - 1n, z = 4294967295, P = (o) => o === "p2tr" ? 330 : o === "p2wpkh" ? 310 : 546, Q = (o) => o instanceof Error ? o.message : String(o), R = (o, t) => t.some((e) => o.includes(e)), w = (o, t, e) => {
218
+ const s = Q(o), g = s.toLowerCase();
219
+ throw R(g, ["insufficient funds", "insufficient_funds"]) ? i(
220
+ I.INSUFFICIENT_FUNDS,
368
221
  "Insufficient funds for outputs and fee",
369
- { ...e, cause: n }
370
- ) : F(i, ["change would be dust", "output amount below dust limit", "dust"]) ? r(
371
- o.DUST_OUTPUT,
222
+ { ...e, cause: s }
223
+ ) : R(g, ["change would be dust", "output amount below dust limit", "dust"]) ? i(
224
+ I.DUST_OUTPUT,
372
225
  "Change would be dust",
373
- { ...e, cause: n }
374
- ) : v(s, o.WORKER_ERROR, {
226
+ { ...e, cause: s }
227
+ ) : Y(o, I.WORKER_ERROR, {
375
228
  ...e,
376
229
  context: t
377
230
  });
378
- }, V = (s) => s === "signet" ? "testnet" : s;
379
- class It {
231
+ }, W = (o) => o === "signet" ? "testnet" : o;
232
+ class O {
380
233
  network;
381
234
  satsPerVbyte;
382
235
  constructor(t, e) {
383
236
  if (!Number.isFinite(e) || e <= 0)
384
- throw r(
385
- o.INVALID_INPUT,
237
+ throw i(
238
+ I.INVALID_INPUT,
386
239
  "satsPerVbyte must be a positive number",
387
240
  { field: "satsPerVbyte" }
388
241
  );
389
242
  this.network = t, this.satsPerVbyte = e;
390
243
  }
391
244
  async getWasm() {
392
- return M();
245
+ return H();
393
246
  }
394
- assertNonceRange(t, e, n) {
395
- if (t < 0n || t > G)
396
- throw r(
397
- o.INVALID_INPUT,
247
+ assertNonceRange(t, e, s) {
248
+ if (t < 0n || t > p)
249
+ throw i(
250
+ I.INVALID_INPUT,
398
251
  "startNonce must be between 0 and 2^64 - 1",
399
252
  { startNonce: t }
400
253
  );
401
- if (!Number.isInteger(e) || e <= 0 || e > it)
402
- throw r(
403
- o.INVALID_INPUT,
254
+ if (!Number.isInteger(e) || e <= 0 || e > z)
255
+ throw i(
256
+ I.INVALID_INPUT,
404
257
  "batchSize must be a positive 32-bit integer",
405
258
  { batchSize: e }
406
259
  );
407
- const i = t + BigInt(e - 1);
408
- if (i > G)
409
- throw r(
410
- o.INVALID_INPUT,
260
+ const g = t + BigInt(e - 1);
261
+ if (g > p)
262
+ throw i(
263
+ I.INVALID_INPUT,
411
264
  "nonce range exceeds u64",
412
265
  { startNonce: t, batchSize: e }
413
266
  );
414
- const g = n ? z : k, I = g(t), c = g(i);
415
- if (I !== c) {
416
- const a = n ? "CBOR length" : "byte-length";
417
- throw r(
418
- o.INVALID_INPUT,
419
- `nonce range crosses ${a} boundary; reduce batch size`,
267
+ const n = s ? F : K, r = n(t), C = n(g);
268
+ if (r !== C) {
269
+ const c = s ? "CBOR length" : "byte-length";
270
+ throw i(
271
+ I.INVALID_INPUT,
272
+ `nonce range crosses ${c} boundary; reduce batch size`,
420
273
  { startNonce: t, batchSize: e }
421
274
  );
422
275
  }
423
- return I;
276
+ return r;
424
277
  }
425
278
  cloneInputs(t) {
426
279
  return t.map((e) => ({ ...e }));
@@ -430,180 +283,180 @@ class It {
430
283
  }
431
284
  validateInputs(t) {
432
285
  if (!Array.isArray(t) || t.length === 0)
433
- throw r(
434
- o.INVALID_INPUT,
286
+ throw i(
287
+ I.INVALID_INPUT,
435
288
  "At least one input is required"
436
289
  );
437
- t.forEach((e, n) => {
438
- if (!st.test(e.txid))
439
- throw r(
440
- o.INVALID_INPUT,
441
- `inputs[${n}].txid must be a 64-character hex`,
442
- { index: n }
290
+ t.forEach((e, s) => {
291
+ if (!U.test(e.txid))
292
+ throw i(
293
+ I.INVALID_INPUT,
294
+ `inputs[${s}].txid must be a 64-character hex`,
295
+ { index: s }
443
296
  );
444
297
  if (!Number.isInteger(e.vout) || e.vout < 0)
445
- throw r(
446
- o.INVALID_INPUT,
447
- `inputs[${n}].vout must be a non-negative integer`,
448
- { index: n }
298
+ throw i(
299
+ I.INVALID_INPUT,
300
+ `inputs[${s}].vout must be a non-negative integer`,
301
+ { index: s }
449
302
  );
450
303
  if (!Number.isInteger(e.amount) || e.amount <= 0)
451
- throw r(
452
- o.INVALID_INPUT,
453
- `inputs[${n}].amount must be a positive integer`,
454
- { index: n }
304
+ throw i(
305
+ I.INVALID_INPUT,
306
+ `inputs[${s}].amount must be a positive integer`,
307
+ { index: s }
455
308
  );
456
- if (typeof e.scriptPubKey != "string" || e.scriptPubKey.length === 0 || e.scriptPubKey.length % 2 !== 0 || !nt.test(e.scriptPubKey))
457
- throw r(
458
- o.INVALID_INPUT,
459
- `inputs[${n}].scriptPubKey must be valid hex`,
460
- { index: n }
309
+ if (typeof e.scriptPubKey != "string" || e.scriptPubKey.length === 0 || e.scriptPubKey.length % 2 !== 0 || !k.test(e.scriptPubKey))
310
+ throw i(
311
+ I.INVALID_INPUT,
312
+ `inputs[${s}].scriptPubKey must be valid hex`,
313
+ { index: s }
461
314
  );
462
315
  });
463
316
  }
464
- validateAddressResult(t, e, n) {
317
+ validateAddressResult(t, e, s) {
465
318
  if (!t.ok) {
466
- const i = t.error ?? "invalid address", I = i.toLowerCase().includes("unsupported") ? o.UNSUPPORTED_ADDRESS_TYPE : o.INVALID_ADDRESS;
467
- throw r(
468
- I,
469
- `outputs[${e}].address is invalid (${i})`,
319
+ const g = t.error ?? "invalid address", r = g.toLowerCase().includes("unsupported") ? I.UNSUPPORTED_ADDRESS_TYPE : I.INVALID_ADDRESS;
320
+ throw i(
321
+ r,
322
+ `outputs[${e}].address is invalid (${g})`,
470
323
  { index: e }
471
324
  );
472
325
  }
473
- if (t.network && t.network !== n && !(t.network === "testnet" && n === "signet"))
474
- throw r(
475
- o.INVALID_ADDRESS,
326
+ if (t.network && t.network !== s && !(t.network === "testnet" && s === "signet"))
327
+ throw i(
328
+ I.INVALID_ADDRESS,
476
329
  `outputs[${e}].address network mismatch`,
477
330
  { index: e }
478
331
  );
479
332
  if (t.addressType && t.addressType !== "p2tr" && t.addressType !== "p2wpkh")
480
- throw r(
481
- o.UNSUPPORTED_ADDRESS_TYPE,
333
+ throw i(
334
+ I.UNSUPPORTED_ADDRESS_TYPE,
482
335
  `outputs[${e}].address uses an unsupported type`,
483
336
  { index: e, addressType: t.addressType }
484
337
  );
485
338
  }
486
339
  async validateOutputs(t) {
487
340
  if (!Array.isArray(t) || t.length === 0)
488
- throw r(
489
- o.INVALID_INPUT,
341
+ throw i(
342
+ I.INVALID_INPUT,
490
343
  "At least one output is required"
491
344
  );
492
- const e = t.filter((g) => g.change).length;
345
+ const e = t.filter((n) => n.change).length;
493
346
  if (e !== 1) {
494
- const g = e === 0 ? o.NO_CHANGE_OUTPUT : o.MULTIPLE_CHANGE_OUTPUTS;
495
- throw r(
496
- g,
347
+ const n = e === 0 ? I.NO_CHANGE_OUTPUT : I.MULTIPLE_CHANGE_OUTPUTS;
348
+ throw i(
349
+ n,
497
350
  "Exactly one change output is required",
498
351
  { changeCount: e }
499
352
  );
500
353
  }
501
- const n = await this.getWasm(), i = V(this.network);
502
- t.forEach((g, I) => {
503
- const c = n.validate_address(g.address, i);
504
- this.validateAddressResult(c, I, i);
505
- const a = gt(c.addressType);
506
- if (g.change) {
507
- if (g.amount !== void 0 && (!Number.isInteger(g.amount) || g.amount < 0))
508
- throw r(
509
- o.INVALID_INPUT,
510
- `outputs[${I}].amount must be a non-negative integer when provided`,
511
- { index: I }
354
+ const s = await this.getWasm(), g = W(this.network);
355
+ t.forEach((n, r) => {
356
+ const C = s.validate_address(n.address, g);
357
+ this.validateAddressResult(C, r, g);
358
+ const c = P(C.addressType);
359
+ if (n.change) {
360
+ if (n.amount !== void 0 && (!Number.isInteger(n.amount) || n.amount < 0))
361
+ throw i(
362
+ I.INVALID_INPUT,
363
+ `outputs[${r}].amount must be a non-negative integer when provided`,
364
+ { index: r }
512
365
  );
513
- } else if (!Number.isInteger(g.amount) || g.amount < a)
514
- throw r(
515
- o.DUST_OUTPUT,
516
- `outputs[${I}].amount must be at least ${a} sats`,
517
- { index: I, addressType: c.addressType }
366
+ } else if (!Number.isInteger(n.amount) || n.amount < c)
367
+ throw i(
368
+ I.DUST_OUTPUT,
369
+ `outputs[${r}].amount must be at least ${c} sats`,
370
+ { index: r, addressType: C.addressType }
518
371
  );
519
372
  });
520
373
  }
521
374
  validateDistribution(t, e) {
522
375
  if (e !== void 0) {
523
376
  if (!Array.isArray(e))
524
- throw r(
525
- o.INVALID_INPUT,
377
+ throw i(
378
+ I.INVALID_INPUT,
526
379
  "distribution must be an array of bigint values"
527
380
  );
528
381
  if (e.length !== t.length)
529
- throw r(
530
- o.INVALID_INPUT,
382
+ throw i(
383
+ I.INVALID_INPUT,
531
384
  "distribution length must match number of outputs",
532
385
  { expected: t.length, actual: e.length }
533
386
  );
534
- return e.map((n, i) => {
535
- if (typeof n != "bigint")
536
- throw r(
537
- o.INVALID_INPUT,
387
+ return e.map((s, g) => {
388
+ if (typeof s != "bigint")
389
+ throw i(
390
+ I.INVALID_INPUT,
538
391
  "distribution values must be bigint",
539
- { index: i }
392
+ { index: g }
540
393
  );
541
- if (n < 0n)
542
- throw r(
543
- o.INVALID_INPUT,
394
+ if (s < 0n)
395
+ throw i(
396
+ I.INVALID_INPUT,
544
397
  "distribution values must be non-negative",
545
- { index: i }
398
+ { index: g }
546
399
  );
547
- return n;
400
+ return s;
548
401
  });
549
402
  }
550
403
  }
551
404
  async buildMiningTemplate(t) {
552
- const { inputs: e, outputs: n, startNonce: i, batchSize: g, distribution: I } = t;
553
- this.validateInputs(e), await this.validateOutputs(n);
554
- const c = this.validateDistribution(n, I), a = Array.isArray(c), A = this.assertNonceRange(i, g, a), C = await this.getWasm();
405
+ const { inputs: e, outputs: s, startNonce: g, batchSize: n, distribution: r } = t;
406
+ this.validateInputs(e), await this.validateOutputs(s);
407
+ const C = this.validateDistribution(s, r), c = Array.isArray(C), d = this.assertNonceRange(g, n, c), A = await this.getWasm();
555
408
  try {
556
- const l = C.build_mining_template(
409
+ const l = A.build_mining_template(
557
410
  this.cloneInputs(e),
558
- this.cloneOutputs(n),
559
- V(this.network),
411
+ this.cloneOutputs(s),
412
+ W(this.network),
560
413
  BigInt(this.satsPerVbyte),
561
- i,
562
414
  g,
563
- c ?? null
415
+ n,
416
+ C ?? null
564
417
  );
565
418
  if (!l || !(l.prefix instanceof Uint8Array) || !(l.suffix instanceof Uint8Array))
566
- throw r(
567
- o.WORKER_ERROR,
419
+ throw i(
420
+ I.WORKER_ERROR,
568
421
  "WASM returned an invalid mining template"
569
422
  );
570
- const u = l.useCborNonce ?? a;
571
- return { ...l, nonceLength: A, useCborNonce: u };
423
+ const u = l.useCborNonce ?? c;
424
+ return { ...l, nonceLength: d, useCborNonce: u };
572
425
  } catch (l) {
573
- throw J(l, "build_mining_template", {
574
- startNonce: i,
575
- batchSize: g,
576
- distribution: c
426
+ throw w(l, "build_mining_template", {
427
+ startNonce: g,
428
+ batchSize: n,
429
+ distribution: C
577
430
  });
578
431
  }
579
432
  }
580
433
  async buildPsbt(t) {
581
- const { inputs: e, outputs: n, nonce: i, distribution: g } = t;
582
- this.validateInputs(e), await this.validateOutputs(n);
583
- const I = this.validateDistribution(n, g);
584
- if (i < 0n || i > G)
585
- throw r(
586
- o.INVALID_INPUT,
434
+ const { inputs: e, outputs: s, nonce: g, distribution: n } = t;
435
+ this.validateInputs(e), await this.validateOutputs(s);
436
+ const r = this.validateDistribution(s, n);
437
+ if (g < 0n || g > p)
438
+ throw i(
439
+ I.INVALID_INPUT,
587
440
  "nonce must be between 0 and 2^64 - 1",
588
- { nonce: i }
441
+ { nonce: g }
589
442
  );
590
- const c = await this.getWasm();
443
+ const C = await this.getWasm();
591
444
  try {
592
- return c.build_psbt(
445
+ return C.build_psbt(
593
446
  this.cloneInputs(e),
594
- this.cloneOutputs(n),
595
- V(this.network),
447
+ this.cloneOutputs(s),
448
+ W(this.network),
596
449
  BigInt(this.satsPerVbyte),
597
- i,
598
- I ?? null
450
+ g,
451
+ r ?? null
599
452
  );
600
- } catch (a) {
601
- throw J(a, "build_psbt", { nonce: i, distribution: I });
453
+ } catch (c) {
454
+ throw w(c, "build_psbt", { nonce: g, distribution: r });
602
455
  }
603
456
  }
604
457
  }
605
- const H = 1, U = 32;
606
- class rt {
458
+ const X = 1, y = 32;
459
+ class E {
607
460
  options;
608
461
  builder;
609
462
  coordinator = null;
@@ -611,7 +464,7 @@ class rt {
611
464
  state = "idle";
612
465
  stopRequested = !1;
613
466
  constructor(t) {
614
- this.options = this.validateOptions(t), this.builder = new It(t.network, t.satsPerVbyte), this.listeners = {
467
+ this.options = this.validateOptions(t), this.builder = new O(t.network, t.satsPerVbyte), this.listeners = {
615
468
  progress: /* @__PURE__ */ new Set(),
616
469
  found: /* @__PURE__ */ new Set(),
617
470
  error: /* @__PURE__ */ new Set(),
@@ -625,24 +478,24 @@ class rt {
625
478
  this.listeners[t].delete(e);
626
479
  }
627
480
  emit(t, e) {
628
- this.listeners[t].forEach((n) => n(e));
481
+ this.listeners[t].forEach((s) => s(e));
629
482
  }
630
483
  validateOptions(t) {
631
484
  if (!Number.isInteger(t.batchSize) || t.batchSize <= 0)
632
- throw r(
633
- o.INVALID_INPUT,
485
+ throw i(
486
+ I.INVALID_INPUT,
634
487
  "batchSize must be a positive integer",
635
488
  { field: "batchSize" }
636
489
  );
637
490
  if (!Number.isInteger(t.workerThreads) || t.workerThreads <= 0)
638
- throw r(
639
- o.INVALID_INPUT,
491
+ throw i(
492
+ I.INVALID_INPUT,
640
493
  "workerThreads must be a positive integer",
641
494
  { field: "workerThreads" }
642
495
  );
643
496
  if (!Number.isFinite(t.satsPerVbyte) || t.satsPerVbyte <= 0)
644
- throw r(
645
- o.INVALID_INPUT,
497
+ throw i(
498
+ I.INVALID_INPUT,
646
499
  "satsPerVbyte must be a positive number",
647
500
  { field: "satsPerVbyte" }
648
501
  );
@@ -658,105 +511,105 @@ class rt {
658
511
  }
659
512
  async mineTransaction(t) {
660
513
  if (this.state === "running")
661
- throw r(
662
- o.INVALID_INPUT,
514
+ throw i(
515
+ I.INVALID_INPUT,
663
516
  "Mining is already in progress"
664
517
  );
665
- if (!Number.isInteger(t.targetZeros) || t.targetZeros < H || t.targetZeros > U)
666
- throw r(
667
- o.INVALID_INPUT,
668
- `targetZeros must be an integer between ${H} and ${U}`,
518
+ if (!Number.isInteger(t.targetZeros) || t.targetZeros < X || t.targetZeros > y)
519
+ throw i(
520
+ I.INVALID_INPUT,
521
+ `targetZeros must be an integer between ${X} and ${y}`,
669
522
  { targetZeros: t.targetZeros }
670
523
  );
671
- const e = t.startNonce ?? 0n, n = t.batchSize ?? this.options.batchSize;
672
- if (!Number.isInteger(n) || n <= 0)
673
- throw r(
674
- o.INVALID_INPUT,
524
+ const e = t.startNonce ?? 0n, s = t.batchSize ?? this.options.batchSize;
525
+ if (!Number.isInteger(s) || s <= 0)
526
+ throw i(
527
+ I.INVALID_INPUT,
675
528
  "batchSize must be a positive integer",
676
- { batchSize: n }
529
+ { batchSize: s }
677
530
  );
678
531
  if (t.signal?.aborted)
679
- throw r(
680
- o.MINING_ABORTED,
532
+ throw i(
533
+ I.MINING_ABORTED,
681
534
  "Abort signal already triggered"
682
535
  );
683
- const i = t.distribution, g = !!(i && i.length > 0);
684
- let I = n;
536
+ const g = t.distribution, n = !!(g && g.length > 0);
537
+ let r = s;
685
538
  try {
686
- const [l] = g ? et(e, n) : tt(e, n);
539
+ const [l] = n ? v(e, s) : f(e, s);
687
540
  if (!l)
688
- throw r(
689
- o.INVALID_INPUT,
541
+ throw i(
542
+ I.INVALID_INPUT,
690
543
  "Failed to compute nonce segments",
691
- { startNonce: e, batchSize: n }
544
+ { startNonce: e, batchSize: s }
692
545
  );
693
- I = l.size;
546
+ r = l.size;
694
547
  } catch (l) {
695
- throw r(
696
- o.INVALID_INPUT,
548
+ throw i(
549
+ I.INVALID_INPUT,
697
550
  l instanceof Error ? l.message : String(l),
698
- { startNonce: e, batchSize: n }
551
+ { startNonce: e, batchSize: s }
699
552
  );
700
553
  }
701
- const c = await this.selectBackend(), a = await this.builder.buildMiningTemplate({
554
+ const C = await this.selectBackend(), c = await this.builder.buildMiningTemplate({
702
555
  inputs: t.inputs,
703
556
  outputs: t.outputs,
704
557
  startNonce: e,
705
- batchSize: I,
706
- distribution: i
707
- }), A = this.options.network === "signet" ? "testnet" : this.options.network, C = new O({
708
- mode: c,
709
- batchSize: n,
558
+ batchSize: r,
559
+ distribution: g
560
+ }), d = this.options.network === "signet" ? "testnet" : this.options.network, A = new T({
561
+ mode: C,
562
+ batchSize: s,
710
563
  workerThreads: this.options.workerThreads
711
564
  });
712
- return this.coordinator = C, this.state = "running", this.stopRequested = !1, new Promise((l, u) => {
565
+ return this.coordinator = A, this.state = "running", this.stopRequested = !1, new Promise((l, u) => {
713
566
  let h = !1;
714
- const y = () => {
715
- this.clearCoordinatorHandlers(C, b), this.coordinator === C && (this.coordinator = null), this.state = "idle", this.stopRequested = !1;
716
- }, E = (d) => {
717
- h || (h = !0, y(), l(d));
718
- }, Z = (d) => {
567
+ const G = () => {
568
+ this.clearCoordinatorHandlers(A, b), this.coordinator === A && (this.coordinator = null), this.state = "idle", this.stopRequested = !1;
569
+ }, J = (a) => {
570
+ h || (h = !0, G(), l(a));
571
+ }, Z = (a) => {
719
572
  if (h) return;
720
573
  h = !0;
721
- const m = v(d);
722
- y(), this.emit("error", m), u(m);
574
+ const m = Y(a);
575
+ G(), this.emit("error", m), u(m);
723
576
  }, b = {};
724
- b.progress = (d) => {
725
- this.emit("progress", d);
726
- }, b.found = async (d) => {
577
+ b.progress = (a) => {
578
+ this.emit("progress", a);
579
+ }, b.found = async (a) => {
727
580
  try {
728
581
  const m = await this.builder.buildPsbt({
729
582
  inputs: t.inputs,
730
583
  outputs: t.outputs,
731
- nonce: d.nonce,
732
- distribution: i
733
- }), f = { ...d, psbt: m };
734
- this.emit("found", f), E(f);
584
+ nonce: a.nonce,
585
+ distribution: g
586
+ }), V = { ...a, psbt: m };
587
+ this.emit("found", V), J(V);
735
588
  } catch (m) {
736
589
  Z(m);
737
590
  }
738
- }, b.error = (d) => {
739
- Z(d);
591
+ }, b.error = (a) => {
592
+ Z(a);
740
593
  }, b.stopped = () => {
741
594
  if (h) return;
742
- const d = this.stopRequested ? r(
743
- o.MINING_ABORTED,
595
+ const a = this.stopRequested ? i(
596
+ I.MINING_ABORTED,
744
597
  "Mining stopped by caller"
745
- ) : r(
746
- o.MINING_ABORTED,
598
+ ) : i(
599
+ I.MINING_ABORTED,
747
600
  "Mining halted"
748
601
  );
749
- this.emit("stopped", void 0), Z(d);
750
- }, C.on("progress", b.progress), C.on("found", b.found), C.on("error", b.error), C.on("stopped", b.stopped), C.start({
602
+ this.emit("stopped", void 0), Z(a);
603
+ }, A.on("progress", b.progress), A.on("found", b.found), A.on("error", b.error), A.on("stopped", b.stopped), A.start({
751
604
  inputs: t.inputs,
752
605
  outputs: t.outputs,
753
- network: A,
606
+ network: d,
754
607
  satsPerVbyte: this.options.satsPerVbyte,
755
- template: a,
608
+ template: c,
756
609
  targetZeros: t.targetZeros,
757
610
  startNonce: e,
758
611
  signal: t.signal,
759
- distribution: i
612
+ distribution: g
760
613
  }).catch(Z);
761
614
  });
762
615
  }
@@ -771,12 +624,12 @@ class rt {
771
624
  }
772
625
  }
773
626
  export {
774
- O as MiningCoordinator,
775
- It as TransactionBuilder,
776
- rt as ZeldMiner,
777
- w as ZeldMinerError,
778
- o as ZeldMinerErrorCode,
779
- r as createMinerError,
780
- v as toZeldMinerError
627
+ T as MiningCoordinator,
628
+ O as TransactionBuilder,
629
+ E as ZeldMiner,
630
+ N as ZeldMinerError,
631
+ I as ZeldMinerErrorCode,
632
+ i as createMinerError,
633
+ Y as toZeldMinerError
781
634
  };
782
635
  //# sourceMappingURL=index.js.map