fix SIN and add tests
This commit is contained in:
parent
87b818badf
commit
0e2df698cd
1364
browser/testdata.js
1364
browser/testdata.js
File diff suppressed because one or more lines are too long
|
@ -40,7 +40,7 @@ var util = require('util');
|
||||||
function Address(version, hash) {
|
function Address(version, hash) {
|
||||||
if (hash && hash.length && hash.length != 20)
|
if (hash && hash.length && hash.length != 20)
|
||||||
throw new Error('Hash must be 20 bytes');
|
throw new Error('Hash must be 20 bytes');
|
||||||
Address.super(this, arguments);
|
Address.super_.call(this, version, hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
util.inherits(Address, VersionedData);
|
util.inherits(Address, VersionedData);
|
||||||
|
@ -138,17 +138,12 @@ Address.fromScriptPubKey = function(scriptPubKey, network) {
|
||||||
// validates the address
|
// validates the address
|
||||||
Address.prototype.validate = function() {
|
Address.prototype.validate = function() {
|
||||||
this.doAsBinary(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 (this.data.length !== 21) throw new Error('invalid data length');
|
||||||
});
|
});
|
||||||
if (typeof this.network() === 'undefined') throw new Error('invalid network');
|
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
|
// returns the network information (livenet or testnet, as described on networks.js) of the address
|
||||||
Address.prototype.network = function() {
|
Address.prototype.network = function() {
|
||||||
var version = this.version();
|
var version = this.version();
|
||||||
|
|
|
@ -22,8 +22,6 @@ var nodeUtil = require('util');
|
||||||
var BIP0031_VERSION = 60000;
|
var BIP0031_VERSION = 60000;
|
||||||
|
|
||||||
function Connection(socket, peer, opts) {
|
function Connection(socket, peer, opts) {
|
||||||
Connection.super(this, arguments);
|
|
||||||
|
|
||||||
this.config = opts || bitcoreDefaults;
|
this.config = opts || bitcoreDefaults;
|
||||||
|
|
||||||
this.network = networks[this.config.network] || networks.livenet;
|
this.network = networks[this.config.network] || networks.livenet;
|
||||||
|
|
|
@ -6,7 +6,7 @@ var util = require('util');
|
||||||
|
|
||||||
//compressed is true if public key is compressed; false otherwise
|
//compressed is true if public key is compressed; false otherwise
|
||||||
function PrivateKey(version, buf, compressed) {
|
function PrivateKey(version, buf, compressed) {
|
||||||
PrivateKey.super(this, arguments);
|
PrivateKey.super_.call(this, version, buf);
|
||||||
if (compressed !== undefined)
|
if (compressed !== undefined)
|
||||||
this.compressed(compressed);
|
this.compressed(compressed);
|
||||||
};
|
};
|
||||||
|
@ -15,7 +15,7 @@ EncodedData.applyEncodingsTo(PrivateKey);
|
||||||
|
|
||||||
PrivateKey.prototype.validate = function() {
|
PrivateKey.prototype.validate = function() {
|
||||||
this.doAsBinary(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)
|
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');
|
throw new Error('invalid data length');
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,7 +4,7 @@ var util = require('util');
|
||||||
|
|
||||||
function SIN(type, payload) {
|
function SIN(type, payload) {
|
||||||
if (typeof type != 'number') {
|
if (typeof type != 'number') {
|
||||||
SIN.super(this, arguments);
|
SIN.super_.call(this, type, payload);
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
this.data = new Buffer(1 + 1 + payload.length);
|
this.data = new Buffer(1 + 1 + payload.length);
|
||||||
|
@ -56,7 +56,7 @@ SIN.prototype.payload = function(data) {
|
||||||
|
|
||||||
SIN.prototype.validate = function() {
|
SIN.prototype.validate = function() {
|
||||||
this.doAsBinary(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');
|
if (this.data.length != 22) throw new Error('invalid data length');
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -17,12 +17,16 @@ describe('Address', function() {
|
||||||
var a = new Address('1KfyjCgBSMsLqiCbakfSdeoBUqMqLUiu3T');
|
var a = new Address('1KfyjCgBSMsLqiCbakfSdeoBUqMqLUiu3T');
|
||||||
should.exist(a);
|
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 = [
|
var data = [
|
||||||
['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', true],
|
['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', true],
|
||||||
['11111111111111111111111111122222234', false], // totally invalid
|
['11111111111111111111111111122222234', false], // totally invalid
|
||||||
['32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6', true],
|
['32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6', true],
|
||||||
['1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', true],
|
['1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', true],
|
||||||
['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', true],
|
|
||||||
['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNb', false], //bad checksum ... thanks @wtogami
|
['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNb', false], //bad checksum ... thanks @wtogami
|
||||||
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', true],
|
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', true],
|
||||||
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW600', false], // bad checksum
|
['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW600', false], // bad checksum
|
||||||
|
|
|
@ -5,21 +5,34 @@ var bitcore = bitcore || require('../bitcore');
|
||||||
|
|
||||||
var should = chai.should();
|
var should = chai.should();
|
||||||
|
|
||||||
var SINModule = bitcore.SIN;
|
var SIN = bitcore.SIN;
|
||||||
var SIN;
|
|
||||||
|
|
||||||
describe('SIN', function() {
|
describe('SIN', function() {
|
||||||
it('should initialze the main object', function() {
|
|
||||||
should.exist(SINModule);
|
|
||||||
});
|
|
||||||
it('should be able to create class', function() {
|
it('should be able to create class', function() {
|
||||||
SIN = SINModule;
|
|
||||||
should.exist(SIN);
|
should.exist(SIN);
|
||||||
});
|
});
|
||||||
it('should be able to create instance', function() {
|
it('should be able to create instance', function() {
|
||||||
var s = new SIN();
|
var s = new SIN();
|
||||||
should.exist(s);
|
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
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -134,4 +134,4 @@ Parser.prototype.varStr = function() {
|
||||||
return this.buffer(len);
|
return this.buffer(len);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = require('soop')(Parser);
|
module.exports = (Parser);
|
||||||
|
|
|
@ -54,16 +54,6 @@ EncodedData.prototype.validate = function() {
|
||||||
this._validate();
|
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)
|
// convert to a string (in base58 form)
|
||||||
EncodedData.prototype.toString = function() {
|
EncodedData.prototype.toString = function() {
|
||||||
return this.as('base58');
|
return this.as('base58');
|
||||||
|
@ -154,4 +144,4 @@ EncodedData.applyEncodingsTo = function(aClass) {
|
||||||
|
|
||||||
EncodedData.applyEncodingsTo(EncodedData);
|
EncodedData.applyEncodingsTo(EncodedData);
|
||||||
|
|
||||||
module.exports = require('soop')(EncodedData);
|
module.exports = (EncodedData);
|
||||||
|
|
|
@ -4,8 +4,8 @@ var EncodedData = require('./EncodedData');
|
||||||
|
|
||||||
|
|
||||||
function VersionedData(version, payload) {
|
function VersionedData(version, payload) {
|
||||||
|
VersionedData.super_.call(this, version, payload);
|
||||||
if (typeof version != 'number') {
|
if (typeof version != 'number') {
|
||||||
VersionedData.super(this, arguments);
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
this.data = new Buffer(payload.length + 1);
|
this.data = new Buffer(payload.length + 1);
|
||||||
|
@ -39,4 +39,4 @@ VersionedData.prototype.payload = function(data) {
|
||||||
return this.as('binary').slice(1);
|
return this.as('binary').slice(1);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = require('soop')(VersionedData);
|
module.exports = (VersionedData);
|
||||||
|
|
Loading…
Reference in New Issue