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
var BitAuth = require('./lib/bitauth');
var bitauth = new BitAuth();
// add node-specific encrypt/decrypt
bitauth.encrypt = require('./lib/encrypt');
bitauth.decrypt = require('./lib/decrypt');
bitauth.middleware = require('./lib/middleware/bitauth');
BitAuth.encrypt = require('./lib/encrypt');
BitAuth.decrypt = require('./lib/decrypt');
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 ) {
var self = this;
if (!keypair) var keypair = new Keypair().fromRandom();
self._keypair = keypair;
@ -25,42 +25,30 @@ BitAuth.prototype.generateIdentity = function( keypair ) {
BitAuth.prototype.sign = function(data, privkey) {
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;
};
BitAuth.prototype.getIdentityFromPublicKey = function( pubkey ) {
var identity = new Identity( pubkey );
return identity.toString();
return identity;
};
BitAuth.prototype.getPublicKeyFromPrivateKey = function( privkey ) {
var keypair = Keypair().fromString( JSON.stringify({ privkey : privkey }) );
return keypair.pubkey.toString();
var keypair = new Keypair();
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, identity, signature, callback) {
BitAuth.prototype.verifySignature = function(data, pubkey, signature, callback) {
var self = this;
console.log('verifySignature', identity , signature );
var pubkey = self.getPublicKeyFromIdentity( identity );
console.log('pubkey', pubkey );
var messageBuffer = new Buffer( data );
var signature = Message.sign( messageBuffer , identity._keypair );
var keypair = new Keypair();
console.log('verifySig', identity);
keypair.fromString( JSON.stringify({ pubkey: pubkey }) )
var identity = new Identity().fromPubkey( keypair.pubkey );
try {
Message.verify( messageBuffer , signature , identity );
@ -70,14 +58,12 @@ BitAuth.prototype.verifySignature = function(data, identity, signature, callback
}
};
BitAuth.prototype.validateIdentity = function( identity , callback ) {
var s = new Identity( identity );
BitAuth.prototype.validateIdentity = function( identity ) {
try {
s.validate();
callback();
new Identity( identity ).validate();
return true;
} 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');
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');
module.exports = function encrypt(password, str) {
@ -9,6 +9,6 @@ module.exports = function encrypt(password, str) {
a.copy(buf, 0);
b.copy(buf, a.length);
return base58.encode(buf);
};

View File

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

View File

@ -1,11 +1,4 @@
cd node_modules/bitcore
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
node_modules/.bin/browserify lib/bitauth.js -s BitAuth -o dist/bitauth.browser.js
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
echo "Done!"

View File

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