import { CosmWasmFeeTable } from "@cosmjs/cosmwasm-stargate" ; import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate" ; import { Bip39 , Random } from "@cosmjs/crypto" ; import { defaultGasLimits as defaultStargateGasLimits , GasLimits , GasPrice , makeCosmoshubPath , } from "@cosmjs/stargate" ; import { DirectSecp256k1HdWallet , OfflineSigner } from "@cosmjs/proto-signing" ; import { LedgerSigner } from "@cosmjs/ledger-amino" ; import { DistributionExtension , QueryClient , setupDistributionExtension , setupStakingExtension , StakingExtension , } from "@cosmjs/stargate" ; import { Tendermint34Client } from "@cosmjs/tendermint-rpc" ; import TransportWebUSB from "@ledgerhq/hw-transport-webusb" ; import { AppConfig } from "../config" ; export function generateMnemonic ( ) { return Bip39 . encode ( Random . getBytes ( 16 ) ) . toString ( ) ; } export function loadOrCreateMnemonic ( ) { const key = "burner-wallet" ; const loaded = localStorage . getItem ( key ) ; if ( loaded ) { return loaded ; } const generated = generateMnemonic ( ) ; localStorage . setItem ( key , generated ) ; return generated ; } export type WalletLoader = ( chainId : string , addressPrefix ? : string ) => Promise < OfflineSigner > ; export async function loadOrCreateWallet ( _chainId , addressPrefix ? ) { const mnemonic = loadOrCreateMnemonic ( ) ; const hdPath = makeCosmoshubPath ( 0 ) ; const wallet = await DirectSecp256k1HdWallet . fromMnemonic ( mnemonic , { hdPaths : [ hdPath ] , prefix : addressPrefix } ) ; return wallet ; } export async function loadLedgerWallet ( _chainId , addressPrefix ? ) { const interactiveTimeout = 120000 ; const ledgerTransport = await TransportWebUSB . create ( interactiveTimeout , interactiveTimeout ) ; return new LedgerSigner ( ledgerTransport , { hdPaths : [ makeCosmoshubPath ( 0 ) ] , prefix : addressPrefix } ) ; } export async function loadKeplrWallet ( chainId ) { const anyWindow = window ; if ( ! anyWindow . getOfflineSigner ) { throw new Error ( "Keplr extension is not available" ) ; } const signer = anyWindow . getOfflineSigner ( chainId ) ; signer . signAmino = signer . signAmino ?? signer . sign ; return Promise . resolve ( signer ) ; } export async function createClient ( config , signer ) { const gasLimits = { ... defaultStargateGasLimits , upload : 1500000 , init : 600000 , exec : 400000 , migrate : 600000 , send : 80000 , changeAdmin : 80000 } ; return SigningCosmWasmClient . connectWithSigner ( config . rpcUrl , signer , { prefix : config . addressPrefix , gasPrice : GasPrice . fromString ( `${ config . gasPrice }${ config . feeToken }` ) , gasLimits : gasLimits } ) ; } export async function createStakingClient ( apiUrl ) { const tmClient = await Tendermint34Client . connect ( apiUrl ) ; return QueryClient . withExtensions ( tmClient , setupStakingExtension , setupDistributionExtension ) ; }