embark/lib/modules/ens/index.js

326 lines
11 KiB
JavaScript
Raw Normal View History

const fs = require('../../core/fs.js');
const utils = require('../../utils/utils.js');
const namehash = require('eth-ens-namehash');
2018-07-26 18:50:38 +00:00
const async = require('async');
2018-08-14 14:09:59 +00:00
const embarkJsUtils = require('embarkjs').Utils;
2018-07-25 20:12:17 +00:00
const reverseAddrSuffix = '.addr.reverse';
class ENS {
2018-05-30 16:26:49 +00:00
constructor(embark, _options) {
2018-07-25 20:21:00 +00:00
this.env = embark.env;
this.isDev = embark.config.blockchainConfig.isDev;
this.logger = embark.logger;
this.events = embark.events;
2018-05-30 16:26:49 +00:00
this.namesConfig = embark.config.namesystemConfig;
this.registration = this.namesConfig.register || {};
this.embark = embark;
2018-08-03 12:50:42 +00:00
if (this.namesConfig === {} ||
this.namesConfig.enabled !== true ||
2018-08-03 19:15:01 +00:00
this.namesConfig.available_providers.indexOf('ens') < 0) {
2018-08-03 12:50:42 +00:00
return;
}
2018-08-06 12:47:27 +00:00
this.doSetENSProvider = this.namesConfig.provider === 'ens';
2018-08-03 12:50:42 +00:00
this.addENSToEmbarkJS();
2018-07-13 16:37:34 +00:00
this.configureContracts();
this.registerEvents();
}
registerEvents() {
this.embark.registerActionForEvent("contracts:deploy:afterAll", this.setProviderAndRegisterDomains.bind(this));
2018-07-31 14:33:10 +00:00
this.events.setCommandHandler("storage:ens:associate", this.associateStorageToEns.bind(this));
}
setProviderAndRegisterDomains(cb) {
const self = this;
async.parallel([
function getENSRegistry(paraCb) {
self.events.request('contracts:contract', "ENSRegistry", (contract) => {
paraCb(null, contract);
});
},
function getRegistrar(paraCb) {
self.events.request('contracts:contract', "FIFSRegistrar", (contract) => {
paraCb(null, contract);
});
},
function getResolver(paraCb) {
self.events.request('contracts:contract', "Resolver", (contract) => {
paraCb(null, contract);
});
}
], (err, results) => {
// result[0] => ENSRegistry; result[1] => FIFSRegistrar; result[2] => FIFSRegistrar
let config = {
env: self.env,
registration: self.registration,
registryAbi: results[0].abiDefinition,
registryAddress: results[0].deployedAddress,
registrarAbi: results[1].abiDefinition,
registrarAddress: results[1].deployedAddress,
resolverAbi: results[2].abiDefinition,
resolverAddress: results[2].deployedAddress
};
2018-08-06 12:47:27 +00:00
if (self.doSetENSProvider) {
self.addSetProvider(config);
}
if ((!self.isDev && self.env !== 'privatenet') || !self.registration || !self.registration.subdomains || !Object.keys(self.registration.subdomains).length) {
return cb();
}
self.registerConfigDomains(config, cb);
});
}
associateStorageToEns(options, cb) {
2018-07-31 14:33:10 +00:00
const self = this;
// Code inspired by https://github.com/monkybrain/ipfs-to-ens
2018-07-30 15:57:00 +00:00
const {name, storageHash} = options;
2018-07-31 14:33:10 +00:00
if (!utils.isValidEthDomain(name)) {
return cb('Invalid domain name ' + name);
}
2018-07-30 15:57:00 +00:00
let hashedName = namehash.hash(name);
let contentHash;
try {
2018-07-31 14:33:10 +00:00
contentHash = utils.hashTo32ByteHexString(storageHash);
} catch (e) {
return cb('Invalid IPFS hash');
}
// Set content
async.waterfall([
function getRegistryABI(next) {
self.events.request('contracts:contract', "ENSRegistry", (contract) => {
next(null, contract);
});
},
function createRegistryContract(contract, next) {
self.events.request("blockchain:contract:create",
{abi: contract.abiDefinition, address: contract.deployedAddress},
(resolver) => {
next(null, resolver);
2018-07-26 18:50:38 +00:00
});
},
function getResolverForName(registry, next) {
2018-07-30 15:57:00 +00:00
registry.methods.resolver(hashedName).call((err, resolverAddress) => {
if (err) {
return cb(err);
}
if (resolverAddress === '0x0000000000000000000000000000000000000000') {
return cb('Name not yet registered');
}
next(null, resolverAddress);
});
},
function getResolverABI(resolverAddress, next) {
self.events.request('contracts:contract', "Resolver", (contract) => {
next(null, resolverAddress, contract);
});
},
function createResolverContract(resolverAddress, contract, next) {
self.events.request("blockchain:contract:create",
{abi: contract.abiDefinition, address: resolverAddress},
(resolver) => {
next(null, resolver);
});
},
function getDefaultAccount(resolver, next) {
self.events.request("blockchain:defaultAccount:get", (defaultAccount) => {
next(null, resolver, defaultAccount);
});
},
function setContent(resolver, defaultAccount, next) {
2018-07-30 17:38:12 +00:00
resolver.methods.setContent(hashedName, contentHash).send({from: defaultAccount}).then((transaction) => {
2018-07-30 15:57:00 +00:00
if (transaction.status !== "0x1" && transaction.status !== "0x01" && transaction.status !== true) {
2018-07-30 19:49:58 +00:00
return next('Association failed. Status: ' + transaction.status);
2018-07-30 15:57:00 +00:00
}
next();
2018-07-30 17:38:12 +00:00
}).catch(next);
}
], cb);
}
registerConfigDomains(config, cb) {
const self = this;
const register = require('./register');
2018-08-14 14:09:59 +00:00
const secureSend = embarkJsUtils.secureSend;
self.events.request("blockchain:defaultAccount:get", (defaultAccount) => {
async.parallel([
function createRegistryContract(paraCb) {
self.events.request("blockchain:contract:create",
{abi: config.registryAbi, address: config.registryAddress},
(registry) => {
paraCb(null, registry);
});
},
function createRegistrarContract(paraCb) {
self.events.request("blockchain:contract:create",
{abi: config.registrarAbi, address: config.registrarAddress},
(registrar) => {
paraCb(null, registrar);
});
},
function createResolverContract(paraCb) {
self.events.request("blockchain:contract:create",
{abi: config.resolverAbi, address: config.resolverAddress},
(resolver) => {
paraCb(null, resolver);
});
}
2018-08-14 19:46:26 +00:00
], function(err, contracts) {
if (err) {
return cb(err);
}
const [ens, registrar, resolver] = contracts;
async.each(Object.keys(self.registration.subdomains), (subDomainName, eachCb) => {
const address = self.registration.subdomains[subDomainName];
2018-07-25 20:12:17 +00:00
const reverseNode = utils.soliditySha3(address.toLowerCase().substr(2) + reverseAddrSuffix);
register(ens, registrar, resolver, defaultAccount, subDomainName, self.registration.rootDomain,
reverseNode, address, self.logger, secureSend, eachCb);
}, cb);
});
});
}
addENSToEmbarkJS() {
const self = this;
// get namehash, import it into file
2018-08-14 19:46:26 +00:00
self.events.request("version:get:eth-ens-namehash", function(EnsNamehashVersion) {
let currentEnsNamehashVersion = require('../../../package.json').dependencies["eth-ens-namehash"];
if (EnsNamehashVersion !== currentEnsNamehashVersion) {
2018-08-14 19:46:26 +00:00
self.events.request("version:getPackageLocation", "eth-ens-namehash", EnsNamehashVersion, function(err, location) {
self.embark.registerImportFile("eth-ens-namehash", fs.dappPath(location));
});
}
});
let code = fs.readFileSync(utils.joinPath(__dirname, 'register.js')).toString();
code += "\n" + fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString();
code += "\nEmbarkJS.Names.registerProvider('ens', __embarkENS);";
this.embark.addCodeToEmbarkJS(code);
}
2018-07-13 16:37:34 +00:00
configureContracts() {
const config = {
"default": {
"gas": "auto",
"contracts": {
"ENS": {
"deploy": false,
"silent": true
}
}
},
"development": {
"contracts": {
"ENSRegistry": {
"deploy": true,
"silent": true,
"args": []
2018-07-13 16:37:34 +00:00
},
"Resolver": {
"deploy": true,
"silent": true,
2018-07-13 16:37:34 +00:00
"args": ["$ENSRegistry"]
},
"FIFSRegistrar": {
"deploy": false
}
}
},
"ropsten": {
"contracts": {
2018-07-13 16:37:34 +00:00
"ENSRegistry": {
"address": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
"silent": true
2018-07-13 16:37:34 +00:00
},
"Resolver": {
"deploy": false
},
"FIFSRegistrar": {
"deploy": false
}
}
},
"rinkeby": {
"contracts": {
2018-07-13 16:37:34 +00:00
"ENSRegistry": {
"address": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A",
"silent": true
2018-07-13 16:37:34 +00:00
},
"Resolver": {
"deploy": false
},
"FIFSRegistrar": {
"deploy": false
}
}
},
"livenet": {
"contracts": {
2018-07-13 16:37:34 +00:00
"ENSRegistry": {
"address": "0x314159265dd8dbb310642f98f50c066173c1259b",
"silent": true
2018-07-13 16:37:34 +00:00
},
"Resolver": {
"deploy": false
},
"FIFSRegistrar": {
"deploy": false
}
}
}
};
2018-08-14 19:46:26 +00:00
config.testnet = config.ropsten;
if (this.registration && this.registration.rootDomain) {
// Register root domain if it is defined
const rootNode = namehash.hash(this.registration.rootDomain);
config.development.contracts['FIFSRegistrar'] = {
"deploy": true,
"silent": true,
"args": ["$ENSRegistry", rootNode],
"onDeploy": [
`ENSRegistry.methods.setOwner('${rootNode}', web3.eth.defaultAccount).send({from: web3.eth.defaultAccount}).then(() => {
2018-08-23 08:23:51 +00:00
ENSRegistry.methods.setResolver('${rootNode}', "$Resolver").send({from: web3.eth.defaultAccount});
var reverseNode = web3.utils.soliditySha3(web3.eth.defaultAccount.toLowerCase().substr(2) + '${reverseAddrSuffix}');
ENSRegistry.methods.setResolver(reverseNode, "$Resolver").send({from: web3.eth.defaultAccount});
Resolver.methods.setAddr('${rootNode}', web3.eth.defaultAccount).send({from: web3.eth.defaultAccount});
Resolver.methods.setName(reverseNode, '${this.registration.rootDomain}').send({from: web3.eth.defaultAccount});
})`
]
};
}
config.privatenet = config.development;
this.embark.registerContractConfiguration(config);
2018-07-13 16:37:34 +00:00
if (this.isDev || this.env === 'privatenet') {
this.embark.events.request("config:contractsFiles:add", this.embark.pathToFile('./contracts/ENSRegistry.sol'));
this.embark.events.request("config:contractsFiles:add", this.embark.pathToFile('./contracts/FIFSRegistrar.sol'));
this.embark.events.request("config:contractsFiles:add", this.embark.pathToFile('./contracts/Resolver.sol'));
}
}
addSetProvider(config) {
let code = "\nEmbarkJS.Names.setProvider('ens'," + JSON.stringify(config) + ");";
let shouldInit = (namesConfig) => {
return (namesConfig.provider === 'ens' && namesConfig.enabled === true);
};
this.embark.addProviderInit('names', code, shouldInit);
}
}
module.exports = ENS;