Skip to content

Commit

Permalink
fix(@embark/ens): fix Infura connection and testnet use of ENS
Browse files Browse the repository at this point in the history
Fixes the use of Infura to connect to the ENS contracts. When
connecting directly to Infura, it would throw with `rejected due to
project ID settings`, because it doesn't accept the VM as the domain
Instead, when passing from the proxy, it works. So I changed the
default when no dappConnection to ['$EMBARK']. I also added a
message when the error happens to help users fix it themselves

When in the testnet, we don,t register because we already have the
addresses, which is fine, but we also didn't populate the ensConfig
object which contains the important information about the addresses
and ABI.

There was a lot of lint problems in a couple of files so I cleaned
that up
  • Loading branch information
jrainville authored and iurimatias committed Jan 16, 2020
1 parent 6f239f4 commit 42bd3b7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 48 deletions.
1 change: 0 additions & 1 deletion packages/embarkjs/ens/src/ENSFunctions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* global require */
const namehash = require('eth-ens-namehash');

// Price of ENS registration contract functions
Expand Down
50 changes: 23 additions & 27 deletions packages/embarkjs/ens/src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* global global require */
/* global ethereum*/
import { reduce } from 'async';
let EmbarkJS = global.EmbarkJS || require('embarkjs');
EmbarkJS = EmbarkJS.default || EmbarkJS;
const ENSFunctions = require('./ENSFunctions').default;
const Web3 = require('web3');
const { RequestManager } = require('web3-core-requestmanager');
const namehash = require('eth-ens-namehash');

