Skip to content

Commit

Permalink
Merge branch '1.x' into feature/add-goerli-identification-support-1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
nivida committed Oct 8, 2019
2 parents 290fe14 + 5035b25 commit c579ecb
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,10 @@ 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)
- Fix incorrectly populating chainId param with `net_version` when signing txs (#2378)
- regeneratorRuntime error fixed (#3058)
- Fix accessing event.name where event is undefined (#3014)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# web3.js - Ethereum JavaScript API

[![Join the chat at https://gitter.im/ethereum/web3.js](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/ethereum/web3.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)[![npm](https://img.shields.io/npm/dm/web3.svg)](https://www.npmjs.com/package/web3) [![Build Status][travis-image]][travis-url] [![dependency status][dep-image]][dep-url] [![dev dependency status][dep-dev-image]][dep-dev-url] [![Coverage Status][coveralls-image]][coveralls-url]
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lerna.js.org/)

This is the Ethereum [JavaScript API][docs]
which connects to the [Generic JSON RPC](https://github.com/ethereum/wiki/wiki/JSON-RPC) spec.
Expand Down
17 changes: 16 additions & 1 deletion docs/web3-eth-personal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,17 @@ sign
web3.eth.personal.sign(dataToSign, address, password [, callback])
Signs data using a specific account.
The sign method calculates an Ethereum specific signature with:

.. code-block:: javascript
sign(keccak256("\x19Ethereum Signed Message:\n" + dataToSign.length + dataToSign)))
Adding a prefix to the message makes the calculated signature recognisable as an Ethereum specific signature.

If you have the original message and the signed message, you can discover the signing account address
using :ref:`web3.eth.personal.ecRecover <eth-personal-ecRecover>` (See example below)


.. note:: Sending your account password over an unsecured HTTP RPC connection is highly unsecure.

Expand Down Expand Up @@ -119,6 +129,11 @@ Example
.then(console.log);
> "0x30755ed65396facf86c53e6217c52b4daebe72aa4941d89635409de4c9c7f9466d4e9aaec7977f05e923889b33c0d0dd27d7226b6e6f56ce737465c5cfd04be400"
// recover the signing account address using original message and signed message
web3.eth.personal.ecRecover("Hello world", "0x30755ed65396...etc...")
.then(console.log);
> "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe"
------------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-core-helpers/src/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ var outputTransactionReceiptFormatter = function (receipt){
receipt.contractAddress = utils.toChecksumAddress(receipt.contractAddress);
}

if(typeof receipt.status !== 'undefined') {
if(typeof receipt.status !== 'undefined' && receipt.status !== null) {
receipt.status = Boolean(parseInt(receipt.status));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var Accounts = function Accounts() {
var _ethereumCall = [
new Method({
name: 'getId',
call: 'net_version',
call: 'eth_chainId',
params: 0,
outputFormatter: utils.hexToNumber
}),
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
16 changes: 13 additions & 3 deletions 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 Expand Up @@ -232,7 +242,7 @@ var fromWei = function(number, unit) {
unit = getUnitValue(unit);

if(!utils.isBN(number) && !_.isString(number)) {
throw new Error('Please pass numbers as strings or BigNumber objects to avoid precision errors.');
throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.');
}

return utils.isBN(number) ? ethjsUnit.fromWei(number, unit) : ethjsUnit.fromWei(number, unit).toString(10);
Expand Down Expand Up @@ -264,7 +274,7 @@ var toWei = function(number, unit) {
unit = getUnitValue(unit);

if(!utils.isBN(number) && !_.isString(number)) {
throw new Error('Please pass numbers as strings or BigNumber objects to avoid precision errors.');
throw new Error('Please pass numbers as strings or BN objects to avoid precision errors.');
}

return utils.isBN(number) ? ethjsUnit.toWei(number, unit) : ethjsUnit.toWei(number, unit).toString(10);
Expand Down
3 changes: 1 addition & 2 deletions packages/web3-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ var Hash = require("eth-lib/lib/hash");
* @return {Boolean}
*/
var isBN = function (object) {
return object instanceof BN ||
(object && object.constructor && object.constructor.name === 'BN');
return BN.isBN(object);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions test/eth.accounts.signTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ describe("eth", function () {
provider.injectResult(1);
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'net_version');
assert.equal(payload.method, 'eth_chainId');
assert.deepEqual(payload.params, []);
});

Expand All @@ -522,7 +522,7 @@ describe("eth", function () {
provider.injectResult(1);
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'net_version');
assert.equal(payload.method, 'eth_chainId');
assert.deepEqual(payload.params, []);
});
provider.injectResult(1);
Expand Down
122 changes: 122 additions & 0 deletions test/formatters.outputTransactionReceiptFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
var assert = require('assert');
var formatters = require('../packages/web3-core-helpers/src/formatters.js');

describe('outputTransactionReceiptFormatter', function() {

it('call outputTransactionReceiptFormatter with a valid receipt', function() {
var receipt = {
status: '0x0',
cumulativeGasUsed: '0x100',
gasUsed: '0x100',
blockNumber: '0x100',
transactionIndex: '0xa',
to: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
from: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
contractAddress: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078'
};

assert.deepEqual(formatters.outputTransactionReceiptFormatter(receipt), {
status: false,
cumulativeGasUsed: 256,
gasUsed: 256,
blockNumber: 256,
transactionIndex: 10,
to: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
from: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
contractAddress: '0x03C9A938fF7f54090d0d99e2c6f80380510Ea078'
});
});

it('call outputTransactionReceiptFormatter with a valid receipt and logs', function() {
var receipt = {
status: '0x0',
cumulativeGasUsed: '0x100',
gasUsed: '0x100',
blockNumber: '0x100',
transactionIndex: '0xa',
to: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
from: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
contractAddress: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
logs: [{}]
};

assert.deepEqual(formatters.outputTransactionReceiptFormatter(receipt), {
status: false,
cumulativeGasUsed: 256,
gasUsed: 256,
blockNumber: 256,
transactionIndex: 10,
to: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
from: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
contractAddress: '0x03C9A938fF7f54090d0d99e2c6f80380510Ea078',
logs: [
{
blockNumber: undefined,
id: null,
logIndex: undefined,
transactionIndex: undefined
}
]
});
});

it('call outputTransactionReceiptFormatter when status is "0x1"', function() {
var receipt = {
status: '0x1',
cumulativeGasUsed: '0x100',
gasUsed: '0x100',
blockNumber: '0x100',
transactionIndex: '0xa',
to: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
from: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
contractAddress: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078'
};

assert.equal(formatters.outputTransactionReceiptFormatter(receipt).status, true)
});

it('call outputTransactionReceiptFormatter when status is "0x01"', function() {
var receipt = {
status: '0x01',
cumulativeGasUsed: '0x100',
gasUsed: '0x100',
blockNumber: '0x100',
transactionIndex: '0xa',
to: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
from: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
contractAddress: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078'
};

assert.equal(formatters.outputTransactionReceiptFormatter(receipt).status, true)
});

it('call outputTransactionReceiptFormatter when status is "undefined"', function() {
var receipt = {
status: undefined,
cumulativeGasUsed: '0x100',
gasUsed: '0x100',
blockNumber: '0x100',
transactionIndex: '0xa',
to: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
from: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
contractAddress: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078'
};

assert.equal(formatters.outputTransactionReceiptFormatter(receipt).status, undefined)
});

it('call outputTransactionReceiptFormatter when status is "null"', function() {
var receipt = {
status: null,
cumulativeGasUsed: '0x100',
gasUsed: '0x100',
blockNumber: '0x100',
transactionIndex: '0xa',
to: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
from: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078',
contractAddress: '0x03c9a938ff7f54090d0d99e2c6f80380510ea078'
};

assert.equal(formatters.outputTransactionReceiptFormatter(receipt).status, null)
});
});
4 changes: 2 additions & 2 deletions test/helpers/test.method.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ var useLocalWallet = function (test, provider, web3) {

test.useLocalWallet(web3);

provider.injectResult(1);
provider.injectResult("0x1");
provider.injectValidation(function (payload) {
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'net_version');
assert.equal(payload.method, 'eth_chainId');
assert.deepEqual(payload.params, []);
});

Expand Down
9 changes: 9 additions & 0 deletions test/utils.fromWei.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,14 @@ describe('lib/utils/utils', function () {
assert.equal(utils.fromWei('1000000000000000000', 'gether'), '0.000000001');
assert.equal(utils.fromWei('1000000000000000000', 'tether'), '0.000000000001');
});

it('should verify "number" arg is string or BN', function () {
try {
utils.fromWei(100000000000, 'wei')
assert.fail();
} catch (error) {
assert(error.message.includes('Please pass numbers as strings or BN objects'))
}
})
});
});
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);
});
});
});
});
9 changes: 9 additions & 0 deletions test/utils.toWei.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,14 @@ describe('lib/utils/utils', function () {

assert.throws(function () {utils.toWei(1, 'wei1');}, Error);
});

it('should verify "number" arg is string or BN', function () {
try {
utils.toWei(1, 'wei')
assert.fail();
} catch (error) {
assert(error.message.includes('Please pass numbers as strings or BN objects'))
}
})
});
});

0 comments on commit c579ecb

Please sign in to comment.