Skip to content

Commit 49dd8f7

Browse files
committed
bip39 checks
1 parent 7456e25 commit 49dd8f7

6 files changed

+94
-62
lines changed

assets_js_bitrequest_bip39.js

Lines changed: 87 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,22 @@ function bipv_pass() {
147147
function test_bip39() {
148148
if (!crypto) { // test for window.crypto
149149
bip39_fail();
150-
glob_let.test_derive = false;
151150
return
152151
}
153152
if (glob_const.has_bigint === false) { // test for js BigInt
154153
bip39_fail();
155-
glob_let.test_derive = false;
156154
return
157155
}
158156
const k_str = glob_const.expected_seed.slice(0, 32),
159157
enc_test = aes_enc(glob_const.test_phrase, k_str),
160158
dec_test = aes_dec(enc_test, k_str);
161159
if (glob_const.test_phrase !== dec_test) { // test encryption
162160
bip39_fail();
163-
glob_let.test_derive = false;
164161
return
165162
}
166163
if (toseed(glob_const.test_phrase) !== glob_const.expected_seed || test_derivation() === false) {
167-
const coinsToDeriveFailure = ["bitcoin", "litecoin", "dogecoin", "dash", "ethereum", "bitcoin-cash"];
164+
bip39_fail();
165+
const coinsToDeriveFailure = ["bitcoin", "litecoin", "dogecoin", "dash", "ethereum", "bitcoin-cash", "monero", "nano"];
168166
derive_fail(coinsToDeriveFailure);
169167
coinsToDeriveFailure.forEach(coin => {
170168
glob_const.c_derive[coin] = false;
@@ -174,6 +172,10 @@ function test_bip39() {
174172
"check": bech32_check,
175173
"coin": "bitcoin"
176174
},
175+
{
176+
"check": bech32_check,
177+
"coin": "litecoin"
178+
},
177179
{
178180
"check": cashaddr_check,
179181
"coin": "bitcoin-cash"
@@ -187,10 +189,10 @@ function test_bip39() {
187189
"coin": "monero"
188190
}
189191
];
190-
derivationChecks.forEach(({
192+
derivationChecks.forEach(function({
191193
check,
192194
coin
193-
}) => {
195+
}) {
194196
if (check() === false) {
195197
derive_fail([coin]);
196198
glob_const.c_derive[coin] = false;
@@ -213,6 +215,7 @@ function test_bip39() {
213215
// Handles BIP39 failure by adding a CSS class
214216
function bip39_fail() {
215217
glob_const.body.addClass("nobip");
218+
glob_let.test_derive = false;
216219
}
217220

218221
// Handles derivation failure for specified cryptocurrencies
@@ -237,71 +240,106 @@ function derive_xpub_fail(arr) {
237240

238241
// Tests derivation process for Bitcoin
239242
function test_derivation() {
240-
const currency = "bitcoin",
241-
test_rootkey = get_rootkey(glob_const.expected_seed),
242-
bip32dat = getbip32dat(currency),
243-
dx_dat = {
244-
"dpath": "m/44'/0'/0'/0/0",
245-
"key": test_rootkey.slice(0, 64),
246-
"cc": test_rootkey.slice(64)
247-
},
248-
x_keys_dat = derive_x(dx_dat),
249-
key_object = format_keys(glob_const.expected_seed, x_keys_dat, bip32dat, 0, currency);
250-
return key_object.address === glob_const.expected_address;
243+
try {
244+
const currency = "bitcoin",
245+
test_rootkey = get_rootkey(glob_const.expected_seed),
246+
bip32dat = getbip32dat(currency),
247+
dx_dat = {
248+
"dpath": "m/44'/0'/0'/0/0",
249+
"key": test_rootkey.slice(0, 64),
250+
"cc": test_rootkey.slice(64)
251+
},
252+
x_keys_dat = derive_x(dx_dat),
253+
key_object = format_keys(glob_const.expected_seed, x_keys_dat, bip32dat, 0, currency);
254+
return key_object.address === glob_const.expected_address;
255+
} catch (e) {
256+
console.error(e.name, e.message);
257+
return false;
258+
}
251259
}
252260

253261
// Checks Bech32 address derivation
254262
function bech32_check() {
255-
const bip84_pub = "03bb4a626f63436a64d7cf1e441713cc964c0d53289a5b17acb1b9c262be57cb17",
256-
bip84_bech32 = pub_to_address_bech32("bc", bip84_pub);
257-
return glob_const.expected_bech32 === bip84_bech32;
263+
try {
264+
const bip84_pub = "03bb4a626f63436a64d7cf1e441713cc964c0d53289a5b17acb1b9c262be57cb17",
265+
bip84_bech32 = pub_to_address_bech32("bc", bip84_pub);
266+
return glob_const.expected_bech32 === bip84_bech32;
267+
} catch (e) {
268+
console.error(e.name, e.message);
269+
return false;
270+
}
258271
}
259272

260273
// Checks Bitcoin Cash cashaddr derivation
261274
function cashaddr_check() {
262-
const bch_legacy = "1AVPurYZinnctgGPiXziwU6PuyZKX5rYZU",
263-
bch_cashaddr = pub_to_cashaddr(bch_legacy);
264-
return glob_const.expected_bch_cashaddr === bch_cashaddr;
275+
try {
276+
const bch_legacy = "1AVPurYZinnctgGPiXziwU6PuyZKX5rYZU",
277+
bch_cashaddr = pub_to_cashaddr(bch_legacy);
278+
return glob_const.expected_bch_cashaddr === bch_cashaddr;
279+
} catch (e) {
280+
console.error(e.name, e.message);
281+
return false;
282+
}
265283
}
266284

267285
// Checks Nano address derivation
268286
function nano_check() {
269-
const expected_nano_address = "nano_1mbtirc4x3kixfy5wufxaqakd3gbojpn6gpmk6kjiyngnjwgy6yty3txgztq",
270-
xnano_address = NanocurrencyWeb.wallet.accounts(glob_const.expected_seed, 0, 0)[0].address;
271-
return expected_nano_address === xnano_address;
287+
try {
288+
const expected_nano_address = "nano_1mbtirc4x3kixfy5wufxaqakd3gbojpn6gpmk6kjiyngnjwgy6yty3txgztq",
289+
xnano_address = NanocurrencyWeb.wallet.accounts(glob_const.expected_seed, 0, 0)[0].address;
290+
return expected_nano_address === xnano_address;
291+
} catch (e) {
292+
console.error(e.name, e.message);
293+
return false;
294+
}
272295
}
273296

274297
// Checks Monero (XMR) address derivation
275298
function xmr_check() { // https://coinomi.github.io/tools/bip39/
276-
const expected_xmr_address = "477h3C6E6C4VLMR36bQL3yLcA8Aq3jts1AHLzm5QXipDdXVCYPnKEvUKykh2GTYqkkeQoTEhWpzvVQ4rMgLM1YpeD6qdHbS",
277-
ssk = get_ssk(glob_const.expected_seed, true),
278-
xko = xmr_getpubs(ssk, 0);
279-
return xko.address === expected_xmr_address;
299+
try {
300+
const expected_xmr_address = "477h3C6E6C4VLMR36bQL3yLcA8Aq3jts1AHLzm5QXipDdXVCYPnKEvUKykh2GTYqkkeQoTEhWpzvVQ4rMgLM1YpeD6qdHbS",
301+
ssk = get_ssk(glob_const.expected_seed, true),
302+
xko = xmr_getpubs(ssk, 0);
303+
return xko.address === expected_xmr_address;
304+
} catch (e) {
305+
console.error(e.name, e.message);
306+
return false;
307+
}
280308
}
281309

282310
// Checks extended public key (xpub) derivation for Bitcoin
283311
function xpub_check() {
284-
const currency = "bitcoin",
285-
xpub_keycc = key_cc_xpub("xpub6Cy7dUR4ZKF22HEuVq7epRgRsoXfL2MK1RE81CSvp1ZySySoYGXk5PUY9y9Cc5ExpnSwXyimQAsVhyyPDNDrfj4xjDsKZJNYgsHXoEPNCYQ"),
286-
dx_dat = {
287-
"dpath": "M/0/0",
288-
"key": xpub_keycc.key,
289-
"cc": xpub_keycc.cc,
290-
"vb": xpub_keycc.version
291-
},
292-
x_keys_dat = derive_x(dx_dat),
293-
bip32dat = getbip32dat(currency),
294-
key_object = format_keys(null, x_keys_dat, bip32dat, 0, currency),
295-
xpub_address = key_object.address,
296-
xpub_wildcard_address = "bc1qk0wlvl4xh3eqe5szqyrlcj4ws8633vz0vhhywl"; // wildcard for bech32 Xpubs (Zpub)
297-
return xpub_address === glob_const.expected_address || xpub_address === xpub_wildcard_address;
312+
try {
313+
const currency = "bitcoin",
314+
xpub_keycc = key_cc_xpub("xpub6Cy7dUR4ZKF22HEuVq7epRgRsoXfL2MK1RE81CSvp1ZySySoYGXk5PUY9y9Cc5ExpnSwXyimQAsVhyyPDNDrfj4xjDsKZJNYgsHXoEPNCYQ"),
315+
dx_dat = {
316+
"dpath": "M/0/0",
317+
"key": xpub_keycc.key,
318+
"cc": xpub_keycc.cc,
319+
"vb": xpub_keycc.version
320+
},
321+
x_keys_dat = derive_x(dx_dat),
322+
bip32dat = getbip32dat(currency),
323+
key_object = format_keys(null, x_keys_dat, bip32dat, 0, currency),
324+
xpub_address = key_object.address,
325+
xpub_wildcard_address = "bc1qk0wlvl4xh3eqe5szqyrlcj4ws8633vz0vhhywl"; // wildcard for bech32 Xpubs (Zpub)
326+
return xpub_address === glob_const.expected_address || xpub_address === xpub_wildcard_address;
327+
} catch (e) {
328+
console.error(e.name, e.message);
329+
return false;
330+
}
298331
}
299332

300333
// Checks Ethereum extended public key (xpub) derivation
301334
function eth_xpub_check() {
302-
const eth_pub = "03c026c4b041059c84a187252682b6f80cbbe64eb81497111ab6914b050a8936fd",
303-
eth_address = pub_to_eth_address(eth_pub);
304-
return glob_const.expected_eth_address === eth_address;
335+
try {
336+
const eth_pub = "03c026c4b041059c84a187252682b6f80cbbe64eb81497111ab6914b050a8936fd",
337+
eth_address = pub_to_eth_address(eth_pub);
338+
return glob_const.expected_eth_address === eth_address;
339+
} catch (e) {
340+
console.error(e.name, e.message);
341+
return false;
342+
}
305343
}
306344

307345
// Check derivations
@@ -1757,9 +1795,9 @@ function phrase_info_pu(coin) {
17571795
if (glob_const.c_derive[currency]) {
17581796
$("#pi_icons").append(icon_node);
17591797
$("#d_paths").append(dp_node);
1798+
$("#xpub_box").append(xp_node);
1799+
$("#segw_box").append(segw_node);
17601800
}
1761-
$("#xpub_box").append(xp_node);
1762-
$("#segw_box").append(segw_node);
17631801
$("#supported_wallets").append(sw_node);
17641802
pi_show();
17651803
}

assets_js_bitrequest_coin_settings.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,8 @@ function test_rpc(rpc_input_box, rpc_data, currency) {
615615
const rpcurl = get_rpc_url(rpc_data),
616616
testadress = {
617617
"bitcoin": glob_const.expected_bech32,
618-
"litecoin": glob_const.expected_ltc_address,
619-
"dogecoin": glob_const.expected_doge_address,
618+
"litecoin": "LZakyXotaE29Pehw21SoPuU832UhvJp4LG",
619+
"dogecoin": "DKvWg8UhQSycj1J8QVxeBDkRpbjDkw3DiW",
620620
"bitcoin-cash": glob_const.expected_bch_cashaddr
621621
} [currency] || "",
622622
pload = glob_let.is_btc ? {

assets_js_bitrequest_core.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,7 +2629,7 @@ function canceldialogtrigger() {
26292629

26302630
// Closes the current dialog
26312631
function canceldialog(pass) {
2632-
if (glob_const.inframe === true) {
2632+
if (glob_const.inframe) {
26332633
if (pass !== true) {
26342634
if ($("#contactformbox").length > 0) {
26352635
return
@@ -5220,7 +5220,6 @@ function handleAddress(result, payment) {
52205220
isxpub = (end_result.length > 103),
52215221
er_val = (payment === "nimiq") ? end_result.replace(/\s/g, "") : end_result,
52225222
validate = isxpub ? check_xpub(end_result, xpub_prefix(payment), payment) : check_address(er_val, payment);
5223-
52245223
clear_xpub_inputs();
52255224
if (validate === true) {
52265225
$("#popup .formbox input.address").val(er_val);

assets_js_bitrequest_settings.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ function complilebackup() {
924924
continue;
925925
} else if (key === "bitrequest_bpdat") {
926926
const not_verified = (glob_let.io.bipv !== "yes");
927-
if (not_verified || (glob_let.test_derive === true && get_setting("backup", "sbu") === true)) {
927+
if (not_verified || (glob_let.test_derive && get_setting("backup", "sbu") === true)) {
928928
const val_obj = JSON.parse(value);
929929
val_obj.dat = null;
930930
jsonfile.push('"' + key + '":' + JSON.stringify(val_obj));
@@ -1605,7 +1605,7 @@ function compare_seeds_callback(compare) {
16051605
function restorestorage(jsonobject, newphrase) {
16061606
$.each(jsonobject, function(key, value) {
16071607
if (key === "bitrequest_bpdat") {
1608-
if (glob_let.test_derive === true && newphrase === true && glob_let.resd.bpdat) {
1608+
if (glob_let.test_derive && newphrase === true && glob_let.resd.bpdat) {
16091609
localStorage.setItem(key, JSON.stringify(glob_let.resd.bpdat));
16101610
}
16111611
} else {

assets_js_lib_global_queries.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ const br_bipobj = br_get_local("bpdat", true),
3333
(br_thishostname === br_thishostname) ? "selfhosted" : "unknown",
3434
br_video = $("#qr-video")[0],
3535
glob_const = {
36-
"default_error": {
37-
"error": "no transactions found",
38-
"console": true
39-
},
4036
"stored_currencies": br_get_local("currencies", true),
4137
"apptitle": "Bitrequest",
4238
"hostname": br_hostname,
@@ -125,14 +121,12 @@ const br_bipobj = br_get_local("bpdat", true),
125121
"seed": true
126122
}
127123
},
128-
// bip39
124+
// bip39 (All addresses / xpubs in this app are test addresses derived from the following testphrase, taken from https://github.com/bitcoinbook/bitcoinbook/blob/f8b883dcd4e3d1b9adf40fed59b7e898fbd9241f/ch05.asciidoc)
129125
"test_phrase": "army van defense carry jealous true garbage claim echo media make crunch", // random phrase used for test derive
130126
"expected_seed": "5b56c417303faa3fcba7e57400e120a0ca83ec5a4fc9ffba757fbe63fbd77a89a1a3be4c67196f57c39a88b76373733891bfaba16ed27a813ceed498804c0570", // expected seed used for test derive
131127
"expected_address": "1HQ3rb7nyLPrjnuW85MUknPekwkn7poAUm", // expected addres used for test derive
132128
"expected_bech32": "bc1qg0azlj4w2lrq8jssrrz6eprt2fe7f7edm4vpd5", // expected bech32 addres used for test derive
133-
"expected_ltc_address": "LZakyXotaE29Pehw21SoPuU832UhvJp4LG",
134129
"expected_bch_cashaddr": "qp5p0eur784pk8wxy2kzlz3ctnq5whfnuqqpp78u22",
135-
"expected_doge_address": "DKvWg8UhQSycj1J8QVxeBDkRpbjDkw3DiW",
136130
"expected_eth_address": "0x2161DedC3Be05B7Bb5aa16154BcbD254E9e9eb68",
137131
"c_derive": {
138132
"bitcoin": true,

assets_styles_styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ body.nobip #bip39_passphrase,
5151
body.nobip .cc_settinglist li[data-id='Xpub'],
5252
body.nobip .cc_settinglist li[data-id='Key derivations'],
5353
.no_derive .cc_settinglist li[data-id='Key derivations'],
54+
.no_derive .cc_settinglist li[data-id='Xpub'],
5455
.no_xpub .cc_settinglist li[data-id='Xpub'],
5556
body.nobip .cc_settinglist li[data-id='Reuse address'],
5657
.no_derive .cc_settinglist li[data-id='Reuse address'],

0 commit comments

Comments
 (0)