|
|
@ -11,18 +11,19 @@ var ECPointFp = require('./jsbn/ec').ECPointFp; |
|
|
|
var ecparams = sec("secp256k1"); |
|
|
|
|
|
|
|
// input can be nothing, array of bytes, hex string, or base58 string
|
|
|
|
var ECKey = function (input,compressed) { |
|
|
|
var ECKey = function (input,compressed,version) { |
|
|
|
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 || Address.address_types.prod; |
|
|
|
} |
|
|
|
else this.import(input,compressed) |
|
|
|
else this.import(input,compressed,version) |
|
|
|
}; |
|
|
|
|
|
|
|
ECKey.prototype.import = function (input,compressed) { |
|
|
|
ECKey.prototype.import = function (input,compressed,version) { |
|
|
|
function has(li,v) { return li.indexOf(v) >= 0 } |
|
|
|
function fromBin(x) { return BigInteger.fromByteArrayUnsigned(x) } |
|
|
|
this.priv = |
|
|
@ -32,7 +33,9 @@ ECKey.prototype.import = function (input,compressed) { |
|
|
|
: typeof input != "string" ? null |
|
|
|
: input.length == 44 ? fromBin(conv.base64ToBytes(input)) |
|
|
|
: input.length == 51 && input[0] == '5' ? fromBin(base58.checkDecode(input)) |
|
|
|
: input.length == 51 && input[0] == '9' ? fromBin(base58.checkDecode(input)) |
|
|
|
: input.length == 52 && has('LK',input[0]) ? fromBin(base58.checkDecode(input).slice(0,32)) |
|
|
|
: input.length == 52 && input[0] == 'c' ? fromBin(base58.checkDecode(input).slice(0,32)) |
|
|
|
: has([64,65],input.length) ? fromBin(conv.hexToBytes(input.slice(0,64))) |
|
|
|
: null |
|
|
|
|
|
|
@ -44,15 +47,32 @@ ECKey.prototype.import = function (input,compressed) { |
|
|
|
: typeof input != "string" ? null |
|
|
|
: input.length == 44 ? false |
|
|
|
: input.length == 51 && input[0] == '5' ? false |
|
|
|
: input.length == 51 && input[0] == '9' ? false |
|
|
|
: input.length == 52 && has('LK',input[0]) ? true |
|
|
|
: input.length == 52 && input[0] == 'c' ? true |
|
|
|
: input.length == 64 ? false |
|
|
|
: input.length == 65 ? true |
|
|
|
: null |
|
|
|
|
|
|
|
this.version = |
|
|
|
version !== undefined ? version |
|
|
|
: input instanceof ECKey ? input.version |
|
|
|
: input instanceof BigInteger ? Address.address_types.prod |
|
|
|
: util.isArray(input) ? Address.address_types.prod |
|
|
|
: typeof input != "string" ? null |
|
|
|
: input.length == 44 ? Address.address_types.prod |
|
|
|
: input.length == 51 && input[0] == '5' ? Address.address_types.prod |
|
|
|
: input.length == 51 && input[0] == '9' ? Address.address_types.testnet |
|
|
|
: input.length == 52 && has('LK',input[0]) ? Address.address_types.prod |
|
|
|
: input.length == 52 && input[0] == 'c' ? Address.address_types.testnet |
|
|
|
: input.length == 64 ? Address.address_types.prod |
|
|
|
: input.length == 65 ? Address.address_types.prod |
|
|
|
: null |
|
|
|
}; |
|
|
|
|
|
|
|
ECKey.prototype.getPub = function(compressed) { |
|
|
|
if (compressed === undefined) compressed = this.compressed |
|
|
|
return ECPubKey(ecparams.getG().multiply(this.priv),compressed) |
|
|
|
return ECPubKey(ecparams.getG().multiply(this.priv),compressed,this.version) |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -68,7 +88,7 @@ ECKey.prototype.toBin = function() { |
|
|
|
} |
|
|
|
|
|
|
|
ECKey.prototype.toBase58 = function() { |
|
|
|
return base58.checkEncode(this.toBytes(), 128) |
|
|
|
return base58.checkEncode(this.toBytes(), ECKey.version_bytes[this.version]) |
|
|
|
} |
|
|
|
|
|
|
|
ECKey.prototype.toWif = ECKey.prototype.toBase58 |
|
|
@ -83,10 +103,14 @@ ECKey.prototype.toBytes = function() { |
|
|
|
return bytes |
|
|
|
} |
|
|
|
|
|
|
|
ECKey.prototype.toBase64 = function() { |
|
|
|
return conv.bytesToBase64(this.toBytes()) |
|
|
|
} |
|
|
|
|
|
|
|
ECKey.prototype.toString = ECKey.prototype.toBase58 |
|
|
|
|
|
|
|
ECKey.prototype.getBitcoinAddress = function(v) { |
|
|
|
return this.getPub().getBitcoinAddress(v) |
|
|
|
ECKey.prototype.getBitcoinAddress = function() { |
|
|
|
return this.getPub().getBitcoinAddress(this.version) |
|
|
|
} |
|
|
|
|
|
|
|
ECKey.prototype.add = function(key) { |
|
|
@ -97,18 +121,24 @@ ECKey.prototype.multiply = function(key) { |
|
|
|
return ECKey(this.priv.multiply(ECKey(key).priv),this.compressed) |
|
|
|
} |
|
|
|
|
|
|
|
var ECPubKey = function(input,compressed) { |
|
|
|
if (!(this instanceof ECPubKey)) { return new ECPubKey(input,compressed); } |
|
|
|
ECKey.version_bytes = { |
|
|
|
0: 128, |
|
|
|
111: 239 |
|
|
|
} |
|
|
|
|
|
|
|
var ECPubKey = function(input,compressed,version) { |
|
|
|
if (!(this instanceof ECPubKey)) { return new ECPubKey(input,compressed,version); } |
|
|
|
if (!input) { |
|
|
|
// Generate new key
|
|
|
|
var n = ecparams.getN(); |
|
|
|
this.pub = ecparams.getG().multiply(ecdsa.getBigRandom(n)) |
|
|
|
this.compressed = compressed || false; |
|
|
|
this.version = version || Address.address_types.prod; |
|
|
|
} |
|
|
|
else this.import(input,compressed) |
|
|
|
else this.import(input,compressed,version) |
|
|
|
} |
|
|
|
|
|
|
|
ECPubKey.prototype.import = function(input,compressed) { |
|
|
|
ECPubKey.prototype.import = function(input,compressed,version) { |
|
|
|
var decode = function(x) { return ECPointFp.decodeFrom(ecparams.getCurve(), x) } |
|
|
|
this.pub = |
|
|
|
input instanceof ECPointFp ? input |
|
|
@ -119,18 +149,24 @@ ECPubKey.prototype.import = function(input,compressed) { |
|
|
|
: ecparams.getG().multiply(ecdsa.getBigRandom(ecparams.getN())) |
|
|
|
|
|
|
|
this.compressed = |
|
|
|
arguments.length > 1 ? compressed |
|
|
|
compressed ? compressed |
|
|
|
: 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 |
|
|
|
: Address.address_types.prod |
|
|
|
} |
|
|
|
|
|
|
|
ECPubKey.prototype.add = function(key) { |
|
|
|
return ECPubKey(this.pub.add(ECPubKey(key).pub),this.compressed) |
|
|
|
return ECPubKey(this.pub.add(ECPubKey(key).pub),this.compressed,this.version) |
|
|
|
} |
|
|
|
|
|
|
|
ECPubKey.prototype.multiply = function(key) { |
|
|
|
return ECPubKey(this.pub.multiply(ECKey(key).priv),this.compressed) |
|
|
|
return ECPubKey(this.pub.multiply(ECKey(key).priv),this.compressed,this.version) |
|
|
|
} |
|
|
|
|
|
|
|
ECPubKey.prototype['export'] = function(format) { |
|
|
@ -152,7 +188,7 @@ ECPubKey.prototype.toBin = function() { |
|
|
|
} |
|
|
|
|
|
|
|
ECPubKey.prototype.toBase58 = function() { |
|
|
|
return base58.checkEncode(this.toBytes(), 128) |
|
|
|
return base58.checkEncode(this.toBytes(), this.version) |
|
|
|
} |
|
|
|
|
|
|
|
ECPubKey.prototype.toWif = ECPubKey.prototype.toBase58 |
|
|
@ -161,8 +197,8 @@ ECPubKey.prototype.toString = function() { |
|
|
|
return this.getBitcoinAddress().toString() |
|
|
|
} |
|
|
|
|
|
|
|
ECPubKey.prototype.getBitcoinAddress = function(v) { |
|
|
|
return new Address(util.sha256ripe160(this.toBytes()), v); |
|
|
|
ECPubKey.prototype.getBitcoinAddress = function() { |
|
|
|
return new Address(util.sha256ripe160(this.toBytes()), this.version); |
|
|
|
} |
|
|
|
|
|
|
|
ECKey.prototype.sign = function (hash) { |
|
|
|