cleaned up implementation, import format check fix
This commit is contained in:
parent
77f7d7bafc
commit
4b6b08706c
1 changed files with 26 additions and 28 deletions
|
@ -3760,7 +3760,7 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
// Sipa Private Key Wallet Import Format (added by bitaddress.org)
|
// Sipa Private Key Wallet Import Format (added by bitaddress.org)
|
||||||
ECKey.prototype.getBitcoinWalletImportFormat = function () {
|
ECKey.prototype.getBitcoinWalletImportFormat = function (compressed) {
|
||||||
// Get a copy of private key as a byte array
|
// Get a copy of private key as a byte array
|
||||||
var bytes = this.priv.toByteArrayUnsigned();
|
var bytes = this.priv.toByteArrayUnsigned();
|
||||||
|
|
||||||
|
@ -3768,23 +3768,11 @@
|
||||||
while (bytes.length < 32) bytes.unshift(0x00);
|
while (bytes.length < 32) bytes.unshift(0x00);
|
||||||
|
|
||||||
bytes.unshift(0x80); // prepend 0x80 byte
|
bytes.unshift(0x80); // prepend 0x80 byte
|
||||||
var checksum = Crypto.SHA256(Crypto.SHA256(bytes, { asBytes: true }), { asBytes: true });
|
|
||||||
bytes = bytes.concat(checksum.slice(0, 4));
|
if (compressed) {
|
||||||
|
bytes.push(0x01); // append 0x01 byte for compressed format
|
||||||
|
}
|
||||||
|
|
||||||
var privWif = Bitcoin.Base58.encode(bytes);
|
|
||||||
return privWif;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Compressed Sipa Private Key Wallet Import Format
|
|
||||||
ECKey.prototype.getBitcoinWalletImportFormatComp = function () {
|
|
||||||
// Get a copy of private key as a byte array
|
|
||||||
var bytes = this.priv.toByteArrayUnsigned();
|
|
||||||
|
|
||||||
// zero pad if private key is less than 32 bytes (thanks Casascius)
|
|
||||||
while (bytes.length < 32) bytes.unshift(0x00);
|
|
||||||
|
|
||||||
bytes.unshift(0x80); // prepend 0x80 byte
|
|
||||||
bytes.push(0x01); // append 0x01 byte for compressed format
|
|
||||||
var checksum = Crypto.SHA256(Crypto.SHA256(bytes, { asBytes: true }), { asBytes: true });
|
var checksum = Crypto.SHA256(Crypto.SHA256(bytes, { asBytes: true }), { asBytes: true });
|
||||||
bytes = bytes.concat(checksum.slice(0, 4));
|
bytes = bytes.concat(checksum.slice(0, 4));
|
||||||
|
|
||||||
|
@ -3814,10 +3802,10 @@
|
||||||
}
|
}
|
||||||
// Wallet Import Format
|
// Wallet Import Format
|
||||||
else if (format.toString().toLowerCase() == "wif") {
|
else if (format.toString().toLowerCase() == "wif") {
|
||||||
return this.getBitcoinWalletImportFormat();
|
return this.getBitcoinWalletImportFormat(0);
|
||||||
}
|
}
|
||||||
else if (format.toString().toLowerCase() == "wifcomp") {
|
else if (format.toString().toLowerCase() == "wifcomp") {
|
||||||
return this.getBitcoinWalletImportFormatComp();
|
return this.getBitcoinWalletImportFormat(1);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return this.getBitcoinHexFormat();
|
return this.getBitcoinHexFormat();
|
||||||
|
@ -4090,15 +4078,18 @@
|
||||||
<div class="notes">
|
<div class="notes">
|
||||||
Your Bitcoin Private Key is a unique secret number that only you know. It can be be encoded in a number of different formats.
|
Your Bitcoin Private Key is a unique secret number that only you know. It can be be encoded in a number of different formats.
|
||||||
Below we show the Bitcoin Address and Public Key that corresponds to your Private Key as well as your Private Key in the most popular encoding formats (WIF, HEX, B64, MINI).
|
Below we show the Bitcoin Address and Public Key that corresponds to your Private Key as well as your Private Key in the most popular encoding formats (WIF, HEX, B64, MINI).
|
||||||
|
|
||||||
|
Bitcoin v0.6+ stores public keys in compressed format. The client now also supports import and export of private keys with importprivkey/dumpprivkey. The format of the exported
|
||||||
|
private key is determined by whether the address was generated in an old or new wallet.
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="item">
|
<div class="item">
|
||||||
<div id="detailqrcodepublic" class="qrcode_public"></div>
|
<div id="detailqrcodepublic" class="qrcode_public"></div>
|
||||||
<span class="label">Bitcoin Address (33 or 34 characters, starts with a '1'):</span>
|
<span class="label">Bitcoin Address:</span>
|
||||||
<span class="output" id="detailaddress"></span>
|
<span class="output" id="detailaddress"></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="item">
|
<div class="item">
|
||||||
<span class="label">Bitcoin Address (compressed 0.6+, 33 or 34 characters, starts with a '1'):</span>
|
<span class="label">Bitcoin Address (compressed format):</span>
|
||||||
<span class="output" id="detailaddresscomp"></span>
|
<span class="output" id="detailaddresscomp"></span>
|
||||||
</div>
|
</div>
|
||||||
<br />
|
<br />
|
||||||
|
@ -4106,13 +4097,17 @@
|
||||||
<span class="label">Public Key (130 characters [0-9A-F]):</span>
|
<span class="label">Public Key (130 characters [0-9A-F]):</span>
|
||||||
<span class="output" id="detailpubkey"></span>
|
<span class="output" id="detailpubkey"></span>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="item">
|
||||||
|
<span class="label">Public Key (compressed format, 65 characters [0-9A-F]):</span>
|
||||||
|
<span class="output" id="detailpubkeycomp"></span>
|
||||||
|
</div>
|
||||||
<div class="item left">
|
<div class="item left">
|
||||||
<div id="detailqrcodeprivate" class="qrcode_private"></div>
|
<div id="detailqrcodeprivate" class="qrcode_private"></div>
|
||||||
<span class="label">Private Key WIF (51 characters base58, starts with a '5'):</span>
|
<span class="label">Private Key WIF (51 characters base58, starts with a '5'):</span>
|
||||||
<span class="output" id="detailprivwif"></span>
|
<span class="output" id="detailprivwif"></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="item left">
|
<div class="item left">
|
||||||
<span class="label">Private Key WIF (52 characters base58, compressed wallet format, bitcoin v0.6+):</span>
|
<span class="label">Private Key WIF (compressed format, 52 characters base58, starts with a 'K' or 'L'):</span>
|
||||||
<span class="output" id="detailprivwifcomp"></span>
|
<span class="output" id="detailprivwifcomp"></span>
|
||||||
</div>
|
</div>
|
||||||
<br /><br />
|
<br /><br />
|
||||||
|
@ -4210,7 +4205,7 @@
|
||||||
try {
|
try {
|
||||||
var key = new Bitcoin.ECKey(false);
|
var key = new Bitcoin.ECKey(false);
|
||||||
var bitcoinAddress = key.getBitcoinAddress(0);
|
var bitcoinAddress = key.getBitcoinAddress(0);
|
||||||
var privateKeyWif = key.getBitcoinWalletImportFormat();
|
var privateKeyWif = key.getBitcoinWalletImportFormat(0);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
alert(e);
|
alert(e);
|
||||||
|
@ -4568,7 +4563,7 @@
|
||||||
// 52 characters base58
|
// 52 characters base58
|
||||||
isCompSipaWalletImportFormat: function (key) {
|
isCompSipaWalletImportFormat: function (key) {
|
||||||
key = key.toString();
|
key = key.toString();
|
||||||
return (/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{52}$/.test(key));
|
return (/^[LK][123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{51}$/.test(key));
|
||||||
},
|
},
|
||||||
// 64 characters [0-9A-F]
|
// 64 characters [0-9A-F]
|
||||||
isHexFormat: function (key) {
|
isHexFormat: function (key) {
|
||||||
|
@ -4669,14 +4664,16 @@
|
||||||
|
|
||||||
if (btcKey != undefined) {
|
if (btcKey != undefined) {
|
||||||
var pubKey = Crypto.util.bytesToHex(btcKey.getPub(0)).toUpperCase();
|
var pubKey = Crypto.util.bytesToHex(btcKey.getPub(0)).toUpperCase();
|
||||||
|
var pubKeyComp = Crypto.util.bytesToHex(btcKey.getPub(1)).toUpperCase();
|
||||||
var halfWayIndex = Math.floor(pubKey.length / 2);
|
var halfWayIndex = Math.floor(pubKey.length / 2);
|
||||||
var pubKeyFirstHalf = pubKey.substr(0, halfWayIndex);
|
var pubKeyFirstHalf = pubKey.substr(0, halfWayIndex);
|
||||||
var pubKeySecondHalf = pubKey.substr(halfWayIndex, pubKey.length - halfWayIndex);
|
var pubKeySecondHalf = pubKey.substr(halfWayIndex, pubKey.length - halfWayIndex);
|
||||||
document.getElementById("detailpubkey").innerHTML = pubKeyFirstHalf + "<br />" + pubKeySecondHalf;
|
document.getElementById("detailpubkey").innerHTML = pubKeyFirstHalf + "<br />" + pubKeySecondHalf;
|
||||||
|
document.getElementById("detailpubkeycomp").innerHTML = pubKeyComp;
|
||||||
document.getElementById("detailaddress").innerHTML = btcKey.getBitcoinAddress(0);
|
document.getElementById("detailaddress").innerHTML = btcKey.getBitcoinAddress(0);
|
||||||
document.getElementById("detailaddresscomp").innerHTML = btcKey.getBitcoinAddress(1);
|
document.getElementById("detailaddresscomp").innerHTML = btcKey.getBitcoinAddress(1);
|
||||||
document.getElementById("detailprivwif").innerHTML = btcKey.getBitcoinWalletImportFormat();
|
document.getElementById("detailprivwif").innerHTML = btcKey.getBitcoinWalletImportFormat(0);
|
||||||
document.getElementById("detailprivwifcomp").innerHTML = btcKey.getBitcoinWalletImportFormatComp();
|
document.getElementById("detailprivwifcomp").innerHTML = btcKey.getBitcoinWalletImportFormat(1);
|
||||||
document.getElementById("detailprivhex").innerHTML = btcKey.toString().toUpperCase();
|
document.getElementById("detailprivhex").innerHTML = btcKey.toString().toUpperCase();
|
||||||
document.getElementById("detailprivb64").innerHTML = btcKey.toString("base64");
|
document.getElementById("detailprivb64").innerHTML = btcKey.toString("base64");
|
||||||
document.getElementById("detailqrcodepublic").innerHTML = "";
|
document.getElementById("detailqrcodepublic").innerHTML = "";
|
||||||
|
@ -4684,12 +4681,12 @@
|
||||||
// show QR codes
|
// show QR codes
|
||||||
try {
|
try {
|
||||||
document.getElementById("detailqrcodepublic").appendChild(ninja.qrCode.createCanvas(btcKey.getBitcoinAddress(0)));
|
document.getElementById("detailqrcodepublic").appendChild(ninja.qrCode.createCanvas(btcKey.getBitcoinAddress(0)));
|
||||||
document.getElementById("detailqrcodeprivate").appendChild(ninja.qrCode.createCanvas(btcKey.getBitcoinWalletImportFormat()));
|
document.getElementById("detailqrcodeprivate").appendChild(ninja.qrCode.createCanvas(btcKey.getBitcoinWalletImportFormat(0)));
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
// for browsers that do not support canvas (IE8)
|
// for browsers that do not support canvas (IE8)
|
||||||
document.getElementById("detailqrcodepublic").innerHTML = ninja.qrCode.createTableHtml(btcKey.getBitcoinAddress(0));
|
document.getElementById("detailqrcodepublic").innerHTML = ninja.qrCode.createTableHtml(btcKey.getBitcoinAddress(0));
|
||||||
document.getElementById("detailqrcodeprivate").innerHTML = ninja.qrCode.createTableHtml(btcKey.getBitcoinWalletImportFormat());
|
document.getElementById("detailqrcodeprivate").innerHTML = ninja.qrCode.createTableHtml(btcKey.getBitcoinWalletImportFormat(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4697,6 +4694,7 @@
|
||||||
|
|
||||||
clear: function () {
|
clear: function () {
|
||||||
document.getElementById("detailpubkey").innerHTML = "";
|
document.getElementById("detailpubkey").innerHTML = "";
|
||||||
|
document.getElementById("detailpubkeycomp").innerHTML = "";
|
||||||
document.getElementById("detailaddress").innerHTML = "";
|
document.getElementById("detailaddress").innerHTML = "";
|
||||||
document.getElementById("detailaddresscomp").innerHTML = "";
|
document.getElementById("detailaddresscomp").innerHTML = "";
|
||||||
document.getElementById("detailprivwif").innerHTML = "";
|
document.getElementById("detailprivwif").innerHTML = "";
|
||||||
|
|
Loading…
Reference in a new issue