This repository was archived by the owner on Aug 9, 2018. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
131 lines (114 loc) · 6.46 KB
/
Copy pathProgram.cs
File metadata and controls
131 lines (114 loc) · 6.46 KB
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
usingSystem;
usingSystem.DirectoryServices.AccountManagement;
usingSystem.Linq;
usingSystem.Threading.Tasks;
usingKeepKeySharp.Contracts;
usingNBitcoin;
namespaceKeepKeySharp.Console
{
classProgram
{
privateUserPrincipal_currentUser;
staticvoidMain(string[]args)
{
varp=newProgram();
p.Run().Wait();
}
privateasyncTaskRun()
{
_currentUser=UserPrincipal.Current;
System.Console.WriteLine("Welcome to the KeepKey C# example console!");
System.Console.WriteLine("Attempting to find KeepKey...");
using(varkk=newKeepKeyDevice())
{
kk.Connected+=KkOnConnected;
kk.Disconnected+=KkOnDisconnected;
if(!kk.TryOpenDevice())
{
System.Console.WriteLine();
System.Console.WriteLine("No KeepKey connected. Please connect KeepKey to the computer NOW. (Leave the console running)");
awaitkk.WaitForKeepKeyConnectionAsync();
}
System.Console.WriteLine();
System.Console.WriteLine("Getting features...");
// Step 1. Initialize the device and get a list of it's features
varfeatures=kk.Initialize();
// Print features to console - ouch!
System.Console.WriteLine("Vendor: {0}",features.Vendor);
System.Console.WriteLine("DeviceId: {0}",features.DeviceId);
System.Console.WriteLine("Label: {0}",features.Label);
System.Console.WriteLine("Version: {0}.{1}.{2}",features.MajorVersion.GetValueOrDefault(),features.MinorVersion.GetValueOrDefault(),features.PatchVersion.GetValueOrDefault());
System.Console.WriteLine("Coins: {0}",features.Coins.Select(c =>$"{c.CoinName} ({c.CoinShortcut})").Aggregate((c,n)=>$"{c}, {n}"));
if(features.Policies.Any())System.Console.WriteLine("Policies: {0}",features.Policies.Select(p =>p.PolicyName).Aggregate((c,n)=>$"{c}, {n}"));
System.Console.Write("Protection: ");
varproPin=features.PinProtection.GetValueOrDefault();
varproPass=features.PassphraseProtection.GetValueOrDefault();
if(proPin&&proPass)System.Console.WriteLine("Pin, Passphrase");
elseif(proPin&&!proPass)System.Console.WriteLine("Pin ONLY");
elseif(!proPin&&proPass)System.Console.WriteLine("Passphrase ONLY");
elseSystem.Console.WriteLine("Neither!");
System.Console.Write("Cached: ");
varcchPin=features.PinCached.GetValueOrDefault();
varcchPass=features.PassphraseCached.GetValueOrDefault();
if(cchPin&&cchPass)System.Console.WriteLine("Pin, Passphrase");
elseif(cchPin&&!cchPass)System.Console.WriteLine("Pin");
elseif(!cchPin&&cchPass)System.Console.WriteLine("Passphrase");
elseSystem.Console.WriteLine("[nothing]");
// Step 2. Have some fun with Ping!
System.Console.WriteLine();
System.Console.WriteLine("Pinging device... (look at it, hold the button)");
varname=_currentUser.DisplayName;// fullname
if(name.IndexOf(' ')!=-1)name=name.Split(' ')[0].Trim();// firstname
varpingReply=kk.Ping($"Hello {name}! Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia quis mauris volutpat scelerisque.",true);
System.Console.WriteLine(pingReply);
// Step 3a. Getting the public key MIGHT require the Pin to be entered so lets define a function that allows it be entered via the console
varpinChallengeFunction=newFunc<PinMatrixRequestType?,string>(t =>
{
System.Console.WriteLine();
System.Console.WriteLine("PIN CHALLENGE!!");
System.Console.WriteLine("┌───┬───┬───┐");
System.Console.WriteLine("│ 7 │ 8 │ 9 │");
System.Console.WriteLine("├───┼───┼───┤");
System.Console.WriteLine("│ 4 │ 5 │ 6 │");
System.Console.WriteLine("├───┼───┼───┤");
System.Console.WriteLine("│ 1 │ 2 │ 3 │");
System.Console.WriteLine("└───┴───┴───┘");
System.Console.WriteLine("Match the device screen with the above and enter the numbers of your scrambled pin below, press <enter> to send.");
varpin=System.Console.ReadLine();
System.Console.WriteLine();
returnpin;
});
// Step 3b. Get the public key and extract the address
for(varacc=0;acc<2;acc++)
{
System.Console.WriteLine();
System.Console.WriteLine("Getting addresses for Bitcoin account {0}...",acc+1);
System.Console.WriteLine();
for(varadr=0;adr<10;adr++)
{
varpublicKey=kk.GetPublicKey(pinChallengeFunction,$"44'/0'/{acc}'/0/{adr}");
varextPubKey=ExtPubKey.Parse(publicKey.Xpub,Network.Main);
varpubKey=extPubKey.PubKey;
varaddress=pubKey.GetAddress(Network.Main);
System.Console.WriteLine("Address: {0}",address);
}
}
System.Console.WriteLine();
System.Console.WriteLine("Press <enter> to quit console gracefully.");
System.Console.ReadLine();
kk.CloseDevice();
}
}
privatevoidKkOnConnected(objectsender,EventArgseventArgs)
{
System.Console.WriteLine();
System.Console.WriteLine("KeepKey connected");
System.Console.WriteLine();
}
privatestaticvoidKkOnDisconnected(objectsender,EventArgseventArgs)
{
System.Console.WriteLine();
System.Console.WriteLine("KeepKey disconnected.");
}
}
}