Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix randomHex #3085

Merged
merged 4 commits into from
Oct 8, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Released with 1.0.0-beta.37 code base.

### Fixed

- Fix randomHex returning inconsistent string lengths (#1490)
- Fix make isBN minification safe (#1777)
- Fix incorrect references to BigNumber in utils.fromWei and utils.toWei error messages (#2468)
- Fix error incorrectly thrown when receipt.status is `null` (#2183)
Expand Down
16 changes: 12 additions & 4 deletions packages/web3-utils/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/web3-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"eth-lib": "0.2.7",
"ethjs-unit": "0.1.6",
"number-to-bn": "1.7.0",
"randomhex": "0.1.5",
"randombytes": "^2.1.0",
"underscore": "1.9.1",
"utf8": "3.0.0"
}
Expand Down
12 changes: 11 additions & 1 deletion packages/web3-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var _ = require('underscore');
var ethjsUnit = require('ethjs-unit');
var utils = require('./utils.js');
var soliditySha3 = require('./soliditySha3.js');
var randomHex = require('randomhex');
var randombytes = require('randombytes');



Expand Down Expand Up @@ -145,6 +145,16 @@ var _flattenTypes = function(includeTuple, puts)
};


/**
* Returns a random hex string by the given bytes size
*
* @param {Number} size
* @returns {string}
*/
var randomHex = function(size) {
return '0x' + randombytes(size).toString('hex');
};

/**
* Should be called to get ascii from it's hex representation
*
Expand Down
25 changes: 25 additions & 0 deletions test/utils.randomHex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var chai = require('chai');
var utils = require('../packages/web3-utils');

var assert = chai.assert;

// Expect 2 chars per bytes plus `0x` prefix
var tests = [
{ value: 0, expected: { prefix: '0x', type: 'string', length: 2 }},
{ value: 15, expected: { prefix: '0x', type: 'string', length: 32 }},
{ value: 16, expected: { prefix: '0x', type: 'string', length: 34 }}
];

describe('lib/utils/utils', function () {
describe('randomHex', function () {
tests.forEach(function (test) {
it('should turn ' + test.value + ' to ' + test.expected, function () {
var result = utils.randomHex(test.value);

assert.strictEqual(typeof result, test.expected.type);
assert.strictEqual(result.slice(0,2), test.expected.prefix);
assert.strictEqual(result.length, test.expected.length);
});
});
});
});