Skip to content

Commit

Permalink
hd-wallet: add pubkey
Browse files Browse the repository at this point in the history
  • Loading branch information
guggero committed Apr 27, 2023
1 parent a2eea30 commit 2563355
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 15 deletions.
16 changes: 13 additions & 3 deletions libs/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -2694,7 +2694,7 @@ angular.module('app').run(['$templateCache', function($templateCache) {
" <label class=\"col-sm-3 control-label\">Extended public key (base58):</label>\n" +
" <div class=\"col-sm-9 input-group\">\n" +
" <input class=\"form-control\"\n" +
" value=\"{{vm.publicKeyWif}}\"\n" +
" value=\"{{vm.xPub}}\"\n" +
" ng-readonly=\"true\">\n" +
" </div>\n" +
" </div>\n" +
Expand Down Expand Up @@ -2769,16 +2769,26 @@ angular.module('app').run(['$templateCache', function($templateCache) {
" </div>\n" +
" </div>\n" +
"\n" +
" <!-- derived key base58 -->\n" +
" <!-- derived private key base58 -->\n" +
" <div class=\"form-group\">\n" +
" <label class=\"col-sm-3 control-label\">Derived key base58:</label>\n" +
" <label class=\"col-sm-3 control-label\">Derived private key base58:</label>\n" +
" <div class=\"col-sm-9 input-group\">\n" +
" <input class=\"form-control\"\n" +
" value=\"{{vm.derivedKey.toBase58()}}\"\n" +
" ng-readonly=\"true\">\n" +
" </div>\n" +
" </div>\n" +
"\n" +
" <!-- derived public key base58 -->\n" +
" <div class=\"form-group\">\n" +
" <label class=\"col-sm-3 control-label\">Derived public key base58:</label>\n" +
" <div class=\"col-sm-9 input-group\">\n" +
" <input class=\"form-control\"\n" +
" value=\"{{vm.derivedXPub}}\"\n" +
" ng-readonly=\"true\">\n" +
" </div>\n" +
" </div>\n" +
"\n" +
" <!-- priv key WIF -->\n" +
" <div class=\"form-group\">\n" +
" <label class=\"col-sm-3 control-label\">Private key (WIF, compressed):</label>\n" +
Expand Down
16 changes: 13 additions & 3 deletions pages/hd-wallet/hd-wallet.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ <h4>BIP39 mnemonic to BIP32 root key</h4>
<label class="col-sm-3 control-label">Extended public key (base58):</label>
<div class="col-sm-9 input-group">
<input class="form-control"
value="{{vm.publicKeyWif}}"
value="{{vm.xPub}}"
ng-readonly="true">
</div>
</div>
Expand Down Expand Up @@ -217,16 +217,26 @@ <h4>BIP32/44/49/84/341 key derivation</h4>
</div>
</div>

<!-- derived key base58 -->
<!-- derived private key base58 -->
<div class="form-group">
<label class="col-sm-3 control-label">Derived key base58:</label>
<label class="col-sm-3 control-label">Derived private key base58:</label>
<div class="col-sm-9 input-group">
<input class="form-control"
value="{{vm.derivedKey.toBase58()}}"
ng-readonly="true">
</div>
</div>

<!-- derived public key base58 -->
<div class="form-group">
<label class="col-sm-3 control-label">Derived public key base58:</label>
<div class="col-sm-9 input-group">
<input class="form-control"
value="{{vm.derivedXPub}}"
ng-readonly="true">
</div>
</div>

<!-- priv key WIF -->
<div class="form-group">
<label class="col-sm-3 control-label">Private key (WIF, compressed):</label>
Expand Down
56 changes: 47 additions & 9 deletions pages/hd-wallet/hd-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const calculatePath = function (bip, coinType, account, change, index) {
return path;
};

const deepCopy = function (obj) {
return JSON.parse(JSON.stringify(obj));
};

function HdWalletPageController(lodash, allNetworks) {
const vm = this;

Expand All @@ -41,17 +45,47 @@ function HdWalletPageController(lodash, allNetworks) {
vm.node = null;
vm.nodeBase58 = null;
vm.privKeyWif = null;
vm.publicKeyWif = null;
vm.xPub = null;
vm.address = null;
vm.account = 0;
vm.change = 0;
vm.index = 0;
vm.bips = [
{id: 0, label: 'BIP32 (Bitcoin Core)', bip: '32', hasCoinType: false, path: 'm/account\'/change\'/index'},
{id: 1, label: 'BIP44 (Legacy wallets, multi coin wallets)', bip: '44', hasCoinType: true, path: 'm/44\'/coin\'/account\'/change/index'},
{id: 2, label: 'BIP49 (SegWit P2SH-P2WPKH)', bip: '49', hasCoinType: true, path: 'm/49\'/coin\'/account\'/change/index'},
{id: 3, label: 'BIP84 (Native SegWit bech32 P2WPKH)', bip: '84', hasCoinType: true, path: 'm/84\'/coin\'/account\'/change/index'},
{id: 4, label: 'BIP86 (Native SegWit v1 bech32m P2TR)', bip: '86', hasCoinType: true, path: 'm/86\'/coin\'/account\'/change/index'},
{
id: 0, label: 'BIP32 (Bitcoin Core)', bip: '32', hasCoinType: false,
path: 'm/account\'/change\'/index', base58Prefixes: [
{public: 0x0488b21e, private: 0x0488ade4}, // xpub, xprv
{public: 0x043587cf, private: 0x04358394} // tpub, tprv
],
},
{
id: 1, label: 'BIP44 (Legacy wallets, multi coin wallets)', bip: '44', hasCoinType: true,
path: 'm/44\'/coin\'/account\'/change/index',base58Prefixes: [
{public: 0x0488b21e, private: 0x0488ade4}, // xpub, xprv
{public: 0x043587cf, private: 0x04358394} // tpub, tprv
],
},
{
id: 2, label: 'BIP49 (SegWit P2SH-P2WPKH)', bip: '49', hasCoinType: true,
path: 'm/49\'/coin\'/account\'/change/index',base58Prefixes: [
{public: 0x049d7cb2, private: 0x049d7878}, // ypub, yprv
{public: 0x044a5262, private: 0x044a4e28} // upub, uprv
],
},
{
id: 3, label: 'BIP84 (Native SegWit bech32 P2WPKH)', bip: '84', hasCoinType: true,
path: 'm/84\'/coin\'/account\'/change/index',base58Prefixes: [
{public: 0x04b24746, private: 0x04b2430c}, // zpub, zprv
{public: 0x045f1cf6, private: 0x045f18bc} // vpub, vprv
],
},
{
id: 4, label: 'BIP86 (Native SegWit v1 bech32m P2TR)', bip: '86', hasCoinType: true,
path: 'm/86\'/coin\'/account\'/change/index',base58Prefixes: [
{public: 0x0488b21e, private: 0x0488ade4}, // xpub, xprv
{public: 0x043587cf, private: 0x04358394} // tpub, tprv
],
},
];
vm.selectedBip = vm.bips[1];
vm.path = calculatePath(vm.selectedBip, vm.coinType, vm.account, vm.change, vm.index);
Expand Down Expand Up @@ -134,8 +168,11 @@ function HdWalletPageController(lodash, allNetworks) {
};

vm.fromNode = function () {
vm.publicKeyWif = vm.node.neutered().toBase58();
vm.address = getP2PKHAddress(vm.node, vm.network.config);
let configCopy = deepCopy(vm.network.config);
configCopy.bip32 = vm.selectedBip.base58Prefixes[configCopy.bip44];
vm.customNode = new bitcoin.bip32.fromPrivateKey(vm.node.privateKey, vm.node.chainCode, configCopy);
vm.xPub = vm.customNode.neutered().toBase58();
vm.address = getP2PKHAddress(vm.customNode, vm.network.config);
vm.calculatePath();
};

Expand All @@ -145,7 +182,8 @@ function HdWalletPageController(lodash, allNetworks) {
};

vm.fromPath = function () {
vm.derivedKey = vm.node.derivePath(vm.path);
vm.derivedKey = vm.customNode.derivePath(vm.path);
vm.derivedXPub = vm.derivedKey.neutered().toBase58();
calculateAddresses(vm.derivedKey, vm.network.config);
};

Expand Down

0 comments on commit 2563355

Please sign in to comment.