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 314db84..a14cfe5 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 && pubKey.length != 65)) + throw new Error('Invalid public key'); + + 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..a1c00be 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 }); @@ -41,9 +42,22 @@ describe('SIN', function() { 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('03e0973263b4e0d5f5f56d25d430e777ab3838ff644db972c0bf32c31da5686c27')}).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'); + }); + + }); }); - -