Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility in verify view for HD wallets to choose the address format #230

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ <h4>Key Derivation</h4>
<p>The path of key derivation</p>

<div class="row">
<div class="col-md-8">
<div class="col-md-6">
<b>Path</b><br>
<select class="form-control" id="hdpathtype"">
<option value="simple">Simple: m/i</option>
Expand All @@ -1216,6 +1216,15 @@ <h4>Key Derivation</h4>
<input type="text" class="form-control derivation_index_end" value="1">
</div>

<div class="col-md-2">
<b>Address format</b><br>
<select class="form-control derivation_addr_format">
<option value="bech32">Bech32</option>
<option value="segwit">SegWit</option>
<option value="legacy">Legacy</option>
</select>
</div>

</div>

<hr>
Expand All @@ -1226,7 +1235,7 @@ <h4>Keys</h4>
<div class="derived_data">
<table class="table table-striped table-hover">
<thead>
<tr><td><b>Index</b></td><td><b>Address</b><td><b>Private Key (WIF)</b></td></td><td><b>Extended xPub</b></td><td><b>Extended xPrv</b></td></tr>
<tr><td><b>Index</b></td><td><b>Address</b></td><td><b>Redeem script</b></td><td><b>Private Key (WIF)</b></td><td><b>Extended xPub</b></td><td><b>Extended xPrv</b></td></tr>
</thead>
<tbody>
</tbody>
Expand Down
33 changes: 28 additions & 5 deletions js/coin.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,20 @@
var privkey = (r.key_bytes).slice(1, 33);
var privkeyHex = Crypto.util.bytesToHex(privkey);
var pubkey = coinjs.newPubkey(privkeyHex);
var addr_format = $("#verifyHDaddress .derivation_addr_format").val();
if (addr_format == "bech32") {
var address = coinjs.bech32Address(pubkey);
} else if (addr_format == "segwit") {
var address = coinjs.segwitAddress(pubkey);
} else {
var address = {'address': coinjs.pubkey2address(pubkey),
'redeemscript': ''};
}

r.keys = {'privkey':privkeyHex,
'pubkey':pubkey,
'address':coinjs.pubkey2address(pubkey),
'address':address.address,
'script':address.redeemscript,
'wif':coinjs.privkey2wif(privkeyHex)};

} else if(r.key_bytes[0] == 0x02 || r.key_bytes[0] == 0x03) {
Expand Down Expand Up @@ -696,23 +706,34 @@
var ecparams = EllipticCurve.getSECCurveByName("secp256k1");
var curve = ecparams.getCurve();

var k, key, pubkey, o;
var k, key, pubkey, o, addr_format, address_fun, address;

o = coinjs.clone(this);
o.chain_code = ir;
o.child_index = i;

addr_format = $("#verifyHDaddress .derivation_addr_format").val();
if (addr_format == "bech32") {
address_fun = function(pk) { return coinjs.bech32Address(pk); };
} else if (addr_format == "segwit") {
address_fun = function(pk) { return coinjs.segwitAddress(pk); };
} else {
address_fun = function(pk) {
return {'address': coinjs.pubkey2address(pk), 'redeemscript': ''};
};
}
if(this.type=='private'){
// derive key pair from from a xprv key
k = il.add(new BigInteger([0].concat(Crypto.util.hexToBytes(this.keys.privkey)))).mod(ecparams.getN());
key = Crypto.util.bytesToHex(k.toByteArrayUnsigned());

pubkey = coinjs.newPubkey(key);

address = address_fun(pubkey);
o.keys = {'privkey':key,
'pubkey':pubkey,
'wif':coinjs.privkey2wif(key),
'address':coinjs.pubkey2address(pubkey)};
'address':address.address,
'script':address.redeemscript};

} else if (this.type=='public'){
// derive xpub key from an xpub key
Expand All @@ -729,9 +750,11 @@
publicKeyBytesCompressed.unshift(0x03)
}
pubkey = Crypto.util.bytesToHex(publicKeyBytesCompressed);
address = address_fun(pubkey);

o.keys = {'pubkey':pubkey,
'address':coinjs.pubkey2address(pubkey)}
'address':address.address,
'script':address.redeemscript}
} else {
// fail
}
Expand Down
1 change: 1 addition & 0 deletions js/coinbin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,7 @@ $(document).ready(function() {
html += '<tr>';
html += '<td>'+i+'</td>';
html += '<td><input type="text" class="form-control" value="'+derived.keys.address+'" readonly></td>';
html += '<td><input type="text" class="form-control" value="'+derived.keys.script+'" readonly></td>';
html += '<td><input type="text" class="form-control" value="'+((derived.keys.wif)?derived.keys.wif:'')+'" readonly></td>';
html += '<td><input type="text" class="form-control" value="'+derived.keys_extended.pubkey+'" readonly></td>';
html += '<td><input type="text" class="form-control" value="'+((derived.keys_extended.privkey)?derived.keys_extended.privkey:'')+'" readonly></td>';
Expand Down