Skip to content

Commit

Permalink
Allow HD master root key to be imported in the wallet-import tool
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Jul 11, 2019
1 parent f3aa615 commit 23f5b42
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
25 changes: 22 additions & 3 deletions libs/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -4094,8 +4094,20 @@ angular.module('app').run(['$templateCache', function($templateCache) {
"<div class=\"well\">\n" +
" <form class=\"form-horizontal\">\n" +
"\n" +
" <!-- mnemonic -->\n" +
" <!-- mode -->\n" +
" <div class=\"form-group\">\n" +
" <label class=\"col-sm-3 control-label\">Import mode:</label>\n" +
" <div class=\"col-sm-9 input-group\">\n" +
" <select ng-model=\"vm.mode\"\n" +
" ng-options=\"mode.label for mode in vm.modes\"\n" +
" ng-change=\"vm.fromSeed()\"\n" +
" class=\"form-control\">\n" +
" </select>\n" +
" </div>\n" +
" </div>\n" +
"\n" +
" <!-- mnemonic -->\n" +
" <div class=\"form-group\" ng-if=\"vm.mode.id === 'mnemonic'\">\n" +
" <label class=\"col-sm-3 control-label\">Seed Mnemonic (BIP39):</label>\n" +
" <div class=\"col-sm-9 input-group\">\n" +
" <input class=\"form-control\" ng-model=\"vm.mnemonic\" ng-change=\"vm.fromMnemonic()\">\n" +
Expand All @@ -4104,7 +4116,7 @@ angular.module('app').run(['$templateCache', function($templateCache) {
" </div>\n" +
"\n" +
" <!-- passphrase -->\n" +
" <div class=\"form-group\">\n" +
" <div class=\"form-group\" ng-if=\"vm.mode.id === 'mnemonic'\">\n" +
" <label class=\"col-sm-3 control-label\">Passphrase:</label>\n" +
" <div class=\"col-sm-9 input-group\">\n" +
" <input class=\"form-control\"\n" +
Expand Down Expand Up @@ -4141,7 +4153,14 @@ angular.module('app').run(['$templateCache', function($templateCache) {
" <div class=\"form-group\">\n" +
" <label class=\"col-sm-3 control-label\">HD master root key:</label>\n" +
" <div class=\"col-sm-9 input-group as-block\">\n" +
" <input class=\"form-control\" value=\"{{vm.nodeBase58}}\" ng-readonly=\"true\">\n" +
" <input class=\"form-control\"\n" +
" style=\"width: 80%\"\n" +
" ng-model=\"vm.nodeBase58\"\n" +
" ng-readonly=\"vm.mode.id === 'mnemonic'\"\n" +
" ng-change=\"vm.fromBase58()\"\n" +
" ng-class=\"{'well-error': vm.mode.id === 'hdroot' && vm.error}\">\n" +
" <span class=\"input-group-addon\" ng-if=\"vm.mode.id === 'hdroot' && !vm.error\" style=\"width: 20%\">&lt;-- paste here to import.</span>\n" +
" <span class=\"input-group-addon well-error\" ng-if=\"vm.mode.id === 'hdroot' && vm.error\" style=\"width: 20%\"> {{vm.error}}</span>\n" +
" </div>\n" +
" </div>\n" +
"\n" +
Expand Down
25 changes: 22 additions & 3 deletions pages/wallet-import/wallet-import.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ <h4>Import HD wallet</h4>
<div class="well">
<form class="form-horizontal">

<!-- mnemonic -->
<!-- mode -->
<div class="form-group">
<label class="col-sm-3 control-label">Import mode:</label>
<div class="col-sm-9 input-group">
<select ng-model="vm.mode"
ng-options="mode.label for mode in vm.modes"
ng-change="vm.fromSeed()"
class="form-control">
</select>
</div>
</div>

<!-- mnemonic -->
<div class="form-group" ng-if="vm.mode.id === 'mnemonic'">
<label class="col-sm-3 control-label">Seed Mnemonic (BIP39):</label>
<div class="col-sm-9 input-group">
<input class="form-control" ng-model="vm.mnemonic" ng-change="vm.fromMnemonic()">
Expand All @@ -33,7 +45,7 @@ <h4>Import HD wallet</h4>
</div>

<!-- passphrase -->
<div class="form-group">
<div class="form-group" ng-if="vm.mode.id === 'mnemonic'">
<label class="col-sm-3 control-label">Passphrase:</label>
<div class="col-sm-9 input-group">
<input class="form-control"
Expand Down Expand Up @@ -70,7 +82,14 @@ <h4>Import HD wallet</h4>
<div class="form-group">
<label class="col-sm-3 control-label">HD master root key:</label>
<div class="col-sm-9 input-group as-block">
<input class="form-control" value="{{vm.nodeBase58}}" ng-readonly="true">
<input class="form-control"
style="width: 80%"
ng-model="vm.nodeBase58"
ng-readonly="vm.mode.id === 'mnemonic'"
ng-change="vm.fromBase58()"
ng-class="{'well-error': vm.mode.id === 'hdroot' && vm.error}">
<span class="input-group-addon" ng-if="vm.mode.id === 'hdroot' && !vm.error" style="width: 20%">&lt;-- paste here to import.</span>
<span class="input-group-addon well-error" ng-if="vm.mode.id === 'hdroot' && vm.error" style="width: 20%"> {{vm.error}}</span>
</div>
</div>

Expand Down
29 changes: 20 additions & 9 deletions pages/wallet-import/wallet-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ angular
bindings: {}
});

function WalletImportPageController(lodash, allNetworks) {
function WalletImportPageController(lodash, bitcoin, allNetworks, Buffer) {
const vm = this;

const PBKDF2_SALT = 'Digital Bitbox',
Expand Down Expand Up @@ -73,11 +73,17 @@ function WalletImportPageController(lodash, allNetworks) {
{label: 'bitcoin-cli importprivkey', id: 'importprivkey'},
{label: 'bitcoin-cli importpubkey', id: 'importpubkey'},
];
const MODES = [
{label: 'Import from BIP39 Mnemonic', id: 'mnemonic'},
{label: 'Import from BIP32 HD master root key', id: 'hdroot'},
];

vm.schemes = SCHEMES;
vm.scheme = SCHEMES[0];
vm.importTypes = TYPES;
vm.importType = TYPES[0];
vm.modes = MODES;
vm.mode = MODES[0];
vm.mnemonic = null;
vm.asPassword = true;
vm.passphrase = null;
Expand Down Expand Up @@ -109,7 +115,7 @@ function WalletImportPageController(lodash, allNetworks) {
if (vm.passphrase) {
if (vm.strenghtening.id === METHOD_PBKDF2) {
pw = bitcoin.pbkdf2.pbkdf2Sync(
bitcoin.Buffer.from(vm.passphrase, 'utf8'),
Buffer.from(vm.passphrase, 'utf8'),
PBKDF2_SALT,
PBKDF2_ROUNDS_APP,
PBKDF2_HMACLEN,
Expand All @@ -118,7 +124,7 @@ function WalletImportPageController(lodash, allNetworks) {
} else if (vm.strenghtening.id === METHOD_COINOMI) {
pw = vm.passphrase;
} else {
pw = bitcoin.Buffer.from(vm.passphrase, 'utf8').toString('hex');
pw = Buffer.from(vm.passphrase, 'utf8').toString('hex');
}
}
vm.seed = bitcoin.bip39.mnemonicToSeed(vm.mnemonic, pw);
Expand All @@ -135,6 +141,15 @@ function WalletImportPageController(lodash, allNetworks) {
vm.path = vm.scheme.path;
};

vm.fromBase58 = function () {
vm.error = null;
try {
vm.node = bitcoin.HDNode.fromBase58(vm.nodeBase58, vm.getConfig());
} catch (e) {
vm.error = e;
}
};

vm.getConfig = function () {
return angular.extend({}, vm.scheme.config, {bip32: {public: vm.scheme.pub, private: vm.scheme.prv}});
};
Expand Down Expand Up @@ -176,8 +191,7 @@ function WalletImportPageController(lodash, allNetworks) {
return str;
};

vm.getResultAsImportprivkey = function (rootNode, basePath, network) {
const date = new Date().toISOString();
vm.getResultAsImportprivkey = function (rootNode, basePath) {
const baseKey = rootNode.derivePath(basePath);
let str = `# Paste the following lines into a command line window.
# You might want to adjust the block number to rescan from at the bottom of the
Expand All @@ -189,16 +203,14 @@ function WalletImportPageController(lodash, allNetworks) {
for (let index = vm.indexStart; index <= vm.indexEnd; index++) {
const indexPath = `${index}${vm.path.indexOf('_idx_\'') >= 0 ? '\'' : ''}`;
const key = changeKey.derivePath(indexPath);
const addr = vm.getAddress(key.keyPair, network);
str += `bitcoin-cli importprivkey ${key.keyPair.toWIF()} "${basePath}/${changePath}/${indexPath}" false\n`;
}
}
str += 'bitcoin-cli rescanblockchain 500000\n';
return str;
};

vm.getResultAsImportpubkey = function (rootNode, basePath, network) {
const date = new Date().toISOString();
vm.getResultAsImportpubkey = function (rootNode, basePath) {
const baseKey = rootNode.derivePath(basePath);
let str = `# Paste the following lines into a command line window.
# You might want to adjust the block number to rescan from at the bottom of the
Expand All @@ -210,7 +222,6 @@ function WalletImportPageController(lodash, allNetworks) {
for (let index = vm.indexStart; index <= vm.indexEnd; index++) {
const indexPath = `${index}${vm.path.indexOf('_idx_\'') >= 0 ? '\'' : ''}`;
const key = changeKey.derivePath(indexPath);
const addr = vm.getAddress(key.keyPair, network);
str += `bitcoin-cli importpubkey ${key.keyPair.getPublicKeyBuffer().toString('hex')} "${basePath}/${changePath}/${indexPath}" false\n`;
}
}
Expand Down

0 comments on commit 23f5b42

Please sign in to comment.