mirror of
https://github.com/embarklabs/embark.git
synced 2025-01-11 14:24:24 +00:00
fix(ens/web3): use blockchain connector for ens and fix global web3
Use the blockchain connector for the ENS module to remove direct web3 usages Fix global web3 being overidden by the code generator
This commit is contained in:
parent
604e267e9d
commit
d5f6da3599
@ -2,11 +2,13 @@
|
||||
|
||||
const __embarkWeb3 = {};
|
||||
|
||||
__embarkWeb3.init = function (_config) {
|
||||
this.web3 = new Web3();
|
||||
if (!global.web3) {
|
||||
global.web3 = this.web3;
|
||||
}
|
||||
__embarkWeb3.init = function(_config) {
|
||||
this.web3 = global.web3 || new Web3();
|
||||
global.web3 = global.web3 || this.web3;
|
||||
};
|
||||
|
||||
__embarkWeb3.getInstance = function () {
|
||||
return this.web3;
|
||||
};
|
||||
|
||||
__embarkWeb3.getAccounts = function () {
|
||||
@ -45,4 +47,8 @@ __embarkWeb3.toWei = function () {
|
||||
return this.web3.toWei(...arguments);
|
||||
};
|
||||
|
||||
__embarkWeb3.getNetworkId = function () {
|
||||
return this.web3.eth.net.getId();
|
||||
};
|
||||
|
||||
|
||||
|
@ -371,7 +371,6 @@ class CodeGenerator {
|
||||
code += "\n if (typeof web3 === 'undefined') {";
|
||||
code += "\n var web3 = new Web3();\n";
|
||||
code += "\n }";
|
||||
code += "\nglobal.web3 = web3;\n";
|
||||
|
||||
let providerCode = self.generateProvider(false);
|
||||
code += providerCode;
|
||||
|
@ -1,4 +1,3 @@
|
||||
/*global web3*/
|
||||
const namehash = require('eth-ens-namehash');
|
||||
// Price of ENS registration contract functions
|
||||
const ENS_GAS_PRICE = 700000;
|
||||
@ -8,7 +7,7 @@ const reverseAddressSuffix = '.addr.reverse';
|
||||
const NoDecodeAddrErr = 'Error: Couldn\'t decode address from ABI: 0x';
|
||||
const NoDecodeStringErr = 'ERROR: The returned value is not a convertible string: 0x0';
|
||||
|
||||
function registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain, rootDomain, reverseNode, address, logger, secureSend, callback) {
|
||||
function registerSubDomain(web3, ens, registrar, resolver, defaultAccount, subdomain, rootDomain, reverseNode, address, logger, secureSend, callback) {
|
||||
const subnode = namehash.hash(subdomain);
|
||||
const rootNode = namehash.hash(rootDomain);
|
||||
const node = namehash.hash(`${subdomain}.${rootDomain}`);
|
||||
|
@ -1,4 +1,4 @@
|
||||
/*global EmbarkJS, web3, registerSubDomain, namehash*/
|
||||
/*global EmbarkJS, Web3, registerSubDomain, namehash*/
|
||||
|
||||
let __embarkENS = {};
|
||||
|
||||
@ -153,13 +153,25 @@ __embarkENS.setProvider = function (config) {
|
||||
self.registration = config.registration;
|
||||
self.env = config.env;
|
||||
EmbarkJS.onReady(() => {
|
||||
web3.eth.net.getId()
|
||||
EmbarkJS.Blockchain.blockchainConnector.getNetworkId()
|
||||
.then((id) => {
|
||||
const registryAddress = self.registryAddresses[id] || config.registryAddress;
|
||||
self._isAvailable = true;
|
||||
self.ens = new EmbarkJS.Blockchain.Contract({abi: config.registryAbi, address: registryAddress, web3: web3});
|
||||
self.registrar = new EmbarkJS.Blockchain.Contract({abi: config.registrarAbi, address: config.registrarAddress, web3: web3});
|
||||
self.resolver = new EmbarkJS.Blockchain.Contract({abi: config.resolverAbi, address: config.resolverAddress, web3: web3});
|
||||
self.ens = new EmbarkJS.Blockchain.Contract({
|
||||
abi: config.registryAbi,
|
||||
address: registryAddress,
|
||||
web3: EmbarkJS.Blockchain.blockchainConnector.getInstance()
|
||||
});
|
||||
self.registrar = new EmbarkJS.Blockchain.Contract({
|
||||
abi: config.registrarAbi,
|
||||
address: config.registrarAddress,
|
||||
web3: EmbarkJS.Blockchain.blockchainConnector.getInstance()
|
||||
});
|
||||
self.resolver = new EmbarkJS.Blockchain.Contract({
|
||||
abi: config.resolverAbi,
|
||||
address: config.resolverAddress,
|
||||
web3: EmbarkJS.Blockchain.blockchainConnector.getInstance()
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
if (err.message.indexOf('Provider not set or invalid') > -1) {
|
||||
@ -188,7 +200,7 @@ __embarkENS.resolve = function (name, callback) {
|
||||
if (!this.ens) {
|
||||
resolveOrReject(providerNotSetError);
|
||||
}
|
||||
if (!web3.eth.defaultAccount) {
|
||||
if (!EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount()) {
|
||||
resolveOrReject(defaultAccountNotSetError);
|
||||
}
|
||||
|
||||
@ -201,7 +213,7 @@ __embarkENS.resolve = function (name, callback) {
|
||||
let resolverContract = new EmbarkJS.Blockchain.Contract({
|
||||
abi: this.resolverInterface,
|
||||
address: resolvedAddress,
|
||||
web3: web3
|
||||
web3: EmbarkJS.Blockchain.blockchainConnector.getInstance()
|
||||
});
|
||||
resolverContract.methods.addr(node).call(resolveOrReject);
|
||||
}).catch(resolveOrReject);
|
||||
@ -225,7 +237,7 @@ __embarkENS.lookup = function (address, callback) {
|
||||
return resolveOrReject(providerNotSetError);
|
||||
}
|
||||
|
||||
if (!web3.eth.defaultAccount) {
|
||||
if (!EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount()) {
|
||||
return resolveOrReject(defaultAccountNotSetError);
|
||||
}
|
||||
|
||||
@ -233,7 +245,7 @@ __embarkENS.lookup = function (address, callback) {
|
||||
address = address.slice(2);
|
||||
}
|
||||
|
||||
let node = web3.utils.soliditySha3(address.toLowerCase() + reverseAddrSuffix);
|
||||
let node = Web3.utils.soliditySha3(address.toLowerCase() + reverseAddrSuffix);
|
||||
|
||||
this.ens.methods.resolver(node).call().then(resolverAddress => {
|
||||
if (resolverAddress === voidAddress) {
|
||||
@ -242,7 +254,7 @@ __embarkENS.lookup = function (address, callback) {
|
||||
const resolverContract = new EmbarkJS.Blockchain.Contract({
|
||||
abi: this.resolverInterface,
|
||||
address: resolverAddress,
|
||||
web3: web3
|
||||
web3: EmbarkJS.Blockchain.blockchainConnector.getInstance()
|
||||
});
|
||||
resolverContract.methods.name(node).call(resolveOrReject);
|
||||
}).catch(resolveOrReject);
|
||||
@ -252,7 +264,7 @@ __embarkENS.lookup = function (address, callback) {
|
||||
__embarkENS.registerSubDomain = function (name, address, callback) {
|
||||
callback = callback || function () {};
|
||||
|
||||
if (!web3.eth.defaultAccount) {
|
||||
if (!EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount()) {
|
||||
return callback(defaultAccountNotSetError);
|
||||
}
|
||||
|
||||
@ -262,13 +274,24 @@ __embarkENS.registerSubDomain = function (name, address, callback) {
|
||||
if (!this.registration || !this.registration.rootDomain) {
|
||||
return callback('No rootDomain is declared in config/namesystem.js (register.rootDomain). Unable to register a subdomain until then.');
|
||||
}
|
||||
if (!address || !web3.utils.isAddress(address)) {
|
||||
if (!address || !Web3.utils.isAddress(address)) {
|
||||
return callback('You need to specify a valid address for the subdomain');
|
||||
}
|
||||
|
||||
// Register function generated by the index
|
||||
registerSubDomain(this.ens, this.registrar, this.resolver, web3.eth.defaultAccount, name, this.registration.rootDomain,
|
||||
web3.utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix), address, console, EmbarkJS.Utils.secureSend, (err, result) => {
|
||||
registerSubDomain(
|
||||
EmbarkJS.Blockchain.blockchainConnector.getInstance(),
|
||||
this.ens,
|
||||
this.registrar,
|
||||
this.resolver,
|
||||
EmbarkJS.Blockchain.blockchainConnector.getDefaultAccount(),
|
||||
name,
|
||||
this.registration.rootDomain,
|
||||
Web3.utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix),
|
||||
address,
|
||||
console,
|
||||
EmbarkJS.Utils.secureSend,
|
||||
(err, result) => {
|
||||
if (err && err.indexOf('Transaction has been reverted by the EVM') > -1) {
|
||||
return callback('Registration was rejected. Are you the owner of the root domain?');
|
||||
}
|
||||
|
@ -281,8 +281,10 @@ class ENS {
|
||||
}
|
||||
|
||||
registerSubDomain(defaultAccount, subDomainName, reverseNode, address, secureSend, cb) {
|
||||
ENSFunctions.registerSubDomain(this.ensContract, this.registrarContract, this.resolverContract, defaultAccount,
|
||||
subDomainName, this.registration.rootDomain, reverseNode, address, this.logger, secureSend, cb);
|
||||
this.events.request("blockchain:get", (web3) => {
|
||||
ENSFunctions.registerSubDomain(web3, this.ensContract, this.registrarContract, this.resolverContract, defaultAccount,
|
||||
subDomainName, this.registration.rootDomain, reverseNode, address, this.logger, secureSend, cb);
|
||||
});
|
||||
}
|
||||
|
||||
createResolverContract(config, callback) {
|
||||
@ -311,7 +313,7 @@ class ENS {
|
||||
}
|
||||
], function (error, address) {
|
||||
if (error) {
|
||||
return res.send({error: error.message});
|
||||
return res.send({error: error.message || error});
|
||||
}
|
||||
res.send({address});
|
||||
});
|
||||
@ -328,7 +330,7 @@ class ENS {
|
||||
}
|
||||
], function (error, name) {
|
||||
if (error) {
|
||||
return res.send({error: error || error.message});
|
||||
return res.send({error: error.message || error});
|
||||
}
|
||||
res.send({name});
|
||||
});
|
||||
@ -340,16 +342,13 @@ class ENS {
|
||||
'/embark-api/ens/register',
|
||||
(req, res) => {
|
||||
self.events.request("blockchain:defaultAccount:get", (defaultAccount) => {
|
||||
const secureSend = embarkJsUtils.secureSend;
|
||||
const {subdomain, address} = req.body;
|
||||
const reverseNode = utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix);
|
||||
ENSFunctions.registerSubDomain(self.ensContract, self.registrarContract, self.resolverContract, defaultAccount,
|
||||
subdomain, self.registration.rootDomain, reverseNode, address, self.logger, secureSend, (error) => {
|
||||
if (error) {
|
||||
return res.send({error: error || error.message});
|
||||
}
|
||||
res.send({name: `${req.body.subdomain}.${self.registration.rootDomain}`, address: req.body.address});
|
||||
});
|
||||
this.safeRegisterSubDomain(subdomain, address, defaultAccount, (error) => {
|
||||
if (error) {
|
||||
return res.send({error: error.message || error});
|
||||
}
|
||||
res.send({name: `${req.body.subdomain}.${self.registration.rootDomain}`, address: req.body.address});
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
|
@ -1,50 +0,0 @@
|
||||
/*global web3*/
|
||||
const namehash = require('eth-ens-namehash');
|
||||
// Price of ENS registration contract functions
|
||||
const ENS_GAS_PRICE = 700000;
|
||||
|
||||
function registerSubDomain(ens, registrar, resolver, defaultAccount, subdomain, rootDomain, reverseNode, address, logger, secureSend, callback) {
|
||||
const subnode = namehash.hash(subdomain);
|
||||
const rootNode = namehash.hash(rootDomain);
|
||||
const node = namehash.hash(`${subdomain}.${rootDomain}`);
|
||||
// FIXME Registrar calls a function in ENS and in privatenet it doesn't work for soem reason
|
||||
// const toSend = registrar.methods.register(subnode, defaultAccount);
|
||||
const toSend = ens.methods.setSubnodeOwner(rootNode, subnode, defaultAccount);
|
||||
let transaction;
|
||||
|
||||
secureSend(web3, toSend, {from: defaultAccount, gas: ENS_GAS_PRICE}, false)
|
||||
// Set resolver for the node
|
||||
.then(transac => {
|
||||
if (transac.status !== "0x1" && transac.status !== "0x01" && transac.status !== true) {
|
||||
logger.warn('Failed transaction', transac);
|
||||
return callback('Failed to register. Check gas cost.');
|
||||
}
|
||||
transaction = transac;
|
||||
return secureSend(web3, ens.methods.setResolver(node, resolver.options.address), {from: defaultAccount, gas: ENS_GAS_PRICE}, false);
|
||||
})
|
||||
// Set address for node
|
||||
.then(_result => {
|
||||
return secureSend(web3, resolver.methods.setAddr(node, address), {from: defaultAccount, gas: ENS_GAS_PRICE}, false);
|
||||
})
|
||||
// Set resolver for the reverse node
|
||||
.then(_result => {
|
||||
return secureSend(web3, ens.methods.setResolver(reverseNode, resolver.options.address), {from: defaultAccount, gas: ENS_GAS_PRICE}, false);
|
||||
})
|
||||
// Set name for reverse node
|
||||
.then(_result => {
|
||||
return secureSend(web3, resolver.methods.setName(reverseNode, `${subdomain}.${rootDomain}`), {from: defaultAccount, gas: ENS_GAS_PRICE}, false);
|
||||
})
|
||||
.then(_result => {
|
||||
callback(null, transaction);
|
||||
})
|
||||
.catch(err => {
|
||||
logger.error(err.message || err);
|
||||
callback('Failed to register with error: ' + (err.message || err));
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof module !== 'undefined' && module.exports) {
|
||||
module.exports = registerSubDomain;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user