From 9c473ca722769beeebae5b7b9656ffc24584270d Mon Sep 17 00:00:00 2001 From: Mark van Cuijk <github@mark.van-cuijk.nl> Date: Thu, 2 Aug 2012 20:03:13 +0200 Subject: [PATCH] Added support for the Base58-encoded private key format --- src/eckey.js | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/eckey.js b/src/eckey.js index 8088f1f..5529abc 100644 --- a/src/eckey.js +++ b/src/eckey.js @@ -15,8 +15,13 @@ Bitcoin.ECKey = (function () { // Prepend zero byte to prevent interpretation as negative integer this.priv = BigInteger.fromByteArrayUnsigned(input); } else if ("string" == typeof input) { - // Prepend zero byte to prevent interpretation as negative integer - this.priv = BigInteger.fromByteArrayUnsigned(Crypto.util.base64ToBytes(input)); + if (input.length == 51 && input[0] == '5') { + // Base58 encoded private key + this.priv = BigInteger.fromByteArrayUnsigned(ECKey.decodeString(input)); + } else { + // Prepend zero byte to prevent interpretation as negative integer + this.priv = BigInteger.fromByteArrayUnsigned(Crypto.util.base64ToBytes(input)); + } } }; @@ -44,6 +49,14 @@ Bitcoin.ECKey = (function () { return addr; }; + ECKey.prototype.getExportedPrivateKey = function () { + var hash = this.priv.toByteArrayUnsigned(); + hash.unshift(0x80); + var checksum = Crypto.SHA256(Crypto.SHA256(hash, {asBytes: true}), {asBytes: true}); + var bytes = hash.concat(checksum.slice(0,4)); + return Bitcoin.Base58.encode(bytes); + }; + ECKey.prototype.setPub = function (pub) { this.pub = pub; }; @@ -64,5 +77,31 @@ Bitcoin.ECKey = (function () { return ECDSA.verify(hash, sig, this.getPub()); }; + /** + * Parse an exported private key contained in a string. + */ + ECKey.decodeString = function (string) { + var bytes = Bitcoin.Base58.decode(string); + + var hash = bytes.slice(0, 33); + + var checksum = Crypto.SHA256(Crypto.SHA256(hash, {asBytes: true}), {asBytes: true}); + + if (checksum[0] != bytes[33] || + checksum[1] != bytes[34] || + checksum[2] != bytes[35] || + checksum[3] != bytes[36]) { + throw "Checksum validation failed!"; + } + + var version = hash.shift(); + + if (version != 0x80) { + throw "Version "+version+" not supported!"; + } + + return hash; + }; + return ECKey; })();