zeldwallet 0.1.4 → 0.1.6

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/bin/setup.js CHANGED
File without changes
@@ -5,7 +5,7 @@
5
5
  "Ouziel Slama <ouziel@gmail.com>"
6
6
  ],
7
7
  "description": "WASM bindings for the Zeldhash miner with optional WebGPU acceleration",
8
- "version": "0.2.0",
8
+ "version": "0.2.2",
9
9
  "license": "MIT",
10
10
  "repository": {
11
11
  "type": "git",
package/dist/worker.js CHANGED
@@ -43,21 +43,21 @@ const R = (1n << 64n) - 1n, K = (t) => {
43
43
  const n = t + BigInt(e - 1);
44
44
  if (n > R)
45
45
  throw new Error("nonce range exceeds u64");
46
- const r = [];
47
- let i = t;
48
- for (; i <= n; ) {
49
- const o = K(i), s = X(o), c = n < s ? n : s, f = c - i + 1n;
50
- if (f > BigInt(Number.MAX_SAFE_INTEGER))
46
+ const i = [];
47
+ let r = t;
48
+ for (; r <= n; ) {
49
+ const s = K(r), o = X(s), a = n < o ? n : o, l = a - r + 1n;
50
+ if (l > BigInt(Number.MAX_SAFE_INTEGER))
51
51
  throw new Error("segment size exceeds safe integer range");
52
- if (r.push({
53
- start: i,
54
- size: Number(f),
55
- nonceLength: o
56
- }), c === n)
52
+ if (i.push({
53
+ start: r,
54
+ size: Number(l),
55
+ nonceLength: s
56
+ }), a === n)
57
57
  break;
58
- i = c + 1n;
58
+ r = a + 1n;
59
59
  }
60
- return r;
60
+ return i;
61
61
  }, J = (t, e) => {
62
62
  if (t < 0n)
63
63
  throw new Error("startNonce must be non-negative");
@@ -66,48 +66,55 @@ const R = (1n << 64n) - 1n, K = (t) => {
66
66
  const n = t + BigInt(e - 1);
67
67
  if (n > R)
68
68
  throw new Error("nonce range exceeds u64");
69
- const r = [];
70
- let i = t;
71
- for (; i <= n; ) {
72
- const o = H(i), s = j(o), c = n < s ? n : s, f = c - i + 1n;
73
- if (f > BigInt(Number.MAX_SAFE_INTEGER))
69
+ const i = [];
70
+ let r = t;
71
+ for (; r <= n; ) {
72
+ const s = H(r), o = j(s), a = n < o ? n : o, l = a - r + 1n;
73
+ if (l > BigInt(Number.MAX_SAFE_INTEGER))
74
74
  throw new Error("segment size exceeds safe integer range");
75
- if (r.push({
76
- start: i,
77
- size: Number(f),
78
- nonceLength: o
79
- }), c === n)
75
+ if (i.push({
76
+ start: r,
77
+ size: Number(l),
78
+ nonceLength: s
79
+ }), a === n)
80
80
  break;
81
- i = c + 1n;
81
+ r = a + 1n;
82
82
  }
83
- return r;
83
+ return i;
84
84
  }, Q = {};
85
- let U = null, d = null, x = !1;
85
+ if (typeof globalThis.__ZELDMINER_WASM_BASE__ > "u")
86
+ try {
87
+ const t = typeof window < "u" && window.location?.origin ? window.location.origin : typeof self < "u" && self.location?.origin ? self.location.origin : "http://localhost";
88
+ globalThis.__ZELDMINER_WASM_BASE__ = new URL("/wasm/", t).href;
89
+ } catch {
90
+ globalThis.__ZELDMINER_WASM_BASE__ = "/wasm/";
91
+ }
92
+ let y = null, d = null, O = !1;
86
93
  const Z = () => {
87
- if (x) return;
88
- x = !0;
94
+ if (O) return;
95
+ O = !0;
89
96
  const t = globalThis.GPUAdapter?.prototype, e = t?.requestDevice;
90
- !t || typeof e != "function" || (t.requestDevice = function(r) {
91
- if (r?.requiredLimits && typeof this.limits == "object") {
92
- const i = r.requiredLimits, o = this.limits;
93
- for (const s of Object.keys(i))
94
- (!(s in o) || o[s] === void 0) && delete i[s];
97
+ !t || typeof e != "function" || (t.requestDevice = function(i) {
98
+ if (i?.requiredLimits && typeof this.limits == "object") {
99
+ const r = i.requiredLimits, s = this.limits;
100
+ for (const o of Object.keys(r))
101
+ (!(o in s) || s[o] === void 0) && delete r[o];
95
102
  }
96
- return e.call(this, r);
103
+ return e.call(this, i);
97
104
  });
98
- }, y = (t) => t.endsWith("/") ? t : `${t}/`, I = (t) => {
105
+ }, U = (t) => t.endsWith("/") ? t : `${t}/`, A = (t) => {
99
106
  const e = t.trim();
100
- return e && (typeof window < "u" && typeof window.location?.origin == "string" ? y(new URL(e, window.location.origin).href) : y(new URL(e, import.meta.url).href));
107
+ return e && (typeof window < "u" && typeof window.location?.origin == "string" ? U(new URL(e, window.location.origin).href) : U(new URL(e, import.meta.url).href));
101
108
  }, tt = () => {
102
109
  const t = globalThis.__ZELDMINER_WASM_BASE__;
103
110
  if (typeof t == "string" && t.trim())
104
- return I(t);
111
+ return A(t);
105
112
  const e = Q?.VITE_ZELDMINER_WASM_BASE;
106
113
  if (typeof e == "string" && e.trim())
107
- return I(e);
108
- const n = "/";
109
- return n.trim() ? I(`${y(n.trim())}wasm/`) : I("/wasm/");
110
- }, M = tt(), W = `${M}zeldhash_miner_wasm.js`, et = `${M}zeldhash_miner_wasm_bg.wasm`, z = (t) => t instanceof Error ? t.message : String(t), nt = async () => {
114
+ return A(e);
115
+ const n = "./";
116
+ return n.trim() ? A(`${U(n.trim())}wasm/`) : A("/wasm/");
117
+ }, z = tt(), W = `${z}zeldhash_miner_wasm.js`, et = `${z}zeldhash_miner_wasm_bg.wasm`, x = (t) => t instanceof Error ? t.message : String(t), nt = async () => {
111
118
  Z();
112
119
  let t;
113
120
  try {
@@ -115,20 +122,20 @@ const Z = () => {
115
122
  /* @vite-ignore */
116
123
  W
117
124
  );
118
- } catch (r) {
125
+ } catch (i) {
119
126
  throw new Error(
120
- `Failed to import WASM bundle (${W}). Did you run ./scripts/build-wasm.sh? (${z(r)})`
127
+ `Failed to import WASM bundle (${W}). Did you run ./scripts/build-wasm.sh? (${x(i)})`
121
128
  );
122
129
  }
123
130
  const e = t.default;
124
131
  if (typeof e != "function")
125
132
  throw new Error("WASM init function is missing from the bundle.");
126
133
  try {
127
- const r = new URL(et, import.meta.url);
128
- await e({ module_or_path: r });
129
- } catch (r) {
134
+ const i = new URL(et, import.meta.url);
135
+ await e({ module_or_path: i });
136
+ } catch (i) {
130
137
  throw new Error(
131
- `Failed to initialize WASM bundle: ${z(r)}`
138
+ `Failed to initialize WASM bundle: ${x(i)}`
132
139
  );
133
140
  }
134
141
  const n = t;
@@ -137,69 +144,77 @@ const Z = () => {
137
144
  } catch {
138
145
  }
139
146
  return n;
140
- }, rt = async () => U || (d || (d = nt().then((t) => (U = t, t)).catch((t) => {
147
+ }, it = async () => y || (d || (d = nt().then((t) => (y = t, t)).catch((t) => {
141
148
  throw d = null, t;
142
- })), d), B = self, it = B.name ?? void 0, N = (t) => t instanceof Error ? t.message : String(t);
143
- let A = "cpu", w = null;
144
- const E = (t) => {
145
- B.postMessage({ ...t, workerId: it });
146
- }, m = (t, e = u.WORKER_ERROR, n) => {
147
- E({ type: "error", message: t, code: e, details: n });
148
- }, st = (t) => typeof t == "object" && t !== null && "ok" in t, ot = (t) => typeof t == "object" && t !== null && "nonce" in t && "txid" in t, ct = (t) => {
149
+ })), d);
150
+ if (typeof globalThis.__ZELDMINER_WASM_BASE__ > "u")
151
+ try {
152
+ const t = typeof self < "u" && self.location?.origin ? self.location.origin : "http://localhost";
153
+ globalThis.__ZELDMINER_WASM_BASE__ = new URL("/wasm/", t).href;
154
+ } catch {
155
+ globalThis.__ZELDMINER_WASM_BASE__ = "/wasm/";
156
+ }
157
+ const B = self, rt = B.name ?? void 0, I = (t) => t instanceof Error ? t.message : String(t);
158
+ let N = "cpu", b = null;
159
+ const w = (t) => {
160
+ B.postMessage({ ...t, workerId: rt });
161
+ }, h = (t, e = u.WORKER_ERROR, n) => {
162
+ w({ type: "error", message: t, code: e, details: n });
163
+ }, ot = (t) => typeof t == "object" && t !== null && "ok" in t, st = (t) => typeof t == "object" && t !== null && "nonce" in t && "txid" in t, at = (t) => {
149
164
  const e = BigInt(Number.MAX_SAFE_INTEGER);
150
165
  return t > e ? Number.MAX_SAFE_INTEGER : t < -e ? -Number.MAX_SAFE_INTEGER : Number(t);
151
- }, at = (t) => ({
166
+ }, ct = (t) => ({
152
167
  nonceLength: t.nonceLength,
153
168
  prefix: new Uint8Array(t.prefix),
154
169
  suffix: new Uint8Array(t.suffix),
155
170
  useCborNonce: t.useCborNonce
156
171
  }), F = (t, e) => {
157
- t.set(e.nonceLength, at(e));
158
- }, ut = async (t, e, n, r, i) => {
159
- const o = t.get(r.nonceLength);
160
- if (o)
161
- return o;
162
- const s = e.build_mining_template(
172
+ t.set(e.nonceLength, ct(e));
173
+ }, ut = async (t, e, n, i, r) => {
174
+ const s = t.get(i.nonceLength);
175
+ if (s)
176
+ return s;
177
+ const o = e.build_mining_template(
163
178
  n.inputs,
164
179
  n.outputs,
165
- i,
180
+ r,
166
181
  BigInt(n.satsPerVbyte),
167
- r.start,
168
- r.size,
182
+ i.start,
183
+ i.size,
169
184
  n.distribution ?? null
170
- ), c = {
171
- ...s,
172
- nonceLength: r.nonceLength,
173
- useCborNonce: s.useCborNonce ?? n.useCborNonce
185
+ ), a = {
186
+ ...o,
187
+ nonceLength: i.nonceLength,
188
+ useCborNonce: o.useCborNonce ?? n.useCborNonce
174
189
  };
175
- return F(t, c), c;
176
- }, ft = async (t, e, n, r, i, o, s, c) => {
190
+ return F(t, a), a;
191
+ }, lt = async (t, e, n, i, r, s, o, a) => {
177
192
  if (e === "gpu") {
178
193
  if (!t.mine_batch_gpu)
179
194
  throw new Error("GPU mining requested but mine_batch_gpu is unavailable");
180
195
  return t.mine_batch_gpu(
181
196
  n,
182
- r,
183
197
  i,
184
- o,
198
+ r,
185
199
  s,
186
- c
200
+ o,
201
+ a
187
202
  );
188
203
  }
189
204
  return t.mine_batch_wasm(
190
205
  n,
191
- r,
192
206
  i,
193
- o,
207
+ r,
194
208
  s,
195
- c
209
+ o,
210
+ a
196
211
  );
197
- }, lt = async (t, e) => {
212
+ }, ft = async (t, e) => {
198
213
  let n;
199
214
  try {
200
- if (n = await rt(), A === "gpu") {
215
+ if (n = await it(), N === "gpu") {
201
216
  if (!n.mine_batch_gpu) {
202
- m(
217
+ h(
203
218
  "GPU mining requested but mine_batch_gpu is unavailable",
204
219
  u.WEBGPU_NOT_AVAILABLE
205
220
  );
@@ -207,27 +222,27 @@ const E = (t) => {
207
222
  }
208
223
  n.init_gpu && await n.init_gpu();
209
224
  }
210
- } catch (g) {
211
- const _ = N(g), b = A === "gpu" ? u.WEBGPU_NOT_AVAILABLE : u.WORKER_ERROR;
212
- m(`Failed to initialize WASM: ${_}`, b);
225
+ } catch (m) {
226
+ const g = I(m), E = N === "gpu" ? u.WEBGPU_NOT_AVAILABLE : u.WORKER_ERROR;
227
+ h(`Failed to initialize WASM: ${g}`, E);
213
228
  return;
214
229
  }
215
- const r = /* @__PURE__ */ new Map(), i = t.template.useCborNonce ?? !!(t.distribution && t.distribution.length > 0);
216
- F(r, { ...t.template, useCborNonce: i });
217
- const o = t.nonceStep ?? BigInt(t.batchSize), s = t.network === "signet" ? "testnet" : t.network;
218
- let c = t.startNonce, f = 0n;
230
+ const i = /* @__PURE__ */ new Map(), r = t.template.useCborNonce ?? !!(t.distribution && t.distribution.length > 0);
231
+ F(i, { ...t.template, useCborNonce: r });
232
+ const s = t.nonceStep ?? BigInt(t.batchSize), o = t.network === "signet" ? "testnet" : t.network;
233
+ let a = t.startNonce, l = 0n;
219
234
  const G = performance.now();
220
235
  for (; !e.signal.aborted; ) {
221
- const g = c;
222
- let _ = t.batchSize, b = 0n;
223
- for (; _ > 0 && !e.signal.aborted; ) {
224
- const P = g + b;
225
- let a;
236
+ const m = a;
237
+ let g = t.batchSize, E = 0n;
238
+ for (; g > 0 && !e.signal.aborted; ) {
239
+ const D = m + E;
240
+ let c;
226
241
  try {
227
- a = (i ? J(P, _) : Y(P, _))[0];
228
- } catch (h) {
229
- m(
230
- `Invalid nonce range: ${N(h)}`,
242
+ c = (r ? J(D, g) : Y(D, g))[0];
243
+ } catch (_) {
244
+ h(
245
+ `Invalid nonce range: ${I(_)}`,
231
246
  u.INVALID_INPUT
232
247
  ), e.abort();
233
248
  return;
@@ -235,102 +250,102 @@ const E = (t) => {
235
250
  let S;
236
251
  try {
237
252
  S = await ut(
238
- r,
253
+ i,
239
254
  n,
240
255
  {
241
256
  inputs: t.inputs,
242
257
  outputs: t.outputs,
243
258
  satsPerVbyte: t.satsPerVbyte,
244
259
  distribution: t.distribution,
245
- useCborNonce: i
260
+ useCborNonce: r
246
261
  },
247
- a,
248
- s
262
+ c,
263
+ o
249
264
  );
250
- } catch (h) {
251
- m(
252
- `Failed to build mining template: ${N(h)}`,
265
+ } catch (_) {
266
+ h(
267
+ `Failed to build mining template: ${I(_)}`,
253
268
  u.WORKER_ERROR
254
269
  ), e.abort();
255
270
  return;
256
271
  }
257
- let l;
272
+ let f;
258
273
  const k = performance.now();
259
274
  try {
260
- l = await ft(
275
+ f = await lt(
261
276
  n,
262
- A,
277
+ N,
263
278
  S.prefix,
264
279
  S.suffix,
265
- a.start,
266
- a.size,
280
+ c.start,
281
+ c.size,
267
282
  t.targetZeros,
268
- i
283
+ r
269
284
  );
270
- } catch (h) {
271
- const p = N(h);
272
- m(
285
+ } catch (_) {
286
+ const p = I(_);
287
+ h(
273
288
  `Batch mining failed: ${p}`,
274
289
  u.WORKER_ERROR
275
290
  ), e.abort();
276
291
  return;
277
292
  }
278
- const O = performance.now() - k;
279
- if (st(l)) {
280
- if (!l.ok) {
281
- m(
282
- l.error ?? "Validation failed",
293
+ const P = performance.now() - k;
294
+ if (ot(f)) {
295
+ if (!f.ok) {
296
+ h(
297
+ f.error ?? "Validation failed",
283
298
  u.INVALID_INPUT
284
299
  ), e.abort();
285
300
  return;
286
301
  }
287
- } else if (ot(l)) {
288
- const h = f + b, p = BigInt(l.nonce) - a.start + 1n, T = h + p, L = performance.now() - G, D = L > 0 ? ct(T) / (L / 1e3) : 0, q = a.start + p - 1n, C = {
302
+ } else if (st(f)) {
303
+ const _ = l + E, p = BigInt(f.nonce) - c.start + 1n, L = _ + p, T = performance.now() - G, M = T > 0 ? at(L) / (T / 1e3) : 0, q = c.start + p - 1n, C = {
289
304
  psbt: "",
290
- txid: l.txid,
291
- nonce: BigInt(l.nonce),
292
- attempts: T,
293
- duration: L,
294
- hashRate: D
305
+ txid: f.txid,
306
+ nonce: BigInt(f.nonce),
307
+ attempts: L,
308
+ duration: T,
309
+ hashRate: M
295
310
  };
296
- E({
311
+ w({
297
312
  type: "found",
298
313
  result: C,
299
- hashesProcessed: T,
300
- hashRate: D,
314
+ hashesProcessed: L,
315
+ hashRate: M,
301
316
  lastNonce: q
302
317
  }), e.abort();
303
318
  return;
304
319
  }
305
- f += BigInt(a.size), b += BigInt(a.size), _ -= a.size;
306
- const v = O > 0 ? a.size / (O / 1e3) : a.size, $ = a.start + BigInt(a.size) - 1n;
307
- E({ type: "progress", hashesProcessed: f, hashRate: v, lastNonce: $ });
320
+ l += BigInt(c.size), E += BigInt(c.size), g -= c.size;
321
+ const v = P > 0 ? c.size / (P / 1e3) : c.size, $ = c.start + BigInt(c.size) - 1n;
322
+ w({ type: "progress", hashesProcessed: l, hashRate: v, lastNonce: $ });
308
323
  }
309
324
  if (e.signal.aborted)
310
325
  break;
311
- const V = g + BigInt(t.batchSize) - 1n;
312
- E({ type: "batch_complete", lastNonce: V }), c = g + o;
326
+ const V = m + BigInt(t.batchSize) - 1n;
327
+ w({ type: "batch_complete", lastNonce: V }), a = m + s;
313
328
  }
314
- }, ht = (t) => {
329
+ }, _t = (t) => {
315
330
  const e = new AbortController();
316
- w?.abort(), w = e, lt(t, e).finally(() => {
317
- w === e && (w = null);
331
+ b?.abort(), b = e, ft(t, e).finally(() => {
332
+ b === e && (b = null);
318
333
  });
319
334
  };
320
335
  B.addEventListener("message", (t) => {
321
336
  const e = t.data;
322
337
  switch (e.type) {
323
338
  case "init":
324
- A = e.mode, E({ type: "ready" });
339
+ N = e.mode, w({ type: "ready" });
325
340
  break;
326
341
  case "mine":
327
- ht(e);
342
+ _t(e);
328
343
  break;
329
344
  case "stop":
330
- w?.abort();
345
+ b?.abort();
331
346
  break;
332
347
  default:
333
- m(
348
+ h(
334
349
  `Unknown message type: ${e.type}`,
335
350
  u.WORKER_ERROR
336
351
  );
@@ -15476,7 +15476,7 @@ class UC {
15476
15476
  `, document.head.appendChild(t);
15477
15477
  }
15478
15478
  }
15479
- const OC = "0.1.4", zC = {
15479
+ const OC = "0.1.6", zC = {
15480
15480
  version: OC
15481
15481
  }, LC = zC.version, gg = ["getInfo", "getAddresses", "signMessage", "signPsbt"], qC = ["WBIP004", "WBIP005", "WBIP006"];
15482
15482
  class KC {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "overrides": {
4
4
  "valibot": "^1.2.0"
5
5
  },
6
- "version": "0.1.4",
6
+ "version": "0.1.6",
7
7
  "description": "Lightweight JavaScript library for creating a Bitcoin wallet directly in the browser",
8
8
  "type": "module",
9
9
  "main": "./dist/zeldwallet.es.js",
@@ -25,7 +25,7 @@
25
25
  "peerDependencies": {
26
26
  "react": "^18.2.0 || ^19.0.0",
27
27
  "react-dom": "^18.2.0 || ^19.0.0",
28
- "zeldhash-miner": "^0.2.0"
28
+ "zeldhash-miner": "0.2.2"
29
29
  },
30
30
  "peerDependenciesMeta": {
31
31
  "react": {
@@ -39,7 +39,7 @@
39
39
  }
40
40
  },
41
41
  "scripts": {
42
- "copy-wasm": "mkdir -p public/wasm && cp node_modules/zeldhash-miner/wasm/* public/wasm/ && cp node_modules/zeldhash-miner/dist/worker.js public/ && node -e \"const fs=require('fs');const p='public/worker.js';if(fs.existsSync(p)){const c=fs.readFileSync(p,'utf8').replace(/\\n?\\/\\/\\# sourceMappingURL=worker\\.js\\.map\\s*$/,'');fs.writeFileSync(p,c);}\"",
42
+ "copy-wasm": "node -e \"const fs=require('fs'),path=require('path');const minerPath='node_modules/zeldhash-miner';if(!fs.existsSync(minerPath)){console.log('zeldhash-miner not installed, skipping wasm copy');process.exit(0);}fs.mkdirSync('public/wasm',{recursive:true});const wasmDir=path.join(minerPath,'wasm');if(fs.existsSync(wasmDir)){fs.readdirSync(wasmDir).forEach(f=>fs.copyFileSync(path.join(wasmDir,f),path.join('public/wasm',f)));}const workerSrc=path.join(minerPath,'dist/worker.js');if(fs.existsSync(workerSrc)){let c=fs.readFileSync(workerSrc,'utf8').replace(/\\n?\\/\\/# sourceMappingURL=worker\\.js\\.map\\s*$/,'');fs.writeFileSync('public/worker.js',c);}\"",
43
43
  "dev": "npm run copy-wasm && vite",
44
44
  "build": "npm run copy-wasm && npm run lint && tsc && vite build",
45
45
  "preview": "vite preview",
@@ -100,6 +100,7 @@
100
100
  "vite-plugin-dts": "^4.5.4",
101
101
  "vite-plugin-top-level-await": "^1.6.0",
102
102
  "vite-plugin-wasm": "^3.5.0",
103
- "vitest": "^4.0.16"
103
+ "vitest": "^4.0.16",
104
+ "zeldhash-miner": "^0.2.2"
104
105
  }
105
106
  }