-
Notifications
You must be signed in to change notification settings - Fork 1
Home
OpenBit supports Dapps in connecting Bitcoin and EVM accounts.
To determine if OpenBit has been loaded, check window.OpenBitProvider
, window.OpenBitProvider helps you make requests from Dapps to OpenBit.
Simply provide the method name and any necessary parameters. For example, use getAddress
to connect with OpenBit.
const accounts = await window.OpenBitProvider.request("getAddresses");
Your application can use the getAddresses method to request a connection to the OpenBit wallet, prompting users to share their Bitcoin addresses.`
[
{
address: string, // The user’s connected wallet address
publicKey: string, // A hex string representing the bytes of the public key of the account. You can use this to construct partially signed
// Bitcoin transactions (PSBT).
isTestnet: boolean, // The network type of the connected account: mainnet or testnet
derivationPath: string,
tweakedPublicKey: string,
type: "p2tr" | "p2wpkh" | "unknow" // The address’s format,
// if the address type returned is 'unknown,' it means the address type is not yet supported by
// OpenBit.
},
]
const accounts = await window.OpenBitProvider.request("getAddresses");
{
result: {
addresses: [
{
"address": "tb1qw5k...zewats8j3",
"type": "p2sh",
"isTestnet": true,
"derivationPath": "m/84'/1'/0'/0/0",
"publicKey": "022d8f380dc8b4b29fa...6bf08c12f400a5e5dda15",
"tweakedPublicKey": "2d8f380dc8b4b29fad8e471aa...26bf08c12f400a5e5dda15"
},
{
"address": "tb1psnaergv47c8a2xt4hr...3k4fqt7rff3",
"type": "p2tr",
"isTestnet": true,
"derivationPath": "m/86'/1'/0'/0/0",
"publicKey": "02dbd9f32c6631786803215b97...30c35a642c533ba",
"tweakedPublicKey": "dbd9f32c6631786803215b97a2b2b8622a12cdf339b861b2230c35a642c533ba"
}
]
}
}
You can request your user to sign a message with their OpenBit's Bitcoin addresses, by invoking the signMessage method.
{
address: string, //a string representing the address to use to sign the message
message: string //a string representing the message to be signed by the wallet
}
{
signature: string, // a string representing the signed message.
address: string, // a string representing the address used for signing
message: string //a string representing the message to be signed by the wallet
}
const result = await window.OpenBitProvider.request("signMessage", {
message: "Hello world",
address: "tb1qpm2duck....efgj39g6wvw6m48"
});
{
signature: "GylRLzywQkyg0j5L/xPtW...gDZNBMnvYwOkGUSmXMHUxrZ9xCmDT5uQ1fJQ=",
address: "tb1qpm2duck....efgj39g6wvw6m48" ,
message: "Hello world"
}
Request the signature and broadcast of a Partially Signed Bitcoin Transaction (PSBT).
{
psbt: string, //Hex of PSBT payload for signing
allowedSighash ?: SignatureHash[], //Sighash types allowed for signing and finalizing inputs (defaults to type ALL)
signAtIndex ?: number | number[], // Input index(es) that should be signed (defaults to sign all inputs)
network : string, // Network for signing: mainnet, testnet,
account: string, //A string representing the address to use to sign the message
broadcast ?: boolean // Whether to broadcast upon signing (default false)
}
{
psbt : string, //The hex encoded signed PSBT
txid ?: string // The transaction id as a hex-encoded string.
// This is only returned if the transaction was broadcasted.
}
const result = await window.OpenBitProvider.request("signPsbt", {
psbt: "7073627...12a34e000000",
signAtIndex: [0],
account: "tb1qpm2duck3g53gj7lm...fgj39g6wvw6m48",
network: "testnet",
broadcast: false
});
{
psbt: "70736274ff010071020000000...555d7cc9000000",
txid: "0201acfcffe78dcf2...3e6006f675284beb0"
}
{
account: string, // Address of account to send
network: string, // Network for send: mainnet or testnet
recipients: RecipientsParam[] // Array of amounts to transfer by recipient address
}
interface RecipientParams {
address: string, // A string representing the recipient's address
amount: string // string representing the amount of Bitcoin to send, denominated in satoshis (Bitcoin base unit)
}
//The request will return a string that is the transaction ID as a hex-encoded string.
const result = await window.OpenBitProvider.request("sendTransfer", {
account: "tb1qpm2duck3g....fgj39g6wvw6m48",
recipients: [
{
address: "tb1qw5k3jylesv....wyw0hzewats8j3,
amount: "10000",
},
],
network: "testnet",
});
console.log(result)
//"0201acfcffe78dcf2...3e6006f675284beb0"
Most extension wallets or mobile wallets (using in-app browser) will publish their wallet's object to websites. The Web DApps can use these objects to interact with specific wallets to authorize, get accounts and another wallet's information and sign the contract or transaction.
This article will guide you through the process of integrating OpenBit with EVM DApps
This section does not only show you how to integrate wallets that support EVM Provider like OpenBit but also demonstrates how a wallet and an EVM DApp work together
You need to follow these steps:
1. Check wallet installation
const wallet = window.OpenBit;
if (!wallet || !wallet.isOpenBit) {
console.warn('OpenBit is not installed');
}
If wallet
is undefined
, it means OpenBit is not installed or activated
Note: OpenBit also publishes EVM provider to window.ethereum
if no wallet is available when the web page finishes loading. We wait until the page is loaded because we don’t want to conflict with EVM wallets like MetaMask.
2. Enable DApp - Wallet connection
try {
const accounts = await wallet.request({method: 'eth_requestAccounts'})
} catch (e) {
// Response an error with code 4001 when the user rejects the request
}
}
Another way to enable the wallet is just run await wallet.enabled()
There are some cases after running this code:
- DApp not in the authorized list: request will respond after user confirm the authorized popup
- DApp had been authorized: request will respond instantly
- DApp is blocked or user cancel authorized popup: request will throw an error with code 4001
3. Subscribe events
Subscribe chain changed
wallet.on('chainChanged', (chainId) => {
// ChainId in hex format
// Common action in almost EVM Dapp is reloading the browser
});
Subscribe accounts changed
wallet.on('accountsChanged', (accounts) => {
// Accounts is a can get from request('eth_accounts')
// Common action in almost EVM Dapp is reloading the browser
});
4. Fetch basic information and start to use app
In before section you can enable wallet and get app accounts with request('eth_requestAccounts')
. After the wallet is enabled you also can use request('eth_accounts')
to get the same thing.
const accounts = await wallet.request({method: 'eth_accounts}')
Not like MetaMask always responds list with one account, OpenBit will respond to all authorized EVM list if you select All Account.
Most EVM DApp will check wallet's current chain to ensure that they can information from the correct chain. Below is 2 ways to get chainId from the wallet
const hexChainId = await wallet.request({method: 'eth_chainId'}) // get chainId in hex format
const decChainId = await wallet.request({method: 'net_version'}) // get chainId in decimal format
You can request wallet to add or change the current network with methods like wallet_addEthereumChain
or wallet_switchEthereumChain
wallet.request({method: 'wallet_switchEthereumChain', [
{
chainId: '0x504' // Hex format of 1287
}
])