From: Ian Coleman Date: Wed, 2 Aug 2017 07:02:43 +0000 (+1000) Subject: Add BIP49 tab X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git;a=commitdiff_plain;h=6c08f364b30e711feac698546041388325a11834 Add BIP49 tab --- diff --git a/src/index.html b/src/index.html index 6a5e644..31e8a2e 100644 --- a/src/index.html +++ b/src/index.html @@ -282,6 +282,9 @@
  • BIP44
  • +
  • + BIP49 +
  • @@ -435,6 +438,96 @@
    +
    +
    +
    + +
    +
    +
    +

    + For more info see the + BIP49 spec. +

    +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    + +
    +

    The account extended keys can be used for importing to most BIP49 compatible wallets.

    +
    +
    +
    + +
    + +
    +
    +
    + +
    + +
    +
    +
    + +
    +

    The BIP32 derivation path and extended keys are the basis for the derived addresses.

    +
    +
    +
    + +
    + +
    +
    +
    +
    +
    diff --git a/src/js/bitcoinjs-extensions.js b/src/js/bitcoinjs-extensions.js index ac2123c..1b40135 100644 --- a/src/js/bitcoinjs-extensions.js +++ b/src/js/bitcoinjs-extensions.js @@ -31,7 +31,7 @@ bitcoinjs.bitcoin.networks.clam = { wif: 0x85, }; -bitcoin.networks.crown = { +bitcoinjs.bitcoin.networks.crown = { messagePrefix: "unused", bip32: { public: 0x0488b21e, @@ -42,7 +42,7 @@ bitcoin.networks.crown = { wif: 0x80, }; -bitcoin.networks.dash = { +bitcoinjs.bitcoin.networks.dash = { messagePrefix: "unused", bip32: { public: 0x0488b21e, diff --git a/src/js/index.js b/src/js/index.js index 763967c..0129808 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -47,8 +47,10 @@ DOM.extendedPubKey = $(".extended-pub-key"); DOM.bip32tab = $("#bip32-tab"); DOM.bip44tab = $("#bip44-tab"); + DOM.bip49tab = $("#bip49-tab"); DOM.bip32panel = $("#bip32"); DOM.bip44panel = $("#bip44"); + DOM.bip49panel = $("#bip49"); DOM.bip32path = $("#bip32-path"); DOM.bip44path = $("#bip44-path"); DOM.bip44purpose = $("#bip44 .purpose"); @@ -57,6 +59,15 @@ DOM.bip44accountXprv = $("#bip44 .account-xprv"); DOM.bip44accountXpub = $("#bip44 .account-xpub"); DOM.bip44change = $("#bip44 .change"); + DOM.bip49unavailable = $("#bip49 .unavailable"); + DOM.bip49available = $("#bip49 .available"); + DOM.bip49path = $("#bip49-path"); + DOM.bip49purpose = $("#bip49 .purpose"); + DOM.bip49coin = $("#bip49 .coin"); + DOM.bip49account = $("#bip49 .account"); + DOM.bip49accountXprv = $("#bip49 .account-xprv"); + DOM.bip49accountXpub = $("#bip49 .account-xpub"); + DOM.bip49change = $("#bip49 .change"); DOM.generatedStrength = $(".generate-container .strength"); DOM.hardenedAddresses = $(".hardened-addresses"); DOM.addresses = $(".addresses"); @@ -90,6 +101,8 @@ DOM.bip32path.on("input", calcForDerivationPath); DOM.bip44account.on("input", calcForDerivationPath); DOM.bip44change.on("input", calcForDerivationPath); + DOM.bip49account.on("input", calcForDerivationPath); + DOM.bip49change.on("input", calcForDerivationPath); DOM.tab.on("shown.bs.tab", calcForDerivationPath); DOM.hardenedAddresses.on("change", calcForDerivationPath); DOM.indexToggle.on("click", toggleIndexes); @@ -108,8 +121,17 @@ // Event handlers function networkChanged(e) { + clearDerivedKeys(); + clearAddressesList(); var networkIndex = e.target.value; - networks[networkIndex].onSelect(); + var network = networks[networkIndex]; + network.onSelect(); + if (network.bip49available) { + showBip49(); + } + else { + hideBip49(); + } if (seed != null) { phraseChanged(); } @@ -252,8 +274,13 @@ function calcForDerivationPath() { showPending(); + clearDerivedKeys(); clearAddressesList(); hideValidationError(); + // Don't show bip49 if it's selected but network doesn't support it + if (bip49TabSelected() && !networkHasBip49()) { + return; + } // Get the derivation path var derivationPath = getDerivationPath(); var errorText = findDerivationPathErrors(derivationPath); @@ -265,6 +292,9 @@ if (bip44TabSelected()) { displayBip44Info(); } + if (bip49TabSelected()) { + displayBip49Info(); + } displayBip32Info(); hidePending(); } @@ -438,6 +468,21 @@ console.log("Using derivation path from BIP44 tab: " + derivationPath); return derivationPath; } + if (bip49TabSelected()) { + var purpose = parseIntNoNaN(DOM.bip49purpose.val(), 49); + var coin = parseIntNoNaN(DOM.bip49coin.val(), 0); + var account = parseIntNoNaN(DOM.bip49account.val(), 0); + var change = parseIntNoNaN(DOM.bip49change.val(), 0); + var path = "m/"; + path += purpose + "'/"; + path += coin + "'/"; + path += account + "'/"; + path += change; + DOM.bip49path.val(path); + var derivationPath = DOM.bip49path.val(); + console.log("Using derivation path from BIP49 tab: " + derivationPath); + return derivationPath; + } else if (bip32TabSelected()) { var derivationPath = DOM.bip32path.val(); console.log("Using derivation path from BIP32 tab: " + derivationPath); @@ -513,6 +558,24 @@ DOM.bip44accountXpub.val(accountXpub); } + function displayBip49Info() { + // Get the derivation path for the account + var purpose = parseIntNoNaN(DOM.bip49purpose.val(), 49); + var coin = parseIntNoNaN(DOM.bip49coin.val(), 0); + var account = parseIntNoNaN(DOM.bip49account.val(), 0); + var path = "m/"; + path += purpose + "'/"; + path += coin + "'/"; + path += account + "'/"; + // Calculate the account extended keys + var accountExtendedKey = calcBip32ExtendedKey(path); + var accountXprv = accountExtendedKey.toBase58(); + var accountXpub = accountExtendedKey.neutered().toBase58(); + // Display the extended keys + DOM.bip49accountXprv.val(accountXprv); + DOM.bip49accountXpub.val(accountXpub); + } + function displayBip32Info() { // Display the key DOM.seed.val(seed); @@ -555,6 +618,8 @@ var self = this; this.shouldGenerate = true; var useHardenedAddresses = DOM.hardenedAddresses.prop("checked"); + var isBip49 = bip49TabSelected(); + var bip49available = networkHasBip49(); function init() { calculateValues(); @@ -598,6 +663,17 @@ privkey = convertRipplePriv(privkey); address = convertRippleAdrr(address); } + // BIP49 addresses are different + if (isBip49) { + if (!bip49available) { + return; + } + var keyhash = bitcoinjs.bitcoin.crypto.hash160(key.getPublicKeyBuffer()); + var scriptsig = bitcoinjs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash); + var addressbytes = bitcoinjs.bitcoin.crypto.hash160(scriptsig); + var scriptpubkey = bitcoinjs.bitcoin.script.scriptHash.output.encode(addressbytes); + address = bitcoinjs.bitcoin.address.fromOutputScript(scriptpubkey, network) + } addAddressToList(indexText, address, pubkey, privkey); }, 50) } @@ -1063,145 +1139,188 @@ return DOM.bip32tab.hasClass("active"); } + function networkHasBip49() { + return networks[DOM.network.val()].bip49available; + } + + function bip49TabSelected() { + return DOM.bip49tab.hasClass("active"); + } + + function setHdCoin(coinValue) { + DOM.bip44coin.val(coinValue); + DOM.bip49coin.val(coinValue); + } + + function showBip49() { + DOM.bip49unavailable.addClass("hidden"); + DOM.bip49available.removeClass("hidden"); + } + + function hideBip49() { + DOM.bip49available.addClass("hidden"); + DOM.bip49unavailable.removeClass("hidden"); + } + var networks = [ { name: "BTC - Bitcoin", + bip49available: true, onSelect: function() { network = bitcoinjs.bitcoin.networks.bitcoin; - DOM.bip44coin.val(0); + setHdCoin(0); }, }, { name: "BTC - Bitcoin Testnet", + bip49available: true, onSelect: function() { network = bitcoinjs.bitcoin.networks.testnet; - DOM.bip44coin.val(1); + setHdCoin(1); }, }, { name: "CLAM - Clams", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.clam; - DOM.bip44coin.val(23); + setHdCoin(23); }, }, { name: "CRW - Crown", + bip49available: false, onSelect: function() { - network = bitcoin.networks.crown; - DOM.bip44coin.val(72); + network = bitcoinjs.bitcoin.networks.crown; + setHdCoin(72); }, }, { name: "DASH - Dash", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.dash; - DOM.bip44coin.val(5); + setHdCoin(5); }, }, { name: "DASH - Dash Testnet", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.dashtn; - DOM.bip44coin.val(1); + setHdCoin(1); }, }, { name: "DOGE - Dogecoin", + bip49available: false, onSelect: function() { - network = bitcoin.networks.dogecoin; - DOM.bip44coin.val(3); + network = bitcoinjs.bitcoin.networks.dogecoin; + setHdCoin(3); }, }, { name: "ETH - Ethereum", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.bitcoin; - DOM.bip44coin.val(60); + setHdCoin(60); }, }, { name: "GAME - GameCredits", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.game; - DOM.bip44coin.val(101); + setHdCoin(101); }, }, { name: "JBS - Jumbucks", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.jumbucks; - DOM.bip44coin.val(26); + setHdCoin(26); }, }, { name: "LTC - Litecoin", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.litecoin; - DOM.bip44coin.val(2); + setHdCoin(2); }, }, { name: "NMC - Namecoin", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.namecoin; - DOM.bip44coin.val(7); + setHdCoin(7); }, }, { name: "PPC - Peercoin", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.peercoin; - DOM.bip44coin.val(6); + setHdCoin(6); }, }, { name: "SDC - ShadowCash", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.shadow; - DOM.bip44coin.val(35); + setHdCoin(35); }, }, { name: "SDC - ShadowCash Testnet", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.shadowtn; - DOM.bip44coin.val(1); + setHdCoin(1); }, }, { name: "SLM - Slimcoin", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.slimcoin; - DOM.bip44coin.val(63); + setHdCoin(63); }, }, { name: "SLM - Slimcoin Testnet", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.slimcointn; - DOM.bip44coin.val(111); + setHdCoin(111); }, }, { name: "VIA - Viacoin", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.viacoin; - DOM.bip44coin.val(14); + setHdCoin(14); }, }, { name: "VIA - Viacoin Testnet", + bip49available: false, onSelect: function() { network = bitcoinjs.bitcoin.networks.viacointestnet; - DOM.bip44coin.val(1); + setHdCoin(1); }, }, { name: "XRP - Ripple", + bip49available: false, onSelect: function() { - network = bitcoin.networks.bitcoin; - DOM.bip44coin.val(144); + network = bitcoinjs.bitcoin.networks.bitcoin; + setHdCoin(144); }, } ] diff --git a/tests.js b/tests.js index ea29043..b6713b0 100644 --- a/tests.js +++ b/tests.js @@ -3726,6 +3726,280 @@ page.open(url, function(status) { }); }, +// BIP49 official test vectors +// https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki#test-vectors +function() { +page.open(url, function(status) { + // set the phrase and select bitcoin testnet + var expected = "2Mww8dCYPUpKHofjgcXcBCEGmniw9CoaiD2"; + page.evaluate(function() { + $("#bip49-tab a").click(); + $(".phrase").val("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"); + $(".network option[selected]").removeAttr("selected"); + $(".network option").filter(function() { + return $(this).html() == "BTC - Bitcoin Testnet"; + }).prop("selected", true); + $(".network").trigger("change"); + $(".phrase").trigger("input"); + }); + // check the first address + waitForGenerate(function() { + var actual = page.evaluate(function() { + return $(".address:first").text(); + }); + if (actual != expected) { + console.log("BIP49 address is incorrect"); + console.log("Expected: " + expected); + console.log("Actual: " + actual); + fail(); + } + next(); + }); +}); +}, + +// BIP49 derivation path is shown +function() { +page.open(url, function(status) { + // set the phrase + var expected = "m/49'/0'/0'/0"; + page.evaluate(function() { + $("#bip49-tab a").click(); + $(".phrase").val("abandon abandon ability").trigger("input"); + }); + // check the derivation path of the first address + waitForGenerate(function() { + var actual = page.evaluate(function() { + return $("#bip49 .path").val(); + }); + if (actual != expected) { + console.log("BIP49 derivation path is incorrect"); + console.log("Expected: " + expected); + console.log("Actual: " + actual); + fail(); + } + next(); + }); +}); +}, + +// BIP49 extended private key is shown +function() { +page.open(url, function(status) { + // set the phrase + var expected = "xprvA1hukYsW7QfX9CVsaDAKde4eryajKa4DKWb6m9YjSnqkiZHrahFwwTJfEQTwBQ5kptWT5pZMkkusT1oK8dc1efQ8VFfq4SLSPAWd7Cpt423"; + page.evaluate(function() { + $("#bip49-tab a").click(); + $(".phrase").val("abandon abandon ability").trigger("input"); + }); + // check the BIP49 extended private key + waitForGenerate(function() { + var actual = page.evaluate(function() { + return $(".extended-priv-key").val(); + }); + if (actual != expected) { + console.log("BIP49 extended private key is incorrect"); + console.log("Expected: " + expected); + console.log("Actual: " + actual); + fail(); + } + next(); + }); +}); +}, + +// BIP49 extended public key is shown +function() { +page.open(url, function(status) { + // set the phrase + var expected = "xpub6EhGA4QPwnDpMgaLgEhKzn1PR1RDj2n4gjWhZXxM18NjbMd18EaCVFd95gkLARJaBD2rXAYJED2gdkUbGn1KkrSzCKR554AdABUELoainnt"; + page.evaluate(function() { + $("#bip49-tab a").click(); + $(".phrase").val("abandon abandon ability").trigger("input"); + }); + // check the BIP49 extended public key + waitForGenerate(function() { + var actual = page.evaluate(function() { + return $(".extended-pub-key").val(); + }); + if (actual != expected) { + console.log("BIP49 extended public key is incorrect"); + console.log("Expected: " + expected); + console.log("Actual: " + actual); + fail(); + } + next(); + }); +}); +}, + +// BIP49 account field changes address list +function() { +page.open(url, function(status) { + // set the phrase + var expected = "381wg1GGN4rP88rNC9v7QWsiww63yLVPsn"; + page.evaluate(function() { + $("#bip49-tab a").click(); + $(".phrase").val("abandon abandon ability").trigger("input"); + }); + waitForGenerate(function() { + // change the bip49 account field to 1 + page.evaluate(function() { + $("#bip49 .account").val("1"); + $("#bip49 .account").trigger("input"); + }); + waitForGenerate(function() { + // check the address for the new derivation path + var actual = page.evaluate(function() { + return $(".address:first").text(); + }); + if (actual != expected) { + console.log("BIP49 account field generates incorrect address"); + console.log("Expected: " + expected); + console.log("Actual: " + actual); + fail(); + } + next(); + }); + }); +}); +}, + +// BIP49 change field changes address list +function() { +page.open(url, function(status) { + // set the phrase + var expected = "3PEM7MiKed5konBoN66PQhK8r3hjGhy9dT"; + page.evaluate(function() { + $("#bip49-tab a").click(); + $(".phrase").val("abandon abandon ability").trigger("input"); + }); + waitForGenerate(function() { + // change the bip49 change field to 1 + page.evaluate(function() { + $("#bip49 .change").val("1"); + $("#bip49 .change").trigger("input"); + }); + waitForGenerate(function() { + // check the address for the new derivation path + var actual = page.evaluate(function() { + return $(".address:first").text(); + }); + if (actual != expected) { + console.log("BIP49 change field generates incorrect address"); + console.log("Expected: " + expected); + console.log("Actual: " + actual); + fail(); + } + next(); + }); + }); +}); +}, + +// BIP49 account extendend private key is shown +function() { +page.open(url, function(status) { + // set the phrase + var expected = "xprv9y3uhgQbfQZbj3o98nfgLDwGGuCJjUn7GKArSAZXjKgMjSdYHjQmTyf78s22g6jsGrxXvHB6HJeFyvFSPkuYZajeTGMZVXV6aNLWw2fagCn"; + page.evaluate(function() { + $("#bip49-tab a").click(); + $(".phrase").val("abandon abandon ability"); + $(".phrase").trigger("input"); + }); + // check the BIP49 account extended private key + waitForGenerate(function() { + var actual = page.evaluate(function() { + return $("#bip49 .account-xprv").val(); + }); + if (actual != expected) { + console.log("BIP49 account extended private key is incorrect"); + console.log("Expected: " + expected); + console.log("Actual: " + actual); + fail(); + } + next(); + }); +}); +}, + +// BIP49 account extendend public key is shown +function() { +page.open(url, function(status) { + // set the phrase + var expected = "xpub6C3G7BwVVn7twXscEpCghMszpw2o8wVxdY6TEYy9HfDLcExgqGj21myazAiq6HSmW2F1cBiFqJa3D1cqcDpSh8pbZF5x4iqpd4PyJvd3gjB"; + page.evaluate(function() { + $("#bip49-tab a").click(); + $(".phrase").val("abandon abandon ability"); + $(".phrase").trigger("input"); + }); + // check the BIP49 account extended public key + waitForGenerate(function() { + var actual = page.evaluate(function() { + return $("#bip49 .account-xpub").val(); + }); + if (actual != expected) { + console.log("BIP49 account extended public key is incorrect"); + console.log("Expected: " + expected); + console.log("Actual: " + actual); + fail(); + } + next(); + }); +}); +}, + +// Test selecting coin where bip49 is unavailable (eg CLAM) +function() { +page.open(url, function(status) { + // set the phrase + page.evaluate(function() { + $("#bip49-tab a").click(); + $(".phrase").val("abandon abandon ability"); + $(".phrase").trigger("input"); + }); + waitForGenerate(function() { + // select non-bip49 network, ie CLAM network + page.evaluate(function() { + $(".network option[selected]").removeAttr("selected"); + $(".network option").filter(function() { + return $(this).html() == "CLAM - Clams"; + }).prop("selected", true); + $(".network").trigger("change"); + }); + // check the BIP49 error is shown + var bip49ErrorShown = page.evaluate(function() { + var bip49hidden = $("#bip49 .available").hasClass("hidden"); + bip49hidden = bip49hidden && !($("#bip49 .unavailable").hasClass("hidden")); + return bip49hidden; + }); + if (!bip49ErrorShown) { + console.log("BIP49 error not shown for non-bip49 network"); + fail(); + } + // check there are no addresses shown + var addressCount = page.evaluate(function() { + return $(".address").length; + }); + if (addressCount != 0) { + console.log("BIP49 address count for non-bip49 network is " + addressCount); + fail(); + } + // check the derived keys are blank + var areBlank = page.evaluate(function() { + var prvKeyIsBlank = $(".extended-priv-key").val().length == 0; + var pubKeyIsBlank = $(".extended-pub-key").val().length == 0; + return prvKeyIsBlank && pubKeyIsBlank; + }); + if (!areBlank) { + console.log("BIP49 extended keys for non-bip49 network are not blank "); + fail(); + } + next(); + }); +}); +}, + // If you wish to add more tests, do so here... // Here is a blank test template