|
1 | | -# BitNav |
2 | | -.NET 6 library for accepting and managing crypto payments |
| 1 | + |
| 2 | +# BitNav - Cryptocurrency Payment Library |
| 3 | + |
| 4 | +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. |
| 5 | + |
| 6 | +## Table of Contents |
| 7 | + |
| 8 | +- [Installation](#installation) |
| 9 | +- [Usage](#usage) |
| 10 | + - [Bitcoin Class](#bitcoin-class) |
| 11 | + - [Transaction Class](#transaction-class) |
| 12 | + - [TransactionManager Class](#transactionManager-class) |
| 13 | +- [Events](#events) |
| 14 | + - [TransactionReceived Event](#transactionreceived-event) |
| 15 | + - [TransactionConfirmed Event](#transactionconfirmed-event) |
| 16 | +- [Examples](#examples) |
| 17 | + |
| 18 | + |
| 19 | +---------- |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +To use BitNav in your C# project, follow these steps: |
| 24 | + |
| 25 | +1. Download the latest release from the [GitHub repository](https://github.com/km/BitNav). |
| 26 | +2. Install the dependency [NBitcoin](https://github.com/MetacoSA/NBitcoin) |
| 27 | +3. Add the downloaded DLL file to your project's references. |
| 28 | +4. Import the necessary namespaces: |
| 29 | +```csharp |
| 30 | +using BitNav; |
| 31 | +``` |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +### Bitcoin Class |
| 36 | + |
| 37 | +The `Bitcoin` class provides functionalities related to Bitcoin transactions and addresses. |
| 38 | + |
| 39 | +#### Constructor |
| 40 | +```csharp |
| 41 | +public Bitcoin(string privateKey, string givenAddress = null) |
| 42 | +``` |
| 43 | +- `privateKey`: The private key associated with the Bitcoin address. |
| 44 | +- `givenAddress`: (Optional) A specific Bitcoin address. If not provided, a new address will be generated using the private key. |
| 45 | + |
| 46 | +#### Methods |
| 47 | + |
| 48 | +- `string GetAddress()`: Returns the Bitcoin address associated with the instance. |
| 49 | + |
| 50 | +- `static long ConvertBitcoinToSatoshis(double bitcoinAmount)`: Converts a Bitcoin amount to satoshis. |
| 51 | +### Transaction Class |
| 52 | + |
| 53 | +The `Transaction` class handles cryptocurrency transactions. |
| 54 | + |
| 55 | +#### Constructor |
| 56 | +```csharp |
| 57 | +public Transaction(object coin, double amount, string address) |
| 58 | +``` |
| 59 | +- `coin`: The cryptocurrency object (e.g., Bitcoin) associated with the transaction. |
| 60 | +- `amount`: The amount of cryptocurrency to be received. |
| 61 | +- `address`: Address to receive the cryptocurrency. |
| 62 | + |
| 63 | +#### Methods |
| 64 | + |
| 65 | +- `bool checkTransactionReceived()`: Checks if the transaction has been received. |
| 66 | + |
| 67 | +- `bool checkTransactionConfirmed()`: Checks if the transaction has been confirmed. |
| 68 | + |
| 69 | +- `string getTransactionHash()`: Returns the transaction hash. |
| 70 | + |
| 71 | +- `string getReceiverAddress()`: Returns the recipient's address. |
| 72 | + |
| 73 | +- `long getCreationTime()`: Returns the transaction's creation time in Unix timestamp. |
| 74 | + |
| 75 | +- `long getReceivedTime()`: Returns the time when the transaction was received in Unix timestamp. |
| 76 | + |
| 77 | +- `long getConfirmationTime()`: Returns the time when the transaction was confirmed in Unix timestamp. |
| 78 | + |
| 79 | +- `double getAmount()`: Returns the amount of cryptocurrency involved in the transaction. |
| 80 | + |
| 81 | +- `object getCoin()`: Returns the cryptocurrency object associated with the transaction. |
| 82 | + |
| 83 | +### TransactionManager Class |
| 84 | + |
| 85 | +The `TransactionManager` class manages a list of transactions, and will check if they're received or confirmed every 2 minutes in the background. |
| 86 | + |
| 87 | +#### Constructor |
| 88 | +```csharp |
| 89 | +public TransactionManager() |
| 90 | +``` |
| 91 | +#### Methods |
| 92 | + |
| 93 | +- `Transaction CreateTransaction(object coin, double amount, string address)`: Creates a new transaction. |
| 94 | + |
| 95 | +- `void AddTransaction(Transaction transaction)`: Adds an existing transaction to the manager. |
| 96 | + |
| 97 | +- `void RemoveTransaction(Transaction transaction)`: Removes a transaction from the manager. |
| 98 | + |
| 99 | +- `List<Transaction> GetTransactions()`: Returns the list of all transactions. |
| 100 | + |
| 101 | +## Events |
| 102 | +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. |
| 103 | +### TransactionReceived Event |
| 104 | + |
| 105 | +The `TransactionReceived` event is triggered when the transaction is received to the address. |
| 106 | + |
| 107 | +#### Syntax |
| 108 | + |
| 109 | +```csharp |
| 110 | +public event EventHandler<TransactionEventArgs> TransactionReceived; |
| 111 | +``` |
| 112 | +#### Usage |
| 113 | +```csharp |
| 114 | +// Subscribe to the TransactionReceived event |
| 115 | +transaction.TransactionReceived += HandleTransactionReceived; |
| 116 | + |
| 117 | +// Event handler method |
| 118 | +private void HandleTransactionReceived(object sender, TransactionEventArgs e) |
| 119 | +{ |
| 120 | + Transaction receivedTransaction = e.Transaction; |
| 121 | + // Add custom logic to handle the received transaction |
| 122 | +} |
| 123 | +``` |
| 124 | +### TransactionConfirmed Event |
| 125 | + |
| 126 | +The `TransactionConfirmed` event is triggered when the transaction is confirmed on the blockchain. |
| 127 | + |
| 128 | +#### Syntax |
| 129 | +```csharp |
| 130 | +public event EventHandler<TransactionEventArgs> TransactionConfirmed; |
| 131 | +``` |
| 132 | +#### Usage |
| 133 | +```csharp |
| 134 | +// Subscribe to the TransactionConfirmed event |
| 135 | +transaction.TransactionConfirmed += HandleTransactionConfirmed; |
| 136 | + |
| 137 | +// Event handler method |
| 138 | +private void HandleTransactionConfirmed(object sender, TransactionEventArgs e) |
| 139 | +{ |
| 140 | + Transaction confirmedTransaction = e.Transaction; |
| 141 | + // Add custom logic to handle the confirmed transaction |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +## Examples |
| 146 | + |
| 147 | +Here's an example of how to use BitNav to create and manage a Bitcoin transaction: |
| 148 | +```csharp |
| 149 | +// Initialize the Bitcoin object with a private key |
| 150 | +Bitcoin bitcoin = new Bitcoin("your_private_key"); |
| 151 | + |
| 152 | +// Generate a new Bitcoin address |
| 153 | +string recipientAddress = bitcoin.GetAddress(); |
| 154 | + |
| 155 | +// Create a transaction manager |
| 156 | +TransactionManager manager = new TransactionManager(); |
| 157 | + |
| 158 | +// Create a new Bitcoin transaction |
| 159 | +Transaction transaction = manager.CreateTransaction(bitcoin, 0.1, recipientAddress); |
| 160 | + |
| 161 | +// Check if the transaction has been received |
| 162 | +bool received = transaction.checkTransactionReceived(); |
| 163 | + |
| 164 | +if (received) |
| 165 | +{ |
| 166 | + // Transaction has been received, check if it's confirmed |
| 167 | + bool confirmed = transaction.checkTransactionConfirmed(); |
| 168 | + |
| 169 | + if (confirmed) |
| 170 | + { |
| 171 | + Console.WriteLine("Transaction confirmed!"); |
| 172 | + } |
| 173 | + else |
| 174 | + { |
| 175 | + Console.WriteLine("Transaction received but not yet confirmed."); |
| 176 | + } |
| 177 | +} |
| 178 | +else |
| 179 | +{ |
| 180 | + Console.WriteLine("Transaction not yet received."); |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +--- |
| 185 | + |
| 186 | +## Acknowledgements |
| 187 | + |
| 188 | +This project is built using the [NBitcoin](https://github.com/MetacoSA/NBitcoin) library. Additionally, it utilizes the [BlockCypher](https://www.blockcypher.com/) API for blockchain interaction. |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +*Note: NBitcoin is a comprehensive Bitcoin library for the .NET platform, and BlockCypher provides a range of blockchain services and APIs.* |
0 commit comments