Using let and const properly, updated number to uint function, general cleaning

pull/13/head
Beaudan 7 years ago
parent ee57c698d7
commit dfa8b59bc3

@ -52,13 +52,16 @@ function initialize({ url }) {
}; };
async function sendMessage(pub_key, data, ttl) { async function sendMessage(pub_key, data, ttl) {
var timestamp = Math.floor(Date.now() / 1000); const timestamp = Math.floor(Date.now() / 1000);
// Nonce is returned as a base64 string to include in header // Nonce is returned as a base64 string to include in header
nonce = await getPoWNonce(timestamp, ttl, pub_key, data).catch((err) => { let nonce;
try {
nonce = await getPoWNonce(timestamp, ttl, pub_key, data);
} catch(err) {
// Something went horribly wrong // Something went horribly wrong
// TODO: Handle gracefully // TODO: Handle gracefully
console.log("Error computing PoW"); console.log("Error computing PoW");
}); };
const options = { const options = {
url: `${url}/send_message`, url: `${url}/send_message`,

@ -1,9 +1,9 @@
const hash = require('js-sha512'); const hash = require('js-sha512');
const bb = require('bytebuffer'); const bb = require('bytebuffer');
// Increment Uint8Array by 1 with carrying // Increment Uint8Array nonce by 1 with carrying
function incrementNonce(nonce) { function incrementNonce(nonce) {
idx = nonce.length - 1; let idx = nonce.length - 1;
nonce[idx] += 1; nonce[idx] += 1;
// Nonce will just reset to 0 if all values are 255 causing infinite loop, should never happen // Nonce will just reset to 0 if all values are 255 causing infinite loop, should never happen
while (nonce[idx] == 0 && idx >= 0) { while (nonce[idx] == 0 && idx >= 0) {
@ -12,52 +12,49 @@ function incrementNonce(nonce) {
return nonce; return nonce;
} }
// Convert a Uint8Array to a base64 string. Copied from stackoverflow // Convert a Uint8Array to a base64 string
function bufferToBase64(buf) { function bufferToBase64(buf) {
var binstr = Array.prototype.map.call(buf, function (ch) { let binstr = Array.prototype.map.call(buf, function (ch) {
return String.fromCharCode(ch); return String.fromCharCode(ch);
}).join(''); }).join('');
return bb.btoa(binstr); return bb.btoa(binstr);
} }
// Convert number to Uint8Array // Convert javascript number to Uint8Array of length 8
function numberToUintArr(numberVal) { function numberToUintArr(numberVal) {
// TODO: Make this not hardcoded for arrays of length 8? let arr = new Uint8Array(8);
var arr = new Uint8Array(8); for (let idx = 7; idx >= 0; idx--) {
for (var idx = 7; idx >= 0; idx--) { let n = 8 - (idx + 1);
// Get the lowest 8 bits from the number // 256 ** n is the value of one bit in arr[idx], modulus to carry over
var byte = numberVal & 0xff; arr[idx] = (numberVal / 256**n) % 256;
arr[idx] = byte;
// Essentially bitshift
numberVal = (numberVal - byte) / 256 ;
} }
return arr; return arr;
} }
// Return nonce that hashes together with payload lower than the target // Return nonce that hashes together with payload lower than the target
function calcPoW(timestamp, ttl, pub_key, data) { function calcPoW(timestamp, ttl, pub_key, data) {
var leadingString = timestamp.toString() + ttl.toString() + pub_key; const leadingString = timestamp.toString() + ttl.toString() + pub_key;
var leadingArray = new Uint8Array(bb.wrap(leadingString, 'binary').toArrayBuffer()); const leadingArray = new Uint8Array(bb.wrap(leadingString, 'binary').toArrayBuffer());
// Payload constructed from concatenating timestamp, ttl and pubkey strings, converting to Uint8Array // Payload constructed from concatenating timestamp, ttl and pubkey strings, converting to Uint8Array
// and then appending to the message data array // and then appending to the message data array
var payload = leadingArray + data; const payload = leadingArray + data;
var nonceLen = 8; const nonceLen = 8;
// Modify this value for difficulty scaling // Modify this value for difficulty scaling
// TODO: Have more informed reason for setting this to 100 // TODO: Have more informed reason for setting this to 100
var nonceTrialsPerByte = 100; const nonceTrialsPerByte = 1000;
var nonce = new Uint8Array(nonceLen); let nonce = new Uint8Array(nonceLen);
var trialValue = numberToUintArr(Number.MAX_SAFE_INTEGER); let trialValue = numberToUintArr(Number.MAX_SAFE_INTEGER);
// Target is converter to Uint8Array for simple comparison with trialValue // Target is converter to Uint8Array for simple comparison with trialValue
var target = numberToUintArr(Math.pow(2, 64) / ( const target = numberToUintArr(Math.floor(Math.pow(2, 64) / (
nonceTrialsPerByte * ( nonceTrialsPerByte * (
payload.length + nonceLen + ( payload.length + nonceLen + (
(ttl * ( payload.length + nonceLen )) (ttl * ( payload.length + nonceLen ))
/ Math.pow(2, 16) / Math.pow(2, 16)
) )
) )
)); )));
initialHash = new Uint8Array(bb.wrap(hash(payload), 'hex').toArrayBuffer()); const initialHash = new Uint8Array(bb.wrap(hash(payload), 'hex').toArrayBuffer());
while (target < trialValue) { while (trialValue > target) {
nonce = incrementNonce(nonce); nonce = incrementNonce(nonce);
trialValue = (new Uint8Array(bb.wrap(hash(nonce + initialHash), 'hex').toArrayBuffer())).slice(0, 8); trialValue = (new Uint8Array(bb.wrap(hash(nonce + initialHash), 'hex').toArrayBuffer())).slice(0, 8);
} }

Loading…
Cancel
Save