Skip to content

01WalletStrategy

Bojan Angjelkoski edited this page Mar 3, 2023 · 6 revisions

The main purpose of the @injectivelabs/wallet-ts is to offer developers a way to have different wallet implementations on Injective. All of these wallets implementations are exposing the same ConcreteStrategy interface which means that users can just use these methods without the need to know the underlying implementation for specific wallets as they are abstracted away.

To start, you have to make an instance of the WalletStrategy class which gives you the ability to use different wallets out of the box. You can switch the current wallet that is used by using the setWallet method on the walletStrategy instance.

Lets have a look at the methods that WalletStrategy strategy exposes and what do they mean:

  • getAddresses gets the addresses from the connected wallet strategy,

  • sendEthereumTransaction sends an Ethereum transaction using the connected wallet strategy,

  • sendTransaction sends an Injective transaction using the connected wallet strategy,

  • signCosmosTransaction signs an Injective transaction using the connected wallet strategy,

  • signEthereumTransaction signs an Ethereum transaction using the connected wallet strategy,

  • getPublicKey get the public key for the Cosmos native wallet strategies,

  • getNetworkId get the network id for the Ethereum native wallet strategies,

  • getChainId get the chain id for the Ethereum native wallet strategies,

  • getEthereumTransactionReceipt get the transaction receipt for Ethereum native transactions for the wallet strategy,

  • getWeb3 gets an Web3 instance. Depending on the wallet strategy it uses a different web3 provider (user provided RPC for some, the window object injected provider for Metamask, etc)

Example usage

import { WalletStrategy } from '@injectivelabs/wallet-ts'
import { EthereumChainId, ChainId } from '@injectivelabs/ts-types'
import { CHAIN_ID, ETHEREUM_CHAIN_ID, IS_TESTNET } from '~/app/utils/constants'

export const alchemyRpcEndpoint = `https://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_KEY}`
export const alchemyWsRpcEndpoint = `wss://eth-mainnet.ws.alchemyapi.io/v2/${process.env.ALCHEMY_KEY}`

export const walletStrategy = new WalletStrategy({
  chainId: ChainId.Mainnet,
  ethereumOptions: {
    ethereumChainId: EthereumChainId.Mainnet,
    wsRpcUrl: alchemyWsRpcEndpoint,
    rpcUrl: alchemyRpcEndpoint
  }
})

// Get wallet's addresses
export const getAddresses = async (): Promise<string[]> => {
  const addresses = await walletStrategy.getAddresses()

  if (addresses.length === 0) {
    throw new Web3Exception('There are no addresses linked in this wallet.')
  }

  return addresses
}

// Sign an Cosmos transaction
export const signTransaction = async (tx: TxRaw): Promise<string[]> => {
  const response = await walletStrategy.signCosmosTransaction(
    transaction: { txRaw: tx; accountNumber: /* */; chainId: 'injective-1' },
    address: 'inj1...',
  )

  return response
}

⚠️ DOCUMENTATION ⚠️

Clone this wiki locally