Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ $ npm test
| transaction/unspentoutput.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| unit.js | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| uri.js | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| util/buffer.js | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| util/js.js | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| util/preconditions.js | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| util/buffer.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| util/js.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |
| util/preconditions.js | ![done](https://i.imgur.com/RXSkZTD.png "Done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") | ![not done](https://i.imgur.com/MleS2Jt.png "Not done") |



Expand Down
1 change: 1 addition & 0 deletions scripts/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const toTest = [
'src/block/',
'src/mnemonic/',
'src/transaction/',
'src/util/',
'src/crypto/ecdsa.js',
'src/crypto/random.js',
'src/crypto/signature.js',
Expand Down
84 changes: 41 additions & 43 deletions src/util/buffer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';
const buffer = require('buffer');
const assert = require('assert');

var buffer = require('buffer');
var assert = require('assert');

var js = require('./js');
var $ = require('./preconditions');
const js = require('./js');
const $ = require('./preconditions');

function equals(a, b) {
if (a.length !== b.length) {
return false;
}
var length = a.length;
for (var i = 0; i < length; i++) {
const { length } = a;
for (let i = 0; i < length; i += 1) {
if (a[i] !== b[i]) {
return false;
}
Expand All @@ -23,18 +21,18 @@ module.exports = {
/**
* Fill a buffer with a value.
*
* @param {Buffer} buffer
* @param {Buffer} buff
* @param {number} value
* @return {Buffer}
*/
fill: function fill(buffer, value) {
$.checkArgumentType(buffer, 'Buffer', 'buffer');
fill: function fill(buff, value) {
$.checkArgumentType(buff, 'Buffer', 'buffer');
$.checkArgumentType(value, 'number', 'value');
var length = buffer.length;
for (var i = 0; i < length; i++) {
buffer[i] = value;
const { length } = buff;
for (let i = 0; i < length; i += 1) {
buff[i] = value;
}
return buffer;
return buff;
},

/**
Expand All @@ -43,10 +41,10 @@ module.exports = {
* @param {Buffer} original
* @return {Buffer}
*/
copy: function(original) {
var buffer = new Buffer(original.length);
original.copy(buffer);
return buffer;
copy(original) {
const buff = Buffer.alloc(original.length);
original.copy(buff);
return buff;
},

/**
Expand All @@ -68,8 +66,8 @@ module.exports = {
*/
emptyBuffer: function emptyBuffer(bytes) {
$.checkArgumentType(bytes, 'number', 'bytes');
var result = new buffer.Buffer(bytes);
for (var i = 0; i < bytes; i++) {
const result = Buffer.alloc(bytes);
for (let i = 0; i < bytes; i += 1) {
result.write('\0', i);
}
return result;
Expand All @@ -82,7 +80,7 @@ module.exports = {
*/
concat: buffer.Buffer.concat,

equals: equals,
equals,
equal: equals,

/**
Expand All @@ -93,7 +91,7 @@ module.exports = {
*/
integerAsSingleByteBuffer: function integerAsSingleByteBuffer(integer) {
$.checkArgumentType(integer, 'number', 'integer');
return new buffer.Buffer([integer & 0xff]);
return Buffer.from([integer & 0xff]);
},

/**
Expand All @@ -104,46 +102,46 @@ module.exports = {
*/
integerAsBuffer: function integerAsBuffer(integer) {
$.checkArgumentType(integer, 'number', 'integer');
var bytes = [];
const bytes = [];
bytes.push((integer >> 24) & 0xff);
bytes.push((integer >> 16) & 0xff);
bytes.push((integer >> 8) & 0xff);
bytes.push(integer & 0xff);
return new Buffer(bytes);
return Buffer.from(bytes);
},

/**
* Transform the first 4 values of a Buffer into a number, in little endian encoding
*
* @param {Buffer} buffer
* @param {Buffer} buff
* @return {number}
*/
integerFromBuffer: function integerFromBuffer(buffer) {
$.checkArgumentType(buffer, 'Buffer', 'buffer');
return buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3];
integerFromBuffer: function integerFromBuffer(buff) {
$.checkArgumentType(buff, 'Buffer', 'buffer');
return (buff[0] << 24) | (buff[1] << 16) | (buff[2] << 8) | (buff[3]);
},

/**
* Transforms the first byte of an array into a number ranging from -128 to 127
* @param {Buffer} buffer
* @param {Buffer} buff
* @return {number}
*/
integerFromSingleByteBuffer: function integerFromBuffer(buffer) {
$.checkArgumentType(buffer, 'Buffer', 'buffer');
return buffer[0];
integerFromSingleByteBuffer: function integerFromBuffer(buff) {
$.checkArgumentType(buff, 'Buffer', 'buffer');
return buff[0];
},

/**
* Transforms a buffer into a string with a number in hexa representation
*
* Shorthand for <tt>buffer.toString('hex')</tt>
*
* @param {Buffer} buffer
* @param {Buffer} buff
* @return {string}
*/
bufferToHex: function bufferToHex(buffer) {
$.checkArgumentType(buffer, 'Buffer', 'buffer');
return buffer.toString('hex');
bufferToHex: function bufferToHex(buff) {
$.checkArgumentType(buff, 'Buffer', 'buffer');
return buff.toString('hex');
},

/**
Expand All @@ -152,8 +150,8 @@ module.exports = {
* @return {Buffer}
*/
reverse: function reverse(param) {
var ret = new buffer.Buffer(param.length);
for (var i = 0; i < param.length; i++) {
const ret = Buffer.alloc(param.length);
for (let i = 0; i < param.length; i += 1) {
ret[i] = param[param.length - i - 1];
}
return ret;
Expand All @@ -169,9 +167,9 @@ module.exports = {
*/
hexToBuffer: function hexToBuffer(string) {
assert(js.isHexa(string));
return new buffer.Buffer(string, 'hex');
}
return Buffer.from(string, 'hex');
},
};

module.exports.NULL_HASH = module.exports.fill(new Buffer(32), 0);
module.exports.EMPTY_BUFFER = new Buffer(0);
module.exports.NULL_HASH = module.exports.fill(Buffer.alloc(32), 0);
module.exports.EMPTY_BUFFER = Buffer.alloc(0);
28 changes: 13 additions & 15 deletions src/util/js.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

var _ = require('lodash');
const _ = require('lodash');

/**
* Determines whether a string contains only hexadecimal values
Expand All @@ -9,7 +7,7 @@ var _ = require('lodash');
* @param {string} value
* @return {boolean} true if the string is the hexa representation of a number
*/
var isHexa = function isHexa(value) {
const isHexa = function isHexa(value) {
if (!_.isString(value)) {
return false;
}
Expand All @@ -28,7 +26,7 @@ module.exports = {
* @return {Object|boolean} false if the argument is not a JSON string.
*/
isValidJSON: function isValidJSON(arg) {
var parsed;
let parsed;
if (!_.isString(arg)) {
return false;
}
Expand All @@ -37,18 +35,18 @@ module.exports = {
} catch (e) {
return false;
}
if (typeof(parsed) === 'object') {
if (typeof (parsed) === 'object') {
return true;
}
return false;
},
isHexa: isHexa,
isHexa,
isHexaString: isHexa,

/**
* Clone an array
*/
cloneArray: function(array) {
cloneArray(array) {
return [].concat(array);
},

Expand All @@ -60,11 +58,11 @@ module.exports = {
* @return {Object} The target object
*/
defineImmutable: function defineImmutable(target, values) {
Object.keys(values).forEach(function(key){
Object.keys(values).forEach((key) => {
Object.defineProperty(target, key, {
configurable: false,
enumerable: true,
value: values[key]
value: values[key],
});
});
return target;
Expand All @@ -76,9 +74,9 @@ module.exports = {
* @return {Boolean}
*/
isNaturalNumber: function isNaturalNumber(value) {
return typeof value === 'number' &&
isFinite(value) &&
Math.floor(value) === value &&
value >= 0;
}
return typeof value === 'number'
&& Number.isFinite(value)
&& Math.floor(value) === value
&& value >= 0;
},
};
24 changes: 10 additions & 14 deletions src/util/preconditions.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
'use strict';

var errors = require('../errors');
var _ = require('lodash');
const _ = require('lodash');
const buffer = require('buffer');
const errors = require('../errors');

module.exports = {
checkState: function(condition, message) {
checkState(condition, message) {
if (!condition) {
throw new errors.InvalidState(message);
}
},
checkArgument: function(condition, argumentName, message, docsPath) {
checkArgument(condition, argumentName, message, docsPath) {
if (!condition) {
throw new errors.InvalidArgument(argumentName, message, docsPath);
}
},
checkArgumentType: function(argument, type, argumentName) {
checkArgumentType(argument, type, argumentName) {
argumentName = argumentName || '(unknown name)';
if (_.isString(type)) {
if (type === 'Buffer') {
var buffer = require('buffer'); // './buffer' fails on cordova & RN
if (!buffer.Buffer.isBuffer(argument)) {
throw new errors.InvalidArgumentType(argument, type, argumentName);
}
} else if (typeof argument !== type) {
} else if (typeof argument !== type) { // eslint-disable-line valid-typeof
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probaby try to refactor this? It is used allo over the place so it will break everything.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe we can create a new pull request so that we fix all instances of this issue at once. We can talk on Monday

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up #70

throw new errors.InvalidArgumentType(argument, type, argumentName);
}
} else {
if (!(argument instanceof type)) {
throw new errors.InvalidArgumentType(argument, type.name, argumentName);
}
} else if (!(argument instanceof type)) {
throw new errors.InvalidArgumentType(argument, type.name, argumentName);
}
}
},
};