fix tests and code that broken them

This commit is contained in:
Gordon Hall 2014-10-07 13:56:18 -04:00
parent 92405b3a90
commit 54235dad6a
7 changed files with 38 additions and 88 deletions

View File

@ -2,11 +2,10 @@
// get base functionality // get base functionality
var BitAuth = require('./lib/bitauth'); var BitAuth = require('./lib/bitauth');
var bitauth = new BitAuth();
// add node-specific encrypt/decrypt // add node-specific encrypt/decrypt
bitauth.encrypt = require('./lib/encrypt'); BitAuth.encrypt = require('./lib/encrypt');
bitauth.decrypt = require('./lib/decrypt'); BitAuth.decrypt = require('./lib/decrypt');
bitauth.middleware = require('./lib/middleware/bitauth'); BitAuth.middleware = require('./lib/middleware/bitauth');
module.exports = bitauth; module.exports = BitAuth;

View File

@ -14,7 +14,7 @@ var BitAuth = function() {
BitAuth.prototype.generateIdentity = function( keypair ) { BitAuth.prototype.generateIdentity = function( keypair ) {
var self = this; var self = this;
if (!keypair) var keypair = new Keypair().fromRandom(); if (!keypair) var keypair = new Keypair().fromRandom();
self._keypair = keypair; self._keypair = keypair;
@ -25,42 +25,30 @@ BitAuth.prototype.generateIdentity = function( keypair ) {
BitAuth.prototype.sign = function(data, privkey) { BitAuth.prototype.sign = function(data, privkey) {
var self = this; var self = this;
var signature = Message.sign( new Buffer( data ) , privkey ); var keypair = new Keypair().fromPrivkey( privkey );
var signature = Message.sign( new Buffer( data ) , keypair );
return signature; return signature;
}; };
BitAuth.prototype.getIdentityFromPublicKey = function( pubkey ) { BitAuth.prototype.getIdentityFromPublicKey = function( pubkey ) {
var identity = new Identity( pubkey ); var identity = new Identity( pubkey );
return identity.toString(); return identity;
}; };
BitAuth.prototype.getPublicKeyFromPrivateKey = function( privkey ) { BitAuth.prototype.getPublicKeyFromPrivateKey = function( privkey ) {
var keypair = Keypair().fromString( JSON.stringify({ privkey : privkey }) ); var keypair = new Keypair();
return keypair.pubkey.toString(); return keypair.fromPrivkey( privkey ).pubkey;
}; };
BitAuth.prototype.getPublicKeyFromIdentity = function( identity ) {
var identity = Identity().fromString( identity );
console.log('pubkeyfromident, ident:' , identity );
return identity.toPubkey(); BitAuth.prototype.verifySignature = function(data, pubkey, signature, callback) {
};
BitAuth.prototype.verifySignature = function(data, identity, signature, callback) {
var self = this; var self = this;
console.log('verifySignature', identity , signature );
var pubkey = self.getPublicKeyFromIdentity( identity );
console.log('pubkey', pubkey );
var messageBuffer = new Buffer( data ); var messageBuffer = new Buffer( data );
var keypair = new Keypair();
var signature = Message.sign( messageBuffer , identity._keypair );
console.log('verifySig', identity); keypair.fromString( JSON.stringify({ pubkey: pubkey }) )
var identity = new Identity().fromPubkey( keypair.pubkey );
try { try {
Message.verify( messageBuffer , signature , identity ); Message.verify( messageBuffer , signature , identity );
@ -70,14 +58,12 @@ BitAuth.prototype.verifySignature = function(data, identity, signature, callback
} }
}; };
BitAuth.prototype.validateIdentity = function( identity , callback ) { BitAuth.prototype.validateIdentity = function( identity ) {
var s = new Identity( identity );
try { try {
s.validate(); new Identity( identity ).validate();
callback(); return true;
} catch(err) { } catch(err) {
callback(err); return false;
} }
}; };

View File

@ -1,4 +1,4 @@
var base58 = require('base58-native'); var base58 = require('bitcore').Base58;
var crypto = require('crypto'); var crypto = require('crypto');
module.exports = function decrypt(password, str) { module.exports = function decrypt(password, str) {

View File

@ -1,4 +1,4 @@
var base58 = require('base58-native'); var base58 = require('bitcore').Base58;
var crypto = require('crypto'); var crypto = require('crypto');
module.exports = function encrypt(password, str) { module.exports = function encrypt(password, str) {
@ -9,6 +9,6 @@ module.exports = function encrypt(password, str) {
a.copy(buf, 0); a.copy(buf, 0);
b.copy(buf, a.length); b.copy(buf, a.length);
return base58.encode(buf); return base58.encode(buf);
}; };

View File

@ -28,8 +28,7 @@
"main": "index.js", "main": "index.js",
"version": "0.2.0", "version": "0.2.0",
"dependencies": { "dependencies": {
"base58-native": "^0.1.4", "bitcore": "bitpay/bitcore#v0.8",
"bitcore": "bitcore",
"body-parser": "^1.2.0", "body-parser": "^1.2.0",
"express": "^4.3.1", "express": "^4.3.1",
"request": "^2.36.0" "request": "^2.36.0"

View File

@ -1,11 +1,4 @@
cd node_modules/bitcore node_modules/.bin/browserify lib/bitauth.js -s BitAuth -o dist/bitauth.browser.js
echo "Building browser bundle for bitcore..."
node browser/build -s lib/Key,lib/SINKey,lib/SIN,util/util
echo "Building browser bundle for bitauth..."
cd ../../
node_modules/.bin/browserify lib/bitauth.js -s bitauth -x buffertools -i bitcore -o dist/bitauth.browser.js
echo "Compiling bitcore and bitauth..." echo "Compiling bitcore and bitauth..."
node_modules/.bin/uglifyjs node_modules/bitcore/browser/bundle.js dist/bitauth.browser.js -b -o dist/bitauth.browser.js
echo "Minifying bundle..."
node_modules/.bin/uglifyjs dist/bitauth.browser.js -o dist/bitauth.browser.min.js node_modules/.bin/uglifyjs dist/bitauth.browser.js -o dist/bitauth.browser.min.js
echo "Done!" echo "Done!"

View File

@ -1,8 +1,8 @@
var should = require('should'); var should = require('should');
var expect = require('expect.js'); var expect = require('expect.js');
var bitauth = require('../index'); var BitAuth = require('../index');
describe('bitauth', function() { describe('new BitAuth()', function() {
var keys = null; var keys = null;
var identity = 'Tf1Jc1xSbqasm5QLwwSQc5umddx2h7mAMHX'; var identity = 'Tf1Jc1xSbqasm5QLwwSQc5umddx2h7mAMHX';
@ -16,7 +16,7 @@ describe('bitauth', function() {
describe('#generateIdentity', function() { describe('#generateIdentity', function() {
it('should generate a identity object', function(done) { it('should generate a identity object', function(done) {
keys = bitauth.generateIdentity()._keypair; keys = new BitAuth().generateIdentity();
should.exist(keys); should.exist(keys);
should.exist(keys._keypair.pubkey); should.exist(keys._keypair.pubkey);
should.exist(keys._keypair.privkey); should.exist(keys._keypair.privkey);
@ -29,16 +29,7 @@ describe('bitauth', function() {
describe('#getPublicKeyFromPrivateKey', function() { describe('#getPublicKeyFromPrivateKey', function() {
it('should properly get the public key', function(done) { it('should properly get the public key', function(done) {
bitauth.getPublicKeyFromPrivateKey(keys.priv).should.equal(keys.pub); new BitAuth().getPublicKeyFromPrivateKey(keys._keypair.privkey).toString().should.equal(keys._keypair.pubkey.toString());
done();
});
});
describe('#getIdentityFromPublicKey', function() {
it('should properly get the identity', function(done) {
bitauth.getIdentityFromPublicKey(keys.pub).should.equal(keys.identity);
done(); done();
}); });
@ -47,7 +38,7 @@ describe('bitauth', function() {
describe('#sign', function() { describe('#sign', function() {
it('should sign the string', function(done) { it('should sign the string', function(done) {
signature = bitauth.sign(contract, keys.priv); signature = new BitAuth().sign(contract, keys._keypair.privkey);
should.exist(signature); should.exist(signature);
done(); done();
}); });
@ -57,51 +48,33 @@ describe('bitauth', function() {
describe('#verifySignature', function() { describe('#verifySignature', function() {
it('should verify the signature', function(done) { it('should verify the signature', function(done) {
bitauth.verifySignature(contract, keys.pub, signature, done); new BitAuth().verifySignature(contract, keys._keypair.pubkey, signature, done);
}); });
}); });
describe('#validateIdentityTrue', function() { describe('#validateIdentity', function() {
var received;
it('should validate the identity as true', function(done) { it('should validate the identity as true', function(done) {
var valid = bitauth.validateIdentity(identity); var valid = new BitAuth().validateIdentity(identity);
should.equal(true, valid); should.equal(true, valid);
done(); done();
}); });
});
describe('#validateIdentityFalse', function() {
it('should validate the identity as false', function(done) { it('should validate the identity as false', function(done) {
var valid = bitauth.validateIdentity(identityb); var valid = new BitAuth().validateIdentity(identityb);
should.equal(false, valid); should.equal(false, valid);
done(); done();
}); });
}); });
describe('#validateIdentityCallback', function() {
var received;
var valid = bitauth.validateIdentity(identityb, function(err){
if ( err && typeof(err) == 'object' ) {
received = true;
}
});
it('should receive error callback', function() {
expect(received).to.eql(true);
});
});
describe('#encrypt', function() { describe('#encrypt', function() {
it('should encrypt the secret message', function(done) { it('should encrypt the secret message', function(done) {
enc = bitauth.encrypt(password, secret); enc = BitAuth.encrypt(password, secret);
should.exist(enc); should.exist(enc);
done(); done();
}); });
@ -111,7 +84,7 @@ describe('bitauth', function() {
describe('#decrypt', function() { describe('#decrypt', function() {
it('should decrypt the secret message', function(done) { it('should decrypt the secret message', function(done) {
var dec = bitauth.decrypt(password, enc); var dec = BitAuth.decrypt(password, enc);
should.exist(dec); should.exist(dec);
done(); done();
}); });
@ -121,7 +94,7 @@ describe('bitauth', function() {
describe('#middleware', function() { describe('#middleware', function() {
it('should expose an express middleware', function(done) { it('should expose an express middleware', function(done) {
bitauth.middleware( {} , {} , function() { BitAuth.middleware( {} , {} , function() {
done(); done();
}); });
}); });