Merge pull request #958 from embark-framework/bug_fix/no-ens-compile

Do not Compile ENS contracts, use known code
This commit is contained in:
Iuri Matias 2018-10-04 15:24:15 -04:00 committed by GitHub
commit bfd0118e65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 847 additions and 485 deletions

View File

@ -15,7 +15,6 @@ function log(eventType, eventName) {
if (eventType.indexOf("log") >= 0) {
return;
}
//console.log(eventType, eventName);
}
EventEmitter.prototype._maxListeners = 350;

View File

@ -1,26 +0,0 @@
pragma solidity ^0.4.18;
interface ENS {
// Logged when the owner of a node assigns a new owner to a subnode.
event NewOwner(bytes32 indexed node, bytes32 indexed label, address owner);
// Logged when the owner of a node transfers ownership to a new account.
event Transfer(bytes32 indexed node, address owner);
// Logged when the resolver for a node changes.
event NewResolver(bytes32 indexed node, address resolver);
// Logged when the TTL of a node changes
event NewTTL(bytes32 indexed node, uint64 ttl);
function setSubnodeOwner(bytes32 node, bytes32 label, address owner) external;
function setResolver(bytes32 node, address resolver) external;
function setOwner(bytes32 node, address owner) external;
function setTTL(bytes32 node, uint64 ttl) external;
function owner(bytes32 node) external view returns (address);
function resolver(bytes32 node) external view returns (address);
function ttl(bytes32 node) external view returns (uint64);
}

View File

@ -1,99 +0,0 @@
pragma solidity ^0.4.18;
import './ENS.sol';
/**
* The ENS registry contract.
*/
contract ENSRegistry is ENS {
struct Record {
address owner;
address resolver;
uint64 ttl;
}
mapping (bytes32 => Record) records;
// Permits modifications only by the owner of the specified node.
modifier only_owner(bytes32 node, address owner) {
require(records[node].owner == 0 || records[node].owner == msg.sender || records[node].owner == owner);
_;
}
/**
* @dev Constructs a new ENS registrar.
*/
constructor() public {
records[0x0].owner = msg.sender;
}
/**
* @dev Transfers ownership of a node to a new address. May only be called by the current owner of the node.
* @param node The node to transfer ownership of.
* @param owner The address of the new owner.
*/
function setOwner(bytes32 node, address owner) public only_owner(node, owner) {
emit Transfer(node, owner);
records[node].owner = owner;
}
/**
* @dev Transfers ownership of a subnode sha3(node, label) to a new address. May only be called by the owner of the parent node.
* @param node The parent node.
* @param label The hash of the label specifying the subnode.
* @param owner The address of the new owner.
*/
function setSubnodeOwner(bytes32 node, bytes32 label, address owner) public only_owner(node, owner) {
bytes32 subnode = keccak256(abi.encodePacked(node, label));
emit NewOwner(node, label, owner);
records[subnode].owner = owner;
}
/**
* @dev Sets the resolver address for the specified node.
* @param node The node to update.
* @param resolver The address of the resolver.
*/
function setResolver(bytes32 node, address resolver) public only_owner(node, 0x0) {
emit NewResolver(node, resolver);
records[node].resolver = resolver;
}
/**
* @dev Sets the TTL for the specified node.
* @param node The node to update.
* @param ttl The TTL in seconds.
*/
function setTTL(bytes32 node, uint64 ttl) public only_owner(node, 0x0) {
emit NewTTL(node, ttl);
records[node].ttl = ttl;
}
/**
* @dev Returns the address that owns the specified node.
* @param node The specified node.
* @return address of the owner.
*/
function owner(bytes32 node) public view returns (address) {
return records[node].owner;
}
/**
* @dev Returns the address of the resolver for the specified node.
* @param node The specified node.
* @return address of the resolver.
*/
function resolver(bytes32 node) public view returns (address) {
return records[node].resolver;
}
/**
* @dev Returns the TTL of a node, and any records associated with it.
* @param node The specified node.
* @return ttl of the node.
*/
function ttl(bytes32 node) public view returns (uint64) {
return records[node].ttl;
}
}

View File

