@leofcoin/workers 1.5.36 → 1.5.38

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.
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface, p as proto, a as proto$1, i as index, b as index$1, E as EasyWorker } from './worker-CZqErLI7.js';
1
+ import { F as FormatInterface, p as proto, a as proto$1, i as index, b as index$1, E as EasyWorker } from './worker-DMCj1e6z.js';
2
2
 
3
3
  class ValidatorMessage extends FormatInterface {
4
4
  get messageName() {
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface, p as proto, a as proto$1, i as index, b as index$1, E as EasyWorker } from './worker-CZqErLI7.js';
1
+ import { F as FormatInterface, p as proto, a as proto$1, i as index, b as index$1, E as EasyWorker } from './worker-DMCj1e6z.js';
2
2
 
3
3
  class ValidatorMessage extends FormatInterface {
4
4
  get messageName() {
@@ -0,0 +1,374 @@
1
+ function bytes(b, ...lengths) {
2
+ if (!(b instanceof Uint8Array))
3
+ throw new Error('Expected Uint8Array');
4
+ if (lengths.length > 0 && !lengths.includes(b.length))
5
+ throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
6
+ }
7
+ function exists(instance, checkFinished = true) {
8
+ if (instance.destroyed)
9
+ throw new Error('Hash instance has been destroyed');
10
+ if (checkFinished && instance.finished)
11
+ throw new Error('Hash#digest() has already been called');
12
+ }
13
+ function output(out, instance) {
14
+ bytes(out);
15
+ const min = instance.outputLen;
16
+ if (out.length < min) {
17
+ throw new Error(`digestInto() expects output buffer of length at least ${min}`);
18
+ }
19
+ }
20
+
21
+ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
22
+ // We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
23
+ // node.js versions earlier than v19 don't declare it in global scope.
24
+ // For node.js, package.json#exports field mapping rewrites import
25
+ // from `crypto` to `cryptoNode`, which imports native module.
26
+ // Makes the utils un-importable in browsers without a bundler.
27
+ // Once node.js 18 is deprecated, we can just drop the import.
28
+ const u8a = (a) => a instanceof Uint8Array;
29
+ // Cast array to view
30
+ const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
31
+ // The rotate right (circular right shift) operation for uint32
32
+ const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
33
+ // big-endian hardware is rare. Just in case someone still decides to run hashes:
34
+ // early-throw an error because we don't support BE yet.
35
+ const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
36
+ if (!isLE)
37
+ throw new Error('Non little-endian hardware is not supported');
38
+ /**
39
+ * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
40
+ */
41
+ function utf8ToBytes(str) {
42
+ if (typeof str !== 'string')
43
+ throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
44
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
45
+ }
46
+ /**
47
+ * Normalizes (non-hex) string or Uint8Array to Uint8Array.
48
+ * Warning: when Uint8Array is passed, it would NOT get copied.
49
+ * Keep in mind for future mutable operations.
50
+ */
51
+ function toBytes(data) {
52
+ if (typeof data === 'string')
53
+ data = utf8ToBytes(data);
54
+ if (!u8a(data))
55
+ throw new Error(`expected Uint8Array, got ${typeof data}`);
56
+ return data;
57
+ }
58
+ // For runtime check if class implements interface
59
+ class Hash {
60
+ // Safe version that clones internal state
61
+ clone() {
62
+ return this._cloneInto();
63
+ }
64
+ }
65
+ function wrapConstructor(hashCons) {
66
+ const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
67
+ const tmp = hashCons();
68
+ hashC.outputLen = tmp.outputLen;
69
+ hashC.blockLen = tmp.blockLen;
70
+ hashC.create = () => hashCons();
71
+ return hashC;
72
+ }
73
+
74
+ // Polyfill for Safari 14
75
+ function setBigUint64(view, byteOffset, value, isLE) {
76
+ if (typeof view.setBigUint64 === 'function')
77
+ return view.setBigUint64(byteOffset, value, isLE);
78
+ const _32n = BigInt(32);
79
+ const _u32_max = BigInt(0xffffffff);
80
+ const wh = Number((value >> _32n) & _u32_max);
81
+ const wl = Number(value & _u32_max);
82
+ const h = isLE ? 4 : 0;
83
+ const l = isLE ? 0 : 4;
84
+ view.setUint32(byteOffset + h, wh, isLE);
85
+ view.setUint32(byteOffset + l, wl, isLE);
86
+ }
87
+ // Base SHA2 class (RFC 6234)
88
+ class SHA2 extends Hash {
89
+ constructor(blockLen, outputLen, padOffset, isLE) {
90
+ super();
91
+ this.blockLen = blockLen;
92
+ this.outputLen = outputLen;
93
+ this.padOffset = padOffset;
94
+ this.isLE = isLE;
95
+ this.finished = false;
96
+ this.length = 0;
97
+ this.pos = 0;
98
+ this.destroyed = false;
99
+ this.buffer = new Uint8Array(blockLen);
100
+ this.view = createView(this.buffer);
101
+ }
102
+ update(data) {
103
+ exists(this);
104
+ const { view, buffer, blockLen } = this;
105
+ data = toBytes(data);
106
+ const len = data.length;
107
+ for (let pos = 0; pos < len;) {
108
+ const take = Math.min(blockLen - this.pos, len - pos);
109
+ // Fast path: we have at least one block in input, cast it to view and process
110
+ if (take === blockLen) {
111
+ const dataView = createView(data);
112
+ for (; blockLen <= len - pos; pos += blockLen)
113
+ this.process(dataView, pos);
114
+ continue;
115
+ }
116
+ buffer.set(data.subarray(pos, pos + take), this.pos);
117
+ this.pos += take;
118
+ pos += take;
119
+ if (this.pos === blockLen) {
120
+ this.process(view, 0);
121
+ this.pos = 0;
122
+ }
123
+ }
124
+ this.length += data.length;
125
+ this.roundClean();
126
+ return this;
127
+ }
128
+ digestInto(out) {
129
+ exists(this);
130
+ output(out, this);
131
+ this.finished = true;
132
+ // Padding
133
+ // We can avoid allocation of buffer for padding completely if it
134
+ // was previously not allocated here. But it won't change performance.
135
+ const { buffer, view, blockLen, isLE } = this;
136
+ let { pos } = this;
137
+ // append the bit '1' to the message
138
+ buffer[pos++] = 0b10000000;
139
+ this.buffer.subarray(pos).fill(0);
140
+ // we have less than padOffset left in buffer, so we cannot put length in current block, need process it and pad again
141
+ if (this.padOffset > blockLen - pos) {
142
+ this.process(view, 0);
143
+ pos = 0;
144
+ }
145
+ // Pad until full block byte with zeros
146
+ for (let i = pos; i < blockLen; i++)
147
+ buffer[i] = 0;
148
+ // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
149
+ // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
150
+ // So we just write lowest 64 bits of that value.
151
+ setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
152
+ this.process(view, 0);
153
+ const oview = createView(out);
154
+ const len = this.outputLen;
155
+ // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
156
+ if (len % 4)
157
+ throw new Error('_sha2: outputLen should be aligned to 32bit');
158
+ const outLen = len / 4;
159
+ const state = this.get();
160
+ if (outLen > state.length)
161
+ throw new Error('_sha2: outputLen bigger than state');
162
+ for (let i = 0; i < outLen; i++)
163
+ oview.setUint32(4 * i, state[i], isLE);
164
+ }
165
+ digest() {
166
+ const { buffer, outputLen } = this;
167
+ this.digestInto(buffer);
168
+ const res = buffer.slice(0, outputLen);
169
+ this.destroy();
170
+ return res;
171
+ }
172
+ _cloneInto(to) {
173
+ to || (to = new this.constructor());
174
+ to.set(...this.get());
175
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
176
+ to.length = length;
177
+ to.pos = pos;
178
+ to.finished = finished;
179
+ to.destroyed = destroyed;
180
+ if (length % blockLen)
181
+ to.buffer.set(buffer);
182
+ return to;
183
+ }
184
+ }
185
+
186
+ // SHA2-256 need to try 2^128 hashes to execute birthday attack.
187
+ // BTC network is doing 2^67 hashes/sec as per early 2023.
188
+ // Choice: a ? b : c
189
+ const Chi = (a, b, c) => (a & b) ^ (~a & c);
190
+ // Majority function, true if any two inpust is true
191
+ const Maj = (a, b, c) => (a & b) ^ (a & c) ^ (b & c);
192
+ // Round constants:
193
+ // first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
194
+ // prettier-ignore
195
+ const SHA256_K = /* @__PURE__ */ new Uint32Array([
196
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
197
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
198
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
199
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
200
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
201
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
202
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
203
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
204
+ ]);
205
+ // Initial state (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
206
+ // prettier-ignore
207
+ const IV = /* @__PURE__ */ new Uint32Array([
208
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
209
+ ]);
210
+ // Temporary buffer, not used to store anything between runs
211
+ // Named this way because it matches specification.
212
+ const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
213
+ class SHA256 extends SHA2 {
214
+ constructor() {
215
+ super(64, 32, 8, false);
216
+ // We cannot use array here since array allows indexing by variable
217
+ // which means optimizer/compiler cannot use registers.
218
+ this.A = IV[0] | 0;
219
+ this.B = IV[1] | 0;
220
+ this.C = IV[2] | 0;
221
+ this.D = IV[3] | 0;
222
+ this.E = IV[4] | 0;
223
+ this.F = IV[5] | 0;
224
+ this.G = IV[6] | 0;
225
+ this.H = IV[7] | 0;
226
+ }
227
+ get() {
228
+ const { A, B, C, D, E, F, G, H } = this;
229
+ return [A, B, C, D, E, F, G, H];
230
+ }
231
+ // prettier-ignore
232
+ set(A, B, C, D, E, F, G, H) {
233
+ this.A = A | 0;
234
+ this.B = B | 0;
235
+ this.C = C | 0;
236
+ this.D = D | 0;
237
+ this.E = E | 0;
238
+ this.F = F | 0;
239
+ this.G = G | 0;
240
+ this.H = H | 0;
241
+ }
242
+ process(view, offset) {
243
+ // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
244
+ for (let i = 0; i < 16; i++, offset += 4)
245
+ SHA256_W[i] = view.getUint32(offset, false);
246
+ for (let i = 16; i < 64; i++) {
247
+ const W15 = SHA256_W[i - 15];
248
+ const W2 = SHA256_W[i - 2];
249
+ const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
250
+ const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
251
+ SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
252
+ }
253
+ // Compression function main loop, 64 rounds
254
+ let { A, B, C, D, E, F, G, H } = this;
255
+ for (let i = 0; i < 64; i++) {
256
+ const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
257
+ const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
258
+ const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
259
+ const T2 = (sigma0 + Maj(A, B, C)) | 0;
260
+ H = G;
261
+ G = F;
262
+ F = E;
263
+ E = (D + T1) | 0;
264
+ D = C;
265
+ C = B;
266
+ B = A;
267
+ A = (T1 + T2) | 0;
268
+ }
269
+ // Add the compressed chunk to the current hash value
270
+ A = (A + this.A) | 0;
271
+ B = (B + this.B) | 0;
272
+ C = (C + this.C) | 0;
273
+ D = (D + this.D) | 0;
274
+ E = (E + this.E) | 0;
275
+ F = (F + this.F) | 0;
276
+ G = (G + this.G) | 0;
277
+ H = (H + this.H) | 0;
278
+ this.set(A, B, C, D, E, F, G, H);
279
+ }
280
+ roundClean() {
281
+ SHA256_W.fill(0);
282
+ }
283
+ destroy() {
284
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
285
+ this.buffer.fill(0);
286
+ }
287
+ }
288
+ /**
289
+ * SHA2-256 hash function
290
+ * @param message - data that would be hashed
291
+ */
292
+ const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
293
+
294
+ const encoder = new TextEncoder();
295
+ const INTEGER_ARRAYS = new Set([
296
+ 'BigInt64Array',
297
+ 'BigUint64Array',
298
+ 'Int8Array',
299
+ 'Int16Array',
300
+ 'Int32Array',
301
+ 'Uint8Array',
302
+ 'Uint8ClampedArray',
303
+ 'Uint16Array',
304
+ 'Uint32Array'
305
+ ]);
306
+ const counterBytes = (counter) => {
307
+ const bytes = new Uint8Array(8);
308
+ const view = new DataView(bytes.buffer);
309
+ view.setBigUint64(0, counter, false);
310
+ return bytes;
311
+ };
312
+ const concatenate = (left, right) => {
313
+ const output = new Uint8Array(left.length + right.length);
314
+ output.set(left);
315
+ output.set(right, left.length);
316
+ return output;
317
+ };
318
+ const asBytes = (value) => new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
319
+ /**
320
+ * Synchronous Web Crypto subset backed by a deterministic SHA-256 stream.
321
+ * The seed must come from finalized consensus data plus the execution identity.
322
+ */
323
+ class DeterministicCrypto {
324
+ #counter = 0n;
325
+ #seed;
326
+ constructor(seed) {
327
+ if (!seed)
328
+ throw new TypeError('deterministic crypto seed is required');
329
+ this.#seed = sha256(encoder.encode(`leofcoin-contract-random-v1:${seed}`));
330
+ }
331
+ #fill(output) {
332
+ let offset = 0;
333
+ while (offset < output.length) {
334
+ const block = sha256(concatenate(this.#seed, counterBytes(this.#counter)));
335
+ this.#counter += 1n;
336
+ const length = Math.min(block.length, output.length - offset);
337
+ output.set(block.subarray(0, length), offset);
338
+ offset += length;
339
+ }
340
+ }
341
+ getRandomValues(array) {
342
+ const name = array?.constructor?.name;
343
+ if (!name || !INTEGER_ARRAYS.has(name)) {
344
+ throw new TypeError('crypto.getRandomValues requires an integer TypedArray');
345
+ }
346
+ if (array.byteLength > 65_536)
347
+ throw new TypeError('crypto.getRandomValues quota exceeded');
348
+ this.#fill(asBytes(array));
349
+ return array;
350
+ }
351
+ randomUUID() {
352
+ const bytes = this.getRandomValues(new Uint8Array(16));
353
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
354
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
355
+ const hex = [...bytes].map((byte) => byte.toString(16).padStart(2, '0')).join('');
356
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
357
+ }
358
+ }
359
+ const createCryptoBridge = (resolve) => Object.freeze({
360
+ getRandomValues: (array) => {
361
+ const crypto = resolve();
362
+ if (!crypto)
363
+ throw new Error('contract crypto requires finalized beacon randomness');
364
+ return crypto.getRandomValues(array);
365
+ },
366
+ randomUUID: () => {
367
+ const crypto = resolve();
368
+ if (!crypto)
369
+ throw new Error('contract crypto requires finalized beacon randomness');
370
+ return crypto.randomUUID();
371
+ }
372
+ });
373
+
374
+ export { DeterministicCrypto, createCryptoBridge };