add fromPubKey to SIN

This commit is contained in:
Matias Alejo Garcia 2014-07-23 18:53:57 -03:00
parent 733835dc7c
commit 14d3165a73
2 changed files with 34 additions and 4 deletions

View File

@ -1,6 +1,8 @@
'use strict';
var VersionedData = require('../util/VersionedData'); var VersionedData = require('../util/VersionedData');
var EncodedData = require('../util/EncodedData'); var EncodedData = require('../util/EncodedData');
var util = require('util'); var util = require('util');
var coinUtil = require('../util');
function SIN(type, payload) { function SIN(type, payload) {
if (typeof type != 'number') { if (typeof type != 'number') {
@ -62,4 +64,18 @@ SIN.prototype.validate = function() {
if (this.data.length != 22) throw new Error('invalid data length'); 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; module.exports = SIN;

View File

@ -21,6 +21,8 @@ describe('SIN', function() {
}); });
var data = [ var data = [
['6bqov85Hsatqb8eLtwLW1PBQLWVNJkzPwgdAT3SYNkB6X2aF2n', false], ['6bqov85Hsatqb8eLtwLW1PBQLWVNJkzPwgdAT3SYNkB6X2aF2n', false],
['TfGPWmEYZCTr1FHqinjoGxnYAxdBhsta4qR', true],
['TexvSXam8vtoUviGajQyDuYdPSAEtwTNyZg', true]
]; ];
data.forEach(function(datum) { data.forEach(function(datum) {
var sin = datum[0]; var sin = datum[0];
@ -28,7 +30,6 @@ describe('SIN', function() {
it('should validate correctly ' + sin, function() { it('should validate correctly ' + sin, function() {
var a = new SIN(sin); var a = new SIN(sin);
var s = a.toString(); var s = a.toString();
a.isValid().should.equal(result); a.isValid().should.equal(result);
s.should.equal(a.toString()); // check that validation doesn't change data s.should.equal(a.toString()); // check that validation doesn't change data
}); });
@ -36,14 +37,27 @@ describe('SIN', function() {
describe('#SIN', function() { describe('#SIN', function() {
it('should be able to create a new SIN with a version byte', 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); var sin = new SIN(SIN.SIN_EPHEM, myhash);
should.exist(sin); 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');
});
});
}); });