bitaddress.org/src/ninja.splitwallet.js

114 lines
3.9 KiB
JavaScript
Raw Normal View History

ninja.wallets.splitwallet = {
2014-03-16 21:43:16 +01:00
open: function () {
document.getElementById("splitarea").style.display = "block";
secrets.setRNG();
2014-04-14 06:27:29 +02:00
secrets.init(7); // 7 bits allows for up to 127 shares
2014-03-16 21:43:16 +01:00
},
close: function () {
document.getElementById("splitarea").style.display = "none";
},
mkOutputRow: function (s, id, lbltxt) {
var row = document.createElement("div");
var label = document.createElement("label");
2014-04-14 06:27:29 +02:00
label.innerHTML = lbltxt;
2014-03-16 21:43:16 +01:00
var qr = document.createElement("div");
2014-04-14 06:27:29 +02:00
var output = document.createElement("span");
output.setAttribute("class", "output");
output.innerHTML = s;
2014-03-16 21:43:16 +01:00
qr.setAttribute("id", id);
row.setAttribute("class", "splitsharerow");
row.appendChild(label);
2014-04-14 06:27:29 +02:00
row.appendChild(output);
2014-03-16 21:43:16 +01:00
row.appendChild(qr);
row.appendChild(document.createElement("br"));
return row;
},
stripLeadZeros: function (hex) { return hex.split(/^0+/).slice(-1)[0]; },
hexToBytes: function (hex) {
//if input has odd number of digits, pad it
if (hex.length % 2 == 1)
hex = "0" + hex;
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
},
// Split a private key and update information in the HTML
splitKey: function () {
try {
2014-04-14 06:27:29 +02:00
var numshares = parseInt(document.getElementById('splitshares').value);
var threshold = parseInt(document.getElementById('splitthreshold').value);
2014-03-16 21:43:16 +01:00
var key = new Bitcoin.ECKey(false);
var bitcoinAddress = key.getBitcoinAddress();
2014-04-14 06:27:29 +02:00
var shares = ninja.wallets.splitwallet.getFormattedShares(key.getBitcoinHexFormat(), numshares, threshold);
2014-03-16 21:43:16 +01:00
var output = document.createElement("div");
output.setAttribute("id", "splitoutput");
var m = {};
output.appendChild(this.mkOutputRow(bitcoinAddress, "split_addr", "Bitcoin Address: "));
m["split_addr"] = bitcoinAddress;
for (var i = 0; i < shares.length; i++) {
var id = "split_qr_" + i;
output.appendChild(this.mkOutputRow(shares[i], id, "Share " + (i + 1) + ": "));
m[id] = shares[i];
}
document.getElementById("splitstep1area").innerHTML = output.innerHTML;
ninja.qrCode.showQrCode(m);
document.getElementById("splitstep1area").style.display = "block";
document.getElementById("splitstep1icon").setAttribute("class", "less");
}
catch (e) {
// browser does not have sufficient JavaScript support to generate a bitcoin address
alert(e);
}
},
// Combine shares of a private key to retrieve the key
combineShares: function () {
try {
2014-04-14 06:27:29 +02:00
document.getElementById("combinedprivatekey").innerHTML = "";
2014-03-16 21:43:16 +01:00
var shares = document.getElementById("combineinput").value.trim().split(/\W+/);
2014-04-14 06:27:29 +02:00
var combinedBytes = ninja.wallets.splitwallet.combineFormattedShares(shares);
var privkeyBase58 = new Bitcoin.ECKey(combinedBytes).getBitcoinWalletImportFormat();
document.getElementById("combinedprivatekey").innerHTML = privkeyBase58;
2014-03-16 21:43:16 +01:00
}
catch (e) {
alert(e);
}
},
2014-04-14 06:27:29 +02:00
// generate shares and format them in base58
2014-03-16 21:43:16 +01:00
getFormattedShares: function (key, numshares, threshold) {
var shares = secrets.share(key, numshares, threshold).map(ninja.wallets.splitwallet.hexToBytes).map(Bitcoin.Base58.encode);
return shares;
},
2014-04-14 06:27:29 +02:00
// combine base58 formatted shares and return a bitcoin byte array
2014-03-16 21:43:16 +01:00
combineFormattedShares: function (shares) {
var combined = secrets.combine(shares.map(Bitcoin.Base58.decode).map(Crypto.util.bytesToHex).map(ninja.wallets.splitwallet.stripLeadZeros));
2014-04-14 06:27:29 +02:00
return ninja.wallets.splitwallet.hexToBytes(combined);
2014-03-16 21:43:16 +01:00
},
openCloseStep: function (num) {
// do close
if (document.getElementById("splitstep" + num + "area").style.display == "block") {
document.getElementById("splitstep" + num + "area").style.display = "none";
document.getElementById("splitstep" + num + "icon").setAttribute("class", "more");
}
// do open
else {
document.getElementById("splitstep" + num + "area").style.display = "block";
document.getElementById("splitstep" + num + "icon").setAttribute("class", "less");
}
}
};