Browse Source

Adds fail fast assertions for invalid input

hk-custom-address
Daniel Cousens 11 years ago
parent
commit
8fd06681ef
  1. 38
      src/eckey.js

38
src/eckey.js

@ -1,30 +1,32 @@
var BigInteger = require('./jsbn/jsbn');
var sec = require('./jsbn/sec');
var base58 = require('./base58');
var util = require('./util');
var convert = require('./convert');
var Address = require('./address');
var ecdsa = require('./ecdsa');
var ECPointFp = require('./jsbn/ec').ECPointFp;
var Address = require('./address')
var assert = require('assert')
var convert = require('./convert')
var base58 = require('./base58')
var BigInteger = require('./jsbn/jsbn')
var ecdsa = require('./ecdsa')
var ECPointFp = require('./jsbn/ec').ECPointFp
var sec = require('./jsbn/sec')
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
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) {
// Generate new key
var n = ecparams.getN();
this.priv = ecdsa.getBigRandom(n);
this.compressed = compressed || false;
var n = ecparams.getN()
this.priv = ecdsa.getBigRandom(n)
this.compressed = compressed || false
}
else this.import(input,compressed)
};
}
ECKey.prototype.import = function (input, compressed) {
function has(li, v) { return li.indexOf(v) >= 0 }
function fromBin(x) { return BigInteger.fromByteArrayUnsigned(x) }
this.priv =
input instanceof ECKey ? input.priv
: 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)))
: null
assert(this.priv !== null)
this.compressed =
compressed !== undefined ? compressed
: input instanceof ECKey ? input.compressed
@ -52,7 +56,9 @@ ECKey.prototype.import = function (input, compressed) {
: input.length == 64 ? false
: input.length == 65 ? true
: null
};
assert(this.compressed !== null)
}
ECKey.prototype.getPub = function(compressed) {
if (compressed === undefined) compressed = this.compressed
@ -137,6 +143,8 @@ ECPubKey.prototype.import = function(input, compressed) {
: Array.isArray(input) ? decode(input)
: null
assert(this.pub !== null)
this.compressed =
compressed ? compressed
: input instanceof ECPointFp ? input.compressed

Loading…
Cancel
Save