Skip to content

Commit

Permalink
Remove all use of 'var'
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklodder committed Aug 21, 2020
1 parent a6f4a65 commit 1b237c2
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 63 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"ecmaVersion": 11
},
"rules": {
"no-var": 1
}
}
2 changes: 1 addition & 1 deletion data/networks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Bitcoin = require('bitcoinjs-lib');
const Bitcoin = require('bitcoinjs-lib');

Bitcoin.networks.litecoin = {
messagePrefix: '\x18Dogecoin Signed Message:\n',
Expand Down
10 changes: 5 additions & 5 deletions examples/dtrust.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ const client = new BlockIo({
});

// create a new address with a random label
var addressLabel = 'dtrust' + crypto.randomBytes(4).toString('hex');
const addressLabel = 'dtrust' + crypto.randomBytes(4).toString('hex');

// create the private key objects for each private key
// NOTE: in production environments you'll do this elsewhere
var privKeys = [
const privKeys = [
BlockIo.ECKey.fromPassphraseString('verysecretkey1'),
BlockIo.ECKey.fromPassphraseString('verysecretkey2'),
BlockIo.ECKey.fromPassphraseString('verysecretkey3'),
BlockIo.ECKey.fromPassphraseString('verysecretkey4')
];

var pubKeys = [];
const pubKeys = [];

// populate our pubkeys array from the keys we just generated
// pubkey entries are expected in hexadecimal format
console.log('* Collecting public keys...');
privKeys.forEach(function (key) {
var pubkey = key.pub.toString('hex');
const pubkey = key.pub.toString('hex');
console.log('>> Adding pubkey: ' + pubkey);
pubKeys.push(pubkey);
});
Expand Down Expand Up @@ -84,7 +84,7 @@ async function dTrust() {
const defaultAddress = defaultData.data.address;

// the amount available minus the network fee needed to transact it
var amountToSend = (parseFloat(availBalance) - 1).toFixed(8);
const amountToSend = (parseFloat(availBalance) - 1).toFixed(8);

console.log('* Sending ' + amountToSend + ' ' + network + ' back to ' + defaultAddress);
console.log(' Creating withdrawal request...');
Expand Down
8 changes: 4 additions & 4 deletions examples/sweep.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
*
* Contact [email protected] if you have any issues
*/
var BlockIo = require('block_io');
const BlockIo = require('block_io');

// NOTE: PIN IS NOT NEEDED!!!
var client = new BlockIo('YOUR_API_KEY');
const client = new BlockIo('YOUR_API_KEY');

var to_address = 'TARGET_ADDRESS';
var private_key = 'WIF_TO_SWEEP';
const to_address = 'TARGET_ADDRESS';
const private_key = 'WIF_TO_SWEEP';

client.sweep_from_address({
to_address: to_address,
Expand Down
39 changes: 19 additions & 20 deletions lib/block_io.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const HttpsClient = require('./httpsclient');
var qs = require('querystring');
var pkgMeta = require('../package.json');
const qs = require('querystring');
const pkgMeta = require('../package.json');

var helper = require('./helper');
var ECKey = require('./key');
const helper = require('./helper');
const ECKey = require('./key');
const networks = require('./networks');

var BlockIo = module.exports = function (config, pin, version, options) {
function BlockIo (config, pin, version, options) {

this.options = {
allowNoPin: false
Expand Down Expand Up @@ -42,7 +42,7 @@ var BlockIo = module.exports = function (config, pin, version, options) {

const client = new HttpsClient(undefined, BlockIo.USER_AGENT);
Object.defineProperty(this, 'client', {value: client, writeable: false});
};
}

// link submodules / subclasses
BlockIo.ECKey = ECKey;
Expand All @@ -55,12 +55,12 @@ BlockIo.HOST = 'block.io';


// Error messages
var ERR_PIN_INV = 'Public key mismatch. Invalid Secret PIN detected.';
var ERR_PK_EXTR = 'Could not extract privkey';
var ERR_WIF_MIS = 'Missing mandatory private_key argument';
var ERR_WIF_INV = 'Could not parse private_key as WIF';
var ERR_DEST_MIS = 'Missing mandatory to_address argument';
var ERR_UNKNOWN = 'Unknown error occured';
const ERR_PIN_INV = 'Public key mismatch. Invalid Secret PIN detected.';
const ERR_PK_EXTR = 'Could not extract privkey';
const ERR_WIF_MIS = 'Missing mandatory private_key argument';
const ERR_WIF_INV = 'Could not parse private_key as WIF';
const ERR_DEST_MIS = 'Missing mandatory to_address argument';
const ERR_UNKNOWN = 'Unknown error occured';
const ERR_NET_UNK = 'Unknown network';

// IF YOU EDIT THIS LIB, PLEASE BE A DARLING AND CHANGE THE USER AGENT :)
Expand Down Expand Up @@ -257,9 +257,9 @@ BlockIo.parseResponse = function (cb) {
return function (err, res, body) {
if (err) return cb(err);

var errOut = null;
var errMsg = ERR_UNKNOWN;
var data = null;
let errOut = null;
let errMsg = ERR_UNKNOWN;
let data = null;

try {
data = JSON.parse(body);
Expand Down Expand Up @@ -311,14 +311,11 @@ BlockIo.prototype.getNetworkParams = function (cb) {
}

BlockIo.prototype._cloneOptions = function (options) {
var self = this;
Object.keys(options).forEach(function (k) {
self.options[k] = options[k];
});
Object.keys(options).forEach( (k) => this.options[k] = options[k]);
};

BlockIo._constructMethod = function (type, callMethod) {
var fn = ['_', type].join('');
const fn = ['_', type].join('');
return function (args, cb) {

// handle overload for function (cb), without args
Expand Down Expand Up @@ -351,3 +348,5 @@ function cbPromiseHelper(promise, cb) {
return promise;
}
}

module.exports = BlockIo;
35 changes: 19 additions & 16 deletions lib/helper.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
var crypto = require('crypto');
var pkbdf2 = require('pbkdf2');
var ECKey = require('./key');
const crypto = require('crypto');
const pkbdf2 = require('pbkdf2');
const ECKey = require('./key');

var Helper = module.exports = {};
const Helper = Object.create(null);

var NULL_IV = Buffer.from('');
const NULL_IV = Buffer.from('');

Helper.encrypt = function (data, key) {

if (!Buffer.isBuffer(key)) key = Buffer.from(key, 'base64');

var cipher = crypto.createCipheriv('aes-256-ecb', key, NULL_IV);
const cipher = crypto.createCipheriv('aes-256-ecb', key, NULL_IV);

var bufs = [];
const bufs = [];
bufs.push(cipher.update(data, 'utf-8'));
bufs.push(cipher.final());

var ctLength = bufs[0].length + bufs[1].length;
var ciphertext = Buffer.concat(bufs, ctLength);
const ctLength = bufs[0].length + bufs[1].length;
const ciphertext = Buffer.concat(bufs, ctLength);

return ciphertext.toString('base64');
};
Expand All @@ -26,14 +26,14 @@ Helper.decrypt = function (ciphertext, key) {

if (!Buffer.isBuffer(key)) key = Buffer.from(key, 'base64');

var cipher = crypto.createDecipheriv('aes-256-ecb', key, NULL_IV);
const cipher = crypto.createDecipheriv('aes-256-ecb', key, NULL_IV);

var bufs = [];
const bufs = [];
bufs.push(cipher.update(ciphertext, 'base64'));
bufs.push(cipher.final());

var tLength = bufs[0].length + bufs[1].length;
var text = Buffer.concat(bufs, tLength);
const tLength = bufs[0].length + bufs[1].length;
const text = Buffer.concat(bufs, tLength);

return text.toString('utf-8');
};
Expand All @@ -42,14 +42,15 @@ Helper.pinToKey = function (pin, salt, iterations) {
if (!salt) salt = '';
if (!iterations) iterations = 2048;

var buf = pkbdf2.pbkdf2Sync(pin, salt, iterations / 2, 16, 'sha256');
buf = pkbdf2.pbkdf2Sync(buf.toString('hex'), salt, iterations / 2, 32, 'sha256');
let buf;
buf = pkbdf2.pbkdf2Sync(pin, salt, iterations / 2, 16, 'sha256');
buf = pkbdf2.pbkdf2Sync(buf.toString('hex'), salt, iterations / 2, 32, 'sha256');

return buf.toString('base64');
};

Helper.extractKey = function (encrypted_data, b64_enc_key) {
var decrypted = this.decrypt(encrypted_data, b64_enc_key);
const decrypted = this.decrypt(encrypted_data, b64_enc_key);
return ECKey.fromPassphrase(decrypted);
};

Expand All @@ -69,3 +70,5 @@ Helper.signInputs = function (privkey, inputs) {

return inputs;
};

module.exports = Helper;
4 changes: 2 additions & 2 deletions lib/httpsclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const DEFAULT_REQUEST_OPTIONS = {
}

function HttpsClient(options, userAgent, dhparam) {
var agentOpts = cloneOrReplace(DEFAULT_AGENT_OPTIONS, options || {});
const agentOpts = cloneOrReplace(DEFAULT_AGENT_OPTIONS, options || {});
setProp(this, 'agent', new https.Agent(agentOpts));
setProp(this, 'userAgent', userAgent);
setProp(this, 'dhparam', dhparam);
Expand Down Expand Up @@ -96,7 +96,7 @@ function cloneOrReplace(a, b) {
Object.keys(a).forEach(k => {
if (['__proto__', 'constructor'].indexOf(k) !== -1) return;

var value = Object.prototype.hasOwnProperty.call(b, k) ? b[k] : a[k];
const value = Object.prototype.hasOwnProperty.call(b, k) ? b[k] : a[k];
setProp(out, k, value);
});

Expand Down
4 changes: 2 additions & 2 deletions lib/key.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var crypto = require('crypto');
var Bitcoin = require('bitcoinjs-lib');
const crypto = require('crypto');
const Bitcoin = require('bitcoinjs-lib');

function Key (d, compressed) {
const pair = Buffer.isBuffer(d) ?
Expand Down
6 changes: 3 additions & 3 deletions lib/networks.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var Bitcoin = require('bitcoinjs-lib');
const Bitcoin = require('bitcoinjs-lib');

// make sure network data is extended
require('../data/networks');

var networks = module.exports = {};
var MAPPING = networks.MAPPING = {
const networks = module.exports = {};
const MAPPING = networks.MAPPING = {
'BTC': 'bitcoin',
'DOGE': 'dogecoin',
'LTC': 'litecoin',
Expand Down
10 changes: 6 additions & 4 deletions test/helpers/cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var c = {};
var l = {};
const c = {};
const l = {};

var cache = module.exports = function (k, v) {
const cache = function (k, v) {
if (v) {
c[k] = v;
if (l[k]) l[k].forEach(fn => fn(v));
Expand All @@ -13,10 +13,12 @@ cache.on = function (k, fn) { if (!l[k]) l[k] = []; l[k].push(fn); if (c[k]) fn(

cache.require = function (keys, fn) {

var _wrapper = function () {
const _wrapper = function () {
if (keys.some(k => !Object.prototype.hasOwnProperty.call(c, k))) return;
fn(keys.map(k => c[k]));
}

keys.forEach(k => cache.on(k, _wrapper));
}

module.exports = cache;
4 changes: 2 additions & 2 deletions test/helpers/generic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var cache = require('./cache');
const cache = require('./cache');

var loggedEnvError = false;
let loggedEnvError = false;

module.exports = {
FEES: {BTC: 0.00001, BTCTEST: 0.00001, DOGE: 1, DOGETEST: 1, LTC: 0.0001, LTCTEST: 0.0001},
Expand Down
2 changes: 1 addition & 1 deletion test/integration/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ CT.create(test, client).title('Get Addresses')
.check((t, args) => t.ok(args[1].data.addresses[0], 'must return at least one address'))
.postProcess(args => {
cache('minFee', helper.FEES[args[1].data.network]);
var hasBalance = args[1].data.addresses.some(function (addr) {
const hasBalance = args[1].data.addresses.some(function (addr) {
if (parseFloat(addr.available_balance, 10) > (20 * cache('minFee'))) {
cache('fromAddress', addr.address);
cache('fromLabel', addr.label);
Expand Down
6 changes: 3 additions & 3 deletions test/integration/dtrust.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const cache = require('../helpers/cache');
const helper = require('../helpers/generic');
const CT = require('../helpers/clienttest');

var BlockIo = require('../../lib/block_io');
var Bitcoin = require('bitcoinjs-lib');
const BlockIo = require('../../lib/block_io');
const Bitcoin = require('bitcoinjs-lib');

if (!helper.checkEnv()) process.exit(1);

Expand Down Expand Up @@ -97,7 +97,7 @@ CT.create(test, client).title('Get DTrust Addresses')
'must return at least one address'))
.postProcess(args => {
cache('minFeeDTrust', helper.FEES[args[1].data.network]);
var hasBalance = args[1].data.addresses.some(function (addr) {
const hasBalance = args[1].data.addresses.some(function (addr) {
if (parseFloat(addr.available_balance, 10) > (20 * cache('minFeeDTrust'))) {
cache('fromDTrustAddress', addr.address);
cache('fromDTrustLabel', addr.label);
Expand Down

0 comments on commit 1b237c2

Please sign in to comment.