@gurge/sdk 0.3.22 → 0.3.24
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/common/src/data-structures/Hinkal/hinkalWithdraw.cjs +1 -1
- package/common/src/data-structures/Hinkal/hinkalWithdraw.mjs +61 -37
- package/common/src/data-structures/MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.cjs +1 -1
- package/common/src/data-structures/MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.mjs +42 -26
- package/common/src/functions/pre-transaction/buildCommitmentValidationData.cjs +1 -1
- package/common/src/functions/pre-transaction/buildCommitmentValidationData.mjs +42 -38
- package/common/src/functions/snarkjs/constructGeneralZkProof.cjs +1 -1
- package/common/src/functions/snarkjs/constructGeneralZkProof.mjs +81 -70
- package/common/src/functions/snarkjs/generateMainAndCommitmentZkProof.cjs +1 -1
- package/common/src/functions/snarkjs/generateMainAndCommitmentZkProof.mjs +21 -10
- package/common/src/functions/snarkjs/generateZkProof.cjs +1 -1
- package/common/src/functions/snarkjs/generateZkProof.mjs +24 -13
- package/common/src/functions/snarkjs/generateZkProofEnclave.cjs +1 -1
- package/common/src/functions/snarkjs/generateZkProofEnclave.mjs +14 -11
- package/common/src/functions/snarkjs/generateZkProofSelf.cjs +1 -1
- package/common/src/functions/snarkjs/generateZkProofSelf.mjs +22 -16
- package/common/src/functions/utils/hinkalTiming.utils.cjs +1 -0
- package/common/src/functions/utils/hinkalTiming.utils.mjs +18 -0
- package/common/src/functions/web3/events/getInputUtxoAndBalance.cjs +1 -1
- package/common/src/functions/web3/events/getInputUtxoAndBalance.mjs +73 -42
- package/common/src/functions/web3/events/getInputUtxosEnclave.cjs +1 -1
- package/common/src/functions/web3/events/getInputUtxosEnclave.mjs +21 -20
- package/common/src/functions/web3/events/getShieldedBalance.cjs +1 -1
- package/common/src/functions/web3/events/getShieldedBalance.mjs +56 -53
- package/common/src/webworker/package.json +1 -1
- package/common/src/webworker/performTaskWithWorker.cjs +1 -1
- package/common/src/webworker/performTaskWithWorker.mjs +18 -17
- package/common/src/webworker/viteWorkerURL.constant.cjs +3 -3
- package/common/src/webworker/viteWorkerURL.constant.mjs +3 -3
- package/package.json +1 -1
- package/sdk/package.json.cjs +1 -1
- package/sdk/package.json.mjs +1 -1
|
@@ -1,16 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createHinkalTiming as e } from "../utils/hinkalTiming.utils.mjs";
|
|
2
|
+
import { buildCommitmentValidationData as t, buildCommitmentValidationDataFromProof as n } from "../pre-transaction/buildCommitmentValidationData.mjs";
|
|
2
3
|
import "../pre-transaction/index.mjs";
|
|
3
|
-
import { generateZkProof as
|
|
4
|
+
import { generateZkProof as r } from "./generateZkProof.mjs";
|
|
4
5
|
//#region libs/shared/common/src/functions/snarkjs/generateMainAndCommitmentZkProof.ts
|
|
5
|
-
var
|
|
6
|
-
let
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
var i = async (i, a, o, s, c, l, u) => {
|
|
7
|
+
let d = e("generateMainAndCommitmentZkProof"), f = await t(i, a, o, s);
|
|
8
|
+
d.log("buildCommitmentValidationData", {
|
|
9
|
+
mainVerifierName: c,
|
|
10
|
+
hasCommitmentProof: !!f
|
|
11
|
+
});
|
|
12
|
+
let p = [c], m = [l];
|
|
13
|
+
f && (p.push(f.verifierName), m.push(f.commitmentInput));
|
|
14
|
+
let h = await r(i, p, m, u);
|
|
15
|
+
d.log("generateZkProof", {
|
|
16
|
+
verifierNames: p,
|
|
17
|
+
generateProofRemotely: u
|
|
18
|
+
});
|
|
19
|
+
let { zkCallData: g, publicSignals: _ } = h[0];
|
|
9
20
|
return {
|
|
10
|
-
zkCallData:
|
|
11
|
-
publicSignals:
|
|
12
|
-
commitmentValidationData:
|
|
21
|
+
zkCallData: g,
|
|
22
|
+
publicSignals: _,
|
|
23
|
+
commitmentValidationData: n(f, h[1])
|
|
13
24
|
};
|
|
14
25
|
};
|
|
15
26
|
//#endregion
|
|
16
|
-
export {
|
|
27
|
+
export { i as generateMainAndCommitmentZkProof };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=require(`../utils/customEnclaveFunctionsRegister.cjs`),t=require(`../../error-handling/logger.cjs`),
|
|
1
|
+
const e=require(`../utils/customEnclaveFunctionsRegister.cjs`),t=require(`../utils/hinkalTiming.utils.cjs`),n=require(`../../error-handling/logger.cjs`),r=require(`./getZKFiles.cjs`),i=require(`./generateZkProofEnclave.cjs`),a=require(`./generateZkProofSelf.cjs`);var o=async(o,s,c,l)=>{let u=t.createHinkalTiming(`generateZkProof`);if(u.log(`start`,{remotely:!!l,verifierNames:s}),l)try{let t=e.getCustomProofGenerator();if(t){let e=await t(c,s.map(e=>r.getWASMFile(e,o)),s.map(e=>r.getZKeyFile(e,o)));return u.log(`customProofGenerator`,{verifierNames:s}),e.map(({zkCallData:e,publicSignals:t})=>({zkCallData:e,publicSignals:t}))}let n=await i.generateZkProofEnclave(o,s,c);return u.log(`enclave`,{verifierNames:s}),n}catch(e){n.Logger.error(`enclave proof error`,e),u.log(`enclave-failed-fallback-self`)}let d=await Promise.all(s.map((e,t)=>a.generateZkProofSelf(o,e,c[t])));return u.log(`self`,{verifierNames:s}),d};exports.generateZkProof=o;
|
|
@@ -1,20 +1,31 @@
|
|
|
1
1
|
import { getCustomProofGenerator as e } from "../utils/customEnclaveFunctionsRegister.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { createHinkalTiming as t } from "../utils/hinkalTiming.utils.mjs";
|
|
3
|
+
import { Logger as n } from "../../error-handling/logger.mjs";
|
|
4
|
+
import { getWASMFile as r, getZKeyFile as i } from "./getZKFiles.mjs";
|
|
5
|
+
import { generateZkProofEnclave as a } from "./generateZkProofEnclave.mjs";
|
|
6
|
+
import { generateZkProofSelf as o } from "./generateZkProofSelf.mjs";
|
|
6
7
|
//#region libs/shared/common/src/functions/snarkjs/generateZkProof.ts
|
|
7
|
-
var
|
|
8
|
-
|
|
8
|
+
var s = async (s, c, l, u) => {
|
|
9
|
+
let d = t("generateZkProof");
|
|
10
|
+
if (d.log("start", {
|
|
11
|
+
remotely: !!u,
|
|
12
|
+
verifierNames: c
|
|
13
|
+
}), u) try {
|
|
9
14
|
let t = e();
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
publicSignals: t
|
|
13
|
-
|
|
15
|
+
if (t) {
|
|
16
|
+
let e = await t(l, c.map((e) => r(e, s)), c.map((e) => i(e, s)));
|
|
17
|
+
return d.log("customProofGenerator", { verifierNames: c }), e.map(({ zkCallData: e, publicSignals: t }) => ({
|
|
18
|
+
zkCallData: e,
|
|
19
|
+
publicSignals: t
|
|
20
|
+
}));
|
|
21
|
+
}
|
|
22
|
+
let n = await a(s, c, l);
|
|
23
|
+
return d.log("enclave", { verifierNames: c }), n;
|
|
14
24
|
} catch (e) {
|
|
15
|
-
|
|
25
|
+
n.error("enclave proof error", e), d.log("enclave-failed-fallback-self");
|
|
16
26
|
}
|
|
17
|
-
|
|
27
|
+
let f = await Promise.all(c.map((e, t) => o(s, e, l[t])));
|
|
28
|
+
return d.log("self", { verifierNames: c }), f;
|
|
18
29
|
};
|
|
19
30
|
//#endregion
|
|
20
|
-
export {
|
|
31
|
+
export { s as generateZkProof };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=require(`./constant.cjs`),t=require(`../../data-structures/http/HttpClient.cjs`);require(`../../data-structures/http/index.cjs`);const n=require(`../utils/encryptInputForEnclave.cjs`),r=require(`./getZKFiles.cjs`);var
|
|
1
|
+
const e=require(`./constant.cjs`),t=require(`../../data-structures/http/HttpClient.cjs`);require(`../../data-structures/http/index.cjs`);const n=require(`../utils/encryptInputForEnclave.cjs`),r=require(`../utils/hinkalTiming.utils.cjs`),i=require(`./getZKFiles.cjs`);var a=e=>{if(e.startsWith(`http`)){let t=e.split(`/`).at(-1);if(!t)throw Error(`invalid verifierName`);return t}return e},o=async(o,s,c)=>{let l=r.createHinkalTiming(`generateZkProofEnclave`),u=s.map(e=>a(i.getWASMFile(e,o))),d=s.map(e=>a(i.getZKeyFile(e,o))),{keyCiphertext:f,inputCiphertext:p}=await n.makeEnclaveHandshakeAndEncryptInput(c);l.log(`handshake+encrypt`,{verifierNames:s});let m={circuit_wasms:u,circuit_zkeys:d,inputs:p,key:f},h=await t.httpClient.post(`${e.ENCLAVE_URL}/generate-proofs`,m);return l.log(`enclave-http`,{verifierNames:s}),h.map(({zk_calldata:e,public_signals:t})=>({zkCallData:e,publicSignals:t}))};exports.generateZkProofEnclave=o;
|
|
@@ -2,26 +2,29 @@ import { ENCLAVE_URL as e } from "./constant.mjs";
|
|
|
2
2
|
import { httpClient as t } from "../../data-structures/http/HttpClient.mjs";
|
|
3
3
|
import "../../data-structures/http/index.mjs";
|
|
4
4
|
import { makeEnclaveHandshakeAndEncryptInput as n } from "../utils/encryptInputForEnclave.mjs";
|
|
5
|
-
import {
|
|
5
|
+
import { createHinkalTiming as r } from "../utils/hinkalTiming.utils.mjs";
|
|
6
|
+
import { getWASMFile as i, getZKeyFile as a } from "./getZKFiles.mjs";
|
|
6
7
|
//#region libs/shared/common/src/functions/snarkjs/generateZkProofEnclave.ts
|
|
7
|
-
var
|
|
8
|
+
var o = (e) => {
|
|
8
9
|
if (e.startsWith("http")) {
|
|
9
10
|
let t = e.split("/").at(-1);
|
|
10
11
|
if (!t) throw Error("invalid verifierName");
|
|
11
12
|
return t;
|
|
12
13
|
}
|
|
13
14
|
return e;
|
|
14
|
-
},
|
|
15
|
-
let
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
}, s = async (s, c, l) => {
|
|
16
|
+
let u = r("generateZkProofEnclave"), d = c.map((e) => o(i(e, s))), f = c.map((e) => o(a(e, s))), { keyCiphertext: p, inputCiphertext: m } = await n(l);
|
|
17
|
+
u.log("handshake+encrypt", { verifierNames: c });
|
|
18
|
+
let h = {
|
|
19
|
+
circuit_wasms: d,
|
|
20
|
+
circuit_zkeys: f,
|
|
21
|
+
inputs: m,
|
|
22
|
+
key: p
|
|
23
|
+
}, g = await t.post(`${e}/generate-proofs`, h);
|
|
24
|
+
return u.log("enclave-http", { verifierNames: c }), g.map(({ zk_calldata: e, public_signals: t }) => ({
|
|
22
25
|
zkCallData: e,
|
|
23
26
|
publicSignals: t
|
|
24
27
|
}));
|
|
25
28
|
};
|
|
26
29
|
//#endregion
|
|
27
|
-
export {
|
|
30
|
+
export { s as generateZkProofEnclave };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=require(`../../webworker/worker.registry.cjs`),
|
|
1
|
+
const e=require(`../utils/hinkalTiming.utils.cjs`),t=require(`../../webworker/worker.registry.cjs`),n=require(`../../webworker/performTaskWithWorker.cjs`),r=require(`./getZKFiles.cjs`),i=require(`../../webworker/snarkjsWorker/snarkjsWorker.types.cjs`);var a=async(a,o,s)=>{let c=e.createHinkalTiming(`generateZkProofSelf:${o}`),l=r.getWASMFile(o,a),u=r.getZKeyFile(o,a);c.log(`start`,{chainId:a,wasmFilePath:l});let{publicSignals:d,zkCallData:f}=await n.performTaskWithWorker({type:t.WorkerVariant.SnarkJS,payload:{type:i.SnarkJSWorkerActionType.FULL_PROVE,data:{input:s,chainId:a,wasmFilePath:l,zKeyFilePath:u}}});return c.log(`worker-done`),{zkCallData:f,publicSignals:d}};exports.generateZkProofSelf=a;
|
|
@@ -1,25 +1,31 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { createHinkalTiming as e } from "../utils/hinkalTiming.utils.mjs";
|
|
2
|
+
import { WorkerVariant as t } from "../../webworker/worker.registry.mjs";
|
|
3
|
+
import { performTaskWithWorker as n } from "../../webworker/performTaskWithWorker.mjs";
|
|
4
|
+
import { getWASMFile as r, getZKeyFile as i } from "./getZKFiles.mjs";
|
|
5
|
+
import { SnarkJSWorkerActionType as a } from "../../webworker/snarkjsWorker/snarkjsWorker.types.mjs";
|
|
5
6
|
//#region libs/shared/common/src/functions/snarkjs/generateZkProofSelf.ts
|
|
6
|
-
var
|
|
7
|
-
let
|
|
8
|
-
|
|
7
|
+
var o = async (o, s, c) => {
|
|
8
|
+
let l = e(`generateZkProofSelf:${s}`), u = r(s, o), d = i(s, o);
|
|
9
|
+
l.log("start", {
|
|
10
|
+
chainId: o,
|
|
11
|
+
wasmFilePath: u
|
|
12
|
+
});
|
|
13
|
+
let { publicSignals: f, zkCallData: p } = await n({
|
|
14
|
+
type: t.SnarkJS,
|
|
9
15
|
payload: {
|
|
10
|
-
type:
|
|
16
|
+
type: a.FULL_PROVE,
|
|
11
17
|
data: {
|
|
12
|
-
input:
|
|
13
|
-
chainId:
|
|
14
|
-
wasmFilePath:
|
|
15
|
-
zKeyFilePath:
|
|
18
|
+
input: c,
|
|
19
|
+
chainId: o,
|
|
20
|
+
wasmFilePath: u,
|
|
21
|
+
zKeyFilePath: d
|
|
16
22
|
}
|
|
17
23
|
}
|
|
18
24
|
});
|
|
19
|
-
return {
|
|
20
|
-
zkCallData:
|
|
21
|
-
publicSignals:
|
|
25
|
+
return l.log("worker-done"), {
|
|
26
|
+
zkCallData: p,
|
|
27
|
+
publicSignals: f
|
|
22
28
|
};
|
|
23
29
|
};
|
|
24
30
|
//#endregion
|
|
25
|
-
export {
|
|
31
|
+
export { o as generateZkProofSelf };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=`[HinkalTiming]`,t=(t,n,r,i)=>{let a=Date.now(),o=a-r.last,s=a-r.start;r.last=a;let c=i?` ${JSON.stringify(i)}`:``;console.log(`${e} [${t}] ${n} +${o}ms (total ${s}ms)${c}`)},n=e=>{let n={start:Date.now(),last:Date.now()};return{marks:n,log:(r,i)=>t(e,r,n,i)}};exports.createHinkalTiming=n;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region libs/shared/common/src/functions/utils/hinkalTiming.utils.ts
|
|
2
|
+
var e = "[HinkalTiming]", t = (t, n, r, i) => {
|
|
3
|
+
let a = Date.now(), o = a - r.last, s = a - r.start;
|
|
4
|
+
r.last = a;
|
|
5
|
+
let c = i ? ` ${JSON.stringify(i)}` : "";
|
|
6
|
+
console.log(`${e} [${t}] ${n} +${o}ms (total ${s}ms)${c}`);
|
|
7
|
+
}, n = (e) => {
|
|
8
|
+
let n = {
|
|
9
|
+
start: Date.now(),
|
|
10
|
+
last: Date.now()
|
|
11
|
+
};
|
|
12
|
+
return {
|
|
13
|
+
marks: n,
|
|
14
|
+
log: (r, i) => t(e, r, n, i)
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
export { n as createHinkalTiming };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
require(`../../../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../../constants/chains.constants.cjs`),t=require(`../../utils/caseInsensitive.utils.cjs`),n=require(`../../../data-structures/utxo/Utxo.cjs`),r=require(`../../utils/upToDateState.cjs`),i=require(`../../utils/cacheFunctions.cjs`),a=require(`../../utils/encodeTokenWithId.cjs`),o=require(`../../utils/solanaMint.utils.cjs`),s=require(`./getInputUtxosEnclave.cjs`),
|
|
1
|
+
require(`../../../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../../constants/chains.constants.cjs`),t=require(`../../utils/caseInsensitive.utils.cjs`),n=require(`../../../data-structures/utxo/Utxo.cjs`),r=require(`../../utils/upToDateState.cjs`),i=require(`../../utils/cacheFunctions.cjs`),a=require(`../../utils/encodeTokenWithId.cjs`),o=require(`../../utils/solanaMint.utils.cjs`),s=require(`../../utils/hinkalTiming.utils.cjs`),c=require(`./getInputUtxosEnclave.cjs`),l=require(`../../utils/addresses.cjs`),u=require(`../../../API/scheduled-transactions-calls.cjs`),d=require(`../../utils/tron.utils.cjs`);var f=new(require(`async-mutex`)).Mutex,p=async(e,t)=>{if(e.allowParallelBalanceLocalDecryption)return t();let n=s.createHinkalTiming(`utxoDecrypt.mutex`);return f.isLocked()&&n.log(`waiting`),f.runExclusive(async()=>{n.log(`acquired`);try{return await t()}finally{n.log(`released`)}})},m=async(e,t,n,a,o,c,l,u,d)=>p(e,async()=>{let f=s.createHinkalTiming(`utxoDecrypt.local`);d+=1;let p=c.slice(d);f.log(`batchProcess-start`,{outputsSlice:p.length,lastOutputIndex:d});let{lastOutput:m,additionalEncryptedOutputs:h}=await e.utxoUtils.batchProcess(p,u,t,n);f.log(`batchProcess-done`,{additionalEncryptedOutputs:h.length});let g=[...l,...h];await r.checkAddressNotUpdated(e,a),i.setHinkalCache({lastOutput:m,encryptedOutputs:g},e,n,o),f.log(`cache-write`);let _=await e.utxoUtils.buildBatchProcess(g,t,n);return f.log(`buildBatchProcess-done`,{utxoCount:_.length}),_}),h=async(e,t,n,r)=>{let{utxos:a,encryptedOutputs:o,lastOutput:s}=await c.getInputUtxosEnclave(e,n,t);return i.setHinkalCache({encryptedOutputs:o,lastOutput:s},e,n,r),a},g=async(e,t,n,r,i,a,o,s,c)=>{try{return await h(e,t,n,i)}catch{return m(e,t,n,r,i,a,o,s,c)}},_=e=>{if(Array.isArray(e))return e;if(e!==void 0)return[e]},v=async(e,t,n)=>await e.utxoUtils.batchFilterUtxosWithNullifier(t,n),y=(e,t,r,i,a,o,s,c)=>{for(e.sort((e,t)=>e.amount<=t.amount?1:-1);e.length<t||e.length>t&&e.length<6;)if(e.push(new n.Utxo({amount:0n,erc20TokenAddress:o,mintAddress:c,nullifyingKey:i,spendingPublicKey:a,isNewStyle:!0})),r)for(;e.length>6;)e.splice(e.length-1)},b=async({hinkal:e,chainId:t,passedShieldedPublicKey:n=void 0,ethAddress:a=void 0,resetCacheBefore:o=!1,allowRemoteDecryption:c=!1,useBlockedUtxos:l=!1})=>{let u=a??await e.getEthereumAddress(),d=n??e.userKeys.getShieldedPublicKey(),{userKeys:f}=e,p=e.encryptedOutputsByChain[t],h=e.nullifiersByChain[t],_=Array.from(p);o&&i.resetCache(e,t,d);let{encryptedOutputs:y,lastOutput:b}=i.getHinkalCache(e,t,d);await r.checkAddressNotUpdated(e,u);let x=b!==``,S=_.findIndex(e=>e.value===b),C=S>-1,w=!x&&S===-1,T=e.areMerkleTreeUpdatesDisabled(),E=!T&&(C||!c&&w),D=!T&&c&&w,O=!E&&!D,k=s.createHinkalTiming(`getInputUtxoAndBalance`);k.log(`branch`,{decryptLocally:E,decryptRemotely:D,useCache:O,hinkalOutputs:_.length,cachedEncryptedOutputs:y.length,lastOutputIndex:S});let A=[];E?(A=await m(e,f,t,u,d,_,y,b,S),k.log(`decryptLocally-done`,{utxoCount:A.length})):D?(A=await g(e,f,t,u,d,_,y,b,S),k.log(`decryptRemotely-done`,{utxoCount:A.length})):O&&(A=await e.utxoUtils.buildBatchProcess(y,f,t),k.log(`useCache-buildBatchProcess-done`,{utxoCount:A.length})),await r.checkAddressNotUpdated(e,u);let j=await v(e,A,h);return k.log(`filterSpentUtxos-done`,{inputUtxos:j.length}),{inputUtxos:l?j.filter(e=>e.isBlocked):j.filter(e=>!e.isBlocked)}},x=async({hinkal:t,chainId:n,passedShieldedPublicKey:r=void 0,ethAddress:i=void 0,resetCacheBefore:a=!1,allowRemoteDecryption:o=!1})=>{let s=i??await t.getEthereumAddressByChain(n),c=l.hashEthereumAddress(e.isTronLike(n)?d.addressToHexFormat(s):s),{inputUtxos:f}=await b({hinkal:t,chainId:n,passedShieldedPublicKey:r,ethAddress:s,resetCacheBefore:a,allowRemoteDecryption:o,useBlockedUtxos:!0});if(!f.length)return{inputUtxos:[]};let{indexes:p}=await u.getScheduledTransactionsNullifierIndexes({hashedEthereumAddress:c,nullifiers:f.map(e=>e.getNullifier())});return{inputUtxos:p.map(e=>f[e]).filter(e=>!!e)}},S=async({hinkal:n,tokenWithId:r,minInput:i=2,sliceIfMore6:s=!0,chainId:c,passedShieldedPrivateKey:l=void 0,passedShieldedPublicKey:u=void 0,ethAddress:d=void 0,resetCacheBefore:f=!1,ensuredTokensWithId:p,allowRemoteDecryption:m=!1,useBlockedUtxos:h=!1})=>{let g=l??n.userKeys.getShieldedPrivateKey(),v=n.userKeys.getSpendingKeyPair().pubSpendingBJJPoint,{inputUtxos:S}=h?await x({hinkal:n,chainId:c,ethAddress:d,passedShieldedPrivateKey:l,resetCacheBefore:f,allowRemoteDecryption:m}):await b({hinkal:n,tokenWithId:r,minInput:i,sliceIfMore6:s,chainId:c,passedShieldedPrivateKey:l,passedShieldedPublicKey:u,ethAddress:d,resetCacheBefore:f,allowRemoteDecryption:m}),C=_(r),w=new Map;S.forEach(e=>{let n=e.getTokenAddress(c);if(C&&!C.some(e=>t.caseInsensitiveEqual(e.erc20TokenAddress,n)))return;let r=a.encodeTokenWithId(c,{erc20TokenAddress:n,tokenId:0}),i=w.get(r)??[];w.set(r,[...i,e])}),p?.forEach(e=>{let t=a.encodeTokenWithId(c,{erc20TokenAddress:e.erc20TokenAddress,tokenId:0});w.set(t,w.get(t)??[])});for(let[t,n]of w){let r=t.split(`-`)[0],a=Number(t.split(`-`)[1]),l=e.isSolanaLike(c),u=l?r:void 0;y(n,i,s,g,v,l?o.formatMintAddress(r).compressedAddress.toLowerCase():r.toLowerCase(),a,u)}return w};exports.attemptGetInputUtxosRemotely=g,exports.getInputUtxoAndBalance=b,exports.getInputUtxoAndBalanceOfStuckUtxos=x,exports.getInputUtxoAndBalancePerToken=S,exports.getInputUtxosRemotely=h;
|
|
@@ -5,35 +5,57 @@ import { checkAddressNotUpdated as i } from "../../utils/upToDateState.mjs";
|
|
|
5
5
|
import { getHinkalCache as a, resetCache as o, setHinkalCache as s } from "../../utils/cacheFunctions.mjs";
|
|
6
6
|
import { encodeTokenWithId as c } from "../../utils/encodeTokenWithId.mjs";
|
|
7
7
|
import { formatMintAddress as l } from "../../utils/solanaMint.utils.mjs";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
8
|
+
import { createHinkalTiming as u } from "../../utils/hinkalTiming.utils.mjs";
|
|
9
|
+
import { getInputUtxosEnclave as d } from "./getInputUtxosEnclave.mjs";
|
|
10
|
+
import { hashEthereumAddress as f } from "../../utils/addresses.mjs";
|
|
11
|
+
import { getScheduledTransactionsNullifierIndexes as p } from "../../../API/scheduled-transactions-calls.mjs";
|
|
12
|
+
import { addressToHexFormat as m } from "../../utils/tron.utils.mjs";
|
|
13
|
+
import { Mutex as h } from "async-mutex";
|
|
13
14
|
//#region libs/shared/common/src/functions/web3/events/getInputUtxoAndBalance.ts
|
|
14
|
-
var
|
|
15
|
-
|
|
16
|
-
let
|
|
17
|
-
return
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
15
|
+
var g = new h(), _ = async (e, t) => {
|
|
16
|
+
if (e.allowParallelBalanceLocalDecryption) return t();
|
|
17
|
+
let n = u("utxoDecrypt.mutex");
|
|
18
|
+
return g.isLocked() && n.log("waiting"), g.runExclusive(async () => {
|
|
19
|
+
n.log("acquired");
|
|
20
|
+
try {
|
|
21
|
+
return await t();
|
|
22
|
+
} finally {
|
|
23
|
+
n.log("released");
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}, v = async (e, t, n, r, a, o, c, l, d) => _(e, async () => {
|
|
27
|
+
let f = u("utxoDecrypt.local");
|
|
28
|
+
d += 1;
|
|
29
|
+
let p = o.slice(d);
|
|
30
|
+
f.log("batchProcess-start", {
|
|
31
|
+
outputsSlice: p.length,
|
|
32
|
+
lastOutputIndex: d
|
|
33
|
+
});
|
|
34
|
+
let { lastOutput: m, additionalEncryptedOutputs: h } = await e.utxoUtils.batchProcess(p, l, t, n);
|
|
35
|
+
f.log("batchProcess-done", { additionalEncryptedOutputs: h.length });
|
|
36
|
+
let g = [...c, ...h];
|
|
37
|
+
await i(e, r), s({
|
|
38
|
+
lastOutput: m,
|
|
39
|
+
encryptedOutputs: g
|
|
40
|
+
}, e, n, a), f.log("cache-write");
|
|
41
|
+
let _ = await e.utxoUtils.buildBatchProcess(g, t, n);
|
|
42
|
+
return f.log("buildBatchProcess-done", { utxoCount: _.length }), _;
|
|
43
|
+
}), y = async (e, t, n, r) => {
|
|
44
|
+
let { utxos: i, encryptedOutputs: a, lastOutput: o } = await d(e, n, t);
|
|
23
45
|
return s({
|
|
24
46
|
encryptedOutputs: a,
|
|
25
47
|
lastOutput: o
|
|
26
48
|
}, e, n, r), i;
|
|
27
|
-
},
|
|
49
|
+
}, b = async (e, t, n, r, i, a, o, s, c) => {
|
|
28
50
|
try {
|
|
29
|
-
return await
|
|
51
|
+
return await y(e, t, n, i);
|
|
30
52
|
} catch {
|
|
31
|
-
return
|
|
53
|
+
return v(e, t, n, r, i, a, o, s, c);
|
|
32
54
|
}
|
|
33
|
-
},
|
|
55
|
+
}, x = (e) => {
|
|
34
56
|
if (Array.isArray(e)) return e;
|
|
35
57
|
if (e !== void 0) return [e];
|
|
36
|
-
},
|
|
58
|
+
}, S = async (e, t, n) => await e.utxoUtils.batchFilterUtxosWithNullifier(t, n), C = (e, t, n, i, a, o, s, c) => {
|
|
37
59
|
for (e.sort((e, t) => e.amount <= t.amount ? 1 : -1); e.length < t || e.length > t && e.length < 6;) if (e.push(new r({
|
|
38
60
|
amount: 0n,
|
|
39
61
|
erc20TokenAddress: o,
|
|
@@ -42,17 +64,26 @@ var h = new m(), g = async (e, t) => e.allowParallelBalanceLocalDecryption ? t()
|
|
|
42
64
|
spendingPublicKey: a,
|
|
43
65
|
isNewStyle: !0
|
|
44
66
|
})), n) for (; e.length > 6;) e.splice(e.length - 1);
|
|
45
|
-
},
|
|
46
|
-
let
|
|
47
|
-
s && o(e, t,
|
|
48
|
-
let { encryptedOutputs:
|
|
49
|
-
await i(e,
|
|
50
|
-
let
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
67
|
+
}, w = async ({ hinkal: e, chainId: t, passedShieldedPublicKey: n = void 0, ethAddress: r = void 0, resetCacheBefore: s = !1, allowRemoteDecryption: c = !1, useBlockedUtxos: l = !1 }) => {
|
|
68
|
+
let d = r ?? await e.getEthereumAddress(), f = n ?? e.userKeys.getShieldedPublicKey(), { userKeys: p } = e, m = e.encryptedOutputsByChain[t], h = e.nullifiersByChain[t], g = Array.from(m);
|
|
69
|
+
s && o(e, t, f);
|
|
70
|
+
let { encryptedOutputs: _, lastOutput: y } = a(e, t, f);
|
|
71
|
+
await i(e, d);
|
|
72
|
+
let x = y !== "", C = g.findIndex((e) => e.value === y), w = C > -1, T = !x && C === -1, E = e.areMerkleTreeUpdatesDisabled(), D = !E && (w || !c && T), O = !E && c && T, k = !D && !O, A = u("getInputUtxoAndBalance");
|
|
73
|
+
A.log("branch", {
|
|
74
|
+
decryptLocally: D,
|
|
75
|
+
decryptRemotely: O,
|
|
76
|
+
useCache: k,
|
|
77
|
+
hinkalOutputs: g.length,
|
|
78
|
+
cachedEncryptedOutputs: _.length,
|
|
79
|
+
lastOutputIndex: C
|
|
80
|
+
});
|
|
81
|
+
let j = [];
|
|
82
|
+
D ? (j = await v(e, p, t, d, f, g, _, y, C), A.log("decryptLocally-done", { utxoCount: j.length })) : O ? (j = await b(e, p, t, d, f, g, _, y, C), A.log("decryptRemotely-done", { utxoCount: j.length })) : k && (j = await e.utxoUtils.buildBatchProcess(_, p, t), A.log("useCache-buildBatchProcess-done", { utxoCount: j.length })), await i(e, d);
|
|
83
|
+
let M = await S(e, j, h);
|
|
84
|
+
return A.log("filterSpentUtxos-done", { inputUtxos: M.length }), { inputUtxos: l ? M.filter((e) => e.isBlocked) : M.filter((e) => !e.isBlocked) };
|
|
85
|
+
}, T = async ({ hinkal: e, chainId: n, passedShieldedPublicKey: r = void 0, ethAddress: i = void 0, resetCacheBefore: a = !1, allowRemoteDecryption: o = !1 }) => {
|
|
86
|
+
let s = i ?? await e.getEthereumAddressByChain(n), c = f(t(n) ? m(s) : s), { inputUtxos: l } = await w({
|
|
56
87
|
hinkal: e,
|
|
57
88
|
chainId: n,
|
|
58
89
|
passedShieldedPublicKey: r,
|
|
@@ -62,20 +93,20 @@ var h = new m(), g = async (e, t) => e.allowParallelBalanceLocalDecryption ? t()
|
|
|
62
93
|
useBlockedUtxos: !0
|
|
63
94
|
});
|
|
64
95
|
if (!l.length) return { inputUtxos: [] };
|
|
65
|
-
let { indexes: u } = await
|
|
96
|
+
let { indexes: u } = await p({
|
|
66
97
|
hashedEthereumAddress: c,
|
|
67
98
|
nullifiers: l.map((e) => e.getNullifier())
|
|
68
99
|
});
|
|
69
100
|
return { inputUtxos: u.map((e) => l[e]).filter((e) => !!e) };
|
|
70
|
-
},
|
|
71
|
-
let g = s ?? t.userKeys.getShieldedPrivateKey(), _ = t.userKeys.getSpendingKeyPair().pubSpendingBJJPoint, { inputUtxos: v } = h ? await
|
|
101
|
+
}, E = async ({ hinkal: t, tokenWithId: r, minInput: i = 2, sliceIfMore6: a = !0, chainId: o, passedShieldedPrivateKey: s = void 0, passedShieldedPublicKey: u = void 0, ethAddress: d = void 0, resetCacheBefore: f = !1, ensuredTokensWithId: p, allowRemoteDecryption: m = !1, useBlockedUtxos: h = !1 }) => {
|
|
102
|
+
let g = s ?? t.userKeys.getShieldedPrivateKey(), _ = t.userKeys.getSpendingKeyPair().pubSpendingBJJPoint, { inputUtxos: v } = h ? await T({
|
|
72
103
|
hinkal: t,
|
|
73
104
|
chainId: o,
|
|
74
105
|
ethAddress: d,
|
|
75
106
|
passedShieldedPrivateKey: s,
|
|
76
107
|
resetCacheBefore: f,
|
|
77
108
|
allowRemoteDecryption: m
|
|
78
|
-
}) : await
|
|
109
|
+
}) : await w({
|
|
79
110
|
hinkal: t,
|
|
80
111
|
tokenWithId: r,
|
|
81
112
|
minInput: i,
|
|
@@ -86,27 +117,27 @@ var h = new m(), g = async (e, t) => e.allowParallelBalanceLocalDecryption ? t()
|
|
|
86
117
|
ethAddress: d,
|
|
87
118
|
resetCacheBefore: f,
|
|
88
119
|
allowRemoteDecryption: m
|
|
89
|
-
}), y =
|
|
120
|
+
}), y = x(r), b = /* @__PURE__ */ new Map();
|
|
90
121
|
v.forEach((e) => {
|
|
91
122
|
let t = e.getTokenAddress(o);
|
|
92
123
|
if (y && !y.some((e) => n(e.erc20TokenAddress, t))) return;
|
|
93
124
|
let r = c(o, {
|
|
94
125
|
erc20TokenAddress: t,
|
|
95
126
|
tokenId: 0
|
|
96
|
-
}), i =
|
|
97
|
-
|
|
127
|
+
}), i = b.get(r) ?? [];
|
|
128
|
+
b.set(r, [...i, e]);
|
|
98
129
|
}), p?.forEach((e) => {
|
|
99
130
|
let t = c(o, {
|
|
100
131
|
erc20TokenAddress: e.erc20TokenAddress,
|
|
101
132
|
tokenId: 0
|
|
102
133
|
});
|
|
103
|
-
|
|
134
|
+
b.set(t, b.get(t) ?? []);
|
|
104
135
|
});
|
|
105
|
-
for (let [t, n] of
|
|
136
|
+
for (let [t, n] of b) {
|
|
106
137
|
let r = t.split("-")[0], s = Number(t.split("-")[1]), c = e(o), u = c ? r : void 0;
|
|
107
|
-
|
|
138
|
+
C(n, i, a, g, _, c ? l(r).compressedAddress.toLowerCase() : r.toLowerCase(), s, u);
|
|
108
139
|
}
|
|
109
|
-
return
|
|
140
|
+
return b;
|
|
110
141
|
};
|
|
111
142
|
//#endregion
|
|
112
|
-
export {
|
|
143
|
+
export { b as attemptGetInputUtxosRemotely, w as getInputUtxoAndBalance, T as getInputUtxoAndBalanceOfStuckUtxos, E as getInputUtxoAndBalancePerToken, y as getInputUtxosRemotely };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
require(`../../../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../../constants/chains.constants.cjs`),t=require(`../../../data-structures/crypto-keys/keys.cjs`),n=require(`../../../data-structures/utxo/Utxo.cjs`),r=require(`../../utils/encryptInputForEnclave.cjs`),i=require(`../../utils/solanaMint.utils.cjs`),a=require(`../../utils/customEnclaveFunctionsRegister.cjs`);let
|
|
1
|
+
require(`../../../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../../constants/chains.constants.cjs`),t=require(`../../../data-structures/crypto-keys/keys.cjs`),n=require(`../../../data-structures/utxo/Utxo.cjs`),r=require(`../../utils/encryptInputForEnclave.cjs`),i=require(`../../utils/solanaMint.utils.cjs`),a=require(`../../utils/customEnclaveFunctionsRegister.cjs`),o=require(`../../utils/hinkalTiming.utils.cjs`);let s=require(`ethers`),c=require(`@solana/web3.js`);var l=e=>{if(!e)return;let t=s.ethers.getBytes(e);if(t.length!==32)return;let n=new c.PublicKey(t),r=n.toBase58(),{compressedAddress:a}=i.formatMintAddress(n);return{mintBase58:r,compressedAddress:a}},u=async(e,t,n)=>{let{keyCiphertext:i,inputCiphertext:a}=await r.makeEnclaveHandshakeAndEncryptUint8Array(n);return e.getAPI().decryptUtxoEnclaveCall(t,i,a)},d=async(r,i,c)=>{let d=o.createHinkalTiming(`getInputUtxosEnclave`),f=c.getShieldedPrivateKey(),{privateKey:p,publicKey:m}=t.UserKeys.getEncryptionKeyPair(f),h=c.getSpendingKeyPair().pubSpendingBJJPoint,g=s.ethers.solidityPacked([`uint256`,`uint256`,`uint256`],[m,p,f]),_=s.ethers.getBytes(g),v=a.getCustomUtxoDecryptor(),{utxos:y,lastOutput:b,encryptedOutputs:x}=await(v?v(i,_):u(r,i,_));return d.log(`decrypt-call-done`,{utxoCount:y.length}),{utxos:y.map(t=>{let r=t;if(e.isSolanaLike(i)){let e=l(t.mintAddress);if(!e)throw Error(`Invalid mint address`);r={...r,erc20TokenAddress:e.compressedAddress,mintAddress:e.mintBase58}}return new n.Utxo({...r,amount:BigInt(r.amount),randomization:r.randomization?BigInt(r.randomization):void 0,nullifyingKey:f,spendingPublicKey:h,isNewStyle:r.isNewStyle,H0:r.H0?[BigInt(r.H0[0]),BigInt(r.H0[1])]:void 0})}),encryptedOutputs:x,lastOutput:b}};exports.getInputUtxosEnclave=d;
|
|
@@ -4,36 +4,37 @@ import { Utxo as n } from "../../../data-structures/utxo/Utxo.mjs";
|
|
|
4
4
|
import { makeEnclaveHandshakeAndEncryptUint8Array as r } from "../../utils/encryptInputForEnclave.mjs";
|
|
5
5
|
import { formatMintAddress as i } from "../../utils/solanaMint.utils.mjs";
|
|
6
6
|
import { getCustomUtxoDecryptor as a } from "../../utils/customEnclaveFunctionsRegister.mjs";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { createHinkalTiming as o } from "../../utils/hinkalTiming.utils.mjs";
|
|
8
|
+
import { ethers as s } from "ethers";
|
|
9
|
+
import { PublicKey as c } from "@solana/web3.js";
|
|
9
10
|
//#region libs/shared/common/src/functions/web3/events/getInputUtxosEnclave.ts
|
|
10
|
-
var
|
|
11
|
+
var l = (e) => {
|
|
11
12
|
if (!e) return;
|
|
12
|
-
let t =
|
|
13
|
+
let t = s.getBytes(e);
|
|
13
14
|
if (t.length !== 32) return;
|
|
14
|
-
let n = new
|
|
15
|
+
let n = new c(t), r = n.toBase58(), { compressedAddress: a } = i(n);
|
|
15
16
|
return {
|
|
16
17
|
mintBase58: r,
|
|
17
18
|
compressedAddress: a
|
|
18
19
|
};
|
|
19
|
-
},
|
|
20
|
+
}, u = async (e, t, n) => {
|
|
20
21
|
let { keyCiphertext: i, inputCiphertext: a } = await r(n);
|
|
21
22
|
return e.getAPI().decryptUtxoEnclaveCall(t, i, a);
|
|
22
|
-
},
|
|
23
|
-
let
|
|
23
|
+
}, d = async (r, i, c) => {
|
|
24
|
+
let d = o("getInputUtxosEnclave"), f = c.getShieldedPrivateKey(), { privateKey: p, publicKey: m } = t.getEncryptionKeyPair(f), h = c.getSpendingKeyPair().pubSpendingBJJPoint, g = s.solidityPacked([
|
|
24
25
|
"uint256",
|
|
25
26
|
"uint256",
|
|
26
27
|
"uint256"
|
|
27
28
|
], [
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
]),
|
|
32
|
-
return {
|
|
33
|
-
utxos:
|
|
29
|
+
m,
|
|
30
|
+
p,
|
|
31
|
+
f
|
|
32
|
+
]), _ = s.getBytes(g), v = a(), { utxos: y, lastOutput: b, encryptedOutputs: x } = await (v ? v(i, _) : u(r, i, _));
|
|
33
|
+
return d.log("decrypt-call-done", { utxoCount: y.length }), {
|
|
34
|
+
utxos: y.map((t) => {
|
|
34
35
|
let r = t;
|
|
35
36
|
if (e(i)) {
|
|
36
|
-
let e =
|
|
37
|
+
let e = l(t.mintAddress);
|
|
37
38
|
if (!e) throw Error("Invalid mint address");
|
|
38
39
|
r = {
|
|
39
40
|
...r,
|
|
@@ -45,15 +46,15 @@ var c = (e) => {
|
|
|
45
46
|
...r,
|
|
46
47
|
amount: BigInt(r.amount),
|
|
47
48
|
randomization: r.randomization ? BigInt(r.randomization) : void 0,
|
|
48
|
-
nullifyingKey:
|
|
49
|
-
spendingPublicKey:
|
|
49
|
+
nullifyingKey: f,
|
|
50
|
+
spendingPublicKey: h,
|
|
50
51
|
isNewStyle: r.isNewStyle,
|
|
51
52
|
H0: r.H0 ? [BigInt(r.H0[0]), BigInt(r.H0[1])] : void 0
|
|
52
53
|
});
|
|
53
54
|
}),
|
|
54
|
-
encryptedOutputs:
|
|
55
|
-
lastOutput:
|
|
55
|
+
encryptedOutputs: x,
|
|
56
|
+
lastOutput: b
|
|
56
57
|
};
|
|
57
58
|
};
|
|
58
59
|
//#endregion
|
|
59
|
-
export {
|
|
60
|
+
export { d as getInputUtxosEnclave };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
require(`../../../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../../constants/vite.constants.cjs`),t=require(`../../../constants/chains.constants.cjs`),n=require(`../../utils/caseInsensitive.utils.cjs`),r=require(`../../../error-handling/error-codes.constants.cjs`),i=require(`../etherFunctions.cjs`),a=require(`../../../data-structures/utxo/Utxo.cjs`),o=require(`../../../error-handling/customErrors/ErrorWithAmount.cjs`);require(`../../../error-handling/customErrors/index.cjs`);const s=require(`../../utils/encodeTokenWithId.cjs`),c=require(`../../utils/solanaMint.utils.cjs`),l=require(`./getInputUtxoAndBalance.cjs`),
|
|
1
|
+
require(`../../../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../../constants/vite.constants.cjs`),t=require(`../../../constants/chains.constants.cjs`),n=require(`../../utils/caseInsensitive.utils.cjs`),r=require(`../../../error-handling/error-codes.constants.cjs`),i=require(`../etherFunctions.cjs`),a=require(`../../../data-structures/utxo/Utxo.cjs`),o=require(`../../../error-handling/customErrors/ErrorWithAmount.cjs`);require(`../../../error-handling/customErrors/index.cjs`);const s=require(`../../utils/encodeTokenWithId.cjs`),c=require(`../../utils/solanaMint.utils.cjs`),l=require(`../../utils/hinkalTiming.utils.cjs`),u=require(`./getInputUtxoAndBalance.cjs`),d=require(`../../../data-structures/TokenDBs/PrivateTokensDB.cjs`),f=require(`../../utils/map.utils.cjs`),p=require(`../../utils/mutexes.utils.cjs`),m=require(`../../utils/erc20tokenFunctions.cjs`);let h=require(`async-mutex`);var g=async(t,r,i,a,o,s=!1,c,l=!1,g=!1)=>f.getOrCreateMapValue(t.balanceFetchingMutexByChain,r,()=>new h.Mutex).runExclusive(async()=>p.getChainBalanceFetchingMutex(r).runShared(async()=>{let{inputUtxos:f}=g?await u.getInputUtxoAndBalanceOfStuckUtxos({hinkal:t,chainId:r,ethAddress:o,passedShieldedPrivateKey:i,resetCacheBefore:s,allowRemoteDecryption:c}):await u.getInputUtxoAndBalance({hinkal:t,sliceIfMore6:!1,chainId:r,passedShieldedPrivateKey:i,passedShieldedPublicKey:a,ethAddress:o,resetCacheBefore:s,allowRemoteDecryption:c});l&&e.isExtension&&await d.privateTokensDB.fetchAndUpdatePrivateTokens(f.map(e=>e.getTokenAddress(r)).filter(e=>e!==void 0),r,o);let p=(e.isExtension?await d.privateTokensDB.getPrivateTokens(r,o):await m.getErc20TokensForChainAPI(r)).map(e=>({token:e,balance:f.reduce((t,i)=>{let a=e.erc20TokenAddress;return n.caseInsensitiveEqual(a,i.getTokenAddress(r))?t+i.amount:t},0n),timestamp:f.filter(t=>n.caseInsensitiveEqual(t.getTokenAddress(r),e.erc20TokenAddress))[0]?.timeStamp,nfts:[]})),h=new Map;return p.forEach(e=>{h.set(e.token.erc20TokenAddress.toLowerCase(),e)}),h})),_=async(e,n,d,f,p=6,h=d.map(()=>0),g=!1,_=!1)=>{let v=l.createHinkalTiming(`addPaddingToUtxos`),{userKeys:y}=e,b=y.getSpendingKeyPair().pubSpendingBJJPoint,x=[],S=[];if(d.length===0)return[];if(g){for(let e=0;e<d.length;e+=1){let r=t.isSolanaLike(n),i=r?d[e]:void 0,o=r?c.formatMintAddress(d[e]).compressedAddress:d[e];x.push(Array.from({length:p},()=>new a.Utxo({amount:0n,erc20TokenAddress:o,mintAddress:i,nullifyingKey:y.getShieldedPrivateKey(),spendingPublicKey:b,isNewStyle:!0})))}return x}let C=await u.getInputUtxoAndBalancePerToken({hinkal:e,sliceIfMore6:!1,ensuredTokensWithId:d.map((e,t)=>({erc20TokenAddress:d[t],tokenId:h[t]})),allowRemoteDecryption:e.generateProofRemotely,useBlockedUtxos:_,chainId:n});v.log(`getInputUtxoAndBalancePerToken`);let w=0;for(let e=0;e<d.length;e+=1){let t=C.get(s.encodeTokenWithId(n,{erc20TokenAddress:d[e],tokenId:h[e]}))??[];if(!t)throw Error(`Missing utxos for token`);w=Math.max(t.length,w),S.push(t)}if(w===2)return S;for(let[e,s]of S.entries())if(s.length>p){let t=s.slice(0,p),a=t.reduce((e,t)=>e+t.amount,0n);if(f[e]<0n&&a<-f[e]){let t=await m.getErc20TokenFromAPI(n,d[e]);if(!t)throw Error(`Token not found`);let s=t.decimals===18?6:2,c=Number(i.getAmountWithPrecision(a,t,s));throw new o.ErrorWithAmount(c,`${r.DIRECTLY_SHOW_MESSAGE_ERROR_CODES.UTXO_LIMITATIONS}. Please try again with ${c} ${t.symbol}, including gas fees.`)}x.push(t)}else if(s.length<=p){let r=[...s],i=p-s.length;for(;i>0;){--i;let o=t.isSolanaLike(n),s=o?d[e]:void 0,l=o?c.formatMintAddress(d[e]).compressedAddress:d[e];r.push(new a.Utxo({amount:0n,erc20TokenAddress:l,mintAddress:s,nullifyingKey:y.getShieldedPrivateKey(),spendingPublicKey:b,isNewStyle:!0}))}x.push(r)}return v.log(`padding-done`,{tokenCount:x.length}),x};exports.addPaddingToUtxos=_,exports.getShieldedBalance=g;
|