fix SIN and add tests

This commit is contained in:
Manuel Araoz 2014-07-10 17:17:24 -03:00
parent 87b818badf
commit 0e2df698cd
10 changed files with 34 additions and 1398 deletions

File diff suppressed because one or more lines are too long

View File

@ -40,7 +40,7 @@ var util = require('util');
function Address(version, hash) {
if (hash && hash.length && hash.length != 20)
throw new Error('Hash must be 20 bytes');
Address.super(this, arguments);
Address.super_.call(this, version, hash);
}
util.inherits(Address, VersionedData);
@ -138,17 +138,12 @@ Address.fromScriptPubKey = function(scriptPubKey, network) {
// validates the address
Address.prototype.validate = function() {
this.doAsBinary(function() {
Address.super(this, 'validate', arguments);
Address.super_.prototype.validate.apply(this);
if (this.data.length !== 21) throw new Error('invalid data length');
});
if (typeof this.network() === 'undefined') throw new Error('invalid network');
};
Address.prototype.isValid = function() {
var answer = Address.super(this, 'isValid', arguments);
return answer;
};
// returns the network information (livenet or testnet, as described on networks.js) of the address
Address.prototype.network = function() {
var version = this.version();

View File

@ -22,8 +22,6 @@ var nodeUtil = require('util');
var BIP0031_VERSION = 60000;
function Connection(socket, peer, opts) {
Connection.super(this, arguments);
this.config = opts || bitcoreDefaults;
this.network = networks[this.config.network] || networks.livenet;

View File

@ -6,7 +6,7 @@ var util = require('util');
//compressed is true if public key is compressed; false otherwise
function PrivateKey(version, buf, compressed) {
PrivateKey.super(this, arguments);
PrivateKey.super_.call(this, version, buf);
if (compressed !== undefined)
this.compressed(compressed);
};
@ -15,7 +15,7 @@ EncodedData.applyEncodingsTo(PrivateKey);
PrivateKey.prototype.validate = function() {
this.doAsBinary(function() {
PrivateKey.super(this, 'validate', arguments);
PrivateKey.super_.prototype.validate.call(this);
if (this.data.length < 32 || (this.data.length > 1 + 32 && !this.compressed()) || (this.data.length == 1 + 32 + 1 && this.data[1 + 32 + 1 - 1] != 1) || this.data.length > 1 + 32 + 1)
throw new Error('invalid data length');
});

View File

@ -4,7 +4,7 @@ var util = require('util');
function SIN(type, payload) {
if (typeof type != 'number') {
SIN.super(this, arguments);
SIN.super_.call(this, type, payload);
return;
};
this.data = new Buffer(1 + 1 + payload.length);
@ -56,7 +56,7 @@ SIN.prototype.payload = function(data) {
SIN.prototype.validate = function() {
this.doAsBinary(function() {
SIN.super(this, 'validate', arguments);
SIN.super_.prototype.validate.call(this);
if (this.data.length != 22) throw new Error('invalid data length');
});
};

View File

@ -17,12 +17,16 @@ describe('Address', function() {
var a = new Address('1KfyjCgBSMsLqiCbakfSdeoBUqMqLUiu3T');
should.exist(a);
});
it('should be able to transform to string', function() {
var a = new Address('1GfGL3iLTfX43KSCd95WhMi4bgU36qjzC1');
a.toString.bind(a).should.not.throw();
a.toString().should.equal('1GfGL3iLTfX43KSCd95WhMi4bgU36qjzC1');
});
var data = [
['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', true],
['11111111111111111111111111122222234', false], // totally invalid
['32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6', true],
['1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', true],
['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', true],
['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNb', false], //bad checksum ... thanks @wtogami
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', true],
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW600', false], // bad checksum

View File

@ -5,21 +5,34 @@ var bitcore = bitcore || require('../bitcore');
var should = chai.should();
var SINModule = bitcore.SIN;
var SIN;
var SIN = bitcore.SIN;
describe('SIN', function() {
it('should initialze the main object', function() {
should.exist(SINModule);
});
it('should be able to create class', function() {
SIN = SINModule;
should.exist(SIN);
});
it('should be able to create instance', function() {
var s = new SIN();
should.exist(s);
});
it('should be able to convert to string', function() {
var s = new SIN('6bqov85Hsatqb8eLtwLW1PBQLWVNJkzPwgdAT3SYNkB6X2aF2n');
s.toString.bind(s).should.not.throw();
});
var data = [
['6bqov85Hsatqb8eLtwLW1PBQLWVNJkzPwgdAT3SYNkB6X2aF2n', false],
];
data.forEach(function(datum) {
var sin = datum[0];
var result = datum[1];
it('should validate correctly ' + sin, function() {
var a = new SIN(sin);
var s = a.toString();
a.isValid().should.equal(result);
s.should.equal(a.toString()); // check that validation doesn't change data
});
});
});

View File

@ -134,4 +134,4 @@ Parser.prototype.varStr = function() {
return this.buffer(len);
};
module.exports = require('soop')(Parser);
module.exports = (Parser);

View File

@ -54,16 +54,6 @@ EncodedData.prototype.validate = function() {
this._validate();
};
// Boolean protocol for testing if valid
EncodedData.prototype.isValid = function() {
try {
this.validate();
return true;
} catch (e) {
return false;
}
};
// convert to a string (in base58 form)
EncodedData.prototype.toString = function() {
return this.as('base58');
@ -154,4 +144,4 @@ EncodedData.applyEncodingsTo = function(aClass) {
EncodedData.applyEncodingsTo(EncodedData);
module.exports = require('soop')(EncodedData);
module.exports = (EncodedData);

View File

@ -4,8 +4,8 @@ var EncodedData = require('./EncodedData');
function VersionedData(version, payload) {
VersionedData.super_.call(this, version, payload);
if (typeof version != 'number') {
VersionedData.super(this, arguments);
return;
};
this.data = new Buffer(payload.length + 1);
@ -39,4 +39,4 @@ VersionedData.prototype.payload = function(data) {
return this.as('binary').slice(1);
};
module.exports = require('soop')(VersionedData);
module.exports = (VersionedData);