From 14d3165a73a8b0a2f769f132b969a5b8cb6bde0c Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 23 Jul 2014 18:53:57 -0300 Subject: [PATCH 1/5] add fromPubKey to SIN --- lib/SIN.js | 16 ++++++++++++++++ test/test.SIN.js | 22 ++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/SIN.js b/lib/SIN.js index 314db84..53889f0 100644 --- a/lib/SIN.js +++ b/lib/SIN.js @@ -1,6 +1,8 @@ +'use strict'; var VersionedData = require('../util/VersionedData'); var EncodedData = require('../util/EncodedData'); var util = require('util'); +var coinUtil = require('../util'); function SIN(type, payload) { if (typeof type != 'number') { @@ -62,4 +64,18 @@ SIN.prototype.validate = function() { if (this.data.length != 22) throw new Error('invalid data length'); }); }; + + +// create a SIN from a public key +SIN.fromPubKey = function(pubKey, type) { + if (!type) + type = SIN.SIN_EPHEM; + + if (!Buffer.isBuffer(pubKey) || pubKey.length !== 33) + throw new Error('Invalid public key' + pubKey.length); + + var hash = coinUtil.sha256ripe160(pubKey); + return new SIN(hash, type); +}; + module.exports = SIN; diff --git a/test/test.SIN.js b/test/test.SIN.js index d84b223..1094727 100644 --- a/test/test.SIN.js +++ b/test/test.SIN.js @@ -21,6 +21,8 @@ describe('SIN', function() { }); var data = [ ['6bqov85Hsatqb8eLtwLW1PBQLWVNJkzPwgdAT3SYNkB6X2aF2n', false], + ['TfGPWmEYZCTr1FHqinjoGxnYAxdBhsta4qR', true], + ['TexvSXam8vtoUviGajQyDuYdPSAEtwTNyZg', true] ]; data.forEach(function(datum) { var sin = datum[0]; @@ -28,7 +30,6 @@ describe('SIN', function() { 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 }); @@ -36,14 +37,27 @@ describe('SIN', function() { describe('#SIN', function() { it('should be able to create a new SIN with a version byte', function() { - var myhash = bitcore.util.sha256ripe160('test'); + var myhash = bitcore.util.sha256ripe160('test123123'); var sin = new SIN(SIN.SIN_EPHEM, myhash); should.exist(sin); }); }); + describe('#fromPubKey', function() { + it('should fail to create a new SIN not using a pub key', function() { + (function() { SIN.fromPubKey('1234')}).should.throw(); + }); + it('should fail to create a new SIN not using a pub key case 2', function() { + (function() { SIN.fromPubKey('X2345678901234567890123456789012112345678901234567890123456781011')}).should.throw(); + }); + it('should be able to create a new SIN using a pub key', function() { + var pubkey1 = new Buffer('03e0973263b4e0d5f5f56d25d430e777ab3838ff644db972c0bf32c31da5686c27', 'hex'); + var sin = SIN.fromPubKey(pubkey1); + should.exist(sin); + sin.toString().should.equal('FrCfKjSFN1Ubp3x6AD6au8M5LTaNAEN8b'); + }); + + }); }); - - From 15c55e560af0145130632e66a9f0fa0dba49d5d3 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 23 Jul 2014 19:49:42 -0300 Subject: [PATCH 2/5] support uncompressed pub keys also --- browser/bundle.js | 18 +++++++++++++++++- lib/SIN.js | 4 ++-- test/mocha.opts | 1 - 3 files changed, 19 insertions(+), 4 deletions(-) delete mode 100644 test/mocha.opts diff --git a/browser/bundle.js b/browser/bundle.js index 4067855..c27d1ba 100644 --- a/browser/bundle.js +++ b/browser/bundle.js @@ -3005,9 +3005,11 @@ module.exports = RpcClient; module.exports=require('tBM27q'); },{}],"tBM27q":[function(require,module,exports){ (function (Buffer){ +'use strict'; var VersionedData = require('../util/VersionedData'); var EncodedData = require('../util/EncodedData'); var util = require('util'); +var coinUtil = require('../util'); function SIN(type, payload) { if (typeof type != 'number') { @@ -3069,10 +3071,24 @@ SIN.prototype.validate = function() { if (this.data.length != 22) throw new Error('invalid data length'); }); }; + + +// create a SIN from a public key +SIN.fromPubKey = function(pubKey, type) { + if (!type) + type = SIN.SIN_EPHEM; + + if (!Buffer.isBuffer(pubKey) || (pubKey.length !== 33 && pubKey.length != 65)) + throw new Error('Invalid public key'); + + var hash = coinUtil.sha256ripe160(pubKey); + return new SIN(hash, type); +}; + module.exports = SIN; }).call(this,require("buffer").Buffer) -},{"../util/EncodedData":"eLfUFE","../util/VersionedData":"QLzNQg","buffer":91,"util":123}],"EyghZQ":[function(require,module,exports){ +},{"../util":181,"../util/EncodedData":"eLfUFE","../util/VersionedData":"QLzNQg","buffer":91,"util":123}],"EyghZQ":[function(require,module,exports){ var coinUtil = require('../util'); var timeUtil = require('../util/time'); var Key = require('./Key'); diff --git a/lib/SIN.js b/lib/SIN.js index 53889f0..a14cfe5 100644 --- a/lib/SIN.js +++ b/lib/SIN.js @@ -71,8 +71,8 @@ SIN.fromPubKey = function(pubKey, type) { if (!type) type = SIN.SIN_EPHEM; - if (!Buffer.isBuffer(pubKey) || pubKey.length !== 33) - throw new Error('Invalid public key' + pubKey.length); + if (!Buffer.isBuffer(pubKey) || (pubKey.length !== 33 && pubKey.length != 65)) + throw new Error('Invalid public key'); var hash = coinUtil.sha256ripe160(pubKey); return new SIN(hash, type); diff --git a/test/mocha.opts b/test/mocha.opts deleted file mode 100644 index e2bfcc5..0000000 --- a/test/mocha.opts +++ /dev/null @@ -1 +0,0 @@ ---ui qunit From cad0fcf993e31a96040d4ba03c3674749f11abca Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 23 Jul 2014 19:51:34 -0300 Subject: [PATCH 3/5] update tests --- test/test.SIN.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.SIN.js b/test/test.SIN.js index 1094727..6f3ffdb 100644 --- a/test/test.SIN.js +++ b/test/test.SIN.js @@ -47,7 +47,7 @@ describe('SIN', function() { (function() { SIN.fromPubKey('1234')}).should.throw(); }); it('should fail to create a new SIN not using a pub key case 2', function() { - (function() { SIN.fromPubKey('X2345678901234567890123456789012112345678901234567890123456781011')}).should.throw(); + (function() { SIN.fromPubKey('03e0973263b4e0d5f5f56d25d430e777ab3838ff644db972c0bf32c31da5686c27')}).should.throw(); }); it('should be able to create a new SIN using a pub key', function() { var pubkey1 = new Buffer('03e0973263b4e0d5f5f56d25d430e777ab3838ff644db972c0bf32c31da5686c27', 'hex'); From 85e013c620fab3b21258624f9bd39b8ccf0ceb75 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 23 Jul 2014 19:55:18 -0300 Subject: [PATCH 4/5] fix tests --- test/test.SIN.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.SIN.js b/test/test.SIN.js index 6f3ffdb..a1c00be 100644 --- a/test/test.SIN.js +++ b/test/test.SIN.js @@ -37,7 +37,7 @@ describe('SIN', function() { describe('#SIN', function() { it('should be able to create a new SIN with a version byte', function() { - var myhash = bitcore.util.sha256ripe160('test123123'); + var myhash = bitcore.util.sha256ripe160('test'); var sin = new SIN(SIN.SIN_EPHEM, myhash); should.exist(sin); }); From 3dd09129c873355ed6cafa60f1703fd5dd519773 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Wed, 23 Jul 2014 19:56:19 -0300 Subject: [PATCH 5/5] fix tests --- test/mocha.opts | 1 + 1 file changed, 1 insertion(+) create mode 100644 test/mocha.opts diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..e2bfcc5 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1 @@ +--ui qunit