miniBTCD is a Golang-based Bitcoin solo node or mock node designed for simulation purposes. It can process mainnet transactions but lacks the P2P network peering feature. This project is ideal for those looking to understand Bitcoin transactions and node operations in a simplified, controlled environment.
- Mining Capability: The server includes functionality to mine new blocks.
- Wallet Operations:
- Create P2PKH (Pay-to-PubKey-Hash) wallets
- Create P2SH (Pay-to-Script-Hash) wallets
- Create P2WPKH (Pay-to-Witness-PubKey-Hash) wallets
- Transaction Operations:
- Create transactions for P2PKH, P2SH, and P2WPKH addresses
- Check wallet balance
- Get address transaction history
- Retrieve block hex data
- Retrieve transaction details by ID
Ensure you have Golang installed on your machine. You can download it from here.
Clone the repository:
git clone https://github.com/Thankgod20/miniBTCD.git
cd miniBTCD
The server handles the blockchain operations and provides an RPC interface for interaction. It also has optional mining capabilities.
Ensure you have Redis installed and running on your machine as it is used for the blockchain state management. You can download Redis from here.
Navigate to the RPCServer
folder and run the server:
cd RPCServer
go run main.go [OPTIONS]
--mining
: Enable mining.--time <minutes>
: Specify the interval for mining in minutes.--address <miner_address>
: Specify the miner address for mining rewards.
go run main.go
To enable mining, provide the --mining
flag along with the --address
of the miner and the --time
interval in minutes:
go run main.go --mining --address="your-miner-address" --time=10
To enable selected mining, provide the --mining
flag along with the --address
of the miner, the --time
interval in minutes,--smining
for enabling selected mining and --mineaddr
for the path to the json file that contains the address thats allowed to be mined:
go run main.go --mining --address="your-miner-address" --time=10 --smining --mineaddr="addresses.json"
To add Selected Address to addresses.json
go to the Add directory and run
go run main.go
Starting The blockchain for the first time, the miner-address is same as the first genesis address to recieve the first 1000 BTC
- Initialize Redis Client: The server initializes a Redis client to manage the blockchain state.
- Initialize Blockchain: The blockchain is initialized with the Redis client.
- Mining: If mining is enabled, the server will periodically check the mempool for transactions and mine new blocks at the specified interval.
- Register RPC Service: The server registers the blockchain as an RPC service and starts listening for RPC requests on port 18885.
When mining is enabled, the server performs the following steps:
- Set Miner Address: The specified miner address is set for mining rewards.
- Check Mempool: The server periodically checks the mempool for transactions.
- Mine Transactions: If transactions are found in the mempool, they are mined into a new block and added to the blockchain.
The server exposes the following RPC endpoints for client interactions:
- GetLatestBlock: Retrieves the latest block in the blockchain.
- GetTransactionHistory: Retrieves the transaction history for a specified address.
- GetBlockRPC: Retrieves block details by block ID.
- VerifyTX: Verifies a transaction by its ID.
- GetTX: Retrieves transaction details by its ID.
- GetCurrentHeight: Retrieves the current height of the blockchain.
- GetBalance: Retrieves the balance of a specified wallet address.
- GetAddressUTXOs: Retrieves the UTXOs for a specified wallet address.
- AddToMempool: Adds a transaction to the mempool for mining.
The server also has an optional HTTP interface to retrieve the latest block. Uncomment the HTTP route section in the code to enable it.
To start the HTTP server, uncomment the following lines in the code:
http.HandleFunc("/block/latest", handleGetLatestBlock(bc))
log.Println("Starting HTTP server on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
To get the latest block via HTTP:
curl http://localhost:8080/block/latest
This will return the latest block in the blockchain in JSON format.
The client allows you to perform various operations such as creating wallets, making transactions, checking balances, and more. Here are the detailed instructions for using the client:
Ensure that the server is running before you start the client. Refer to the Running the Server section for instructions.
Navigate to the client
folder and run the client:
cd client
go run main.go [OPTIONS]
--latestblock
: Get the latest block.--blockheight
: Get the current block height.--newWallet <seed>
: Create a new wallet using the provided seed phrase.--wallet
: Wallet-related operations. Use additional flags like--balance
,--address
, or--createtx
for specific actions.--balance <address>
: Check the balance of the specified wallet address.--address <address>
: Specify the wallet address for operations.--createtx <passphrase>
: Create a new transaction. Use additional flags like--to
,--amount
,--wallettype
, and--fees
.--to <address>
: Specify the recipient's address for the transaction.--amount <value>
: Specify the amount for the transaction.--fees <value>
: Specify the transaction fee.--decodetx <hex>
: Decode a transaction from its hex representation.--bech32
: Use Bech32 address format for wallet creation.--p2pkh
: Use P2PKH address format for wallet creation.--p2sh
: Use P2SH address format for wallet creation.--broadcast <hex>
: Broadcast a transaction using its hex representation.--verifytxID <txID>
: Verify a transaction by its ID.--getTrx <txID>
: Get transaction details by its ID.--getblock <blockID>
: Get block details by its ID.--trnxs <address>
: Get transaction history for a specified address.
go run main.go --latestblock
go run main.go --blockheight
go run main.go --newWallet="your-seed-phrase" --bech32
or
go run main.go --newWallet="your-seed-phrase" --p2pkh
or
go run main.go --newWallet="your-seed-phrase" --p2sh
go run main.go --wallet --balance="wallet-address"
go run main.go --wallet --createtx="your-seed-phrase" --to="recipient-address" --amount=10 --wallettype="p2pkh" --fees=0.0001
go run main.go --decodetx="transaction-hex"
go run main.go --broadcast="transaction-hex"
go run main.go --verifytxID="transaction-id"
go run main.go --getTrx="transaction-id"
go run main.go --getblock="block-id"
go run main.go --trnxs="wallet-address"
Once the server and client are running, you can interact with the node using the client to perform various operations such as creating wallets, making transactions, checking balances, and more.
Contributions are welcome! Please fork the repository and submit a pull request with your changes.
This project is licensed under the MIT License - see the LICENSE file for details.
For any inquiries or issues, please open an issue on the repository or contact me directly.
Feel free to adjust any details to better suit your preferences or additional information you might want to include.