@ -1,38 +0,0 @@
pragma solidity ^0.4.18;
import './ENS.sol';
import './Resolver.sol';
/**
* A registrar that allocates subdomains to the first person to claim them.
*/
contract FIFSRegistrar {
ENS ens;
bytes32 rootNode;
modifier only_owner(bytes32 subnode) {
bytes32 node = keccak256(abi.encodePacked(rootNode, subnode));
address currentOwner = ens.owner(node);
require(currentOwner == 0 || currentOwner == msg.sender);
_;
}
/**
* Constructor.
* @param ensAddr The address of the ENS registry.
* @param node The node that this registrar administers.
*/
constructor(ENS ensAddr, bytes32 node) public {
ens = ensAddr;
rootNode = node;
}
/**
* Register a name, or change the owner of an existing registration.
* @param subnode The hash of the label to register.
* @param owner The address of the new owner.
*/
function register(bytes32 subnode, address owner) public only_owner(subnode) {
ens.setSubnodeOwner(rootNode, subnode, owner);
}
}

View File

@ -1,193 +0,0 @@
pragma solidity ^0.4.23;
import "./ENS.sol";
/**
* A simple resolver anyone can use; only allows the owner of a node to set its
* address.
*/
contract Resolver {
event AddrChanged(bytes32 indexed node, address a);
event ContentChanged(bytes32 indexed node, bytes32 hash);
event NameChanged(bytes32 indexed node, string name);
event ABIChanged(bytes32 indexed node, uint256 indexed contentType);
event PubkeyChanged(bytes32 indexed node, bytes32 x, bytes32 y);
event TextChanged(bytes32 indexed node, string indexedKey, string key);
struct PublicKey {
bytes32 x;
bytes32 y;
}
struct Record {
address addr;
bytes32 content;
string name;
PublicKey pubkey;
mapping(string=>string) text;
mapping(uint256=>bytes) abis;
}
ENS ens;
mapping (bytes32 => Record) records;
modifier only_owner(bytes32 node) {
// FIXME Calling ens.owner makes the transaction fail on privatenet for some reason
// address currentOwner = ens.owner(node);
// require(currentOwner == 0 || currentOwner == msg.sender);
require(true == true);
_;
}
/**
* Constructor.
* @param ensAddr The ENS registrar contract.
*/
constructor(ENS ensAddr) public {
ens = ensAddr;
}
/**
* Sets the address associated with an ENS node.
* May only be called by the owner of that node in the ENS registry.
* @param node The node to update.
* @param addr The address to set.
*/
function setAddr(bytes32 node, address addr) public only_owner(node) {
records[node].addr = addr;
emit AddrChanged(node, addr);
}
/**
* Sets the content hash associated with an ENS node.
* May only be called by the owner of that node in the ENS registry.
* Note that this resource type is not standardized, and will likely change
* in future to a resource type based on multihash.
* @param node The node to update.
* @param hash The content hash to set
*/
function setContent(bytes32 node, bytes32 hash) public only_owner(node) {
records[node].content = hash;
emit ContentChanged(node, hash);
}
/**
* Sets the name associated with an ENS node, for reverse records.
* May only be called by the owner of that node in the ENS registry.
* @param node The node to update.
* @param name The name to set.
*/
function setName(bytes32 node, string name) public only_owner(node) {
records[node].name = name;
emit NameChanged(node, name);
}
/**
* Sets the ABI associated with an ENS node.
* Nodes may have one ABI of each content type. To remove an ABI, set it to
* the empty string.
* @param node The node to update.
* @param contentType The content type of the ABI
* @param data The ABI data.
*/
function setABI(bytes32 node, uint256 contentType, bytes data) public only_owner(node) {
// Content types must be powers of 2
require(((contentType - 1) & contentType) == 0);
records[node].abis[contentType] = data;
emit ABIChanged(node, contentType);
}
/**
* Sets the SECP256k1 public key associated with an ENS node.
* @param node The ENS node to query
* @param x the X coordinate of the curve point for the public key.
* @param y the Y coordinate of the curve point for the public key.
*/
function setPubkey(bytes32 node, bytes32 x, bytes32 y) public only_owner(node) {
records[node].pubkey = PublicKey(x, y);
emit PubkeyChanged(node, x, y);
}
/**
* Sets the text data associated with an ENS node and key.
* May only be called by the owner of that node in the ENS registry.
* @param node The node to update.
* @param key The key to set.
* @param value The text data value to set.
*/
function setText(bytes32 node, string key, string value) public only_owner(node) {
records[node].text[key] = value;
emit TextChanged(node, key, key);
}
/**
* Returns the text data associated with an ENS node and key.
* @param node The ENS node to query.
* @param key The text data key to query.
* @return The associated text data.
*/
function text(bytes32 node, string key) public view returns (string) {
return records[node].text[key];
}
/**
* Returns the SECP256k1 public key associated with an ENS node.
* Defined in EIP 619.
* @param node The ENS node to query
* @return x, y the X and Y coordinates of the curve point for the public key.
*/
function pubkey(bytes32 node) public view returns (bytes32 x, bytes32 y) {
return (records[node].pubkey.x, records[node].pubkey.y);
}
/**
* Returns the ABI associated with an ENS node.
* Defined in EIP205.
* @param node The ENS node to query
* @param contentTypes A bitwise OR of the ABI formats accepted by the caller.
* @return contentType The content type of the return value
* @return data The ABI data
*/
function ABI(bytes32 node, uint256 contentTypes) public view returns (uint256 contentType, bytes data) {
Record storage record = records[node];
for (contentType = 1; contentType <= contentTypes; contentType <<= 1) {
if ((contentType & contentTypes) != 0 && record.abis[contentType].length > 0) {
data = record.abis[contentType];
return;
}
}
contentType = 0;
}
/**
* Returns the name associated with an ENS node, for reverse records.
* Defined in EIP181.
* @param node The ENS node to query.
* @return The associated name.
*/
function name(bytes32 node) public view returns (string) {
return records[node].name;
}
/**
* Returns the content hash associated with an ENS node.
* Note that this resource type is not standardized, and will likely change
* in future to a resource type based on multihash.
* @param node The ENS node to query.
* @return The associated content hash.
*/
function content(bytes32 node) public view returns (bytes32) {
return records[node].content;
}
/**
* Returns the address associated with an ENS node.
* @param node The ENS node to query.
* @return The associated address.
*/
function addr(bytes32 node) public view returns (address) {
return records[node].addr;
}
}

