2014-10-20 14:35:49 -07:00
|
|
|
'use strict';
|
|
|
|
|
2015-09-18 06:49:41 -07:00
|
|
|
var bitauth = require('../');
|
|
|
|
var chai = require('chai');
|
2014-06-25 11:15:41 -07:00
|
|
|
|
|
|
|
describe('bitauth', function() {
|
|
|
|
|
2014-10-20 14:35:49 -07:00
|
|
|
var should = chai.should();
|
|
|
|
|
2014-10-28 13:19:02 -07:00
|
|
|
// previously known keys for comparison
|
|
|
|
var keysKnown = {
|
|
|
|
priv: '97811b691dd7ebaeb67977d158e1da2c4d3eaa4ee4e2555150628acade6b344c',
|
|
|
|
pub: '02326209e52f6f17e987ec27c56a1321acf3d68088b8fb634f232f12ccbc9a4575',
|
|
|
|
sin: 'Tf3yr5tYvccKNVrE26BrPs6LWZRh8woHwjR'
|
2015-09-18 06:49:17 -07:00
|
|
|
};
|
2014-10-28 13:19:02 -07:00
|
|
|
|
2014-11-03 09:05:33 -08:00
|
|
|
// a private key that will produce a public key with a leading zero
|
|
|
|
var privateKeyToZero = 'c6b7f6bfe5bb19b1e390e55ed4ba5df8af6068d0eb89379a33f9c19aacf6c08c';
|
|
|
|
|
2014-10-28 13:19:02 -07:00
|
|
|
// keys generated
|
2015-09-18 06:49:17 -07:00
|
|
|
var keys = null;
|
2014-10-28 13:19:02 -07:00
|
|
|
|
|
|
|
// invalid checksum
|
2015-09-18 06:49:17 -07:00
|
|
|
var sinbad = 'Tf1Jc1xSbqasm5QLwwSQc5umddx2h7mAMhX';
|
2014-10-28 13:19:02 -07:00
|
|
|
|
|
|
|
// valid sin
|
2015-09-18 06:49:17 -07:00
|
|
|
var singood = 'TfG4ScDgysrSpodWD4Re5UtXmcLbY5CiUHA';
|
2014-10-28 13:19:02 -07:00
|
|
|
|
|
|
|
// data to sign
|
2015-09-18 06:49:17 -07:00
|
|
|
var contract = 'kéyboard cät';
|
|
|
|
var secret = 'o hai, nsa. how i do teh cryptos?';
|
|
|
|
var password = 's4705hiru13z!';
|
2015-02-02 15:54:47 -08:00
|
|
|
var encryptedSecret = '291Dm9unZMwfxBA7BEHiQsraRxCrMRqwJ2TjCWwEH3Sp5QGMehNFNgZLo62sgF5Khe';
|
2015-09-18 06:49:17 -07:00
|
|
|
|
2014-10-28 13:19:02 -07:00
|
|
|
// signature from generate keys
|
2014-06-25 11:15:41 -07:00
|
|
|
var signature = null;
|
2015-09-18 06:49:17 -07:00
|
|
|
var enc = null;
|
2014-06-25 11:15:41 -07:00
|
|
|
|
|
|
|
describe('#generateSin', function() {
|
|
|
|
|
|
|
|
it('should generate a sin object', function(done) {
|
|
|
|
keys = bitauth.generateSin();
|
|
|
|
should.exist(keys);
|
|
|
|
should.exist(keys.pub);
|
|
|
|
should.exist(keys.priv);
|
|
|
|
should.exist(keys.sin);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#getPublicKeyFromPrivateKey', function() {
|
|
|
|
|
|
|
|
it('should properly get the public key', function(done) {
|
|
|
|
bitauth.getPublicKeyFromPrivateKey(keys.priv).should.equal(keys.pub);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2014-10-28 13:19:02 -07:00
|
|
|
it('should properly get compressed public key from a previously known private key', function(done) {
|
|
|
|
bitauth.getPublicKeyFromPrivateKey(keysKnown.priv).should.equal(keysKnown.pub);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2014-06-25 11:15:41 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#getSinFromPublicKey', function() {
|
|
|
|
|
|
|
|
it('should properly get the sin', function(done) {
|
|
|
|
bitauth.getSinFromPublicKey(keys.pub).should.equal(keys.sin);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2014-10-28 13:19:02 -07:00
|
|
|
it('should properly get the sin from a previously known compressed public key', function(done) {
|
|
|
|
bitauth.getSinFromPublicKey(keysKnown.pub).should.equal(keysKnown.sin);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2014-06-25 11:15:41 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#sign', function() {
|
|
|
|
|
|
|
|
it('should sign the string', function(done) {
|
|
|
|
signature = bitauth.sign(contract, keys.priv);
|
|
|
|
should.exist(signature);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#verifySignature', function() {
|
|
|
|
|
|
|
|
it('should verify the signature', function(done) {
|
2015-09-18 06:49:17 -07:00
|
|
|
bitauth.verifySignature(contract, keys.pub, signature, function(err, valid) {
|
2014-10-28 13:19:02 -07:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(valid);
|
|
|
|
valid.should.equal(true);
|
|
|
|
done();
|
|
|
|
});
|
2014-06-25 11:15:41 -07:00
|
|
|
});
|
|
|
|
|
2014-11-03 09:05:33 -08:00
|
|
|
it('should verify the signature with leading zero public key', function(done) {
|
|
|
|
|
|
|
|
var leadingZeroKeys = {
|
|
|
|
priv: privateKeyToZero,
|
|
|
|
pub: bitauth.getPublicKeyFromPrivateKey(privateKeyToZero)
|
|
|
|
};
|
|
|
|
|
|
|
|
signature = bitauth.sign(contract, leadingZeroKeys.priv);
|
2015-09-18 06:49:17 -07:00
|
|
|
bitauth.verifySignature(contract, leadingZeroKeys.pub, signature, function(err, valid) {
|
2014-11-03 09:05:33 -08:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(valid);
|
|
|
|
valid.should.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-06-25 11:15:41 -07:00
|
|
|
});
|
|
|
|
|
2014-07-16 10:46:49 -07:00
|
|
|
describe('#validateSinTrue', function() {
|
|
|
|
|
|
|
|
it('should validate the sin as true', function(done) {
|
2014-10-28 13:19:02 -07:00
|
|
|
var valid = bitauth.validateSin(singood);
|
2014-07-16 10:46:49 -07:00
|
|
|
should.equal(true, valid);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#validateSinFalse', function() {
|
|
|
|
|
2014-11-03 10:30:11 -08:00
|
|
|
it('should validate the sin as false because of bad checksum', function(done) {
|
2014-10-28 13:19:02 -07:00
|
|
|
var valid = bitauth.validateSin(sinbad);
|
2014-07-16 10:46:49 -07:00
|
|
|
should.equal(false, valid);
|
|
|
|
done();
|
|
|
|
});
|
2014-11-03 10:30:11 -08:00
|
|
|
|
|
|
|
it('should validate the sin as false because of non-base58', function(done) {
|
|
|
|
var valid = bitauth.validateSin('not#base!58');
|
|
|
|
should.equal(false, valid);
|
|
|
|
done();
|
|
|
|
});
|
2014-07-16 10:46:49 -07:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#validateSinCallback', function() {
|
|
|
|
|
2014-10-20 15:29:50 -07:00
|
|
|
it('should receive error callback', function(done) {
|
2015-09-18 06:49:17 -07:00
|
|
|
bitauth.validateSin(sinbad, function(err) {
|
2014-10-20 15:29:50 -07:00
|
|
|
should.exist(err);
|
2014-10-28 13:19:02 -07:00
|
|
|
err.message.should.equal('Checksum does not match');
|
2014-10-20 15:29:50 -07:00
|
|
|
done();
|
|
|
|
});
|
2014-07-16 10:46:49 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-10-20 14:35:49 -07:00
|
|
|
// node specific tests
|
2015-09-18 06:49:17 -07:00
|
|
|
if (typeof(window) === 'undefined') {
|
2014-10-20 14:35:49 -07:00
|
|
|
|
|
|
|
describe('#encrypt', function() {
|
|
|
|
|
|
|
|
it('should encrypt the secret message', function(done) {
|
|
|
|
enc = bitauth.encrypt(password, secret);
|
|
|
|
should.exist(enc);
|
|
|
|
done();
|
|
|
|
});
|
2014-06-25 11:15:41 -07:00
|
|
|
});
|
|
|
|
|
2014-10-20 14:35:49 -07:00
|
|
|
describe('#decrypt', function() {
|
2014-06-25 11:15:41 -07:00
|
|
|
|
2014-10-20 14:35:49 -07:00
|
|
|
it('should decrypt the secret message', function(done) {
|
|
|
|
var dec = bitauth.decrypt(password, enc);
|
|
|
|
should.exist(dec);
|
2015-02-02 15:54:47 -08:00
|
|
|
dec.should.equal(secret);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should decrypt a previously known message', function(done) {
|
|
|
|
var dec = bitauth.decrypt(password, encryptedSecret);
|
|
|
|
should.exist(dec);
|
|
|
|
dec.should.equal(secret);
|
2014-10-20 14:35:49 -07:00
|
|
|
done();
|
|
|
|
});
|
2014-06-25 11:15:41 -07:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-10-20 14:35:49 -07:00
|
|
|
describe('#middleware', function() {
|
2014-06-29 15:01:41 -07:00
|
|
|
|
2014-10-20 14:35:49 -07:00
|
|
|
it('should expose an express middleware', function(done) {
|
2015-09-18 06:49:17 -07:00
|
|
|
bitauth.middleware({}, {}, function() {
|
2014-10-20 14:35:49 -07:00
|
|
|
done();
|
|
|
|
});
|
2014-06-29 15:01:41 -07:00
|
|
|
});
|
2014-10-20 14:35:49 -07:00
|
|
|
|
2014-06-29 15:01:41 -07:00
|
|
|
});
|
|
|
|
|
2014-10-20 14:35:49 -07:00
|
|
|
}
|
2014-06-29 15:01:41 -07:00
|
|
|
|
2014-06-25 11:15:41 -07:00
|
|
|
});
|