-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathProgram.cs
209 lines (171 loc) · 10.8 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright (c) 2014 - 2016 George Kimionis
// See the accompanying file LICENSE for the Software License Aggrement
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Reflection;
using BitcoinLib.Auxiliary;
using BitcoinLib.ExceptionHandling.Rpc;
using BitcoinLib.Responses;
using BitcoinLib.Services.Coins.Base;
using BitcoinLib.Services.Coins.Bitcoin;
namespace ConsoleClient
{
internal sealed class Program
{
// Read 'UseTestnet' flag from App.config instead of hardcoding
private static readonly bool UseTestnet = bool.Parse(ConfigurationManager.AppSettings["UseTestnet"]);
// Inject useTestnet setting into the BitcoinService
private static readonly ICoinService CoinService = new BitcoinService(useTestnet: UseTestnet);
private static void Main()
{
try
{
Console.Write("\n\nConnecting to {0} {1}Net via RPC at {2}...", CoinService.Parameters.CoinLongName, (CoinService.Parameters.UseTestnet ? "Test" : "Main"), CoinService.Parameters.SelectedDaemonUrl);
// Network difficulty
var networkDifficulty = CoinService.GetDifficulty();
Console.WriteLine("[OK]\n\n{0} Network Difficulty: {1}", CoinService.Parameters.CoinLongName, networkDifficulty.ToString("#,###", CultureInfo.InvariantCulture));
// Mining info
var miningInfo = CoinService.GetMiningInfo();
Console.WriteLine("[OK]\n\n{0} NetworkHashPS: {1}", CoinService.Parameters.CoinLongName, miningInfo.NetworkHashPS.ToString("#,###", CultureInfo.InvariantCulture));
// My balance
var myBalance = CoinService.GetBalance();
Console.WriteLine("\nMy balance: {0} {1}", myBalance, CoinService.Parameters.CoinShortName);
// Current block
Console.WriteLine("Current block: {0}",
CoinService.GetBlockCount().ToString("#,#", CultureInfo.InvariantCulture));
// Wallet state
Console.WriteLine("Wallet state: {0}", CoinService.IsWalletEncrypted() ? "Encrypted" : "Unencrypted");
// Keys and addresses
if (myBalance > 0)
{
// My non-empty addresses
Console.WriteLine("\n\nMy non-empty addresses:");
var myNonEmptyAddresses = CoinService.ListReceivedByAddress();
foreach (var address in myNonEmptyAddresses)
{
Console.WriteLine("\n--------------------------------------------------");
Console.WriteLine("Account: " + (string.IsNullOrWhiteSpace(address.Account) ? "(no label)" : address.Account));
Console.WriteLine("Address: " + address.Address);
Console.WriteLine("Amount: " + address.Amount);
Console.WriteLine("Confirmations: " + address.Confirmations);
Console.WriteLine("--------------------------------------------------");
}
// My private keys
if (bool.Parse(ConfigurationManager.AppSettings["ExtractMyPrivateKeys"]) && myNonEmptyAddresses.Count > 0 && CoinService.IsWalletEncrypted())
{
const short secondsToUnlockTheWallet = 30;
Console.Write("\nWill now unlock the wallet for " + secondsToUnlockTheWallet + ((secondsToUnlockTheWallet > 1) ? " seconds" : " second") + "...");
CoinService.WalletPassphrase(CoinService.Parameters.WalletPassword, secondsToUnlockTheWallet);
Console.WriteLine("[OK]\n\nMy private keys for non-empty addresses:\n");
foreach (var address in myNonEmptyAddresses)
{
Console.WriteLine("Private Key for address " + address.Address + ": " + CoinService.DumpPrivKey(address.Address));
}
Console.Write("\nLocking wallet...");
CoinService.WalletLock();
Console.WriteLine("[OK]");
}
// My transactions
Console.WriteLine("\n\nMy transactions: ");
var myTransactions = CoinService.ListTransactions(null, int.MaxValue, 0);
foreach (var transaction in myTransactions)
{
Console.WriteLine("\n---------------------------------------------------------------------------");
Console.WriteLine("Account: " + (string.IsNullOrWhiteSpace(transaction.Account) ? "(no label)" : transaction.Account));
Console.WriteLine("Address: " + transaction.Address);
Console.WriteLine("Category: " + transaction.Category);
Console.WriteLine("Amount: " + transaction.Amount);
Console.WriteLine("Fee: " + transaction.Fee);
Console.WriteLine("Confirmations: " + transaction.Confirmations);
Console.WriteLine("BlockHash: " + transaction.BlockHash);
Console.WriteLine("BlockIndex: " + transaction.BlockIndex);
Console.WriteLine("BlockTime: " + transaction.BlockTime + " - " + UnixTime.UnixTimeToDateTime(transaction.BlockTime));
Console.WriteLine("TxId: " + transaction.TxId);
Console.WriteLine("Time: " + transaction.Time + " - " + UnixTime.UnixTimeToDateTime(transaction.Time));
Console.WriteLine("TimeReceived: " + transaction.TimeReceived + " - " + UnixTime.UnixTimeToDateTime(transaction.TimeReceived));
if (!string.IsNullOrWhiteSpace(transaction.Comment))
{
Console.WriteLine("Comment: " + transaction.Comment);
}
if (!string.IsNullOrWhiteSpace(transaction.OtherAccount))
{
Console.WriteLine("Other Account: " + transaction.OtherAccount);
}
if (transaction.WalletConflicts != null && transaction.WalletConflicts.Any())
{
Console.Write("Conflicted Transactions: ");
foreach (var conflictedTxId in transaction.WalletConflicts)
{
Console.Write(conflictedTxId + " ");
}
Console.WriteLine();
}
Console.WriteLine("---------------------------------------------------------------------------");
}
// Transaction Details
Console.WriteLine("\n\nMy transactions' details:");
foreach (var transaction in myTransactions)
{
// Move transactions don't have a txId, which this logic fails for
if (transaction.Category == "move")
{
continue;
}
var localWalletTransaction = CoinService.GetTransaction(transaction.TxId);
IEnumerable<PropertyInfo> localWalletTrasactionProperties = localWalletTransaction.GetType().GetProperties();
IList<GetTransactionResponseDetails> localWalletTransactionDetailsList = localWalletTransaction.Details.ToList();
Console.WriteLine("\nTransaction\n-----------");
foreach (var propertyInfo in localWalletTrasactionProperties)
{
var propertyInfoName = propertyInfo.Name;
if (propertyInfoName != "Details" && propertyInfoName != "WalletConflicts")
{
Console.WriteLine(propertyInfoName + ": " + propertyInfo.GetValue(localWalletTransaction, null));
}
}
foreach (var details in localWalletTransactionDetailsList)
{
IEnumerable<PropertyInfo> detailsProperties = details.GetType().GetProperties();
Console.WriteLine("\nTransaction details " + (localWalletTransactionDetailsList.IndexOf(details) + 1) + " of total " + localWalletTransactionDetailsList.Count + "\n--------------------------------");
foreach (var propertyInfo in detailsProperties)
{
Console.WriteLine(propertyInfo.Name + ": " + propertyInfo.GetValue(details, null));
}
}
}
// Unspent transactions
Console.WriteLine("\nMy unspent transactions:");
var unspentList = CoinService.ListUnspent();
foreach (var unspentResponse in unspentList)
{
IEnumerable<PropertyInfo> detailsProperties = unspentResponse.GetType().GetProperties();
Console.WriteLine("\nUnspent transaction " + (unspentList.IndexOf(unspentResponse) + 1) + " of " + unspentList.Count + "\n--------------------------------");
foreach (var propertyInfo in detailsProperties)
{
Console.WriteLine(propertyInfo.Name + " : " + propertyInfo.GetValue(unspentResponse, null));
}
}
}
Console.ReadLine();
}
catch (RpcInternalServerErrorException exception)
{
var errorCode = 0;
var errorMessage = string.Empty;
if (exception.RpcErrorCode.GetHashCode() != 0)
{
errorCode = exception.RpcErrorCode.GetHashCode();
errorMessage = exception.RpcErrorCode.ToString();
}
Console.WriteLine("[Failed] {0} {1} {2}", exception.Message, errorCode != 0 ? "Error code: " + errorCode : string.Empty, !string.IsNullOrWhiteSpace(errorMessage) ? errorMessage : string.Empty);
}
catch (Exception exception)
{
Console.WriteLine("[Failed]\n\nPlease check your configuration and make sure that the daemon is up and running and that it is synchronized. \n\nException: " + exception);
}
}
}
}