@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.
- package/data-structures/MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.cjs +1 -1
- package/data-structures/MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.d.ts +1 -1
- package/data-structures/MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.mjs +33 -29
- package/functions/web3/events/getInputUtxoAndBalance.cjs +1 -1
- package/functions/web3/events/getInputUtxoAndBalance.mjs +19 -18
- package/package.json +1 -1
- package/types/IMultiThreadedUtxoUtils.d.ts +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
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
|
|
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
|
|
4
|
-
import { WorkerVariant as
|
|
5
|
-
import { isNode as
|
|
6
|
-
class
|
|
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
|
-
|
|
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
|
|
14
|
-
type:
|
|
13
|
+
return await U({
|
|
14
|
+
type: y.UTXO,
|
|
15
15
|
payload: s
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
18
|
async batchFilterUtxosWithNullifier(s, t) {
|
|
19
|
-
const
|
|
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((
|
|
23
|
+
utxos: o.map((n) => n.getConstructableParams()),
|
|
24
24
|
nullifiers: t
|
|
25
25
|
}
|
|
26
26
|
})
|
|
27
27
|
);
|
|
28
|
-
return (await Promise.all(
|
|
28
|
+
return (await Promise.all(e)).flat().map((o) => new h(o));
|
|
29
29
|
}
|
|
30
30
|
async buildBatchProcess(s, t) {
|
|
31
|
-
const
|
|
32
|
-
(
|
|
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:
|
|
34
|
+
data: { signature: t.getSignature(), encryptedOutputs: n }
|
|
35
35
|
})
|
|
36
36
|
);
|
|
37
|
-
return (await Promise.all(
|
|
37
|
+
return (await Promise.all(e)).flat().map((n) => new h(n));
|
|
38
38
|
}
|
|
39
39
|
sliceArrayIntoParts(s, t) {
|
|
40
|
-
const
|
|
40
|
+
const r = Math.ceil(s.length / t);
|
|
41
41
|
return Array.from(
|
|
42
42
|
{ length: t },
|
|
43
|
-
(c, o) => s.slice(
|
|
43
|
+
(c, o) => s.slice(r * o, r * (o + 1))
|
|
44
44
|
);
|
|
45
45
|
}
|
|
46
46
|
findLastOutput(s, t) {
|
|
47
|
-
let
|
|
48
|
-
return s.forEach((
|
|
49
|
-
|
|
50
|
-
}),
|
|
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
|
|
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:
|
|
57
|
+
encryptedOutputs: a,
|
|
58
58
|
lastOutput: t,
|
|
59
59
|
// doesn't matter what we pass here, it will not be used regardless
|
|
60
|
-
signature:
|
|
60
|
+
signature: o
|
|
61
61
|
}
|
|
62
62
|
};
|
|
63
63
|
return this.createUtxoPromise(i);
|
|
64
|
-
}),
|
|
65
|
-
|
|
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, {
|
|
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
|
-
|
|
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"),
|
|
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
|
|
3
|
-
const O = async (t, i,
|
|
4
|
-
const s = await t.utxoUtils.batchFilterUtxosWithNullifier(i,
|
|
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((
|
|
7
|
-
s.push(new y({ amount: 0n, erc20TokenAddress: o, shieldedPrivateKey:
|
|
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:
|
|
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:
|
|
25
|
-
let a = [],
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
const x = s.slice(
|
|
29
|
-
({ lastOutput: c, additionalEncryptedOutputs:
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
51
|
+
f,
|
|
51
52
|
o,
|
|
52
|
-
|
|
53
|
+
r,
|
|
53
54
|
u,
|
|
54
|
-
|
|
55
|
+
d,
|
|
55
56
|
i,
|
|
56
57
|
n
|
|
57
58
|
);
|
package/package.json
CHANGED
|
@@ -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
|
}
|