aboutsummaryrefslogtreecommitdiff
path: root/src
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 /src
parent7733ac322b60127fa8d845f90e7605625996c9d1 (diff)
downloadBIP39-6f7fa3539e4f2cf3be19ebfbd4a914bf0992ca88.tar.gz
BIP39-6f7fa3539e4f2cf3be19ebfbd4a914bf0992ca88.tar.zst
BIP39-6f7fa3539e4f2cf3be19ebfbd4a914bf0992ca88.zip
Parse extended root key regardless of prefix
Diffstat (limited to 'src')
-rw-r--r--src/js/index.js62
1 files changed, 62 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 }