@gurge/sdk 0.3.16 → 0.3.19
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/crypto/EddsaRN.cjs +1 -0
- package/common/src/crypto/EddsaRN.d.ts +19 -0
- package/common/src/crypto/EddsaRN.mjs +41 -0
- package/common/src/crypto/eddsa.cjs +1 -1
- package/common/src/crypto/eddsa.mjs +7 -5
- package/common/src/data-structures/Hinkal/hinkalWithdraw.cjs +1 -1
- package/common/src/data-structures/Hinkal/hinkalWithdraw.mjs +41 -24
- package/common/src/functions/snarkjs/constructGeneralZkProof.cjs +1 -1
- package/common/src/functions/snarkjs/constructGeneralZkProof.mjs +80 -73
- package/common/src/functions/snarkjs/generateZkProofEnclave.cjs +1 -1
- package/common/src/functions/snarkjs/generateZkProofEnclave.mjs +20 -10
- package/common/src/functions/utils/hinkalTiming.utils.cjs +1 -0
- package/common/src/functions/utils/hinkalTiming.utils.d.ts +9 -0
- package/common/src/functions/utils/hinkalTiming.utils.mjs +18 -0
- package/common/src/webworker/logError-DCl9T86l.js +2 -0
- package/common/src/webworker/package.json +1 -1
- package/common/src/webworker/performTaskWithWorker.cjs +1 -1
- package/common/src/webworker/performTaskWithWorker.mjs +21 -18
- package/common/src/webworker/utxoWorker/utxoWorkerLauncher.ts?worker&url.cjs +1 -1
- package/common/src/webworker/utxoWorker/utxoWorkerLauncher.ts?worker&url.mjs +1 -1
- package/common/src/webworker/utxoWorkerNode.cjs +1 -1
- package/common/src/webworker/viteWorkerURL.constant.cjs +3 -3
- package/common/src/webworker/viteWorkerURL.constant.mjs +3 -3
- package/common/src/webworker/zkProofWorker/zkProofWorkerLauncher.ts?worker&url.cjs +1 -1
- package/common/src/webworker/zkProofWorker/zkProofWorkerLauncher.ts?worker&url.mjs +1 -1
- package/common/src/webworker/zkProofWorkerNode.cjs +1 -1
- package/package.json +1 -1
- package/react-native/HinkalWebViewWorker.cjs +35 -7
- package/react-native/HinkalWebViewWorker.mjs +102 -53
- package/react-native/bridge/HinkalRNWorker.cjs +1 -1
- package/react-native/bridge/HinkalRNWorker.d.ts +1 -0
- package/react-native/bridge/HinkalRNWorker.mjs +11 -9
- package/sdk/package.json.cjs +1 -1
- package/sdk/package.json.mjs +1 -1
- package/common/src/webworker/logError-BTCYh_YM.js +0 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require(`../../../_virtual/_rolldown/runtime.cjs`);const e=require(`./poseidon.cjs`),t=require(`../functions/utils/bigint-math.utils.cjs`),n=require(`./babyJub.cjs`);let r=require(`@noble/hashes/blake1.js`);var i=21888242871839275222246405745257275088614511777268538073601725287587578984328n/8n,a=(e,t=0,n=e.length-t)=>{let r=0n;for(let i=n-1;i>=0;--i)r=(r<<8n)+BigInt(e[t+i]);return r},o=(e,n,r,i)=>{let a=t.mod(r);for(let t=0;t<i;t+=1)e[n+t]=Number(a&255n),a>>=8n},s=e=>t.mod(e,i),c=e=>{let t=new Uint8Array(e);return t[0]&=248,t[31]&=127,t[31]|=64,t},l=class{constructor(e,t){this.babyJub=e,this.poseidon=t}prv2pub(e){let t=a(c((0,r.blake512)(e).slice(0,32)),0,32);return this.babyJub.mulPointEscalar(this.babyJub.Base8,t/8n)}signPoseidon(e,n){let i=c((0,r.blake512)(e)),l=a(i,0,32),u=this.babyJub.mulPointEscalar(this.babyJub.Base8,l/8n),d=new Uint8Array(64);d.set(i.slice(32),0),o(d,32,n,32);let f=s(a((0,r.blake512)(d),0,64)),p=this.babyJub.mulPointEscalar(this.babyJub.Base8,f),m=this.poseidon([p[0],p[1],u[0],u[1],n]);return{R8:p,S:s(f+s(t.mod(BigInt(this.poseidon.F.toString(m)))*l))}}},u=async()=>(await e.poseidonHolder.init(),await n.jubHolder.init(),new l(n.jubHolder.getJub(),e.poseidonHolder.getPoseidon()));exports.buildEddsaRN=u;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { BabyJubRN } from './BabyJubRN';
|
|
2
|
+
type PoseidonFn = {
|
|
3
|
+
(inputs: bigint[]): bigint;
|
|
4
|
+
F: {
|
|
5
|
+
toString: (v: bigint | number | string) => string;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
export declare class EddsaRN {
|
|
9
|
+
private readonly babyJub;
|
|
10
|
+
private readonly poseidon;
|
|
11
|
+
constructor(babyJub: BabyJubRN, poseidon: PoseidonFn);
|
|
12
|
+
prv2pub(prv: Uint8Array): [bigint, bigint];
|
|
13
|
+
signPoseidon(prv: Uint8Array, msg: bigint): {
|
|
14
|
+
R8: [bigint, bigint];
|
|
15
|
+
S: bigint;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export declare const buildEddsaRN: () => Promise<EddsaRN>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { poseidonHolder as e } from "./poseidon.mjs";
|
|
2
|
+
import { mod as t } from "../functions/utils/bigint-math.utils.mjs";
|
|
3
|
+
import { jubHolder as n } from "./babyJub.mjs";
|
|
4
|
+
import { blake512 as r } from "@noble/hashes/blake1.js";
|
|
5
|
+
//#region libs/shared/common/src/crypto/EddsaRN.ts
|
|
6
|
+
var i = 21888242871839275222246405745257275088614511777268538073601725287587578984328n / 8n, a = (e, t = 0, n = e.length - t) => {
|
|
7
|
+
let r = 0n;
|
|
8
|
+
for (let i = n - 1; i >= 0; --i) r = (r << 8n) + BigInt(e[t + i]);
|
|
9
|
+
return r;
|
|
10
|
+
}, o = (e, n, r, i) => {
|
|
11
|
+
let a = t(r);
|
|
12
|
+
for (let t = 0; t < i; t += 1) e[n + t] = Number(a & 255n), a >>= 8n;
|
|
13
|
+
}, s = (e) => t(e, i), c = (e) => {
|
|
14
|
+
let t = new Uint8Array(e);
|
|
15
|
+
return t[0] &= 248, t[31] &= 127, t[31] |= 64, t;
|
|
16
|
+
}, l = class {
|
|
17
|
+
constructor(e, t) {
|
|
18
|
+
this.babyJub = e, this.poseidon = t;
|
|
19
|
+
}
|
|
20
|
+
prv2pub(e) {
|
|
21
|
+
let t = a(c(r(e).slice(0, 32)), 0, 32);
|
|
22
|
+
return this.babyJub.mulPointEscalar(this.babyJub.Base8, t / 8n);
|
|
23
|
+
}
|
|
24
|
+
signPoseidon(e, n) {
|
|
25
|
+
let i = c(r(e)), l = a(i, 0, 32), u = this.babyJub.mulPointEscalar(this.babyJub.Base8, l / 8n), d = new Uint8Array(64);
|
|
26
|
+
d.set(i.slice(32), 0), o(d, 32, n, 32);
|
|
27
|
+
let f = s(a(r(d), 0, 64)), p = this.babyJub.mulPointEscalar(this.babyJub.Base8, f), m = this.poseidon([
|
|
28
|
+
p[0],
|
|
29
|
+
p[1],
|
|
30
|
+
u[0],
|
|
31
|
+
u[1],
|
|
32
|
+
n
|
|
33
|
+
]);
|
|
34
|
+
return {
|
|
35
|
+
R8: p,
|
|
36
|
+
S: s(f + s(t(BigInt(this.poseidon.F.toString(m))) * l))
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}, u = async () => (await e.init(), await n.init(), new l(n.getJub(), e.getPoseidon()));
|
|
40
|
+
//#endregion
|
|
41
|
+
export { u as buildEddsaRN };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
require(`../../../_virtual/_rolldown/runtime.cjs`);
|
|
1
|
+
require(`../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../constants/vite.constants.cjs`),t=require(`./EddsaRN.cjs`);let n=require(`circomlibjs-hinkal-fork`);var r=new class{eddsa=void 0;async init(){this.eddsa||=e.isReactNative?await t.buildEddsaRN():await(0,n.buildEddsa)()}getEddsa(){return this.eddsa}},i=()=>r.getEddsa();exports.eddsaHolder=r,exports.eddsaInstance=i;
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { isReactNative as e } from "../constants/vite.constants.mjs";
|
|
2
|
+
import { buildEddsaRN as t } from "./EddsaRN.mjs";
|
|
3
|
+
import { buildEddsa as n } from "circomlibjs-hinkal-fork";
|
|
4
|
+
var r = new class {
|
|
3
5
|
eddsa = void 0;
|
|
4
6
|
async init() {
|
|
5
|
-
this.eddsa ||= await
|
|
7
|
+
this.eddsa ||= e ? await t() : await n();
|
|
6
8
|
}
|
|
7
9
|
getEddsa() {
|
|
8
10
|
return this.eddsa;
|
|
9
11
|
}
|
|
10
|
-
}(),
|
|
12
|
+
}(), i = () => r.getEddsa();
|
|
11
13
|
//#endregion
|
|
12
|
-
export {
|
|
14
|
+
export { r as eddsaHolder, i as eddsaInstance };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=require(`../../constants/chains.constants.cjs`),t=require(`../../constants/protocol.constants.cjs`),n=require(`../../types/hinkal.types.cjs`),r=require(`../../error-handling/error-codes.constants.cjs`),i=require(`../../functions/utils/time.utils.cjs`),a=require(`../../types/external-action.types.cjs`),o=require(`../../functions/utils/tron.utils.cjs`),s=require(`../../functions/utils/token-check.utils.cjs`),c=require(`../../functions/web3/events/getShieldedBalance.cjs`),l=require(`../../functions/pre-transaction/outputUtxoProcessing.cjs`),u=require(`../../functions/pre-transaction/getVolatileTransferAmount.cjs`),d=require(`../../functions/pre-transaction/constructAdminData.cjs`),f=require(`../../functions/pre-transaction/getFeeStructure.cjs`);require(`../../functions/pre-transaction/index.cjs`);const
|
|
1
|
+
const e=require(`../../constants/chains.constants.cjs`),t=require(`../../constants/protocol.constants.cjs`),n=require(`../../types/hinkal.types.cjs`),r=require(`../../error-handling/error-codes.constants.cjs`),i=require(`../../functions/utils/time.utils.cjs`),a=require(`../../types/external-action.types.cjs`),o=require(`../../functions/utils/tron.utils.cjs`),s=require(`../../functions/utils/token-check.utils.cjs`),c=require(`../../functions/web3/events/getShieldedBalance.cjs`),l=require(`../../functions/pre-transaction/outputUtxoProcessing.cjs`),u=require(`../../functions/pre-transaction/getVolatileTransferAmount.cjs`),d=require(`../../functions/pre-transaction/constructAdminData.cjs`),f=require(`../../functions/pre-transaction/getFeeStructure.cjs`),p=require(`../../functions/utils/hinkalTiming.utils.cjs`);require(`../../functions/pre-transaction/index.cjs`);const m=require(`../../functions/utils/fees.utils.cjs`),h=require(`../../functions/snarkjs/constructGeneralZkProof.cjs`),g=require(`../../functions/web3/functionCalls/transactCallDirect.cjs`),_=require(`../../functions/web3/functionCalls/transactCallRelayer.cjs`),v=require(`../../functions/web3/functionCalls/transactCallDirectTron.cjs`),y=require(`../../functions/pre-transaction/merge-with-fee-structure.cjs`);var b=async(e,t,n,r)=>{let a=[...await c.addPaddingToUtxos(e,t,n,r)],o=[],s=i.getCurrentTimeInSeconds().toString();for(let t=0;t<n.length;t+=1){let{outputUtxos:n}=l.outputUtxoProcessing(e.userKeys,a[t],r[t],s);o.push(n)}return{inputUtxosArray:a,outputUtxosArray:o}},x=async(i,c,l,x,S,C,w,T)=>{let E=s.validateAndGetChainId(c),D=p.createHinkalTiming(`withdraw:chain-${E}`);D.log(`start`,{isRelayerOff:S});let O=e.isTronLike(E),k=o.addressToHexFormat(await i.getEthereumAddressByChain(E)),A=c.map(e=>e.erc20TokenAddress),j=await u.modifyVolatileTokenAmountChanges(E,c,l),M=c[0];if(!M)throw Error(`Deposit Action: No Token Found`);let N;if(!S){let e=w??await f.getFeeStructure(E,C,A,a.ExternalActionId.Transact);N=await m.calculateModifiedFeeStructure(E,M,-j[0],e),await y.mergeWithFeeStructure(E,A,j,N)}D.log(`feeStructure`);let P={externalActionId:0n,externalAddress:o.addressToHexFormat(x),externalActionMetadata:`0x00`},[F,{inputUtxosArray:I,outputUtxosArray:L}]=await Promise.all([i.getRandomRelay(E,!0),b(i,E,A,j)]);if(D.log(`relay+inputOutputUtxos`),!S&&!F)throw Error(r.transactionErrorCodes.RELAYER_NOT_AVAILABLE);D.log(`constructZkProof-start`);let{zkCallData:R,circomData:z,dimData:B,commitmentValidationData:V}=await h.constructZkProof(i.merkleTreeHinkalByChain[E],I,L,i.userKeys,P.externalActionId,P.externalAddress,P.externalActionMetadata,i.generateProofRemotely,N??n.zeroFeeStructure,S?t.zeroAddress:F,E,void 0,void 0,S?k:void 0);D.log(`constructZkProof-done`);let H=O&&S?void 0:await o.reorderZkCallData(O,R,B,z);if(D.log(`reorderZkCallData`),S){if(!M)throw Error(`Withdraw Action: No Token Found`);if(O){D.log(`transactCallDirectTron-start`);let e=await v.transactCallDirectTron(i,E,j[0],M,R,z,B);return D.log(`transactCallDirectTron-done`,{txid:e}),e}D.log(`transactCallDirect-start`);let e=await g.transactCallDirect(i,E,j[0],M,R,z,B);return D.log(`transactCallDirect-done`),e}let U=d.constructAdminData(T,E,A,j,k);D.log(`transactCallRelayer-start`);let W=await _.transactCallRelayer(E,R,B,z,V,void 0,void 0,U,H);return D.log(`transactCallRelayer-done`,{txHash:W}),W};exports.hinkalWithdraw=x;
|
|
@@ -11,15 +11,16 @@ import { outputUtxoProcessing as u } from "../../functions/pre-transaction/outpu
|
|
|
11
11
|
import { modifyVolatileTokenAmountChanges as d } from "../../functions/pre-transaction/getVolatileTransferAmount.mjs";
|
|
12
12
|
import { constructAdminData as f } from "../../functions/pre-transaction/constructAdminData.mjs";
|
|
13
13
|
import { getFeeStructure as p } from "../../functions/pre-transaction/getFeeStructure.mjs";
|
|
14
|
+
import { createHinkalTiming as m } from "../../functions/utils/hinkalTiming.utils.mjs";
|
|
14
15
|
import "../../functions/pre-transaction/index.mjs";
|
|
15
|
-
import { calculateModifiedFeeStructure as
|
|
16
|
-
import { constructZkProof as
|
|
17
|
-
import { transactCallDirect as
|
|
18
|
-
import { transactCallRelayer as
|
|
19
|
-
import { transactCallDirectTron as
|
|
20
|
-
import { mergeWithFeeStructure as
|
|
16
|
+
import { calculateModifiedFeeStructure as h } from "../../functions/utils/fees.utils.mjs";
|
|
17
|
+
import { constructZkProof as g } from "../../functions/snarkjs/constructGeneralZkProof.mjs";
|
|
18
|
+
import { transactCallDirect as _ } from "../../functions/web3/functionCalls/transactCallDirect.mjs";
|
|
19
|
+
import { transactCallRelayer as v } from "../../functions/web3/functionCalls/transactCallRelayer.mjs";
|
|
20
|
+
import { transactCallDirectTron as y } from "../../functions/web3/functionCalls/transactCallDirectTron.mjs";
|
|
21
|
+
import { mergeWithFeeStructure as b } from "../../functions/pre-transaction/merge-with-fee-structure.mjs";
|
|
21
22
|
//#region libs/shared/common/src/data-structures/Hinkal/hinkalWithdraw.ts
|
|
22
|
-
var
|
|
23
|
+
var x = async (e, t, n, r) => {
|
|
23
24
|
let a = [...await l(e, t, n, r)], o = [], s = i().toString();
|
|
24
25
|
for (let t = 0; t < n.length; t += 1) {
|
|
25
26
|
let { outputUtxos: n } = u(e.userKeys, a[t], r[t], s);
|
|
@@ -29,26 +30,42 @@ var b = async (e, t, n, r) => {
|
|
|
29
30
|
inputUtxosArray: a,
|
|
30
31
|
outputUtxosArray: o
|
|
31
32
|
};
|
|
32
|
-
},
|
|
33
|
-
let
|
|
34
|
-
|
|
35
|
-
let M;
|
|
36
|
-
if (!
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
}, S = async (i, l, u, S, C, w, T, E) => {
|
|
34
|
+
let D = c(l), O = m(`withdraw:chain-${D}`);
|
|
35
|
+
O.log("start", { isRelayerOff: C });
|
|
36
|
+
let k = e(D), A = o(await i.getEthereumAddressByChain(D)), j = l.map((e) => e.erc20TokenAddress), M = await d(D, l, u), N = l[0];
|
|
37
|
+
if (!N) throw Error("Deposit Action: No Token Found");
|
|
38
|
+
let P;
|
|
39
|
+
if (!C) {
|
|
40
|
+
let e = T ?? await p(D, w, j, a.Transact);
|
|
41
|
+
P = await h(D, N, -M[0], e), await b(D, j, M, P);
|
|
39
42
|
}
|
|
40
|
-
|
|
43
|
+
O.log("feeStructure");
|
|
44
|
+
let F = {
|
|
41
45
|
externalActionId: 0n,
|
|
42
|
-
externalAddress: o(
|
|
46
|
+
externalAddress: o(S),
|
|
43
47
|
externalActionMetadata: "0x00"
|
|
44
|
-
}, [
|
|
45
|
-
if (!
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
}, [I, { inputUtxosArray: L, outputUtxosArray: R }] = await Promise.all([i.getRandomRelay(D, !0), x(i, D, j, M)]);
|
|
49
|
+
if (O.log("relay+inputOutputUtxos"), !C && !I) throw Error(r.RELAYER_NOT_AVAILABLE);
|
|
50
|
+
O.log("constructZkProof-start");
|
|
51
|
+
let { zkCallData: z, circomData: B, dimData: V, commitmentValidationData: H } = await g(i.merkleTreeHinkalByChain[D], L, R, i.userKeys, F.externalActionId, F.externalAddress, F.externalActionMetadata, i.generateProofRemotely, P ?? n, C ? t : I, D, void 0, void 0, C ? A : void 0);
|
|
52
|
+
O.log("constructZkProof-done");
|
|
53
|
+
let U = k && C ? void 0 : await s(k, z, V, B);
|
|
54
|
+
if (O.log("reorderZkCallData"), C) {
|
|
55
|
+
if (!N) throw Error("Withdraw Action: No Token Found");
|
|
56
|
+
if (k) {
|
|
57
|
+
O.log("transactCallDirectTron-start");
|
|
58
|
+
let e = await y(i, D, M[0], N, z, B, V);
|
|
59
|
+
return O.log("transactCallDirectTron-done", { txid: e }), e;
|
|
60
|
+
}
|
|
61
|
+
O.log("transactCallDirect-start");
|
|
62
|
+
let e = await _(i, D, M[0], N, z, B, V);
|
|
63
|
+
return O.log("transactCallDirect-done"), e;
|
|
50
64
|
}
|
|
51
|
-
|
|
65
|
+
let W = f(E, D, j, M, A);
|
|
66
|
+
O.log("transactCallRelayer-start");
|
|
67
|
+
let G = await v(D, z, V, B, H, void 0, void 0, W, U);
|
|
68
|
+
return O.log("transactCallRelayer-done", { txHash: G }), G;
|
|
52
69
|
};
|
|
53
70
|
//#endregion
|
|
54
|
-
export {
|
|
71
|
+
export { S as hinkalWithdraw };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=require(`../../constants/chains.constants.cjs`),t=require(`../../constants/protocol.constants.cjs`),n=require(`../../crypto/poseidon.cjs`),r=require(`../web3/etherFunctions.cjs`),i=require(`../../data-structures/crypto-keys/keys.cjs`),a=require(`../utils/time.utils.cjs`),o=require(`../../types/external-action.types.cjs`),s=require(`../../error-handling/logger.cjs`),c=require(`../utils/bit.operations.cjs`),l=require(`./common.snarkjs.cjs`),ee=require(`./signedMessageHash.cjs`),u=require(`./generateCircomData.cjs`),te=require(`../../data-structures/AccountActions/AccountActions.cjs`),d=require(`./generateMainAndCommitmentZkProof.cjs`),
|
|
1
|
+
const e=require(`../../constants/chains.constants.cjs`),t=require(`../../constants/protocol.constants.cjs`),n=require(`../../crypto/poseidon.cjs`),r=require(`../web3/etherFunctions.cjs`),i=require(`../../data-structures/crypto-keys/keys.cjs`),a=require(`../utils/time.utils.cjs`),o=require(`../../types/external-action.types.cjs`),s=require(`../../error-handling/logger.cjs`),c=require(`../utils/bit.operations.cjs`),l=require(`./common.snarkjs.cjs`),ee=require(`./signedMessageHash.cjs`),u=require(`./generateCircomData.cjs`),te=require(`../../data-structures/AccountActions/AccountActions.cjs`),d=require(`../utils/hinkalTiming.utils.cjs`),f=require(`./generateMainAndCommitmentZkProof.cjs`),p=require(`./getMerkleTreeSiblingsAndRootHashes.cjs`),m=require(`../private-wallet/emporium.helpers.cjs`);var h=async(h,g,_,v,y,b,x,S,C,w=t.zeroAddress,T=e.chainIds.localhost,E=g.map(()=>!1),D=void 0,O,k)=>{let A=l.getZkProofVerifierName(g,_),j=d.createHinkalTiming(`constructZkProof:chain-${T}`);j.log(`start`,{verifier:A});let M=l.calcAmountChanges(g,_),N=g.map(e=>e[0].erc20TokenAddress),P=l.calcEncryptedOutputs(_),F=t.defaultSignatureData,I=v.getShieldedPrivateKey(),{pubSpendingBJJPoint:ne}=v.getSpendingKeyPair(),L=l.calcStealthAddressStructure(i.UserKeys.findCorrectRandomization(r.randomBigInt(31),I),I,v.getSpendingKeyPair().pubSpendingBJJPoint),{inCommitmentSiblings:re,inCommitmentSiblingSides:ie,rootHashHinkal:R,inNullifiers:z}=await p.getDataFromWorkers(T,h,g.map(e=>e.map(e=>e.getConstructableParams())));j.log(`getDataFromWorkers`);let B=l.buildOutCommitments(_),V=r.randomBigInt(31),H=n.poseidonFunction(V),U=BigInt(_.length>0?_[0][0].timeStamp:a.getCurrentTimeInSeconds()),W={rootHashHinkal:R,spendingPublicKey:ne,nullifyingPrivateKey:I,erc20TokenAddresses:N,amountChanges:M,inAmounts:g.map(e=>e.map(e=>e.amount.toString())),inRandomizations:g.map(e=>e.map(e=>l.getUtxoCircuitInRandomization(e))),inH0Ax:g.map(e=>e.map(e=>l.getUtxoCircuitH0Coords(e)[0].toString())),inH0Ay:g.map(e=>e.map(e=>l.getUtxoCircuitH0Coords(e)[1].toString())),isNewStyle:g.map(e=>e.map(e=>!!e.isNewStyle)),inTimeStamps:g.map(e=>e.map(e=>e.timeStamp)),inNullifiers:z,inCommitmentSiblings:re,inCommitmentSiblingSides:ie,outAmounts:_.map(e=>e.map(e=>e.amount.toString())),outTimeStamp:U,outPublicKeys:_.map(e=>e.map(e=>e.getStealthAddress())),outCommitments:B,calldataHash:0n,messageSeed:V,H0Ax:c.takeOffHighestBit(L.extraRandomization),H0Ay:L.H0},G=l.calcPublicSignalCount(A,W.erc20TokenAddresses,W.amountChanges,W.inNullifiers,W.outCommitments),K=l.calcAmountChanges(g,_,!0),q=l.getSlippageValues(K);s.Logger.log({publicSignalCount:G});let J=Array.isArray(x)?x:[x],Y=e.getOnchainChainId(T),ae=k?te.AccountActions.getSignerAddressFromPrivateKey(Y,k):void 0,X=y===o.ExternalActionId.Emporium?await m.encodeEmporiumMetadata(T,b??t.zeroAddress,k,J,H,ae):await m.encodeEmporiumMetadata(T,t.zeroAddress,void 0,y===0n?[]:J,H,t.zeroAddress);W.calldataHash=l.createCallDataHash(G,w,b,y,X,P,D,q,E,C,F,O);let Z=ee.computeSignedMessageHashEvm({rootHashHinkal:R,erc20TokenAddresses:N,amountChanges:M,outTimeStamp:U,inNullifiers:z,outCommitments:B,calldataHash:W.calldataHash,message:H,outH1Ay:L.H1,H0Ax:W.H0Ax,H0Ay:L.H0}),{R8:Q,S:oe}=v.signEddsa(Z);W.eddsaSignature=[Q[0],Q[1],oe],W.signedMessageHash=Z;let $=A.startsWith(`mainEVMCircuitMin0`),se={outTimeStamp:U,calldataHash:W.calldataHash,messageSeed:V};j.log(`generateMainAndCommitmentZkProof-start`);let{zkCallData:ce,commitmentValidationData:le}=await f.generateMainAndCommitmentZkProof(T,v,N,g,A,$?se:W,S);return j.log(`generateMainAndCommitmentZkProof-done`),{zkCallData:ce,circomData:u.generateCircomData(B,z,R,K,N,_,P,G,y,b,X,w,W.calldataHash,L,E,D,$?Number(U):void 0,q,C,F,O),dimData:{tokenNumber:g.length,nullifierAmount:g.length>0?g[0].length:0,outputAmount:_.length>0?_[0].length:0},encryptedOutputs:P,commitmentValidationData:le}};exports.constructZkProof=h;
|
|
@@ -1,82 +1,89 @@
|
|
|
1
1
|
import { chainIds as e, getOnchainChainId as t } from "../../constants/chains.constants.mjs";
|
|
2
2
|
import { defaultSignatureData as ee, zeroAddress as n } from "../../constants/protocol.constants.mjs";
|
|
3
|
-
import { poseidonFunction as
|
|
4
|
-
import { randomBigInt as
|
|
5
|
-
import { UserKeys as
|
|
6
|
-
import { getCurrentTimeInSeconds as
|
|
7
|
-
import { ExternalActionId as
|
|
8
|
-
import { Logger as
|
|
9
|
-
import { takeOffHighestBit as
|
|
10
|
-
import { buildOutCommitments as
|
|
11
|
-
import { computeSignedMessageHashEvm as
|
|
12
|
-
import { generateCircomData as
|
|
13
|
-
import { AccountActions as
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
3
|
+
import { poseidonFunction as r } from "../../crypto/poseidon.mjs";
|
|
4
|
+
import { randomBigInt as i } from "../web3/etherFunctions.mjs";
|
|
5
|
+
import { UserKeys as te } from "../../data-structures/crypto-keys/keys.mjs";
|
|
6
|
+
import { getCurrentTimeInSeconds as ne } from "../utils/time.utils.mjs";
|
|
7
|
+
import { ExternalActionId as re } from "../../types/external-action.types.mjs";
|
|
8
|
+
import { Logger as ie } from "../../error-handling/logger.mjs";
|
|
9
|
+
import { takeOffHighestBit as ae } from "../utils/bit.operations.mjs";
|
|
10
|
+
import { buildOutCommitments as oe, calcAmountChanges as a, calcEncryptedOutputs as se, calcPublicSignalCount as o, calcStealthAddressStructure as s, createCallDataHash as c, getSlippageValues as l, getUtxoCircuitH0Coords as u, getUtxoCircuitInRandomization as ce, getZkProofVerifierName as le } from "./common.snarkjs.mjs";
|
|
11
|
+
import { computeSignedMessageHashEvm as ue } from "./signedMessageHash.mjs";
|
|
12
|
+
import { generateCircomData as de } from "./generateCircomData.mjs";
|
|
13
|
+
import { AccountActions as fe } from "../../data-structures/AccountActions/AccountActions.mjs";
|
|
14
|
+
import { createHinkalTiming as pe } from "../utils/hinkalTiming.utils.mjs";
|
|
15
|
+
import { generateMainAndCommitmentZkProof as me } from "./generateMainAndCommitmentZkProof.mjs";
|
|
16
|
+
import { getDataFromWorkers as d } from "./getMerkleTreeSiblingsAndRootHashes.mjs";
|
|
17
|
+
import { encodeEmporiumMetadata as f } from "../private-wallet/emporium.helpers.mjs";
|
|
17
18
|
//#region libs/shared/common/src/functions/snarkjs/constructGeneralZkProof.ts
|
|
18
|
-
var
|
|
19
|
-
let O =
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
19
|
+
var p = async (p, m, h, g, _, v, y, b, x, S = n, C = e.localhost, w = m.map(() => !1), T = void 0, E, D) => {
|
|
20
|
+
let O = le(m, h), k = pe(`constructZkProof:chain-${C}`);
|
|
21
|
+
k.log("start", { verifier: O });
|
|
22
|
+
let A = a(m, h), j = m.map((e) => e[0].erc20TokenAddress), M = se(h), N = ee, P = g.getShieldedPrivateKey(), { pubSpendingBJJPoint: F } = g.getSpendingKeyPair(), I = s(te.findCorrectRandomization(i(31), P), P, g.getSpendingKeyPair().pubSpendingBJJPoint), { inCommitmentSiblings: L, inCommitmentSiblingSides: R, rootHashHinkal: z, inNullifiers: B } = await d(C, p, m.map((e) => e.map((e) => e.getConstructableParams())));
|
|
23
|
+
k.log("getDataFromWorkers");
|
|
24
|
+
let V = oe(h), H = i(31), U = r(H), W = BigInt(h.length > 0 ? h[0][0].timeStamp : ne()), G = {
|
|
25
|
+
rootHashHinkal: z,
|
|
26
|
+
spendingPublicKey: F,
|
|
27
|
+
nullifyingPrivateKey: P,
|
|
28
|
+
erc20TokenAddresses: j,
|
|
29
|
+
amountChanges: A,
|
|
30
|
+
inAmounts: m.map((e) => e.map((e) => e.amount.toString())),
|
|
31
|
+
inRandomizations: m.map((e) => e.map((e) => ce(e))),
|
|
32
|
+
inH0Ax: m.map((e) => e.map((e) => u(e)[0].toString())),
|
|
33
|
+
inH0Ay: m.map((e) => e.map((e) => u(e)[1].toString())),
|
|
34
|
+
isNewStyle: m.map((e) => e.map((e) => !!e.isNewStyle)),
|
|
35
|
+
inTimeStamps: m.map((e) => e.map((e) => e.timeStamp)),
|
|
36
|
+
inNullifiers: B,
|
|
37
|
+
inCommitmentSiblings: L,
|
|
38
|
+
inCommitmentSiblingSides: R,
|
|
39
|
+
outAmounts: h.map((e) => e.map((e) => e.amount.toString())),
|
|
40
|
+
outTimeStamp: W,
|
|
41
|
+
outPublicKeys: h.map((e) => e.map((e) => e.getStealthAddress())),
|
|
42
|
+
outCommitments: V,
|
|
38
43
|
calldataHash: 0n,
|
|
39
|
-
messageSeed:
|
|
40
|
-
H0Ax:
|
|
41
|
-
H0Ay:
|
|
42
|
-
},
|
|
43
|
-
|
|
44
|
-
let
|
|
45
|
-
|
|
46
|
-
let
|
|
47
|
-
rootHashHinkal:
|
|
48
|
-
erc20TokenAddresses:
|
|
49
|
-
amountChanges:
|
|
50
|
-
outTimeStamp:
|
|
51
|
-
inNullifiers:
|
|
52
|
-
outCommitments:
|
|
53
|
-
calldataHash:
|
|
54
|
-
message:
|
|
55
|
-
outH1Ay:
|
|
56
|
-
H0Ax:
|
|
57
|
-
H0Ay:
|
|
58
|
-
}), { R8:
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
],
|
|
64
|
-
let
|
|
65
|
-
outTimeStamp:
|
|
66
|
-
calldataHash:
|
|
67
|
-
messageSeed:
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
44
|
+
messageSeed: H,
|
|
45
|
+
H0Ax: ae(I.extraRandomization),
|
|
46
|
+
H0Ay: I.H0
|
|
47
|
+
}, K = o(O, G.erc20TokenAddresses, G.amountChanges, G.inNullifiers, G.outCommitments), q = a(m, h, !0), J = l(q);
|
|
48
|
+
ie.log({ publicSignalCount: K });
|
|
49
|
+
let Y = Array.isArray(y) ? y : [y], he = t(C), ge = D ? fe.getSignerAddressFromPrivateKey(he, D) : void 0, X = _ === re.Emporium ? await f(C, v ?? n, D, Y, U, ge) : await f(C, n, void 0, _ === 0n ? [] : Y, U, n);
|
|
50
|
+
G.calldataHash = c(K, S, v, _, X, M, T, J, w, x, N, E);
|
|
51
|
+
let Z = ue({
|
|
52
|
+
rootHashHinkal: z,
|
|
53
|
+
erc20TokenAddresses: j,
|
|
54
|
+
amountChanges: A,
|
|
55
|
+
outTimeStamp: W,
|
|
56
|
+
inNullifiers: B,
|
|
57
|
+
outCommitments: V,
|
|
58
|
+
calldataHash: G.calldataHash,
|
|
59
|
+
message: U,
|
|
60
|
+
outH1Ay: I.H1,
|
|
61
|
+
H0Ax: G.H0Ax,
|
|
62
|
+
H0Ay: I.H0
|
|
63
|
+
}), { R8: Q, S: _e } = g.signEddsa(Z);
|
|
64
|
+
G.eddsaSignature = [
|
|
65
|
+
Q[0],
|
|
66
|
+
Q[1],
|
|
67
|
+
_e
|
|
68
|
+
], G.signedMessageHash = Z;
|
|
69
|
+
let $ = O.startsWith("mainEVMCircuitMin0"), ve = {
|
|
70
|
+
outTimeStamp: W,
|
|
71
|
+
calldataHash: G.calldataHash,
|
|
72
|
+
messageSeed: H
|
|
73
|
+
};
|
|
74
|
+
k.log("generateMainAndCommitmentZkProof-start");
|
|
75
|
+
let { zkCallData: ye, commitmentValidationData: be } = await me(C, g, j, m, O, $ ? ve : G, b);
|
|
76
|
+
return k.log("generateMainAndCommitmentZkProof-done"), {
|
|
77
|
+
zkCallData: ye,
|
|
78
|
+
circomData: de(V, B, z, q, j, h, M, K, _, v, X, S, G.calldataHash, I, w, T, $ ? Number(W) : void 0, J, x, N, E),
|
|
72
79
|
dimData: {
|
|
73
|
-
tokenNumber:
|
|
74
|
-
nullifierAmount:
|
|
75
|
-
outputAmount:
|
|
80
|
+
tokenNumber: m.length,
|
|
81
|
+
nullifierAmount: m.length > 0 ? m[0].length : 0,
|
|
82
|
+
outputAmount: h.length > 0 ? h[0].length : 0
|
|
76
83
|
},
|
|
77
|
-
encryptedOutputs:
|
|
78
|
-
commitmentValidationData:
|
|
84
|
+
encryptedOutputs: M,
|
|
85
|
+
commitmentValidationData: be
|
|
79
86
|
};
|
|
80
87
|
};
|
|
81
88
|
//#endregion
|
|
82
|
-
export {
|
|
89
|
+
export { p as constructZkProof };
|
|
@@ -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(`enclave-proof:chain-${o}`);l.log(`start`,{verifierCount:s.length,verifiers:s});let 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(`encrypt-input-done`,{inputCount:c.length});let m={circuit_wasms:u,circuit_zkeys:d,inputs:p,key:f};l.log(`generate-proofs-request-start`,{url:`${e.ENCLAVE_URL}/generate-proofs`});let h=await t.httpClient.post(`${e.ENCLAVE_URL}/generate-proofs`,m);return l.log(`generate-proofs-request-done`,{proofCount:h.length}),h.map(({zk_calldata:e,public_signals:t})=>({zkCallData:e,publicSignals:t}))};exports.generateZkProofEnclave=o;
|
|
@@ -2,26 +2,36 @@ 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
|
-
|
|
15
|
+
}, s = async (s, c, l) => {
|
|
16
|
+
let u = r(`enclave-proof:chain-${s}`);
|
|
17
|
+
u.log("start", {
|
|
18
|
+
verifierCount: c.length,
|
|
19
|
+
verifiers: c
|
|
20
|
+
});
|
|
21
|
+
let d = c.map((e) => o(i(e, s))), f = c.map((e) => o(a(e, s))), { keyCiphertext: p, inputCiphertext: m } = await n(l);
|
|
22
|
+
u.log("encrypt-input-done", { inputCount: l.length });
|
|
23
|
+
let h = {
|
|
24
|
+
circuit_wasms: d,
|
|
25
|
+
circuit_zkeys: f,
|
|
26
|
+
inputs: m,
|
|
27
|
+
key: p
|
|
20
28
|
};
|
|
21
|
-
|
|
29
|
+
u.log("generate-proofs-request-start", { url: `${e}/generate-proofs` });
|
|
30
|
+
let g = await t.post(`${e}/generate-proofs`, h);
|
|
31
|
+
return u.log("generate-proofs-request-done", { proofCount: g.length }), g.map(({ zk_calldata: e, public_signals: t }) => ({
|
|
22
32
|
zkCallData: e,
|
|
23
33
|
publicSignals: t
|
|
24
34
|
}));
|
|
25
35
|
};
|
|
26
36
|
//#endregion
|
|
27
|
-
export {
|
|
37
|
+
export { s as generateZkProofEnclave };
|
|
@@ -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,9 @@
|
|
|
1
|
+
export type HinkalTimingMarks = {
|
|
2
|
+
start: number;
|
|
3
|
+
last: number;
|
|
4
|
+
};
|
|
5
|
+
export declare const logHinkalTiming: (scope: string, phase: string, marks: HinkalTimingMarks, extra?: Record<string, unknown>) => void;
|
|
6
|
+
export declare const createHinkalTiming: (scope: string) => {
|
|
7
|
+
marks: HinkalTimingMarks;
|
|
8
|
+
log: (phase: string, extra?: Record<string, unknown>) => void;
|
|
9
|
+
};
|
|
@@ -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 };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
const e=require(`./workerProxy-DPeuP2Mg.js`);let t=require(`buffer`),n=require(`process`);n=e.u(n);let r=require(`circomlibjs-hinkal-fork`),i=require(`@noble/hashes/blake1.js`),a=require(`ethers`),o=require(`poseidon-lite`);o=e.u(o);let s=require(`libsodium-wrappers`);s=e.u(s);let c=require(`@solana/web3.js`),l=require(`bs58`);l=e.u(l);let u=require(`tweetnacl`);u=e.u(u);let d=require(`tronweb`);`${`00`.repeat(20)}`;var f=21888242871839275222246405745257275088548364400416034343698204186575808495617n,p=f/2n,m=200n;`${`00`.repeat(32)}`;var h=(e,t=f)=>{let n=e%t;return n>=0n?n:n+t},g=(e,t,n)=>{let r=1n,i=h(e,n),a=t;for(;a>0n;)a%2n==1n&&(r=r*i%n),a/=2n,i=i*i%n;return r},_=f,v={p:_,zero:0n,one:1n,e:e=>h(BigInt(e)),add:(e,t)=>h(e+t),sub:(e,t)=>h(e-t),mul:(e,t)=>h(e*t),div:(e,t)=>{if(t===0n)throw Error(`Division by zero in BabyJub field`);return h(e*g(t,_-2n,_))},toString:e=>h(e).toString()},y=class{F=v;A=v.e(`168700`);D=v.e(`168696`);Base8=[v.e(`5299619240641551281634865583518297030282874472190772894086521144482721001553`),v.e(`16950150798460657717958625567821834550301663161624707787222815936182638968203`)];addPoint(e,t){let n=v.mul(e[0],t[1]),r=v.mul(e[1],t[0]),i=v.mul(v.sub(e[1],v.mul(this.A,e[0])),v.add(t[0],t[1])),a=v.mul(n,r),o=v.mul(this.D,a);return[v.div(v.add(n,r),v.add(v.one,o)),v.div(v.add(i,v.sub(v.mul(this.A,n),r)),v.sub(v.one,o))]}mulPointEscalar(e,t){let n=[v.zero,v.one],r=BigInt(t),i=e;for(;r!==0n;)r%2n==1n&&(n=this.addPoint(n,i)),i=this.addPoint(i,i),r/=2n;return n}},b=new class{babyjub=void 0;async init(){this.babyjub||=e.o?new y:await(0,r.buildBabyjub)()}getJub(){return this.babyjub}},x=()=>b.getJub();function S(e){return typeof e==`object`&&`toBigInt`in e?e.toBigInt():BigInt(e)}var C=()=>{let e=e=>{let t=o[`poseidon${e.length}`];if(!t)throw Error(`Poseidon: arity ${e.length} not supported (1..16)`);return t(e.map(e=>BigInt(e)))};return e.F={toString:e=>BigInt(e).toString()},e},w=new class{poseidon=void 0;async init(){this.poseidon||=e.o?C():await(0,r.buildPoseidon)()}getPoseidon(){return this.poseidon}};function T(...e){let t=w.getPoseidon();return S(t.F.toString(t(e)))}var E=(...e)=>{let t=w.getPoseidon();return a.ethers.toBeHex(t.F.toString(t([...e])))},D=21888242871839275222246405745257275088614511777268538073601725287587578984328n/8n,O=(e,t=0,n=e.length-t)=>{let r=0n;for(let i=n-1;i>=0;--i)r=(r<<8n)+BigInt(e[t+i]);return r},k=(e,t,n,r)=>{let i=h(n);for(let n=0;n<r;n+=1)e[t+n]=Number(i&255n),i>>=8n},A=e=>h(e,D),j=e=>{let t=new Uint8Array(e);return t[0]&=248,t[31]&=127,t[31]|=64,t},ee=class{constructor(e,t){this.babyJub=e,this.poseidon=t}prv2pub(e){let t=O(j((0,i.blake512)(e).slice(0,32)),0,32);return this.babyJub.mulPointEscalar(this.babyJub.Base8,t/8n)}signPoseidon(e,t){let n=j((0,i.blake512)(e)),r=O(n,0,32),a=this.babyJub.mulPointEscalar(this.babyJub.Base8,r/8n),o=new Uint8Array(64);o.set(n.slice(32),0),k(o,32,t,32);let s=A(O((0,i.blake512)(o),0,64)),c=this.babyJub.mulPointEscalar(this.babyJub.Base8,s),l=this.poseidon([c[0],c[1],a[0],a[1],t]);return{R8:c,S:A(s+A(h(BigInt(this.poseidon.F.toString(l)))*r))}}},te=async()=>(await w.init(),await b.init(),new ee(b.getJub(),w.getPoseidon())),M=new class{eddsa=void 0;async init(){this.eddsa||=e.o?await te():await(0,r.buildEddsa)()}getEddsa(){return this.eddsa}},N=()=>M.getEddsa(),P=async(e=!1)=>{(typeof window<`u`||typeof self<`u`)&&(typeof window<`u`&&(window.global=window),globalThis.Buffer=t.Buffer,globalThis.process=n.default),await Promise.all([s.default.ready,w.init(),b.init(),...e?[]:[M.init()]])},F=class{inMemoryCache=new Map;constructor(e){e&&this.mergeWithSerialized(e)}getInMemory(e){return this.inMemoryCache.get(e)??void 0}setInMemory(e,t){this.inMemoryCache.set(e,t)}mergeWithSerialized(t){Object.entries(t).forEach(([t,n])=>{this.set(t,e.c(n))})}},I=class extends F{isAttached=!1;get(e){return this.getInMemory(e)}set(e,t){this.setInMemory(e,t)}serialize(){return Object.fromEntries([...this.inMemoryCache].map(([t,n])=>[t,e.l(n)]))}attach(t){if(this.isAttached)throw Error(`can't reattach cache device`);return this.isAttached=!0,(...n)=>{let r=e.l(n),i=this.get(r);if(i!==void 0)return i;let a=t(...n);return this.set(r,a),a}}},L=e=>{if(e<0n||e>=21888242871839275222246405745257275088548364400416034343698204186575808495617n)throw RangeError();return e>p},R=e=>L(e)?1n:0n,z=new I,B=new I,V=new I,H=class{signature;nullifyingKey;PREFIX_FOR_SPENDING_PAIR=`1`;PREFIX_FOR_NULLIFYING_PAIR=`2`;constructor(e,t){this.signature=e,this.nullifyingKey=t}getSignature(){return this.requireSignature(),this.signature}setSignature(e){this.signature=e}requireSignature(){if(!this.signature)throw Error(`No signature provided`)}requireKeyMaterial(){if(!this.signature&&!this.nullifyingKey)throw Error(`No signature or private key provided`)}verifyTronMessage(e,t){return this.requireSignature(),t===d.Trx.verifyMessageV2(e,this.signature)}getShieldedPrivateKey=()=>(this.requireKeyMaterial(),this.nullifyingKey||=a.ethers.keccak256(this.signature),this.nullifyingKey);getShieldedPublicKey=()=>(this.requireKeyMaterial(),E(this.getShieldedPrivateKey()));getSpendingKeyPair=()=>{this.requireSignature();let e=N(),t=x(),n=E(this.PREFIX_FOR_SPENDING_PAIR,this.getSignature()),r=Buffer.from(a.ethers.getBytes(n)),i=e.prv2pub(r);return{privSpendingKey:n,pubSpendingBJJPoint:[BigInt(t.F.toString(i[0])),BigInt(t.F.toString(i[1]))]}};signEddsa=e=>{this.requireSignature();let t=x(),n=N(),r=Buffer.from(a.ethers.getBytes(this.getSpendingKeyPair().privSpendingKey)),i=t.F.e(e),o=n.signPoseidon(r,i);return{R8:[BigInt(t.F.toString(o.R8[0])),BigInt(t.F.toString(o.R8[1]))],S:BigInt(o.S)}};getShieldedPrivateKeyFromNonce=e=>{this.requireKeyMaterial();let t=E(e,this.getShieldedPrivateKey(),this.getShieldedPublicKey());return a.ethers.keccak256(a.ethers.toUtf8Bytes(`hinkal-offchain-shielded-key:${t}`))};getClaimableSignatureFromNonce=e=>{this.requireKeyMaterial();let t=E(e,this.getShieldedPrivateKey(),this.getShieldedPublicKey());return a.ethers.keccak256(a.ethers.toUtf8Bytes(`hinkal-claimable-utxo-signature:${t}`))};getDerivedEthereumAddress=()=>(this.requireKeyMaterial(),new a.ethers.Wallet(this.getShieldedPrivateKey()).address);getDerivedSolanaPublicKey=()=>(this.requireKeyMaterial(),c.Keypair.fromSeed(a.ethers.getBytes(this.getShieldedPrivateKey())).publicKey.toBase58());getNearIntentsAccountId=()=>{this.requireKeyMaterial();let e=c.Keypair.fromSeed(a.ethers.getBytes(this.getShieldedPrivateKey()));return a.ethers.hexlify(e.publicKey.toBytes()).slice(2)};getNearIntentsKeyPairString=()=>{this.requireKeyMaterial();let e=c.Keypair.fromSeed(a.ethers.getBytes(this.getShieldedPrivateKey()));return`ed25519:${l.default.encode(e.secretKey)}`};getSignerPrivateKeyFromNonce=e=>{this.requireKeyMaterial();let t=E(e,this.getShieldedPrivateKey(),this.getShieldedPublicKey());return a.ethers.keccak256(a.ethers.toUtf8Bytes(t))};getSignerSolanaPrivateKeyFromNonce=e=>{let t=this.getSignerPrivateKeyFromNonce(e),n=a.ethers.getBytes(t),r=c.Keypair.fromSeed(n);return l.default.encode(r.secretKey)};verifyMessage(e){return this.requireSignature(),a.ethers.verifyMessage(e,this.signature)}verifySolanaMessage(e,t){this.requireSignature();let n=new TextEncoder().encode(e),r=a.ethers.getBytes(this.signature),i=new c.PublicKey(t).toBytes();return u.default.sign.detached.verify(n,r,i)}static getRandomizedStealthPairOld=B.attach((e,t)=>{let n=x(),r=e*(BigInt(t)%f)%f,i=n.mulPointEscalar(n.Base8,e),a=n.mulPointEscalar(n.Base8,r);return{H0:[BigInt(n.F.toString(i[0])),BigInt(n.F.toString(i[1]))],H1:[BigInt(n.F.toString(a[0])),BigInt(n.F.toString(a[1]))]}});static getRandomizedStealthPair=z.attach((e,t)=>{let n=x(),r=BigInt(t)%f,i=n.mulPointEscalar(n.Base8,e),a=n.mulPointEscalar(i,r);return{H0:[BigInt(n.F.toString(i[0])),BigInt(n.F.toString(i[1]))],H1:[BigInt(n.F.toString(a[0])),BigInt(n.F.toString(a[1]))]}});static getStealthAddressCompressedPoints=(e,t)=>{let{H0:n,H1:r}=this.getRandomizedStealthPairOld(e,t),[i,a]=[n,r].map(e=>BigInt(e[1])+2n**255n*R(e[0]));return{h0:i,h1:a}};static checkSignature=(e,t,n,r)=>{let{h0:i,h1:a}=this.getStealthAddressCompressedPoints(e,r);return t===i&&n===a};static getStealthAddress=V.attach((e,t)=>{let{H0:n,H1:r}=this.getRandomizedStealthPairOld(e,t);return E(2n*R(n[0])+R(r[0]),n[1],r[1])});static getH1FromH0=(e,t)=>{let n=x(),r=BigInt(t)%f,i=[n.F.e(e[0]),n.F.e(e[1])],a=n.mulPointEscalar(i,r);return[BigInt(n.F.toString(a[0])),BigInt(n.F.toString(a[1]))]};static verifyStealthPair=(e,t,n,r=!0)=>{let i=this.getH1FromH0(e,n);return r?i[1]===t[1]:i===t};static getStealthAddressNewStyle=(e,t,n)=>{if(n?.length!==2)throw Error(`Spending public key must be an array of 2 elements`);let r=this.getH1FromH0(e,t);return E(2n*R(e[0])+R(r[0]),e[1],r[1],n[0],n[1],t)};static findCorrectRandomization(e,t){if(!t)throw Error(`No Private Shielded Key Provided`);let n=BigInt(t)%f,r,i,a=0n;do r=e*10n**a%f,i=r*n%f,a+=1n;while(i>=2n**253n||r>=2n**253n);return r}static getH0FromRandomization=e=>{let t=x(),n=t.mulPointEscalar(t.Base8,e);return[BigInt(t.F.toString(n[0])),BigInt(t.F.toString(n[1]))]};static findH0(e,t){if(!t)return;let{H0:n}=this.getRandomizedStealthPair(e,t);return n}static getEncryptionKeyPair=e=>{let t=s.default.crypto_box_seed_keypair(a.ethers.getBytes(e));return{privateKey:a.ethers.hexlify(t.privateKey),publicKey:a.ethers.hexlify(t.publicKey)}};getAccessKey=()=>(this.requireKeyMaterial(),E(this.getShieldedPrivateKey(),this.getShieldedPublicKey()));getBackendToken=()=>(this.requireKeyMaterial(),E(this.getAccessKey(),this.getShieldedPublicKey()))},U={CANNOT_CERTIFY_FOR_PAST_WEEK:`Certification for the past week is not available`,CYCLICAL_REFERRALS_FORBIDDEN:`Cyclical referrals are forbidden`,USER_IS_UNRANKED:`Please certify transactions to see rank`},W={WC_NO_MATCHING_KEY:`creating the transaction failed
|
|
2
|
+
version`,CONNECTION_PROBLEM:`Failed to connect wallet. Please try again.`,REQUEST_PENDING:`request of type 'wallet_requestpermissions' already pending for origin`},G={INSUFFICIENT_FUNDS:`Insufficient funds`,INSUFFICIENT_FUNDS_WITHDRAW:`Not enough funds to withdraw`,INSUFFICIENT_FUNDS_TRANSFER:`Not enough funds to transfer`,INSUFFICIENT_FUNDS_TO_TRANSACT:`Not enough funds to transact`,INSUFFICIENT_FUNDS_GAS:`Insufficient funds for gas * price`,INSUFFICIENT_TRON_BALANCE_FOR_FEE:`Insufficient TRX balance to cover transfer amount and transaction fee reserve`,INSUFFICIENT_SOL_BALANCE:`Insufficient SOL balance`},K={GAS_PRICE_EXCEEDS_AUTOSHIELD_LIMIT:`Skipped shielding due to gas fee exceeding your limit.`,AUTO_SHIELD_INSUFFICIENT_GAS:`Auto-Shield skipped due to insufficient gas.`};({...U,...W,...G,...K});var q=e=>BigInt(a.ethers.hexlify(a.ethers.randomBytes(e))),J=()=>Math.floor(Date.now()/1e3),Y=class t{amount;erc20TokenAddress;mintAddress;timeStamp;nullifyingKey;spendingPublicKey;randomization;H0;stealthAddress;encryptionKey;commitment;nullifier;isBlocked;isNewStyle;constructor({amount:e,erc20TokenAddress:t,mintAddress:n,timeStamp:r,nullifyingKey:i,spendingPublicKey:a,randomization:o,H0:s,stealthAddress:c,encryptionKey:l,commitment:u=void 0,nullifier:d=void 0,isBlocked:f=!1,isNewStyle:p=!1}){this.amount=e,this.erc20TokenAddress=t,this.mintAddress=n,this.timeStamp=r??J().toString(),this.nullifyingKey=i,this.spendingPublicKey=a,this.randomization=o??(i?H.findCorrectRandomization(q(31),i):void 0),this.H0=s??(p&&this.randomization?H.findH0(this.randomization,i):void 0),this.stealthAddress=c,this.encryptionKey=l,this.commitment=u,this.nullifier=d,this.isBlocked=f,this.isNewStyle=p}static createFrom(e,n){return new t({...e.getConstructableParams(),nullifier:void 0,commitment:void 0,...n})}getConstructableParams(){return{amount:this.amount,erc20TokenAddress:this.erc20TokenAddress,mintAddress:this.mintAddress,timeStamp:this.timeStamp,nullifyingKey:this.nullifyingKey,spendingPublicKey:this.spendingPublicKey,randomization:this.randomization,H0:this.H0,stealthAddress:this.stealthAddress,encryptionKey:this.encryptionKey,commitment:this.commitment,nullifier:this.nullifier,isBlocked:this.isBlocked,isNewStyle:this.isNewStyle}}getBasicUtxoParams(){return{amount:this.amount,erc20TokenAddress:this.erc20TokenAddress,mintAddress:this.mintAddress,timeStamp:this.timeStamp,randomization:this.randomization,H0:this.H0,stealthAddress:this.stealthAddress,commitment:this.commitment,nullifier:this.nullifier,isBlocked:this.isBlocked,isNewStyle:this.isNewStyle}}getCommitment(){if(!this.commitment){let e=[this.amount,this.erc20TokenAddress,BigInt(this.getStealthAddress()),this.timeStamp];this.commitment=E(...e)}return this.commitment}getNullifier(){if(!this.nullifier){if(!this.nullifyingKey)throw Error(`No Nullifiers if private key is not provided`);let e=this.getCommitment(),t=E(this.nullifyingKey,e);this.nullifier=E(e,t)}return this.nullifier}getStealthAddress(){if(!this.stealthAddress){if(!this.nullifyingKey)throw Error(`No stealth address in UTXO if private key is not provided`);if(this.isNewStyle){if(!this.H0)throw Error(`No H0 point provided`);this.stealthAddress=H.getStealthAddressNewStyle(this.H0,this.nullifyingKey,this.spendingPublicKey)}else{if(!this.randomization)throw Error(`No randomization provided`);this.stealthAddress=H.getStealthAddress(this.randomization,this.nullifyingKey)}}return this.stealthAddress}getTokenAddress(t){let n=e.r(t)?this.mintAddress:this.erc20TokenAddress;if(!n)throw Error(`No token address provided`);return n}getEncryptionKey(){if(!this.nullifyingKey){if(!this.encryptionKey)throw Error(`No encryption key provided in UTXO`);return this.encryptionKey}return H.getEncryptionKeyPair(this.nullifyingKey).publicKey}},X=()=>s.default.crypto_box_SEALBYTES+s.default.crypto_secretbox_KEYBYTES,Z=e=>{let t=s.default.crypto_secretbox_NONCEBYTES,n=X(),r=0,i=e[r];r+=1;let a=e.slice(r,r+t);r+=t;let o=[];for(let t=0;t<i;t+=1)o.push(e.slice(r,r+n)),r+=n;return{nonce:a,sealedKeys:o,ciphertext:e.slice(r)}},ne=(e,t,n,r)=>{let{nonce:i,sealedKeys:o,ciphertext:c}=Z(e),l=s.default.crypto_box_seal_open(o[r],a.ethers.getBytes(n),a.ethers.getBytes(t)),u=s.default.crypto_secretbox_open_easy(c,i,l);return Buffer.from(u)},re=(e,t,n)=>{let r=s.default.crypto_box_seal_open(e,a.ethers.getBytes(n),a.ethers.getBytes(t));return Buffer.from(r)},ie=54912,ae=2,oe=e=>e[0]*256+e[1],Q=(e,t)=>{let{privateKey:n,publicKey:r}=H.getEncryptionKeyPair(t.getShieldedPrivateKey()),i=oe(e)===ie?ne(e.slice(ae),n,r,0):re(e,n,r),a=Buffer.from(i).toString(`utf-8`).split(`0x`).filter(e=>e.length!==0),o=e=>BigInt(`0x${e}`),s=e=>`0x${e}`,l=a.length>=8,u=l&&o(a[5])===1n,d=l?8:6,f=a[d]?s(a[d]):void 0,p=f?new c.PublicKey(Buffer.from(f.slice(2),`hex`)).toString():void 0;return{amount:o(a[0]),erc20TokenAddress:s(a[1]),randomization:o(a[2]),stealthAddress:s(a[3]),nullifyingKey:t.getShieldedPrivateKey(),timeStamp:o(a[4]).toString(),isNewStyle:u,...u?{H0:[o(a[6]),o(a[7])]}:{},mintAddress:p}},$=(e,t)=>new Y(Q(e,t)),se=e=>e&2n**255n-1n,ce=e=>e>>255n&1n,le=(...t)=>{e.a&&console.log(...t)};Object.defineProperty(exports,`a`,{enumerable:!0,get:function(){return Q}}),Object.defineProperty(exports,`c`,{enumerable:!0,get:function(){return V}}),Object.defineProperty(exports,`d`,{enumerable:!0,get:function(){return T}}),Object.defineProperty(exports,`f`,{enumerable:!0,get:function(){return E}}),Object.defineProperty(exports,`i`,{enumerable:!0,get:function(){return $}}),Object.defineProperty(exports,`l`,{enumerable:!0,get:function(){return z}}),Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return ce}}),Object.defineProperty(exports,`o`,{enumerable:!0,get:function(){return Y}}),Object.defineProperty(exports,`p`,{enumerable:!0,get:function(){return m}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return se}}),Object.defineProperty(exports,`s`,{enumerable:!0,get:function(){return H}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return le}}),Object.defineProperty(exports,`u`,{enumerable:!0,get:function(){return P}});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=require(`../constants/vite.constants.cjs`),t=require(`../constants/chains.constants.cjs`),n=require(`./workerFactory.cjs`);var
|
|
1
|
+
const e=require(`../constants/vite.constants.cjs`),t=require(`../constants/chains.constants.cjs`),n=require(`../functions/utils/hinkalTiming.utils.cjs`),r=require(`./workerFactory.cjs`);var i=e=>e&&typeof e==`object`&&`type`in e?String(e.type):`n/a`,a=async a=>{let o=i(a.payload),s=n.createHinkalTiming(`worker:${a.type}:${o}`);s.log(`start`);let c=await r.WorkerFactory.getWebWorker(a.type);return s.log(`getWebWorker`),new Promise((n,r)=>{let i={};if(typeof window<`u`&&window.location){let{origin:e,protocol:t,host:n,port:r,hostname:a,href:o}=window.location;i={origin:e,protocol:t,host:n,port:r,hostname:a,href:o}}c.onMessage(e=>{s.log(`worker-result`),n(e),c.terminate()}),c.onError(e=>{s.log(`worker-error`,{message:e instanceof Error?e.message:String(e)}),r(e),c.terminate()});let o={payload:a.payload,metadata:{hostLocation:i,constants:{chains:{chainIds:t.chainIds},isDevelopment:e.isDevelopment}}};c.postMessage(o),s.log(`postMessage`)})};exports.performTaskWithWorker=a;
|
|
@@ -1,39 +1,42 @@
|
|
|
1
1
|
import { isDevelopment as e } from "../constants/vite.constants.mjs";
|
|
2
2
|
import { chainIds as t } from "../constants/chains.constants.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { createHinkalTiming as n } from "../functions/utils/hinkalTiming.utils.mjs";
|
|
4
|
+
import { WorkerFactory as r } from "./workerFactory.mjs";
|
|
4
5
|
//#region libs/shared/common/src/webworker/performTaskWithWorker.ts
|
|
5
|
-
var
|
|
6
|
-
let i =
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
var i = (e) => e && typeof e == "object" && "type" in e ? String(e.type) : "n/a", a = async (a) => {
|
|
7
|
+
let o = i(a.payload), s = n(`worker:${a.type}:${o}`);
|
|
8
|
+
s.log("start");
|
|
9
|
+
let c = await r.getWebWorker(a.type);
|
|
10
|
+
return s.log("getWebWorker"), new Promise((n, r) => {
|
|
11
|
+
let i = {};
|
|
9
12
|
if (typeof window < "u" && window.location) {
|
|
10
|
-
let { origin: e, protocol: t, host: n, port: r, hostname:
|
|
11
|
-
|
|
13
|
+
let { origin: e, protocol: t, host: n, port: r, hostname: a, href: o } = window.location;
|
|
14
|
+
i = {
|
|
12
15
|
origin: e,
|
|
13
16
|
protocol: t,
|
|
14
17
|
host: n,
|
|
15
18
|
port: r,
|
|
16
|
-
hostname:
|
|
17
|
-
href:
|
|
19
|
+
hostname: a,
|
|
20
|
+
href: o
|
|
18
21
|
};
|
|
19
22
|
}
|
|
20
|
-
|
|
21
|
-
n(e),
|
|
22
|
-
}),
|
|
23
|
-
|
|
23
|
+
c.onMessage((e) => {
|
|
24
|
+
s.log("worker-result"), n(e), c.terminate();
|
|
25
|
+
}), c.onError((e) => {
|
|
26
|
+
s.log("worker-error", { message: e instanceof Error ? e.message : String(e) }), r(e), c.terminate();
|
|
24
27
|
});
|
|
25
|
-
let
|
|
26
|
-
payload:
|
|
28
|
+
let o = {
|
|
29
|
+
payload: a.payload,
|
|
27
30
|
metadata: {
|
|
28
|
-
hostLocation:
|
|
31
|
+
hostLocation: i,
|
|
29
32
|
constants: {
|
|
30
33
|
chains: { chainIds: t },
|
|
31
34
|
isDevelopment: e
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
37
|
};
|
|
35
|
-
|
|
38
|
+
c.postMessage(o), s.log("postMessage");
|
|
36
39
|
});
|
|
37
40
|
};
|
|
38
41
|
//#endregion
|
|
39
|
-
export {
|
|
42
|
+
export { a as performTaskWithWorker };
|