BitNav is a .NET 6 library for handling and managing cryptocurrency payments, initially focusing on Bitcoin. This library provides a set of classes and methods to facilitate the creation, monitoring, and confirmation of transactions.
To use BitNav in your C# project, follow these steps:
- Download the latest release from the GitHub repository.
- Install the dependency NBitcoin
- Add the downloaded DLL file to your project's references.
- Import the necessary namespaces:
usingBitNav;The Bitcoin class provides functionalities related to Bitcoin transactions and addresses.
publicBitcoin(string privateKey,string givenAddress =null)privateKey: The private key associated with the Bitcoin address.givenAddress: (Optional) A specific Bitcoin address. If not provided, a new address will be generated using the private key.
string GetAddress(): Returns the Bitcoin address associated with the instance.static long ConvertBitcoinToSatoshis(double bitcoinAmount): Converts a Bitcoin amount to satoshis.
The Transaction class handles cryptocurrency transactions.
publicTransaction(object coin,double amount,string address)coin: The cryptocurrency object (e.g., Bitcoin) associated with the transaction.amount: The amount of cryptocurrency to be received.address: Address to receive the cryptocurrency.
bool checkTransactionReceived(): Checks if the transaction has been received.bool checkTransactionConfirmed(): Checks if the transaction has been confirmed.string getTransactionHash(): Returns the transaction hash.string getReceiverAddress(): Returns the recipient's address.long getCreationTime(): Returns the transaction's creation time in Unix timestamp.long getReceivedTime(): Returns the time when the transaction was received in Unix timestamp.long getConfirmationTime(): Returns the time when the transaction was confirmed in Unix timestamp.double getAmount(): Returns the amount of cryptocurrency involved in the transaction.object getCoin(): Returns the cryptocurrency object associated with the transaction.
The TransactionManager class manages a list of transactions, and will check if they're received or confirmed every 2 minutes in the background.
publicTransactionManager()Transaction CreateTransaction(object coin, double amount, string address): Creates a new transaction.void AddTransaction(Transaction transaction): Adds an existing transaction to the manager.void RemoveTransaction(Transaction transaction): Removes a transaction from the manager.List<Transaction> GetTransactions(): Returns the list of all transactions.
Each instance of the Transaction.cs object is equipped with both TransactionReceived and TransactionConfirmed events, which can be subscribed to. These events are automatically triggered when a TransactionManager is active in the background, and the corresponding Transaction is managed within it.
The TransactionReceived event is triggered when the transaction is received to the address.
publiceventEventHandler<TransactionEventArgs> TransactionReceived;// Subscribe to the TransactionReceived eventtransaction.TransactionReceived+=HandleTransactionReceived;// Event handler methodprivatevoidHandleTransactionReceived(objectsender,TransactionEventArgse){TransactionreceivedTransaction=e.Transaction;// Add custom logic to handle the received transaction}The TransactionConfirmed event is triggered when the transaction is confirmed on the blockchain.
publiceventEventHandler<TransactionEventArgs> TransactionConfirmed;// Subscribe to the TransactionConfirmed eventtransaction.TransactionConfirmed+=HandleTransactionConfirmed;// Event handler methodprivatevoidHandleTransactionConfirmed(objectsender,TransactionEventArgse){TransactionconfirmedTransaction=e.Transaction;// Add custom logic to handle the confirmed transaction}Here's an example of how to use BitNav to create and manage a Bitcoin transaction:
// Initialize the Bitcoin object with a private keyBitcoinbitcoin=newBitcoin("your_private_key");// Generate a new Bitcoin addressstringrecipientAddress=bitcoin.GetAddress();// Create a transaction managerTransactionManagermanager=newTransactionManager();// Create a new Bitcoin transactionTransactiontransaction=manager.CreateTransaction(bitcoin,0.1,recipientAddress);// Check if the transaction has been receivedboolreceived=transaction.checkTransactionReceived();if(received){// Transaction has been received, check if it's confirmedboolconfirmed=transaction.checkTransactionConfirmed();if(confirmed){Console.WriteLine("Transaction confirmed!");}else{Console.WriteLine("Transaction received but not yet confirmed.");}}else{Console.WriteLine("Transaction not yet received.");}This project is built using the NBitcoin library. Additionally, it utilizes the BlockCypher API for blockchain interaction.
Note: NBitcoin is a comprehensive Bitcoin library for the .NET platform, and BlockCypher provides a range of blockchain services and APIs.