address review comments

This commit is contained in:
Manuel Araoz 2014-12-16 11:40:22 -03:00
parent 7683d2c2ed
commit 3eba0bc546
5 changed files with 49 additions and 71 deletions

View File

@ -1,7 +1,6 @@
'use strict'; 'use strict';
var BN = require('./bn'); var BN = require('./bn');
var BufferReader = require('../encoding/bufferreader');
var Point = require('./point'); var Point = require('./point');
var Signature = require('./signature'); var Signature = require('./signature');
var PublicKey = require('../publickey'); var PublicKey = require('../publickey');

View File

@ -1,12 +1,12 @@
'use strict'; 'use strict';
var _ = require('lodash');
var Address = require('./address'); var Address = require('./address');
var BN = require('./crypto/bn'); var BN = require('./crypto/bn');
var Point = require('./crypto/point'); var Point = require('./crypto/point');
var JSUtil = require('./util/js'); var JSUtil = require('./util/js');
var Network = require('./networks'); var Network = require('./networks');
var _ = require('lodash'); var _ = require('lodash');
var $ = require('./util/preconditions');
/** /**
* Instantiate a PublicKey from a 'PrivateKey', 'Point', 'string', 'Buffer'. * Instantiate a PublicKey from a 'PrivateKey', 'Point', 'string', 'Buffer'.
@ -34,9 +34,9 @@ var PublicKey = function PublicKey(data, extra) {
if (!(this instanceof PublicKey)) { if (!(this instanceof PublicKey)) {
return new PublicKey(data, extra); return new PublicKey(data, extra);
} }
if (!data) {
throw new TypeError('First argument is required, please include public key data.'); $.checkArgument(data, new TypeError('First argument is required, please include public key data.'));
}
if (data instanceof PublicKey) { if (data instanceof PublicKey) {
// Return copy, but as it's an immutable object, return same argument // Return copy, but as it's an immutable object, return same argument
return data; return data;
@ -93,7 +93,8 @@ var PublicKey = function PublicKey(data, extra) {
* @private * @private
*/ */
PublicKey._isPrivateKey = function(param) { PublicKey._isPrivateKey = function(param) {
return param && param.constructor && param.constructor.name && param.constructor.name === 'PrivateKey'; var PrivateKey = require('./privatekey');
return param instanceof PrivateKey;
}; };
/** /**
@ -126,10 +127,9 @@ PublicKey._isJSON = function(json) {
* @private * @private
*/ */
PublicKey._transformPrivateKey = function(privkey) { PublicKey._transformPrivateKey = function(privkey) {
$.checkArgument(PublicKey._isPrivateKey(privkey),
new TypeError('Must be an instance of PrivateKey'));
var info = {}; var info = {};
if (!PublicKey._isPrivateKey(privkey)) {
throw new TypeError('Must be an instance of PrivateKey');
}
info.point = Point.getG().mul(privkey.bn); info.point = Point.getG().mul(privkey.bn);
info.compressed = privkey.compressed; info.compressed = privkey.compressed;
info.network = privkey.network; info.network = privkey.network;
@ -145,10 +145,8 @@ PublicKey._transformPrivateKey = function(privkey) {
* @private * @private
*/ */
PublicKey._transformDER = function(buf, strict) { PublicKey._transformDER = function(buf, strict) {
$.checkArgument(PublicKey._isBuffer(buf), new TypeError('Must be a hex buffer of DER encoded public key'));
var info = {}; var info = {};
if (!PublicKey._isBuffer(buf)) {
throw new TypeError('Must be a hex buffer of DER encoded public key');
}
strict = _.isUndefined(strict) ? true : strict; strict = _.isUndefined(strict) ? true : strict;
@ -192,10 +190,9 @@ PublicKey._transformDER = function(buf, strict) {
* @private * @private
*/ */
PublicKey._transformX = function(odd, x) { PublicKey._transformX = function(odd, x) {
$.checkArgument(typeof odd === 'boolean',
new TypeError('Must specify whether y is odd or not (true or false)'));
var info = {}; var info = {};
if (typeof odd !== 'boolean') {
throw new TypeError('Must specify whether y is odd or not (true or false)');
}
info.point = Point.fromX(odd, x); info.point = Point.fromX(odd, x);
return info; return info;
}; };
@ -207,10 +204,8 @@ PublicKey._transformX = function(odd, x) {
* @returns {PublicKey} A new valid instance of PublicKey * @returns {PublicKey} A new valid instance of PublicKey
*/ */
PublicKey.fromJSON = function(json) { PublicKey.fromJSON = function(json) {
if (!PublicKey._isJSON(json)) { $.checkArgument(PublicKey._isJSON(json),
throw new TypeError('Must be a valid JSON string or plain object'); new TypeError('Must be a valid JSON string or plain object'));
}
return new PublicKey(json); return new PublicKey(json);
}; };
@ -240,9 +235,7 @@ PublicKey._transformJSON = function(json) {
* @returns {PublicKey} A new valid instance of PublicKey * @returns {PublicKey} A new valid instance of PublicKey
*/ */
PublicKey.fromPrivateKey = function(privkey) { PublicKey.fromPrivateKey = function(privkey) {
if (!PublicKey._isPrivateKey(privkey)) { $.checkArgument(PublicKey._isPrivateKey(privkey), new TypeError('Must be an instance of PrivateKey'));
throw new TypeError('Must be an instance of PrivateKey');
}
var info = PublicKey._transformPrivateKey(privkey); var info = PublicKey._transformPrivateKey(privkey);
return new PublicKey(info.point, { return new PublicKey(info.point, {
compressed: info.compressed, compressed: info.compressed,
@ -256,10 +249,9 @@ PublicKey.fromPrivateKey = function(privkey) {
* @param {bool} [strict] - if set to false, will loosen some conditions * @param {bool} [strict] - if set to false, will loosen some conditions
* @returns {PublicKey} A new valid instance of PublicKey * @returns {PublicKey} A new valid instance of PublicKey
*/ */
PublicKey.fromBuffer = function(buf, strict) { PublicKey.fromDER = PublicKey.fromBuffer = function(buf, strict) {
if (!PublicKey._isBuffer(buf)) { $.checkArgument(PublicKey._isBuffer(buf),
throw new TypeError('Must be a hex buffer of DER encoded public key'); new TypeError('Must be a hex buffer of DER encoded public key'));
}
var info = PublicKey._transformDER(buf, strict); var info = PublicKey._transformDER(buf, strict);
return new PublicKey(info.point, { return new PublicKey(info.point, {
compressed: info.compressed compressed: info.compressed
@ -274,28 +266,13 @@ PublicKey.fromBuffer = function(buf, strict) {
* @returns {PublicKey} A new valid instance of PublicKey * @returns {PublicKey} A new valid instance of PublicKey
*/ */
PublicKey.fromPoint = function(point, compressed) { PublicKey.fromPoint = function(point, compressed) {
if (!(point instanceof Point)) { $.checkArgument(point instanceof Point,
throw new TypeError('First argument must be an instance of Point.'); new TypeError('First argument must be an instance of Point.'));
}
return new PublicKey(point, { return new PublicKey(point, {
compressed: compressed compressed: compressed
}); });
}; };
/**
* Instantiate a PublicKey from a DER Buffer
*
* @param {Buffer} buf - A DER Buffer
* @param {bool} [strict] - if set to false, will loosen some conditions
* @returns {PublicKey} A new valid instance of PublicKey
*/
PublicKey.fromDER = function(buf, strict) {
var info = PublicKey._transformDER(buf, strict);
return new PublicKey(info.point, {
compressed: info.compressed
});
};
/** /**
* Instantiate a PublicKey from a DER hex encoded string * Instantiate a PublicKey from a DER hex encoded string
* *

View File

@ -268,7 +268,7 @@ Script.prototype.isScriptHashOut = function() {
return (buf.length === 23 && return (buf.length === 23 &&
buf[0] === Opcode.OP_HASH160 && buf[0] === Opcode.OP_HASH160 &&
buf[1] === 0x14 && buf[1] === 0x14 &&
buf[22] === Opcode.OP_EQUAL); buf[buf.length - 1] === Opcode.OP_EQUAL);
}; };
/** /**
@ -526,7 +526,9 @@ Script.buildMultisigOut = function(pubkeys, m, opts) {
opts = opts || {}; opts = opts || {};
var s = new Script(); var s = new Script();
s.add(Opcode.smallInt(m)); s.add(Opcode.smallInt(m));
pubkeys = _.map(pubkeys, function(pubkey) { return PublicKey(pubkey); }); pubkeys = _.map(pubkeys, function(pubkey) {
return PublicKey(pubkey);
});
var sorted = pubkeys; var sorted = pubkeys;
if (!opts.noSorting) { if (!opts.noSorting) {
sorted = _.sortBy(pubkeys, function(pubkey) { sorted = _.sortBy(pubkeys, function(pubkey) {

View File

@ -41,7 +41,7 @@ describe('Signature', function() {
blank, blank,
blank blank
]); ]);
var sig = new Signature.fromCompact(compressed); var sig = Signature.fromCompact(compressed);
sig.r.cmp(0).should.equal(0); sig.r.cmp(0).should.equal(0);
sig.s.cmp(0).should.equal(0); sig.s.cmp(0).should.equal(0);
}); });
@ -53,7 +53,7 @@ describe('Signature', function() {
var buf = new Buffer('3044022075fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e62770220729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2', 'hex'); var buf = new Buffer('3044022075fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e62770220729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2', 'hex');
it('should parse this DER format signature', function() { it('should parse this DER format signature', function() {
var sig = new Signature.fromDER(buf); var sig = Signature.fromDER(buf);
sig.r.toBuffer({ sig.r.toBuffer({
size: 32 size: 32
}).toString('hex').should.equal('75fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e6277'); }).toString('hex').should.equal('75fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e6277');
@ -69,7 +69,7 @@ describe('Signature', function() {
var buf = new Buffer('3044022075fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e62770220729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2', 'hex'); var buf = new Buffer('3044022075fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e62770220729e85cc46ffab881065ec07694220e71d4df9b2b8c8fd12c3122cf3a5efbcf2', 'hex');
it('should parse this DER format signature in hex', function() { it('should parse this DER format signature in hex', function() {
var sig = new Signature.fromString(buf.toString('hex')); var sig = Signature.fromString(buf.toString('hex'));
sig.r.toBuffer({ sig.r.toBuffer({
size: 32 size: 32
}).toString('hex').should.equal('75fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e6277'); }).toString('hex').should.equal('75fc517e541bd54769c080b64397e32161c850f6c1b2b67a5c433affbb3e6277');

View File

@ -18,7 +18,7 @@ describe('Script', function() {
it('should parse this buffer containing an OP code', function() { it('should parse this buffer containing an OP code', function() {
var buf = new Buffer(1); var buf = new Buffer(1);
buf[0] = Opcode('OP_0').toNumber(); buf[0] = Opcode.OP_0;
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
script.chunks[0].opcodenum.should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
@ -26,7 +26,7 @@ describe('Script', function() {
it('should parse this buffer containing another OP code', function() { it('should parse this buffer containing another OP code', function() {
var buf = new Buffer(1); var buf = new Buffer(1);
buf[0] = Opcode('OP_CHECKMULTISIG').toNumber(); buf[0] = Opcode.OP_CHECKMULTISIG;
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
script.chunks[0].opcodenum.should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
@ -41,7 +41,7 @@ describe('Script', function() {
it('should parse this buffer containing OP_PUSHDATA1 and three bytes of data', function() { it('should parse this buffer containing OP_PUSHDATA1 and three bytes of data', function() {
var buf = new Buffer([0, 0, 1, 2, 3]); var buf = new Buffer([0, 0, 1, 2, 3]);
buf[0] = Opcode('OP_PUSHDATA1').toNumber(); buf[0] = Opcode.OP_PUSHDATA1;
buf.writeUInt8(3, 1); buf.writeUInt8(3, 1);
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
@ -50,7 +50,7 @@ describe('Script', function() {
it('should parse this buffer containing OP_PUSHDATA2 and three bytes of data', function() { it('should parse this buffer containing OP_PUSHDATA2 and three bytes of data', function() {
var buf = new Buffer([0, 0, 0, 1, 2, 3]); var buf = new Buffer([0, 0, 0, 1, 2, 3]);
buf[0] = Opcode('OP_PUSHDATA2').toNumber(); buf[0] = Opcode.OP_PUSHDATA2;
buf.writeUInt16LE(3, 1); buf.writeUInt16LE(3, 1);
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
@ -59,7 +59,7 @@ describe('Script', function() {
it('should parse this buffer containing OP_PUSHDATA4 and three bytes of data', function() { it('should parse this buffer containing OP_PUSHDATA4 and three bytes of data', function() {
var buf = new Buffer([0, 0, 0, 0, 0, 1, 2, 3]); var buf = new Buffer([0, 0, 0, 0, 0, 1, 2, 3]);
buf[0] = Opcode('OP_PUSHDATA4').toNumber(); buf[0] = Opcode.OP_PUSHDATA4;
buf.writeUInt16LE(3, 1); buf.writeUInt16LE(3, 1);
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
@ -68,10 +68,10 @@ describe('Script', function() {
it('should parse this buffer an OP code, data, and another OP code', function() { it('should parse this buffer an OP code, data, and another OP code', function() {
var buf = new Buffer([0, 0, 0, 0, 0, 0, 1, 2, 3, 0]); var buf = new Buffer([0, 0, 0, 0, 0, 0, 1, 2, 3, 0]);
buf[0] = Opcode('OP_0').toNumber(); buf[0] = Opcode.OP_0;
buf[1] = Opcode('OP_PUSHDATA4').toNumber(); buf[1] = Opcode.OP_PUSHDATA4;
buf.writeUInt16LE(3, 2); buf.writeUInt16LE(3, 2);
buf[buf.length - 1] = Opcode('OP_0').toNumber(); buf[buf.length - 1] = Opcode.OP_0;
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(3); script.chunks.length.should.equal(3);
script.chunks[0].opcodenum.should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
@ -85,7 +85,7 @@ describe('Script', function() {
it('should output this buffer containing an OP code', function() { it('should output this buffer containing an OP code', function() {
var buf = new Buffer(1); var buf = new Buffer(1);
buf[0] = Opcode('OP_0').toNumber(); buf[0] = Opcode.OP_0;
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
script.chunks[0].opcodenum.should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
@ -94,7 +94,7 @@ describe('Script', function() {
it('should output this buffer containing another OP code', function() { it('should output this buffer containing another OP code', function() {
var buf = new Buffer(1); var buf = new Buffer(1);
buf[0] = Opcode('OP_CHECKMULTISIG').toNumber(); buf[0] = Opcode.OP_CHECKMULTISIG;
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
script.chunks[0].opcodenum.should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
@ -111,7 +111,7 @@ describe('Script', function() {
it('should output this buffer containing OP_PUSHDATA1 and three bytes of data', function() { it('should output this buffer containing OP_PUSHDATA1 and three bytes of data', function() {
var buf = new Buffer([0, 0, 1, 2, 3]); var buf = new Buffer([0, 0, 1, 2, 3]);
buf[0] = Opcode('OP_PUSHDATA1').toNumber(); buf[0] = Opcode.OP_PUSHDATA1;
buf.writeUInt8(3, 1); buf.writeUInt8(3, 1);
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
@ -121,7 +121,7 @@ describe('Script', function() {
it('should output this buffer containing OP_PUSHDATA2 and three bytes of data', function() { it('should output this buffer containing OP_PUSHDATA2 and three bytes of data', function() {
var buf = new Buffer([0, 0, 0, 1, 2, 3]); var buf = new Buffer([0, 0, 0, 1, 2, 3]);
buf[0] = Opcode('OP_PUSHDATA2').toNumber(); buf[0] = Opcode.OP_PUSHDATA2;
buf.writeUInt16LE(3, 1); buf.writeUInt16LE(3, 1);
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
@ -131,7 +131,7 @@ describe('Script', function() {
it('should output this buffer containing OP_PUSHDATA4 and three bytes of data', function() { it('should output this buffer containing OP_PUSHDATA4 and three bytes of data', function() {
var buf = new Buffer([0, 0, 0, 0, 0, 1, 2, 3]); var buf = new Buffer([0, 0, 0, 0, 0, 1, 2, 3]);
buf[0] = Opcode('OP_PUSHDATA4').toNumber(); buf[0] = Opcode.OP_PUSHDATA4;
buf.writeUInt16LE(3, 1); buf.writeUInt16LE(3, 1);
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(1); script.chunks.length.should.equal(1);
@ -141,10 +141,10 @@ describe('Script', function() {
it('should output this buffer an OP code, data, and another OP code', function() { it('should output this buffer an OP code, data, and another OP code', function() {
var buf = new Buffer([0, 0, 0, 0, 0, 0, 1, 2, 3, 0]); var buf = new Buffer([0, 0, 0, 0, 0, 0, 1, 2, 3, 0]);
buf[0] = Opcode('OP_0').toNumber(); buf[0] = Opcode.OP_0;
buf[1] = Opcode('OP_PUSHDATA4').toNumber(); buf[1] = Opcode.OP_PUSHDATA4;
buf.writeUInt16LE(3, 2); buf.writeUInt16LE(3, 2);
buf[buf.length - 1] = Opcode('OP_0').toNumber(); buf[buf.length - 1] = Opcode.OP_0;
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(3); script.chunks.length.should.equal(3);
script.chunks[0].opcodenum.should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
@ -175,10 +175,10 @@ describe('Script', function() {
it('should output this buffer an OP code, data, and another OP code', function() { it('should output this buffer an OP code, data, and another OP code', function() {
var buf = new Buffer([0, 0, 0, 0, 0, 0, 1, 2, 3, 0]); var buf = new Buffer([0, 0, 0, 0, 0, 0, 1, 2, 3, 0]);
buf[0] = Opcode('OP_0').toNumber(); buf[0] = Opcode.OP_0;
buf[1] = Opcode('OP_PUSHDATA4').toNumber(); buf[1] = Opcode.OP_PUSHDATA4;
buf.writeUInt16LE(3, 2); buf.writeUInt16LE(3, 2);
buf[buf.length - 1] = Opcode('OP_0').toNumber(); buf[buf.length - 1] = Opcode.OP_0;
var script = Script.fromBuffer(buf); var script = Script.fromBuffer(buf);
script.chunks.length.should.equal(3); script.chunks.length.should.equal(3);
script.chunks[0].opcodenum.should.equal(buf[0]); script.chunks[0].opcodenum.should.equal(buf[0]);
@ -352,7 +352,7 @@ describe('Script', function() {
Script().add(1000).toString().should.equal('0x03e8'); Script().add(1000).toString().should.equal('0x03e8');
Script().add('OP_CHECKMULTISIG').toString().should.equal('OP_CHECKMULTISIG'); Script().add('OP_CHECKMULTISIG').toString().should.equal('OP_CHECKMULTISIG');
Script().add('OP_1').add('OP_2').toString().should.equal('OP_1 OP_2'); Script().add('OP_1').add('OP_2').toString().should.equal('OP_1 OP_2');
Script().add(new Opcode('OP_CHECKMULTISIG')).toString().should.equal('OP_CHECKMULTISIG'); Script().add(Opcode.OP_CHECKMULTISIG).toString().should.equal('OP_CHECKMULTISIG');
Script().add(Opcode.map.OP_CHECKMULTISIG).toString().should.equal('OP_CHECKMULTISIG'); Script().add(Opcode.map.OP_CHECKMULTISIG).toString().should.equal('OP_CHECKMULTISIG');
}); });
@ -390,7 +390,7 @@ describe('Script', function() {
}); });
it('should work for no data OP_RETURN', function() { it('should work for no data OP_RETURN', function() {
Script().add(Opcode('OP_RETURN')).add(new Buffer('')).toString().should.equal('OP_RETURN 0'); Script().add(Opcode.OP_RETURN).add(new Buffer('')).toString().should.equal('OP_RETURN 0');
}); });
}); });