@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.
Files changed (32) hide show
  1. package/common/src/data-structures/Hinkal/hinkalWithdraw.cjs +1 -1
  2. package/common/src/data-structures/Hinkal/hinkalWithdraw.mjs +61 -37
  3. package/common/src/data-structures/MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.cjs +1 -1
  4. package/common/src/data-structures/MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.mjs +42 -26
  5. package/common/src/functions/pre-transaction/buildCommitmentValidationData.cjs +1 -1
  6. package/common/src/functions/pre-transaction/buildCommitmentValidationData.mjs +42 -38
  7. package/common/src/functions/snarkjs/constructGeneralZkProof.cjs +1 -1
  8. package/common/src/functions/snarkjs/constructGeneralZkProof.mjs +81 -70
  9. package/common/src/functions/snarkjs/generateMainAndCommitmentZkProof.cjs +1 -1
  10. package/common/src/functions/snarkjs/generateMainAndCommitmentZkProof.mjs +21 -10
  11. package/common/src/functions/snarkjs/generateZkProof.cjs +1 -1
  12. package/common/src/functions/snarkjs/generateZkProof.mjs +24 -13
  13. package/common/src/functions/snarkjs/generateZkProofEnclave.cjs +1 -1
  14. package/common/src/functions/snarkjs/generateZkProofEnclave.mjs +14 -11
  15. package/common/src/functions/snarkjs/generateZkProofSelf.cjs +1 -1
  16. package/common/src/functions/snarkjs/generateZkProofSelf.mjs +22 -16
  17. package/common/src/functions/utils/hinkalTiming.utils.cjs +1 -0
  18. package/common/src/functions/utils/hinkalTiming.utils.mjs +18 -0
  19. package/common/src/functions/web3/events/getInputUtxoAndBalance.cjs +1 -1
  20. package/common/src/functions/web3/events/getInputUtxoAndBalance.mjs +73 -42
  21. package/common/src/functions/web3/events/getInputUtxosEnclave.cjs +1 -1
  22. package/common/src/functions/web3/events/getInputUtxosEnclave.mjs +21 -20
  23. package/common/src/functions/web3/events/getShieldedBalance.cjs +1 -1
  24. package/common/src/functions/web3/events/getShieldedBalance.mjs +56 -53
  25. package/common/src/webworker/package.json +1 -1
  26. package/common/src/webworker/performTaskWithWorker.cjs +1 -1
  27. package/common/src/webworker/performTaskWithWorker.mjs +18 -17
  28. package/common/src/webworker/viteWorkerURL.constant.cjs +3 -3
  29. package/common/src/webworker/viteWorkerURL.constant.mjs +3 -3
  30. package/package.json +1 -1
  31. package/sdk/package.json.cjs +1 -1
  32. package/sdk/package.json.mjs +1 -1
@@ -1,16 +1,27 @@
1
- import { buildCommitmentValidationData as e, buildCommitmentValidationDataFromProof as t } from "../pre-transaction/buildCommitmentValidationData.mjs";
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 n } from "./generateZkProof.mjs";
4
+ import { generateZkProof as r } from "./generateZkProof.mjs";
4
5
  //#region libs/shared/common/src/functions/snarkjs/generateMainAndCommitmentZkProof.ts
5
- var r = async (r, i, a, o, s, c, l) => {
6
- let u = await e(r, i, a, o), d = [s], f = [c];
7
- u && (d.push(u.verifierName), f.push(u.commitmentInput));
8
- let p = await n(r, d, f, l), { zkCallData: m, publicSignals: h } = p[0];
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: m,
11
- publicSignals: h,
12
- commitmentValidationData: t(u, p[1])
21
+ zkCallData: g,
22
+ publicSignals: _,
23
+ commitmentValidationData: n(f, h[1])
13
24
  };
14
25
  };
15
26
  //#endregion
