@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,118 @@
|
|
|
1
|
+
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { TorClient } from './TorClient';
|
|
3
|
+
import { createTorAdapter } from './TorAdapter';
|
|
4
|
+
import { Logger } from '../../error-handling/logger';
|
|
5
|
+
import { HttpMethod } from '../../types';
|
|
6
|
+
|
|
7
|
+
class HttpClient {
|
|
8
|
+
private torClient?: TorClient;
|
|
9
|
+
|
|
10
|
+
private torAxiosInstance?: AxiosInstance;
|
|
11
|
+
|
|
12
|
+
private regularAxiosInstance: AxiosInstance;
|
|
13
|
+
|
|
14
|
+
private useTor = false;
|
|
15
|
+
|
|
16
|
+
constructor() {
|
|
17
|
+
this.regularAxiosInstance = axios.create();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private initializeTorClient = async (): Promise<void> => {
|
|
21
|
+
if (this.torClient && this.torAxiosInstance) return;
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
this.torClient = await TorClient.create();
|
|
25
|
+
|
|
26
|
+
this.torAxiosInstance = axios.create({
|
|
27
|
+
adapter: createTorAdapter(this.torClient),
|
|
28
|
+
});
|
|
29
|
+
} catch (error) {
|
|
30
|
+
Logger.error('Failed to initialize Tor client', error);
|
|
31
|
+
this.torClient = undefined;
|
|
32
|
+
this.torAxiosInstance = undefined;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
setUseTor = async (enabled: boolean): Promise<void> => {
|
|
37
|
+
this.useTor = enabled;
|
|
38
|
+
if (enabled) await this.initializeTorClient();
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
getUseTor = (): boolean => {
|
|
42
|
+
return this.useTor;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
private buildRequestConfig = (
|
|
46
|
+
method: HttpMethod,
|
|
47
|
+
url: string,
|
|
48
|
+
dataOrConfig?: any,
|
|
49
|
+
config?: AxiosRequestConfig,
|
|
50
|
+
): AxiosRequestConfig => {
|
|
51
|
+
if (method === HttpMethod.GET || method === HttpMethod.DELETE) {
|
|
52
|
+
return { ...dataOrConfig, method, url };
|
|
53
|
+
}
|
|
54
|
+
return { ...config, method, url, data: dataOrConfig };
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
private request = async <T = any>(
|
|
58
|
+
method: HttpMethod,
|
|
59
|
+
url: string,
|
|
60
|
+
dataOrConfig?: any,
|
|
61
|
+
config?: AxiosRequestConfig,
|
|
62
|
+
): Promise<T> => {
|
|
63
|
+
const requestConfig = this.buildRequestConfig(method, url, dataOrConfig, config);
|
|
64
|
+
|
|
65
|
+
if (this.useTor) {
|
|
66
|
+
await this.initializeTorClient();
|
|
67
|
+
|
|
68
|
+
if (this.torAxiosInstance) {
|
|
69
|
+
try {
|
|
70
|
+
const torRequestConfig = { ...requestConfig, timeout: 60 * 1000 };
|
|
71
|
+
const response = await this.torAxiosInstance.request<T>(torRequestConfig);
|
|
72
|
+
return response.data;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
// Fallback if Tor returns 4xx/5xx errors (CloudFront blocks Tor, etc.)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Regular axios
|
|
80
|
+
const response = await this.regularAxiosInstance.request<T>(requestConfig);
|
|
81
|
+
return response.data;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
get = <T = any>(url: string, config?: AxiosRequestConfig): Promise<T> => {
|
|
85
|
+
return this.request<T>(HttpMethod.GET, url, config);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
post = <T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> => {
|
|
89
|
+
return this.request<T>(HttpMethod.POST, url, data, config);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
put = <T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> => {
|
|
93
|
+
return this.request<T>(HttpMethod.PUT, url, data, config);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
patch = <T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> => {
|
|
97
|
+
return this.request<T>(HttpMethod.PATCH, url, data, config);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
delete = <T = any>(url: string, config?: AxiosRequestConfig): Promise<T> => {
|
|
101
|
+
return this.request<T>(HttpMethod.DELETE, url, config);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
closeTorClient = async (): Promise<void> => {
|
|
105
|
+
if (this.torClient) {
|
|
106
|
+
await this.torClient.close();
|
|
107
|
+
this.torClient = undefined;
|
|
108
|
+
this.torAxiosInstance = undefined;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
resetTorClient = (): void => {
|
|
113
|
+
this.torClient = undefined;
|
|
114
|
+
this.torAxiosInstance = undefined;
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const httpClient = new HttpClient();
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { AxiosAdapter, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
|
2
|
+
import { TorClient } from './TorClient';
|
|
3
|
+
import { HttpMethod } from '../../types';
|
|
4
|
+
import { CustomJSONParse } from '../../functions/utils/serialize.utils';
|
|
5
|
+
|
|
6
|
+
export const createTorAdapter = (torClient: TorClient): AxiosAdapter => {
|
|
7
|
+
return async (config: InternalAxiosRequestConfig): Promise<AxiosResponse> => {
|
|
8
|
+
if (!config.url) {
|
|
9
|
+
throw new Error('URL is required');
|
|
10
|
+
}
|
|
11
|
+
const { url, data } = config;
|
|
12
|
+
const method = (config.method?.toUpperCase() || 'GET') as HttpMethod;
|
|
13
|
+
|
|
14
|
+
const headers: Record<string, string> = {};
|
|
15
|
+
if (config.headers) {
|
|
16
|
+
const headersObj = typeof config.headers.toJSON === 'function' ? config.headers.toJSON() : config.headers;
|
|
17
|
+
|
|
18
|
+
Object.entries(headersObj).forEach(([key, value]) => {
|
|
19
|
+
if (value !== undefined && value !== null) {
|
|
20
|
+
headers[key] = String(value);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
const response = await torClient.fetchViaTor(method, url, data, headers);
|
|
27
|
+
|
|
28
|
+
const responseText = await response.text();
|
|
29
|
+
|
|
30
|
+
let responseData = responseText;
|
|
31
|
+
const contentType = response.headers.get('content-type') || '';
|
|
32
|
+
if (contentType.includes('application/json') && responseText) {
|
|
33
|
+
try {
|
|
34
|
+
responseData = CustomJSONParse(responseText);
|
|
35
|
+
} catch {
|
|
36
|
+
// Keep as text if JSON parsing fails
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const responseHeaders: Record<string, string> = {};
|
|
41
|
+
response.headers.forEach((value, key) => {
|
|
42
|
+
responseHeaders[key] = value;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const axiosResponse: AxiosResponse = {
|
|
46
|
+
data: responseData,
|
|
47
|
+
status: response.status,
|
|
48
|
+
statusText: response.statusText,
|
|
49
|
+
headers: responseHeaders,
|
|
50
|
+
config,
|
|
51
|
+
request: undefined,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const validateStatus = config.validateStatus || ((status) => status >= 200 && status < 300);
|
|
55
|
+
if (!validateStatus(response.status)) {
|
|
56
|
+
const error: any = new Error(`Request failed with status code ${response.status}`);
|
|
57
|
+
error.config = config;
|
|
58
|
+
error.code = 'ERR_BAD_REQUEST';
|
|
59
|
+
error.request = undefined;
|
|
60
|
+
error.response = axiosResponse;
|
|
61
|
+
error.isAxiosError = true;
|
|
62
|
+
error.toJSON = () => ({
|
|
63
|
+
message: error.message,
|
|
64
|
+
name: error.name,
|
|
65
|
+
stack: error.stack,
|
|
66
|
+
config: error.config,
|
|
67
|
+
code: error.code,
|
|
68
|
+
status: error.response?.status,
|
|
69
|
+
});
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return axiosResponse;
|
|
74
|
+
} catch (error: any) {
|
|
75
|
+
if (error.isAxiosError) {
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const axiosError: any = new Error(error.message || 'Network Error');
|
|
80
|
+
axiosError.config = config;
|
|
81
|
+
axiosError.code = 'ERR_NETWORK';
|
|
82
|
+
axiosError.request = undefined;
|
|
83
|
+
axiosError.isAxiosError = true;
|
|
84
|
+
axiosError.toJSON = () => ({
|
|
85
|
+
message: axiosError.message,
|
|
86
|
+
name: axiosError.name,
|
|
87
|
+
stack: axiosError.stack,
|
|
88
|
+
config: axiosError.config,
|
|
89
|
+
code: axiosError.code,
|
|
90
|
+
});
|
|
91
|
+
throw axiosError;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
};
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { Circuit, Consensus, createSnowflakeStream, TorClientDuplex } from '@hazae41/echalote';
|
|
2
|
+
import { Ciphers, TlsClientDuplex } from '@hazae41/cadenas';
|
|
3
|
+
import { fetch, FetchParams } from '@hazae41/fleche';
|
|
4
|
+
import { Opaque, Writable } from '@hazae41/binary';
|
|
5
|
+
import { Ed25519 } from '@hazae41/ed25519';
|
|
6
|
+
import { X25519 } from '@hazae41/x25519';
|
|
7
|
+
import { ChaCha20Poly1305 } from '@hazae41/chacha20poly1305';
|
|
8
|
+
import { Sha1 } from '@hazae41/sha1';
|
|
9
|
+
import { WalletWasm } from '@brumewallet/wallet.wasm';
|
|
10
|
+
import { WebSocketDuplex } from './WebSocketDuplex';
|
|
11
|
+
import { Logger } from '../../error-handling/logger';
|
|
12
|
+
import { HttpMethod } from '../../types';
|
|
13
|
+
import { CustomJSONStringify } from '../../functions/utils/serialize.utils';
|
|
14
|
+
import { Buffer } from 'buffer';
|
|
15
|
+
|
|
16
|
+
if (typeof globalThis.Buffer === 'undefined') {
|
|
17
|
+
globalThis.Buffer = Buffer;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class TorClient {
|
|
21
|
+
private static initializationPromise?: Promise<TorClient>;
|
|
22
|
+
|
|
23
|
+
private cryptoInitialized = false;
|
|
24
|
+
|
|
25
|
+
private socket?: WebSocket;
|
|
26
|
+
|
|
27
|
+
private tor?: TorClientDuplex;
|
|
28
|
+
|
|
29
|
+
private consensus?: Consensus;
|
|
30
|
+
|
|
31
|
+
private abortController?: AbortController;
|
|
32
|
+
|
|
33
|
+
private middles: Consensus.Microdesc.Head[] = [];
|
|
34
|
+
|
|
35
|
+
private exits: Consensus.Microdesc.Head[] = [];
|
|
36
|
+
|
|
37
|
+
private circuits: Circuit[] = [];
|
|
38
|
+
|
|
39
|
+
private circuitIndex = 0;
|
|
40
|
+
|
|
41
|
+
private readonly CIRCUIT_POOL_SIZE = 5;
|
|
42
|
+
|
|
43
|
+
private readonly TOP_RELAY_COUNT = 50;
|
|
44
|
+
|
|
45
|
+
static async create(): Promise<TorClient> {
|
|
46
|
+
if (this.initializationPromise) {
|
|
47
|
+
return this.initializationPromise;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.initializationPromise = (async () => {
|
|
51
|
+
try {
|
|
52
|
+
const client = new TorClient();
|
|
53
|
+
await client.initialize();
|
|
54
|
+
return client;
|
|
55
|
+
} catch (error) {
|
|
56
|
+
this.initializationPromise = undefined;
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
})();
|
|
60
|
+
|
|
61
|
+
return this.initializationPromise;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private async initialize(): Promise<void> {
|
|
65
|
+
await this.initializeCryptoOrThrow();
|
|
66
|
+
this.socket = await this.createSnowflakeSocketOrThrow();
|
|
67
|
+
this.tor = await this.createTorClientOrThrow(this.socket);
|
|
68
|
+
await this.loadConsensus();
|
|
69
|
+
|
|
70
|
+
const buildPromises = Array(this.CIRCUIT_POOL_SIZE)
|
|
71
|
+
.fill(null)
|
|
72
|
+
.map((_, i) =>
|
|
73
|
+
this.createNewCircuit().catch((err) => {
|
|
74
|
+
Logger.error(`Failed to build initial circuit ${i}`, err);
|
|
75
|
+
return null;
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const circuits = await Promise.all(buildPromises);
|
|
80
|
+
this.circuits = circuits.filter((c): c is Circuit => c !== null);
|
|
81
|
+
|
|
82
|
+
if (this.circuits.length === 0) {
|
|
83
|
+
throw new Error('Failed to build any circuits');
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private async initializeCryptoOrThrow(): Promise<void> {
|
|
88
|
+
if (this.cryptoInitialized) return;
|
|
89
|
+
|
|
90
|
+
await WalletWasm.initBundled();
|
|
91
|
+
Sha1.set(Sha1.fromWasm(WalletWasm));
|
|
92
|
+
Ed25519.set(await Ed25519.fromNativeOrWasm(WalletWasm));
|
|
93
|
+
X25519.set(X25519.fromWasm(WalletWasm));
|
|
94
|
+
ChaCha20Poly1305.set(ChaCha20Poly1305.fromWasm(WalletWasm));
|
|
95
|
+
this.cryptoInitialized = true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private async createSnowflakeSocketOrThrow(): Promise<WebSocket> {
|
|
99
|
+
const socket = new WebSocket('wss://snowflake.torproject.net/');
|
|
100
|
+
socket.binaryType = 'arraybuffer';
|
|
101
|
+
|
|
102
|
+
await new Promise((resolve, reject) => {
|
|
103
|
+
socket.onopen = resolve;
|
|
104
|
+
socket.onerror = reject;
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return socket;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private async createTorClientOrThrow(socket: WebSocket): Promise<TorClientDuplex> {
|
|
111
|
+
const stream = new WebSocketDuplex(socket);
|
|
112
|
+
const tcp = createSnowflakeStream(stream);
|
|
113
|
+
const tor = new TorClientDuplex();
|
|
114
|
+
|
|
115
|
+
this.abortController = new AbortController();
|
|
116
|
+
|
|
117
|
+
tcp.outer.readable.pipeTo(tor.inner.writable, { signal: this.abortController.signal }).catch((error) => {
|
|
118
|
+
Logger.error('Pipe to Tor failed:', error);
|
|
119
|
+
});
|
|
120
|
+
tor.inner.readable.pipeTo(tcp.outer.writable, { signal: this.abortController.signal }).catch((error) => {
|
|
121
|
+
Logger.error('Pipe to TCP failed:', error);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
await tor.waitOrThrow();
|
|
125
|
+
return tor;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private async loadConsensus(): Promise<void> {
|
|
129
|
+
if (!this.tor) throw new Error('Tor client not initialized');
|
|
130
|
+
|
|
131
|
+
const tempCircuit = await this.tor.createOrThrow();
|
|
132
|
+
this.consensus = await Consensus.fetchOrThrow(tempCircuit);
|
|
133
|
+
|
|
134
|
+
this.middles = this.consensus.microdescs.filter(
|
|
135
|
+
(it) => it.flags.includes('Fast') && it.flags.includes('Stable') && it.flags.includes('V2Dir'),
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
this.middles.sort((a, b) => {
|
|
139
|
+
const bwA = Number(a.bandwidth?.['Bandwidth']) || 0;
|
|
140
|
+
const bwB = Number(b.bandwidth?.['Bandwidth']) || 0;
|
|
141
|
+
return bwB - bwA;
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
this.middles = this.middles.slice(0, this.TOP_RELAY_COUNT);
|
|
145
|
+
|
|
146
|
+
this.exits = this.consensus.microdescs.filter(
|
|
147
|
+
(it) =>
|
|
148
|
+
it.flags.includes('Fast') &&
|
|
149
|
+
it.flags.includes('Stable') &&
|
|
150
|
+
it.flags.includes('Exit') &&
|
|
151
|
+
!it.flags.includes('BadExit'),
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
this.exits.sort((a, b) => {
|
|
155
|
+
const bwA = Number(a.bandwidth?.['Bandwidth']) || 0;
|
|
156
|
+
const bwB = Number(b.bandwidth?.['Bandwidth']) || 0;
|
|
157
|
+
return bwB - bwA;
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
this.exits = this.exits.slice(0, this.TOP_RELAY_COUNT);
|
|
161
|
+
|
|
162
|
+
if (this.middles.length === 0 || this.exits.length === 0) {
|
|
163
|
+
throw new Error('No suitable relays found');
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private async createNewCircuit(): Promise<Circuit> {
|
|
168
|
+
if (!this.tor) throw new Error('Tor client not initialized');
|
|
169
|
+
|
|
170
|
+
const circuit = await this.tor.createOrThrow();
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
const middle = this.middles[Math.floor(Math.random() * this.middles.length)];
|
|
174
|
+
if (!middle) throw new Error('No middle relays available');
|
|
175
|
+
const middleDesc = await Consensus.Microdesc.fetchOrThrow(circuit, middle);
|
|
176
|
+
await circuit.extendOrThrow(middleDesc);
|
|
177
|
+
|
|
178
|
+
const exit = this.exits[Math.floor(Math.random() * this.exits.length)];
|
|
179
|
+
if (!exit) throw new Error('No exit relays available');
|
|
180
|
+
const exitDesc = await Consensus.Microdesc.fetchOrThrow(circuit, exit);
|
|
181
|
+
await circuit.extendOrThrow(exitDesc);
|
|
182
|
+
|
|
183
|
+
return circuit;
|
|
184
|
+
} catch (error) {
|
|
185
|
+
Logger.error('Failed to build circuit', error);
|
|
186
|
+
throw error;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private async openTlsConnectionOrThrow(
|
|
191
|
+
circuit: Circuit,
|
|
192
|
+
hostname: string,
|
|
193
|
+
port = 443,
|
|
194
|
+
): Promise<{ readable: ReadableStream<Opaque>; writable: WritableStream<Writable> }> {
|
|
195
|
+
const ttcp = await circuit.openOrThrow(hostname, port);
|
|
196
|
+
const ciphers = [Ciphers.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, Ciphers.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384];
|
|
197
|
+
const ttls = new TlsClientDuplex({ host_name: hostname, ciphers });
|
|
198
|
+
|
|
199
|
+
ttcp.outer.readable.pipeTo(ttls.inner.writable).catch(() => {});
|
|
200
|
+
ttls.inner.readable.pipeTo(ttcp.outer.writable).catch(() => {});
|
|
201
|
+
|
|
202
|
+
return ttls.outer;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async fetchViaTor(method: HttpMethod, url: string, body?: any, headers?: Record<string, string>): Promise<Response> {
|
|
206
|
+
if (this.circuits.length === 0) throw new Error('Circuit pool is empty, client may be closed or failed to init.');
|
|
207
|
+
|
|
208
|
+
const parsedUrl = new URL(url);
|
|
209
|
+
|
|
210
|
+
const totalCircuits = this.circuits.length;
|
|
211
|
+
let lastError: any;
|
|
212
|
+
|
|
213
|
+
for (let attempt = 0; attempt < totalCircuits; attempt += 1) {
|
|
214
|
+
const circuitIndex = (this.circuitIndex + attempt) % totalCircuits;
|
|
215
|
+
const circuit = this.circuits[circuitIndex];
|
|
216
|
+
if (circuit) {
|
|
217
|
+
try {
|
|
218
|
+
// eslint-disable-next-line no-await-in-loop
|
|
219
|
+
const tlsStream = await this.openTlsConnectionOrThrow(circuit, parsedUrl.hostname, 443);
|
|
220
|
+
|
|
221
|
+
const requestHeaders: Record<string, string> = {
|
|
222
|
+
Host: parsedUrl.hostname,
|
|
223
|
+
Connection: 'keep-alive',
|
|
224
|
+
...headers,
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const fetchOptions: RequestInit & FetchParams = {
|
|
228
|
+
method,
|
|
229
|
+
stream: tlsStream,
|
|
230
|
+
headers: requestHeaders,
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
if (body && method !== HttpMethod.GET) {
|
|
234
|
+
fetchOptions.body = typeof body === 'string' ? body : CustomJSONStringify(body);
|
|
235
|
+
if (!requestHeaders['Content-Type'] && !requestHeaders['content-type'])
|
|
236
|
+
requestHeaders['Content-Type'] = 'application/json';
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// eslint-disable-next-line no-await-in-loop
|
|
240
|
+
const response = await fetch(url, fetchOptions);
|
|
241
|
+
|
|
242
|
+
this.circuitIndex = circuitIndex;
|
|
243
|
+
return response;
|
|
244
|
+
} catch (error: any) {
|
|
245
|
+
lastError = error;
|
|
246
|
+
const msg = error.message?.toLowerCase() || '';
|
|
247
|
+
Logger.error(`[TorClient] Circuit ${circuitIndex} failed:`, msg);
|
|
248
|
+
|
|
249
|
+
// Background rebuild if it looks like a dead circuit
|
|
250
|
+
if (msg.includes('circuit') || msg.includes('destroyed') || msg.includes('closed')) {
|
|
251
|
+
this.createNewCircuit()
|
|
252
|
+
.then((newCircuit) => {
|
|
253
|
+
this.circuits[circuitIndex] = newCircuit;
|
|
254
|
+
Logger.log(`[TorClient] Replaced circuit ${circuitIndex}`);
|
|
255
|
+
})
|
|
256
|
+
.catch((err) => {
|
|
257
|
+
Logger.error(`[TorClient] Failed to replace circuit ${circuitIndex}`, err);
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// All circuits failed
|
|
265
|
+
throw new Error(`[TorClient] All ${totalCircuits} circuits failed. Last error: ${lastError?.message || 'unknown'}`);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async close(): Promise<void> {
|
|
269
|
+
if (this.abortController) this.abortController.abort();
|
|
270
|
+
|
|
271
|
+
if (this.socket && (this.socket.readyState === WebSocket.OPEN || this.socket.readyState === WebSocket.CONNECTING))
|
|
272
|
+
this.socket.close();
|
|
273
|
+
|
|
274
|
+
this.circuits = [];
|
|
275
|
+
|
|
276
|
+
this.socket = undefined;
|
|
277
|
+
this.tor = undefined;
|
|
278
|
+
this.consensus = undefined;
|
|
279
|
+
this.abortController = undefined;
|
|
280
|
+
|
|
281
|
+
TorClient.initializationPromise = undefined;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Opaque, Writable } from '@hazae41/binary';
|
|
2
|
+
import { HalfDuplex } from '@hazae41/cascade';
|
|
3
|
+
|
|
4
|
+
export class WebSocketDuplex {
|
|
5
|
+
readonly duplex: HalfDuplex<Opaque, Writable>;
|
|
6
|
+
|
|
7
|
+
constructor(socket: WebSocket) {
|
|
8
|
+
this.duplex = new HalfDuplex<Opaque, Writable>({
|
|
9
|
+
output: {
|
|
10
|
+
write(message) {
|
|
11
|
+
socket.send(Writable.writeToBytesOrThrow(message));
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
socket.addEventListener('close', () => this.duplex.close());
|
|
17
|
+
socket.addEventListener('error', (e) => this.duplex.error(e));
|
|
18
|
+
|
|
19
|
+
socket.addEventListener('message', async (e: MessageEvent<string | ArrayBuffer>) => {
|
|
20
|
+
if (typeof e.data === 'string') return;
|
|
21
|
+
|
|
22
|
+
const bytes = new Uint8Array(e.data);
|
|
23
|
+
const opaque = new Opaque(bytes);
|
|
24
|
+
|
|
25
|
+
this.duplex.input.enqueue(opaque);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
get outer() {
|
|
30
|
+
return this.duplex.outer;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { TokenBalance } from '../../../types';
|
|
2
|
+
|
|
3
|
+
export enum TransactionType {
|
|
4
|
+
DEPOSIT,
|
|
5
|
+
WITHDRAW,
|
|
6
|
+
SWAP,
|
|
7
|
+
UNKNOWN,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface TransactionDetails {
|
|
11
|
+
type: TransactionType;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type HistoryTokenBalance = TokenBalance | { token: { erc20TokenAddress: string }; balance: bigint };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './history/history.types';
|