Bitcoin

Wallet Provider Injection

Since the Bitcoin community does not have a standard specification for Providers, imToken references EIP-1193 from the Ethereum community and implements it by injecting a bitcoin property into the window object. The specific implementation is as follows:

window.bitcoin = new BitcoinProvider({
  requestTransport: ({ method, params }) => window.imToken.callApi(
    'bitcoin.request',
    {
      method,
      params
    }
  )
})

Developers can access the API provided by the Wallet Provider via window.bitcoin:

Schema

type WalletRpcSchema = [
  /**
   * @description Requests that the user provides an address to be identified by.
   * @example
   * provider.request({ method: 'btc_requestAccounts' }] })
   * // => ['tbc1...', 'tbc1...']
   */
  {
    Method: 'btc_requestAccounts'
    Parameters?: undefined
    ReturnType: string[]
  },
  /**
   * @description Returns the current network associated with the wallet.
   * @example
   * provider.request({ method: 'btc_getNetwork' })
   * // => 'signet'
   */
  {
    Method: 'btc_getNetwork'
    Parameters?: undefined
    ReturnType: Network
  },

  /**
   * @description Gets the public key of the connected wallet.
   * @example
   * provider.request({ method: 'btc_getPublicKey' }] })
   * // => 'publicKey...'
   */
  {
    Method: 'btc_getPublicKey'
    Parameters?: undefined
    ReturnType: string
  },

  /**
   * @description Returns the balance of an address in satoshis.
   * @example
   * provider.request({ method: 'btc_getBalance', params: ['tbc1...'] })
   * // => 1000
   */
  {
    Method: 'btc_getBalance'
    Parameters: [address: string]
    ReturnType: number
  },

  /**
   * @description Retrieves the unspent transaction outputs (UTXOs) for a given address and amount.
   * If the amount is provided, it will return UTXOs that cover the specified amount.
   * If the amount is not provided, it will return all available UTXOs for the address.
   * @example
   * provider.request({ method: 'btc_getUnspent', params: ['tbc1...', 100] })
   * // => [{ utxo }]
   */
  {
    Method: 'btc_getUnspent'
    Parameters: [address: string, amount: number]
    ReturnType: UTXO[]
  },

  /**
   * @description Signs the given PSBT in hex format.
   * @example
   * provider.request({ method: 'btc_signPsbt', params: ['0x...', true] })
   * // => '0x...'
   */
  {
    Method: 'btc_signPsbt'
    Parameters: [psbtHex: Hex, autoFinalize?: boolean]
    ReturnType: Hex
  },
  /**
   * @description Signs multiple PSBTs in hex format.
   * @example
   * provider.request({ method: 'btc_signPsbts', params: ['0x...', '0x...'] })
   * // => ['0x...', '0x...']
   */
  {
    Method: 'btc_signPsbts'
    Parameters: [psbtsHexes: Hex[]]
    ReturnType: Hex[]
  },
  /**
   * @description Signs a message using BIP-322.
   * @example
   * provider.request({ method: 'btc_signMessage', params: ['tb1...'] })
   * // => 'base64...'
   */
  {
    Method: 'btc_signMessage'
    Parameters: [
      message: string,
      type: 'bip322-simple' | 'bip322-full' | 'bip322-legacy'
    ]
    ReturnType: string
  },
  /**
   * @description send raw transaction
   * @example
   * provider.request({ method: 'btc_sendRawTransaction', params: ['signature'] })
   * // => 'string'
   */
   {
     Method: 'btc_sendRawTransaction'
     Parameters: [signature: string]
     ReturnType: string
   },

  /**
   * @description Switch the wallet to the given network.
   * @example
   * provider.request({ method: 'btc_switchNetwork', params: ['signet'] })
   * // => null
   */
  {
    Method: 'btc_switchNetwork'
    Parameters: [network: Network]
    ReturnType: null
  }
]

Provider API

Request User Accounts

  • Method: btc_requestAccounts

  • Parameters: None

  • Returns: string[]

  • Example:

Get Current Network

  • Method: btc_getNetwork

  • Parameters: None

  • Returns: Network

  • Example:

Get Public Key

  • Method: btc_getPublicKey

  • Parameters: None

  • Returns: string

  • Example:

Get Balance

  • Method: btc_getBalance

  • Parameters: [address: string]

  • Returns: number (unit is satoshi)

  • Example:

Get Unspent Transaction Outputs (UTXO)

  • Method: btc_getUnspent

  • Parameters: [address: string, amount: number]

  • Returns: UTXO[]

  • Example:

Sign PSBT

  • Method: btc_signPsbt

  • Parameters: [psbtHex: Hex, autoFinalize?: boolean]

  • Returns: Hex

  • Example:

Batch Sign PSBT

Not yet supported

  • Method: btc_signPsbts

  • Parameters: [psbtsHexes: Hex[]]

  • Returns: Hex[]

  • Example:

Sign Message

Only bip322-simple is supported.

  • Method: btc_signMessage

  • Parameters: [message: string, type: 'bip322-simple' | 'bip322-full' | 'bip322-legacy']

  • Returns: string (Base64 encoded)

  • Example:

Send Raw Transaction

  • Method: btc_sendRawTransaction

  • Parameters: [signature: string]

  • Returns: string

  • Example:

Switch Network

Not yet supported

  • Method: btc_switchNetwork

  • Parameters: [network: Network]

  • Returns: null

  • Example:

Other Methods

Get Wallet Name and Icon

Event Listening

The Provider inherits from EventEmitter and can use standard event listening methods.

Version Compatibility

imToken started injecting the bitcoin provider from version 2.15.4.

Last updated

Was this helpful?