@hinkal/common 0.1.50 → 0.1.51

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