Skip to content

Commit

Permalink
Fix randomHex (#3085)
Browse files Browse the repository at this point in the history
* randomHex fixed

* Add utils.randomHex unit test
  • Loading branch information
cgewecke authored and nivida committed Oct 8, 2019
1 parent 0cbf9ae commit 2b09606
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
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);
});
});
});
});

0 comments on commit 2b09606

Please sign in to comment.