From 74cecb7fdfc0ff2a9889186eff0923a9526ccf16 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 29 Oct 2014 19:09:53 -0400 Subject: [PATCH] add padding of leading zeros --- lib/bitauth.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/bitauth.js b/lib/bitauth.js index fb06aa2..83be89a 100644 --- a/lib/bitauth.js +++ b/lib/bitauth.js @@ -5,6 +5,18 @@ var bs58 = require('bs58'); var BitAuth = {}; +/** + * Will add leading zeros to hex string + * + * @returns {String} A hex string + */ +BitAuth._addPaddingToHex = function(hex){ + while( hex.length < 64 ){ + hex = '0'+hex; + } + return hex; +} + /** * Will return a key pair and identity * @@ -17,12 +29,13 @@ BitAuth.generateSin = function() { var keys = ecdsa.genKeyPair(); - var publicKey = this.getPublicKeyFromPrivateKey(keys.getPrivate('hex')); + var privateKey = this._addPaddingToHex(keys.getPrivate('hex')); + var publicKey = this.getPublicKeyFromPrivateKey(privateKey); var sin = this.getSinFromPublicKey(publicKey); var sinObj = { created: new Date().getTime(), - priv: keys.getPrivate('hex'), + priv: privateKey, pub: publicKey, sin: sin } @@ -43,8 +56,9 @@ BitAuth.getPublicKeyFromPrivateKey = function(privkey) { // compressed public key var pubKey = keys.getPublic(); - var xbuf = new Buffer(pubKey.x.toString('hex'), 'hex'); - var ybuf = new Buffer(pubKey.y.toString('hex'), 'hex'); + var xbuf = new Buffer(this._addPaddingToHex(pubKey.x.toString('hex')), 'hex'); + var ybuf = new Buffer(this._addPaddingToHex(pubKey.y.toString('hex')), 'hex'); + if (ybuf[ybuf.length-1] % 2) { //odd var pub = Buffer.concat([new Buffer([3]), xbuf]); }