|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Globalization; |
| 5 | +using System.Linq; |
| 6 | +using System.Net; |
| 7 | +using System.Text; |
| 8 | +using System.Text.Json; |
| 9 | +using System.Text.Json.Nodes; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using System.Transactions; |
| 12 | + |
| 13 | +namespace BitNav |
| 14 | +{ |
| 15 | + public class Transaction |
| 16 | + { |
| 17 | + private object coin; |
| 18 | + private int amount; |
| 19 | + private string address; |
| 20 | + private string transactionHash; |
| 21 | + private long creationTime; |
| 22 | + private long receivedTime; |
| 23 | + private long confirmationTime; |
| 24 | + private WebClient wc; |
| 25 | + public event EventHandler<TransactionEventArgs> TransactionReceived; |
| 26 | + public event EventHandler<TransactionEventArgs> TransactionConfirmed; |
| 27 | + |
| 28 | + public Transaction(object coin, int amount, string address) |
| 29 | + { |
| 30 | + this.coin = coin; |
| 31 | + this.amount = amount; |
| 32 | + this.address = address; |
| 33 | + this.transactionHash = null; |
| 34 | + this.wc = new WebClient(); |
| 35 | + this.creationTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); |
| 36 | + } |
| 37 | + |
| 38 | + //returns true if transaction is received and sets the transaction hash |
| 39 | + public bool checkTransactionReceived() |
| 40 | + { |
| 41 | + if (transactionHash != null) |
| 42 | + { |
| 43 | + return true; |
| 44 | + } |
| 45 | + string url = ""; |
| 46 | + switch(coin) |
| 47 | + { |
| 48 | + case Bitcoin: |
| 49 | + url = "https://api.blockcypher.com/v1/btc/main/addrs/"+address+"/full"; |
| 50 | + break; |
| 51 | + default: |
| 52 | + throw new Exception("Invalid coin type"); |
| 53 | + break; |
| 54 | + } |
| 55 | + |
| 56 | + string json = wc.DownloadString(url); |
| 57 | + JsonDocument doc = JsonDocument.Parse(json); |
| 58 | + JsonElement root = doc.RootElement; |
| 59 | + JsonElement txs = root.GetProperty("txrefs"); |
| 60 | + |
| 61 | + foreach (JsonElement tx in txs.EnumerateArray()) |
| 62 | + { |
| 63 | + |
| 64 | + string dateString = tx.GetProperty("received").GetString(); |
| 65 | + DateTime dateTime = DateTime.ParseExact(dateString, "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); |
| 66 | + long unixTime = ((DateTimeOffset)dateTime).ToUnixTimeSeconds(); |
| 67 | + if (unixTime >= creationTime) |
| 68 | + { |
| 69 | + JsonElement txAmount = tx.GetProperty("outputs"); |
| 70 | + foreach (JsonElement txAmounts in txAmount.EnumerateArray()) |
| 71 | + { |
| 72 | + Console.WriteLine(txAmounts.GetProperty("addresses").GetString()); |
| 73 | + if (txAmounts.GetProperty("addresses").GetString() == address) |
| 74 | + { |
| 75 | + if (checkEqualWithMargin(amount, txAmounts.GetProperty("value").GetInt32(), 0)) |
| 76 | + { |
| 77 | + transactionHash = tx.GetProperty("tx_hash").GetString(); |
| 78 | + receivedTime = unixTime; |
| 79 | + OnTransactionReceived(); |
| 80 | + //checks if confirmation time exists and parses it if it does |
| 81 | + try |
| 82 | + { |
| 83 | + string confirmedString = tx.GetProperty("confirmed").GetString(); |
| 84 | + DateTime confirmeDateTime = DateTime.ParseExact(confirmedString, "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); |
| 85 | + this.confirmationTime = ((DateTimeOffset)confirmeDateTime).ToUnixTimeSeconds(); |
| 86 | + OnTransactionConfirmed(); |
| 87 | + } |
| 88 | + catch |
| 89 | + { |
| 90 | + } |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + public bool checkTransactionConfirmed() |
| 107 | + { |
| 108 | + if (confirmationTime != 0) |
| 109 | + { |
| 110 | + return true; |
| 111 | + } |
| 112 | + else if (transactionHash == null) |
| 113 | + { |
| 114 | + checkTransactionReceived(); |
| 115 | + |
| 116 | + return confirmationTime != 0; |
| 117 | + } |
| 118 | + string url = ""; |
| 119 | + switch (coin) |
| 120 | + { |
| 121 | + case Bitcoin: |
| 122 | + url = "https://api.blockcypher.com/v1/btc/main/txs/" + transactionHash; |
| 123 | + break; |
| 124 | + default: |
| 125 | + throw new Exception("Invalid coin type"); |
| 126 | + break; |
| 127 | + } |
| 128 | + |
| 129 | + string json = wc.DownloadString(url); |
| 130 | + JsonDocument doc = JsonDocument.Parse(json); |
| 131 | + JsonElement root = doc.RootElement; |
| 132 | + try |
| 133 | + { |
| 134 | + string confirmedString = root.GetProperty("confirmed").GetString(); |
| 135 | + DateTime confirmeDateTime = DateTime.ParseExact(confirmedString, "yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); |
| 136 | + this.confirmationTime = ((DateTimeOffset)confirmeDateTime).ToUnixTimeSeconds(); |
| 137 | + OnTransactionConfirmed(); |
| 138 | + return true; |
| 139 | + } |
| 140 | + catch (Exception e) |
| 141 | + { |
| 142 | + } |
| 143 | + |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + private bool checkEqualWithMargin(int a, int b, int margin) |
| 148 | + { |
| 149 | + return a >= b - margin && a <= b + margin; |
| 150 | + } |
| 151 | + //returns null if transaction is not received or the transaction hash if it is |
| 152 | + public string getTransactionHash() |
| 153 | + { |
| 154 | + return transactionHash; |
| 155 | + } |
| 156 | + |
| 157 | + public string getReceiverAddress() |
| 158 | + { |
| 159 | + return address; |
| 160 | + } |
| 161 | + |
| 162 | + public long getCreationTime() |
| 163 | + { |
| 164 | + return creationTime; |
| 165 | + } |
| 166 | + |
| 167 | + public long getReceivedTime() |
| 168 | + { |
| 169 | + return receivedTime; |
| 170 | + } |
| 171 | + |
| 172 | + public long getConfirmationTime() |
| 173 | + { |
| 174 | + return confirmationTime; |
| 175 | + } |
| 176 | + public int getAmount() |
| 177 | + { |
| 178 | + return amount; |
| 179 | + } |
| 180 | + public object getCoin() |
| 181 | + { |
| 182 | + return coin; |
| 183 | + } |
| 184 | + |
| 185 | + private void OnTransactionReceived() |
| 186 | + { |
| 187 | + TransactionReceived?.Invoke(this, new TransactionEventArgs(this)); |
| 188 | + } |
| 189 | + private void OnTransactionConfirmed() |
| 190 | + { |
| 191 | + TransactionConfirmed?.Invoke(this, new TransactionEventArgs(this)); |
| 192 | + } |
| 193 | + |
| 194 | + } |
| 195 | +} |
0 commit comments