@hinkal/common 0.1.38 → 0.1.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/data-structures/Hinkal/Hinkal.cjs +1 -1
- package/data-structures/Hinkal/Hinkal.d.ts +1 -0
- package/data-structures/Hinkal/Hinkal.mjs +22 -21
- package/data-structures/Hinkal/IHinkal.d.ts +1 -0
- package/data-structures/MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.cjs +1 -1
- package/data-structures/MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.d.ts +1 -0
- package/data-structures/MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.mjs +39 -30
- package/functions/utils/cacheFunctions.cjs +1 -1
- package/functions/utils/cacheFunctions.d.ts +7 -5
- package/functions/utils/cacheFunctions.mjs +36 -30
- package/functions/web3/events/getInputUtxoAndBalance.cjs +1 -1
- package/functions/web3/events/getInputUtxoAndBalance.mjs +39 -46
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("../../types/hinkal.types.cjs"),h=require("../../types/ethereum-network.types.cjs"),a=require("../../functions/web3/functionCalls/accessTokenCalls.cjs"),A=require("../../functions/web3/events/getShieldedBalance.cjs"),o=require("../crypto-keys/keys.cjs"),y=require("./hinkalActionBeefy.cjs"),C=require("./hinkalActionConvex.cjs"),P=require("./hinkalActionCurve.cjs"),w=require("./hinkalActionPendle.cjs"),d=require("./hinkalDeposit.cjs"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("../../types/hinkal.types.cjs"),h=require("../../types/ethereum-network.types.cjs"),a=require("../../functions/web3/functionCalls/accessTokenCalls.cjs"),A=require("../../functions/web3/events/getShieldedBalance.cjs"),o=require("../crypto-keys/keys.cjs"),y=require("./hinkalActionBeefy.cjs"),C=require("./hinkalActionConvex.cjs"),P=require("./hinkalActionCurve.cjs"),w=require("./hinkalActionPendle.cjs"),d=require("./hinkalDeposit.cjs"),m=require("./hinkalSwap.cjs"),T=require("./hinkalWithdraw.cjs"),S=require("./resetMerkleTrees.cjs"),l=require("../merkle-tree/MerkleTree.cjs"),u=require("../../crypto/poseidon.cjs");require("circomlibjs");const q=require("../../constants/chains.constants.cjs"),g=require("../../error-handling/error-codes.constants.cjs"),f=require("../../API/callMonitor.cjs"),E=require("./hinkalActionLidoEth.cjs");require("../../constants/vite.constants.cjs");require("axios");require("../../constants/coingecko.constants.cjs");const M=require("../../API/getAxelarGasEstimate.cjs"),p=require("../../API/API.cjs");require("ethers");require("../../types/curve.types.cjs");require("../../constants/contracts.constants.cjs");const F=require("../../constants/kyc.constants.cjs");require("../../constants/axelar.constants.cjs");require("../../constants/rewards.constants.cjs");require("../../constants/reorg-depths.constants.cjs");const H=require("../transactions-manager/TransactionsManager.cjs");require("../../functions/utils/external-action.utils.cjs");require("../../types/circom-data.types.cjs");require("../../types/transactions.types.cjs");const I=require("../../functions/utils/reloadPage.cjs"),R=require("./hinkalActionVolatile.cjs"),k=require("./hinkalActionPendleLP.cjs"),L=require("../../API/getConnextReceiveFee.cjs"),W=require("./hinkalActionStake.cjs"),x=require("../MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.cjs"),K=require("./hinkalPrivateWallet.cjs");class N{providerAdapter;userKeys;signingMessage="Login to Hinkal Protocol";merkleTreeHinkal;merkleTreeAccessToken;nullifiers;encryptedOutputs;commitmentsSnapshotService;nullifierSnapshotService;accessTokenSnapshotService;transactionsManager;utxoUtils;inMemoryCache;constructor(){this.userKeys=new o.UserKeys(void 0),this.merkleTreeHinkal=l.MerkleTree.create(u.poseidonFunction,25n,0n),this.merkleTreeAccessToken=l.MerkleTree.create(u.poseidonFunction,25n,0n),this.nullifiers=new Set,this.encryptedOutputs=[],this.transactionsManager=new H.TransactionsManager,this.utxoUtils=new x.MultiThreadedUtxoUtils,this.inMemoryCache=new Map}async initProviderAdapter(e,t){await this.updateProviderAdapter(t),this.providerAdapter?.initConnector(e);const r=await this.connectAndPatchProvider(e);await t.init(r),await this.setListeners()}async initUserKeys(){this.userKeys=new o.UserKeys(await this.getProviderAdapter().signMessage(this.signingMessage))}async initUserKeysWithPassword(e){this.userKeys=new o.UserKeys(e)}async resetMerkle(){this.isSelectedNetworkSupported()&&await S.resetMerkleTrees(this)}getProviderAdapter(){if(!this.providerAdapter)throw new Error("ProviderAdapter is not initialized");return this.providerAdapter}async waitForTransaction(e,t=1){return!!await this.providerAdapter?.waitForTransaction(e,t)}getContractMetadata(e,t){return this.getProviderAdapter().getContractMetadata(e,t)}getContract(e,t=void 0,r){return this.getProviderAdapter().getContract(e,t,r)}async signMessage(e){return await this.getProviderAdapter().signMessage(e)}async signTypedData(e,t,r){return await this.getProviderAdapter().signTypedData(e,t,r)}getContractWithSigner(e,t=""){return this.getProviderAdapter().getContractWithSigner(e,t)}getContractWithFetcher(e,t=""){return this.getProviderAdapter().getContractWithFetcher(e,t)}getContractWithFetcherForEthereum(e,t){return this.getProviderAdapter().getContractWithFetcherForEthereum(e,t)}isSelectedNetworkSupported(){return!!q.networkRegistry[this.getCurrentChainId()]}async switchNetwork(e){try{await this.getProviderAdapter().switchNetwork(e)}catch{throw new Error(g.transactionErrorCodes.FAILED_TO_SWITCH_NETWORKS)}}getCurrentChainId(){const{chainId:e}=this.getProviderAdapter();if(!e)throw new Error("Illegal State: no chainId");return e}getSelectedNetwork(){return this.providerAdapter?.getSelectedNetwork()}isPermitterAvailable(){return this.getProviderAdapter().isPermitterAvailable()}async connectAndPatchProvider(e){return await this.getProviderAdapter().connectAndPatchProvider(e)}async disconnectFromConnector(){await this.getProviderAdapter().disconnectFromConnector()}async updateProviderAdapter(e){try{this.providerAdapter&&this.providerAdapter.release(),this.providerAdapter=e}catch(t){throw console.error(t),Error("updateProviderAdapter failed, please try again.")}}async setListeners(){this.providerAdapter?.setChainEventListener({onAccountChanged:()=>{this.onAccountChanged()},onChainChanged:e=>{this.onChainChanged(e)}})}async onAccountChanged(){await this.getProviderAdapter().onAccountChanged();const e=c.EventType.AccountChanged;typeof document<"u"?document?.dispatchEvent(new Event(e)):process?.emit("message",e,void 0)}async onChainChanged(e){if(e){await this.getProviderAdapter().onChainChanged(e);const t=c.EventType.NetworkChange;typeof document<"u"?document.dispatchEvent(new Event(t)):process?.emit("message",t,void 0)}else await this.disconnectFromConnector(),I.reloadPage()}async monitorConnectedAddress(){f.monitor(await this.getEthereumAddress(),this.getCurrentChainId())}async getBalances(){return A.getShieldedBalance(this)}getSupportedPassportLinks(){return F.supportedPassportLinks}checkAccessToken(){return a.checkHinkalAccessToken(this)}async getAxelarEstimate(e,t,r,n){return M.getAxelarGasEstimate(e,t,r,n,this.userKeys.getAccessKey())}async getConnextReceiveFee(e,t){return L.getConnextReceiveFee(this.getCurrentChainId(),e,t)}async mintHinkalAccessToken(e){return a.mintAccessToken(this,e)}async mintHinkalAccessTokenCrossChain(e,t){return a.mintTokenCrossChain(this,e,t)}async getHinkalTreeRootHash(){return this.getContractWithFetcher(h.ContractType.HinkalContract).getRootHash()}async getAccessTokenTreeRootHash(){return this.getContractWithFetcher(h.ContractType.CrossChainAccessTokenContract).getRootHash()}async resetMerkleTreesIfNecessary(){if(!this.isSelectedNetworkSupported())throw new Error(g.transactionErrorCodes.UNSUPPORTED_NETWORK);const[e,t]=await Promise.all([this.getHinkalTreeRootHash(),this.getAccessTokenTreeRootHash()]);(e.toBigInt()!==this.merkleTreeHinkal.getRootHash()||t.toBigInt()!==this.merkleTreeAccessToken.getRootHash())&&await this.resetMerkle()}async getEventsFromHinkal(){await Promise.all([this.accessTokenSnapshotService?.retrieveEventsFromLatestBlock(),this.commitmentsSnapshotService?.retrieveEventsFromLatestBlock(),this.nullifierSnapshotService?.retrieveEventsFromLatestBlock()])}getEthereumAddress(){return this.getProviderAdapter().getAddress()}async getRandomRelay(){return(await p.API.getIdleRelay(this.getCurrentChainId())).relay}getGasPrice(){if(!this.providerAdapter?.chainId)throw new Error("Illegal State of providerAdapter in Hinkal: no chainId");return this.providerAdapter.getGasPrice()}getAPI(){return p.API}snapshotsClearInterval(){this.commitmentsSnapshotService?.intervalClear(),this.accessTokenSnapshotService?.intervalClear(),this.nullifierSnapshotService?.intervalClear()}async deposit(e,t){return d.hinkalDeposit(this,e,t)}async depositForOther(e,t,r){return d.hinkalDepositForOther(this,e,t,r)}async withdraw(e,t,r,n,i=!1){return T.hinkalWithdraw(this,e,t,r,n,i)}async swap(e,t,r,n,i=!1,s=!1){return m.hinkalSwap(this,e,t,r,n,i,s)}async actionCurve(e,t,r,n,i,s=!1){return P.hinkalActionCurve(this,e,t,r,n,i,s)}async actionConvex(e,t,r,n,i=!1){return C.hinkalActionConvex(this,e,t,r,n,i)}async actionBeefy(e,t,r,n,i,s,v=!1){return y.hinkalActionBeefy(this,e,t,r,n,i,s,v)}async actionLido(e,t,r,n,i,s=!1){return E.hinkalActionLido(this,e,t,r,n,i,s)}async actionPendle(e,t,r,n,i=!1){return w.hinkalActionPendle(this,e,t,r,n,i)}async actionPendleLP(e,t,r,n,i=!1){return k.hinkalActionPendleLP(this,e,t,r,n,i)}async actionVolatile(e,t,r,n=!1){return R.hinkalActionVolatile(this,e,t,r,n)}async actionStake(e,t,r,n){return W.hinkalActionStake(this,e,t,r,n)}async actionPrivateWallet(e,t,r,n=[],i,s=!1){return K.hinkalPrivateWallet(this,e,t,r,n,i,s)}async getPendleLPClaimableParams(e,t){return k.hinkalGetPendleLPClaimableParams(this,e,t)}}exports.Hinkal=N;
|
|
@@ -32,6 +32,7 @@ export declare class Hinkal<ConnectorType> implements IHinkal {
|
|
|
32
32
|
accessTokenSnapshotService?: ClientAccessTokenSnapshotService;
|
|
33
33
|
transactionsManager: TransactionsManager;
|
|
34
34
|
utxoUtils: IMultiThreadedUtxoUtils;
|
|
35
|
+
inMemoryCache: Map<string, string>;
|
|
35
36
|
constructor();
|
|
36
37
|
initProviderAdapter(connector: ConnectorType, providerAdapter: IProviderAdapter<ConnectorType>): Promise<void>;
|
|
37
38
|
initUserKeys(): Promise<void>;
|
|
@@ -2,7 +2,7 @@ import { EventType as o } from "../../types/hinkal.types.mjs";
|
|
|
2
2
|
import { ContractType as c } from "../../types/ethereum-network.types.mjs";
|
|
3
3
|
import { checkHinkalAccessToken as g, mintAccessToken as u, mintTokenCrossChain as v } from "../../functions/web3/functionCalls/accessTokenCalls.mjs";
|
|
4
4
|
import { getShieldedBalance as k } from "../../functions/web3/events/getShieldedBalance.mjs";
|
|
5
|
-
import { UserKeys as
|
|
5
|
+
import { UserKeys as s } from "../crypto-keys/keys.mjs";
|
|
6
6
|
import { hinkalActionBeefy as A } from "./hinkalActionBeefy.mjs";
|
|
7
7
|
import { hinkalActionConvex as y } from "./hinkalActionConvex.mjs";
|
|
8
8
|
import { hinkalActionCurve as C } from "./hinkalActionCurve.mjs";
|
|
@@ -21,16 +21,16 @@ import { hinkalActionLido as I } from "./hinkalActionLidoEth.mjs";
|
|
|
21
21
|
import "../../constants/vite.constants.mjs";
|
|
22
22
|
import "axios";
|
|
23
23
|
import "../../constants/coingecko.constants.mjs";
|
|
24
|
-
import { getAxelarGasEstimate as
|
|
24
|
+
import { getAxelarGasEstimate as M } from "../../API/getAxelarGasEstimate.mjs";
|
|
25
25
|
import { API as l } from "../../API/API.mjs";
|
|
26
26
|
import "ethers";
|
|
27
27
|
import "../../types/curve.types.mjs";
|
|
28
28
|
import "../../constants/contracts.constants.mjs";
|
|
29
|
-
import { supportedPassportLinks as
|
|
29
|
+
import { supportedPassportLinks as R } from "../../constants/kyc.constants.mjs";
|
|
30
30
|
import "../../constants/axelar.constants.mjs";
|
|
31
31
|
import "../../constants/rewards.constants.mjs";
|
|
32
32
|
import "../../constants/reorg-depths.constants.mjs";
|
|
33
|
-
import { TransactionsManager as
|
|
33
|
+
import { TransactionsManager as L } from "../transactions-manager/TransactionsManager.mjs";
|
|
34
34
|
import "../../functions/utils/external-action.utils.mjs";
|
|
35
35
|
import "../../types/circom-data.types.mjs";
|
|
36
36
|
import "../../types/transactions.types.mjs";
|
|
@@ -41,7 +41,7 @@ import { getConnextReceiveFee as O } from "../../API/getConnextReceiveFee.mjs";
|
|
|
41
41
|
import { hinkalActionStake as B } from "./hinkalActionStake.mjs";
|
|
42
42
|
import { MultiThreadedUtxoUtils as U } from "../MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.mjs";
|
|
43
43
|
import { hinkalPrivateWallet as b } from "./hinkalPrivateWallet.mjs";
|
|
44
|
-
class
|
|
44
|
+
class Re {
|
|
45
45
|
providerAdapter;
|
|
46
46
|
userKeys;
|
|
47
47
|
signingMessage = "Login to Hinkal Protocol";
|
|
@@ -54,8 +54,9 @@ class Le {
|
|
|
54
54
|
accessTokenSnapshotService;
|
|
55
55
|
transactionsManager;
|
|
56
56
|
utxoUtils;
|
|
57
|
+
inMemoryCache;
|
|
57
58
|
constructor() {
|
|
58
|
-
this.userKeys = new
|
|
59
|
+
this.userKeys = new s(void 0), this.merkleTreeHinkal = h.create(d, 25n, 0n), this.merkleTreeAccessToken = h.create(d, 25n, 0n), this.nullifiers = /* @__PURE__ */ new Set(), this.encryptedOutputs = [], this.transactionsManager = new L(), this.utxoUtils = new U(), this.inMemoryCache = /* @__PURE__ */ new Map();
|
|
59
60
|
}
|
|
60
61
|
async initProviderAdapter(e, t) {
|
|
61
62
|
await this.updateProviderAdapter(t), this.providerAdapter?.initConnector(e);
|
|
@@ -63,10 +64,10 @@ class Le {
|
|
|
63
64
|
await t.init(r), await this.setListeners();
|
|
64
65
|
}
|
|
65
66
|
async initUserKeys() {
|
|
66
|
-
this.userKeys = new
|
|
67
|
+
this.userKeys = new s(await this.getProviderAdapter().signMessage(this.signingMessage));
|
|
67
68
|
}
|
|
68
69
|
async initUserKeysWithPassword(e) {
|
|
69
|
-
this.userKeys = new
|
|
70
|
+
this.userKeys = new s(e);
|
|
70
71
|
}
|
|
71
72
|
async resetMerkle() {
|
|
72
73
|
this.isSelectedNetworkSupported() && await E(this);
|
|
@@ -165,13 +166,13 @@ class Le {
|
|
|
165
166
|
return k(this);
|
|
166
167
|
}
|
|
167
168
|
getSupportedPassportLinks() {
|
|
168
|
-
return
|
|
169
|
+
return R;
|
|
169
170
|
}
|
|
170
171
|
checkAccessToken() {
|
|
171
172
|
return g(this);
|
|
172
173
|
}
|
|
173
174
|
async getAxelarEstimate(e, t, r, i) {
|
|
174
|
-
return
|
|
175
|
+
return M(
|
|
175
176
|
e,
|
|
176
177
|
t,
|
|
177
178
|
r,
|
|
@@ -239,7 +240,7 @@ class Le {
|
|
|
239
240
|
async withdraw(e, t, r, i, n = !1) {
|
|
240
241
|
return S(this, e, t, r, i, n);
|
|
241
242
|
}
|
|
242
|
-
async swap(e, t, r, i, n = !1,
|
|
243
|
+
async swap(e, t, r, i, n = !1, a = !1) {
|
|
243
244
|
return T(
|
|
244
245
|
this,
|
|
245
246
|
e,
|
|
@@ -247,16 +248,16 @@ class Le {
|
|
|
247
248
|
r,
|
|
248
249
|
i,
|
|
249
250
|
n,
|
|
250
|
-
|
|
251
|
+
a
|
|
251
252
|
);
|
|
252
253
|
}
|
|
253
|
-
async actionCurve(e, t, r, i, n,
|
|
254
|
-
return C(this, e, t, r, i, n,
|
|
254
|
+
async actionCurve(e, t, r, i, n, a = !1) {
|
|
255
|
+
return C(this, e, t, r, i, n, a);
|
|
255
256
|
}
|
|
256
257
|
async actionConvex(e, t, r, i, n = !1) {
|
|
257
258
|
return y(this, e, t, r, i, n);
|
|
258
259
|
}
|
|
259
|
-
async actionBeefy(e, t, r, i, n,
|
|
260
|
+
async actionBeefy(e, t, r, i, n, a, m = !1) {
|
|
260
261
|
return A(
|
|
261
262
|
this,
|
|
262
263
|
e,
|
|
@@ -264,12 +265,12 @@ class Le {
|
|
|
264
265
|
r,
|
|
265
266
|
i,
|
|
266
267
|
n,
|
|
267
|
-
|
|
268
|
+
a,
|
|
268
269
|
m
|
|
269
270
|
);
|
|
270
271
|
}
|
|
271
|
-
async actionLido(e, t, r, i, n,
|
|
272
|
-
return I(this, e, t, r, i, n,
|
|
272
|
+
async actionLido(e, t, r, i, n, a = !1) {
|
|
273
|
+
return I(this, e, t, r, i, n, a);
|
|
273
274
|
}
|
|
274
275
|
async actionPendle(e, t, r, i, n = !1) {
|
|
275
276
|
return f(this, e, t, r, i, n);
|
|
@@ -290,13 +291,13 @@ class Le {
|
|
|
290
291
|
async actionStake(e, t, r, i) {
|
|
291
292
|
return B(this, e, t, r, i);
|
|
292
293
|
}
|
|
293
|
-
async actionPrivateWallet(e, t, r, i = [], n,
|
|
294
|
-
return b(this, e, t, r, i, n,
|
|
294
|
+
async actionPrivateWallet(e, t, r, i = [], n, a = !1) {
|
|
295
|
+
return b(this, e, t, r, i, n, a);
|
|
295
296
|
}
|
|
296
297
|
async getPendleLPClaimableParams(e, t) {
|
|
297
298
|
return x(this, e, t);
|
|
298
299
|
}
|
|
299
300
|
}
|
|
300
301
|
export {
|
|
301
|
-
|
|
302
|
+
Re as Hinkal
|
|
302
303
|
};
|
|
@@ -31,6 +31,7 @@ export interface IHinkal<ConnectorType = unknown> {
|
|
|
31
31
|
nullifierSnapshotService?: ClientNullifierSnapshotService;
|
|
32
32
|
accessTokenSnapshotService?: ClientAccessTokenSnapshotService;
|
|
33
33
|
transactionsManager: TransactionsManager;
|
|
34
|
+
inMemoryCache: Map<string, string>;
|
|
34
35
|
getBalances(): Promise<TokenBalance[]>;
|
|
35
36
|
getSupportedPassportLinks(): string[];
|
|
36
37
|
checkAccessToken(): Promise<boolean>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("../utxo/Utxo.cjs"),u=require("../../webworker/utxoWorker/utxoWorker.types.cjs"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("../utxo/Utxo.cjs"),u=require("../../webworker/utxoWorker/utxoWorker.types.cjs"),d=require("../../webworker/performTaskWithWorker.cjs"),y=require("../../webworker/worker.registry.cjs"),m=require("../../constants/vite.constants.cjs");class S{NUM_WORKERS;NUM_WORKERS_BATCH_PROCESS;constructor(){m.isNode?(this.NUM_WORKERS=1,this.NUM_WORKERS_BATCH_PROCESS=1):(this.NUM_WORKERS=3,this.NUM_WORKERS_BATCH_PROCESS=Math.ceil(navigator.deviceMemory??4))}async createUtxoPromise(s){return await d.performTaskWithWorker({type:y.WorkerVariant.UTXO,payload:s})}async batchFilterUtxosWithNullifier(s,t){const r=this.sliceArrayIntoParts(s,this.NUM_WORKERS).map(o=>this.createUtxoPromise({type:u.UtxoWorkerActionType.BATCH_FILTER_UTXOS_WITH_NULLIFIER,data:{utxos:o.map(a=>a.getConstructableParams()),nullifiers:t}}));return(await Promise.all(r)).flat().map(o=>new p.Utxo(o))}async buildBatchProcess(s,t){const r=this.sliceArrayIntoParts(s,this.NUM_WORKERS).map(a=>this.createUtxoPromise({type:u.UtxoWorkerActionType.BUILD_UTXOS,data:{signature:t.getSignature(),encryptedOutputs:a}}));return(await Promise.all(r)).flat().map(a=>new p.Utxo(a))}sliceArrayIntoParts(s,t){const e=Math.ceil(s.length/t);return Array.from({length:t},(c,o)=>s.slice(e*o,e*(o+1)))}findLastOutput(s,t){let e;return s.forEach(r=>{r!==t&&(e=r)}),e}async batchProcess(s,t,e){const r=this.sliceArrayIntoParts(s,this.NUM_WORKERS_BATCH_PROCESS),c=e.getSignature(),o=r.map(i=>{const n={type:u.UtxoWorkerActionType.DECIPHER_OUTPUTS,data:{encryptedOutputs:i,lastOutput:t,signature:c}};return this.createUtxoPromise(n)}),a=await Promise.all(o),R=a.map(({additionalEncryptedOutputs:i})=>i).flat(),l=new Set,h=R.reduce((i,n)=>l.has(n.value)?i:(l.add(n.value),[...i,n]),[]),U=this.findLastOutput(a.map(i=>i.lastOutput),t);return{additionalEncryptedOutputs:h,lastOutput:U??t}}}exports.MultiThreadedUtxoUtils=S;
|
|
@@ -15,5 +15,6 @@ export declare class MultiThreadedUtxoUtils implements IMultiThreadedUtxoUtils {
|
|
|
15
15
|
batchFilterUtxosWithNullifier(allUtxos: Utxo[], nullifiers: Set<string>): Promise<Utxo[]>;
|
|
16
16
|
buildBatchProcess(encryptedOutputs: EncryptedOutputWithSign[], userKeys: UserKeys): Promise<Utxo[]>;
|
|
17
17
|
sliceArrayIntoParts<T>(arr: T[], numberOfParts: number): T[][];
|
|
18
|
+
findLastOutput(lastOutputsArray: string[], lastOutput: string): undefined;
|
|
18
19
|
batchProcess(encryptedOutputs: EncryptedOutputWithSign[], lastOutput: string, userKeys: UserKeys): Promise<OutputsWithSignAndLast<boolean>>;
|
|
19
20
|
}
|
|
@@ -1,64 +1,73 @@
|
|
|
1
|
-
import { Utxo as
|
|
1
|
+
import { Utxo as l } from "../utxo/Utxo.mjs";
|
|
2
2
|
import { UtxoWorkerActionType as u } from "../../webworker/utxoWorker/utxoWorker.types.mjs";
|
|
3
|
-
import { performTaskWithWorker as
|
|
4
|
-
import { WorkerVariant as
|
|
5
|
-
import { isNode as
|
|
3
|
+
import { performTaskWithWorker as d } from "../../webworker/performTaskWithWorker.mjs";
|
|
4
|
+
import { WorkerVariant as O } from "../../webworker/worker.registry.mjs";
|
|
5
|
+
import { isNode as S } from "../../constants/vite.constants.mjs";
|
|
6
6
|
class W {
|
|
7
7
|
NUM_WORKERS;
|
|
8
8
|
NUM_WORKERS_BATCH_PROCESS;
|
|
9
9
|
constructor() {
|
|
10
|
-
|
|
10
|
+
S ? (this.NUM_WORKERS = 1, this.NUM_WORKERS_BATCH_PROCESS = 1) : (this.NUM_WORKERS = 3, this.NUM_WORKERS_BATCH_PROCESS = Math.ceil(navigator.deviceMemory ?? 4));
|
|
11
11
|
}
|
|
12
12
|
async createUtxoPromise(s) {
|
|
13
|
-
return await
|
|
14
|
-
type:
|
|
13
|
+
return await d({
|
|
14
|
+
type: O.UTXO,
|
|
15
15
|
payload: s
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
|
-
async batchFilterUtxosWithNullifier(s,
|
|
19
|
-
const
|
|
20
|
-
(
|
|
18
|
+
async batchFilterUtxosWithNullifier(s, t) {
|
|
19
|
+
const r = this.sliceArrayIntoParts(s, this.NUM_WORKERS).map(
|
|
20
|
+
(a) => this.createUtxoPromise({
|
|
21
21
|
type: u.BATCH_FILTER_UTXOS_WITH_NULLIFIER,
|
|
22
22
|
data: {
|
|
23
|
-
utxos:
|
|
24
|
-
nullifiers:
|
|
23
|
+
utxos: a.map((o) => o.getConstructableParams()),
|
|
24
|
+
nullifiers: t
|
|
25
25
|
}
|
|
26
26
|
})
|
|
27
27
|
);
|
|
28
|
-
return (await Promise.all(
|
|
28
|
+
return (await Promise.all(r)).flat().map((a) => new l(a));
|
|
29
29
|
}
|
|
30
|
-
async buildBatchProcess(s,
|
|
31
|
-
const
|
|
32
|
-
(
|
|
30
|
+
async buildBatchProcess(s, t) {
|
|
31
|
+
const r = this.sliceArrayIntoParts(s, this.NUM_WORKERS).map(
|
|
32
|
+
(o) => this.createUtxoPromise({
|
|
33
33
|
type: u.BUILD_UTXOS,
|
|
34
|
-
data: { signature:
|
|
34
|
+
data: { signature: t.getSignature(), encryptedOutputs: o }
|
|
35
35
|
})
|
|
36
36
|
);
|
|
37
|
-
return (await Promise.all(
|
|
37
|
+
return (await Promise.all(r)).flat().map((o) => new l(o));
|
|
38
38
|
}
|
|
39
|
-
sliceArrayIntoParts(s,
|
|
40
|
-
const
|
|
39
|
+
sliceArrayIntoParts(s, t) {
|
|
40
|
+
const e = Math.ceil(s.length / t);
|
|
41
41
|
return Array.from(
|
|
42
|
-
{ length:
|
|
43
|
-
(c,
|
|
42
|
+
{ length: t },
|
|
43
|
+
(c, a) => s.slice(e * a, e * (a + 1))
|
|
44
44
|
);
|
|
45
45
|
}
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
findLastOutput(s, t) {
|
|
47
|
+
let e;
|
|
48
|
+
return s.forEach((r) => {
|
|
49
|
+
r !== t && (e = r);
|
|
50
|
+
}), e;
|
|
51
|
+
}
|
|
52
|
+
async batchProcess(s, t, e) {
|
|
53
|
+
const r = this.sliceArrayIntoParts(s, this.NUM_WORKERS_BATCH_PROCESS), c = e.getSignature(), a = r.map((i) => {
|
|
48
54
|
const n = {
|
|
49
55
|
type: u.DECIPHER_OUTPUTS,
|
|
50
56
|
data: {
|
|
51
|
-
encryptedOutputs:
|
|
52
|
-
lastOutput:
|
|
57
|
+
encryptedOutputs: i,
|
|
58
|
+
lastOutput: t,
|
|
53
59
|
// doesn't matter what we pass here, it will not be used regardless
|
|
54
|
-
signature:
|
|
60
|
+
signature: c
|
|
55
61
|
}
|
|
56
62
|
};
|
|
57
63
|
return this.createUtxoPromise(n);
|
|
58
|
-
}),
|
|
64
|
+
}), o = await Promise.all(a), m = o.map(({ additionalEncryptedOutputs: i }) => i).flat(), p = /* @__PURE__ */ new Set(), R = m.reduce((i, n) => p.has(n.value) ? i : (p.add(n.value), [...i, n]), []), h = this.findLastOutput(
|
|
65
|
+
o.map((i) => i.lastOutput),
|
|
66
|
+
t
|
|
67
|
+
);
|
|
59
68
|
return {
|
|
60
|
-
additionalEncryptedOutputs:
|
|
61
|
-
lastOutput:
|
|
69
|
+
additionalEncryptedOutputs: R,
|
|
70
|
+
lastOutput: h ?? t
|
|
62
71
|
// keep the last output the same if no new output is present
|
|
63
72
|
};
|
|
64
73
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("./getDataFromTransaction.cjs"),C=require("idb-keyval"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("../../types/ethereum-network.types.cjs"),y=require("./getDataFromTransaction.cjs"),C=require("idb-keyval"),g={encryptedOutputs:[],lastOutput:""},d=(e,t)=>{const s=e.substring(0,25),r=t.substring(0,25);return{shortPublicKey:s,shortHinkalAddress:r}},H=e=>{const t=e.getCurrentChainId(),s=e.getContract(u.ContractType.HinkalContract).address,r=e.userKeys.getShieldedPublicKey();if(!t||!r||!s)throw Error("GetHinkalCache: incorrect arguments");const{shortPublicKey:c,shortHinkalAddress:a}=d(r,s);return JSON.parse(typeof localStorage<"u"?localStorage.getItem(`hinkalCache-${t}-${c}-${a}`)??JSON.stringify(g):e.inMemoryCache.get(`hinkalCache-${t}-${c}-${a}`)??JSON.stringify(g))},S=(e,t)=>{const s=t.getCurrentChainId(),r=t.getContract(u.ContractType.HinkalContract).address,c=t.userKeys.getShieldedPublicKey();if(!s||!c||!r)throw Error("SetHinkalCache: incorrect arguments");const{shortPublicKey:a,shortHinkalAddress:n}=d(c,r);typeof localStorage<"u"?localStorage.setItem(`hinkalCache-${s}-${a}-${n}`,JSON.stringify(e)):t.inMemoryCache.set(`hinkalCache-${s}-${a}-${n}`,JSON.stringify(e))},T=e=>{S(g,e)},f=(e,t,s)=>`txsCache-${e}-${t}-${s}`,m=async(e,t,s,r,c)=>{if(!t||!s||!r)throw Error("saveTxsCache: incorrect arguments");const{shortPublicKey:a,shortHinkalAddress:n}=d(s,r),l=y.serializeDecodedTxs(e),o=f(t,a,n),h=JSON.stringify({serializedTxs:l,lastHash:c});try{await C.set(o,h)}catch{localStorage.setItem(o,h)}},x={serializedTxs:[],lastHash:""},p=async(e,t,s)=>{if(!e||!t||!s)throw Error("loadTxsCache: incorrect arguments");const{shortPublicKey:r,shortHinkalAddress:c}=d(t,s),a=f(e,r,c);let n;try{const i=await C.get(a);if(!i)throw new Error("Empty indexedDB cache");n=i}catch{n=localStorage.getItem(a)}const{serializedTxs:l,lastHash:o}=JSON.parse(n??JSON.stringify(x));return{decodedTxs:l.map(i=>y.deserializeDecodedTxs(i)),lastHash:o===""?void 0:o}};exports.getHinkalCache=H;exports.loadTxsCache=p;exports.resetCache=T;exports.saveTxsCache=m;exports.setHinkalCache=S;
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IHinkal } from '../../data-structures';
|
|
2
|
+
import { EncryptedOutputWithSign } from '../../types/commitments.types';
|
|
3
|
+
import { DecodedTx } from '../../types/transactions.types';
|
|
2
4
|
interface HinkalCacheInterface {
|
|
3
5
|
encryptedOutputs: EncryptedOutputWithSign[];
|
|
4
6
|
lastOutput: string;
|
|
5
7
|
}
|
|
6
|
-
export declare const getHinkalCache: (
|
|
7
|
-
export declare const setHinkalCache: (hinkalCache: HinkalCacheInterface,
|
|
8
|
-
export declare const resetCache: (
|
|
8
|
+
export declare const getHinkalCache: (hinkal: IHinkal) => HinkalCacheInterface;
|
|
9
|
+
export declare const setHinkalCache: (hinkalCache: HinkalCacheInterface, hinkal: IHinkal) => void;
|
|
10
|
+
export declare const resetCache: (hinkal: IHinkal) => void;
|
|
9
11
|
export declare const saveTxsCache: (txs: DecodedTx[], chainId: number, shieldedPublicKey: string, hinkalAddress: string, lastHash: string) => Promise<void>;
|
|
10
12
|
export declare const loadTxsCache: (chainId: number, shieldedPublicKey: string, hinkalAddress: string) => Promise<{
|
|
11
|
-
decodedTxs: import(
|
|
13
|
+
decodedTxs: import("../../types/transactions.types").DecodedTxWithUtxos<bigint, import("ethers").BigNumber>[];
|
|
12
14
|
lastHash: string | undefined;
|
|
13
15
|
}>;
|
|
14
16
|
export {};
|
|
@@ -1,54 +1,60 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { ContractType as y } from "../../types/ethereum-network.types.mjs";
|
|
2
|
+
import { serializeDecodedTxs as C, deserializeDecodedTxs as f } from "./getDataFromTransaction.mjs";
|
|
3
|
+
import { set as m, get as S } from "idb-keyval";
|
|
4
|
+
const g = {
|
|
4
5
|
encryptedOutputs: [],
|
|
5
6
|
lastOutput: ""
|
|
6
|
-
},
|
|
7
|
-
const
|
|
8
|
-
return { shortPublicKey:
|
|
9
|
-
},
|
|
10
|
-
|
|
7
|
+
}, d = (t, e) => {
|
|
8
|
+
const s = t.substring(0, 25), r = e.substring(0, 25);
|
|
9
|
+
return { shortPublicKey: s, shortHinkalAddress: r };
|
|
10
|
+
}, k = (t) => {
|
|
11
|
+
const e = t.getCurrentChainId(), s = t.getContract(y.HinkalContract).address, r = t.userKeys.getShieldedPublicKey();
|
|
12
|
+
if (!e || !r || !s)
|
|
11
13
|
throw Error("GetHinkalCache: incorrect arguments");
|
|
12
|
-
const { shortPublicKey:
|
|
14
|
+
const { shortPublicKey: c, shortHinkalAddress: o } = d(r, s);
|
|
13
15
|
return JSON.parse(
|
|
14
|
-
typeof localStorage < "u" ? localStorage.getItem(`hinkalCache-${e}-${
|
|
16
|
+
typeof localStorage < "u" ? localStorage.getItem(`hinkalCache-${e}-${c}-${o}`) ?? JSON.stringify(g) : t.inMemoryCache.get(`hinkalCache-${e}-${c}-${o}`) ?? JSON.stringify(g)
|
|
15
17
|
);
|
|
16
|
-
}, p = (
|
|
17
|
-
|
|
18
|
+
}, p = (t, e) => {
|
|
19
|
+
const s = e.getCurrentChainId(), r = e.getContract(y.HinkalContract).address, c = e.userKeys.getShieldedPublicKey();
|
|
20
|
+
if (!s || !c || !r)
|
|
18
21
|
throw Error("SetHinkalCache: incorrect arguments");
|
|
19
|
-
const { shortPublicKey: o, shortHinkalAddress:
|
|
20
|
-
typeof localStorage < "u" ? localStorage.setItem(`hinkalCache-${
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
const { shortPublicKey: o, shortHinkalAddress: n } = d(c, r);
|
|
23
|
+
typeof localStorage < "u" ? localStorage.setItem(`hinkalCache-${s}-${o}-${n}`, JSON.stringify(t)) : e.inMemoryCache.set(
|
|
24
|
+
`hinkalCache-${s}-${o}-${n}`,
|
|
25
|
+
JSON.stringify(t)
|
|
26
|
+
);
|
|
27
|
+
}, K = (t) => {
|
|
28
|
+
p(g, t);
|
|
29
|
+
}, u = (t, e, s) => `txsCache-${t}-${e}-${s}`, O = async (t, e, s, r, c) => {
|
|
30
|
+
if (!e || !s || !r)
|
|
25
31
|
throw Error("saveTxsCache: incorrect arguments");
|
|
26
|
-
const { shortPublicKey:
|
|
32
|
+
const { shortPublicKey: o, shortHinkalAddress: n } = d(s, r), l = C(t), a = u(e, o, n), h = JSON.stringify({ serializedTxs: l, lastHash: c });
|
|
27
33
|
try {
|
|
28
|
-
await
|
|
34
|
+
await m(a, h);
|
|
29
35
|
} catch {
|
|
30
|
-
localStorage.setItem(
|
|
36
|
+
localStorage.setItem(a, h);
|
|
31
37
|
}
|
|
32
|
-
}, x = { serializedTxs: [], lastHash: "" },
|
|
33
|
-
if (!
|
|
38
|
+
}, x = { serializedTxs: [], lastHash: "" }, b = async (t, e, s) => {
|
|
39
|
+
if (!t || !e || !s)
|
|
34
40
|
throw Error("loadTxsCache: incorrect arguments");
|
|
35
|
-
const { shortPublicKey:
|
|
41
|
+
const { shortPublicKey: r, shortHinkalAddress: c } = d(e, s), o = u(t, r, c);
|
|
36
42
|
let n;
|
|
37
43
|
try {
|
|
38
|
-
const i = await
|
|
44
|
+
const i = await S(o);
|
|
39
45
|
if (!i)
|
|
40
46
|
throw new Error("Empty indexedDB cache");
|
|
41
47
|
n = i;
|
|
42
48
|
} catch {
|
|
43
|
-
n = localStorage.getItem(
|
|
49
|
+
n = localStorage.getItem(o);
|
|
44
50
|
}
|
|
45
|
-
const { serializedTxs:
|
|
46
|
-
return { decodedTxs:
|
|
51
|
+
const { serializedTxs: l, lastHash: a } = JSON.parse(n ?? JSON.stringify(x));
|
|
52
|
+
return { decodedTxs: l.map((i) => f(i)), lastHash: a === "" ? void 0 : a };
|
|
47
53
|
};
|
|
48
54
|
export {
|
|
49
55
|
k as getHinkalCache,
|
|
50
|
-
|
|
51
|
-
|
|
56
|
+
b as loadTxsCache,
|
|
57
|
+
K as resetCache,
|
|
52
58
|
O as saveTxsCache,
|
|
53
59
|
p as setHinkalCache
|
|
54
60
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("../../../data-structures/utxo/Utxo.cjs"),g=require("../../utils/cacheFunctions.cjs"),h=async(t,i,a,u,n=0)=>(await t.utxoUtils.buildBatchProcess(i,a)).reduce((o,e)=>u&&n!==0?e.erc20TokenAddress.toLowerCase()===u.toLowerCase()&&e.tokenId===n?[...o,e]:o:u?e.erc20TokenAddress.toLowerCase()===u.toLowerCase()?[...o,e]:o:[...o,e],[]),O=async(t,i,a,u,n,r,o,e=0)=>{const s=await t.utxoUtils.batchFilterUtxosWithNullifier(i,a);if(o){for(s.sort((d,l)=>d.amount<=l.amount?1:-1);s.length<u||s.length>u&&s.length<6;)s.push(new f.Utxo({amount:0n,erc20TokenAddress:o,shieldedPrivateKey:r,tokenId:e}));if(n)for(;s.length>6;)s.splice(s.length-1)}return{inputUtxos:s}},C=async({hinkal:t,erc20TokenAddress:i=void 0,minInput:a=2,sliceIfMore6:u=!0,tokenId:n=0})=>{if(!t.getCurrentChainId||!t.getSelectedNetwork())return{inputUtxos:[]};const{encryptedOutputs:r,nullifiers:o,userKeys:e}=t,s=Array.from(r),d=e.getShieldedPrivateKey();let l=[],U=[],{encryptedOutputs:y,lastOutput:c}=g.getHinkalCache(t),p=s.findIndex(x=>x.value===c);if(p>-1||y.length===0){p+=1;const x=s.slice(p);({lastOutput:c,additionalEncryptedOutputs:U}=await t.utxoUtils.batchProcess(x,c,e)),l=[...y,...U],g.setHinkalCache({lastOutput:c,encryptedOutputs:l},t)}const w=await h(t,l,e,i,n);return await O(t,w,o,a,u,d,i,n)};exports.getInputUtxoAndBalance=C;exports.getInputUtxosFromEncryptedOutputs=h;
|
|
@@ -1,69 +1,62 @@
|
|
|
1
|
-
import { Utxo as
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const s = await e.utxoUtils.batchFilterUtxosWithNullifier(u, r);
|
|
1
|
+
import { Utxo as y } from "../../../data-structures/utxo/Utxo.mjs";
|
|
2
|
+
import { getHinkalCache as h, setHinkalCache as g } from "../../utils/cacheFunctions.mjs";
|
|
3
|
+
const O = async (t, i, l, u, n = 0) => (await t.utxoUtils.buildBatchProcess(i, l)).reduce((o, e) => u && n !== 0 ? e.erc20TokenAddress.toLowerCase() === u.toLowerCase() && e.tokenId === n ? [...o, e] : o : u ? e.erc20TokenAddress.toLowerCase() === u.toLowerCase() ? [...o, e] : o : [...o, e], []), m = async (t, i, l, u, n, r, o, e = 0) => {
|
|
4
|
+
const s = await t.utxoUtils.batchFilterUtxosWithNullifier(i, l);
|
|
6
5
|
if (o) {
|
|
7
|
-
for (s.sort((
|
|
8
|
-
s.push(new
|
|
9
|
-
if (
|
|
6
|
+
for (s.sort((p, a) => p.amount <= a.amount ? 1 : -1); s.length < u || s.length > u && s.length < 6; )
|
|
7
|
+
s.push(new y({ amount: 0n, erc20TokenAddress: o, shieldedPrivateKey: r, tokenId: e }));
|
|
8
|
+
if (n)
|
|
10
9
|
for (; s.length > 6; )
|
|
11
10
|
s.splice(s.length - 1);
|
|
12
11
|
}
|
|
13
12
|
return {
|
|
14
13
|
inputUtxos: s
|
|
15
14
|
};
|
|
16
|
-
},
|
|
17
|
-
hinkal:
|
|
18
|
-
erc20TokenAddress:
|
|
19
|
-
minInput:
|
|
20
|
-
sliceIfMore6:
|
|
21
|
-
tokenId:
|
|
15
|
+
}, P = async ({
|
|
16
|
+
hinkal: t,
|
|
17
|
+
erc20TokenAddress: i = void 0,
|
|
18
|
+
minInput: l = 2,
|
|
19
|
+
sliceIfMore6: u = !0,
|
|
20
|
+
tokenId: n = 0
|
|
22
21
|
}) => {
|
|
23
|
-
if (!
|
|
22
|
+
if (!t.getCurrentChainId || !t.getSelectedNetwork())
|
|
24
23
|
return { inputUtxos: [] };
|
|
25
|
-
const { encryptedOutputs:
|
|
26
|
-
let
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (h > -1 || g.length === 0) {
|
|
32
|
-
h += 1;
|
|
33
|
-
const x = s.slice(h);
|
|
34
|
-
({ lastOutput: d, additionalEncryptedOutputs: f } = await e.utxoUtils.batchProcess(
|
|
24
|
+
const { encryptedOutputs: r, nullifiers: o, userKeys: e } = t, s = Array.from(r), p = e.getShieldedPrivateKey();
|
|
25
|
+
let a = [], U = [], { encryptedOutputs: f, lastOutput: c } = h(t), d = s.findIndex((x) => x.value === c);
|
|
26
|
+
if (d > -1 || f.length === 0) {
|
|
27
|
+
d += 1;
|
|
28
|
+
const x = s.slice(d);
|
|
29
|
+
({ lastOutput: c, additionalEncryptedOutputs: U } = await t.utxoUtils.batchProcess(
|
|
35
30
|
x,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
)),
|
|
31
|
+
c,
|
|
32
|
+
e
|
|
33
|
+
)), a = [...f, ...U], g(
|
|
39
34
|
{
|
|
40
|
-
lastOutput:
|
|
41
|
-
encryptedOutputs:
|
|
35
|
+
lastOutput: c,
|
|
36
|
+
encryptedOutputs: a
|
|
42
37
|
},
|
|
43
|
-
|
|
44
|
-
y,
|
|
45
|
-
c
|
|
38
|
+
t
|
|
46
39
|
);
|
|
47
40
|
}
|
|
48
|
-
const w = await
|
|
49
|
-
e,
|
|
50
|
-
p,
|
|
41
|
+
const w = await O(
|
|
51
42
|
t,
|
|
52
|
-
|
|
53
|
-
i
|
|
54
|
-
);
|
|
55
|
-
return await I(
|
|
43
|
+
a,
|
|
56
44
|
e,
|
|
45
|
+
i,
|
|
46
|
+
n
|
|
47
|
+
);
|
|
48
|
+
return await m(
|
|
49
|
+
t,
|
|
57
50
|
w,
|
|
58
51
|
o,
|
|
59
|
-
|
|
60
|
-
n,
|
|
61
|
-
U,
|
|
52
|
+
l,
|
|
62
53
|
u,
|
|
63
|
-
|
|
54
|
+
p,
|
|
55
|
+
i,
|
|
56
|
+
n
|
|
64
57
|
);
|
|
65
58
|
};
|
|
66
59
|
export {
|
|
67
|
-
|
|
68
|
-
|
|
60
|
+
P as getInputUtxoAndBalance,
|
|
61
|
+
O as getInputUtxosFromEncryptedOutputs
|
|
69
62
|
};
|