Skip to content

Commit 42bd3b7

Browse files
jrainvilleiurimatias
authored andcommitted
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
1 parent 6f239f4 commit 42bd3b7

File tree

4 files changed

+47
-48
lines changed

4 files changed

+47
-48
lines changed

packages/embarkjs/ens/src/ENSFunctions.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* global require */
21
const namehash = require('eth-ens-namehash');
32

43
// Price of ENS registration contract functions

packages/embarkjs/ens/src/index.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
/* global global require */
1+
/* global ethereum*/
22
import { reduce } from 'async';
33
let EmbarkJS = global.EmbarkJS || require('embarkjs');
44
EmbarkJS = EmbarkJS.default || EmbarkJS;
55
const ENSFunctions = require('./ENSFunctions').default;
66
const Web3 = require('web3');
7-
const { RequestManager } = require('web3-core-requestmanager');
87
const namehash = require('eth-ens-namehash');
98

109
const __embarkENS = {};
@@ -175,7 +174,7 @@ async function connectWeb3(web3, callback) {
175174
try {
176175
await ethereum.enable();
177176
web3.setProvider(ethereum);
178-
return checkConnect(callback);
177+
return checkConnection(callback);
179178
} catch (e) {
180179
return callback(null, {
181180
error: e,
@@ -204,48 +203,45 @@ function checkConnection(web3, callback) {
204203
__embarkENS.web3 = new Web3();
205204

206205
__embarkENS.setProvider = function(config) {
207-
const self = this;
208206
const ERROR_MESSAGE = 'ENS is not available in this chain';
209-
self.registration = config.registration;
210-
self.env = config.env;
211-
self.ready = false;
212-
213-
let connectionErrors = {};
207+
this.registration = config.registration;
208+
this.env = config.env;
209+
this.ready = false;
214210

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

220216
if (connectionString === '$WEB3') {
221-
connectWeb3(self.web3, next);
217+
connectWeb3(this.web3, next);
222218
} else if ((/^wss?:\/\//).test(connectionString)) {
223-
connectWebSocket(self.web3, connectionString, next);
219+
connectWebSocket(this.web3, connectionString, next);
224220
} else {
225-
connectHttp(self.web3, connectionString, next);
221+
connectHttp(this.web3, connectionString, next);
226222
}
227223
}, async (err, result) => {
228-
if (!result.connected || result.error) {
224+
if (result.error) {
229225
console.error(result.error);
230226
}
231227
try {
232-
const accounts = await self.web3.eth.getAccounts();
233-
self.web3.eth.defaultAccount = accounts[0];
234-
const id = await self.web3.eth.net.getId()
235-
const registryAddress = self.registryAddresses[id] || config.registryAddress;
236-
self._isAvailable = true;
237-
self.ens = new self.web3.eth.Contract(config.registryAbi, registryAddress);
238-
self.registrar = new self.web3.eth.Contract(config.registrarAbi, config.registrarAddress);
239-
self.resolver = new self.web3.eth.Contract(config.resolverAbi, config.resolverAddress);
240-
self.ready = true;
241-
} catch (err) {
242-
self.ready = true;
243-
if (err.message.indexOf('Provider not set or invalid') > -1) {
228+
const accounts = await this.web3.eth.getAccounts();
229+
this.web3.eth.defaultAccount = accounts[0];
230+
const id = await this.web3.eth.net.getId();
231+
const registryAddress = this.registryAddresses[id] || config.registryAddress;
232+
this._isAvailable = true;
233+
this.ens = new this.web3.eth.Contract(config.registryAbi, registryAddress);
234+
this.registrar = new this.web3.eth.Contract(config.registrarAbi, config.registrarAddress);
235+
this.resolver = new this.web3.eth.Contract(config.resolverAbi, config.resolverAddress);
236+
this.ready = true;
237+
} catch (e) {
238+
this.ready = true;
239+
if (e.message.indexOf('Provider not set or invalid') > -1) {
244240
console.warn(ERROR_MESSAGE);
245241
return;
246242
}
247-
console.error(err);
248-
};
243+
console.error(e);
244+
}
249245
});
250246
};
251247

packages/plugins/ens/src/index.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class ENS {
8989
}
9090

9191
async init(cb = () => {}) {
92-
if (this.initated || this.config.namesystemConfig === {} ||
92+
if (this.initated ||
9393
this.config.namesystemConfig.enabled !== true ||
9494
!this.config.namesystemConfig.available_providers ||
9595
this.config.namesystemConfig.available_providers.indexOf('ens') < 0) {
@@ -277,18 +277,17 @@ class ENS {
277277
return cb();
278278
}
279279
const web3 = await this.web3;
280-
280+
281281
const networkId = await web3.eth.net.getId();
282-
283-
282+
284283
if (ensContractAddresses[networkId]) {
285284
if (this.config.namesystemConfig.register && this.config.namesystemConfig.register.rootDomain) {
286285
this.logger.warn(__("Cannot register subdomains on this network, because we do not own the ENS contracts. Are you on testnet or mainnet?"));
287286
}
288287
this.config.namesystemConfig.register = false; // force subdomains from being registered
289288
this.ensConfig = recursiveMerge(this.ensConfig, ensContractAddresses[networkId]);
290289
}
291-
290+
292291
const registration = this.config.namesystemConfig.register;
293292
const doRegister = registration && registration.rootDomain;
294293

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

300+
const config = {
301+
registryAbi: self.ensConfig.ENSRegistry.abiDefinition,
302+
registryAddress: self.ensConfig.ENSRegistry.deployedAddress,
303+
resolverAbi: self.ensConfig.Resolver.abiDefinition,
304+
resolverAddress: self.ensConfig.Resolver.deployedAddress
305+
};
306+
307+
self.ensContract = new web3.eth.Contract(config.registryAbi, config.registryAddress);
308+
self.resolverContract = new web3.eth.Contract(config.resolverAbi, config.resolverAddress);
309+
301310
async.waterfall([
302311
function checkRootNode(next) {
303312
if (!doRegister) {
@@ -317,25 +326,15 @@ class ENS {
317326
self.events.request('contracts:add', self.ensConfig.FIFSRegistrar, (_err, contract) => {
318327
self.ensConfig.FIFSRegistrar = contract;
319328
self.events.request('deployment:contract:deploy', self.ensConfig.FIFSRegistrar, (err) => {
329+
config.registrarAbi = self.ensConfig.FIFSRegistrar.abiDefinition;
330+
config.registrarAddress = self.ensConfig.FIFSRegistrar.deployedAddress;
331+
self.registrarContract = new web3.eth.Contract(config.registrarAbi, config.registrarAddress);
320332
return next(err);
321333
});
322334
});
323335
},
324336
function registerRoot(next) {
325-
let config = {
326-
registryAbi: self.ensConfig.ENSRegistry.abiDefinition,
327-
registryAddress: self.ensConfig.ENSRegistry.deployedAddress,
328-
registrarAbi: self.ensConfig.FIFSRegistrar.abiDefinition,
329-
registrarAddress: self.ensConfig.FIFSRegistrar.deployedAddress,
330-
resolverAbi: self.ensConfig.Resolver.abiDefinition,
331-
resolverAddress: self.ensConfig.Resolver.deployedAddress
332-
};
333-
334337
async function send() {
335-
self.ensContract = new web3.eth.Contract(config.registryAbi, config.registryAddress);
336-
self.registrarContract = new web3.eth.Contract(config.registrarAbi, config.registrarAddress);
337-
self.resolverContract = new web3.eth.Contract(config.resolverAbi, config.resolverAddress);
338-
339338
const defaultAccount = await self.web3DefaultAccount;
340339

341340
const rootNode = namehash.hash(registration.rootDomain);

packages/stack/embarkjs/src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,18 @@ class EmbarkJS {
117117
let code = "";
118118
if (stackName === 'storage') {
119119
code = `EmbarkJS.${moduleName}.setProviders(${JSON.stringify(config)});`;
120-
} else if (stackName === 'blockchain') {
120+
} else if (stackName === 'blockchain' || stackName === 'names') {
121121
const endpoint = await this.events.request2("proxy:endpoint:get");
122122
const dappConnectionConfig = {
123123
dappConnection: [endpoint]
124124
};
125-
code = `EmbarkJS.${moduleName}.setProvider('${pluginName}', ${config});
125+
if (stackName === 'blockchain') {
126+
code = `EmbarkJS.${moduleName}.setProvider('${pluginName}', ${config});
126127
EmbarkJS.Blockchain.connect(${JSON.stringify(dappConnectionConfig)}, (err) => {if (err) { console.error(err); } });`;
128+
} else {
129+
code = `EmbarkJS.${moduleName}.setProvider('${pluginName}', ${JSON.stringify(Object.assign(config, dappConnectionConfig))});`;
130+
}
131+
127132
} else {
128133
code = `EmbarkJS.${moduleName}.setProvider('${pluginName}', ${JSON.stringify(config)});`;
129134
}

0 commit comments

Comments
 (0)