After setting up your wallet, you can have access to the Bitcoin provider:
Copy // react
const { providers } = useTomo()
const { bitcoinProvider } = providers;
// pure js
const bitcoinProvider = window.tomo_bitcoin
Get Address and Balance
You can first check and change the address type you want to use
Copy // interface
getAddressType(): string
changeAddressType(addressType: string): Promise<void>
export enum AddressType {
P2PKH,
P2WPKH,
P2TR,
P2SH_P2WPKH,
}
// react
const btcAddressType= await bitcoinProvider.getAddressType()
await bitcoinProvider.changeAddressType("P2SH_P2WPKH")
// pure js
const btcAddress = await window.tomo_bitcoin.getAddressType()
await window.tomo_bitcoin.changeAddressType("P2PKH")
Then, get the address. However, if the user uses extension wallet, not all address type may have value.
Copy // interface
// get current type address, by default it is taproot
getAddress(): Promise<string>
getAllAddresses(): Promise<AddressInfo[]>
getBalance(address?: string): Promise<number>
interface AddressInfo {
value: AddressType;
label: string;
name: string;
displayName?: string;
hdPath: string;
address?: string;
}
// form wallet state
const { walletState, providers } = useTomo()
const { btcAddress } = walletState
// or
const { bitcoinProvider } = providers;
const btcAddress = await bitcoinProvider.getAddress()
const allAddresses= await bitcoinProvider.getAllAddresses()
const balance= await bitcoinProvider.getBalance(btcAddress)
// pure js
const btcAddress = await window.tomo_bitcoin.getAddress()
const allAddresses= await window.tomo_bitcoin.getAllAddresses()
const balance= await window.tomo_bitcoin.getBalance(btcAddress)
Get the public key
In addition to the address, you can get the concrete public key from the provider.
Copy //interface
getPublicKey(): Promise<string>
//react
const publicKey = await bitcoinProvider.getPublicKey()
// pure js
const publicKey = await window.tomo_bitcoin.getPublicKey()
Switch Network
We support three networks for different purposes:
Copy // interface
getNetwork(): string
switchNetwork(network: Network): Promise<void>
const network = 'signet' | 'testnet' | 'mainnet'
// react
const current = await bitcoinProvider.getNetwork()
const sendRes = await bitcoinProvider.switchNetwork(network)
// pure js
const current = await window.tomo_bitcoin.getNetwork()
const sendRes = await window.tomo_bitcoin.switchNetwork(network)
Provider Functions
providers.bitcoinProvider
exposes three functions for sending requests to the user's wallet:
Signing a Message
It uses the bitcoinprovider
to sign a plaintext offchain message, which returns the message's signature.
Copy // interface
signMessage(message: string, _type: "ecdsa" | "bip322-simple" = "ecdsa"): Promise<string>
const message = 'your message'
const type: "ecdsa" | "bip322-simple" = 'ecdsa' // default is ecdsa
// react
const signedMessage = await bitcoinProvider.signMessage(message, type)
// pure js
const signedMessage = window.tomo_bitcoin.signMessage(message, type)
Sign Psbt
Given a hex raw transaction you can use signPsbt
method to get the signed transaction in hex value if the user approves the request.
Copy // interface
signPsbt(psbtHex: string): Promise<string>
signPsbts(psbtHexs: string[]): Promise<string[]>
const psbtHex = 'hex String'
// react
const signed = await bitcoinProvider.signPsbt(psbtHex)
// pure js
const signed = await window.tomo_bitcoin.signPsbt(psbtHex)
Send Bitcoin
You can also send the transaction directly through this sendBitcoin
method. The return value is the transaction signature if signed and sent correctly.
Copy const toAddress = 'toAddress'
const satAmount = 1 // BTC
const feeRate = 123 // sat/vB
// react
const sendRes = await bitcoinProvider.sendBitcoin(toAddress, satAmount, feeRate)
// pure js
const sendRes = await window.tomo_bitcoin.sendBitcoin(toAddress, satAmount, feeRate)