@hinkal/common 0.1.39 → 0.1.41

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 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("../utxo/Utxo.cjs"),u=require("../../webworker/utxoWorker/utxoWorker.types.cjs"),U=require("../../webworker/performTaskWithWorker.cjs"),y=require("../../webworker/worker.registry.cjs"),d=require("../../constants/vite.constants.cjs");class S{NUM_WORKERS;NUM_WORKERS_BATCH_PROCESS;constructor(){d.isNode?(this.NUM_WORKERS=1,this.NUM_WORKERS_BATCH_PROCESS=1):(this.NUM_WORKERS=3,this.NUM_WORKERS_BATCH_PROCESS=Math.ceil(navigator.deviceMemory??4))}async createUtxoPromise(e){return await U.performTaskWithWorker({type:y.WorkerVariant.UTXO,payload:e})}async batchFilterUtxosWithNullifier(e,s){const i=this.sliceArrayIntoParts(e,this.NUM_WORKERS).map(t=>this.createUtxoPromise({type:u.UtxoWorkerActionType.BATCH_FILTER_UTXOS_WITH_NULLIFIER,data:{utxos:t.map(r=>r.getConstructableParams()),nullifiers:s}}));return(await Promise.all(i)).flat().map(t=>new p.Utxo(t))}async buildBatchProcess(e,s){const i=this.sliceArrayIntoParts(e,this.NUM_WORKERS).map(r=>this.createUtxoPromise({type:u.UtxoWorkerActionType.BUILD_UTXOS,data:{signature:s.getSignature(),encryptedOutputs:r}}));return(await Promise.all(i)).flat().map(r=>new p.Utxo(r))}sliceArrayIntoParts(e,s){const o=Math.ceil(e.length/s);return Array.from({length:s},(c,t)=>e.slice(o*t,o*(t+1)))}async batchProcess(e,s,o){const c=this.sliceArrayIntoParts(e,this.NUM_WORKERS_BATCH_PROCESS).map(a=>{const n={type:u.UtxoWorkerActionType.DECIPHER_OUTPUTS,data:{encryptedOutputs:a,lastOutput:s,signature:o.getSignature()}};return this.createUtxoPromise(n)}),t=await Promise.all(c),r=t.map(({additionalEncryptedOutputs:a})=>a).flat(),l=new Set,R=r.reduce((a,n)=>l.has(n.value)?a:(l.add(n.value),[...a,n]),[]),h=t.length?t[t.length-1]?.lastOutput:void 0;return{additionalEncryptedOutputs:R,lastOutput:h??s}}}exports.MultiThreadedUtxoUtils=S;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("../utxo/Utxo.cjs"),u=require("../../webworker/utxoWorker/utxoWorker.types.cjs"),y=require("../../webworker/performTaskWithWorker.cjs"),d=require("../../webworker/worker.registry.cjs"),m=require("../../constants/vite.constants.cjs");class S{NUM_WORKERS;NUM_WORKERS_BATCH_PROCESS;constructor(){m.isNode?(this.NUM_WORKERS=1,this.NUM_WORKERS_BATCH_PROCESS=1):(this.NUM_WORKERS=3,this.NUM_WORKERS_BATCH_PROCESS=Math.ceil(navigator.deviceMemory??4))}async createUtxoPromise(s){return await y.performTaskWithWorker({type:d.WorkerVariant.UTXO,payload:s})}async batchFilterUtxosWithNullifier(s,t){const r=this.sliceArrayIntoParts(s,this.NUM_WORKERS).map(o=>this.createUtxoPromise({type:u.UtxoWorkerActionType.BATCH_FILTER_UTXOS_WITH_NULLIFIER,data:{utxos:o.map(a=>a.getConstructableParams()),nullifiers:t}}));return(await Promise.all(r)).flat().map(o=>new p.Utxo(o))}async buildBatchProcess(s,t){const r=this.sliceArrayIntoParts(s,this.NUM_WORKERS).map(a=>this.createUtxoPromise({type:u.UtxoWorkerActionType.BUILD_UTXOS,data:{signature:t.getSignature(),encryptedOutputs:a}}));return(await Promise.all(r)).flat().map(a=>new p.Utxo(a))}sliceArrayIntoParts(s,t){const e=Math.ceil(s.length/t);return Array.from({length:t},(c,o)=>s.slice(e*o,e*(o+1)))}findLastOutput(s,t){let e=t;return s.forEach(r=>{r!==t&&(e=r)}),e}async batchProcess(s,t,e){const r=this.sliceArrayIntoParts(s,this.NUM_WORKERS_BATCH_PROCESS),c=e.getSignature(),o=r.map(i=>{const n={type:u.UtxoWorkerActionType.DECIPHER_OUTPUTS,data:{encryptedOutputs:i,lastOutput:t,signature:c}};return this.createUtxoPromise(n)}),a=await Promise.all(o),R=a.map(({additionalEncryptedOutputs:i})=>i).flat(),l=new Set,h=R.reduce((i,n)=>l.has(n.value)?i:(l.add(n.value),[...i,n]),[]),U=this.findLastOutput(a.map(i=>i.lastOutput),t);return{additionalEncryptedOutputs:h,lastOutput:U??t}}}exports.MultiThreadedUtxoUtils=S;
@@ -15,5 +15,6 @@ export declare class MultiThreadedUtxoUtils implements IMultiThreadedUtxoUtils {
15
15
  batchFilterUtxosWithNullifier(allUtxos: Utxo[], nullifiers: Set<string>): Promise<Utxo[]>;
16
16
  buildBatchProcess(encryptedOutputs: EncryptedOutputWithSign[], userKeys: UserKeys): Promise<Utxo[]>;
17
17
  sliceArrayIntoParts<T>(arr: T[], numberOfParts: number): T[][];
18
+ findLastOutput(lastOutputsArray: string[], lastOutput: string): string;
18
19
  batchProcess(encryptedOutputs: EncryptedOutputWithSign[], lastOutput: string, userKeys: UserKeys): Promise<OutputsWithSignAndLast<boolean>>;
19
20
  }
