-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.html
225 lines (183 loc) · 6.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>bitimage</title>
<script type="text/javascript" src="bitcoinjs.min.js"></script>
<script type="text/javascript" src="bip32.min.js"></script>
<script type="text/javascript" src="bip39.min.js"></script>
<script type="text/javascript" src="bigi.min.js"></script>
<script type="text/javascript" src="crypto-js.min.js"></script>
<script type="text/javascript" src="qrcode.js"></script>
</head>
<body>
<header>
<h1>A picture is worth a thousand satoshis</h1>
</header>
<article>
<h2>Turn Any Image, Document or Audio File Into A BIP39 Mnemonic</h2>
<p>
1) BIP39 Passphrase (Optional):
<input type="text" id="passphrase" type="password" onmouseover="mouseoverPass();" onmouseout="mouseoutPass();">
</p>
<label for="files">2) Select a file you would like to turn into a Mnemonic Phrase:</label>
<input id="files" type="file" multiple/>
<p><a href="https://github.com/coreyphillips/bitimage" target='_blank' style="text-decoration: none;color:#29bb9c">View on GitHub</a></p>
<output id="result"/>
</article>
</body>
</html>
<style>
body {
font-size: 12pt;
word-wrap: break-word;
}
header {
background-color: #29bb9c;
}
header h1 {
width: 80%;
margin: auto;
font-size: 14pt;
color: #fff;
padding: 20px;
}
article {
width: 80%;
margin: auto;
margin-top: 10px;
}
.container {
display: table;
width: 100%;
}
.cell {
display: table-cell;
vertical-align: middle;
height: 200px;
}
.content {
float: left;
}
.content2 {
float: right;
}
.filename {
margin: 20px 10px 10px 0px;
}
.thumbnail {
height: 100px;
margin: 20px 10px 10px 0px;
}
#bitcoinQr {
margin-bottom: 0px;
}
#reveal-passphrase {
visibility: hidden;
font-weight: bold;
}
.encrypted-msg:hover #reveal-passphrase {
visibility: visible;
}
</style>
<script>
function mouseoverPass(obj) {
var obj = document.getElementById('passphrase');
obj.type = "text";
}
function mouseoutPass(obj) {
var obj = document.getElementById('passphrase');
obj.type = "password";
}
window.onload = function () {
//Check File API support
if (window.File && window.FileList && window.FileReader) {
var filesInput = document.getElementById("files");
filesInput.addEventListener("change", function (event) {
var files = event.target.files;
var output = document.getElementById("result");
for (var i = 0; i < files.length; i++) {
var file = files[i];
var fileName = file.name;
var picReader = new FileReader();
picReader.addEventListener("load", function (event) {
var picFile = event.target;
//Set passphrase and encryptedMsg if passphrase is present
var passphrase = "";
var encryptedMsg = "";
if (document.getElementById("passphrase").value) {
passphrase = document.getElementById("passphrase").value;
encryptedMsg = "<span style='color:#ff6666'>Private Key & Address Encrypted With Password (Hover to Reveal): </span>"
}
var file = picFile.result;
//Remove data:[<media type>][;base64], we only want the base64 <data> string.
var n = 0;
var word = ",";
if (file.includes(word)) n = file.lastIndexOf(word);
if (n > 0) file = file.slice(n+word.length, file.length);
//Create SHA256 hash of the file
var hash = Bitcoin.crypto.sha256(file);
//Create Private Key & Address From Hash
var mnemonic = bip39.entropyToMnemonic(hash);
bip39.mnemonicToSeed(mnemonic, passphrase)
.then(function (seed) {
var root = bip32.fromSeed(seed, Bitcoin.networks.bitcoin);
//Setup Key Derivation Paths
var p2shAddressPath = `m/49'/0'/0'/0/0`;
var bech32AddressPath = `m/84'/0'/0'/0/0`;
//Get keypairs based on key derivation paths
var p2shKeypair = root.derivePath(p2shAddressPath);
var bech32Keypair = root.derivePath(bech32AddressPath);
//Get p2sh & p2wpkh addresses
var p2sh = Bitcoin.payments.p2sh({
redeem: Bitcoin.payments.p2wpkh({pubkey: p2shKeypair.publicKey})
});
var p2shAddress = p2sh.address;
var bech32 = Bitcoin.payments.p2wpkh({pubkey: bech32Keypair.publicKey});
var bech32Address = bech32.address;
//Setup QRCode
var typeNumber = 14;
var errorCorrectionLevel = 'H';
//Setup Segwit-Compatible p2sh QR code
var p2shQR = qrcode(typeNumber, errorCorrectionLevel);
p2shQR.addData(p2shAddress);
p2shQR.make();
//Setup Bech32 p2wpkh QR code
var bech32QR = qrcode(typeNumber, errorCorrectionLevel);
bech32QR.addData(bech32Address);
bech32QR.make();
var div = document.createElement("div");
//Create & Set Address URLs
var url = "https://blockstream.info/address/";
var p2shURL = url + p2shAddress;
var bech32URL = url + bech32Address;
var fileContent = "<p class='filename'>" + fileName + "</p>";
if (picFile.result.substr(0, 10) === "data:image") {
fileContent = "<img class='thumbnail' src='" + picFile.result + "'" + "title='" + fileName + "'/>";
}
div.innerHTML =
"<hr><div class='container'><div class='cell'><div class='content'>" +
fileContent +
"<br /><div class='encrypted-msg'>" + encryptedMsg + "<span id='reveal-passphrase'>" + passphrase + "</span></div>" +
"<br /><span>Mnemonic Phrase:</span><p>" + mnemonic + "</p>" +
"<br /><span>Segwit P2SH Address " + p2shAddressPath + ":<br /></span><a href='" + p2shURL + "' target='_blank'>" + p2shAddress + "</a><br />" +
"<br /><span>Bech32 Address " + bech32AddressPath + ":<br /></span><a href='" + bech32URL + "' target='_blank'>" + bech32Address + "</a>" +
"</div></div>" +
"<div class='cell'><div class='content2'>" +
"<p id='bitcoinQr' align='center'>Segwit P2SH Address</p>" + p2shQR.createImgTag() + "" +
"<p id='bitcoinQr' align='center'>Bech32 Address</p>" + bech32QR.createImgTag() + "" +
"</div></div>" +
"</div>";
output.insertBefore(div, null);
});
});
//Read the image
picReader.readAsDataURL(file);
}
});
} else {
alert("Your browser does not support File API");
console.log("Your browser does not support File API");
}
}
</script>