Skip to content

Commit

Permalink
fix(@embark/core): ensure 0x0 address are extended to full zero addre…
Browse files Browse the repository at this point in the history
…sses

This was a regression introduced in a web3 upgrade where `0x0` addresses where no longer
accepted as valid addresses and have to be replaced with full zero addresses.

This commit ensures that, if Embark apps still make use of `0x0` addresses in
their configs, they are properly replaced or extended to satisfy web3's APIs.

Fixes #956
  • Loading branch information
0x-r4bbit committed Nov 14, 2018
1 parent 70a61c5 commit d3f6b43
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 52 deletions.
94 changes: 50 additions & 44 deletions npm-shrinkwrap.json

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

48 changes: 40 additions & 8 deletions src/lib/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const web3 = require('web3');
const constants = require('../constants');
const {canonicalHost, defaultHost} = require('../utils/host');
const cloneDeep = require('lodash.clonedeep');
import { extendZeroAddressShorthand, replaceZeroAddressShorthand } from '../utils/addressUtils';

const DEFAULT_CONFIG_PATH = 'config/';
const unitRegex = /([0-9]+) ([a-zA-Z]+)/;
Expand Down Expand Up @@ -299,16 +300,47 @@ Config.prototype.loadContractsConfigFile = function() {
}
});
}

Object.keys(newContractsConfig.contracts).forEach(contractName => {
let gas = newContractsConfig.contracts[contractName].gas;
let gasPrice = newContractsConfig.contracts[contractName].gasPrice;
if (gas && gas.match(unitRegex)) {
newContractsConfig.contracts[contractName].gas = utils.getWeiBalanceFromString(gas, web3);
}
if (gasPrice && gasPrice.match(unitRegex)) {
newContractsConfig.contracts[contractName].gasPrice = utils.getWeiBalanceFromString(gasPrice, web3);
}

const gas = newContractsConfig.contracts[contractName].gas;
const gasPrice = newContractsConfig.contracts[contractName].gasPrice;
const address = newContractsConfig.contracts[contractName].address;
const args = newContractsConfig.contracts[contractName].args;
const onDeploy = newContractsConfig.contracts[contractName].onDeploy;

if (gas && gas.match(unitRegex)) {
newContractsConfig.contracts[contractName].gas = utils.getWeiBalanceFromString(gas, web3);
}

if (gasPrice && gasPrice.match(unitRegex)) {
newContractsConfig.contracts[contractName].gasPrice = utils.getWeiBalanceFromString(gasPrice, web3);
}

if (address) {
newContractsConfig.contracts[contractName].address = extendZeroAddressShorthand(address);
}

if (args && args.length) {
newContractsConfig.contracts[contractName].args = args.map(val => {
if (typeof val === 'string') {
return extendZeroAddressShorthand(val);
}
return val;
});
}

if (onDeploy && onDeploy.length) {
newContractsConfig.contracts[contractName].onDeploy = onDeploy.map(replaceZeroAddressShorthand);
}
});

const afterDeploy = newContractsConfig.contracts.afterDeploy;

if (afterDeploy && afterDeploy.length) {
newContractsConfig.contracts.afterDeploy = afterDeploy.map(replaceZeroAddressShorthand);
}

if (!deepEqual(newContractsConfig, this.contractsConfig)) {
this.contractsConfig = newContractsConfig;
}
Expand Down
14 changes: 14 additions & 0 deletions src/lib/utils/addressUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const ZERO_ADDRESS_SHORTHAND_REGEX = /^0x0$/;
const ZERO_ADDRESS_SHORTHAND_SEARCH_REGEX = /'0x0'/g;
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";

export function extendZeroAddressShorthand(value: string) {
if (value.match(ZERO_ADDRESS_SHORTHAND_REGEX) !== null) {
return ZERO_ADDRESS;
}
return value;
}

export function replaceZeroAddressShorthand(value: string) {
return value.replace(ZERO_ADDRESS_SHORTHAND_SEARCH_REGEX, `'${ZERO_ADDRESS}'`);
}
Loading

0 comments on commit d3f6b43

Please sign in to comment.