Skip to content

Commit 6c604bc

Browse files
committed
Add possibility to specify HMAC SHA-256 iterations for key generation and show the seed in WIF format.
1 parent 2982634 commit 6c604bc

3 files changed

Lines changed: 30 additions & 7 deletions

File tree

index.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,21 @@ <h2>New HD Address <small>making bip32 even easier</small></h2>
582582
</span>
583583
</div>
584584

585+
<label>Seed (WIF)</label>
586+
<div class="input-group">
587+
<input id="newHDseed" type="text" class="form-control" value="" readonly>
588+
<span class="input-group-btn">
589+
<button class="deriveHDbtn btn btn-default" type="button"><span title="Derive from key" class="glyphicon glyphicon-chevron-right"></span></button>
590+
</span>
591+
</div>
592+
585593
<h3>Address Options</h3>
586594
<p>You can use the advanced options below to generate different kinds of master addresses.</p>
587595

588596
<div class="checkbox">
589597
<label><input type="checkbox" id="newHDBrainwallet" class="checkbox-inline"> Custom Seed or Brain Wallet</label>
590598
<input type="text" class="form-control hidden" id="HDBrainwallet">
599+
<input type="text" class="form-control hidden" id="HDBrainwalletIters" value="50000" title="Number of HMAC SHA-256 iterations for generation, with value 0 just calculate SHA-256 like before.">
591600
</div>
592601

593602
<input type="button" class="btn btn-primary" value="Generate" id="newHDKeysBtn">
@@ -1153,7 +1162,6 @@ <h4>HD Address</h4>
11531162
<input type="text" class="form-control chain_code" value="" readonly>
11541163
</div>
11551164

1156-
11571165
<div class="col-md-5">
11581166
<b>Key</b><br>
11591167
<input type="text" class="form-control hdwifkey" value="" readonly>

js/coin.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@
608608
r.parent_fingerprint = bytes.slice(5, 9);
609609
r.child_index = coinjs.uint(bytes.slice(9, 13), 4);
610610
r.chain_code = bytes.slice(13, 45);
611+
r.seed_wif = '';
611612
r.key_bytes = bytes.slice(45, 78);
612613

613614
var c = coinjs.compressed; // get current default
@@ -746,17 +747,23 @@
746747
}
747748

748749
// make a master hd xprv/xpub
749-
r.master = function(pass) {
750+
r.master = function(pass, iters) {
750751
if (pass) {
751-
var seed = Crypto.util.hexToBytes("0000000000000000000000000000000000000000000000000000000000000000");
752-
for (var i = 0; i < 50000; i++) {
753-
seed = Crypto.HMAC(Crypto.SHA256, seed, pass, { asBytes: true });
752+
var seed_iters = (iters) ? Math.abs(iters * 1) : 0;
753+
if (seed_iters == 0) {
754+
var seed = Crypto.SHA256(pass);
755+
} else {
756+
var seed = Crypto.util.hexToBytes("0000000000000000000000000000000000000000000000000000000000000000");
757+
for (var i = 0; i < seed_iters; i++) {
758+
seed = Crypto.HMAC(Crypto.SHA256, seed, pass, { asBytes: true });
759+
}
760+
seed = Crypto.util.bytesToHex(seed);
754761
}
755-
seed = Crypto.util.bytesToHex(seed);
756762
} else {
757763
var seed = coinjs.newPrivkey();
758764
}
759765

766+
var seed_wif = coinjs.privkey2wif(seed);
760767
var hasher = new jsSHA(seed, 'HEX');
761768
var I = hasher.getHMAC("Bitcoin seed", "TEXT", "SHA-512", "HEX");
762769

@@ -768,6 +775,7 @@
768775
'parent_fingerprint':[0,0,0,0],
769776
'child_index':0,
770777
'chain_code':chain,
778+
'seed_wif':seed_wif,
771779
'privkey':I.slice(0, 64),
772780
'pubkey':coinjs.newPubkey(I.slice(0, 64))});
773781
}
@@ -812,6 +820,8 @@
812820
var ret = pub.concat(checksum);
813821
o.pubkey = coinjs.base58encode(ret);
814822
}
823+
824+
o.seed_wif = data.seed_wif;
815825
return o;
816826
}
817827

js/coinbin.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,18 +570,22 @@ $(document).ready(function() {
570570
$("#newHDKeysBtn").click(function(){
571571
coinjs.compressed = true;
572572
var s = ($("#newHDBrainwallet").is(":checked")) ? $("#HDBrainwallet").val() : null;
573+
var siters = ($("#newHDBrainwallet").is(":checked")) ? $("#HDBrainwalletIters").val()*1 : null;
573574
var hd = coinjs.hd();
574-
var pair = hd.master(s);
575+
var pair = hd.master(s, siters);
575576
$("#newHDxpub").val(pair.pubkey);
576577
$("#newHDxprv").val(pair.privkey);
578+
$("#newHDseed").val(pair.seed_wif);
577579

578580
});
579581

580582
$("#newHDBrainwallet").click(function(){
581583
if($(this).is(":checked")){
582584
$("#HDBrainwallet").removeClass("hidden");
585+
$("#HDBrainwalletIters").removeClass("hidden");
583586
} else {
584587
$("#HDBrainwallet").addClass("hidden");
588+
$("#HDBrainwalletIters").addClass("hidden");
585589
}
586590
});
587591

@@ -1681,6 +1685,7 @@ $(document).ready(function() {
16811685
if(hex == hex_cmp_prv || hex == hex_cmp_pub){
16821686
var hd = coinjs.hd(s);
16831687
$("#verifyHDaddress .hdKey").html(s);
1688+
$("#verifyHDaddress .seed_wif").val(hd.seed_wif);
16841689
$("#verifyHDaddress .chain_code").val(Crypto.util.bytesToHex(hd.chain_code));
16851690
$("#verifyHDaddress .depth").val(hd.depth);
16861691
$("#verifyHDaddress .version").val('0x'+(hd.version).toString(16));

0 commit comments

Comments
 (0)