16
- export { r as generateMainAndCommitmentZkProof };
27
+ export { i as generateMainAndCommitmentZkProof };
@@ -1 +1 @@
1
- const e=require(`../utils/customEnclaveFunctionsRegister.cjs`),t=require(`../../error-handling/logger.cjs`),n=require(`./getZKFiles.cjs`),r=require(`./generateZkProofEnclave.cjs`),i=require(`./generateZkProofSelf.cjs`);var a=async(a,o,s,c)=>{if(c)try{let t=e.getCustomProofGenerator();return t?(await t(s,o.map(e=>n.getWASMFile(e,a)),o.map(e=>n.getZKeyFile(e,a)))).map(({zkCallData:e,publicSignals:t})=>({zkCallData:e,publicSignals:t})):await r.generateZkProofEnclave(a,o,s)}catch(e){t.Logger.error(`enclave proof error`,e)}return Promise.all(o.map((e,t)=>i.generateZkProofSelf(a,e,s[t])))};exports.generateZkProof=a;
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 { Logger as t } from "../../error-handling/logger.mjs";
3
- import { getWASMFile as n, getZKeyFile as r } from "./getZKFiles.mjs";
4
- import { generateZkProofEnclave as i } from "./generateZkProofEnclave.mjs";
5
- import { generateZkProofSelf as a } from "./generateZkProofSelf.mjs";
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 o = async (o, s, c, l) => {
8
- if (l) try {
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
- return t ? (await t(c, s.map((e) => n(e, o)), s.map((e) => r(e, o)))).map(({ zkCallData: e, publicSignals: t }) => ({
11
- zkCallData: e,
12
- publicSignals: t
13
- })) : await i(o, s, c);
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
- t.error("enclave proof error", e);
25
+ n.error("enclave proof error", e), d.log("enclave-failed-fallback-self");
16
26
  }
17
- return Promise.all(s.map((e, t) => a(o, e, c[t])));
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 { o as generateZkProof };
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 i=e=>{if(e.startsWith(`http`)){let t=e.split(`/`).at(-1);if(!t)throw Error(`invalid verifierName`);return t}return e},a=async(a,o,s)=>{let c=o.map(e=>i(r.getWASMFile(e,a))),l=o.map(e=>i(r.getZKeyFile(e,a))),{keyCiphertext:u,inputCiphertext:d}=await n.makeEnclaveHandshakeAndEncryptInput(s),f={circuit_wasms:c,circuit_zkeys:l,inputs:d,key:u};return(await t.httpClient.post(`${e.ENCLAVE_URL}/generate-proofs`,f)).map(({zk_calldata:e,public_signals:t})=>({zkCallData:e,publicSignals:t}))};exports.generateZkProofEnclave=a;
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 { getWASMFile as r, getZKeyFile as i } from "./getZKFiles.mjs";
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 a = (e) => {
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
- }, o = async (o, s, c) => {
15
- let l = s.map((e) => a(r(e, o))), u = s.map((e) => a(i(e, o))), { keyCiphertext: d, inputCiphertext: f } = await n(c), p = {
16
- circuit_wasms: l,
17
- circuit_zkeys: u,
18
- inputs: f,
19
- key: d
20
- };
21
- return (await t.post(`${e}/generate-proofs`, p)).map(({ zk_calldata: e, public_signals: t }) => ({
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 { o as generateZkProofEnclave };
30
+ export { s as generateZkProofEnclave };
@@ -1 +1 @@
1
- const e=require(`../../webworker/worker.registry.cjs`),t=require(`../../webworker/performTaskWithWorker.cjs`),n=require(`./getZKFiles.cjs`),r=require(`../../webworker/snarkjsWorker/snarkjsWorker.types.cjs`);var i=async(i,a,o)=>{let s=n.getWASMFile(a,i),c=n.getZKeyFile(a,i),{publicSignals:l,zkCallData:u}=await t.performTaskWithWorker({type:e.WorkerVariant.SnarkJS,payload:{type:r.SnarkJSWorkerActionType.FULL_PROVE,data:{input:o,chainId:i,wasmFilePath:s,zKeyFilePath:c}}});return{zkCallData:u,publicSignals:l}};exports.generateZkProofSelf=i;
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 { WorkerVariant as e } from "../../webworker/worker.registry.mjs";
2
- import { performTaskWithWorker as t } from "../../webworker/performTaskWithWorker.mjs";
3
- import { getWASMFile as n, getZKeyFile as r } from "./getZKFiles.mjs";
4
- import { SnarkJSWorkerActionType as i } from "../../webworker/snarkjsWorker/snarkjsWorker.types.mjs";
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 a = async (a, o, s) => {
7
- let c = n(o, a), l = r(o, a), { publicSignals: u, zkCallData: d } = await t({
8
- type: e.SnarkJS,
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: i.FULL_PROVE,
16
+ type: a.FULL_PROVE,
11
17
  data: {
12
- input: s,
13
- chainId: a,
14
- wasmFilePath: c,
15
- zKeyFilePath: l
18
+ input: c,
19
+ chainId: o,
20
+ wasmFilePath: u,
21
+ zKeyFilePath: d
16
22
  }
17
23
  }
18
24
  });
19
- return {
20
- zkCallData: d,
21
- publicSignals: u
25
+ return l.log("worker-done"), {
26
+ zkCallData: p,
27
+ publicSignals: f
22
28
  };
23
29
  };
24
30
  //#endregion
25
- export { a as generateZkProofSelf };
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`),c=require(`../../utils/addresses.cjs`),l=require(`../../../API/scheduled-transactions-calls.cjs`),u=require(`../../utils/tron.utils.cjs`);var d=new(require(`async-mutex`)).Mutex,f=async(e,t)=>e.allowParallelBalanceLocalDecryption?t():d.runExclusive(t),p=async(e,t,n,a,o,s,c,l,u)=>f(e,async()=>{u+=1;let d=s.slice(u),{lastOutput:f,additionalEncryptedOutputs:p}=await e.utxoUtils.batchProcess(d,l,t,n),m=[...c,...p];return await r.checkAddressNotUpdated(e,a),i.setHinkalCache({lastOutput:f,encryptedOutputs:m},e,n,o),await e.utxoUtils.buildBatchProcess(m,t,n)}),m=async(e,t,n,r)=>{let{utxos:a,encryptedOutputs:o,lastOutput:c}=await s.getInputUtxosEnclave(e,n,t);return i.setHinkalCache({encryptedOutputs:o,lastOutput:c},e,n,r),a},h=async(e,t,n,r,i,a,o,s,c)=>{try{return await m(e,t,n,i)}catch{return p(e,t,n,r,i,a,o,s,c)}},g=e=>{if(Array.isArray(e))return e;if(e!==void 0)return[e]},_=async(e,t,n)=>await e.utxoUtils.batchFilterUtxosWithNullifier(t,n),v=(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)},y=async({hinkal:e,chainId:t,passedShieldedPublicKey:n=void 0,ethAddress:a=void 0,resetCacheBefore:o=!1,allowRemoteDecryption:s=!1,useBlockedUtxos:c=!1})=>{let l=a??await e.getEthereumAddress(),u=n??e.userKeys.getShieldedPublicKey(),{userKeys:d}=e,f=e.encryptedOutputsByChain[t],m=e.nullifiersByChain[t],g=Array.from(f);o&&i.resetCache(e,t,u);let{encryptedOutputs:v,lastOutput:y}=i.getHinkalCache(e,t,u);await r.checkAddressNotUpdated(e,l);let b=y!==``,x=g.findIndex(e=>e.value===y),S=x>-1,C=!b&&x===-1,w=e.areMerkleTreeUpdatesDisabled(),T=!w&&(S||!s&&C),E=!w&&s&&C,D=!T&&!E,O=[];T?O=await p(e,d,t,l,u,g,v,y,x):E?O=await h(e,d,t,l,u,g,v,y,x):D&&(O=await e.utxoUtils.buildBatchProcess(v,d,t)),await r.checkAddressNotUpdated(e,l);let k=await _(e,O,m);return{inputUtxos:c?k.filter(e=>e.isBlocked):k.filter(e=>!e.isBlocked)}},b=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),d=c.hashEthereumAddress(e.isTronLike(n)?u.addressToHexFormat(s):s),{inputUtxos:f}=await y({hinkal:t,chainId:n,passedShieldedPublicKey:r,ethAddress:s,resetCacheBefore:a,allowRemoteDecryption:o,useBlockedUtxos:!0});if(!f.length)return{inputUtxos:[]};let{indexes:p}=await l.getScheduledTransactionsNullifierIndexes({hashedEthereumAddress:d,nullifiers:f.map(e=>e.getNullifier())});return{inputUtxos:p.map(e=>f[e]).filter(e=>!!e)}},x=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 _=l??n.userKeys.getShieldedPrivateKey(),x=n.userKeys.getSpendingKeyPair().pubSpendingBJJPoint,{inputUtxos:S}=h?await b({hinkal:n,chainId:c,ethAddress:d,passedShieldedPrivateKey:l,resetCacheBefore:f,allowRemoteDecryption:m}):await y({hinkal:n,tokenWithId:r,minInput:i,sliceIfMore6:s,chainId:c,passedShieldedPrivateKey:l,passedShieldedPublicKey:u,ethAddress:d,resetCacheBefore:f,allowRemoteDecryption:m}),C=g(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;v(n,i,s,_,x,l?o.formatMintAddress(r).compressedAddress.toLowerCase():r.toLowerCase(),a,u)}return w};exports.attemptGetInputUtxosRemotely=h,exports.getInputUtxoAndBalance=y,exports.getInputUtxoAndBalanceOfStuckUtxos=b,exports.getInputUtxoAndBalancePerToken=x,exports.getInputUtxosRemotely=m;
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 { getInputUtxosEnclave as u } from "./getInputUtxosEnclave.mjs";
9
- import { hashEthereumAddress as d } from "../../utils/addresses.mjs";
10
- import { getScheduledTransactionsNullifierIndexes as f } from "../../../API/scheduled-transactions-calls.mjs";
11
- import { addressToHexFormat as p } from "../../utils/tron.utils.mjs";
12
- import { Mutex as m } from "async-mutex";
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 h = new m(), g = async (e, t) => e.allowParallelBalanceLocalDecryption ? t() : h.runExclusive(t), _ = async (e, t, n, r, a, o, c, l, u) => g(e, async () => {
15
- u += 1;
16
- let d = o.slice(u), { lastOutput: f, additionalEncryptedOutputs: p } = await e.utxoUtils.batchProcess(d, l, t, n), m = [...c, ...p];
17
- return await i(e, r), s({
18
- lastOutput: f,
19
- encryptedOutputs: m
20
- }, e, n, a), await e.utxoUtils.buildBatchProcess(m, t, n);
21
- }), v = async (e, t, n, r) => {
22
- let { utxos: i, encryptedOutputs: a, lastOutput: o } = await u(e, n, t);
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
- }, y = async (e, t, n, r, i, a, o, s, c) => {
49
+ }, b = async (e, t, n, r, i, a, o, s, c) => {
28
50
  try {
29
- return await v(e, t, n, i);
51
+ return await y(e, t, n, i);
30
52
  } catch {
31
- return _(e, t, n, r, i, a, o, s, c);
53
+ return v(e, t, n, r, i, a, o, s, c);
32
54
  }
33
- }, b = (e) => {
55
+ }, x = (e) => {
34
56
  if (Array.isArray(e)) return e;
35
57
  if (e !== void 0) return [e];
36
- }, x = async (e, t, n) => await e.utxoUtils.batchFilterUtxosWithNullifier(t, n), S = (e, t, n, i, a, o, s, c) => {
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
- }, C = async ({ hinkal: e, chainId: t, passedShieldedPublicKey: n = void 0, ethAddress: r = void 0, resetCacheBefore: s = !1, allowRemoteDecryption: c = !1, useBlockedUtxos: l = !1 }) => {
46
- let u = r ?? await e.getEthereumAddress(), d = n ?? e.userKeys.getShieldedPublicKey(), { userKeys: f } = e, p = e.encryptedOutputsByChain[t], m = e.nullifiersByChain[t], h = Array.from(p);
47
- s && o(e, t, d);
48
- let { encryptedOutputs: g, lastOutput: v } = a(e, t, d);
49
- await i(e, u);
50
- let b = v !== "", S = h.findIndex((e) => e.value === v), C = S > -1, w = !b && S === -1, T = e.areMerkleTreeUpdatesDisabled(), E = !T && (C || !c && w), D = !T && c && w, O = !E && !D, k = [];
51
- E ? k = await _(e, f, t, u, d, h, g, v, S) : D ? k = await y(e, f, t, u, d, h, g, v, S) : O && (k = await e.utxoUtils.buildBatchProcess(g, f, t)), await i(e, u);
52
- let A = await x(e, k, m);
53
- return { inputUtxos: l ? A.filter((e) => e.isBlocked) : A.filter((e) => !e.isBlocked) };
54
- }, w = async ({ hinkal: e, chainId: n, passedShieldedPublicKey: r = void 0, ethAddress: i = void 0, resetCacheBefore: a = !1, allowRemoteDecryption: o = !1 }) => {
55
- let s = i ?? await e.getEthereumAddressByChain(n), c = d(t(n) ? p(s) : s), { inputUtxos: l } = await C({
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 f({
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
- }, T = 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 }) => {
71
- let g = s ?? t.userKeys.getShieldedPrivateKey(), _ = t.userKeys.getSpendingKeyPair().pubSpendingBJJPoint, { inputUtxos: v } = h ? await w({
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 C({
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 = b(r), x = /* @__PURE__ */ new Map();
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 = x.get(r) ?? [];
97
- x.set(r, [...i, e]);
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
- x.set(t, x.get(t) ?? []);
134
+ b.set(t, b.get(t) ?? []);
104
135
  });
105
- for (let [t, n] of x) {
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
- S(n, i, a, g, _, c ? l(r).compressedAddress.toLowerCase() : r.toLowerCase(), s, u);
138
+ C(n, i, a, g, _, c ? l(r).compressedAddress.toLowerCase() : r.toLowerCase(), s, u);
108
139
  }
109
- return x;
140
+ return b;
110
141
  };
111
142
  //#endregion
112
- export { y as attemptGetInputUtxosRemotely, C as getInputUtxoAndBalance, w as getInputUtxoAndBalanceOfStuckUtxos, T as getInputUtxoAndBalancePerToken, v as getInputUtxosRemotely };
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 o=require(`ethers`),s=require(`@solana/web3.js`);var c=e=>{if(!e)return;let t=o.ethers.getBytes(e);if(t.length!==32)return;let n=new s.PublicKey(t),r=n.toBase58(),{compressedAddress:a}=i.formatMintAddress(n);return{mintBase58:r,compressedAddress:a}},l=async(e,t,n)=>{let{keyCiphertext:i,inputCiphertext:a}=await r.makeEnclaveHandshakeAndEncryptUint8Array(n);return e.getAPI().decryptUtxoEnclaveCall(t,i,a)},u=async(r,i,s)=>{let u=s.getShieldedPrivateKey(),{privateKey:d,publicKey:f}=t.UserKeys.getEncryptionKeyPair(u),p=s.getSpendingKeyPair().pubSpendingBJJPoint,m=o.ethers.solidityPacked([`uint256`,`uint256`,`uint256`],[f,d,u]),h=o.ethers.getBytes(m),g=a.getCustomUtxoDecryptor(),{utxos:_,lastOutput:v,encryptedOutputs:y}=await(g?g(i,h):l(r,i,h));return{utxos:_.map(t=>{let r=t;if(e.isSolanaLike(i)){let e=c(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:u,spendingPublicKey:p,isNewStyle:r.isNewStyle,H0:r.H0?[BigInt(r.H0[0]),BigInt(r.H0[1])]:void 0})}),encryptedOutputs:y,lastOutput:v}};exports.getInputUtxosEnclave=u;
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 { ethers as o } from "ethers";
8
- import { PublicKey as s } from "@solana/web3.js";
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 c = (e) => {
11
+ var l = (e) => {
11
12
  if (!e) return;
12
- let t = o.getBytes(e);
13
+ let t = s.getBytes(e);
13
14
  if (t.length !== 32) return;
14
- let n = new s(t), r = n.toBase58(), { compressedAddress: a } = i(n);
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
- }, l = async (e, t, n) => {
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
- }, u = async (r, i, s) => {
23
- let u = s.getShieldedPrivateKey(), { privateKey: d, publicKey: f } = t.getEncryptionKeyPair(u), p = s.getSpendingKeyPair().pubSpendingBJJPoint, m = o.solidityPacked([
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
- f,
29
- d,
30
- u
31
- ]), h = o.getBytes(m), g = a(), { utxos: _, lastOutput: v, encryptedOutputs: y } = await (g ? g(i, h) : l(r, i, h));
32
- return {
33
- utxos: _.map((t) => {
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 = c(t.mintAddress);
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: u,
49
- spendingPublicKey: p,
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: y,
55
- lastOutput: v
55
+ encryptedOutputs: x,
56
+ lastOutput: b
56
57
  };
57
58
  };
58
59
  //#endregion
59
- export { u as getInputUtxosEnclave };
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`),u=require(`../../../data-structures/TokenDBs/PrivateTokensDB.cjs`),d=require(`../../utils/map.utils.cjs`),f=require(`../../utils/mutexes.utils.cjs`),p=require(`../../utils/erc20tokenFunctions.cjs`);let m=require(`async-mutex`);var h=async(t,r,i,a,o,s=!1,c,h=!1,g=!1)=>d.getOrCreateMapValue(t.balanceFetchingMutexByChain,r,()=>new m.Mutex).runExclusive(async()=>f.getChainBalanceFetchingMutex(r).runShared(async()=>{let{inputUtxos:d}=g?await l.getInputUtxoAndBalanceOfStuckUtxos({hinkal:t,chainId:r,ethAddress:o,passedShieldedPrivateKey:i,resetCacheBefore:s,allowRemoteDecryption:c}):await l.getInputUtxoAndBalance({hinkal:t,sliceIfMore6:!1,chainId:r,passedShieldedPrivateKey:i,passedShieldedPublicKey:a,ethAddress:o,resetCacheBefore:s,allowRemoteDecryption:c});h&&e.isExtension&&await u.privateTokensDB.fetchAndUpdatePrivateTokens(d.map(e=>e.getTokenAddress(r)).filter(e=>e!==void 0),r,o);let f=(e.isExtension?await u.privateTokensDB.getPrivateTokens(r,o):await p.getErc20TokensForChainAPI(r)).map(e=>({token:e,balance:d.reduce((t,i)=>{let a=e.erc20TokenAddress;return n.caseInsensitiveEqual(a,i.getTokenAddress(r))?t+i.amount:t},0n),timestamp:d.filter(t=>n.caseInsensitiveEqual(t.getTokenAddress(r),e.erc20TokenAddress))[0]?.timeStamp,nfts:[]})),m=new Map;return f.forEach(e=>{m.set(e.token.erc20TokenAddress.toLowerCase(),e)}),m})),g=async(e,n,u,d,f=6,m=u.map(()=>0),h=!1,g=!1)=>{let{userKeys:_}=e,v=_.getSpendingKeyPair().pubSpendingBJJPoint,y=[],b=[];if(u.length===0)return[];if(h){for(let e=0;e<u.length;e+=1){let r=t.isSolanaLike(n),i=r?u[e]:void 0,o=r?c.formatMintAddress(u[e]).compressedAddress:u[e];y.push(Array.from({length:f},()=>new a.Utxo({amount:0n,erc20TokenAddress:o,mintAddress:i,nullifyingKey:_.getShieldedPrivateKey(),spendingPublicKey:v,isNewStyle:!0})))}return y}let x=await l.getInputUtxoAndBalancePerToken({hinkal:e,sliceIfMore6:!1,ensuredTokensWithId:u.map((e,t)=>({erc20TokenAddress:u[t],tokenId:m[t]})),allowRemoteDecryption:e.generateProofRemotely,useBlockedUtxos:g,chainId:n}),S=0;for(let e=0;e<u.length;e+=1){let t=x.get(s.encodeTokenWithId(n,{erc20TokenAddress:u[e],tokenId:m[e]}))??[];if(!t)throw Error(`Missing utxos for token`);S=Math.max(t.length,S),b.push(t)}if(S===2)return b;for(let[e,s]of b.entries())if(s.length>f){let t=s.slice(0,f),a=t.reduce((e,t)=>e+t.amount,0n);if(d[e]<0n&&a<-d[e]){let t=await p.getErc20TokenFromAPI(n,u[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.`)}y.push(t)}else if(s.length<=f){let r=[...s],i=f-s.length;for(;i>0;){--i;let o=t.isSolanaLike(n),s=o?u[e]:void 0,l=o?c.formatMintAddress(u[e]).compressedAddress:u[e];r.push(new a.Utxo({amount:0n,erc20TokenAddress:l,mintAddress:s,nullifyingKey:_.getShieldedPrivateKey(),spendingPublicKey:v,isNewStyle:!0}))}y.push(r)}return y};exports.addPaddingToUtxos=g,exports.getShieldedBalance=h;
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;