Skip to content

Commit 6a6c2c6

Browse files
committed
In verify for HD wallets add field to choose the address format.
1 parent 9a0175e commit 6a6c2c6

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

index.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ <h4>Key Derivation</h4>
11911191
<p>The path of key derivation</p>
11921192

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

1219+
<div class="col-md-2">
1220+
<b>Address format</b><br>
1221+
<select class="form-control derivation_addr_format">
1222+
<option value="bech32">Bech32</option>
1223+
<option value="segwit">SegWit</option>
1224+
<option value="legacy">Legacy</option>
1225+
</select>
1226+
</div>
1227+
12191228
</div>
12201229

12211230
<hr>
@@ -1226,7 +1235,7 @@ <h4>Keys</h4>
12261235
<div class="derived_data">
12271236
<table class="table table-striped table-hover">
12281237
<thead>
1229-
<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>
1238+
<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>
12301239
</thead>
12311240
<tbody>
12321241
</tbody>

js/coin.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -618,10 +618,20 @@
618618
var privkey = (r.key_bytes).slice(1, 33);
619619
var privkeyHex = Crypto.util.bytesToHex(privkey);
620620
var pubkey = coinjs.newPubkey(privkeyHex);
621+
var addr_format = $("#verifyHDaddress .derivation_addr_format").val();
622+
if (addr_format == "bech32") {
623+
var address = coinjs.bech32Address(pubkey);
624+
} else if (addr_format == "segwit") {
625+
var address = coinjs.segwitAddress(pubkey);
626+
} else {
627+
var address = {'address': coinjs.pubkey2address(pubkey),
628+
'redeemscript': ''};
629+
}
621630

622631
r.keys = {'privkey':privkeyHex,
623632
'pubkey':pubkey,
624-
'address':coinjs.pubkey2address(pubkey),
633+
'address':address.address,
634+
'script':address.redeemscript,
625635
'wif':coinjs.privkey2wif(privkeyHex)};
626636

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

699-
var k, key, pubkey, o;
709+
var k, key, pubkey, o, addr_format, address_fun, address;
700710

701711
o = coinjs.clone(this);
702712
o.chain_code = ir;
703713
o.child_index = i;
704714

715+
addr_format = $("#verifyHDaddress .derivation_addr_format").val();
716+
if (addr_format == "bech32") {
717+
address_fun = function(pk) { return coinjs.bech32Address(pk); };
718+
} else if (addr_format == "segwit") {
719+
address_fun = function(pk) { return coinjs.segwitAddress(pk); };
720+
} else {
721+
address_fun = function(pk) {
722+
return {'address': coinjs.pubkey2address(pk), 'redeemscript': ''};
723+
};
724+
}
705725
if(this.type=='private'){
706726
// derive key pair from from a xprv key
707727
k = il.add(new BigInteger([0].concat(Crypto.util.hexToBytes(this.keys.privkey)))).mod(ecparams.getN());
708728
key = Crypto.util.bytesToHex(k.toByteArrayUnsigned());
709729

710730
pubkey = coinjs.newPubkey(key);
711-
731+
address = address_fun(pubkey);
712732
o.keys = {'privkey':key,
713733
'pubkey':pubkey,
714734
'wif':coinjs.privkey2wif(key),
715-
'address':coinjs.pubkey2address(pubkey)};
735+
'address':address.address,
736+
'script':address.redeemscript};
716737

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

733755
o.keys = {'pubkey':pubkey,
734-
'address':coinjs.pubkey2address(pubkey)}
756+
'address':address.address,
757+
'script':address.redeemscript}
735758
} else {
736759
// fail
737760
}

js/coinbin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,7 @@ $(document).ready(function() {
17141714
html += '<tr>';
17151715
html += '<td>'+i+'</td>';
17161716
html += '<td><input type="text" class="form-control" value="'+derived.keys.address+'" readonly></td>';
1717+
html += '<td><input type="text" class="form-control" value="'+derived.keys.script+'" readonly></td>';
17171718
html += '<td><input type="text" class="form-control" value="'+((derived.keys.wif)?derived.keys.wif:'')+'" readonly></td>';
17181719
html += '<td><input type="text" class="form-control" value="'+derived.keys_extended.pubkey+'" readonly></td>';
17191720
html += '<td><input type="text" class="form-control" value="'+((derived.keys_extended.privkey)?derived.keys_extended.privkey:'')+'" readonly></td>';

0 commit comments

Comments
 (0)