Changes to new version-less ECKey API

This commit is contained in:
Daniel Cousens 2014-03-22 18:17:54 +11:00
parent a1be488d1b
commit b40374e332
9 changed files with 61 additions and 85 deletions

View file

@ -19,7 +19,7 @@ var Address = function (bytes, version) {
}
else {
this.hash = bytes;
this.version = version || bytes.version || mainnet;
this.version = version || mainnet;
}
};

View file

@ -291,11 +291,11 @@ var ECDSA = {
*/
calcPubkeyRecoveryParam: function (origPubkey, r, s, hash)
{
var address = origPubkey.getBitcoinAddress().toString();
var address = origPubkey.getAddress().toString();
for (var i = 0; i < 4; i++) {
var pubkey = ECDSA.recoverPubKey(r, s, hash, i);
pubkey.compressed = origPubkey.compressed;
if (pubkey.getBitcoinAddress().toString() == address) {
if (pubkey.getAddress().toString() == address) {
return i;
}
}

View file

@ -7,25 +7,22 @@ var Address = require('./address');
var ecdsa = require('./ecdsa');
var ECPointFp = require('./jsbn/ec').ECPointFp;
var Network = require('./network')
var mainnet = Network.mainnet.addressVersion
var testnet = Network.testnet.addressVersion
var ecparams = sec("secp256k1");
// input can be nothing, array of bytes, hex string, or base58 string
var ECKey = function (input,compressed,version) {
if (!(this instanceof ECKey)) { return new ECKey(input,compressed,version); }
var ECKey = function (input,compressed) {
if (!(this instanceof ECKey)) { return new ECKey(input,compressed); }
if (!input) {
// Generate new key
var n = ecparams.getN();
this.priv = ecdsa.getBigRandom(n);
this.compressed = compressed || false;
this.version = version || mainnet;
}
else this.import(input,compressed,version)
else this.import(input,compressed)
};
ECKey.prototype.import = function (input,compressed,version) {
ECKey.prototype.import = function (input,compressed) {
function has(li,v) { return li.indexOf(v) >= 0 }
function fromBin(x) { return BigInteger.fromByteArrayUnsigned(x) }
this.priv =
@ -55,26 +52,11 @@ ECKey.prototype.import = function (input,compressed,version) {
: input.length == 64 ? false
: input.length == 65 ? true
: null
this.version =
version !== undefined ? version
: input instanceof ECKey ? input.version
: input instanceof BigInteger ? mainnet
: Array.isArray(input) ? mainnet
: typeof input != "string" ? null
: input.length == 44 ? mainnet
: input.length == 51 && input[0] == '5' ? mainnet
: input.length == 51 && input[0] == '9' ? testnet
: input.length == 52 && has('LK',input[0]) ? mainnet
: input.length == 52 && input[0] == 'c' ? testnet
: input.length == 64 ? mainnet
: input.length == 65 ? mainnet
: null
};
ECKey.prototype.getPub = function(compressed) {
if (compressed === undefined) compressed = this.compressed
return ECPubKey(ecparams.getG().multiply(this.priv),compressed,this.version)
return ECPubKey(ecparams.getG().multiply(this.priv),compressed)
}
/**
@ -83,17 +65,22 @@ ECKey.prototype.getPub = function(compressed) {
ECKey.prototype['export'] = function(format) {
format || (format = 'hex')
return this['to' + format.substr(0, 1).toUpperCase() + format.substr(1)]()
};
}
ECKey.prototype.toBin = function() {
return convert.bytesToString(this.toBytes())
}
ECKey.prototype.toBase58 = function() {
return base58.checkEncode(this.toBytes(), ECKey.version_bytes[this.version])
ECKey.version_bytes = {
0: 128,
111: 239
}
ECKey.prototype.toWif = ECKey.prototype.toBase58
ECKey.prototype.toWif = function(version) {
var version = version || Network.mainnet.addressVersion;
return base58.checkEncode(this.toBytes(), ECKey.version_bytes[version])
}
ECKey.prototype.toHex = function() {
return convert.bytesToHex(this.toBytes())
@ -109,10 +96,10 @@ ECKey.prototype.toBase64 = function() {
return convert.bytesToBase64(this.toBytes())
}
ECKey.prototype.toString = ECKey.prototype.toBase58
ECKey.prototype.toString = ECKey.prototype.toWif
ECKey.prototype.getBitcoinAddress = function() {
return this.getPub().getBitcoinAddress(this.version)
ECKey.prototype.getAddress = function(version) {
return this.getPub().getAddress(version)
}
ECKey.prototype.add = function(key) {
@ -123,24 +110,18 @@ ECKey.prototype.multiply = function(key) {
return ECKey(this.priv.multiply(ECKey(key).priv),this.compressed)
}
ECKey.version_bytes = {
0: 128,
111: 239
}
var ECPubKey = function(input,compressed,version) {
if (!(this instanceof ECPubKey)) { return new ECPubKey(input,compressed,version); }
var ECPubKey = function(input,compressed) {
if (!(this instanceof ECPubKey)) { return new ECPubKey(input,compressed); }
if (!input) {
// Generate new key
var n = ecparams.getN();
this.pub = ecparams.getG().multiply(ecdsa.getBigRandom(n))
this.compressed = compressed || false;
this.version = version || mainnet;
}
else this.import(input,compressed,version)
else this.import(input,compressed)
}
ECPubKey.prototype.import = function(input,compressed,version) {
ECPubKey.prototype.import = function(input,compressed) {
var decode = function(x) { return ECPointFp.decodeFrom(ecparams.getCurve(), x) }
this.pub =
input instanceof ECPointFp ? input
@ -155,20 +136,14 @@ ECPubKey.prototype.import = function(input,compressed,version) {
: input instanceof ECPointFp ? input.compressed
: input instanceof ECPubKey ? input.compressed
: (this.pub[0] < 4)
this.version =
version ? version
: input instanceof ECPointFp ? input.version
: input instanceof ECPubKey ? input.version
: mainnet
}
ECPubKey.prototype.add = function(key) {
return ECPubKey(this.pub.add(ECPubKey(key).pub),this.compressed,this.version)
return ECPubKey(this.pub.add(ECPubKey(key).pub),this.compressed)
}
ECPubKey.prototype.multiply = function(key) {
return ECPubKey(this.pub.multiply(ECKey(key).priv),this.compressed,this.version)
return ECPubKey(this.pub.multiply(ECKey(key).priv),this.compressed)
}
ECPubKey.prototype['export'] = function(format) {
@ -189,18 +164,18 @@ ECPubKey.prototype.toBin = function() {
return convert.bytesToString(this.toBytes())
}
ECPubKey.prototype.toBase58 = function() {
return base58.checkEncode(this.toBytes(), this.version)
ECPubKey.prototype.toWif = function(version) {
var version = version || Network.mainnet.addressVersion;
return base58.checkEncode(this.toBytes(), version)
}
ECPubKey.prototype.toWif = ECPubKey.prototype.toBase58
ECPubKey.prototype.toString = ECPubKey.prototype.toWif
ECPubKey.prototype.toString = function() {
return this.getBitcoinAddress().toString()
}
ECPubKey.prototype.getAddress = function(version) {
var version = version || Network.mainnet.addressVersion;
ECPubKey.prototype.getBitcoinAddress = function() {
return new Address(util.sha256ripe160(this.toBytes()), this.version);
return new Address(util.sha256ripe160(this.toBytes()), version);
}
ECKey.prototype.sign = function (hash) {
@ -214,6 +189,7 @@ ECKey.prototype.verify = function (hash, sig) {
/**
* Parse an exported private key contained in a string.
*/
module.exports = { ECKey: ECKey, ECPubKey: ECPubKey };
module.exports = {
ECKey: ECKey,
ECPubKey: ECPubKey
};

View file

@ -21,7 +21,7 @@ var HDWallet = module.exports = function(seed, network) {
throw new Error("Unknown network: " + this.network)
}
this.priv = new ECKey(I.slice(0, 32).concat([1]), true, this.getKeyVersion())
this.priv = new ECKey(I.slice(0, 32).concat([1]), true)
this.pub = this.priv.getPub()
this.index = 0
this.depth = 0
@ -109,10 +109,10 @@ HDWallet.fromBytes = function(input) {
// 33 bytes: the public key or private key data (0x02 + X or 0x03 + X for
// public keys, 0x00 + k for private keys)
if (type == 'priv') {
hd.priv = new ECKey(input.slice(46, 78).concat([1]), true, hd.getKeyVersion())
hd.priv = new ECKey(input.slice(46, 78).concat([1]), true)
hd.pub = hd.priv.getPub()
} else {
hd.pub = new ECPubKey(input.slice(45, 78), true, hd.getKeyVersion())
hd.pub = new ECPubKey(input.slice(45, 78), true)
}
return hd
@ -126,7 +126,7 @@ HDWallet.prototype.getFingerprint = function() {
return this.getIdentifier().slice(0, 4)
}
HDWallet.prototype.getBitcoinAddress = function() {
HDWallet.prototype.getAddress = function() {
return new Address(util.sha256ripe160(this.pub.toBytes()), this.getKeyVersion())
}
@ -221,7 +221,7 @@ HDWallet.prototype.derive = function(i) {
hd.pub = hd.priv.getPub()
} else {
// Ki = (IL + kpar)*G = IL*G + Kpar
hd.pub = this.pub.add(new ECKey(IL.concat([1]), true, this.getKeyVersion()).getPub())
hd.pub = this.pub.add(new ECKey(IL.concat([1]), true).getPub())
}
// ci = IR.

View file

@ -60,7 +60,7 @@ Message.verifyMessage = function (address, sig, message) {
var pubKey = ecdsa.recoverPubKey(sig.r, sig.s, hash, sig.i);
pubKey.compressed = isCompressed;
var expectedAddress = pubKey.getBitcoinAddress().toString();
var expectedAddress = pubKey.getAddress().toString();
return (address === expectedAddress);
};

View file

@ -310,7 +310,7 @@ Transaction.prototype.signWithKeys = function(keys, outputs, type) {
key = new ECKey(key);
return {
key: key,
address: key.getBitcoinAddress().toString()
address: key.getAddress().toString()
}
});
var hmap = {};

View file

@ -50,13 +50,13 @@ var Wallet = function (seed, options) {
this.generateAddress = function() {
var key = externalAccount.derive(this.addresses.length)
this.addresses.push(key.getBitcoinAddress().toString())
this.addresses.push(key.getAddress().toString())
return this.addresses[this.addresses.length - 1]
}
this.generateChangeAddress = function() {
var key = internalAccount.derive(this.changeAddresses.length)
this.changeAddresses.push(key.getBitcoinAddress().toString())
this.changeAddresses.push(key.getAddress().toString())
return this.changeAddresses[this.changeAddresses.length - 1]
}