@@ -1,64 +1,73 @@
1
- import { Utxo as p } from "../utxo/Utxo.mjs";
1
+ import { Utxo as l } from "../utxo/Utxo.mjs";
2
2
  import { UtxoWorkerActionType as u } from "../../webworker/utxoWorker/utxoWorker.types.mjs";
3
- import { performTaskWithWorker as h } from "../../webworker/performTaskWithWorker.mjs";
4
- import { WorkerVariant as S } from "../../webworker/worker.registry.mjs";
5
- import { isNode as U } from "../../constants/vite.constants.mjs";
3
+ import { performTaskWithWorker as S } from "../../webworker/performTaskWithWorker.mjs";
4
+ import { WorkerVariant as d } from "../../webworker/worker.registry.mjs";
5
+ import { isNode as O } from "../../constants/vite.constants.mjs";
6
6
  class W {
7
7
  NUM_WORKERS;
8
8
  NUM_WORKERS_BATCH_PROCESS;
9
9
  constructor() {
10
- U ? (this.NUM_WORKERS = 1, this.NUM_WORKERS_BATCH_PROCESS = 1) : (this.NUM_WORKERS = 3, this.NUM_WORKERS_BATCH_PROCESS = Math.ceil(navigator.deviceMemory ?? 4));
10
+ O ? (this.NUM_WORKERS = 1, this.NUM_WORKERS_BATCH_PROCESS = 1) : (this.NUM_WORKERS = 3, this.NUM_WORKERS_BATCH_PROCESS = Math.ceil(navigator.deviceMemory ?? 4));
11
11
  }
12
12
  async createUtxoPromise(s) {
13
- return await h({
14
- type: S.UTXO,
13
+ return await S({
14
+ type: d.UTXO,
15
15
  payload: s
16
16
  });
17
17
  }
18
- async batchFilterUtxosWithNullifier(s, e) {
19
- const i = this.sliceArrayIntoParts(s, this.NUM_WORKERS).map(
20
- (t) => this.createUtxoPromise({
18
+ async batchFilterUtxosWithNullifier(s, t) {
19
+ const r = this.sliceArrayIntoParts(s, this.NUM_WORKERS).map(
20
+ (a) => this.createUtxoPromise({
21
21
  type: u.BATCH_FILTER_UTXOS_WITH_NULLIFIER,
22
22
  data: {
23
- utxos: t.map((r) => r.getConstructableParams()),
24
- nullifiers: e
23
+ utxos: a.map((o) => o.getConstructableParams()),
24
+ nullifiers: t
25
25
  }
26
26
  })
27
27
  );
28
- return (await Promise.all(i)).flat().map((t) => new p(t));
28
+ return (await Promise.all(r)).flat().map((a) => new l(a));
29
29
  }
30
- async buildBatchProcess(s, e) {
31
- const i = this.sliceArrayIntoParts(s, this.NUM_WORKERS).map(
32
- (r) => this.createUtxoPromise({
30
+ async buildBatchProcess(s, t) {
31
+ const r = this.sliceArrayIntoParts(s, this.NUM_WORKERS).map(
32
+ (o) => this.createUtxoPromise({
33
33
  type: u.BUILD_UTXOS,
34
- data: { signature: e.getSignature(), encryptedOutputs: r }
34
+ data: { signature: t.getSignature(), encryptedOutputs: o }
35
35
  })
36
36
  );
37
- return (await Promise.all(i)).flat().map((r) => new p(r));
37
+ return (await Promise.all(r)).flat().map((o) => new l(o));
38
38
  }
39
- sliceArrayIntoParts(s, e) {
40
- const a = Math.ceil(s.length / e);
39
+ sliceArrayIntoParts(s, t) {
40
+ const e = Math.ceil(s.length / t);
41
41
  return Array.from(
42
- { length: e },
43
- (c, t) => s.slice(a * t, a * (t + 1))
42
+ { length: t },
43
+ (c, a) => s.slice(e * a, e * (a + 1))
44
44
  );
45
45
  }
46
- async batchProcess(s, e, a) {
47
- const c = this.sliceArrayIntoParts(s, this.NUM_WORKERS_BATCH_PROCESS).map((o) => {
46
+ findLastOutput(s, t) {
47
+ let e = t;
48
+ return s.forEach((r) => {
49
+ r !== t && (e = r);
50
+ }), e;
51
+ }
52
+ async batchProcess(s, t, e) {
53
+ const r = this.sliceArrayIntoParts(s, this.NUM_WORKERS_BATCH_PROCESS), c = e.getSignature(), a = r.map((i) => {
48
54
  const n = {
49
55
  type: u.DECIPHER_OUTPUTS,
50
56
  data: {
51
- encryptedOutputs: o,
52
- lastOutput: e,
57
+ encryptedOutputs: i,
58
+ lastOutput: t,
53
59
  // doesn't matter what we pass here, it will not be used regardless
54
- signature: a.getSignature()
60
+ signature: c
55
61
  }
56
62
  };
57
63
  return this.createUtxoPromise(n);
58
- }), t = await Promise.all(c), r = t.map(({ additionalEncryptedOutputs: o }) => o).flat(), l = /* @__PURE__ */ new Set(), m = r.reduce((o, n) => l.has(n.value) ? o : (l.add(n.value), [...o, n]), []), R = t.length ? t[t.length - 1]?.lastOutput : void 0;
64
+ }), o = await Promise.all(a), m = o.map(({ additionalEncryptedOutputs: i }) => i).flat(), p = /* @__PURE__ */ new Set(), R = m.reduce((i, n) => p.has(n.value) ? i : (p.add(n.value), [...i, n]), []), h = this.findLastOutput(
65
+ o.map((i) => i.lastOutput),
66
+ t
67
+ );
59
68
  return {
60
- additionalEncryptedOutputs: m,
61
- lastOutput: R ?? e
69
+ additionalEncryptedOutputs: R,
70
+ lastOutput: h ?? t
62
71
  // keep the last output the same if no new output is present
63
72
  };
64
73
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hinkal/common",
3
- "version": "0.1.39",
3
+ "version": "0.1.41",
4
4
  "homepage": "hinkal.pro",
5
5
  "author": {
6
6
  "name": "Hinkal Protocol"