From 877b8bdfecabed14e8d5e8a84365743b81da0175 Mon Sep 17 00:00:00 2001 From: AndreasGassmann Date: Sat, 7 Nov 2020 16:58:08 +0100 Subject: feat(bip85): add bip85 logic --- src/js/index.js | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'src/js') diff --git a/src/js/index.js b/src/js/index.js index 7e67327..80b78bc 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -94,6 +94,18 @@ DOM.bip84accountXprv = $("#bip84 .account-xprv"); DOM.bip84accountXpub = $("#bip84 .account-xpub"); DOM.bip84change = $("#bip84 .change"); + DOM.bip85 = $('.bip85'); + DOM.showBip85 = $('.showBip85'); + DOM.bip85Field = $('.bip85Field'); + DOM.bip85application = $('#bip85-application'); + DOM.bip85mnemonicLanguage = $('#bip85-mnemonic-language'); + DOM.bip85mnemonicLanguageInput = $('.bip85-mnemonic-language-input'); + DOM.bip85mnemonicLength = $('#bip85-mnemonic-length'); + DOM.bip85mnemonicLengthInput = $('.bip85-mnemonic-length-input'); + DOM.bip85index = $('#bip85-index'); + DOM.bip85indexInput = $('.bip85-index-input'); + DOM.bip85bytes = $('#bip85-bytes'); + DOM.bip85bytesInput = $('.bip85-bytes-input'); DOM.bip141unavailable = $("#bip141 .unavailable"); DOM.bip141available = $("#bip141 .available"); DOM.bip141path = $("#bip141-path"); @@ -141,6 +153,7 @@ DOM.more.on("click", showMore); DOM.seed.on("input", delayedSeedChanged); DOM.rootKey.on("input", delayedRootKeyChanged); + DOM.showBip85.on('change', toggleBip85); DOM.litecoinUseLtub.on("change", litecoinUseLtubChanged); DOM.bip32path.on("input", calcForDerivationPath); DOM.bip44account.on("input", calcForDerivationPath); @@ -149,6 +162,11 @@ DOM.bip49change.on("input", calcForDerivationPath); DOM.bip84account.on("input", calcForDerivationPath); DOM.bip84change.on("input", calcForDerivationPath); + DOM.bip85application.on('input', calcBip85); + DOM.bip85mnemonicLanguage.on('change', calcBip85); + DOM.bip85mnemonicLength.on('change', calcBip85); + DOM.bip85index.on('input', calcBip85); + DOM.bip85bytes.on('input', calcBip85); DOM.bip141path.on("input", calcForDerivationPath); DOM.bip141semantics.on("change", tabChanged); DOM.tab.on("shown.bs.tab", tabChanged); @@ -268,6 +286,7 @@ var passphrase = DOM.passphrase.val(); calcBip32RootKeyFromSeed(phrase, passphrase); calcForDerivationPath(); + calcBip85(); // Show the word indexes showWordIndexes(); writeSplitPhrase(phrase); @@ -404,6 +423,7 @@ } // Calculate and display calcForDerivationPath(); + calcBip85(); } function rootKeyChanged() { @@ -418,6 +438,7 @@ // Calculate and display calcBip32RootKeyFromBase58(rootKeyBase58); calcForDerivationPath(); + calcBip85(); } function litecoinUseLtubChanged() { @@ -440,6 +461,71 @@ } } + function toggleBip85() { + if (DOM.showBip85.prop('checked')) { + DOM.bip85.removeClass('hidden'); + toggleBip85Fields(); + } else { + DOM.bip85.addClass('hidden'); + } + } + + function toggleBip85Fields() { + if (DOM.showBip85.prop('checked')) { + DOM.bip85mnemonicLanguageInput.addClass('hidden'); + DOM.bip85mnemonicLengthInput.addClass('hidden'); + DOM.bip85bytesInput.addClass('hidden'); + + var app = DOM.bip85application.val(); + if (app === 'bip39') { + DOM.bip85mnemonicLanguageInput.removeClass('hidden'); + DOM.bip85mnemonicLengthInput.removeClass('hidden'); + } else if (app === 'hex') { + DOM.bip85bytesInput.removeClass('hidden'); + } + } + } + + function calcBip85() { + toggleBip85Fields(); + + var app = DOM.bip85application.val(); + + var phrase = DOM.phrase.val(); + var passphrase = DOM.passphrase.val(); + if (!phrase) { + return; + } + try { + var master = libs.bip85.BIP85.fromMnemonic(phrase, passphrase); + + var result; + + const index = parseInt(DOM.bip85index.val(), 10); + + if (app === 'bip39') { + const language = parseInt(DOM.bip85mnemonicLanguage.val(), 10); + const length = parseInt(DOM.bip85mnemonicLength.val(), 10); + + result = master.deriveBIP39(language, length, index).toMnemonic(); + } else if (app === 'wif') { + result = master.deriveWIF(index).toWIF(); + } else if (app === 'xprv') { + result = master.deriveXPRV(index).toXPRV(); + } else if (app === 'hex') { + const bytes = parseInt(DOM.bip85bytes.val(), 10); + + result = master.deriveHex(bytes, index).toEntropy(); + } + + hideValidationError(); + DOM.bip85Field.val(result); + } catch (e) { + showValidationError('BIP85: ' + e.message); + DOM.bip85Field.val(''); + } + } + function calcForDerivationPath() { clearDerivedKeys(); clearAddressesList(); -- cgit v1.2.3