Web (JavaScript/TypeScript) - BTC Connect
Integrating BTC Connect within a web application.
BTC Connect for Web
BTC Connect is the first ERC-4337 account abstraction protocol on Bitcoin. Essentially, BTC Connect allows Layer-1 Bitcoin wallets to own and control smart accounts on Bitcoin Layer-2s. This is facilitated by a Bitcoin-specific wallet connection modal which allows a user to connect with Unisat, BitGet, OKX, Bybit, Bitget, or TokenPocket and immediately begin controlling both their native Bitcoin wallet and a directly associated smart account on various EVM-compatible chains without ever leaving the application or wallet.
Developers building with BTC Connect can seamlessly bridge interaction between Bitcoin Layer-2s (using smart accounts) and Bitcoin Layer-1, bringing the EVM landscape to their applications in under 100 lines of code and ushering in new possibilities for consumer-facing applications on Bitcoin.
Currently, BTC Connect is in its initial alpha release phase, and therefore only supports Web. The BTC Connect SDK (@particle-network/btc-connectkit
) acts as the sole library for integrating BTC Connect, facilitating wallet connection (through the aforementioned unified modal), smart account assignment and interaction, native Bitcoin transactions, etc.
In this page, you’ll find a complete rundown of what you need to know when using @particle-network/btc-connectkit
.
Getting Started
BTC Connect follows Particle Network’s AA SDK and Particle Auth Core very closely in terms of structure, so if you’ve used either of these SDKs before, you may find this process familiar.
Before diving into the process of configuring and initializing the SDK, you’ll need to go through the installation process and retrieve a few required values from the Particle dashboard.
Installation
BTC Connect is driven by one primary SDK, @particle-network/btc-connectkit
, and can be installed through either of the two following commands:
Setting up the Particle dashboard
Every Particle Network SDK requires the retrieval of three key values, your projectId
, clientKey
, and appId
.
These are used primarily to authenticate API requests and tie your project to the Particle dashboard, allowing you to track users, spin up RPC endpoints, fund your Paymaster for gasless transactions, and so on.
Initialization
Now that you’ve installed @particle-network/btc-connectkit
, you’ll need to configure and initialize it through the ConnectProvider
object. ConnectProvider
should, like AuthCoreContextProvider
from Particle Auth Core, wrap your primary App
either directly within its JSX or through your index
file. Essentially, ConnectProvider
controls the EVM chains you’d like to support, the wallets available through the connection modal, and other core configurations required to initialize BTC Connect. ConnectProvider
takes the following within its options
parameter:
projectId
, previously retrieved from the Particle dashboard.clientKey
, previously retrieved from the Particle dashboard.appId
, previously retrieved from the Particle dashboard.aaOptions
, an object containing:accountContracts
, which should be another object with:BTC
, an array of objects that take:chainIds
, an array of chain IDs representing the blockchains you intend to use within your application (these chains are also supported within@particle-network/chains
, in which you can export dedicated objects containing the chain ID, such asPolygonMumbai.id
).version
, which dictates the smart account version you’d like to use. Currently, both1.0.0
and2.0.0
are supported, depending on the chain (chainIds
) you define.2.0.0
is a newer implementation with better gas optimizations.
walletOptions
, an optional object containing:visible
, a Boolean determining whether or not the Particle Wallet modal is shown after connection. In this case, the wallet modal can be used to control the smart account, although if you intend to implement equivalent functionality within your application, such as user-defined transactions, you may not need this.
Then, outside of the options
parameter (which expects the above values), you’ll also need to define connectors
, an object containing an array of wallet connector instances imported from @particle-network/btc-connectkit
, such as:
UnisatConnector
OKXConnector
BitgetConnector
TokenPocketConnector
BybitConnector
WizzConnector
XverseConnector
Below is a code snippet showcasing a complete configuration of ConnectProvider
within a standard index
file setup, following the structure covered above:
Examples of Utilization
Like Particle Auth Core, BTC Connect functions primarily based on hooks (available from @particle-network/btc-connectkit
) for functions such as facilitating wallet connection, using the smart account, sending BTC, etc. Below, we’ll review the four primary hooks within BTC Connect.
useConnectModal
(for wallet connection)
Wallet connection within BTC Connect happens through a unified connection modal with wallet options according to your connectors
parameter within ConnectProvider
. useConnectModal
exposes two functions for this:
openConnectModal
. This will open the aforementioned modal immediately upon being called.disconnect
. Similarly,disconnect
will reset the wallet/account state (log a user out). See the snippet below for an example of defining and using these functions fromuseConnectModal
:
Upon calling openConnectModal
, the user will be presented with a menu that looks like the following (deviating depending only on connectors
from ConnectProvider
).
useAccounts
(and associated address retrieval methods)
Given that with BTC Connect users will have two addresses (one for their Layer-1 Bitcoin account, and one for their EVM smart account). It’s important to retrieve, reflect, and manage these addresses.
BTC Connect has three primary mechanisms in place for address retrieval:
- BTC address retrieval through
useAccounts
oruseBTCProvider
, and: - EVM account retrieval through
useETHProvider
withaccount
.
useAccounts
and useBTCProvider
both expose accounts
, an array of connected Bitcoin addresses (which will change based on whether a user is connected to the Livenet or Testnet). Additionally, useETHProvider
has account
, a string representing the associated smart account address.
Below is an example of defining each of these variables:
useBTCProvider
(for native Bitcoin functions)
useBTCProvider
acts as the primary hook allowing to sign messages, send transactions, and retrieve additional wallet information (such as the network, public key, etc.) with the user’s native Bitcoin account. Confirmations will be routed through the same wallet as useETHProvider
, but these transactions will be executed on either the Bitcoin Livenet or Testnet. useBTCProvider
has the following functions:
getNetwork
, for retrieving the current network of the wallet (Livenet or Testnet).switchNetwork
, to forcibly switch the network. Takes a string, either ‘livenet’ or ‘testnet’.signMessage
, to request that a user signs a message. Takes any string (the message to be signed).getPublicKey
, for retrieving the public key of the wallet.sendBitcoin
, for sending a standard Bitcoin transaction. This function takes the following:toAddress
, the recipient of the transaction.satoshis
, the value of the transaction in satoshis.options
, an optional object containing:feeRate
, for adjusting transaction fees.
An example of leveraging useBTCProvider
has been included below:
useETHProvider
(for controlling the associated smart account)
Most importantly, useETHProvider
acts as the single mechanism for driving interaction through the associated EVM smart account. Interaction is driven through one of two ways:
- With native UserOperation construction and execution functions, such as
buildUserOp
andsendUserOp
, which will mimic similar functions found within Particle Network’s AA SDK for managing transactions. - Through the attached EIP-1193
provider
object, enabling utilization of the smart account through a standard Web3 library such as Ethers, Web3.js, viem, etc.
Below is the full list of functions derived from useETHProvider
:
chainId
, the ID for the currently connected chain.- For a complete list of networks supported within BTC Connect, see Network Coverage.
account
, the smart account address (automatically populated upon login).getSmartAccountInfo
, get smart account info(version, isDeployed, etc.).switchChain
, for forcibly changing the EVM chain used through the smart account. This takes a specificchainId
(should be one of the aforementioned supported chains).provider
, the EIP-1193 provider object associated with the smart account.getFeeQuotes
, for retrieving fee quotes (a collection of UserOperation structures based upon different fee payment mechanisms); this is equivalent to getFeeQuotes.buildUserOp
, to build a execution-ready UserOperation with a standard transaction object.sendUserOp
, for sending UserOperations (either objects derived fromgetFeeQuotes
/buildUserOp
, or standard transaction objects).publicClient
, the viemPublicClient
object for making standard JSON-RPC requests.
The following snippet is an example of defining and using each of these methods:
Master reference
For a direct, raw view into every method provided through useAccount
, useETHProvider
, useBTCProvider
, and useConnectModal
, below is a table containing every relevant one alongside specific parameters and a short description. For methods listed that weren’t covered in the above examples, live implementation often mimics the common structure covered throughout this document.
Hook | Function | Parameters (* indicates optional) |
---|---|---|
useConnectModal | openConnectModal | N/A |
useConnectModal | disconnect | N/A |
useAccounts | accounts | N/A |
useBTCProvider | provider | N/A |
useBTCProvider | accounts | N/A |
useBTCProvider | getPublicKey | N/A |
useBTCProvider | signMessage | message: string |
useBTCProvider | getNetwork | N/A |
useBTCProvider | switchNetwork | network: ‘livenet’ | ‘testnet’ |
useBTCProvider | sendBitcoin | toAddress: string, satoshis: number, options*: {feeRate: number} |
useETHProvider | account | N/A |
useETHProvider | getSmartAccountInfo | N/A |
useETHProvider | provider | N/A |
useETHProvider | switchChain | chainId: number |
useETHProvider | chainId | N/A |
useETHProvider | getFeeQuotes | tx: Transaction | Transaction[] |
useETHProvider | buildUserOp | { tx: Transaction | Transaction\[], feeQuote: FeeQuote, tokenPaymasterAddress: string } |
useETHProvider | sendUserOp | params: SendTransactionParams, forceHideConfirmModal?: boolean |
useETHProvider | publicClient | N/A |
Was this page helpful?