aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Coleman <ian@iancoleman.io>2018-05-29 10:21:21 +1000
committerIan Coleman <ian@iancoleman.io>2018-05-29 11:30:48 +1000
commit6f7fa3539e4f2cf3be19ebfbd4a914bf0992ca88 (patch)
tree22027f6d86f94f5134fb0d831ff355c42761f4b0
parent7733ac322b60127fa8d845f90e7605625996c9d1 (diff)
downloadBIP39-6f7fa3539e4f2cf3be19ebfbd4a914bf0992ca88.tar.gz
BIP39-6f7fa3539e4f2cf3be19ebfbd4a914bf0992ca88.tar.zst
BIP39-6f7fa3539e4f2cf3be19ebfbd4a914bf0992ca88.zip
Parse extended root key regardless of prefix
-rw-r--r--src/js/index.js62
-rw-r--r--tests/spec/tests.js19
2 files changed, 81 insertions, 0 deletions
diff --git a/src/js/index.js b/src/js/index.js
index ff35755..bfaf879 100644
--- a/src/js/index.js
+++ b/src/js/index.js
@@ -478,6 +478,37 @@
478 } 478 }
479 479
480 function calcBip32RootKeyFromBase58(rootKeyBase58) { 480 function calcBip32RootKeyFromBase58(rootKeyBase58) {
481 // try parsing with various segwit network params since this extended
482 // key may be from any one of them.
483 if (networkHasSegwit()) {
484 var n = network;
485 if ("baseNetwork" in n) {
486 n = bitcoinjs.bitcoin.networks[n.baseNetwork];
487 }
488 // try parsing using base network params
489 try {
490 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n);
491 return;
492 }
493 catch (e) {}
494 // try parsing using p2wpkh params
495 if ("p2wpkh" in n) {
496 try {
497 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkh);
498 return;
499 }
500 catch (e) {}
501 }
502 // try parsing using p2wpkh-in-p2sh network params
503 if ("p2wpkhInP2sh" in n) {
504 try {
505 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkhInP2sh);
506 return;
507 }
508 catch (e) {}
509 }
510 }
511 // try the network params as currently specified
481 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network); 512 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network);
482 } 513 }
483 514
@@ -551,6 +582,37 @@
551 } 582 }
552 583
553 function validateRootKey(rootKeyBase58) { 584 function validateRootKey(rootKeyBase58) {
585 // try various segwit network params since this extended key may be from
586 // any one of them.
587 if (networkHasSegwit()) {
588 var n = network;
589 if ("baseNetwork" in n) {
590 n = bitcoinjs.bitcoin.networks[n.baseNetwork];
591 }
592 // try parsing using base network params
593 try {
594 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n);
595 return "";
596 }
597 catch (e) {}
598 // try parsing using p2wpkh params
599 if ("p2wpkh" in n) {
600 try {
601 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkh);
602 return "";
603 }
604 catch (e) {}
605 }
606 // try parsing using p2wpkh-in-p2sh network params
607 if ("p2wpkhInP2sh" in n) {
608 try {
609 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkhInP2sh);
610 return "";
611 }
612 catch (e) {}
613 }
614 }
615 // try the network params as currently specified
554 try { 616 try {
555 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network); 617 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network);
556 } 618 }
diff --git a/tests/spec/tests.js b/tests/spec/tests.js
index 5ddd686..b90906b 100644
--- a/tests/spec/tests.js
+++ b/tests/spec/tests.js
@@ -3681,4 +3681,23 @@ it('Shows litecoin BIP49 addresses', function(done) {
3681 }); 3681 });
3682}); 3682});
3683 3683
3684it('Can use root keys to generate segwit table rows', function(done) {
3685 // segwit uses ypub / zpub instead of xpub but the root key should still
3686 // be valid regardless of the encoding used to import that key.
3687 // Maybe this breaks the reason for the different extended key prefixes, but
3688 // since the parsed root key is used behind the scenes anyhow this should be
3689 // allowed.
3690 driver.findElement(By.css('#root-key'))
3691 .sendKeys('xprv9s21ZrQH143K2jkGDCeTLgRewT9F2pH5JZs2zDmmjXes34geVnFiuNa8KTvY5WoYvdn4Ag6oYRoB6cXtc43NgJAEqDXf51xPm6fhiMCKwpi');
3692 driver.findElement(By.css('#bip49-tab a'))
3693 .click()
3694 // bip49 addresses are shown
3695 driver.sleep(generateDelay).then(function() {
3696 getFirstAddress(function(address) {
3697 expect(address).toBe("3QG2Y9AA4xZ846gKHZqNf7mvVKbLqMKxr2");
3698 done();
3699 });
3700 });
3701});
3702
3684}); 3703});