Skip to content

Commit

Permalink
Merge pull request #448 from matiu/feature/sin02
Browse files Browse the repository at this point in the history
Feature/sin02
  • Loading branch information
Ryan X. Charles committed Jul 24, 2014
2 parents b75b964 + 3dd0912 commit cd1d667
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
18 changes: 17 additions & 1 deletion browser/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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');
Expand Down
16 changes: 16 additions & 0 deletions lib/SIN.js
Original file line number Diff line number Diff line change
@@ -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') {
Expand Down Expand Up @@ -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;
20 changes: 17 additions & 3 deletions test/test.SIN.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ describe('SIN', function() {
});
var data = [
['6bqov85Hsatqb8eLtwLW1PBQLWVNJkzPwgdAT3SYNkB6X2aF2n', false],
['TfGPWmEYZCTr1FHqinjoGxnYAxdBhsta4qR', true],
['TexvSXam8vtoUviGajQyDuYdPSAEtwTNyZg', true]
];
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
});
Expand All @@ -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');
});

});
});





0 comments on commit cd1d667

Please sign in to comment.