Extend the SDK

Besides, users can also customize their own wallet provider in an easier way. The BTCProvider class provides default implementations for most Bitcoin interfaces. What users need to do is compare the interfaces of the target wallet with the default implementations of BTCProvider. The CosmosProvider class provides default implementations for most Cosmos interfaces. What users need to do is compare the interfaces of the target wallet with the default implementations of CosmosProvider.

import {
  BTCProvider,
  CosmosProvider,
  TomoChain
} from '@tomo-inc/tomo-wallet-provider'

class XYZWallet extends BTCProvider {
  constructor(chains: TomoChain[]) {
    // @ts-ignore
    const bitcoinNetworkProvider = window?.xyzWallet

    if (!bitcoinNetworkProvider) {
      throw new Error('XYZ Wallet not found')
    }
    super(chains, bitcoinNetworkProvider)
  }

  connectWallet = async (): Promise<this> => {
    const accounts = await this.bitcoinNetworkProvider?.requestAccounts()

    const address = accounts[0]
    const publicKeyHex = await this.getPublicKeyHex()

    if (!address || !publicKeyHex) {
      throw new Error('Could not connect to XYZ Wallet')
    }
    return this
  }
}

class ABCWallet extends CosmosProvider {
  constructor(chains: TomoChain[]) {
    // @ts-ignore
    const cosmosProvider = window?.abcCosmos

    if (!cosmosProvider) {
      throw new Error('ABC Wallet not found')
    }
    super(chains, cosmosProvider)
  }
}

Then you can use it in the wallet context provider

<TomoContextProvider
  additionalWallets={[
    {
      id: 'xyz',
      name: 'XYZ BTC Wallet',
      chainType: 'bitcoin',
      connectProvider: XYZWallet, // XYZWallet should extends BTCProvider
      type: 'extension',
      img: 'https://your wallet logo.svg'
    }, {
      id: 'abc',
      name: 'ABC Cosmos Wallet',
      chainType: 'cosmos',
      connectProvider: ABCWallet, // ABCWallet should extends CosmosProvider
      type: 'extension',
      img: 'https://your wallet logo.svg'
    },
  ]}
>
</TomoContextProvider>

Last updated