From 098c613cb0be398da92bd4ac665040617fa7eebd Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Fri, 18 Jul 2014 09:24:57 -0700 Subject: [PATCH 1/2] SIN should call EncodedData constructor Creating SINs was broken due to not calling the parent constructor, shich sets "converts" and "_encoding". I've fixed the problem and added tests that reveal the error. --- lib/SIN.js | 5 ++--- test/test.SIN.js | 8 ++++++++ test/test.SINKey.js | 16 ++++++++++------ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/SIN.js b/lib/SIN.js index dc69499..12f480d 100644 --- a/lib/SIN.js +++ b/lib/SIN.js @@ -3,10 +3,9 @@ var EncodedData = require('../util/EncodedData'); var util = require('util'); function SIN(type, payload) { - if (typeof type != 'number') { - SIN.super_.call(this, type, payload); + SIN.super_.call(this, type, payload); + if (typeof type != 'number') return; - }; this.data = new Buffer(1 + 1 + payload.length); this.encoding('binary'); this.prefix(0x0F); // SIN magic number, in numberspace diff --git a/test/test.SIN.js b/test/test.SIN.js index c91c29f..d84b223 100644 --- a/test/test.SIN.js +++ b/test/test.SIN.js @@ -33,6 +33,14 @@ describe('SIN', function() { s.should.equal(a.toString()); // check that validation doesn't change data }); }); + + describe('#SIN', function() { + it('should be able to create a new SIN with a version byte', function() { + var myhash = bitcore.util.sha256ripe160('test'); + var sin = new SIN(SIN.SIN_EPHEM, myhash); + should.exist(sin); + }); + }); }); diff --git a/test/test.SINKey.js b/test/test.SINKey.js index 3b66308..2bae2ec 100644 --- a/test/test.SINKey.js +++ b/test/test.SINKey.js @@ -4,16 +4,11 @@ var chai = chai || require('chai'); var bitcore = bitcore || require('../bitcore'); var should = chai.should(); -var SINKeyModule = bitcore.SINKey; -var SINKey; +var SINKey = bitcore.SINKey; describe('SINKey', function() { it('should initialze the main object', function() { - should.exist(SINKeyModule); - }); - it('should be able to create class', function() { - SINKey = SINKeyModule; should.exist(SINKey); }); it('should be able to create instance', function() { @@ -24,4 +19,13 @@ describe('SINKey', function() { should.exist(sk.privKey.public); should.exist(sk.privKey.compressed); }); + + describe('#storeObj', function() { + it('should give an object', function() { + var sinkey = new SINKey(); + sinkey.generate(); + var obj = sinkey.storeObj(); + should.exist(obj); + }); + }); }); From 4523012867d4ccb943705c3ab64b60f50aee588c Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Fri, 18 Jul 2014 11:40:13 -0700 Subject: [PATCH 2/2] set .converters and ._encoding by hand ...revert to previous change, since always calling the constructor of VersionedData may have unintended consequences. Instead, just set .converts and ._encoding, since they are no longer in the prototype and must be set on the object itself. --- lib/SIN.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/SIN.js b/lib/SIN.js index 12f480d..314db84 100644 --- a/lib/SIN.js +++ b/lib/SIN.js @@ -3,10 +3,13 @@ var EncodedData = require('../util/EncodedData'); var util = require('util'); function SIN(type, payload) { - SIN.super_.call(this, type, payload); - if (typeof type != 'number') + if (typeof type != 'number') { + SIN.super_.call(this, type, payload); return; + } this.data = new Buffer(1 + 1 + payload.length); + this.converters = this.encodings['binary'].converters; + this._encoding = this.encodings['binary']._encoding; this.encoding('binary'); this.prefix(0x0F); // SIN magic number, in numberspace this.type(type);