Adds fail fast assertions for invalid input
This commit is contained in:
parent
3fa43b83b6
commit
8fd06681ef
1 changed files with 68 additions and 60 deletions
128
src/eckey.js
128
src/eckey.js
|
@ -1,58 +1,64 @@
|
||||||
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 =
|
|
||||||
input instanceof ECKey ? input.priv
|
|
||||||
: input instanceof BigInteger ? input.mod(ecparams.getN())
|
|
||||||
: Array.isArray(input) ? fromBin(input.slice(0, 32))
|
|
||||||
: typeof input != "string" ? null
|
|
||||||
: input.length == 44 ? fromBin(convert.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(convert.hexToBytes(input.slice(0, 64)))
|
|
||||||
: null
|
|
||||||
|
|
||||||
this.compressed =
|
this.priv =
|
||||||
compressed !== undefined ? compressed
|
input instanceof ECKey ? input.priv
|
||||||
: input instanceof ECKey ? input.compressed
|
: input instanceof BigInteger ? input.mod(ecparams.getN())
|
||||||
: input instanceof BigInteger ? false
|
: Array.isArray(input) ? fromBin(input.slice(0, 32))
|
||||||
: Array.isArray(input) ? false
|
: typeof input != "string" ? null
|
||||||
: typeof input != "string" ? null
|
: input.length == 44 ? fromBin(convert.base64ToBytes(input))
|
||||||
: input.length == 44 ? false
|
: input.length == 51 && input[0] == '5' ? fromBin(base58.checkDecode(input))
|
||||||
: input.length == 51 && input[0] == '5' ? false
|
: input.length == 51 && input[0] == '9' ? fromBin(base58.checkDecode(input))
|
||||||
: input.length == 51 && input[0] == '9' ? false
|
: input.length == 52 && has('LK', input[0]) ? fromBin(base58.checkDecode(input).slice(0, 32))
|
||||||
: input.length == 52 && has('LK', input[0]) ? true
|
: input.length == 52 && input[0] == 'c' ? fromBin(base58.checkDecode(input).slice(0, 32))
|
||||||
: input.length == 52 && input[0] == 'c' ? true
|
: has([64,65],input.length) ? fromBin(convert.hexToBytes(input.slice(0, 64)))
|
||||||
: input.length == 64 ? false
|
: null
|
||||||
: input.length == 65 ? true
|
|
||||||
: null
|
assert(this.priv !== null)
|
||||||
};
|
|
||||||
|
this.compressed =
|
||||||
|
compressed !== undefined ? compressed
|
||||||
|
: input instanceof ECKey ? input.compressed
|
||||||
|
: input instanceof BigInteger ? false
|
||||||
|
: Array.isArray(input) ? false
|
||||||
|
: 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
|
||||||
|
|
||||||
|
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
|
||||||
|
@ -127,21 +133,23 @@ var ECPubKey = function(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)
|
||||||
: null
|
: null
|
||||||
|
|
||||||
this.compressed =
|
assert(this.pub !== null)
|
||||||
compressed ? compressed
|
|
||||||
: input instanceof ECPointFp ? input.compressed
|
this.compressed =
|
||||||
: input instanceof ECPubKey ? input.compressed
|
compressed ? compressed
|
||||||
: (this.pub[0] < 4)
|
: input instanceof ECPointFp ? input.compressed
|
||||||
|
: input instanceof ECPubKey ? input.compressed
|
||||||
|
: (this.pub[0] < 4)
|
||||||
}
|
}
|
||||||
|
|
||||||
ECPubKey.prototype.add = function(key) {
|
ECPubKey.prototype.add = function(key) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue