forked from y4htse/desktop-xamarin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportWalletPrompt.cs
224 lines (197 loc) · 8.27 KB
/
ImportWalletPrompt.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TurtleWallet
{
public partial class ImportWalletPrompt : TurtleWalletForm
{
public string ImportWalletPath
{
get;
set;
}
public string ImportWalletPassword
{
get;
set;
}
public ImportWalletPrompt()
{
InitializeComponent();
this.Text = "Turtle Wallet";
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
Utilities.CloseProgram(e);
}
private void ImportWalletButton_MouseEnter(object sender, EventArgs e)
{
var backcolor = Color.FromArgb(44, 44, 44);
var forcolor = Color.FromArgb(39, 170, 107);
var currentButton = (Label)sender;
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
private void ImportWalletButton_MouseLeave(object sender, EventArgs e)
{
var backcolor = Color.FromArgb(52, 52, 52);
var forcolor = Color.FromArgb(224, 224, 224);
var currentButton = (Label)sender;
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
private void CancelButton_MouseEnter(object sender, EventArgs e)
{
var backcolor = Color.FromArgb(44, 44, 44);
var forcolor = Color.FromArgb(39, 170, 107);
var currentButton = (Label)sender;
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
private void CancelButton_MouseLeave(object sender, EventArgs e)
{
var backcolor = Color.FromArgb(52, 52, 52);
var forcolor = Color.FromArgb(224, 224, 224);
var currentButton = (Label)sender;
currentButton.BackColor = backcolor;
currentButton.ForeColor = forcolor;
}
private void CancelButton_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Are you sure you want to cancel your Turtle Wallet import?", "Cancel wallet import?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
Utilities.SetDialogResult(this, DialogResult.Cancel);
Utilities.Close(this);
}
}
private void ImportWalletButton_Click(object sender, EventArgs e)
{
ImportWallet();
}
private void WalletNameText_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
ImportWallet();
}
}
private void ImportWallet()
{
var curDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
var _walletFile = System.IO.Path.Combine(curDir, walletNameText.Text + ".wallet");
if (walletNameText.Text == "")
{
MessageBox.Show("Please enter a valid wallet name", "Turtle Wallet Import");
return;
}
else if (walletNameText.Text.Any(c => !Char.IsLetterOrDigit(c)))
{
MessageBox.Show("Wallet name cannot contain special characters", "Turtle Wallet Import");
return;
}
else if (System.IO.File.Exists(_walletFile))
{
MessageBox.Show("A wallet with that name already exists! Choose a different name or choose the \"Select Existing Wallet\" option instead.", "Turtle Wallet Import");
return;
}
if (passwordText.Text == "")
{
MessageBox.Show("Please enter a valid password", "Turtle Wallet Import");
return;
}
else if (passwordText.Text.Length < 6)
{
MessageBox.Show("Please enter a password that is larger than 6 characters", "Turtle Wallet Import");
return;
}
else if (passwordText.Text.Length > 150)
{
MessageBox.Show("Passwords cannot be longer than 150 characters!", "Turtle Wallet Import");
return;
}
if (passwordText.Text != passwordConfirmText.Text)
{
MessageBox.Show("Passwords do not match", "Turtle Wallet Import");
return;
}
if (viewSecretKeyText.Text.Length != 64 || spendSecretKeyText.Text.Length != 64)
{
MessageBox.Show("View key or spend key is incorrect length! Should be 64 characters long.", "Turtle Wallet Import");
return;
}
var walletdexe = System.IO.Path.Combine(curDir, "walletd.exe");
if (IsRunningOnMono())
{
walletdexe = System.IO.Path.Combine(curDir, "walletd");
}
if (!System.IO.File.Exists(walletdexe))
{
MessageBox.Show("The 'walletd' daemon is missing from the folder the wallet is currently running from! Please place 'walletd' next to your wallet exe and run again!", "Turtle Wallet Import", MessageBoxButtons.OK, MessageBoxIcon.Error);
Utilities.SetDialogResult(this, DialogResult.Abort);
Utilities.Close(this);
}
importProgressbar.Visible = true;
StringBuilder tmpstdout = new StringBuilder();
try
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = walletdexe;
p.StartInfo.Arguments = CLIEncoder.Encode(new string[]
{"-w", _walletFile, "-p", passwordText.Text, "--view-key",
viewSecretKeyText.Text, "--spend-key", spendSecretKeyText.Text,
"-g"});
p.OutputDataReceived += (sender, args) => tmpstdout.AppendLine(args.Data);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit(10000);
p.CancelOutputRead();
if (!System.IO.File.Exists(_walletFile))
{
MessageBox.Show("Wallet failed to import after communicating with daemon. Please ensure your secret keys are correct, and open walletd.log for more information on what went wrong, if it exists.", "Turtle Wallet Import", MessageBoxButtons.OK, MessageBoxIcon.Error);
Utilities.SetDialogResult(this, DialogResult.Abort);
Utilities.Close(this);
}
else
{
ImportWalletPath = _walletFile;
ImportWalletPassword = passwordText.Text;
MessageBox.Show("Wallet successfully imported at: " + Environment.NewLine + _walletFile, "Turtle Wallet Import", MessageBoxButtons.OK, MessageBoxIcon.Information);
Utilities.SetDialogResult(this, DialogResult.OK);
Utilities.Close(this);
}
}
catch (Exception ex)
{
MessageBox.Show("An exception occured while attempting to import the wallet." + Environment.NewLine + "Error:" + Environment.NewLine + ex.Message, "Turtle Wallet Import", MessageBoxButtons.OK, MessageBoxIcon.Error);
Utilities.SetDialogResult(this, DialogResult.Abort);
Utilities.Close(this);
}
}
public static bool IsRunningOnMono()
{
return Type.GetType("Mono.Runtime") != null;
}
private void PasswordText_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
ImportWallet();
}
}
private void PasswordConfirmText_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
ImportWallet();
}
}
}
}