diff options
author | Ian Coleman <ian@iancoleman.io> | 2018-02-06 09:43:13 +1100 |
---|---|---|
committer | Ian Coleman <ian@iancoleman.io> | 2018-02-06 09:43:13 +1100 |
commit | cc61ec30c5647b08cf11d918a6656cfc922e4586 (patch) | |
tree | f4b554f6aa6ab19acaa479cb3440435f5f1c0b8d /src | |
parent | 0f0a888e13ed565a95454fa7a1e46d5db0daf55a (diff) | |
download | BIP39-cc61ec30c5647b08cf11d918a6656cfc922e4586.tar.gz BIP39-cc61ec30c5647b08cf11d918a6656cfc922e4586.tar.zst BIP39-cc61ec30c5647b08cf11d918a6656cfc922e4586.zip |
Encrypt private keys with BIP38
with fakes for missing UI elements
Diffstat (limited to 'src')
-rw-r--r-- | src/js/index.js | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/src/js/index.js b/src/js/index.js index 5a1037b..6aee5dc 100644 --- a/src/js/index.js +++ b/src/js/index.js | |||
@@ -766,6 +766,8 @@ | |||
766 | var self = this; | 766 | var self = this; |
767 | this.shouldGenerate = true; | 767 | this.shouldGenerate = true; |
768 | var useHardenedAddresses = DOM.hardenedAddresses.prop("checked"); | 768 | var useHardenedAddresses = DOM.hardenedAddresses.prop("checked"); |
769 | var useBip38 = false; // TODO get from DOM | ||
770 | var bip38password = "bip38password"; // TODO get from DOM | ||
769 | var isSegwit = segwitSelected(); | 771 | var isSegwit = segwitSelected(); |
770 | var segwitAvailable = networkHasSegwit(); | 772 | var segwitAvailable = networkHasSegwit(); |
771 | var isP2wpkh = p2wpkhSelected(); | 773 | var isP2wpkh = p2wpkhSelected(); |
@@ -780,6 +782,7 @@ | |||
780 | if (!self.shouldGenerate) { | 782 | if (!self.shouldGenerate) { |
781 | return; | 783 | return; |
782 | } | 784 | } |
785 | // derive HDkey for this row of the table | ||
783 | var key = "NA"; | 786 | var key = "NA"; |
784 | if (useHardenedAddresses) { | 787 | if (useHardenedAddresses) { |
785 | key = bip32ExtendedKey.deriveHardened(index); | 788 | key = bip32ExtendedKey.deriveHardened(index); |
@@ -787,19 +790,36 @@ | |||
787 | else { | 790 | else { |
788 | key = bip32ExtendedKey.derive(index); | 791 | key = bip32ExtendedKey.derive(index); |
789 | } | 792 | } |
790 | var address = key.getAddress().toString(); | 793 | // bip38 requires uncompressed keys |
794 | // see https://github.com/iancoleman/bip39/issues/140#issuecomment-352164035 | ||
795 | var keyPair = key.keyPair; | ||
796 | var useUncompressed = useBip38; | ||
797 | if (useUncompressed) { | ||
798 | keyPair = new bitcoinjs.bitcoin.ECPair(keyPair.d, null, { compressed: false }); | ||
799 | } | ||
800 | // get address | ||
801 | var address = keyPair.getAddress().toString(); | ||
802 | // get privkey | ||
803 | var hasPrivkey = !key.isNeutered(); | ||
791 | var privkey = "NA"; | 804 | var privkey = "NA"; |
792 | if (!key.isNeutered()) { | 805 | if (hasPrivkey) { |
793 | privkey = key.keyPair.toWIF(network); | 806 | privkey = keyPair.toWIF(network); |
807 | // BIP38 encode private key if required | ||
808 | if (useBip38) { | ||
809 | privkey = bitcoinjsBip38.encrypt(keyPair.d.toBuffer(), false, bip38password, function(p) { | ||
810 | console.log("Progressed " + p.percent.toFixed(1) + "% for index " + index); | ||
811 | }); | ||
812 | } | ||
794 | } | 813 | } |
795 | var pubkey = key.getPublicKeyBuffer().toString('hex'); | 814 | // get pubkey |
815 | var pubkey = keyPair.getPublicKeyBuffer().toString('hex'); | ||
796 | var indexText = getDerivationPath() + "/" + index; | 816 | var indexText = getDerivationPath() + "/" + index; |
797 | if (useHardenedAddresses) { | 817 | if (useHardenedAddresses) { |
798 | indexText = indexText + "'"; | 818 | indexText = indexText + "'"; |
799 | } | 819 | } |
800 | // Ethereum values are different | 820 | // Ethereum values are different |
801 | if (networks[DOM.network.val()].name == "ETH - Ethereum") { | 821 | if (networks[DOM.network.val()].name == "ETH - Ethereum") { |
802 | var privKeyBuffer = key.keyPair.d.toBuffer(32); | 822 | var privKeyBuffer = keyPair.d.toBuffer(32); |
803 | privkey = privKeyBuffer.toString('hex'); | 823 | privkey = privKeyBuffer.toString('hex'); |
804 | var addressBuffer = ethUtil.privateToAddress(privKeyBuffer); | 824 | var addressBuffer = ethUtil.privateToAddress(privKeyBuffer); |
805 | var hexAddress = addressBuffer.toString('hex'); | 825 | var hexAddress = addressBuffer.toString('hex'); |