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:
using BitNav;
The Bitcoin
class provides functionalities related to Bitcoin transactions and addresses.
public Bitcoin(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.
public Transaction(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.
public TransactionManager()
-
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.
public event EventHandler<TransactionEventArgs> TransactionReceived;
// Subscribe to the TransactionReceived event
transaction.TransactionReceived += HandleTransactionReceived;
// Event handler method
private void HandleTransactionReceived(object sender, TransactionEventArgs e)
{
Transaction receivedTransaction = e.Transaction;
// Add custom logic to handle the received transaction
}
The TransactionConfirmed
event is triggered when the transaction is confirmed on the blockchain.
public event EventHandler<TransactionEventArgs> TransactionConfirmed;
// Subscribe to the TransactionConfirmed event
transaction.TransactionConfirmed += HandleTransactionConfirmed;
// Event handler method
private void HandleTransactionConfirmed(object sender, TransactionEventArgs e)
{
Transaction confirmedTransaction = 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 key
Bitcoin bitcoin = new Bitcoin("your_private_key");
// Generate a new Bitcoin address
string recipientAddress = bitcoin.GetAddress();
// Create a transaction manager
TransactionManager manager = new TransactionManager();
// Create a new Bitcoin transaction
Transaction transaction = manager.CreateTransaction(bitcoin, 0.1, recipientAddress);
// Check if the transaction has been received
bool received = transaction.checkTransactionReceived();
if (received)
{
// Transaction has been received, check if it's confirmed
bool confirmed = 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.