// binancew3w.bitcoinif (window.binancew3w.bitcoin) { console.log("Bitcoin Provider is detected!");}// we also proxy window.unisat, but some of the APIs are differentif (window.unisat && window.unisat.isBinance) { console.log("Bitcoin Provider is detected!");}
Connecting to Bitcoin
Connecting" or "logging in" to Binance Web3 Wallet effectively means "to access the user's Bitcoin
account(s)".
We recommend that you provide a button to allow the user to connect Binance Web3 Wallet to your
dapp. Clicking this button should call the following method:
Code
binancew3w.bitcoin.requestAccounts();
Methods
requestAccounts
Code
binancew3w.bitcoin.requestAccounts();
Connect the current account.
Parameters
none
Returns
Promise returns string[] : Address of current account.
network - string: the network. livenettestnet or signet
Returns
none
Example
Code
try { let res = await window.binancew3w.bitcoin.switchNetwork("livenet"); console.log(res)} catch (e) { console.error(e)}> true
getPublicKey
Code
binancew3w.bitcoin.getPublicKey();
Get publicKey of current account
Parameters
none
Returns
Promise returns string : publicKey (no tweak for taproot)
Example
Code
try { let res = await window.binancew3w.bitcoin.getPublicKey(); console.log(res)} catch (e) { console.log(e);}> 03cbaedc26f03fd3ba02fc936f338e980c9e2172c5e23128877ed46827e935296f
getBalance
Code
binancew3w.bitcoin.getBalance();
Get BTC balance
Parameters
none
Returns
Promise returns Object :
confirmed - number: the confirmed satoshis
unconfirmed - number: the unconfirmed satoshis
total - number: the total satoshis
Example
Code
try { let res = await window.binancew3w.bitcoin.getBalance(); console.log(res)} catch (e) { console.log(e);}> { "confirmed":0, "unconfirmed":100000, "total":100000 }
signMessage
Code
binancew3w.signMessage(msg[, type])
sign message
Parameters
msg - string: a string to sign
type - string: (Optional) "ecdsa" | "bip322-simple". default is "ecdsa"
Returns
Promise returns string: the signature
Example
Code
// sign by ecdsatry { let res = await window.binancew3w.bitcoin.signMessage("abcdefghijk123456789"); console.log(res)} catch (e) { console.log(e);}> G+LrYa7T5dUMDgQduAErw+i6ebK4GqTXYVWIDM+snYk7Yc6LdPitmaqM6j+iJOeID1CsMXOJFpVopvPiHBdulkE=// sign by bip322-simpletry { let res = await window.binancew3w.bitcoin.signMessage("abcdefghijk123456789","bip322-simple"); console.log(res)} catch (e) { console.log(e);}> AkcwRAIgeHUcjr0jODaR7GMM8cenWnIj0MYdGmmrpGyMoryNSkgCICzVXWrLIKKp5cFtaCTErY7FGNXTFe6kuEofl4G+Vi5wASECaIeVi8xMtvjATqSSYPDRDjEsQbr0hSUpU7FHJNtVKqw=
signPsbt
Code
binancew3w.signPsbt(psbtHex[, options])
Sign PSBT
This method will traverse all inputs that match the current address to sign.
Parameters
psbtHex - string: the hex string of psbt to sign
options - object:
autoFinalized - boolean: whether finalize inputs signed by user's wallet after signing,
default is true
toSignInputs - array:
index - number: which input to sign
address - string: (at least specify either an address or a publicKey) Which corresponding
private key to use for signing
publicKey - string: (at least specify either an address or a publicKey) Which
corresponding private key to use for signing
sighashTypes - number[]: (optionals) sighashTypes
disableTweakSigner - boolean: (optionals) When signing and unlocking Taproot addresses,
the tweakSigner is used by default for signature generation. Enabling this allows for signing
with the original private key.
Returns
Promise returns string: the hex string of signed psbt