UNPKG

@web5nexus/web3auth-core

Version:

Core Implementation for 1 click Web3 Social Auth Implementation for Ethereum, XDC, Bitcoin,Cosmos and other Altcoins.

122 lines 5.36 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const cosmoschains_1 = __importDefault(require("./cosmoschains")); const stargate_1 = require("@cosmjs/stargate"); const proto_signing_1 = require("@cosmjs/proto-signing"); class CosmosRpc { constructor(blockchain, network, provider) { this.blockchain = blockchain; this.hrp = this.getHrpValue(blockchain, network); this.provider = provider; this.rpcTarget = this.getRpcValue(blockchain, network); this.denom = this.getDenomValue(blockchain, network); } getHrpValue(blockchain, network) { blockchain = blockchain.toLowerCase(); network = network.toLowerCase(); const chain = cosmoschains_1.default.find(chain => chain.blockchain === blockchain && chain.network === network); if (chain) { return chain.hrp; } throw new Error(`Could not find hrp value for the specified blockchain (${blockchain}) and network (${network}) combination.`); } getRpcValue(blockchain, network) { blockchain = blockchain.toLowerCase(); network = network.toLowerCase(); const chain = cosmoschains_1.default.find(chain => chain.blockchain === blockchain && chain.network === network); if (chain) { return chain.rpcTarget; } throw new Error(`Could not find RPC Target value for the specified blockchain (${blockchain}) and network (${network}) combination.`); } getDenomValue(blockchain, network) { blockchain = blockchain.toLowerCase(); network = network.toLowerCase(); const chain = cosmoschains_1.default.find(chain => chain.blockchain === blockchain && chain.network === network); if (chain) { return chain.denom; } throw new Error(`Could not find Denom value for the specified blockchain (${blockchain}) and network (${network}) combination.`); } async getAccounts() { try { const privateKey = Buffer.from(await this.getPrivateKey(), "hex"); const walletPromise = await proto_signing_1.DirectSecp256k1Wallet.fromKey(privateKey, this.hrp); return (await walletPromise.getAccounts())[0].address; } catch (error) { console.error(error); throw error; } } async getBalance() { try { const client = await stargate_1.StargateClient.connect(this.rpcTarget); // Get user's Cosmos public address const privateKey = Buffer.from(await this.getPrivateKey(), "hex"); const walletPromise = await proto_signing_1.DirectSecp256k1Wallet.fromKey(privateKey, this.hrp); const address = (await walletPromise.getAccounts())[0].address; const balanceResponse = await client.getAllBalances(address); const balance = balanceResponse.find(b => b.denom === this.denom); return balance ? balance.amount : "0"; } catch (error) { return error; } } async getChainId() { try { const client = await stargate_1.StargateClient.connect(this.rpcTarget); // Get the connected Chain's ID const chainId = await client.getChainId(); return chainId.toString(); } catch (error) { return error; } } async getPrivateKey() { try { return await this.provider.request({ method: "eth_private_key", }); } catch (error) { console.error(error); throw error; } } async sendTransaction(amount, toAddress, gasFee, gasLimit) { try { await stargate_1.StargateClient.connect(this.rpcTarget); const privateKey = Buffer.from(await this.getPrivateKey(), "hex"); const walletPromise = await proto_signing_1.DirectSecp256k1Wallet.fromKey(privateKey, this.hrp); const fromAddress = (await walletPromise.getAccounts())[0].address; const destination = toAddress; const defaultGas = "100000"; const gasLimits = gasLimit !== undefined ? gasLimit : defaultGas; const defaultFee = "4000"; const gasFees = gasFee !== undefined ? gasFee : defaultFee; const getSignerFromKey = async () => { return proto_signing_1.DirectSecp256k1Wallet.fromKey(privateKey, this.hrp); }; const signer = await getSignerFromKey(); const signingClient = await stargate_1.SigningStargateClient.connectWithSigner(this.rpcTarget, signer); const result = await signingClient.sendTokens(fromAddress, destination, [{ denom: this.denom, amount: amount }], { amount: [{ denom: this.denom, amount: gasFees }], gas: gasLimits, }); const transactionHash = result.transactionHash; const height = result.height; return { transactionHash, height }; } catch (error) { return error; } } } exports.default = CosmosRpc; //# sourceMappingURL=cosmos.js.map