bitauth/lib/bitauth.js

178 lines
4.6 KiB
JavaScript
Raw Normal View History

2014-11-03 10:44:03 -08:00
var elliptic = require('elliptic');
var ecdsa = new elliptic.ec(elliptic.curves.secp256k1);
var hashjs = require('hash.js');
var bs58 = require('bs58');
2014-05-27 09:04:57 -07:00
var BitAuth = {};
2014-05-27 09:04:57 -07:00
/**
* Will return a key pair and identity
*
* @example
* var keys = BitAuth.generateSin();
*
* @returns {Object} An object with keys: created, priv, pub and sin
*/
2014-05-27 09:04:57 -07:00
BitAuth.generateSin = function() {
var keys = ecdsa.genKeyPair();
var privateKey = keys.getPrivate('hex');
2014-10-29 16:09:53 -07:00
var publicKey = this.getPublicKeyFromPrivateKey(privateKey);
var sin = this.getSinFromPublicKey(publicKey);
var sinObj = {
created: new Date().getTime(),
2014-10-29 16:09:53 -07:00
priv: privateKey,
pub: publicKey,
sin: sin
2014-11-03 10:44:03 -08:00
};
return sinObj;
2014-05-27 09:04:57 -07:00
};
/**
* Will return an public key from a private key
*
* @param {String} A private key in hex
* @returns {String} A compressed public key in hex
*/
2014-05-27 09:04:57 -07:00
BitAuth.getPublicKeyFromPrivateKey = function(privkey) {
2014-06-25 10:56:24 -07:00
var keys = ecdsa.keyPair(privkey, 'hex');
2014-06-25 10:56:24 -07:00
// compressed public key
var pubKey = keys.getPublic();
var xbuf = new Buffer(pubKey.x.toString('hex', 64), 'hex');
var ybuf = new Buffer(pubKey.y.toString('hex', 64), 'hex');
2014-11-03 10:44:03 -08:00
var pub;
2014-10-29 16:09:53 -07:00
if (ybuf[ybuf.length-1] % 2) { //odd
2014-11-03 10:44:03 -08:00
pub = Buffer.concat([new Buffer([3]), xbuf]);
} else { //even
pub = Buffer.concat([new Buffer([2]), xbuf]);
}
var hexPubKey = pub.toString('hex');
return hexPubKey;
2014-05-27 09:04:57 -07:00
};
/**
2014-11-03 10:44:03 -08:00
* Will return a SIN from a compressed public key
*
* @param {String} A public key in hex
* @returns {String} A SIN identity
*/
2014-05-27 09:04:57 -07:00
BitAuth.getSinFromPublicKey = function(pubkey) {
// sha256 hash the pubkey
var pubHash = (new hashjs.sha256()).update(pubkey, 'hex').digest('hex');
// get the ripemd160 hash of the pubkey
var pubRipe = (new hashjs.ripemd160()).update(pubHash, 'hex').digest('hex');
// add the version
var pubPrefixed = '0f02'+pubRipe;
// two rounds of hashing to generate the checksum
var hash1 = (new hashjs.sha256()).update(pubPrefixed, 'hex').digest('hex');
var checksumTotal = (new hashjs.sha256()).update(hash1, 'hex').digest('hex');
// slice the hash to arrive at the checksum
var checksum = checksumTotal.slice(0,8);
// add the checksum to the ripemd160 pubkey
var pubWithChecksum = pubPrefixed + checksum;
// encode into base58
var sin = bs58.encode(new Buffer(pubWithChecksum, 'hex'));
return sin;
2014-11-03 10:44:03 -08:00
};
2014-05-27 09:04:57 -07:00
/**
* Will return a signature from a private key
*
* @param {String} data - A string of data to be signed
2014-11-03 10:44:03 -08:00
* @param {String} privkey - A private key in hex
* @returns {String} signature - A DER signature in hex
*/
2014-05-27 09:04:57 -07:00
BitAuth.sign = function(data, privkey) {
var hash = (new hashjs.sha256()).update(data).digest('hex');
var signature = ecdsa.sign(hash, privkey);
var hexsignature = signature.toDER('hex');
return hexsignature;
2014-05-27 09:04:57 -07:00
};
/**
* Will verify a signature
*
* @param {String} data - A string of data that has been signed
* @param {String} pubkey - The public identity that has signed the data
* @param {String} hexsignature - A DER signature in hex
* @returns {Function|Boolean} - If the signature is valid
*/
BitAuth.verifySignature = function(data, pubkey, hexsignature, callback) {
var hash = (new hashjs.sha256()).update(data).digest('hex');
var signature = new Buffer(hexsignature, 'hex');
var valid = ecdsa.verify(hash, signature, pubkey);
2014-11-03 10:44:03 -08:00
if ( callback )
return callback(null, valid);
2014-11-03 10:44:03 -08:00
return valid;
2014-05-27 09:04:57 -07:00
};
/**
* Will verify that a SIN is valid
*
* @param {String} sin - A SIN identity
* @returns {Function|Boolean} - If the SIN identity is valid
*/
2014-07-16 10:46:49 -07:00
BitAuth.validateSin = function(sin, callback) {
2014-11-03 10:44:03 -08:00
var pubWithChecksum;
// check for non-base58 characters
try {
2014-11-03 10:44:03 -08:00
pubWithChecksum = new Buffer(bs58.decode(sin), 'hex').toString('hex');
} catch( err ) {
2014-11-03 10:44:03 -08:00
if ( callback )
return callback( err );
2014-11-03 10:44:03 -08:00
return false;
}
// check the version
if ( pubWithChecksum.slice(0, 4) != '0f02' ) {
2014-11-03 10:44:03 -08:00
if ( callback )
return callback(new Error('Invalid prefix or SIN version'));
2014-07-16 10:46:49 -07:00
return false;
}
// get the checksum
2014-11-03 10:44:03 -08:00
var checksum = pubWithChecksum.slice(pubWithChecksum.length-8,
pubWithChecksum.length);
var pubPrefixed = pubWithChecksum.slice(0, pubWithChecksum.length-8);
// two rounds of hashing to generate the checksum
var hash1 = (new hashjs.sha256()).update(pubPrefixed, 'hex').digest('hex');
var checksumTotal = (new hashjs.sha256()).update(hash1, 'hex').digest('hex');
// check the checksum
if ( checksumTotal.slice(0,8) == checksum ) {
2014-11-03 10:44:03 -08:00
if ( callback )
return callback(null);
2014-11-03 10:44:03 -08:00
return true;
} else {
2014-11-03 10:44:03 -08:00
if ( callback )
return callback(new Error('Checksum does not match'));
return false;
}
2014-07-16 10:46:49 -07:00
};
2014-05-27 09:04:57 -07:00
module.exports = BitAuth;