From e58a86a2fad0e9ac4c9530abd1035a71abbbcce7 Mon Sep 17 00:00:00 2001 From: pointbiz Date: Thu, 22 Mar 2012 21:05:50 -0400 Subject: [PATCH] v1.5 wallet details compressed keys --- bitaddress.org.html | 145 +++++++++++++++++++++++++------------------- 1 file changed, 84 insertions(+), 61 deletions(-) diff --git a/bitaddress.org.html b/bitaddress.org.html index cf4bee4..f9ecaa7 100644 --- a/bitaddress.org.html +++ b/bitaddress.org.html @@ -25,7 +25,7 @@ window.Bitcoin MIT License The bitaddress.org software is available under The MIT License (MIT) - Copyright (c) 2011 bitaddress.org + Copyright (c) 2011-2012 bitaddress.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including @@ -948,24 +948,25 @@ }; // patched by bitaddress.org and Casascius for use with Bitcoin.ECKey + // patched by coretechs to support compressed public keys ec.PointFp.prototype.getEncoded = function (compressed) { var x = this.getX().toBigInteger(); var y = this.getY().toBigInteger(); var len = 32; // integerToBytes will zero pad if integer is less than 32 bytes. 32 bytes length is required by the Bitcoin protocol. var enc = ec.integerToBytes(x, len); - // modded from old code from forum post + // when compressed prepend byte depending if y point is even or odd if (compressed) { if (y.isEven()) { enc.unshift(0x02); - } + } else { enc.unshift(0x03); - } - } + } + } else { enc.unshift(0x04); - enc = enc.concat(ec.integerToBytes(y, len)); + enc = enc.concat(ec.integerToBytes(y, len)); // uncompressed public key appends the bytes of the y point } return enc; }; @@ -3742,75 +3743,97 @@ } }; - ECKey.prototype.getPub = function (compressed) { - //if (this.pub) return this.pub; - this.pub = ecparams.getG().multiply(this.priv).getEncoded(compressed); - return this.pub; + ECKey.prototype.getPub = function () { + if (this.pub) return this.pub; + return this.pub = ecparams.getG().multiply(this.priv).getEncoded(0); }; - ECKey.prototype.getPubKeyHash = function (compressed) { - //if (this.pubKeyHash) return this.pubKeyHash; - return this.pubKeyHash = Bitcoin.Util.sha256ripe160(this.getPub(compressed)); + ECKey.prototype.getPubCompressed = function () { + if (this.pubCompressed) return this.pubCompressed; + return this.pubCompressed = ecparams.getG().multiply(this.priv).getEncoded(1); }; - ECKey.prototype.getBitcoinAddress = function (compressed) { - var hash = this.getPubKeyHash(compressed); + ECKey.prototype.getPubKeyHash = function () { + if (this.pubKeyHash) return this.pubKeyHash; + return this.pubKeyHash = Bitcoin.Util.sha256ripe160(this.getPub()); + }; + + ECKey.prototype.getPubKeyHashCompressed = function () { + if (this.pubKeyHashCompressed) return this.pubKeyHashCompressed; + return this.pubKeyHashCompressed = Bitcoin.Util.sha256ripe160(this.getPubCompressed()); + }; + + ECKey.prototype.getBitcoinAddress = function () { + var hash = this.getPubKeyHash(); var addr = new Bitcoin.Address(hash); return addr.toString(); }; - // Sipa Private Key Wallet Import Format (added by bitaddress.org) - ECKey.prototype.getBitcoinWalletImportFormat = function (compressed) { - // 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); + ECKey.prototype.getBitcoinAddressCompressed = function () { + var hash = this.getPubKeyHashCompressed(); + var addr = new Bitcoin.Address(hash); + return addr.toString(); + }; + // Sipa Private Key Wallet Import Format + ECKey.prototype.getBitcoinWalletImportFormat = function () { + var bytes = this.getBitcoinPrivateKeyByteArray(); 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)); - var privWif = Bitcoin.Base58.encode(bytes); return privWif; }; - // Private Key Hex Format (added by bitaddress.org) + // Sipa Private Key Wallet Import Format Compressed + ECKey.prototype.getBitcoinWalletImportFormatCompressed = function () { + var bytes = this.getBitcoinPrivateKeyByteArray(); + 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 }); + bytes = bytes.concat(checksum.slice(0, 4)); + var privWifComp = Bitcoin.Base58.encode(bytes); + return privWifComp; + }; + + // Private Key Hex Format ECKey.prototype.getBitcoinHexFormat = function () { + return Crypto.util.bytesToHex(this.getBitcoinPrivateKeyByteArray()).toString().toUpperCase(); + }; + + // Private Key Base64 Format + ECKey.prototype.getBitcoinBase64Format = function () { + return Crypto.util.bytesToBase64(this.getBitcoinPrivateKeyByteArray()); + }; + + ECKey.prototype.getBitcoinPrivateKeyByteArray = 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 while (bytes.length < 32) bytes.unshift(0x00); - - return Crypto.util.bytesToHex(bytes).toString().toUpperCase(); + return bytes; }; - ECKey.prototype.setPub = function (pub) { this.pub = pub; }; + ECKey.prototype.setPubCompressed = function (pubCompressed) { + this.pubCompressed = pubCompressed; + }; + ECKey.prototype.toString = function (format) { format = format || ""; - // 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 - while (bytes.length < 32) bytes.unshift(0x00); - if (format === "base64" || format === "b64") { - return Crypto.util.bytesToBase64(bytes); + if (format.toString().toLowerCase() == "base64" || format.toString().toLowerCase() == "b64") { + return this.getBitcoinBase64Format(); } // Wallet Import Format else if (format.toString().toLowerCase() == "wif") { - return this.getBitcoinWalletImportFormat(0); + return this.getBitcoinWalletImportFormat(); } else if (format.toString().toLowerCase() == "wifcomp") { - return this.getBitcoinWalletImportFormat(1); + return this.getBitcoinWalletImportFormatCompressed(); } else { return this.getBitcoinHexFormat(); @@ -3822,7 +3845,7 @@ }; ECKey.prototype.verify = function (hash, sig) { - return ECDSA.verify(hash, sig, this.getPub(0)); + return ECDSA.verify(hash, sig, this.getPub()); }; return ECKey; @@ -4005,7 +4028,7 @@ } - +
@@ -4083,7 +4106,7 @@
- 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 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).

@@ -4201,7 +4224,7 @@