Skip to content

Commit

Permalink
Make HD wallet compatible with wallets generated on https://bip32.org
Browse files Browse the repository at this point in the history
…and bitcoin-cli.
  • Loading branch information
ok2 committed Apr 7, 2021
1 parent 9a0175e commit 2c8ee40
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
17 changes: 15 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1193,14 +1193,14 @@ <h4>Key Derivation</h4>
<div class="row">
<div class="col-md-8">
<b>Path</b><br>
<select class="form-control" id="hdpathtype"">
<select class="form-control" id="hdpathtype">
<option value="simple">Simple: m/i</option>
<option value="custom">Custom</option>
</select>

<div id="hdpath" class="hidden" style="margin-top:4px">
<span class="input-group">
<input type="text" class="form-control" value="m/0/1"> <br>
<input type="text" class="form-control" value="m/0/1" title="WARNING: see #settings page when using hardened paths!"> <br>
<span class="input-group-addon"> / </span>
</span>
</div>
Expand Down Expand Up @@ -1456,6 +1456,19 @@ <h2>Settings <small> making coinb.in even better!</small></h2>
</div>
</div>

<hr>

<div class="row">
<div class="col-md-12">
<b>HD wallet hardened path derivation</b>: <br>
<p class="text-muted">The path derivation for hardened paths was calculated wrong in earlier versions of coinb.in. Please select the old path derivation to recover HD wallet keys generated with older version of coinb.in.</p>
<select class="form-control" id="coinjs_derivation">
<option value="bip32_derivation">BIP32 compliant derivation</option>
<option value="coinbin_broken">Old (broken) coinb.in path derivation</option>
</select>
</div>
</div>

<br>

<div id="statusSettings" class="hidden alert">
Expand Down
8 changes: 7 additions & 1 deletion js/coin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

coinjs.compressed = false;

coinjs.hd_derivation = "bip32_derivation";

/* other vars */
coinjs.developer = '33tht1bKDgZVxb39MnZsWa8oxHXHvUYE4G'; //bitcoin

Expand Down Expand Up @@ -685,7 +687,11 @@
r.derive = function(i){

i = (i)?i:0;
var blob = (Crypto.util.hexToBytes(this.keys.pubkey)).concat(coinjs.numToBytes(i,4).reverse());
if ((i >= 0x80000000) && (coinjs.hd_derivation == "bip32_derivation")) {
var blob = (Crypto.util.hexToBytes("00").concat(Crypto.util.hexToBytes(this.keys.privkey)).concat(coinjs.numToBytes(i,4).reverse()));
} else {
var blob = (Crypto.util.hexToBytes(this.keys.pubkey)).concat(coinjs.numToBytes(i,4).reverse());
}

var j = new jsSHA(Crypto.util.bytesToHex(blob), 'HEX');
var hash = j.getHMAC(Crypto.util.bytesToHex(r.chain_code), "HEX", "SHA-512", "HEX");
Expand Down
17 changes: 14 additions & 3 deletions js/coinbin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1701,15 +1701,23 @@ $(document).ready(function() {

function deriveHDaddress() {
var hd = coinjs.hd($("#verifyHDaddress .hdKey").html());
var index_start = $("#verifyHDaddress .derivation_index_start").val()*1;
var index_end = $("#verifyHDaddress .derivation_index_end").val()*1;
var index_start = $("#verifyHDaddress .derivation_index_start").val();
if ((index_start.length > 1) && (index_start[index_start.length - 1] == '\'')) {
var use_private_index = '\'';
index_start = index_start.replace(/[']/, "") * 1;
} else {
var use_private_index = '';
index_start = index_start.replace(/[']/, "") * 1;
}
var index_end = $("#verifyHDaddress .derivation_index_end").val().replace(/[']/, "") * 1;
$("#verifyHDaddress .derivation_index_end").val(index_end + use_private_index);
var html = '';
$("#verifyHDaddress .derived_data table tbody").html("");
for(var i=index_start;i<=index_end;i++){
if($("#hdpathtype option:selected").val()=='simple'){
var derived = hd.derive(i);
} else {
var derived = hd.derive_path(($("#hdpath input").val().replace(/\/+$/, ""))+'/'+i);
var derived = hd.derive_path(($("#hdpath input").val().replace(/\/+$/, ""))+'/'+i+use_private_index);
}
html += '<tr>';
html += '<td>'+i+'</td>';
Expand Down Expand Up @@ -1914,6 +1922,8 @@ $(document).ready(function() {
coinjs.hdkey.pub = $("#coinjs_hdpub").val()*1;
coinjs.hdkey.prv = $("#coinjs_hdprv").val()*1;

coinjs.hd_derivation = $("#coinjs_derivation").val();

configureBroadcast();
configureGetUnspentTx();

Expand Down Expand Up @@ -1959,6 +1969,7 @@ $(document).ready(function() {
$("#coinjs_multisig").val(o[2]);
$("#coinjs_hdpub").val(o[3]);
$("#coinjs_hdprv").val(o[4]);
$("#coinjs_derivation").val(o[7]);

// hide/show custom screen
if($("option:selected",this).val()=="custom"){
Expand Down

0 comments on commit 2c8ee40

Please sign in to comment.