-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwallet.js
194 lines (173 loc) · 6.34 KB
/
wallet.js
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
var show_generate = function() {
document.getElementById("generate").style.display = "block";
document.getElementById("restore").style.display = "none";
document.getElementById("step2").style.display = "none";
};
var show_restore = function() {
document.getElementById("restore").style.display = "block";
document.getElementById("generate").style.display = "none";
document.getElementById("step2").style.display = "none";
};
var getStringWords = function(string) {
return string.replace(/^\s*(.*)\s*$/, '$1').replace(/\s+/, ' ').split(' ');
};
var genkeys = function(additional_entropy, lang) {
var seed = cnUtil.sc_reduce32(poor_mans_kdf(additional_entropy + cnUtil.rand_32()));
var keys = cnUtil.create_address(seed);
var passPhrase = mn_encode(seed, lang);
return {
keys: keys,
mnemonic: passPhrase
}
};
var restore_keys = function(lang) {
var seed_phrase = document.getElementById("seed_phrase").value;
var seed = mn_decode(seed_phrase);
var keys = cnUtil.create_address(seed);
address_widget.innerHTML = keys.public_addr;
mnemonic_widget.innerHTML = seed_phrase;
spend_key_widget.innerHTML = keys.spend.sec;
view_key_widget.innerHTML = keys.view.sec;
document.getElementById("step2").style.display = "block";
};
var genwallet = function(lang) {
document.getElementById("step2").style.display = "block";
var spend_key_widget = document.getElementById("spend_key_widget");
var view_key_widget = document.getElementById("view_key_widget");
var address_widget = document.getElementById("address_widget");
var address_qr_widget = document.getElementById("address_qr_widget");
var user_entropy_widget = document.getElementById("user_entropy_widget");
var res = genkeys(user_entropy_widget.value, lang);
var keys = res.keys;
var mnemonic = res.mnemonic;
address_widget.innerHTML = keys.public_addr;
mnemonic_widget.innerHTML = mnemonic;
spend_key_widget.innerHTML = keys.spend.sec;
view_key_widget.innerHTML = keys.view.sec;
// wallet_keys_widget.innerHTML = keys.privateKeys;
//address_qr_widget.innerHTML = "";
//qr=new QRCode(address_qr_widget, {correctLevel:QRCode.CorrectLevel.L});
//qr.makeCode("2acoin:"+keys.public_addr);
};
/*
previous_button_text = "";
prefix = "";
function genwallet_prefix_worker()
{
attempts = 0;
while (true) {
attempts++;
seed = cnUtil.sc_reduce32(cnUtil.rand_32());
var passPhrase = mn_encode(seed,current_lang);
var words = getStringWords(passPhrase);
seed = CryptoJS.SHA256(salt + words.join(' ')).toString();
keys = cnUtil.create_address_if_prefix(seed,prefix);
if (keys != null) {
gen_prefix_widget = document.getElementById("gen_prefix_widget");
prefix_widget = document.getElementById("prefix_widget");
gen_prefix_widget.value = previous_button_text;
prefix_widget.disabled = false;
generating = false;
break;
}
if (attempts == 10) {
if (generating)
setTimeout(genwallet_prefix_worker, 0);
return;
}
}
mnemonic = passPhrase;
spend_key_widget = document.getElementById("spend_key_widget");
view_key_widget = document.getElementById("view_key_widget");
address_widget = document.getElementById("address_widget");
// mnemonic_widget = document.getElementById("mnemonic_widget");
spend_key_widget.innerHTML = keys.spend.sec;
view_key_widget.innerHTML = keys.view.sec;
address_widget.innerHTML = keys.public_addr;
address_qr_widget.innerHTML = "";
// mnemonic_widget.innerHTML = mnemonic;
wallet_keys_widget.innerHTML = keys.privateKeys;
qr=new QRCode(address_qr_widget, {correctLevel:QRCode.CorrectLevel.L});
qr.makeCode("2acoin:"+keys.public_addr);
}
var zerohex="0000000000000000000000000000000000000000000000000000000000000000";
var ffhex="ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
var lowest_address=cnUtil.pubkeys_to_string(zerohex,zerohex);
var highest_address=cnUtil.pubkeys_to_string(ffhex,ffhex);
function is_valid_prefix(prefix)
{
if (prefix.length <= 0 || prefix.length >= 95)
return false;
var lowest=lowest_address.substr(0,prefix.length);
var highest=highest_address.substr(0,prefix.length);
if (prefix<lowest)
return false;
if (prefix>highest)
return false;
return true;
}
function check_prefix_validity()
{
gen_prefix_widget = document.getElementById("gen_prefix_widget");
prefix_widget = document.getElementById("prefix_widget");
if (gen_prefix_widget.value == "STOP")
return;
prefix=prefix_widget.value;
if (is_valid_prefix(prefix)) {
gen_prefix_widget.value = "Generate wallet with prefix";
gen_prefix_widget.disabled = false;
}
else {
gen_prefix_widget.value = "Invalid prefix";
gen_prefix_widget.disabled = true;
}
}
generating = false;
function genwallet_prefix()
{
gen_prefix_widget = document.getElementById("gen_prefix_widget");
prefix_widget = document.getElementById("prefix_widget");
if (generating) {
generating = false;
gen_prefix_widget.value = previous_button_text;
prefix_widget.disabled = false;
}
else {
prefix_widget = document.getElementById("prefix_widget");
prefix = prefix_widget.value;
prefix.trim();
if (prefix.length < 5 || prefix[0] != "guns") {
alert("Bad prefix "+prefix+" should start with K and be at least one extra character");
return;
}
if (!is_valid_prefix(prefix)) {
alert("Bad prefix "+prefix+" is not a valid address prefix");
return;
}
generating = true;
previous_button_text = gen_prefix_widget.value;
gen_prefix_widget.value = "STOP";
prefix_widget.disabled = true;
setTimeout(genwallet_prefix_worker, 0);
}
}
*/
// function toggle_qr()
// {
// address_qr_widget = document.getElementById("address_qr_widget");
// spend_key_widget = document.getElementById("spend_key_widget");
// view_key_widget = document.getElementById("view_key_widget");
// // mnemonic_widget = document.getElementById("mnemonic_widget");
// if (address_qr_widget.style.display=="block") {
// address_qr_widget.style.display="none";
// spend_key_widget.style.display = "block";
// view_key_widget.style.display = "block";
// // mnemonic_widget.style.display = "block";
// }
// else {
// address_qr_widget.style.display="block";
// spend_key_widget.style.display = "none";
// view_key_widget.style.display = "none";
// // mnemonic_widget.style.display = "none";
// }
// }