Bitcoin
Bitcoin payment method for WDK. Powered by Electrum protocol โก
โจ Features:
๐ Support for P2WPKH/BIP84 HD path traversal.
๐ฐ Internal UTXO management
๐งฎ Internal balance calculation.
๐ก Transaction broadcasting
๐งฉ Modular design. drop in seed/storage/block source components
Terminology
Wallet Software
Electrum: A popular, feature-rich Bitcoin wallet software that supports various advanced features like multi-signature wallets, hardware wallet integration, and coin control. It uses a distributed network of servers to verify transactions and broadcast them to the Bitcoin network.
Fulcrum: An open-source implementation of the Electrum Server protocol. It allows users to run their own Electrum server, providing enhanced privacy and independence from third-party servers when using Electrum wallet software.
Bitcoin Improvement Proposals (BIPs)
BIPs are design documents for introducing features or information to Bitcoin. Here are links to the repositories of three important BIPs related to wallet management:
These BIPs work together to provide a standardized way of generating and managing Bitcoin addresses and keys, enhancing interoperability between different wallet implementations.
These BIPs work together to provide a standardized way of generating and managing Bitcoin addresses and keys, enhancing interoperability between different wallet implementations. To learn more about Bitcoin check out the Plan B school
๐ Usage
๐ Methods
๐ ๏ธ Methods
The following methods are available on this module:
๐ getNewAddress()
getNewAddress()
Description: Generates a new Bitcoin address.
Return Value: A Promise that resolves to the newly generated address.
Example usage:
๐ฐ getBalance(opts, addr)
getBalance(opts, addr)
Description: Retrieves the balance of an address or the entire wallet.
Return Value: A Promise that resolves to the balance in BTC (or a rejection with an error message).
Parameters: +
opts
(optional): An object containing configuration options for the method. Currently, no specific properties are required. +addr
: The address you want to get the balance for
Example usage:
๐ syncTransactions(opts)
syncTransactions(opts)
Description: Syncs transactions with Electrum.
Return Value: A Promise that resolves when syncing is complete (or a rejection with an error message).
Parameters: +
opts
(optional): An object containing configuration options for the method. Currently, no specific properties are required. -reset
Boolean, if you want to resync from start
Example usage:
โธ๏ธ pauseSync()
pauseSync()
Description: Pauses syncing transactions from Electrum.
Return Value: A Promise that resolves immediately (or a rejection with an error message).
Example usage:
๐ค sendTransaction(opts, outgoing)
sendTransaction(opts, outgoing)
Description: Sends a transaction to a specified address.
Return Value: A Promise that resolves when the transaction is sent (or a rejection with an error message).
Parameters: +
outgoing
: An object containing configuration options for the method. Required properties include: -address
-amount
-unit
main
for btc andbase
for sats -fee
in sats per byte: +opts
:
Example usage:
๐ getTransactions(opts, fn)
getTransactions(opts, fn)
Description: Retrieves transaction history from the history store. This method iterates through all entries in the history store and processes transactions using the provided callback function.
Return Value: A Promise that resolves when all transactions have been processed (or a rejection with an error if an exception occurs).
Parameters:
fn
(Function): A callback function to process each set of transactions.
Example usage:
Notes:
The callback function
fn
should be an async function that takes one parameter: an array of transaction objects for a specific block height.This method retrieves all transactions stored in the history store, including those in the mempool (height 0).
Transactions are grouped by block height in the history store.
The method uses the
entries
method of the underlying store, which may have performance implications for large transaction histories.
๐ Events
The BitcoinPay
instance emits the following events:
1. ๐ข 'ready'
'ready'
Description: Emitted when the wallet is fully initialized and ready for use.
Callback Parameters: None
Example usage:
2. ๐ 'synced-path'
'synced-path'
Description: Emitted for each HD path that has been synced during the transaction synchronization process.
Callback Parameters:
pathType
(String): Type of the path (e.g., 'external', 'internal')path
(String): The HD path that was syncedhasTx
(Boolean): Whether the path has any transactionsprogress
(Object): Sync progress information
Example usage:
3. ๐ธ 'new-tx'
'new-tx'
Description: Emitted when a new transaction is detected for the wallet.
Callback Parameters:
transaction
(Object): The new transaction object
Example usage:
๐ ๏ธ Development
To set up the development environment for the Bitcoin payment module, follow these steps:
Set up a local Bitcoin environment:
Follow the instructions in the Test tools : Bitcoin
This will help you set up a local Bitcoin regtest network for development and testing
Clone the repository:
Install dependencies:
Run tests:
๐ Links
List of Electrum nodes (Fulcrum recommended)
๐งช Testing
There is extensive integration tests for this package.
We use Brittle for testing. Checkout package.json for various test commands.
Integration tests need a electrum server connected to a regtest bitcoin node.
To setup testing environment see: WDK test tools
to run tests, take a look at package.json
for the various test scripts.
Last updated