Skip to content

Add Bitcoin Blockchain Integration (Basic) #29

Description

@kelly-musk

Overview

This issue involves adding basic Bitcoin blockchain support to enable operations like connecting to a Bitcoin node, validating addresses, checking balances, and broadcasting transactions. This is part of the multi-chain expansion, allowing Aframp to handle Bitcoin alongside Stellar and Ethereum for broader crypto support. Focus on core RPC interactions without advanced features like multi-sig or Lightning. This will integrate into the unified blockchain service (Issue #15) and support future onramp/offramp for BTC.

Use a reliable Bitcoin RPC client to interact with nodes (testnet/mainnet), ensuring compatibility with AFRI if bridged to Bitcoin in the future. Emphasize security, efficiency, and error handling to prevent issues like invalid transactions or network failures. This complements Ethereum integration (Issue #13) and builds on Phase 1 infrastructure.

Key Requirements

Detailed Tasks

  1. Bitcoin RPC Connection:

    • Function: async fn init_client(url: &str, auth: &Auth) -> Result<BitcoinClient, BtcError>
    • Set up RPC client with username/password or cookie auth.
    • Health check: Call getblockchaininfo to verify connection and network.
    • Support testnet (e.g., signet/regtest for dev).
  2. Address Validation:

    • Function: fn validate_address(address: &str) -> Result<bool, BtcError>
    • Check if address is valid for the network (P2PKH, P2SH, Bech32/SegWit).
    • Use crate utilities or regex for format; optionally query RPC for validateaddress.
  3. Balance Checking:

    • Function: async fn get_balance(address: &str) -> Result<Satoshi, BtcError>
    • Scan UTXOs or use getreceivedbyaddress if wallet-enabled; for non-custodial, prefer scantxoutset or external indexing if needed.
    • Convert to BTC decimals; cache in Redis with TTL (e.g., 1 minute).
  4. Transaction Broadcasting:

    • Function: async fn broadcast_tx(raw_tx: &str) -> Result<TxId, BtcError>
    • Send raw hex transaction via sendrawtransaction.
    • Validate tx format before broadcast; handle errors like low fee or double-spend.

Acceptance Criteria

  • Connect to a Bitcoin testnet node, validate a sample address, fetch its balance, and broadcast a mock/test transaction.
  • Handle errors: Invalid address (false/error), no connection (retry/fail), broadcast rejection (e.g., insufficient fee).
  • Performance: RPC calls <1s; efficient for high-volume checks.
  • Logs: Structured entries, e.g., "Bitcoin balance [amount] BTC for address [addr]; tx [id] broadcasted".
  • Documentation: Inline code comments; update multi-chain guide in Issue Implement POST /api/offramp/initiate Endpoint #62.

Estimated Effort

4-5 hours: 1h connection/setup, 1h validation/balance, 1h broadcasting, 1h testing/debugging.

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Stellar WaveIssues in the Stellar wave program

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions