cleaned up implementation, import format check fix

This commit is contained in:
coretechs 2012-03-10 21:22:32 -05:00
parent 77f7d7bafc
commit 4b6b08706c

View file

@ -3760,7 +3760,7 @@
};
// 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
var bytes = this.priv.toByteArrayUnsigned();
@ -3768,23 +3768,11 @@
while (bytes.length < 32) bytes.unshift(0x00);
bytes.unshift(0x80); // prepend 0x80 byte
var checksum = Crypto.SHA256(Crypto.SHA256(bytes, { asBytes: true }), { asBytes: true });
bytes = bytes.concat(checksum.slice(0, 4));
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
if (compressed) {
bytes.push(0x01); // append 0x01 byte for compressed format
}
var checksum = Crypto.SHA256(Crypto.SHA256(bytes, { asBytes: true }), { asBytes: true });
bytes = bytes.concat(checksum.slice(0, 4));
@ -3814,10 +3802,10 @@
}
// Wallet Import Format
else if (format.toString().toLowerCase() == "wif") {
return this.getBitcoinWalletImportFormat();
return this.getBitcoinWalletImportFormat(0);
}
else if (format.toString().toLowerCase() == "wifcomp") {
return this.getBitcoinWalletImportFormatComp();
return this.getBitcoinWalletImportFormat(1);
}
else {
return this.getBitcoinHexFormat();
@ -4090,15 +4078,18 @@
<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.
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 class="item">
<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>
</div>
<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>
</div>
<br />
@ -4106,13 +4097,17 @@
<span class="label">Public Key (130 characters [0-9A-F]):</span>
<span class="output" id="detailpubkey"></span>
</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 id="detailqrcodeprivate" class="qrcode_private"></div>
<span class="label">Private Key WIF (51 characters base58, starts with a '5'):</span>
<span class="output" id="detailprivwif"></span>
</div>
<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>
</div>
<br /><br />
@ -4210,7 +4205,7 @@
try {
var key = new Bitcoin.ECKey(false);
var bitcoinAddress = key.getBitcoinAddress(0);
var privateKeyWif = key.getBitcoinWalletImportFormat();
var privateKeyWif = key.getBitcoinWalletImportFormat(0);
}
catch (e) {
alert(e);
@ -4568,7 +4563,7 @@
// 52 characters base58
isCompSipaWalletImportFormat: function (key) {
key = key.toString();
return (/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{52}$/.test(key));
return (/^[LK][123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{51}$/.test(key));
},
// 64 characters [0-9A-F]
isHexFormat: function (key) {
@ -4669,14 +4664,16 @@
if (btcKey != undefined) {
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 pubKeyFirstHalf = pubKey.substr(0, halfWayIndex);
var pubKeySecondHalf = pubKey.substr(halfWayIndex, pubKey.length - halfWayIndex);
document.getElementById("detailpubkey").innerHTML = pubKeyFirstHalf + "<br />" + pubKeySecondHalf;
document.getElementById("detailpubkeycomp").innerHTML = pubKeyComp;
document.getElementById("detailaddress").innerHTML = btcKey.getBitcoinAddress(0);
document.getElementById("detailaddresscomp").innerHTML = btcKey.getBitcoinAddress(1);
document.getElementById("detailprivwif").innerHTML = btcKey.getBitcoinWalletImportFormat();
document.getElementById("detailprivwifcomp").innerHTML = btcKey.getBitcoinWalletImportFormatComp();
document.getElementById("detailprivwif").innerHTML = btcKey.getBitcoinWalletImportFormat(0);
document.getElementById("detailprivwifcomp").innerHTML = btcKey.getBitcoinWalletImportFormat(1);
document.getElementById("detailprivhex").innerHTML = btcKey.toString().toUpperCase();
document.getElementById("detailprivb64").innerHTML = btcKey.toString("base64");
document.getElementById("detailqrcodepublic").innerHTML = "";
@ -4684,12 +4681,12 @@
// show QR codes
try {
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) {
// for browsers that do not support canvas (IE8)
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 () {
document.getElementById("detailpubkey").innerHTML = "";
document.getElementById("detailpubkeycomp").innerHTML = "";
document.getElementById("detailaddress").innerHTML = "";
document.getElementById("detailaddresscomp").innerHTML = "";
document.getElementById("detailprivwif").innerHTML = "";