@sabaaa1/common 0.0.1
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/.env.example +5 -0
- package/.eslintrc.json +18 -0
- package/README.md +164 -0
- package/copyPackageJson.js +81 -0
- package/handleCDNWorkers.js +92 -0
- package/jest.config.ts +10 -0
- package/package.json +101 -0
- package/polyfillWorker.ts +15 -0
- package/project.json +32 -0
- package/src/API/API.ts +203 -0
- package/src/API/admin-calls.ts +8 -0
- package/src/API/ai-calls.ts +48 -0
- package/src/API/brotherhoodCalls.ts +184 -0
- package/src/API/callLifiAPI.ts +41 -0
- package/src/API/callMonitor.ts +9 -0
- package/src/API/callOdosAPI.ts +89 -0
- package/src/API/callOneInchAPI.ts +47 -0
- package/src/API/callRelayer.ts +23 -0
- package/src/API/checkRisk.ts +7 -0
- package/src/API/duneAPI.ts +26 -0
- package/src/API/enclaveCalls.ts +23 -0
- package/src/API/fetchCommitmentsCache.ts +63 -0
- package/src/API/generate-trading-points.ts +77 -0
- package/src/API/get-token-data.ts +12 -0
- package/src/API/getBridgeProtocols.ts +16 -0
- package/src/API/getCoingeckoPrice.ts +59 -0
- package/src/API/getGasEstimates.ts +30 -0
- package/src/API/getLifiStatus.ts +35 -0
- package/src/API/getRelayerURL.ts +27 -0
- package/src/API/getServerURL.ts +116 -0
- package/src/API/getTokenPrice.ts +39 -0
- package/src/API/getWebsiteURL.ts +58 -0
- package/src/API/index.ts +19 -0
- package/src/API/is-tx-stateless.ts +32 -0
- package/src/API/kycCalls.ts +81 -0
- package/src/API/leaderboardCalls.ts +32 -0
- package/src/API/mysteryBoxesCalls.ts +141 -0
- package/src/API/passwordCalls.ts +34 -0
- package/src/API/proxyAccountsCalls.ts +40 -0
- package/src/API/referralsCalls.ts +52 -0
- package/src/API/relayCalls.ts +14 -0
- package/src/API/restoreSnapshots.ts +7 -0
- package/src/API/rewardsUserCalls.ts +124 -0
- package/src/API/send-telegram-callback.ts +17 -0
- package/src/API/simulate-batch-tx.ts +19 -0
- package/src/API/tenderly.api.ts +96 -0
- package/src/API/token-calls.ts +19 -0
- package/src/API/weeklyAnalyticCalls.ts +43 -0
- package/src/constants/activity.constants.ts +10 -0
- package/src/constants/addresses.constants.ts +74 -0
- package/src/constants/assets.constants.ts +15 -0
- package/src/constants/backend.constants.ts +16 -0
- package/src/constants/balance.constants.ts +1 -0
- package/src/constants/bridging.constants.ts +5 -0
- package/src/constants/chains.constants.ts +194 -0
- package/src/constants/coingecko.constants.ts +67 -0
- package/src/constants/contracts.constants.ts +106 -0
- package/src/constants/conversion.constants.ts +1 -0
- package/src/constants/deploy-data/deploy-data-arbMainnet.json +5685 -0
- package/src/constants/deploy-data/deploy-data-arcTestnet.json +6157 -0
- package/src/constants/deploy-data/deploy-data-avalanche.json +5921 -0
- package/src/constants/deploy-data/deploy-data-base.json +5695 -0
- package/src/constants/deploy-data/deploy-data-bnbMainnet.json +5921 -0
- package/src/constants/deploy-data/deploy-data-ethMainnet.json +5922 -0
- package/src/constants/deploy-data/deploy-data-localhost.json +5921 -0
- package/src/constants/deploy-data/deploy-data-optimism.json +5687 -0
- package/src/constants/deploy-data/deploy-data-polygon.json +5695 -0
- package/src/constants/deploy-data/index.ts +21 -0
- package/src/constants/events.constants.ts +10 -0
- package/src/constants/fees.constants.ts +4 -0
- package/src/constants/function-signatures.constants.ts +6 -0
- package/src/constants/index.ts +30 -0
- package/src/constants/kyc.constants.ts +202 -0
- package/src/constants/lifi.constants.ts +1 -0
- package/src/constants/mediaUrls.constants.ts +25 -0
- package/src/constants/mystery-boxes.constants.ts +402 -0
- package/src/constants/oauthUrls.constants.ts +7 -0
- package/src/constants/permit2.constants.ts +28 -0
- package/src/constants/presale.constants.ts +503 -0
- package/src/constants/protocol.constants.ts +65 -0
- package/src/constants/reorg-depths.constants.ts +13 -0
- package/src/constants/rewards.constants.ts +27 -0
- package/src/constants/save-depths.ts +13 -0
- package/src/constants/server.constants.ts +233 -0
- package/src/constants/tasks.constants.ts +205 -0
- package/src/constants/token-data/ERC20Registry.ts +90 -0
- package/src/constants/token-data/arbMainnetRegistry.json +1841 -0
- package/src/constants/token-data/arbMainnetRegistryFixed.json +1841 -0
- package/src/constants/token-data/arcTestnetRegistry.json +436 -0
- package/src/constants/token-data/avalancheRegistry.json +729 -0
- package/src/constants/token-data/avalancheRegistryFixed.json +729 -0
- package/src/constants/token-data/baseRegistry.json +1134 -0
- package/src/constants/token-data/baseRegistryFixed.json +1134 -0
- package/src/constants/token-data/bnbMainnetRegistry.json +900 -0
- package/src/constants/token-data/bnbMainnetRegistryFixed.json +900 -0
- package/src/constants/token-data/ethMainnetRegistry.json +4298 -0
- package/src/constants/token-data/ethMainnetRegistryFixed.json +4298 -0
- package/src/constants/token-data/index.ts +60 -0
- package/src/constants/token-data/localhostRegistry.json +2784 -0
- package/src/constants/token-data/optimismRegistry.json +701 -0
- package/src/constants/token-data/optimismRegistryFixed.json +701 -0
- package/src/constants/token-data/polygonRegistry.json +2309 -0
- package/src/constants/token-data/polygonRegistryFixed.json +2309 -0
- package/src/constants/token-data/popularTokens.constants.ts +11 -0
- package/src/constants/token-data/tokenPricing.consts.ts +2 -0
- package/src/constants/token.limits.constants.ts +31 -0
- package/src/constants/vite.constants.ts +74 -0
- package/src/constants/wallet.constants.ts +9 -0
- package/src/crypto/babyJub.ts +31 -0
- package/src/crypto/index.ts +3 -0
- package/src/crypto/poseidon.ts +36 -0
- package/src/crypto/preProcessing.ts +20 -0
- package/src/data-structures/ApprovalDBs/EventsPublicApprovalsDB.ts +288 -0
- package/src/data-structures/ApprovalDBs/index.ts +1 -0
- package/src/data-structures/Hinkal/Hinkal.ts +746 -0
- package/src/data-structures/Hinkal/IHinkal.ts +315 -0
- package/src/data-structures/Hinkal/handleAutoDepositBack.ts +138 -0
- package/src/data-structures/Hinkal/hinkalActionFundApproveAndTransact.ts +64 -0
- package/src/data-structures/Hinkal/hinkalActionReceive.ts +65 -0
- package/src/data-structures/Hinkal/hinkalActionStake.ts +152 -0
- package/src/data-structures/Hinkal/hinkalApprove.ts +170 -0
- package/src/data-structures/Hinkal/hinkalCheckTokenRegistry.ts +26 -0
- package/src/data-structures/Hinkal/hinkalDeposit.ts +199 -0
- package/src/data-structures/Hinkal/hinkalGetRecipientInfo.ts +12 -0
- package/src/data-structures/Hinkal/hinkalGetZkMeProvider.ts +31 -0
- package/src/data-structures/Hinkal/hinkalInsideTransact.ts +190 -0
- package/src/data-structures/Hinkal/hinkalMultiSend.ts +26 -0
- package/src/data-structures/Hinkal/hinkalPrivateWallet.ts +198 -0
- package/src/data-structures/Hinkal/hinkalProoflessDeposit.ts +91 -0
- package/src/data-structures/Hinkal/hinkalProxySwap.ts +91 -0
- package/src/data-structures/Hinkal/hinkalSignSubAccount.ts +28 -0
- package/src/data-structures/Hinkal/hinkalSwap.ts +157 -0
- package/src/data-structures/Hinkal/hinkalTransfer.ts +171 -0
- package/src/data-structures/Hinkal/hinkalWithdraw.ts +161 -0
- package/src/data-structures/Hinkal/index.ts +9 -0
- package/src/data-structures/Hinkal/resetMerkleTrees.ts +64 -0
- package/src/data-structures/IndexedDB/activity-db.ts +204 -0
- package/src/data-structures/IndexedDB/balances-db.ts +97 -0
- package/src/data-structures/IndexedDB/contact-db.ts +81 -0
- package/src/data-structures/IndexedDB/index.ts +6 -0
- package/src/data-structures/IndexedDB/prices-db.ts +89 -0
- package/src/data-structures/IndexedDB/private-balances-db.ts +31 -0
- package/src/data-structures/IndexedDB/signatures-db.ts +62 -0
- package/src/data-structures/MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.ts +152 -0
- package/src/data-structures/TokenDBs/AlchemyPublicTokensDB.ts +224 -0
- package/src/data-structures/TokenDBs/ArcPublicTokensDB.ts +198 -0
- package/src/data-structures/TokenDBs/EventsPublicTokensDB.ts +216 -0
- package/src/data-structures/TokenDBs/IPublicTokensDB.ts +7 -0
- package/src/data-structures/TokenDBs/PrivateTokensDB.ts +96 -0
- package/src/data-structures/TokenDBs/PublicTokensDB.ts +49 -0
- package/src/data-structures/TokenDBs/index.ts +4 -0
- package/src/data-structures/TokenDBs/token-visibility-db.ts +154 -0
- package/src/data-structures/ValueCache/ValueCache.ts +29 -0
- package/src/data-structures/ValueCache/index.ts +1 -0
- package/src/data-structures/cacheDevices/AttachableMemoryCacheDevice.ts +33 -0
- package/src/data-structures/cacheDevices/BaseCacheDevice.ts +30 -0
- package/src/data-structures/cacheDevices/FileCacheDevice.ts +59 -0
- package/src/data-structures/cacheDevices/LocalStorageCacheDevice.ts +36 -0
- package/src/data-structures/cacheDevices/index.ts +2 -0
- package/src/data-structures/crypto-keys/decodeUTXO.ts +60 -0
- package/src/data-structures/crypto-keys/encryptDecryptUtxo.ts +74 -0
- package/src/data-structures/crypto-keys/index.ts +4 -0
- package/src/data-structures/crypto-keys/keyUtils.ts +8 -0
- package/src/data-structures/crypto-keys/keys.ts +203 -0
- package/src/data-structures/custom-token-registry/CustomTokenRegistry.ts +49 -0
- package/src/data-structures/event-service/AbstractAccessTokenSnapshotService.ts +142 -0
- package/src/data-structures/event-service/AbstractApprovalsSnapshotService.ts +152 -0
- package/src/data-structures/event-service/AbstractCommitmentsSnapshotService.ts +116 -0
- package/src/data-structures/event-service/AbstractEventService.ts +44 -0
- package/src/data-structures/event-service/AbstractNullifierSnapshotService.ts +76 -0
- package/src/data-structures/event-service/AbstractSnapshotService.ts +63 -0
- package/src/data-structures/event-service/BlockchainEventEmitter.ts +132 -0
- package/src/data-structures/event-service/index.ts +7 -0
- package/src/data-structures/index.ts +18 -0
- package/src/data-structures/merkle-tree/MerkleTree.test.ts +77 -0
- package/src/data-structures/merkle-tree/MerkleTree.ts +253 -0
- package/src/data-structures/merkle-tree/MerkleTreeIncompleteError.ts +6 -0
- package/src/data-structures/merkle-tree/getPatchedAccessTokenMerkleTree.ts +24 -0
- package/src/data-structures/merkle-tree/index.ts +3 -0
- package/src/data-structures/presale/PresaleContractWrapper.ts +80 -0
- package/src/data-structures/presale/index.ts +1 -0
- package/src/data-structures/provider-adapter/IProviderAdapter.ts +43 -0
- package/src/data-structures/snapshot/ClientAccessTokenSnapshotService.ts +41 -0
- package/src/data-structures/snapshot/ClientApprovalsSnapshotService.ts +36 -0
- package/src/data-structures/snapshot/ClientCommitmentsSnapshotService.ts +30 -0
- package/src/data-structures/snapshot/ClientNullifierSnapshotService.ts +28 -0
- package/src/data-structures/snapshot/SnapshotFetcherService.ts +68 -0
- package/src/data-structures/snapshot/index.ts +1 -0
- package/src/data-structures/token-price-fetcher/TokenChecker.ts +45 -0
- package/src/data-structures/token-price-fetcher/index.ts +1 -0
- package/src/data-structures/tor/HttpClient.ts +118 -0
- package/src/data-structures/tor/TorAdapter.ts +94 -0
- package/src/data-structures/tor/TorClient.ts +283 -0
- package/src/data-structures/tor/WebSocketDuplex.ts +32 -0
- package/src/data-structures/tor/index.ts +3 -0
- package/src/data-structures/transactions-manager/history/history.types.ts +14 -0
- package/src/data-structures/transactions-manager/index.ts +1 -0
- package/src/data-structures/utxo/Utxo.ts +189 -0
- package/src/error-handling/customErrors/ErrorWithAmount.ts +8 -0
- package/src/error-handling/customErrors/ErrorWithTx.ts +10 -0
- package/src/error-handling/customErrors/FeeOverTransactionValueError.ts +16 -0
- package/src/error-handling/customErrors/SimulationFailureError.ts +9 -0
- package/src/error-handling/customErrors/customErrors.helpers.ts +39 -0
- package/src/error-handling/customErrors/index.ts +5 -0
- package/src/error-handling/error-codes.constants.ts +214 -0
- package/src/error-handling/get-error.message.ts +149 -0
- package/src/error-handling/handleErrorRestore.ts +25 -0
- package/src/error-handling/index.ts +7 -0
- package/src/error-handling/logError.ts +8 -0
- package/src/error-handling/logger.ts +21 -0
- package/src/error-handling/types.ts +10 -0
- package/src/externalABIs/BUSD.ts +516 -0
- package/src/externalABIs/BabPassport.json +24 -0
- package/src/externalABIs/DAI.ts +331 -0
- package/src/externalABIs/ERC1155.json +316 -0
- package/src/externalABIs/ERC20.json +297 -0
- package/src/externalABIs/ERC20.ts +1 -0
- package/src/externalABIs/ERC721.json +205 -0
- package/src/externalABIs/GalxePassport.json +23 -0
- package/src/externalABIs/IQuoterV2.json +211 -0
- package/src/externalABIs/ISwapRouter.json +568 -0
- package/src/externalABIs/IUniswapV3Factory.json +207 -0
- package/src/externalABIs/IUniswapV3Pool.json +999 -0
- package/src/externalABIs/NodeInterfaceABI.json +1 -0
- package/src/externalABIs/OptimismGasPriceOracle.json +1 -0
- package/src/externalABIs/PoLidoNftAbi.json +307 -0
- package/src/externalABIs/SanctionsList.ts +86 -0
- package/src/externalABIs/USDC.ts +718 -0
- package/src/externalABIs/USDR.ts +343 -0
- package/src/externalABIs/USDR3CRV.ts +539 -0
- package/src/externalABIs/USDT.ts +390 -0
- package/src/externalABIs/UniswapV2PoolAbi.json +66 -0
- package/src/externalABIs/WETH.ts +153 -0
- package/src/externalABIs/amToken.ts +347 -0
- package/src/externalABIs/index.ts +53 -0
- package/src/externalABIs/swapAbi.ts +442 -0
- package/src/externalABIs/transactionsProver.json +45 -0
- package/src/functions/index.ts +16 -0
- package/src/functions/kyc/aiPriseHelper.ts +0 -0
- package/src/functions/kyc/authentoHelper.ts +44 -0
- package/src/functions/kyc/index.ts +3 -0
- package/src/functions/kyc/openDefaultPassportWindow.ts +9 -0
- package/src/functions/kyc/passportHelper.ts +13 -0
- package/src/functions/kyc/zkMeHelper.ts +69 -0
- package/src/functions/pre-transaction/constructAdminData.ts +21 -0
- package/src/functions/pre-transaction/getExternalSwapAddress.ts +30 -0
- package/src/functions/pre-transaction/getFeeStructure.ts +44 -0
- package/src/functions/pre-transaction/getFlatFees.ts +88 -0
- package/src/functions/pre-transaction/getSignatureDataForTransact.ts +16 -0
- package/src/functions/pre-transaction/getVolatileTransferAmount.ts +56 -0
- package/src/functions/pre-transaction/index.ts +7 -0
- package/src/functions/pre-transaction/interaction-to-action.ts +17 -0
- package/src/functions/pre-transaction/merge-with-fee-structure-emporium.ts +50 -0
- package/src/functions/pre-transaction/merge-with-fee-structure.ts +35 -0
- package/src/functions/pre-transaction/outputApprovalDataProcessing.ts +95 -0
- package/src/functions/pre-transaction/outputUtxoProcessing.ts +52 -0
- package/src/functions/pre-transaction/process-gas-estimates.ts +36 -0
- package/src/functions/pre-transaction/shouldPatchAccessTokenMerkleTree.ts +15 -0
- package/src/functions/private-wallet/emporium.helpers.ts +296 -0
- package/src/functions/private-wallet/emporium.swap.helpers.ts +284 -0
- package/src/functions/private-wallet/index.ts +3 -0
- package/src/functions/private-wallet/opProducer.ts +33 -0
- package/src/functions/snarkjs/common.snarkjs.ts +377 -0
- package/src/functions/snarkjs/constant.ts +490 -0
- package/src/functions/snarkjs/constructEmporiumProof.ts +301 -0
- package/src/functions/snarkjs/constructGeneralZkProof.ts +281 -0
- package/src/functions/snarkjs/generateCircomData.ts +81 -0
- package/src/functions/snarkjs/generateZkProof.ts +21 -0
- package/src/functions/snarkjs/generateZkProofEnclave.ts +45 -0
- package/src/functions/snarkjs/generateZkProofSelf.ts +29 -0
- package/src/functions/snarkjs/getOriginalSender.ts +4 -0
- package/src/functions/snarkjs/getZKFiles.ts +29 -0
- package/src/functions/snarkjs/index.ts +4 -0
- package/src/functions/staking/index.ts +78 -0
- package/src/functions/utils/addresses.ts +37 -0
- package/src/functions/utils/amounts.utils.test.ts +91 -0
- package/src/functions/utils/amounts.utils.ts +119 -0
- package/src/functions/utils/arraysMatch.ts +4 -0
- package/src/functions/utils/cacheDevice.utils.ts +21 -0
- package/src/functions/utils/cacheFunctions.ts +154 -0
- package/src/functions/utils/caseInsensitive.utils.ts +10 -0
- package/src/functions/utils/convertEmporiumOpToCallInfo.ts +19 -0
- package/src/functions/utils/convertIntegrationProviderToExternalActionId.ts +14 -0
- package/src/functions/utils/create-provider.ts +11 -0
- package/src/functions/utils/encodeTokenWithId.ts +4 -0
- package/src/functions/utils/encryptInputForEnclave.ts +119 -0
- package/src/functions/utils/enum.utils.ts +30 -0
- package/src/functions/utils/erc20tokenFunctions.ts +110 -0
- package/src/functions/utils/ethers-formatter.utils.ts +32 -0
- package/src/functions/utils/evmNetworkFunctions.ts +9 -0
- package/src/functions/utils/external-action.utils.ts +15 -0
- package/src/functions/utils/fees.utils.ts +26 -0
- package/src/functions/utils/get-hinkal-approvals.ts +36 -0
- package/src/functions/utils/get-signature-header.ts +5 -0
- package/src/functions/utils/getBlockExplorerUrl.ts +14 -0
- package/src/functions/utils/getDataFromTransaction.ts +79 -0
- package/src/functions/utils/getRecipientInfoFromUserKeys.ts +28 -0
- package/src/functions/utils/inLogicMetadata.ts +9 -0
- package/src/functions/utils/index.ts +43 -0
- package/src/functions/utils/involves-permit2-op.ts +17 -0
- package/src/functions/utils/ipfs.ts +74 -0
- package/src/functions/utils/is-valid-url.ts +5 -0
- package/src/functions/utils/lifi.utils.ts +3 -0
- package/src/functions/utils/memoize.utils.ts +23 -0
- package/src/functions/utils/merkleTree.utils.ts +27 -0
- package/src/functions/utils/mutexes.utils.ts +3 -0
- package/src/functions/utils/mystery-box-auction.utils.ts +61 -0
- package/src/functions/utils/networks.utils.ts +6 -0
- package/src/functions/utils/nftTokenFunctions.ts +25 -0
- package/src/functions/utils/nickname.utils.ts +4 -0
- package/src/functions/utils/postToOffscreen.ts +6 -0
- package/src/functions/utils/prepareHinkal.ts +78 -0
- package/src/functions/utils/process.utils.ts +23 -0
- package/src/functions/utils/processUseApprovalUtxoData.ts +26 -0
- package/src/functions/utils/publicBalance.utils.ts +131 -0
- package/src/functions/utils/reloadPage.ts +1 -0
- package/src/functions/utils/replaceAddressInCalldata.ts +6 -0
- package/src/functions/utils/requireEnv.ts +10 -0
- package/src/functions/utils/resolve-sync.utils.ts +40 -0
- package/src/functions/utils/rpc-int-encode.ts +7 -0
- package/src/functions/utils/serialize.utils.ts +12 -0
- package/src/functions/utils/string.utils.ts +92 -0
- package/src/functions/utils/time.utils.ts +80 -0
- package/src/functions/utils/token-check.utils.ts +12 -0
- package/src/functions/utils/trimFieldValues.ts +24 -0
- package/src/functions/utils/upToDateState.ts +12 -0
- package/src/functions/utils/userAgent.ts +26 -0
- package/src/functions/utils/walletBalances.utils.ts +117 -0
- package/src/functions/web3/EIP-712.test.ts +302 -0
- package/src/functions/web3/EIP-712.ts +243 -0
- package/src/functions/web3/etherFunctions.test.ts +129 -0
- package/src/functions/web3/etherFunctions.ts +84 -0
- package/src/functions/web3/events/balanceChangedCustomHandler.ts +13 -0
- package/src/functions/web3/events/balanceChangedHandler.ts +10 -0
- package/src/functions/web3/events/getApprovedBalance.ts +154 -0
- package/src/functions/web3/events/getInputUtxoAndBalance.ts +305 -0
- package/src/functions/web3/events/getInputUtxosEnclave.ts +40 -0
- package/src/functions/web3/events/getShieldedBalance.ts +163 -0
- package/src/functions/web3/events/getTransactionLogEvents.ts +55 -0
- package/src/functions/web3/events/index.ts +7 -0
- package/src/functions/web3/events/web3RetrieveEvents.ts +33 -0
- package/src/functions/web3/functionCalls/accessTokenCalls.ts +27 -0
- package/src/functions/web3/functionCalls/approveToken.ts +180 -0
- package/src/functions/web3/functionCalls/approveTokensToHinkal.ts +21 -0
- package/src/functions/web3/functionCalls/estimateGasRelayer.ts +40 -0
- package/src/functions/web3/functionCalls/getRootHash.ts +7 -0
- package/src/functions/web3/functionCalls/inHinkalApprovalCalls.ts +60 -0
- package/src/functions/web3/functionCalls/relayFunctions.ts +12 -0
- package/src/functions/web3/functionCalls/transactCallDirect.ts +71 -0
- package/src/functions/web3/functionCalls/transactCallRelayer.ts +57 -0
- package/src/functions/web3/getContractMetadata.ts +43 -0
- package/src/functions/web3/getPublicAddressBalance.ts +17 -0
- package/src/functions/web3/getTokenHolder.ts +53 -0
- package/src/functions/web3/index.ts +9 -0
- package/src/functions/web3/lifiAPI.ts +41 -0
- package/src/functions/web3/odosAPI.ts +50 -0
- package/src/functions/web3/oneInchAPI.ts +47 -0
- package/src/functions/web3/runContractFunction.ts +116 -0
- package/src/functions/web3/uniswapAPI.ts +156 -0
- package/src/index.ts +9 -0
- package/src/mutexes/index.ts +1 -0
- package/src/mutexes/mutex.ts +3 -0
- package/src/providers/EthersProviderAdapter.ts +230 -0
- package/src/providers/TorExternalProvider.ts +36 -0
- package/src/providers/WagmiProviderAdapter.ts +311 -0
- package/src/providers/exportProviers.ts +11 -0
- package/src/providers/prepareEthersHinkal.ts +18 -0
- package/src/providers/prepareWagmiHinkal.ts +16 -0
- package/src/types/API.types.ts +23 -0
- package/src/types/ICacheDevice.ts +5 -0
- package/src/types/IMultiThreadedUtxoUtils.ts +16 -0
- package/src/types/RewardUserEnums.ts +12 -0
- package/src/types/TransactionSimulator.types.ts +30 -0
- package/src/types/WeeklyAnalytics.types.ts +44 -0
- package/src/types/activities.types.ts +90 -0
- package/src/types/admin.types.ts +30 -0
- package/src/types/all-points.types.ts +8 -0
- package/src/types/approvals.types.ts +45 -0
- package/src/types/balances.types.ts +20 -0
- package/src/types/big-intable.types.ts +3 -0
- package/src/types/bridging.types.ts +30 -0
- package/src/types/brotherhood-user.types.ts +13 -0
- package/src/types/cache.types.ts +42 -0
- package/src/types/circom-data.types.ts +120 -0
- package/src/types/coingecko.types.ts +216 -0
- package/src/types/commitments.types.ts +39 -0
- package/src/types/confirmation-request.types.ts +10 -0
- package/src/types/contacts.types.ts +6 -0
- package/src/types/crypto.types.ts +8 -0
- package/src/types/duneAPI.types.ts +9 -0
- package/src/types/eip5792.types.ts +31 -0
- package/src/types/ethereum-network.types.ts +70 -0
- package/src/types/external-action.types.ts +19 -0
- package/src/types/fee.types.ts +11 -0
- package/src/types/generatePoints.ts +14 -0
- package/src/types/hinkal.stake.types.ts +12 -0
- package/src/types/hinkal.types.ts +150 -0
- package/src/types/index.ts +50 -0
- package/src/types/kyc.types.ts +49 -0
- package/src/types/merch-order.types.ts +18 -0
- package/src/types/message.types.ts +6 -0
- package/src/types/mystery-boxes.types.ts +50 -0
- package/src/types/new-rewards.type.ts +181 -0
- package/src/types/offscreen.types.ts +8 -0
- package/src/types/presaleReferral.types.ts +4 -0
- package/src/types/proxy.types.ts +11 -0
- package/src/types/referral.types.ts +13 -0
- package/src/types/relayer.types.ts +16 -0
- package/src/types/remote-proof.types.ts +18 -0
- package/src/types/rewards.types.ts +53 -0
- package/src/types/routing.types.ts +7 -0
- package/src/types/sandbox.types.ts +246 -0
- package/src/types/session.types.ts +12 -0
- package/src/types/signature.types.ts +11 -0
- package/src/types/slippage.types.ts +3 -0
- package/src/types/snark.types.ts +43 -0
- package/src/types/tenderly.api.types.ts +200 -0
- package/src/types/token-prices.types.ts +8 -0
- package/src/types/token-with-id.types.ts +4 -0
- package/src/types/token.types.ts +86 -0
- package/src/types/transactions.types.ts +120 -0
- package/src/types/wc.types.ts +83 -0
- package/src/types/with-id.types.ts +1 -0
- package/src/webworker/performTaskWithWorker.ts +50 -0
- package/src/webworker/snarkjsWorker/snarkjs.d.ts +2 -0
- package/src/webworker/snarkjsWorker/snarkjsWorker.types.ts +18 -0
- package/src/webworker/snarkjsWorker/snarkjsWorkerLauncher.ts +19 -0
- package/src/webworker/snarkjsWorker/snarkjsWorkerLogic.ts +90 -0
- package/src/webworker/utxoWorker/utxoWorker.types.ts +41 -0
- package/src/webworker/utxoWorker/utxoWorkerLauncher.ts +19 -0
- package/src/webworker/utxoWorker/utxoWorkerLogic.ts +107 -0
- package/src/webworker/viteWorkerURL.constant.ts +25 -0
- package/src/webworker/worker.registry.ts +11 -0
- package/src/webworker/worker.types.ts +35 -0
- package/src/webworker/workerErrorHandler.ts +10 -0
- package/src/webworker/workerFactory.ts +47 -0
- package/src/webworker/workerProxy.ts +88 -0
- package/src/webworker/zkProofWorker/zkProofWorker.types.ts +52 -0
- package/src/webworker/zkProofWorker/zkProofWorkerLauncher.ts +19 -0
- package/src/webworker/zkProofWorker/zkProofWorkerLogic.ts +84 -0
- package/tsconfig.json +23 -0
- package/tsconfig.lib.json +10 -0
- package/tsconfig.spec.json +15 -0
- package/vite.config.ts +97 -0
- package/vite.config.workers.ts +51 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { GenerateProofResponseType } from '../../types/remote-proof.types';
|
|
2
|
+
import { NewZkCallDataType } from '../../types/snark.types';
|
|
3
|
+
import { getWASMFile, getZKeyFile } from './getZKFiles';
|
|
4
|
+
import { ENCLAVE_EC2_URL } from './constant';
|
|
5
|
+
import { ProverVersion } from '../../types/hinkal.types';
|
|
6
|
+
import { makeEnclaveHandshakeAndEncryptInput } from '../utils/encryptInputForEnclave';
|
|
7
|
+
import { httpClient } from '../../data-structures/tor';
|
|
8
|
+
|
|
9
|
+
const retrieveVerifierFilename = (verifierName: string): string => {
|
|
10
|
+
if (verifierName.startsWith('http')) {
|
|
11
|
+
const splitVerifierName = verifierName.split('/');
|
|
12
|
+
const result = splitVerifierName.at(-1);
|
|
13
|
+
if (!result) throw new Error('invalid verifierName');
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
return verifierName;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const generateZkProofEnclave = async (
|
|
20
|
+
proverVersion: ProverVersion,
|
|
21
|
+
chainId: number,
|
|
22
|
+
verifierName: string,
|
|
23
|
+
input: any,
|
|
24
|
+
): Promise<{ zkCallData: NewZkCallDataType; publicSignals: string[] }> => {
|
|
25
|
+
const wasmFilename = retrieveVerifierFilename(getWASMFile(proverVersion, verifierName, chainId));
|
|
26
|
+
const zKeyFilename = retrieveVerifierFilename(getZKeyFile(proverVersion, verifierName, chainId));
|
|
27
|
+
|
|
28
|
+
// encrypt data for enclave, results are in base64
|
|
29
|
+
const { keyCiphertext, inputCiphertext } = await makeEnclaveHandshakeAndEncryptInput(input);
|
|
30
|
+
|
|
31
|
+
const data = {
|
|
32
|
+
chainId,
|
|
33
|
+
circuit_wasm: wasmFilename,
|
|
34
|
+
circuit_zkey: zKeyFilename,
|
|
35
|
+
input: inputCiphertext,
|
|
36
|
+
key: keyCiphertext,
|
|
37
|
+
version: proverVersion,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// generate proof and get necessary result from enclave
|
|
41
|
+
const proofResponse = await httpClient.post<GenerateProofResponseType>(`${ENCLAVE_EC2_URL}/generate`, data);
|
|
42
|
+
const { public_signals: publicSignals, zk_calldata: zkCallData } = proofResponse;
|
|
43
|
+
|
|
44
|
+
return { zkCallData, publicSignals };
|
|
45
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { NewZkCallDataType, ProverVersion, ZkResultType } from '../../types';
|
|
2
|
+
import { getWASMFile, getZKeyFile } from './getZKFiles';
|
|
3
|
+
import { performTaskWithWorker } from '../../webworker/performTaskWithWorker';
|
|
4
|
+
import { SnarkJSPayload, SnarkJSWorkerActionType } from '../../webworker/snarkjsWorker/snarkjsWorker.types';
|
|
5
|
+
import { WorkerVariant } from '../../webworker/worker.registry';
|
|
6
|
+
|
|
7
|
+
export const generateZkProofSelf = async (
|
|
8
|
+
proverVersion: ProverVersion,
|
|
9
|
+
chainId: number,
|
|
10
|
+
verifierName: string,
|
|
11
|
+
input: any,
|
|
12
|
+
): Promise<{ zkCallData: NewZkCallDataType; publicSignals: string[] }> => {
|
|
13
|
+
const wasmFilePath = getWASMFile(proverVersion, verifierName, chainId);
|
|
14
|
+
const zKeyFilePath = getZKeyFile(proverVersion, verifierName, chainId);
|
|
15
|
+
|
|
16
|
+
// snarkjs `input` param is structuredClone() safe so we can safely pass to worker
|
|
17
|
+
const { publicSignals, zkCallData } = await performTaskWithWorker<
|
|
18
|
+
SnarkJSPayload,
|
|
19
|
+
ZkResultType & { zkCallData: NewZkCallDataType }
|
|
20
|
+
>({
|
|
21
|
+
type: WorkerVariant.SnarkJS,
|
|
22
|
+
payload: {
|
|
23
|
+
type: SnarkJSWorkerActionType.FULL_PROVE,
|
|
24
|
+
data: { input, chainId, wasmFilePath, zKeyFilePath },
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return { zkCallData, publicSignals };
|
|
29
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ProverVersion } from '../../types/hinkal.types';
|
|
2
|
+
import { chainIds } from '../../constants/chains.constants';
|
|
3
|
+
import { localVerifiers, prodVerifiers } from './constant';
|
|
4
|
+
|
|
5
|
+
export const getWASMFile = (proverVersion: ProverVersion, filename: string, chainId: number) => {
|
|
6
|
+
const exactVersion = proverVersion === 'v1x0' ? '' : proverVersion;
|
|
7
|
+
if (chainId === chainIds.localhost) {
|
|
8
|
+
const verifier = localVerifiers[`${filename}${exactVersion}Wasm` as keyof typeof localVerifiers];
|
|
9
|
+
return verifier;
|
|
10
|
+
}
|
|
11
|
+
const verifier = prodVerifiers(chainId, proverVersion)[
|
|
12
|
+
`${filename}${exactVersion}Wasm` as keyof typeof prodVerifiers
|
|
13
|
+
];
|
|
14
|
+
console.log({ verifier });
|
|
15
|
+
return verifier;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const getZKeyFile = (proverVersion: ProverVersion, filename: string, chainId: number) => {
|
|
19
|
+
const exactVersion = proverVersion === 'v1x0' ? '' : proverVersion;
|
|
20
|
+
if (chainId === chainIds.localhost) {
|
|
21
|
+
const verifier = localVerifiers[`${filename}${exactVersion}Zkey` as keyof typeof localVerifiers];
|
|
22
|
+
return verifier;
|
|
23
|
+
}
|
|
24
|
+
const verifier = prodVerifiers(chainId, proverVersion)[
|
|
25
|
+
`${filename}${exactVersion}Zkey` as keyof typeof prodVerifiers
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
return verifier;
|
|
29
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/* eslint-disable no-await-in-loop */
|
|
2
|
+
/* eslint-disable @typescript-eslint/dot-notation */
|
|
3
|
+
/* eslint-disable no-continue */
|
|
4
|
+
/* eslint-disable no-restricted-syntax */
|
|
5
|
+
import { ethers } from 'ethers';
|
|
6
|
+
import { UserKeys } from '../../data-structures/crypto-keys/keys';
|
|
7
|
+
import sodium from 'libsodium-wrappers';
|
|
8
|
+
import { poseidonHash } from '../../crypto';
|
|
9
|
+
|
|
10
|
+
interface StakeDecryptOutput {
|
|
11
|
+
stakeBlinding: bigint;
|
|
12
|
+
stakedTokenRecipient: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const encryptStake = (stakeBlinding: bigint, stakedTokenRecipient: string, encryptionKey: string) => {
|
|
16
|
+
const dataToEncrypt = ethers.utils.defaultAbiCoder.encode(
|
|
17
|
+
['uint256', 'address'],
|
|
18
|
+
[stakeBlinding, stakedTokenRecipient],
|
|
19
|
+
);
|
|
20
|
+
const encryptedData = sodium.crypto_box_seal(dataToEncrypt, ethers.utils.arrayify(encryptionKey)); // (data,key)
|
|
21
|
+
const encryptedString = `0x${Buffer.from(encryptedData).toString('hex')}`;
|
|
22
|
+
return encryptedString;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const decryptStake = (
|
|
26
|
+
encryptedStakeCommitmentContent: string,
|
|
27
|
+
minterKeys: UserKeys,
|
|
28
|
+
): StakeDecryptOutput | undefined => {
|
|
29
|
+
const bufferData = Buffer.from(encryptedStakeCommitmentContent.slice(2), 'hex');
|
|
30
|
+
const { publicKey, privateKey } = UserKeys.getEncryptionKeyPair(minterKeys.getShieldedPrivateKey());
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const rawData = sodium.crypto_box_seal_open(
|
|
34
|
+
bufferData,
|
|
35
|
+
ethers.utils.arrayify(publicKey),
|
|
36
|
+
ethers.utils.arrayify(privateKey),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const decryptedAbiEncodedData = Buffer.from(rawData).toString('utf-8');
|
|
40
|
+
const [stakeBlinding, stakedTokenRecipient] = ethers.utils.defaultAbiCoder.decode(
|
|
41
|
+
['uint256', 'address'],
|
|
42
|
+
decryptedAbiEncodedData,
|
|
43
|
+
);
|
|
44
|
+
return { stakeBlinding: stakeBlinding.toBigInt(), stakedTokenRecipient };
|
|
45
|
+
} catch {
|
|
46
|
+
console.log('failed to decrypt');
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const createStakeCommitment = (
|
|
52
|
+
isStake: boolean,
|
|
53
|
+
stakedTokenRecipient: string,
|
|
54
|
+
stakeBlinding: bigint,
|
|
55
|
+
tokenAddress: string,
|
|
56
|
+
amountChange: bigint,
|
|
57
|
+
timeStamp: bigint,
|
|
58
|
+
) => {
|
|
59
|
+
return BigInt(
|
|
60
|
+
poseidonHash(...[isStake, stakedTokenRecipient, stakeBlinding, tokenAddress, amountChange, timeStamp].map(BigInt)),
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const calculateStakeNullifier = (stakeCommitment: bigint, blinding: bigint) => {
|
|
65
|
+
return poseidonHash(stakeCommitment, blinding);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const encodeHStakeMetadata = (
|
|
69
|
+
action: 1 | 0,
|
|
70
|
+
stakeCommitment: bigint,
|
|
71
|
+
depositorAddress: string,
|
|
72
|
+
encryptedContent: string,
|
|
73
|
+
encryptedContentAlt: string,
|
|
74
|
+
) =>
|
|
75
|
+
ethers.utils.defaultAbiCoder.encode(
|
|
76
|
+
['uint8', 'uint256', 'address', 'bytes', 'bytes'],
|
|
77
|
+
[action, stakeCommitment, depositorAddress, encryptedContent, encryptedContentAlt],
|
|
78
|
+
);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { StealthAddressStructure } from '../../types/circom-data.types';
|
|
3
|
+
|
|
4
|
+
export const isValidPrivateAddress = (address: string): boolean => {
|
|
5
|
+
const [randomization, stealthAddress, encryptionKey] = address.split(',');
|
|
6
|
+
|
|
7
|
+
const missingVariable = !randomization || !stealthAddress || !encryptionKey;
|
|
8
|
+
const incorrectAddressFormat = stealthAddress?.substring(0, 2) !== '0x' || encryptionKey?.substring(0, 2) !== '0x';
|
|
9
|
+
const incorrectLength = encryptionKey?.length !== 66 || stealthAddress?.length > 66 || stealthAddress?.length < 64;
|
|
10
|
+
const incorrectSymbols = address?.includes('"');
|
|
11
|
+
|
|
12
|
+
return !(missingVariable || incorrectAddressFormat || incorrectLength || incorrectSymbols);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const isValidExtendedPrivateAddress = (address: string): boolean => {
|
|
16
|
+
const [randomization, stealthAddress, H0, H1] = address.split(',');
|
|
17
|
+
const missingVariable = !stealthAddress || !randomization || !H0 || !H1;
|
|
18
|
+
const incorrectAddressFormat = stealthAddress?.substring(0, 2) !== '0x';
|
|
19
|
+
const incorrectLength = stealthAddress?.length > 66 || stealthAddress?.length < 64;
|
|
20
|
+
const incorrectSymbols = address?.includes('"');
|
|
21
|
+
|
|
22
|
+
return !(missingVariable || incorrectAddressFormat || incorrectLength || incorrectSymbols);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const constructStealthAddressStructure = (recipientInfo: string): StealthAddressStructure => {
|
|
26
|
+
const [randomization, stealthAddress, H0, H1] = recipientInfo.split(',');
|
|
27
|
+
return {
|
|
28
|
+
extraRandomization: BigInt(randomization),
|
|
29
|
+
stealthAddress: BigInt(stealthAddress),
|
|
30
|
+
H0: BigInt(H0),
|
|
31
|
+
H1: BigInt(H1),
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const hashEthereumAddress = (ethereumAddress: string): string => {
|
|
36
|
+
return ethers.utils.keccak256(ethers.utils.toUtf8Bytes(ethereumAddress.toLowerCase()));
|
|
37
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { toCommaSeparatedNumberString, trimLeadingZeros } from './amounts.utils';
|
|
2
|
+
|
|
3
|
+
describe('toCommaSeparatedNumberString', () => {
|
|
4
|
+
it('should format number string with commas', () => {
|
|
5
|
+
const numberString = '1234567890.1234567890';
|
|
6
|
+
const result = toCommaSeparatedNumberString(numberString);
|
|
7
|
+
expect(result).toEqual('1,234,567,890.1234567890');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('should format integer part with commas', () => {
|
|
11
|
+
const numberString = '9876543210';
|
|
12
|
+
const result = toCommaSeparatedNumberString(numberString);
|
|
13
|
+
expect(result).toEqual('9,876,543,210');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should handle number string without decimal part', () => {
|
|
17
|
+
const numberString = '1234567890';
|
|
18
|
+
const result = toCommaSeparatedNumberString(numberString);
|
|
19
|
+
expect(result).toEqual('1,234,567,890');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('should handle number string with empty decimal part', () => {
|
|
23
|
+
const numberString = '1234567890.';
|
|
24
|
+
const result = toCommaSeparatedNumberString(numberString);
|
|
25
|
+
expect(result).toEqual('1,234,567,890.');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('should handle number string with leading zeros', () => {
|
|
29
|
+
const numberString = '0000123456.789000';
|
|
30
|
+
const result = toCommaSeparatedNumberString(numberString);
|
|
31
|
+
expect(result).toEqual('123,456.789000');
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('toCommaSeparatedNumberString', () => {
|
|
36
|
+
it('should format number string with commas', () => {
|
|
37
|
+
const numberString = '1234567890.1234567890';
|
|
38
|
+
const result = toCommaSeparatedNumberString(numberString);
|
|
39
|
+
expect(result).toEqual('1,234,567,890.1234567890');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should format integer part with commas', () => {
|
|
43
|
+
const numberString = '9876543210';
|
|
44
|
+
const result = toCommaSeparatedNumberString(numberString);
|
|
45
|
+
expect(result).toEqual('9,876,543,210');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should handle number string without decimal part', () => {
|
|
49
|
+
const numberString = '1234567890';
|
|
50
|
+
const result = toCommaSeparatedNumberString(numberString);
|
|
51
|
+
expect(result).toEqual('1,234,567,890');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should handle number string with empty decimal part', () => {
|
|
55
|
+
const numberString = '1234567890.';
|
|
56
|
+
const result = toCommaSeparatedNumberString(numberString);
|
|
57
|
+
expect(result).toEqual('1,234,567,890.');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should handle number string with leading zeros', () => {
|
|
61
|
+
const numberString = '0000123456.789000';
|
|
62
|
+
const result = toCommaSeparatedNumberString(numberString);
|
|
63
|
+
expect(result).toEqual('123,456.789000');
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe('trimLeadingZeros', () => {
|
|
68
|
+
it('should trim repeating zeros to single zero', () => {
|
|
69
|
+
const numberString = '0000000';
|
|
70
|
+
const result = trimLeadingZeros(numberString);
|
|
71
|
+
expect(result).toEqual('0');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should keep one leading zero before decimal point', () => {
|
|
75
|
+
const numberString = '0.123';
|
|
76
|
+
const result = trimLeadingZeros(numberString);
|
|
77
|
+
expect(result).toEqual('0.123');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('should trim leading zeros before integer part', () => {
|
|
81
|
+
const numberString = '0000123456.789000';
|
|
82
|
+
const result = trimLeadingZeros(numberString);
|
|
83
|
+
expect(result).toEqual('123456.789000');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should not trim leading zeros if there is no integer part', () => {
|
|
87
|
+
const numberString = '0.789000';
|
|
88
|
+
const result = trimLeadingZeros(numberString);
|
|
89
|
+
expect(result).toEqual('0.789000');
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { type Utxo } from '../../data-structures/utxo/Utxo';
|
|
2
|
+
import { BigIntable } from '../../types/big-intable.types';
|
|
3
|
+
|
|
4
|
+
export const calculateSum = (arr: number[]) => arr.reduce((prev, item) => prev + item, 0);
|
|
5
|
+
export const calculateBigIntSum = (arr: bigint[]) => arr.reduce((prev, item) => prev + item, 0n);
|
|
6
|
+
|
|
7
|
+
export const beepsToPercentage = (beeps: number) => beeps / 100;
|
|
8
|
+
export const calculateAmountUsingBeeps = (beep: bigint, amount: bigint) => (amount * beep) / 10000n;
|
|
9
|
+
|
|
10
|
+
export function toBigInt(value: BigIntable): bigint {
|
|
11
|
+
if (typeof value === 'object' && 'toBigInt' in value) {
|
|
12
|
+
return value.toBigInt();
|
|
13
|
+
}
|
|
14
|
+
return BigInt(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Converts the value to a BigInt or returns undefined if can't */
|
|
18
|
+
export const toBigIntOrUndefined = (value: any) => {
|
|
19
|
+
try {
|
|
20
|
+
return BigInt(value);
|
|
21
|
+
} catch {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** Converts the value to a number or returns undefined if can't */
|
|
27
|
+
export const toNumberOrUndefined = (value: any): number | undefined => {
|
|
28
|
+
const num = Number(value);
|
|
29
|
+
return Number.isNaN(num) ? undefined : num;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const toInt = (param: string | undefined, defaultValue: number): number =>
|
|
33
|
+
param ? parseInt(param, 10) : defaultValue;
|
|
34
|
+
|
|
35
|
+
export function bigintApplySugar() {
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
37
|
+
// @ts-ignore
|
|
38
|
+
// To support serialization through JSON.stringify. Do not call in libraries!
|
|
39
|
+
BigInt.prototype.toJSON = function toJSON() {
|
|
40
|
+
return this.toString();
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function bigintMax(...values: bigint[]): bigint {
|
|
45
|
+
return values.reduce((max, value) => (value > max ? value : max));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// here + before Number convers string to number and cuts unused 0s at the end
|
|
49
|
+
export function fixDecimalsAmount(n: number) {
|
|
50
|
+
if (n === 0) {
|
|
51
|
+
return 0;
|
|
52
|
+
}
|
|
53
|
+
return n < 0.00000099999 ? '< 0.000001' : +Number(n).toFixed(6);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const getValueFirstNDigit = (num: number, numberOfDigits: number): string => {
|
|
57
|
+
const numStr = num.toFixed(20); // if num = 0.03232e-17 -> it will transform to 0.00...03232
|
|
58
|
+
|
|
59
|
+
const indexOfDecimal = numStr.indexOf('.');
|
|
60
|
+
// If there's no decimal point, return the original number (or handle this case as you need)
|
|
61
|
+
if (indexOfDecimal === -1) return numStr;
|
|
62
|
+
|
|
63
|
+
// Determine the end position for substring, which is start position + n digits after decimal
|
|
64
|
+
const endPosition = indexOfDecimal + numberOfDigits + 1;
|
|
65
|
+
|
|
66
|
+
// Get the string from the start to the specified end position
|
|
67
|
+
const resultStr = numStr.substring(0, endPosition);
|
|
68
|
+
|
|
69
|
+
return resultStr;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const trimLeadingZeros = (numberString: string): string => {
|
|
73
|
+
// String of repeating 0's (e.g. '0000000') should be replaced with '0', not empty string
|
|
74
|
+
if (/^0+$/.test(numberString)) {
|
|
75
|
+
return '0';
|
|
76
|
+
}
|
|
77
|
+
// One leading zero before decimal point is valid
|
|
78
|
+
if (/^0\.\d+$/.test(numberString)) {
|
|
79
|
+
return numberString;
|
|
80
|
+
}
|
|
81
|
+
const trimmed = numberString.replace(/^0+/, '');
|
|
82
|
+
return trimmed.startsWith('.') ? `0${trimmed}` : trimmed;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const toCommaSeparatedNumberString = (numberString: string): string => {
|
|
86
|
+
const parts = numberString.split('.');
|
|
87
|
+
const integerPart = parts[0];
|
|
88
|
+
// Only add the . and decimal part if exist
|
|
89
|
+
const decimalPart = parts.length > 1 ? `.${parts[1]}` : '';
|
|
90
|
+
|
|
91
|
+
// Use a regular expression to format the integer part with commas
|
|
92
|
+
const formattedIntegerPart = trimLeadingZeros(integerPart).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
93
|
+
|
|
94
|
+
return `${formattedIntegerPart}${decimalPart}`;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const truncateToDecimalPlaces = (numStr: string, decimalPlaces = 18): string => {
|
|
98
|
+
const match = numStr.match(/^(\d+)(\.(\d+))?$/);
|
|
99
|
+
if (match) {
|
|
100
|
+
const integerPart = match[1];
|
|
101
|
+
const decimalPart = match[3]?.slice(0, decimalPlaces) || '';
|
|
102
|
+
return decimalPart ? `${integerPart}.${decimalPart}` : integerPart;
|
|
103
|
+
}
|
|
104
|
+
return numStr;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/** Return absolute value of BigInt */
|
|
108
|
+
export const absBigInt = (n: bigint) => (n < 0n ? -n : n);
|
|
109
|
+
|
|
110
|
+
/** Return the smallest values of the 2 or 3 values provided */
|
|
111
|
+
export const minBigInt = (a: bigint, b: bigint, c?: bigint) => {
|
|
112
|
+
const minOne = a < b ? a : b;
|
|
113
|
+
if (!c) return minOne;
|
|
114
|
+
return minOne < c ? minOne : c;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export const maxBigInt = (a: bigint, b: bigint) => (a > b ? a : b);
|
|
118
|
+
|
|
119
|
+
export const countTotalAmountInUtxos = (utxos: Utxo[]) => utxos.reduce((prev, { amount }) => prev + amount, 0n);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type HinkalConfig } from '../../types/hinkal.types';
|
|
2
|
+
import { isNode } from '../../constants';
|
|
3
|
+
import { FileCacheDevice, LocalStorageCacheDevice } from '../../data-structures/cacheDevices';
|
|
4
|
+
|
|
5
|
+
export const createCacheDevice = (hinkalConfig?: HinkalConfig) => {
|
|
6
|
+
if (isNode) {
|
|
7
|
+
let useFileCache: boolean | undefined;
|
|
8
|
+
let cacheFilePath: string | undefined;
|
|
9
|
+
|
|
10
|
+
if (hinkalConfig) {
|
|
11
|
+
({ useFileCache, cacheFilePath } = hinkalConfig);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
|
|
15
|
+
const path = require('path');
|
|
16
|
+
const cachePath: string = path.resolve(process.cwd(), 'hinkalCache.json');
|
|
17
|
+
return new FileCacheDevice(useFileCache, cacheFilePath ?? cachePath);
|
|
18
|
+
} else {
|
|
19
|
+
return new LocalStorageCacheDevice(hinkalConfig?.disableCaching, hinkalConfig?.serializedCache);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { type IHinkal } from '../../data-structures/Hinkal/IHinkal';
|
|
2
|
+
import { ContractType } from '../../types/ethereum-network.types';
|
|
3
|
+
import { type DecodedTx } from '../../types/transactions.types';
|
|
4
|
+
import { type EncryptedOutputWithSign } from '../../types/commitments.types';
|
|
5
|
+
import { deserializeDecodedTxs, serializeDecodedTxs } from './getDataFromTransaction';
|
|
6
|
+
import { get, set } from 'idb-keyval';
|
|
7
|
+
|
|
8
|
+
// CACHE FOR ENCRYPTED OUTPUTS
|
|
9
|
+
|
|
10
|
+
export interface HinkalCacheInterface {
|
|
11
|
+
encryptedOutputs: EncryptedOutputWithSign[];
|
|
12
|
+
lastOutput: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const emptyHinkalCache: HinkalCacheInterface = {
|
|
16
|
+
encryptedOutputs: [],
|
|
17
|
+
lastOutput: '',
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const getShortPublicKeyAndHinkalAddress = (shieldedPublicKey: string, hinkalAddress: string) => {
|
|
21
|
+
const shortPublicKey = shieldedPublicKey.substring(0, 25);
|
|
22
|
+
const shortHinkalAddress = hinkalAddress.substring(0, 25);
|
|
23
|
+
return { shortPublicKey, shortHinkalAddress };
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const getFilePath = (chainId: number, shortPublicKey: string, shortHinkalAddress: string) => {
|
|
27
|
+
return `hinkalCache-${chainId}-${shortPublicKey}-${shortHinkalAddress}`;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const getHinkalCache = (hinkal: IHinkal, chainId: number, shieldedPublicKey: string): HinkalCacheInterface => {
|
|
31
|
+
const hinkalAddress = hinkal.getContract(ContractType.HinkalContract).address;
|
|
32
|
+
if (!chainId || !shieldedPublicKey || !hinkalAddress) throw Error('GetHinkalCache: incorrect arguments');
|
|
33
|
+
|
|
34
|
+
const { shortPublicKey, shortHinkalAddress } = getShortPublicKeyAndHinkalAddress(shieldedPublicKey, hinkalAddress);
|
|
35
|
+
|
|
36
|
+
return JSON.parse(
|
|
37
|
+
hinkal.cacheDevice.get(getFilePath(chainId, shortPublicKey, shortHinkalAddress)) ??
|
|
38
|
+
JSON.stringify(emptyHinkalCache),
|
|
39
|
+
);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const setHinkalCache = (
|
|
43
|
+
hinkalCache: HinkalCacheInterface,
|
|
44
|
+
hinkal: IHinkal,
|
|
45
|
+
chainId: number,
|
|
46
|
+
shieldedPublicKey: string,
|
|
47
|
+
) => {
|
|
48
|
+
const hinkalAddress = hinkal.getContract(ContractType.HinkalContract).address;
|
|
49
|
+
if (!chainId || !shieldedPublicKey || !hinkalAddress) throw Error('SetHinkalCache: incorrect arguments');
|
|
50
|
+
|
|
51
|
+
const { shortPublicKey, shortHinkalAddress } = getShortPublicKeyAndHinkalAddress(shieldedPublicKey, hinkalAddress);
|
|
52
|
+
|
|
53
|
+
hinkal.cacheDevice.set(getFilePath(chainId, shortPublicKey, shortHinkalAddress), JSON.stringify(hinkalCache));
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const resetCache = (hinkal: IHinkal, chainId: number, shieldedPublicKey: string) => {
|
|
57
|
+
setHinkalCache(emptyHinkalCache, hinkal, chainId, shieldedPublicKey);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// CACHE FOR TRANSACTIONS
|
|
61
|
+
|
|
62
|
+
const constructTxsCacheKey = (chainId: number, shortPublicKey: string, shortHinkalAddress: string) => {
|
|
63
|
+
return `txsCache-${chainId}-${shortPublicKey}-${shortHinkalAddress}`;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const saveTxsCache = async (
|
|
67
|
+
txs: DecodedTx[],
|
|
68
|
+
chainId: number,
|
|
69
|
+
shieldedPublicKey: string,
|
|
70
|
+
hinkalAddress: string,
|
|
71
|
+
lastHash: string,
|
|
72
|
+
hinkal: IHinkal,
|
|
73
|
+
) => {
|
|
74
|
+
if (!chainId || !shieldedPublicKey || !hinkalAddress) throw Error('saveTxsCache: incorrect arguments');
|
|
75
|
+
const { shortPublicKey, shortHinkalAddress } = getShortPublicKeyAndHinkalAddress(shieldedPublicKey, hinkalAddress);
|
|
76
|
+
const serializedTxs = serializeDecodedTxs(txs);
|
|
77
|
+
const key = constructTxsCacheKey(chainId, shortPublicKey, shortHinkalAddress);
|
|
78
|
+
const stringContent = JSON.stringify({ serializedTxs, lastHash });
|
|
79
|
+
try {
|
|
80
|
+
await set(key, stringContent);
|
|
81
|
+
} catch {
|
|
82
|
+
// if indexeddb not available fall back to localstorage
|
|
83
|
+
hinkal.cacheDevice.set(key, stringContent);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const emptyCacheString = { serializedTxs: [], lastHash: '' }; // if we use undefined instead of '', then we will not be able to get it after stringify and parse
|
|
88
|
+
|
|
89
|
+
export const loadTxsCache = async (
|
|
90
|
+
chainId: number,
|
|
91
|
+
shieldedPublicKey: string,
|
|
92
|
+
hinkalAddress: string,
|
|
93
|
+
hinkal: IHinkal,
|
|
94
|
+
) => {
|
|
95
|
+
if (!chainId || !shieldedPublicKey || !hinkalAddress) throw Error('loadTxsCache: incorrect arguments');
|
|
96
|
+
const { shortPublicKey, shortHinkalAddress } = getShortPublicKeyAndHinkalAddress(shieldedPublicKey, hinkalAddress);
|
|
97
|
+
const key = constructTxsCacheKey(chainId, shortPublicKey, shortHinkalAddress);
|
|
98
|
+
let stringData: string | null | undefined;
|
|
99
|
+
try {
|
|
100
|
+
const stringDataIndexedDB = await get<string>(key);
|
|
101
|
+
if (!stringDataIndexedDB) throw new Error('Empty indexedDB cache');
|
|
102
|
+
stringData = stringDataIndexedDB;
|
|
103
|
+
} catch {
|
|
104
|
+
stringData = hinkal.cacheDevice.get(key);
|
|
105
|
+
}
|
|
106
|
+
const { serializedTxs, lastHash } = JSON.parse(stringData ?? JSON.stringify(emptyCacheString)) as {
|
|
107
|
+
serializedTxs: DecodedTx<string, string>[];
|
|
108
|
+
lastHash: string;
|
|
109
|
+
};
|
|
110
|
+
const decodedTxs = serializedTxs.map((tx) => deserializeDecodedTxs(tx));
|
|
111
|
+
return { decodedTxs, lastHash: lastHash === '' ? undefined : lastHash }; // we want to have undefined value instead of empty string '', since server does not like "/" at the end of the request
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// CACHE FOR InHinkalAddresses
|
|
115
|
+
export const getInHinkalAddressesFilePath = (chainId: number, shortPublicKey: string, shortHinkalAddress: string) => {
|
|
116
|
+
return `hinkalApprovalsCache-${chainId}-${shortPublicKey}-${shortHinkalAddress}`;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export const setInHinkalAddressesCache = (
|
|
120
|
+
hinkalCache: Record<string, string>,
|
|
121
|
+
hinkal: IHinkal,
|
|
122
|
+
chainId: number,
|
|
123
|
+
shieldedPublicKey: string,
|
|
124
|
+
) => {
|
|
125
|
+
const hinkalAddress = hinkal.getContract(ContractType.HinkalContract, undefined, chainId).address;
|
|
126
|
+
if (!chainId || !shieldedPublicKey || !hinkalAddress) throw Error('SetHinkalCache: incorrect arguments');
|
|
127
|
+
|
|
128
|
+
const { shortPublicKey, shortHinkalAddress } = getShortPublicKeyAndHinkalAddress(shieldedPublicKey, hinkalAddress);
|
|
129
|
+
|
|
130
|
+
hinkal.cacheDevice.set(
|
|
131
|
+
getInHinkalAddressesFilePath(chainId, shortPublicKey, shortHinkalAddress),
|
|
132
|
+
JSON.stringify(hinkalCache),
|
|
133
|
+
);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export const getInHinkalAddressesCache = (
|
|
137
|
+
hinkal: IHinkal,
|
|
138
|
+
chainId: number,
|
|
139
|
+
shieldedPublicKey: string,
|
|
140
|
+
): Record<string, string> => {
|
|
141
|
+
const hinkalAddress = hinkal.getContract(ContractType.HinkalContract, undefined, chainId).address;
|
|
142
|
+
if (!chainId || !shieldedPublicKey || !hinkalAddress) throw Error('GetHinkalCache: incorrect arguments');
|
|
143
|
+
|
|
144
|
+
const { shortPublicKey, shortHinkalAddress } = getShortPublicKeyAndHinkalAddress(shieldedPublicKey, hinkalAddress);
|
|
145
|
+
|
|
146
|
+
return JSON.parse(
|
|
147
|
+
hinkal.cacheDevice.get(getInHinkalAddressesFilePath(chainId, shortPublicKey, shortHinkalAddress)) ??
|
|
148
|
+
JSON.stringify(emptyHinkalCache),
|
|
149
|
+
) as Record<string, string>;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export const resetInHinkalAddressesCache = (hinkal: IHinkal, chainId: number, shieldedPublicKey: string) => {
|
|
153
|
+
setInHinkalAddressesCache({}, hinkal, chainId, shieldedPublicKey);
|
|
154
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const caseInsensitiveEqual = (a?: string | null, b?: string | null) => {
|
|
2
|
+
if (a === undefined || b === undefined || a === null || b === null) return false;
|
|
3
|
+
return a.toLowerCase() === b.toLowerCase();
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export const lowerCaseIncludes = (array: string[], address?: string) =>
|
|
7
|
+
array.some((item) => caseInsensitiveEqual(item, address));
|
|
8
|
+
|
|
9
|
+
export const lowerCaseStartsWith = (array: string[], s: string) =>
|
|
10
|
+
array.some((item) => item.toLowerCase().startsWith(s.toLowerCase()));
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CallInfo } from '../../types/API.types';
|
|
2
|
+
import { ethers } from 'ethers';
|
|
3
|
+
import { networkRegistry } from '../../constants/chains.constants';
|
|
4
|
+
|
|
5
|
+
export const convertEmporiumOpToCallInfo = (op: string, walletAddress: string, chainId: number): CallInfo => {
|
|
6
|
+
const { hinkalAddress } = networkRegistry[chainId].contractData;
|
|
7
|
+
|
|
8
|
+
const opBytes = ethers.utils.arrayify(op);
|
|
9
|
+
const toBytes = opBytes.slice(0, 20);
|
|
10
|
+
const invokeWalletBytes = opBytes.slice(20, 21);
|
|
11
|
+
const valueBytes = opBytes.slice(21, 37);
|
|
12
|
+
const calldataBytes = opBytes.slice(37);
|
|
13
|
+
|
|
14
|
+
const [to, value, invokeWallet, calldata] = [toBytes, valueBytes, invokeWalletBytes, calldataBytes].map((bytes) =>
|
|
15
|
+
ethers.utils.hexlify(bytes),
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
return { from: BigInt(invokeWallet) > 0 ? walletAddress : hinkalAddress, to, value: BigInt(value), calldata };
|
|
19
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { IntegrationProvider } from '../../types/hinkal.types';
|
|
2
|
+
import { ExternalActionId } from '../../types/external-action.types';
|
|
3
|
+
|
|
4
|
+
const conversionTable: Record<IntegrationProvider, ExternalActionId | undefined> = {
|
|
5
|
+
[IntegrationProvider.UNISWAP]: ExternalActionId.Uniswap,
|
|
6
|
+
[IntegrationProvider.ODOS]: ExternalActionId.Odos,
|
|
7
|
+
[IntegrationProvider.ONEINCH]: ExternalActionId.OneInch,
|
|
8
|
+
[IntegrationProvider.LIFI]: ExternalActionId.Lifi,
|
|
9
|
+
[IntegrationProvider.ALL]: undefined,
|
|
10
|
+
[IntegrationProvider.NONE]: undefined,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const convertIntegrationProviderToExternalActionId = (integrationProvider: IntegrationProvider) =>
|
|
14
|
+
conversionTable[integrationProvider];
|