const __embarkENS = {};
Expand Down Expand Up @@ -175,7 +174,7 @@ async function connectWeb3(web3, callback) {
try {
await ethereum.enable();
web3.setProvider(ethereum);
return checkConnect(callback);
return checkConnection(callback);
} catch (e) {
return callback(null, {
error: e,
Expand Down Expand Up @@ -204,48 +203,45 @@ function checkConnection(web3, callback) {
__embarkENS.web3 = new Web3();

__embarkENS.setProvider = function(config) {
const self = this;
const ERROR_MESSAGE = 'ENS is not available in this chain';
self.registration = config.registration;
self.env = config.env;
self.ready = false;

let connectionErrors = {};
this.registration = config.registration;
this.env = config.env;
this.ready = false;

reduce(config.dappConnection, false, (result, connectionString, next) => {
if (result.connected) {
return next(null, result);
}

if (connectionString === '$WEB3') {
connectWeb3(self.web3, next);
connectWeb3(this.web3, next);
} else if ((/^wss?:\/\//).test(connectionString)) {
connectWebSocket(self.web3, connectionString, next);
connectWebSocket(this.web3, connectionString, next);
} else {
connectHttp(self.web3, connectionString, next);
connectHttp(this.web3, connectionString, next);
}
}, async (err, result) => {
if (!result.connected || result.error) {
if (result.error) {
console.error(result.error);
}
try {
const accounts = await self.web3.eth.getAccounts();
self.web3.eth.defaultAccount = accounts[0];
const id = await self.web3.eth.net.getId()
const registryAddress = self.registryAddresses[id] || config.registryAddress;
self._isAvailable = true;
self.ens = new self.web3.eth.Contract(config.registryAbi, registryAddress);
self.registrar = new self.web3.eth.Contract(config.registrarAbi, config.registrarAddress);
self.resolver = new self.web3.eth.Contract(config.resolverAbi, config.resolverAddress);
self.ready = true;
} catch (err) {
self.ready = true;
if (err.message.indexOf('Provider not set or invalid') > -1) {
const accounts = await this.web3.eth.getAccounts();
this.web3.eth.defaultAccount = accounts[0];
const id = await this.web3.eth.net.getId();
const registryAddress = this.registryAddresses[id] || config.registryAddress;
this._isAvailable = true;
this.ens = new this.web3.eth.Contract(config.registryAbi, registryAddress);
this.registrar = new this.web3.eth.Contract(config.registrarAbi, config.registrarAddress);
this.resolver = new this.web3.eth.Contract(config.resolverAbi, config.resolverAddress);
this.ready = true;
} catch (e) {
this.ready = true;
if (e.message.indexOf('Provider not set or invalid') > -1) {
console.warn(ERROR_MESSAGE);
return;
}
console.error(err);
};
console.error(e);
}
});
};

Expand Down
35 changes: 17 additions & 18 deletions packages/plugins/ens/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class ENS {
}

async init(cb = () => {}) {
if (this.initated || this.config.namesystemConfig === {} ||
if (this.initated ||
this.config.namesystemConfig.enabled !== true ||
!this.config.namesystemConfig.available_providers ||
this.config.namesystemConfig.available_providers.indexOf('ens') < 0) {
Expand Down Expand Up @@ -277,18 +277,17 @@ class ENS {
return cb();
}
const web3 = await this.web3;

const networkId = await web3.eth.net.getId();



if (ensContractAddresses[networkId]) {
if (this.config.namesystemConfig.register && this.config.namesystemConfig.register.rootDomain) {
this.logger.warn(__("Cannot register subdomains on this network, because we do not own the ENS contracts. Are you on testnet or mainnet?"));
}
this.config.namesystemConfig.register = false; // force subdomains from being registered
this.ensConfig = recursiveMerge(this.ensConfig, ensContractAddresses[networkId]);
}

const registration = this.config.namesystemConfig.register;
const doRegister = registration && registration.rootDomain;

Expand All @@ -298,6 +297,16 @@ class ENS {
this.ensConfig.Resolver = await this.events.request2('contracts:add', this.ensConfig.Resolver);
await this.events.request2('deployment:contract:deploy', this.ensConfig.Resolver);

const config = {
registryAbi: self.ensConfig.ENSRegistry.abiDefinition,
registryAddress: self.ensConfig.ENSRegistry.deployedAddress,
resolverAbi: self.ensConfig.Resolver.abiDefinition,
resolverAddress: self.ensConfig.Resolver.deployedAddress
};

self.ensContract = new web3.eth.Contract(config.registryAbi, config.registryAddress);
self.resolverContract = new web3.eth.Contract(config.resolverAbi, config.resolverAddress);

async.waterfall([
function checkRootNode(next) {
if (!doRegister) {
Expand All @@ -317,25 +326,15 @@ class ENS {
self.events.request('contracts:add', self.ensConfig.FIFSRegistrar, (_err, contract) => {
self.ensConfig.FIFSRegistrar = contract;
self.events.request('deployment:contract:deploy', self.ensConfig.FIFSRegistrar, (err) => {
config.registrarAbi = self.ensConfig.FIFSRegistrar.abiDefinition;
config.registrarAddress = self.ensConfig.FIFSRegistrar.deployedAddress;
self.registrarContract = new web3.eth.Contract(config.registrarAbi, config.registrarAddress);
return next(err);
});
});
},
function registerRoot(next) {
let config = {
registryAbi: self.ensConfig.ENSRegistry.abiDefinition,
registryAddress: self.ensConfig.ENSRegistry.deployedAddress,
registrarAbi: self.ensConfig.FIFSRegistrar.abiDefinition,
registrarAddress: self.ensConfig.FIFSRegistrar.deployedAddress,
resolverAbi: self.ensConfig.Resolver.abiDefinition,
resolverAddress: self.ensConfig.Resolver.deployedAddress
};

async function send() {
self.ensContract = new web3.eth.Contract(config.registryAbi, config.registryAddress);
self.registrarContract = new web3.eth.Contract(config.registrarAbi, config.registrarAddress);
self.resolverContract = new web3.eth.Contract(config.resolverAbi, config.resolverAddress);

const defaultAccount = await self.web3DefaultAccount;

const rootNode = namehash.hash(registration.rootDomain);
Expand Down
9 changes: 7 additions & 2 deletions packages/stack/embarkjs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,18 @@ class EmbarkJS {
let code = "";
if (stackName === 'storage') {
code = `EmbarkJS.${moduleName}.setProviders(${JSON.stringify(config)});`;
} else if (stackName === 'blockchain') {
} else if (stackName === 'blockchain' || stackName === 'names') {
const endpoint = await this.events.request2("proxy:endpoint:get");
const dappConnectionConfig = {
dappConnection: [endpoint]
};
code = `EmbarkJS.${moduleName}.setProvider('${pluginName}', ${config});
if (stackName === 'blockchain') {
code = `EmbarkJS.${moduleName}.setProvider('${pluginName}', ${config});
EmbarkJS.Blockchain.connect(${JSON.stringify(dappConnectionConfig)}, (err) => {if (err) { console.error(err); } });`;
} else {
code = `EmbarkJS.${moduleName}.setProvider('${pluginName}', ${JSON.stringify(Object.assign(config, dappConnectionConfig))});`;
}

} else {
code = `EmbarkJS.${moduleName}.setProvider('${pluginName}', ${JSON.stringify(config)});`;
}
Expand Down

0 comments on commit 42bd3b7

Please sign in to comment.