Add the new operator when BN was used without it

This commit is contained in:
Esteban Ordano 2015-02-05 16:46:18 -03:00
parent 9b2ba6f2a2
commit 6b05f20397
14 changed files with 59 additions and 59 deletions

View File

@ -195,7 +195,7 @@ BlockHeader.prototype.toBufferWriter = function toBufferWriter(bw) {
* @returns {BN} - An instance of BN with the decoded difficulty bits
*/
BlockHeader.prototype.getTargetDifficulty = function getTargetDifficulty(info) {
var target = BN(this.bits & 0xffffff);
var target = new BN(this.bits & 0xffffff);
var mov = 8 * ((this.bits >>> 24) - 3);
while (mov-- > 0) {
target = target.mul(2);

View File

@ -14,12 +14,12 @@ var reversebuf = function(buf) {
BN.fromNumber = function(n) {
$.checkArgument(_.isNumber(n));
return BN(n);
return new BN(n);
};
BN.fromString = function(str) {
$.checkArgument(_.isString(str));
return BN(str);
return new BN(str);
};
BN.fromBuffer = function(buf, opts) {

View File

@ -60,7 +60,7 @@ Point.getG = function getG() {
* @returns {BN} A BN instance of the number of points on the curve
*/
Point.getN = function getN() {
return BN(ec.curve.n.toArray());
return new BN(ec.curve.n.toArray());
};
Point.prototype._getX = Point.prototype.getX;
@ -72,7 +72,7 @@ Point.prototype._getX = Point.prototype.getX;
* @returns {BN} A BN instance of the X coordinate
*/
Point.prototype.getX = function getX() {
return BN(this._getX().toArray());
return new BN(this._getX().toArray());
};
Point.prototype._getY = Point.prototype.getY;
@ -84,7 +84,7 @@ Point.prototype._getY = Point.prototype.getY;
* @returns {BN} A BN instance of the Y coordinate
*/
Point.prototype.getY = function getY() {
return BN(this._getY().toArray());
return new BN(this._getY().toArray());
};
/**

View File

@ -141,13 +141,13 @@ BufferReader.prototype.readVarintBN = function() {
var first = this.readUInt8();
switch (first) {
case 0xFD:
return BN(this.readUInt16LE());
return new BN(this.readUInt16LE());
case 0xFE:
return BN(this.readUInt32LE());
return new BN(this.readUInt32LE());
case 0xFF:
return this.readUInt64LEBN();
default:
return BN(first);
return new BN(first);
}
};

View File

@ -102,7 +102,7 @@ PrivateKey.prototype._classifyArguments = function(data, network) {
info.network = Networks.get(data);
} else if (typeof(data) === 'string'){
if (JSUtil.isHexa(data)) {
info.bn = BN(new Buffer(data, 'hex'));
info.bn = new BN(new Buffer(data, 'hex'));
} else {
info = PrivateKey._transformWIF(data, network);
}
@ -216,7 +216,7 @@ PrivateKey._transformJSON = function(json) {
if (JSUtil.isValidJSON(json)) {
json = JSON.parse(json);
}
var bn = BN(json.bn, 'hex');
var bn = new BN(json.bn, 'hex');
return {
bn: bn,
network: json.network,

View File

@ -169,18 +169,18 @@ PublicKey._transformDER = function(buf, strict) {
if (xbuf.length !== 32 || ybuf.length !== 32 || buf.length !== 65) {
throw new TypeError('Length of x and y must be 32 bytes');
}
x = BN(xbuf);
y = BN(ybuf);
x = new BN(xbuf);
y = new BN(ybuf);
info.point = new Point(x, y);
info.compressed = false;
} else if (buf[0] === 0x03) {
xbuf = buf.slice(1);
x = BN(xbuf);
x = new BN(xbuf);
info = PublicKey._transformX(true, x);
info.compressed = true;
} else if (buf[0] === 0x02) {
xbuf = buf.slice(1);
x = BN(xbuf);
x = new BN(xbuf);
info = PublicKey._transformX(false, x);
info.compressed = true;
} else {
@ -228,8 +228,8 @@ PublicKey._transformJSON = function(json) {
if (JSUtil.isValidJSON(json)) {
json = JSON.parse(json);
}
var x = BN(json.x, 'hex');
var y = BN(json.y, 'hex');
var x = new BN(json.x, 'hex');
var y = new BN(json.y, 'hex');
var point = new Point(x, y);
return new PublicKey(point, {
compressed: json.compressed

View File

@ -400,7 +400,7 @@ Interpreter.prototype.step = function() {
// ( -- value)
// ScriptNum bn((int)opcode - (int)(Opcode.OP_1 - 1));
n = opcodenum - (Opcode.OP_1 - 1);
buf = BN(n).toScriptNumBuffer();
buf = new BN(n).toScriptNumBuffer();
this.stack.push(buf);
// The result of these opcodes should always be the minimal way to push the data
// they push, so no need for a CheckMinimalPush here.
@ -623,7 +623,7 @@ Interpreter.prototype.step = function() {
case Opcode.OP_DEPTH:
{
// -- stacksize
buf = BN(this.stack.length).toScriptNumBuffer();
buf = new BN(this.stack.length).toScriptNumBuffer();
this.stack.push(buf);
}
break;
@ -748,7 +748,7 @@ Interpreter.prototype.step = function() {
this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
return false;
}
bn = BN(this.stack[this.stack.length - 1].length);
bn = new BN(this.stack[this.stack.length - 1].length);
this.stack.push(bn.toScriptNumBuffer());
}
break;
@ -817,10 +817,10 @@ Interpreter.prototype.step = function() {
}
break;
case Opcode.OP_NOT:
bn = BN((bn.cmp(0) === 0) + 0);
bn = new BN((bn.cmp(0) === 0) + 0);
break;
case Opcode.OP_0NOTEQUAL:
bn = BN((bn.cmp(0) !== 0) + 0);
bn = new BN((bn.cmp(0) !== 0) + 0);
break;
//default: assert(!'invalid opcode'); break; // TODO: does this ever occur?
}
@ -850,7 +850,7 @@ Interpreter.prototype.step = function() {
}
bn1 = BN.fromScriptNumBuffer(this.stack[this.stack.length - 2], fRequireMinimal);
bn2 = BN.fromScriptNumBuffer(this.stack[this.stack.length - 1], fRequireMinimal);
bn = BN(0);
bn = new BN(0);
switch (opcodenum) {
case Opcode.OP_ADD:
@ -863,39 +863,39 @@ Interpreter.prototype.step = function() {
// case Opcode.OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero); break;
case Opcode.OP_BOOLAND:
bn = BN(((bn1.cmp(0) !== 0) && (bn2.cmp(0) !== 0)) + 0);
bn = new BN(((bn1.cmp(0) !== 0) && (bn2.cmp(0) !== 0)) + 0);
break;
// case Opcode.OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero); break;
case Opcode.OP_BOOLOR:
bn = BN(((bn1.cmp(0) !== 0) || (bn2.cmp(0) !== 0)) + 0);
bn = new BN(((bn1.cmp(0) !== 0) || (bn2.cmp(0) !== 0)) + 0);
break;
// case Opcode.OP_NUMEQUAL: bn = (bn1 == bn2); break;
case Opcode.OP_NUMEQUAL:
bn = BN((bn1.cmp(bn2) === 0) + 0);
bn = new BN((bn1.cmp(bn2) === 0) + 0);
break;
// case Opcode.OP_NUMEQUALVERIFY: bn = (bn1 == bn2); break;
case Opcode.OP_NUMEQUALVERIFY:
bn = BN((bn1.cmp(bn2) === 0) + 0);
bn = new BN((bn1.cmp(bn2) === 0) + 0);
break;
// case Opcode.OP_NUMNOTEQUAL: bn = (bn1 != bn2); break;
case Opcode.OP_NUMNOTEQUAL:
bn = BN((bn1.cmp(bn2) !== 0) + 0);
bn = new BN((bn1.cmp(bn2) !== 0) + 0);
break;
// case Opcode.OP_LESSTHAN: bn = (bn1 < bn2); break;
case Opcode.OP_LESSTHAN:
bn = BN((bn1.cmp(bn2) < 0) + 0);
bn = new BN((bn1.cmp(bn2) < 0) + 0);
break;
// case Opcode.OP_GREATERTHAN: bn = (bn1 > bn2); break;
case Opcode.OP_GREATERTHAN:
bn = BN((bn1.cmp(bn2) > 0) + 0);
bn = new BN((bn1.cmp(bn2) > 0) + 0);
break;
// case Opcode.OP_LESSTHANOREQUAL: bn = (bn1 <= bn2); break;
case Opcode.OP_LESSTHANOREQUAL:
bn = BN((bn1.cmp(bn2) <= 0) + 0);
bn = new BN((bn1.cmp(bn2) <= 0) + 0);
break;
// case Opcode.OP_GREATERTHANOREQUAL: bn = (bn1 >= bn2); break;
case Opcode.OP_GREATERTHANOREQUAL:
bn = BN((bn1.cmp(bn2) >= 0) + 0);
bn = new BN((bn1.cmp(bn2) >= 0) + 0);
break;
case Opcode.OP_MIN:
bn = (bn1.cmp(bn2) < 0 ? bn1 : bn2);

View File

@ -727,7 +727,7 @@ Transaction.prototype.verify = function() {
}
// Check for negative or overflow output values
var valueoutbn = BN(0);
var valueoutbn = new BN(0);
for (var i = 0; i < this.outputs.length; i++) {
var txout = this.outputs[i];
var valuebn = txout._satoshisBN;

View File

@ -69,13 +69,13 @@ describe('Block', function() {
it('should instantiate from a raw block binary', function() {
var x = Block.fromRawBlock(dataRawBlockBinary);
x.header.version.should.equal(2);
BN(x.header.bits).toString('hex').should.equal('1c3fffc0');
new BN(x.header.bits).toString('hex').should.equal('1c3fffc0');
});
it('should instantiate from raw block buffer', function() {
var x = Block.fromRawBlock(dataRawBlockBuffer);
x.header.version.should.equal(2);
BN(x.header.bits).toString('hex').should.equal('1c3fffc0');
new BN(x.header.bits).toString('hex').should.equal('1c3fffc0');
});
});

View File

@ -185,13 +185,13 @@ describe('BlockHeader', function() {
it('should instantiate from a raw block binary', function() {
var x = BlockHeader.fromRawBlock(dataRawBlockBinary);
x.version.should.equal(2);
BN(x.bits).toString('hex').should.equal('1c3fffc0');
new BN(x.bits).toString('hex').should.equal('1c3fffc0');
});
it('should instantiate from raw block buffer', function() {
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
x.version.should.equal(2);
BN(x.bits).toString('hex').should.equal('1c3fffc0');
new BN(x.bits).toString('hex').should.equal('1c3fffc0');
});
});

View File

@ -83,7 +83,7 @@ describe('BN', function() {
describe('#toString', function() {
it('should make a string', function() {
BN(5).toString().should.equal('5');
new BN(5).toString().should.equal('5');
});
});

View File

@ -42,8 +42,8 @@ describe('ECDSA', function() {
it('calulates this known i', function() {
var hashbuf = Hash.sha256(new Buffer('some data'));
var r = BN('71706645040721865894779025947914615666559616020894583599959600180037551395766', 10);
var s = BN('109412465507152403114191008482955798903072313614214706891149785278625167723646', 10);
var r = new BN('71706645040721865894779025947914615666559616020894583599959600180037551395766', 10);
var s = new BN('109412465507152403114191008482955798903072313614214706891149785278625167723646', 10);
var ecdsa = new ECDSA({
privkey: new Privkey(BN.fromBuffer(Hash.sha256(new Buffer('test')))),
hashbuf: hashbuf,
@ -83,7 +83,7 @@ describe('ECDSA', function() {
it('should generate a random k that is (almost always) greater than this relatively small number', function() {
ecdsa.randomK();
var k1 = ecdsa.k;
var k2 = BN(Math.pow(2, 32)).mul(BN(Math.pow(2, 32))).mul(BN(Math.pow(2, 32)));
var k2 = new BN(Math.pow(2, 32)).mul(BN(Math.pow(2, 32))).mul(BN(Math.pow(2, 32)));
k2.gt(k1).should.equal(false);
});
@ -125,7 +125,7 @@ describe('ECDSA', function() {
describe('#toPublicKey', function() {
it('should calculate the correct public key', function() {
ecdsa.k = BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10);
ecdsa.k = new BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10);
ecdsa.sign();
ecdsa.sig.i = 0;
var pubkey = ecdsa.toPublicKey();
@ -133,7 +133,7 @@ describe('ECDSA', function() {
});
it('should calculate the correct public key for this signature with low s', function() {
ecdsa.k = BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10);
ecdsa.k = new BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10);
ecdsa.sig = Signature.fromString('3045022100ec3cfe0e335791ad278b4ec8eac93d0347' +
'a97877bb1d54d35d189e225c15f6650220278cf15b05ce47fb37d2233802899d94c774d5480bba9f0f2d996baa13370c43');
ecdsa.sig.i = 0;
@ -142,7 +142,7 @@ describe('ECDSA', function() {
});
it('should calculate the correct public key for this signature with high s', function() {
ecdsa.k = BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10);
ecdsa.k = new BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10);
ecdsa.sign();
ecdsa.sig = Signature.fromString('3046022100ec3cfe0e335791ad278b4ec8eac93d0347' +
'a97877bb1d54d35d189e225c15f665022100d8730ea4fa31b804c82ddcc7fd766269f33a079ea38e012c9238f2e2bcff34fe');
@ -167,8 +167,8 @@ describe('ECDSA', function() {
'710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'));
ecdsa.pubkey = pk;
ecdsa.sig = new Signature();
ecdsa.sig.r = BN(0);
ecdsa.sig.s = BN(0);
ecdsa.sig.r = new BN(0);
ecdsa.sig.s = new BN(0);
ecdsa.sigError().should.equal('r and s not in range');
});
@ -270,8 +270,8 @@ describe('ECDSA', function() {
k: BN.fromBuffer(new Buffer(obj.k, 'hex')),
hashbuf: Hash.sha256(new Buffer(obj.message)),
sig: new Signature().set({
r: BN(obj.signature.r),
s: BN(obj.signature.s),
r: new BN(obj.signature.r),
s: new BN(obj.signature.s),
i: obj.i
})
});
@ -290,7 +290,7 @@ describe('ECDSA', function() {
it('should validate invalid.sigError vector ' + i + ': ' + obj.description, function() {
var ecdsa = ECDSA().set({
pubkey: Pubkey.fromPoint(point.fromX(true, 1)),
sig: new Signature(BN(obj.signature.r), BN(obj.signature.s)),
sig: new Signature(BN(obj.signature.r), new BN(obj.signature.s)),
hashbuf: Hash.sha256(new Buffer(obj.message))
});
ecdsa.sigError().should.equal(obj.exception);

View File

@ -18,8 +18,8 @@ describe('Signature', function() {
});
it('should work with conveniently setting r, s', function() {
var r = BN();
var s = BN();
var r = new BN();
var s = new BN();
var sig = new Signature(r, s);
should.exist(sig);
sig.r.toString().should.equal(r.toString());
@ -166,8 +166,8 @@ describe('Signature', function() {
describe('#toDER', function() {
it('should convert these known r and s values into a known signature', function() {
var r = BN('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = BN('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var r = new BN('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = new BN('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var sig = new Signature({
r: r,
s: s
@ -180,8 +180,8 @@ describe('Signature', function() {
describe('#toString', function() {
it('should convert this signature in to hex DER', function() {
var r = BN('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = BN('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var r = new BN('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = new BN('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var sig = new Signature({
r: r,
s: s
@ -234,9 +234,9 @@ describe('Signature', function() {
});
describe('#hasLowS', function() {
it('should detect high and low S', function() {
var r = BN('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = BN('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var s2 = BN('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B2000');
var r = new BN('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = new BN('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var s2 = new BN('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B2000');
var sig = new Signature({
r: r,
s: s

View File

@ -135,7 +135,7 @@ describe('PrivateKey', function() {
it('should not create a zero private key', function() {
expect(function() {
var bn = BN(0);
var bn = new BN(0);
return new PrivateKey(bn);
}).to.throw(TypeError);
});