Cosmos Provider

Using Cosmos wallet provider

Cosmos wallet provider api is as below:

import { TomoCosmosInjected, WalletProvider } from from '@tomo-inc/tomo-wallet-provider';
import { AminoSignResponse, BroadcastMode, KeplrSignOptions, StdSignature, StdSignDoc } from '@keplr-wallet/types';
import { OfflineAminoSigner, OfflineDirectSigner } from '@keplr-wallet/types/src/cosmjs';
import { SigningStargateClient } from '@cosmjs/stargate';

export declare class CosmosProvider extends WalletProvider {
    provider: TomoCosmosInjected;
    offlineSigner?: OfflineAminoSigner & OfflineDirectSigner;
    clientPromise?: Promise<SigningStargateClient>;
    constructor(chains: any[], provider: TomoCosmosInjected);
    connectWallet(): Promise<this>;
    /**
     * get @cosmjs/stargate SigningStargateClient
     */
    getSigningStargateClient(): Promise<SigningStargateClient>;
    getBalance(searchDenom: string): Promise<bigint>;
    /**
     * Gets the bech32Address of the connected wallet.
     * @returns A promise that resolves to the address of the connected wallet.
     */
    getAddress(): Promise<string>;
    /**
     * get the chainId of the connected wallet
     */
    getNetwork(): Promise<string>;
    signAmino(signerAddress: string, signDoc: StdSignDoc, signOptions?: KeplrSignOptions): Promise<AminoSignResponse>;
    signArbitrary(signer: string, data: string | Uint8Array): Promise<StdSignature>;
    sendTx(tx: Uint8Array, mode: BroadcastMode): Promise<Uint8Array>;
    getOfflineSigner(): Promise<OfflineAminoSigner & OfflineDirectSigner>;
    getPublicKeyHex(): Promise<string>
}

And after connected to wallet, cosmos wallet provider could be used as follows:

<TomoContextProvider
  chainTypes={['cosmos', 'bitcoin']} // can be removed, we support both by default
  cosmosChains={[
    {
      id: 2,
      name: 'Cosmos',
      type: 'cosmos',
      network: 'cosmoshub-4'
    }
  ]}
  providerOptions={{  // optional, support customized window object context
    getWindow() {
      return window.parent
    }
  }}
  ...
>
  <ChildComponent />
</TomoContextProvider>

// after connected, providers could be used
export function ChildComponent(props: ChildProps) {
  const providers = useTomoProviders()

  const signCosmos = async (address: string, amount: string) => {
    const curChainId = providers.cosmosProvider.getChainId()
    const key = await providers.cosmosProvider.provider.getKey(curChainId)  

    // construct your signDoc
    ...
    
    const { signed, signature } = await providers.cosmosProvider.signAmino(
      key.bech32Address,
      signDoc
    )
  }

  return (
    <div>
      <TomoSocial />
    </div>
  )
}

Using customized SigningStargateClient

const providers = useTomoProviders()

providers.cosmosProvider!.initSigningStargateClient(
    await SigningStargateClient.connectWithSigner(
        rpcUrl,
        providers.cosmosProvider!.offlineSigner!,
        options
    )
);

Last updated