Browse Source

Merge branch 'dangerpriv' of github.com:dcousens/bitcoinjs-lib into dcousens-dangerpriv

hk-custom-address
Wei Lu 11 years ago
parent
commit
06b6112d58
  1. 52
      src/eckey.js

52
src/eckey.js

@ -1,30 +1,32 @@
var BigInteger = require('./jsbn/jsbn'); var Address = require('./address')
var sec = require('./jsbn/sec'); var assert = require('assert')
var base58 = require('./base58'); var convert = require('./convert')
var util = require('./util'); var base58 = require('./base58')
var convert = require('./convert'); var BigInteger = require('./jsbn/jsbn')
var Address = require('./address'); var ecdsa = require('./ecdsa')
var ecdsa = require('./ecdsa'); var ECPointFp = require('./jsbn/ec').ECPointFp
var ECPointFp = require('./jsbn/ec').ECPointFp; var sec = require('./jsbn/sec')
var Network = require('./network') var Network = require('./network')
var util = require('./util')
var ecparams = sec("secp256k1"); var ecparams = sec("secp256k1")
// input can be nothing, array of bytes, hex string, or base58 string // input can be nothing, array of bytes, hex string, or base58 string
var ECKey = function (input, compressed) { var ECKey = function (input, compressed) {
if (!(this instanceof ECKey)) { return new ECKey(input, compressed); } if (!(this instanceof ECKey)) { return new ECKey(input, compressed) }
if (!input) { if (!input) {
// Generate new key // Generate new key
var n = ecparams.getN(); var n = ecparams.getN()
this.priv = ecdsa.getBigRandom(n); this.priv = ecdsa.getBigRandom(n)
this.compressed = compressed || false; this.compressed = compressed || false
} }
else this.import(input,compressed) else this.import(input,compressed)
}; }
ECKey.prototype.import = function (input, compressed) { ECKey.prototype.import = function (input, compressed) {
function has(li, v) { return li.indexOf(v) >= 0 } function has(li, v) { return li.indexOf(v) >= 0 }
function fromBin(x) { return BigInteger.fromByteArrayUnsigned(x) } function fromBin(x) { return BigInteger.fromByteArrayUnsigned(x) }
this.priv = this.priv =
input instanceof ECKey ? input.priv input instanceof ECKey ? input.priv
: input instanceof BigInteger ? input.mod(ecparams.getN()) : input instanceof BigInteger ? input.mod(ecparams.getN())
@ -38,6 +40,8 @@ ECKey.prototype.import = function (input,compressed) {
: has([64,65],input.length) ? fromBin(convert.hexToBytes(input.slice(0, 64))) : has([64,65],input.length) ? fromBin(convert.hexToBytes(input.slice(0, 64)))
: null : null
assert(this.priv !== null)
this.compressed = this.compressed =
compressed !== undefined ? compressed compressed !== undefined ? compressed
: input instanceof ECKey ? input.compressed : input instanceof ECKey ? input.compressed
@ -52,7 +56,9 @@ ECKey.prototype.import = function (input,compressed) {
: input.length == 64 ? false : input.length == 64 ? false
: input.length == 65 ? true : input.length == 65 ? true
: null : null
};
assert(this.compressed !== null)
}
ECKey.prototype.getPub = function(compressed) { ECKey.prototype.getPub = function(compressed) {
if (compressed === undefined) compressed = this.compressed if (compressed === undefined) compressed = this.compressed
@ -111,25 +117,25 @@ ECKey.prototype.verify = function(hash, sig) {
} }
var ECPubKey = function(input, compressed) { var ECPubKey = function(input, compressed) {
if (!(this instanceof ECPubKey)) { return new ECPubKey(input, compressed); } if (!(this instanceof ECPubKey)) {
if (!input) { return new ECPubKey(input, compressed)
// Generate new key
var n = ecparams.getN();
this.pub = ecparams.getG().multiply(ecdsa.getBigRandom(n))
this.compressed = compressed || false;
} }
else this.import(input,compressed)
this.import(input, compressed)
} }
ECPubKey.prototype.import = function(input, compressed) { ECPubKey.prototype.import = function(input, compressed) {
var decode = function(x) { return ECPointFp.decodeFrom(ecparams.getCurve(), x) } var decode = function(x) { return ECPointFp.decodeFrom(ecparams.getCurve(), x) }
this.pub = this.pub =
input instanceof ECPointFp ? input input instanceof ECPointFp ? input
: input instanceof ECKey ? ecparams.getG().multiply(input.priv) : input instanceof ECKey ? ecparams.getG().multiply(input.priv)
: input instanceof ECPubKey ? input.pub : input instanceof ECPubKey ? input.pub
: typeof input == "string" ? decode(convert.hexToBytes(input)) : typeof input == "string" ? decode(convert.hexToBytes(input))
: Array.isArray(input) ? decode(input) : Array.isArray(input) ? decode(input)
: ecparams.getG().multiply(ecdsa.getBigRandom(ecparams.getN())) : null
assert(this.pub !== null)
this.compressed = this.compressed =
compressed ? compressed compressed ? compressed

Loading…
Cancel
Save