Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ultraviolet
GitHub Repository: ultraviolet/bitaddress.org
Path: blob/master/src/ninja.splitwallet.js
248 views
1
ninja.wallets.splitwallet = {
2
isOpen: function () {
3
return (document.getElementById("splitwallet").className.indexOf("selected") != -1);
4
},
5
6
open: function () {
7
document.getElementById("splitarea").style.display = "block";
8
secrets.setRNG();
9
secrets.init(7); // 7 bits allows for up to 127 shares
10
},
11
12
close: function () {
13
document.getElementById("splitarea").style.display = "none";
14
},
15
16
mkOutputRow: function (s, id, lbltxt) {
17
var row = document.createElement("div");
18
var label = document.createElement("label");
19
label.innerHTML = lbltxt;
20
var qr = document.createElement("div");
21
var output = document.createElement("span");
22
output.setAttribute("class", "output");
23
output.innerHTML = s;
24
25
qr.setAttribute("id", id);
26
row.setAttribute("class", "splitsharerow");
27
row.appendChild(label);
28
row.appendChild(output);
29
row.appendChild(qr);
30
row.appendChild(document.createElement("br"));
31
32
return row;
33
},
34
35
stripLeadZeros: function (hex) { return hex.split(/^0+/).slice(-1)[0]; },
36
37
hexToBytes: function (hex) {
38
//if input has odd number of digits, pad it
39
if (hex.length % 2 == 1)
40
hex = "0" + hex;
41
for (var bytes = [], c = 0; c < hex.length; c += 2)
42
bytes.push(parseInt(hex.substr(c, 2), 16));
43
return bytes;
44
},
45
46
// Split a private key and update information in the HTML
47
splitKey: function () {
48
try {
49
var numshares = parseInt(document.getElementById('splitshares').value);
50
var threshold = parseInt(document.getElementById('splitthreshold').value);
51
var key = new Bitcoin.ECKey(false);
52
var bitcoinAddress = key.getBitcoinAddress();
53
var shares = ninja.wallets.splitwallet.getFormattedShares(key.getBitcoinHexFormat(), numshares, threshold);
54
55
var output = document.createElement("div");
56
output.setAttribute("id", "splitoutput");
57
var m = {};
58
output.appendChild(this.mkOutputRow(bitcoinAddress, "split_addr", "Bitcoin Address: "));
59
m["split_addr"] = bitcoinAddress;
60
61
for (var i = 0; i < shares.length; i++) {
62
var id = "split_qr_" + i;
63
output.appendChild(this.mkOutputRow(shares[i], id, "Share " + (i + 1) + ": "));
64
m[id] = shares[i];
65
}
66
67
document.getElementById("splitstep1area").innerHTML = output.innerHTML;
68
ninja.qrCode.showQrCode(m);
69
70
document.getElementById("splitstep1area").style.display = "block";
71
document.getElementById("splitstep1icon").setAttribute("class", "less");
72
}
73
catch (e) {
74
// browser does not have sufficient JavaScript support to generate a bitcoin address
75
alert(e);
76
}
77
},
78
79
// Combine shares of a private key to retrieve the key
80
combineShares: function () {
81
try {
82
document.getElementById("combinedprivatekey").innerHTML = "";
83
var shares = document.getElementById("combineinput").value.trim().split(/\W+/);
84
var combinedBytes = ninja.wallets.splitwallet.combineFormattedShares(shares);
85
var privkeyBase58 = new Bitcoin.ECKey(combinedBytes).getBitcoinWalletImportFormat();
86
document.getElementById("combinedprivatekey").innerHTML = privkeyBase58;
87
}
88
catch (e) {
89
alert(e);
90
}
91
},
92
93
// generate shares and format them in base58
94
getFormattedShares: function (key, numshares, threshold) {
95
var shares = secrets.share(key, numshares, threshold).map(ninja.wallets.splitwallet.hexToBytes).map(Bitcoin.Base58.encode);
96
return shares;
97
},
98
99
// combine base58 formatted shares and return a bitcoin byte array
100
combineFormattedShares: function (shares) {
101
var combined = secrets.combine(shares.map(Bitcoin.Base58.decode).map(Crypto.util.bytesToHex).map(ninja.wallets.splitwallet.stripLeadZeros));
102
return ninja.wallets.splitwallet.hexToBytes(combined);
103
},
104
105
openCloseStep: function (num) {
106
// do close
107
if (document.getElementById("splitstep" + num + "area").style.display == "block") {
108
document.getElementById("splitstep" + num + "area").style.display = "none";
109
document.getElementById("splitstep" + num + "icon").setAttribute("class", "more");
110
}
111
// do open
112
else {
113
document.getElementById("splitstep" + num + "area").style.display = "block";
114
document.getElementById("splitstep" + num + "icon").setAttribute("class", "less");
115
}
116
}
117
};
118
119