bitcoinjs-lib/src/eckey.js

69 lines
1.9 KiB
JavaScript
Raw Normal View History

2011-05-04 17:02:56 +01:00
Bitcoin.ECKey = (function () {
2012-01-11 02:40:45 +01:00
var ECDSA = Bitcoin.ECDSA;
var ecparams = getSECCurveByName("secp256k1");
var rng = new SecureRandom();
var ECKey = function (input) {
if (!input) {
// Generate new key
var n = ecparams.getN();
this.priv = ECDSA.getBigRandom(n);
} else if (input instanceof BigInteger) {
// Input is a private key value
this.priv = input;
} else if (Bitcoin.Util.isArray(input)) {
// 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));
}
};
ECKey.prototype.getPub = function () {
if (this.pub) return this.pub;
return this.pub = ecparams.getG().multiply(this.priv).getEncoded();
};
2012-01-11 10:41:52 +01:00
/**
* Get the pubKeyHash for this key.
*
* This is calculated as RIPE160(SHA256([encoded pubkey])) and returned as
* a byte array.
*/
2012-01-11 02:40:45 +01:00
ECKey.prototype.getPubKeyHash = function () {
if (this.pubKeyHash) return this.pubKeyHash;
return this.pubKeyHash = Bitcoin.Util.sha256ripe160(this.getPub());
};
ECKey.prototype.getBitcoinAddress = function () {
var hash = this.getPubKeyHash();
var addr = new Bitcoin.Address(hash);
return addr;
};
2011-05-04 17:02:56 +01:00
ECKey.prototype.setPub = function (pub) {
this.pub = pub;
};
2012-01-11 02:40:45 +01:00
ECKey.prototype.toString = function (format) {
if (format === "base64") {
return Crypto.util.bytesToBase64(this.priv.toByteArrayUnsigned());
} else {
return Crypto.util.bytesToHex(this.priv.toByteArrayUnsigned());
}
};
2011-05-04 17:02:56 +01:00
2012-01-11 02:40:45 +01:00
ECKey.prototype.sign = function (hash) {
return ECDSA.sign(hash, this.priv);
};
2011-05-04 17:02:56 +01:00
2012-01-11 02:40:45 +01:00
ECKey.prototype.verify = function (hash, sig) {
return ECDSA.verify(hash, sig, this.getPub());
};
2011-05-04 17:02:56 +01:00
2012-01-11 02:40:45 +01:00
return ECKey;
2011-05-04 17:02:56 +01:00
})();