cleanup docs and formatting
This commit is contained in:
parent
f39f5a9b61
commit
7f0d542a6a
|
@ -2,15 +2,11 @@ var elliptic = require('elliptic');
|
|||
var ecdsa = new elliptic.ec(elliptic.curves.secp256k1);
|
||||
var hashjs = require('hash.js');
|
||||
var bs58 = require('bs58');
|
||||
|
||||
var BitAuth = {};
|
||||
|
||||
/**
|
||||
* Will return a key pair and identity
|
||||
*
|
||||
* @example
|
||||
* var keys = BitAuth.generateSin();
|
||||
*
|
||||
* @returns {Object} An object with keys: created, priv, pub and sin
|
||||
*/
|
||||
BitAuth.generateSin = function() {
|
||||
|
@ -22,7 +18,7 @@ BitAuth.generateSin = function() {
|
|||
var sin = this.getSinFromPublicKey(publicKey);
|
||||
|
||||
var sinObj = {
|
||||
created: new Date().getTime(),
|
||||
created: Math.round(Date.now() / 1000),
|
||||
priv: privateKey,
|
||||
pub: publicKey,
|
||||
sin: sin
|
||||
|
@ -31,9 +27,8 @@ BitAuth.generateSin = function() {
|
|||
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
|
||||
* @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} privkey - A private key in hex
|
||||
|
@ -112,7 +107,7 @@ BitAuth.sign = function(data, privkey) {
|
|||
* 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} pubkey - The compressed public key in hex that has signed the data
|
||||
* @param {String} hexsignature - A DER signature in hex
|
||||
* @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 signature = new Buffer(hexsignature, 'hex');
|
||||
var valid = ecdsa.verify(hash, signature, pubkey);
|
||||
if ( callback )
|
||||
if (callback)
|
||||
return callback(null, valid);
|
||||
return valid;
|
||||
};
|
||||
|
@ -139,15 +134,15 @@ BitAuth.validateSin = function(sin, callback) {
|
|||
// check for non-base58 characters
|
||||
try {
|
||||
pubWithChecksum = new Buffer(bs58.decode(sin), 'hex').toString('hex');
|
||||
} catch( err ) {
|
||||
if ( callback )
|
||||
return callback( err );
|
||||
} catch(err) {
|
||||
if (callback)
|
||||
return callback(err);
|
||||
return false;
|
||||
}
|
||||
|
||||
// check the version
|
||||
if ( pubWithChecksum.slice(0, 4) != '0f02' ) {
|
||||
if ( callback )
|
||||
if (pubWithChecksum.slice(0, 4) !== '0f02') {
|
||||
if (callback)
|
||||
return callback(new Error('Invalid prefix or SIN version'));
|
||||
return false;
|
||||
}
|
||||
|
@ -162,12 +157,12 @@ BitAuth.validateSin = function(sin, callback) {
|
|||
var checksumTotal = (new hashjs.sha256()).update(hash1, 'hex').digest('hex');
|
||||
|
||||
// check the checksum
|
||||
if ( checksumTotal.slice(0,8) == checksum ) {
|
||||
if ( callback )
|
||||
if (checksumTotal.slice(0,8) === checksum) {
|
||||
if (callback)
|
||||
return callback(null);
|
||||
return true;
|
||||
} else {
|
||||
if ( callback )
|
||||
if (callback)
|
||||
return callback(new Error('Checksum does not match'));
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,10 @@
|
|||
{
|
||||
"name": "Gordon Hall",
|
||||
"email": "gordon@bitpay.com"
|
||||
},
|
||||
{
|
||||
"name": "Braydon Fuller",
|
||||
"email": "braydon@bitpay.com"
|
||||
}
|
||||
],
|
||||
"scripts": {
|
||||
|
|
Loading…
Reference in New Issue