File diff suppressed because one or more lines are too long

View File

@ -5,75 +5,45 @@ const async = require('async');
const embarkJsUtils = require('embarkjs').Utils;
const reverseAddrSuffix = '.addr.reverse';
const DEFAULT_ENS_CONTRACTS_CONFIG = {
"default": {
"contracts": {
"ENS": {
"deploy": false,
"silent": true
},
"ENSRegistry": {
"deploy": true,
"silent": true,
"args": []
},
"Resolver": {
"deploy": true,
"silent": true,
"args": ["$ENSRegistry"]
},
"FIFSRegistrar": {
"deploy": false
}
}
}
};
const MAINNET_ID = '1';
const ROPSTEN_ID = '3';
const RINKEBY_ID = '4';
const ENS_CONTRACTS_CONFIG = {
[MAINNET_ID]: {
"contracts": {
"ENSRegistry": {
"address": "0x314159265dd8dbb310642f98f50c066173c1259b",
"silent": true
},
"Resolver": {
"deploy": false
},
"FIFSRegistrar": {
"deploy": false
}
"ENSRegistry": {
"address": "0x314159265dd8dbb310642f98f50c066173c1259b",
"silent": true
},
"Resolver": {
"deploy": false
},
"FIFSRegistrar": {
"deploy": false
}
},
[ROPSTEN_ID]: {
"contracts": {
"ENSRegistry": {
"address": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
"silent": true
},
"Resolver": {
"deploy": false
},
"FIFSRegistrar": {
"deploy": false
}
"ENSRegistry": {
"address": "0x112234455c3a32fd11230c42e7bccd4a84e02010",
"silent": true
},
"Resolver": {
"deploy": false
},
"FIFSRegistrar": {
"deploy": false
}
},
[RINKEBY_ID]: {
"contracts": {
"ENSRegistry": {
"address": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A",
"silent": true
},
"Resolver": {
"deploy": false
},
"FIFSRegistrar": {
"deploy": false
}
"ENSRegistry": {
"address": "0xe7410170f87102DF0055eB195163A03B7F2Bff4A",
"silent": true
},
"Resolver": {
"deploy": false
},
"FIFSRegistrar": {
"deploy": false
}
}
};
@ -86,6 +56,8 @@ class ENS {
this.namesConfig = embark.config.namesystemConfig;
this.registration = this.namesConfig.register || {};
this.embark = embark;
this.ensConfig = require('./ensContractConfigs');
this.configured = false;
if (this.namesConfig === {} ||
this.namesConfig.enabled !== true ||
@ -99,61 +71,36 @@ class ENS {
}
registerEvents() {
this.events.once("contracts:deploy:afterAll", this.setProviderAndRegisterDomains.bind(this));
this.events.once("web3Ready", this.configureContracts.bind(this));
this.embark.registerActionForEvent("deploy:beforeAll", this.configureContractsAndRegister.bind(this));
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) => {
if (err) {
return cb(err);
}
if (!results[0] || !results[1] || !results[2]) {
return cb(__('Aborting ENS setup as some of the contracts did not deploy properly'));
}
// 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
};
let config = {
env: self.env,
registration: self.registration,
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
};
if (self.doSetENSProvider) {
self.addSetProvider(config);
if (self.doSetENSProvider) {
self.addSetProvider(config);
}
self.events.request('blockchain:networkId', (networkId) => {
const isKnownNetwork = Boolean(ENS_CONTRACTS_CONFIG[networkId]);
const shouldRegisterSubdomain = self.registration && self.registration.subdomains && Object.keys(self.registration.subdomains).length;
if (isKnownNetwork || !shouldRegisterSubdomain) {
return cb();
}
self.events.request('blockchain:networkId', (networkId) => {
const isKnownNetwork = Object.keys(ENS_CONTRACTS_CONFIG).includes(networkId);
const shouldRegisterSubdomain = self.registration && self.registration.subdomains && Object.keys(self.registration.subdomains).length;
if (isKnownNetwork || !shouldRegisterSubdomain) {
return cb();
}
self.registerConfigDomains(config, cb);
});
self.registerConfigDomains(config, cb);
});
}
@ -291,33 +238,59 @@ class ENS {
this.embark.addCodeToEmbarkJS(code);
}
configureContracts() {
this.events.request('blockchain:networkId', (networkId) => {
const config = Object.assign({}, DEFAULT_ENS_CONTRACTS_CONFIG, {[this.env]: ENS_CONTRACTS_CONFIG[networkId]});
if (this.registration && this.registration.rootDomain) {
// Register root domain if it is defined
const rootNode = namehash.hash(this.registration.rootDomain);
config.default.contracts['FIFSRegistrar'] = {
"deploy": true,
"silent": true,
"args": ["$ENSRegistry", rootNode],
"onDeploy": [
`ENSRegistry.methods.setOwner('${rootNode}', web3.eth.defaultAccount).send({from: web3.eth.defaultAccount}).then(() => {
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});
})`
]
};
configureContractsAndRegister(cb) {
const self = this;
if (self.configured) {
return cb();
}
self.events.request('blockchain:networkId', (networkId) => {
if (ENS_CONTRACTS_CONFIG[networkId]) {
self.ensConfig = utils.recursiveMerge(self.ensConfig, ENS_CONTRACTS_CONFIG[networkId]);
}
this.embark.registerContractConfiguration(config);
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'));
async.waterfall([
function registry(next) {
self.events.request('deploy:contract', self.ensConfig.ENSRegistry, (err, _receipt) => {
return next(err);
});
},
function resolver(next) {
self.ensConfig.Resolver.args = [self.ensConfig.ENSRegistry.deployedAddress];
self.events.request('deploy:contract', self.ensConfig.Resolver, (err, _receipt) => {
return next(err);
});
},
function registrar(next) {
if (!self.registration || !self.registration.rootDomain) {
return next();
}
const registryAddress = self.ensConfig.ENSRegistry.deployedAddress;
const resolverAddress = self.ensConfig.Resolver.deployedAddress;
const rootNode = namehash.hash(self.registration.rootDomain);
const contract = self.ensConfig.FIFSRegistrar;
contract.args = [registryAddress, rootNode];
contract.onDeploy = [
`ENSRegistry.methods.setOwner('${rootNode}', web3.eth.defaultAccount).send({from: web3.eth.defaultAccount}).then(() => {
ENSRegistry.methods.setResolver('${rootNode}', "${resolverAddress}").send({from: web3.eth.defaultAccount});
var reverseNode = web3.utils.soliditySha3(web3.eth.defaultAccount.toLowerCase().substr(2) + '${reverseAddrSuffix}');
ENSRegistry.methods.setResolver(reverseNode, "${resolverAddress}").send({from: web3.eth.defaultAccount});
Resolver.methods.setAddr('${rootNode}', web3.eth.defaultAccount).send({from: web3.eth.defaultAccount});
Resolver.methods.setName(reverseNode, '${self.registration.rootDomain}').send({from: web3.eth.defaultAccount});
})`
];
self.events.request('deploy:contract', contract, (err, _receipt) => {
return next(err);
});
}
], (err) => {
self.configured = true;
if (err) {
self.logger.error('Error while deploying ENS contracts');
self.logger.error(err.message || err);
return;
}
self.setProviderAndRegisterDomains(cb);
});
});
}