cleanup docs and formatting

This commit is contained in:
Braydon Fuller 2014-11-04 10:53:37 -05:00
parent f39f5a9b61
commit 7f0d542a6a
2 changed files with 18 additions and 19 deletions

View File

@ -2,15 +2,11 @@ var elliptic = require('elliptic');
var ecdsa = new elliptic.ec(elliptic.curves.secp256k1); var ecdsa = new elliptic.ec(elliptic.curves.secp256k1);
var hashjs = require('hash.js'); var hashjs = require('hash.js');
var bs58 = require('bs58'); var bs58 = require('bs58');
var BitAuth = {};
var BitAuth = {};
/** /**
* Will return a key pair and identity * Will return a key pair and identity
* *
* @example
* var keys = BitAuth.generateSin();
*
* @returns {Object} An object with keys: created, priv, pub and sin * @returns {Object} An object with keys: created, priv, pub and sin
*/ */
BitAuth.generateSin = function() { BitAuth.generateSin = function() {
@ -22,7 +18,7 @@ BitAuth.generateSin = function() {
var sin = this.getSinFromPublicKey(publicKey); var sin = this.getSinFromPublicKey(publicKey);
var sinObj = { var sinObj = {
created: new Date().getTime(), created: Math.round(Date.now() / 1000),
priv: privateKey, priv: privateKey,
pub: publicKey, pub: publicKey,
sin: sin sin: sin
@ -31,9 +27,8 @@ BitAuth.generateSin = function() {
return sinObj; return sinObj;
}; };
/** /**
* Will return an public key from a private key * Will return a public key from a private key
* *
* @param {String} A private key in hex * @param {String} A private key in hex
* @returns {String} A compressed public key in hex * @returns {String} A compressed public key in hex
@ -95,7 +90,7 @@ BitAuth.getSinFromPublicKey = function(pubkey) {
}; };
/** /**
* Will return a signature from a private key * Will sign a string of data with a private key
* *
* @param {String} data - A string of data to be signed * @param {String} data - A string of data to be signed
* @param {String} privkey - A private key in hex * @param {String} privkey - A private key in hex
@ -112,7 +107,7 @@ BitAuth.sign = function(data, privkey) {
* Will verify a signature * Will verify a signature
* *
* @param {String} data - A string of data that has been signed * @param {String} data - A string of data that has been signed
* @param {String} pubkey - The public identity that has signed the data * @param {String} pubkey - The compressed public key in hex that has signed the data
* @param {String} hexsignature - A DER signature in hex * @param {String} hexsignature - A DER signature in hex
* @returns {Function|Boolean} - If the signature is valid * @returns {Function|Boolean} - If the signature is valid
*/ */
@ -120,7 +115,7 @@ BitAuth.verifySignature = function(data, pubkey, hexsignature, callback) {
var hash = (new hashjs.sha256()).update(data).digest('hex'); var hash = (new hashjs.sha256()).update(data).digest('hex');
var signature = new Buffer(hexsignature, 'hex'); var signature = new Buffer(hexsignature, 'hex');
var valid = ecdsa.verify(hash, signature, pubkey); var valid = ecdsa.verify(hash, signature, pubkey);
if ( callback ) if (callback)
return callback(null, valid); return callback(null, valid);
return valid; return valid;
}; };
@ -139,15 +134,15 @@ BitAuth.validateSin = function(sin, callback) {
// check for non-base58 characters // check for non-base58 characters
try { try {
pubWithChecksum = new Buffer(bs58.decode(sin), 'hex').toString('hex'); pubWithChecksum = new Buffer(bs58.decode(sin), 'hex').toString('hex');
} catch( err ) { } catch(err) {
if ( callback ) if (callback)
return callback( err ); return callback(err);
return false; return false;
} }
// check the version // check the version
if ( pubWithChecksum.slice(0, 4) != '0f02' ) { if (pubWithChecksum.slice(0, 4) !== '0f02') {
if ( callback ) if (callback)
return callback(new Error('Invalid prefix or SIN version')); return callback(new Error('Invalid prefix or SIN version'));
return false; return false;
} }
@ -162,12 +157,12 @@ BitAuth.validateSin = function(sin, callback) {
var checksumTotal = (new hashjs.sha256()).update(hash1, 'hex').digest('hex'); var checksumTotal = (new hashjs.sha256()).update(hash1, 'hex').digest('hex');
// check the checksum // check the checksum
if ( checksumTotal.slice(0,8) == checksum ) { if (checksumTotal.slice(0,8) === checksum) {
if ( callback ) if (callback)
return callback(null); return callback(null);
return true; return true;
} else { } else {
if ( callback ) if (callback)
return callback(new Error('Checksum does not match')); return callback(new Error('Checksum does not match'));
return false; return false;
} }

View File

@ -17,6 +17,10 @@
{ {
"name": "Gordon Hall", "name": "Gordon Hall",
"email": "gordon@bitpay.com" "email": "gordon@bitpay.com"
},
{
"name": "Braydon Fuller",
"email": "braydon@bitpay.com"
} }
], ],
"scripts": { "scripts": {