fix(@embark/ens): fix Infura connection and testnet use of ENS

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
This commit is contained in:
Jonathan Rainville 2020-01-14 15:39:06 -05:00 committed by Iuri Matias
parent 6f239f4d90
commit 42bd3b7792
4 changed files with 47 additions and 48 deletions

View File

@ -1,4 +1,3 @@
/* global require */
const namehash = require('eth-ens-namehash');
// Price of ENS registration contract functions

View File

@ -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 = {};
@ -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,
@ -204,13 +203,10 @@ 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) {
@ -218,34 +214,34 @@ __embarkENS.setProvider = function(config) {
}
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);
}
});
};

View File

@ -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) {
@ -280,7 +280,6 @@ class ENS {
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?"));
@ -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) {
@ -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);

View File

@ -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]
};
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)});`;
}