mrtnetwork / bitcoin_base

A versatile library for Bitcoin, Dogecoin, Litecoin, Dash, BSV and Bitcoin Cash. Supports P2PK, P2PKH, P2SH, P2WPKH, P2WSH, Taproot, with advanced creation, signing, and spending capabilities.
BSD 3-Clause "New" or "Revised" License
18 stars 15 forks source link
bitcoin bitcoin-cash bitcoin-transaction bitcoin-wallet blockcypher-api bsv cashtokens dash dogecoin litecoin mempool multi-signature p2pk p2pkh p2sh p2sh-p2wsh p2tr p2wpkh schnorr-signatures

BITCOIN Dart Package

a comprehensive and versatile Dart library that provides robust support for various cryptocurrency transaction types. It is designed to meet your transaction needs for Bitcoin, Dogecoin, Litecoin, Dash, Bitcoin Cash and Bitcoin SV. The library offers features such as spending transactions, address management, Schnorr signatures for Bitcoin, BIP-39 mnemonic phrase generation, hierarchical deterministic (HD) wallet derivation, and Web3 Secret Storage Definition..

For BIP32 HD wallet, BIP39, and Secret storage definitions, please refer to the blockchain_utils package.

This package was inspired by the python-bitcoin-utils package and turned into Dart

Features

Supported Cryptocurrencies

Transaction Types

ss This comprehensive package provides robust support for a wide array of Bitcoin transaction types, encompassing the full spectrum of Bitcoin transaction capabilities. Whether you need to execute standard payments, facilitate complex multi-signature wallets, leverage Segregated Witness (SegWit) transactions for lower fees and enhanced scalability, or embrace the privacy and flexibility of Pay-to-Taproot (P2TR) transactions, this package has you covered. Additionally, it empowers users to engage in legacy transactions, create time-locked transactions, and harness the security of multisignature (multisig) transactions. With this package, you can seamlessly navigate the diverse landscape of Bitcoin transactions, ensuring your interactions with the Bitcoin network are secure, efficient, and tailored to your specific needs.

Tokens on Bitcoin Cash

CashTokens are digital assets that can be created and used on the global, decentralized Bitcoin Cash (BCH) network. These tokens can be issued by any person, organization, or decentralized application.

Create Transaction

Using this package, you can create a Bitcoin transaction in two ways: either through the BtcTransaction or the BitcoinTransactionBuilder class

Addresses

Addresses specific to Bitcoin Cash

Sign

Node Provider

We have integrated three APIs—Mempool, BlockCypher, and Electrum—into the plugin to facilitate network access. These APIs enable seamless retrieval of information such as unspent transactions (UTXO), network fees, sending transactions, receiving transaction details, and fetching account transactions.

EXAMPLES

Key and addresses

Transaction

In the example folder, you'll find various examples tailored for each supported network, including Bitcoin, Dogecoin, Litecoin, Bitcoin Cash, and Dash.

Node provider

I haven't implemented any specific HTTP service or socket service within this plugin. The reason is that different applications may use various plugins or methods to interact with network protocols. However, I have included numerous examples to demonstrate how Electrum and HTTP services can be utilized. You can leverage these examples as a reference to easily create services tailored to your application's specific needs. examples

  const network = BitcoinNetwork.mainnet;

  /// connect to electrum service with websocket
  /// please see `services_examples` folder for how to create electrum websocket service
  final service =
      await ElectrumSSLService.connect("testnet.aranguren.org:51002");

  /// create provider with service
  final provider = ElectrumApiProvider(service);

  final address = P2trAddress.fromAddress(address: ".....", network: network);

  /// Return the confirmed and unconfirmed balances of a script hash.
  final accountBalance = await provider
      .request(ElectrumGetScriptHashBalance(scriptHash: address.pubKeyHash()));

  /// Return an ordered list of UTXOs sent to a script hash.
  final accountUnspend = await provider
      .request(ElectrumScriptHashListUnspent(scriptHash: address.pubKeyHash()));

  /// Return the confirmed and unconfirmed history of a script hash.
  final accountHistory = await provider
      .request(ElectrumScriptHashGetHistory(scriptHash: address.pubKeyHash()));

  /// Broadcast a transaction to the network.
  final broadcastTransaction = await provider
      .request(ElectrumBroadCastTransaction(transactionRaw: "txDigest"));

  /// ....
  /// Define the blockchain network you want to work with, in this case, it's Bitcoin.
  const network = BitcoinNetwork.mainnet;

  /// see the example_service.dart for how to create a http service.
  final service = BitcoinApiService();

  /// Create an API provider instance for interacting with the BlockCypher API for the specified network.
  final api = ApiProvider.fromBlocCypher(network, service);

  /// Get the current network fee rate, which is essential for estimating transaction fees.
  final fee = await api.getNetworkFeeRate();

  /// Send a raw transaction represented by its transaction digest to the blockchain network.
  final transactionId = await api.sendRawTransaction("txDigest");

  /// Retrieve the Unspent Transaction Outputs (UTXOs) associated with a specific address.
  final utxo = await api.getAccountUtxo(address);

  /// Fetch information about a specific transaction using its transaction ID.
  /// For the Mempool API, use MempoolTransaction in the function template to receive the correct type.
  final transaction =
      await api.getTransaction<BlockCypherTransaction>(transactionId);

  /// Get a list of account transactions related to a specific address.
  /// For the Mempool API, use MempoolTransaction in the function template to receive the correct type.
  final accountTransactions =
      await api.getAccountTransactions<MempoolTransaction>('address');

Contributing

Contributions are welcome! Please follow these guidelines:

Feature requests and bugs

Please file feature requests and bugs in the issue tracker.