BIP32 Root Key can be specified by user

This commit is contained in:
Ian Coleman
2016-08-23 10:31:39 +10:00
parent 563e401a4f
commit efe4158670
3 changed files with 150 additions and 24 deletions

View File

@@ -109,7 +109,7 @@
<div class="form-group"> <div class="form-group">
<label for="root-key" class="col-sm-2 control-label">BIP32 Root Key</label> <label for="root-key" class="col-sm-2 control-label">BIP32 Root Key</label>
<div class="col-sm-10"> <div class="col-sm-10">
<textarea id="root-key" class="root-key form-control" readonly="readonly"></textarea> <textarea id="root-key" class="root-key form-control"></textarea>
</div> </div>
</div> </div>
</form> </form>
@@ -14830,6 +14830,7 @@ var Mnemonic = function(language) {
var showPrivKey = true; var showPrivKey = true;
var phraseChangeTimeoutEvent = null; var phraseChangeTimeoutEvent = null;
var rootKeyChangedTimeoutEvent = null;
var DOM = {}; var DOM = {};
DOM.network = $(".network"); DOM.network = $(".network");
@@ -14868,12 +14869,13 @@ var Mnemonic = function(language) {
DOM.passphrase.on("input", delayedPhraseChanged); DOM.passphrase.on("input", delayedPhraseChanged);
DOM.generate.on("click", generateClicked); DOM.generate.on("click", generateClicked);
DOM.more.on("click", showMore); DOM.more.on("click", showMore);
DOM.bip32path.on("input", delayedPhraseChanged); DOM.rootKey.on("input", delayedRootKeyChanged);
DOM.bip44purpose.on("input", delayedPhraseChanged); DOM.bip32path.on("input", calcForDerivationPath);
DOM.bip44coin.on("input", delayedPhraseChanged); DOM.bip44purpose.on("input", calcForDerivationPath);
DOM.bip44account.on("input", delayedPhraseChanged); DOM.bip44coin.on("input", calcForDerivationPath);
DOM.bip44change.on("input", delayedPhraseChanged); DOM.bip44account.on("input", calcForDerivationPath);
DOM.tab.on("click", delayedPhraseChanged); DOM.bip44change.on("input", calcForDerivationPath);
DOM.tab.on("shown.bs.tab", calcForDerivationPath);
DOM.indexToggle.on("click", toggleIndexes); DOM.indexToggle.on("click", toggleIndexes);
DOM.addressToggle.on("click", toggleAddresses); DOM.addressToggle.on("click", toggleAddresses);
DOM.privateKeyToggle.on("click", togglePrivateKeys); DOM.privateKeyToggle.on("click", togglePrivateKeys);
@@ -14888,7 +14890,7 @@ var Mnemonic = function(language) {
function networkChanged(e) { function networkChanged(e) {
var network = e.target.value; var network = e.target.value;
networks[network].onSelect(); networks[network].onSelect();
delayedPhraseChanged(); displayBip32Info();
} }
function delayedPhraseChanged() { function delayedPhraseChanged() {
@@ -14905,12 +14907,57 @@ var Mnemonic = function(language) {
hideValidationError(); hideValidationError();
// Get the mnemonic phrase // Get the mnemonic phrase
var phrase = DOM.phrase.val(); var phrase = DOM.phrase.val();
var passphrase = DOM.passphrase.val();
var errorText = findPhraseErrors(phrase); var errorText = findPhraseErrors(phrase);
if (errorText) { if (errorText) {
showValidationError(errorText); showValidationError(errorText);
return; return;
} }
// Calculate and display
var passphrase = DOM.passphrase.val();
calcBip32RootKeyFromSeed(phrase, passphrase);
calcForDerivationPath();
hidePending();
}
function delayedRootKeyChanged() {
// Warn if there is an existing mnemonic or passphrase.
if (DOM.phrase.val().length > 0 || DOM.passphrase.val().length > 0) {
if (!confirm("This will clear existing mnemonic and passphrase")) {
DOM.rootKey.val(bip32RootKey);
return
}
}
hideValidationError();
showPending();
// Clear existing mnemonic and passphrase
DOM.phrase.val("");
DOM.passphrase.val("");
seed = null;
if (rootKeyChangedTimeoutEvent != null) {
clearTimeout(rootKeyChangedTimeoutEvent);
}
rootKeyChangedTimeoutEvent = setTimeout(rootKeyChanged, 400);
}
function rootKeyChanged() {
showPending();
hideValidationError();
// Validate the root key TODO
var rootKeyBase58 = DOM.rootKey.val();
var errorText = validateRootKey(rootKeyBase58);
if (errorText) {
showValidationError(errorText);
return;
}
// Calculate and display
calcBip32RootKeyFromBase58(rootKeyBase58);
calcForDerivationPath();
hidePending();
}
function calcForDerivationPath() {
showPending();
hideValidationError();
// Get the derivation path // Get the derivation path
var derivationPath = getDerivationPath(); var derivationPath = getDerivationPath();
var errorText = findDerivationPathErrors(derivationPath); var errorText = findDerivationPathErrors(derivationPath);
@@ -14918,8 +14965,7 @@ var Mnemonic = function(language) {
showValidationError(errorText); showValidationError(errorText);
return; return;
} }
// Calculate and display calcBip32ExtendedKey(derivationPath);
calcBip32Seed(phrase, passphrase, derivationPath);
displayBip32Info(); displayBip32Info();
hidePending(); hidePending();
} }
@@ -14966,9 +15012,16 @@ var Mnemonic = function(language) {
return words; return words;
} }
function calcBip32Seed(phrase, passphrase, path) { function calcBip32RootKeyFromSeed(phrase, passphrase) {
seed = mnemonic.toSeed(phrase, passphrase); seed = mnemonic.toSeed(phrase, passphrase);
bip32RootKey = bitcoin.HDNode.fromSeedHex(seed, network); bip32RootKey = bitcoin.HDNode.fromSeedHex(seed, network);
}
function calcBip32RootKeyFromBase58(rootKeyBase58) {
bip32RootKey = bitcoin.HDNode.fromBase58(rootKeyBase58);
}
function calcBip32ExtendedKey(path) {
bip32ExtendedKey = bip32RootKey; bip32ExtendedKey = bip32RootKey;
// Derive the key from the path // Derive the key from the path
var pathBits = path.split("/"); var pathBits = path.split("/");
@@ -15031,6 +15084,16 @@ var Mnemonic = function(language) {
return false; return false;
} }
function validateRootKey(rootKeyBase58) {
try {
bitcoin.HDNode.fromBase58(rootKeyBase58);
}
catch (e) {
return "Invalid root key";
}
return "";
}
function getDerivationPath() { function getDerivationPath() {
if (DOM.bip44tab.hasClass("active")) { if (DOM.bip44tab.hasClass("active")) {
var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44); var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44);

View File

@@ -105,7 +105,7 @@
<div class="form-group"> <div class="form-group">
<label for="root-key" class="col-sm-2 control-label">BIP32 Root Key</label> <label for="root-key" class="col-sm-2 control-label">BIP32 Root Key</label>
<div class="col-sm-10"> <div class="col-sm-10">
<textarea id="root-key" class="root-key form-control" readonly="readonly"></textarea> <textarea id="root-key" class="root-key form-control"></textarea>
</div> </div>
</div> </div>
</form> </form>

View File

@@ -12,6 +12,7 @@
var showPrivKey = true; var showPrivKey = true;
var phraseChangeTimeoutEvent = null; var phraseChangeTimeoutEvent = null;
var rootKeyChangedTimeoutEvent = null;
var DOM = {}; var DOM = {};
DOM.network = $(".network"); DOM.network = $(".network");
@@ -50,12 +51,13 @@
DOM.passphrase.on("input", delayedPhraseChanged); DOM.passphrase.on("input", delayedPhraseChanged);
DOM.generate.on("click", generateClicked); DOM.generate.on("click", generateClicked);
DOM.more.on("click", showMore); DOM.more.on("click", showMore);
DOM.bip32path.on("input", delayedPhraseChanged); DOM.rootKey.on("input", delayedRootKeyChanged);
DOM.bip44purpose.on("input", delayedPhraseChanged); DOM.bip32path.on("input", calcForDerivationPath);
DOM.bip44coin.on("input", delayedPhraseChanged); DOM.bip44purpose.on("input", calcForDerivationPath);
DOM.bip44account.on("input", delayedPhraseChanged); DOM.bip44coin.on("input", calcForDerivationPath);
DOM.bip44change.on("input", delayedPhraseChanged); DOM.bip44account.on("input", calcForDerivationPath);
DOM.tab.on("click", delayedPhraseChanged); DOM.bip44change.on("input", calcForDerivationPath);
DOM.tab.on("shown.bs.tab", calcForDerivationPath);
DOM.indexToggle.on("click", toggleIndexes); DOM.indexToggle.on("click", toggleIndexes);
DOM.addressToggle.on("click", toggleAddresses); DOM.addressToggle.on("click", toggleAddresses);
DOM.privateKeyToggle.on("click", togglePrivateKeys); DOM.privateKeyToggle.on("click", togglePrivateKeys);
@@ -70,7 +72,7 @@
function networkChanged(e) { function networkChanged(e) {
var network = e.target.value; var network = e.target.value;
networks[network].onSelect(); networks[network].onSelect();
delayedPhraseChanged(); displayBip32Info();
} }
function delayedPhraseChanged() { function delayedPhraseChanged() {
@@ -87,12 +89,57 @@
hideValidationError(); hideValidationError();
// Get the mnemonic phrase // Get the mnemonic phrase
var phrase = DOM.phrase.val(); var phrase = DOM.phrase.val();
var passphrase = DOM.passphrase.val();
var errorText = findPhraseErrors(phrase); var errorText = findPhraseErrors(phrase);
if (errorText) { if (errorText) {
showValidationError(errorText); showValidationError(errorText);
return; return;
} }
// Calculate and display
var passphrase = DOM.passphrase.val();
calcBip32RootKeyFromSeed(phrase, passphrase);
calcForDerivationPath();
hidePending();
}
function delayedRootKeyChanged() {
// Warn if there is an existing mnemonic or passphrase.
if (DOM.phrase.val().length > 0 || DOM.passphrase.val().length > 0) {
if (!confirm("This will clear existing mnemonic and passphrase")) {
DOM.rootKey.val(bip32RootKey);
return
}
}
hideValidationError();
showPending();
// Clear existing mnemonic and passphrase
DOM.phrase.val("");
DOM.passphrase.val("");
seed = null;
if (rootKeyChangedTimeoutEvent != null) {
clearTimeout(rootKeyChangedTimeoutEvent);
}
rootKeyChangedTimeoutEvent = setTimeout(rootKeyChanged, 400);
}
function rootKeyChanged() {
showPending();
hideValidationError();
// Validate the root key TODO
var rootKeyBase58 = DOM.rootKey.val();
var errorText = validateRootKey(rootKeyBase58);
if (errorText) {
showValidationError(errorText);
return;
}
// Calculate and display
calcBip32RootKeyFromBase58(rootKeyBase58);
calcForDerivationPath();
hidePending();
}
function calcForDerivationPath() {
showPending();
hideValidationError();
// Get the derivation path // Get the derivation path
var derivationPath = getDerivationPath(); var derivationPath = getDerivationPath();
var errorText = findDerivationPathErrors(derivationPath); var errorText = findDerivationPathErrors(derivationPath);
@@ -100,8 +147,7 @@
showValidationError(errorText); showValidationError(errorText);
return; return;
} }
// Calculate and display calcBip32ExtendedKey(derivationPath);
calcBip32Seed(phrase, passphrase, derivationPath);
displayBip32Info(); displayBip32Info();
hidePending(); hidePending();
} }
@@ -148,9 +194,16 @@
return words; return words;
} }
function calcBip32Seed(phrase, passphrase, path) { function calcBip32RootKeyFromSeed(phrase, passphrase) {
seed = mnemonic.toSeed(phrase, passphrase); seed = mnemonic.toSeed(phrase, passphrase);
bip32RootKey = bitcoin.HDNode.fromSeedHex(seed, network); bip32RootKey = bitcoin.HDNode.fromSeedHex(seed, network);
}
function calcBip32RootKeyFromBase58(rootKeyBase58) {
bip32RootKey = bitcoin.HDNode.fromBase58(rootKeyBase58);
}
function calcBip32ExtendedKey(path) {
bip32ExtendedKey = bip32RootKey; bip32ExtendedKey = bip32RootKey;
// Derive the key from the path // Derive the key from the path
var pathBits = path.split("/"); var pathBits = path.split("/");
@@ -213,6 +266,16 @@
return false; return false;
} }
function validateRootKey(rootKeyBase58) {
try {
bitcoin.HDNode.fromBase58(rootKeyBase58);
}
catch (e) {
return "Invalid root key";
}
return "";
}
function getDerivationPath() { function getDerivationPath() {
if (DOM.bip44tab.hasClass("active")) { if (DOM.bip44tab.hasClass("active")) {
var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44); var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44);