This project enables the creation, signing, and broadcasting of Bitcoin testnet transactions using a Securosys PrimusHSM and a Blockstream-compatible node API. It securely stores and uses cryptographic keys inside a hardware security module (HSM) and builds fully-signed SegWit transactions for testnet.
- ✅ SegWit (P2WPKH) transaction creation
- 🔐 HSM-based private key management and ECDSA signing
- 📡 Transaction broadcasting to Blockstream’s testnet node
- ⚖️ UTXO handling, fee control, and change output logic
- 🧪 Built for Bitcoin testnet network
- Go to Securosys CloudHSM
- Register and start a 90-day free trial
- Obtain your API access token
-
Go to Blockstream Dashboard
-
Create an account and log in
-
Register a new application to get:
NODE_CLIENT_ID
NODE_CLIENT_SECRET
-
These will allow authenticated access to the testnet API and UTXO services
Sample Curl-requests:
ACCESS_TOKEN=
curl \
--request GET \
--location "https://enterprise.blockstream.info/testnet/api/address/tb1q680p9239dsze0jwfm8ccf0zaepu9nk5jansrns/utxo" \
--header "Authorization: Bearer ${ACCESS_TOKEN}"
Create a .env
file based on this template:
# HSM Configuration
TSB_API_URL=https://sbx-rest-api.cloudshsm.com/v1/
TSB_KEY_LABEL=btc-test-key
TSB_ACCESS_TOKEN=your_securosys_bearer_access_token_here
# Blockstream Node API
NODE_AUTH_URL=https://login.blockstream.com/realms/blockstream-public/protocol/openid-connect/token
NODE_API_URL=https://enterprise.blockstream.info/testnet/api
NODE_CLIENT_ID=your_blockstream_client_id
NODE_CLIENT_SECRET=your_blockstream_client_secret
# Transaction Settings
TO_ADDRESS_NEW=tb1qlj64u6fqutr0xue85kl55fx0gt4m4urun25p7q
You can fund your address using: https://testnet-faucet.com/send.php or https://bitcoinfaucet.uo1.net/send.php To send your testnet coins back, simply initiate a transaction from your testnet wallet to the following BTC address: tb1qlj64u6fqutr0xue85kl55fx0gt4m4urun25p7q
- Python 3.8+
- Dependencies (install via
requirements.txt
):
pip install -r requirements.txt
Run the transaction script:
python main.py
or using streamlit:
streamlit run streamlit_app.py
This script will:
- Retrieve or create a key from your HSM
- Derive and verify your public key and sender address
- Fetch UTXOs from Blockstream’s API
- Build a signed SegWit transaction
- (Optionally) Broadcast the signed transaction to the network
project/
├── main.py # Main script
├── hsm/ # HSM interface
│ ├── securosys_rest_api.py
│ └── helper.py
├── blockstream/ # Blockstream API handlers
│ └── blockstream_node.py
├── .env-template # Example configuration
├── README.md
- This project is for testnet only – not for production Bitcoin use.
- Ensure recipient address (
TO_ADDRESS_NEW
) is a valid P2WPKH testnet address (tb1...
) - Fees and amounts are configurable in
main.py
- Uses Securosys HSM for secure private key storage and signing
- No private key material leaves the HSM
- Secure API tokens required for both HSM and transaction broadcasting