Skip to content

Instantly share code, notes, and snippets.

@AlwaysBCoding
Created February 19, 2017 21:46
Show Gist options
  • Save AlwaysBCoding/027a7e8fb3eaa59025c82d00f137e35f to your computer and use it in GitHub Desktop.
Save AlwaysBCoding/027a7e8fb3eaa59025c82d00f137e35f to your computer and use it in GitHub Desktop.
Ethereum Ðapp Development - Video 13 | Ethereum Name Service (ENS)
// Config
global.config = {
rpc: {
host: "localhost",
port: "8545"
}
}
// Load Libraries
global.solc = require("solc")
global.EthTx = require("ethereumjs-tx")
global.EthUtil = require("ethereumjs-util")
global.fs = require("fs")
global.Web3 = require("web3")
global.lodash = require("lodash")
global.SolidityFunction = require("web3/lib/web3/function")
// Connect Web3 Instance
// global.web3 = new Web3(new Web3.providers.HttpProvider(`http://${global.config.rpc.host}:${global.config.rpc.port}`))
// global.web3 = new Web3(new Web3.providers.HttpProvider(`https://mainnet.infura.io/EjLdRlni9SfrUBEnnvVt`))
global.web3 = new Web3(new Web3.providers.HttpProvider(`https://ropsten.infura.io/EjLdRlni9SfrUBEnnvVt`))
// ENS
// global.ENS = require("ethereum-ens")
global.ENS = require("./ensutils.js")
global.ens = new ENS(global.web3)
// Global Account Accessors
// global.acct1 = web3.eth.accounts[0]
// global.acct2 = web3.eth.accounts[1]
// global.acct3 = web3.eth.accounts[2]
// global.acct4 = web3.eth.accounts[3]
// global.acct5 = web3.eth.accounts[4]
// Helper Functions
class Helpers {
contractName(source) {
var re1 = /contract.*{/g
var re2 = /\s\w+\s/
return source.match(re1).pop().match(re2)[0].trim()
}
createContract(source, options={}) {
var compiled = solc.compile(source)
var contractName = this.contractName(source)
var bytecode = compiled["contracts"][contractName]["bytecode"]
var abi = JSON.parse(compiled["contracts"][contractName]["interface"])
var contract = global.web3.eth.contract(abi)
var gasEstimate = global.web3.eth.estimateGas({ data: bytecode })
var deployed = contract.new(Object.assign({
from: global.web3.eth.accounts[0],
value: global.web3.toWei(3, 'ether'),
data: bytecode,
gas: gasEstimate,
gasPrice: global.web3.eth.gasPrice
}, options), (error, result) => { })
return deployed
}
loadContract(name) {
var path = `./${name.toLowerCase()}.sol`
return fs.readFileSync(path, 'utf8')
}
contractObject(name) {
var source = this.loadContract(name)
var compiled = solc.compile(source)
var contractName = this.contractName(source)
var bytecode = compiled["contracts"][contractName]["bytecode"]
var abi = JSON.parse(compiled["contracts"][contractName]["interface"])
var contract = global.web3.eth.contract(abi)
return contract
}
deployedObject(name, address) {
var source = this.loadContract(name)
var compiled = solc.compile(source)
var contractName = this.contractName(source)
var bytecode = compiled["contracts"][contractName]["bytecode"]
var abi = JSON.parse(compiled["contracts"][contractName]["interface"])
var contract = global.web3.eth.contract(abi)
return contract.at(address)
}
signContractCall() {
var deployed = arguments['0'].deployed
var methodName = arguments['0'].methodName
var pKeyx = arguments['0'].pKeyx
var fromAddress = arguments['0'].fromAddress
var args = [...arguments]; var params = args.slice(1, args.length);
var solidityFunction = new global.SolidityFunction('', lodash.find(deployed.abi, { name: methodName }), '')
var payloadData = solidityFunction.toPayload(params).data
var rawTx = {
nonce: global.web3.toHex(global.web3.eth.getTransactionCount(fromAddress)),
gasPrice: global.web3.toHex(global.web3.eth.gasPrice),
gasLimit: global.web3.toHex(300000),
to: deployed.address,
from: fromAddress,
data: payloadData
}
var tx = new global.EthTx(rawTx)
tx.sign(pKeyx)
return tx.serialize().toString('hex')
}
callContract() {
var deployed = arguments['0'].deployed
var methodName = arguments['0'].methodName
var pKeyx = arguments['0'].pKeyx
var fromAddress = arguments['0'].fromAddress
var args = [...arguments]; var params = args.slice(1, args.length);
var solidityFunction = new global.SolidityFunction('', lodash.find(deployed.abi, { name: methodName }), '')
var payloadData = solidityFunction.toPayload(params).data
var rawTx = {
nonce: global.web3.toHex(global.web3.eth.getTransactionCount(fromAddress)),
gasPrice: global.web3.toHex(global.web3.eth.gasPrice),
gasLimit: global.web3.toHex(arguments['0'].gas || 300000),
to: deployed.address,
from: fromAddress,
data: payloadData
}
var tx = new global.EthTx(rawTx)
tx.sign(pKeyx)
var txData = tx.serialize().toString('hex')
global.web3.eth.sendRawTransaction(`0x${txData}`, (error, txHash) => {
if(error) {
console.log(`ERROR...`)
console.log(error)
} else {
console.log(`TxHash...`)
console.log(txHash)
}
})
return true
}
deployContract(name, options={}) {
var source = this.loadContract(name)
return this.createContract(source, options)
}
etherBalance(contract) {
switch(typeof(contract)) {
case "object":
if(contract.address) {
return global.web3.fromWei(global.web3.eth.getBalance(contract.address), 'ether').toNumber()
} else {
return new Error("cannot call getEtherBalance on an object that does not have a property 'address'")
}
break
case "string":
return global.web3.fromWei(global.web3.eth.getBalance(contract), 'ether').toNumber()
break
}
}
}
// Load Helpers into Decypher namespace
global.decypher = new Helpers()
// Start repl
require('repl').start({})
var ensContractABI = [
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "resolver",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "label",
"type": "bytes32"
},
{
"name": "owner",
"type": "address"
}
],
"name": "setSubnodeOwner",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "ttl",
"type": "uint64"
}
],
"name": "setTTL",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "ttl",
"outputs": [
{
"name": "",
"type": "uint64"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "resolver",
"type": "address"
}
],
"name": "setResolver",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "owner",
"type": "address"
}
],
"name": "setOwner",
"outputs": [],
"payable": false,
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "node",
"type": "bytes32"
},
{
"indexed": false,
"name": "owner",
"type": "address"
}
],
"name": "Transfer",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "node",
"type": "bytes32"
},
{
"indexed": true,
"name": "label",
"type": "bytes32"
},
{
"indexed": false,
"name": "owner",
"type": "address"
}
],
"name": "NewOwner",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "node",
"type": "bytes32"
},
{
"indexed": false,
"name": "resolver",
"type": "address"
}
],
"name": "NewResolver",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "node",
"type": "bytes32"
},
{
"indexed": false,
"name": "ttl",
"type": "uint64"
}
],
"name": "NewTTL",
"type": "event"
}
]
var auctionRegistrarContractABI = [
{
"constant": false,
"inputs": [
{
"name": "_hash",
"type": "bytes32"
}
],
"name": "releaseDeed",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "unhashedName",
"type": "string"
}
],
"name": "invalidateName",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "hash",
"type": "bytes32"
},
{
"name": "owner",
"type": "address"
},
{
"name": "value",
"type": "uint256"
},
{
"name": "salt",
"type": "bytes32"
}
],
"name": "shaBid",
"outputs": [
{
"name": "sealedBid",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "bytes32"
}
],
"name": "entries",
"outputs": [
{
"name": "status",
"type": "uint8"
},
{
"name": "deed",
"type": "address"
},
{
"name": "registrationDate",
"type": "uint256"
},
{
"name": "value",
"type": "uint256"
},
{
"name": "highestBid",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "ens",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_hash",
"type": "bytes32"
}
],
"name": "transferRegistrars",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "bytes32"
}
],
"name": "sealedBids",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_hash",
"type": "bytes32"
},
{
"name": "newOwner",
"type": "address"
}
],
"name": "transfer",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_hash",
"type": "bytes32"
}
],
"name": "finalizeAuction",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_hash",
"type": "bytes32"
},
{
"name": "_owner",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
},
{
"name": "_salt",
"type": "bytes32"
}
],
"name": "unsealBid",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "registryCreated",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "sealedBid",
"type": "bytes32"
}
],
"name": "newBid",
"outputs": [],
"payable": true,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "seal",
"type": "bytes32"
}
],
"name": "cancelBid",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_hashes",
"type": "bytes32[]"
}
],
"name": "startAuctions",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_hash",
"type": "bytes32"
}
],
"name": "startAuction",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "rootNode",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"inputs": [
{
"name": "_ens",
"type": "address"
},
{
"name": "_rootNode",
"type": "bytes32"
}
],
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "hash",
"type": "bytes32"
},
{
"indexed": false,
"name": "auctionExpiryDate",
"type": "uint256"
}
],
"name": "AuctionStarted",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "hash",
"type": "bytes32"
},
{
"indexed": false,
"name": "deposit",
"type": "uint256"
}
],
"name": "NewBid",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "hash",
"type": "bytes32"
},
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
},
{
"indexed": false,
"name": "status",
"type": "uint8"
}
],
"name": "BidRevealed",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "hash",
"type": "bytes32"
},
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
},
{
"indexed": false,
"name": "now",
"type": "uint256"
}
],
"name": "HashRegistered",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "hash",
"type": "bytes32"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "HashReleased",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "hash",
"type": "bytes32"
},
{
"indexed": true,
"name": "name",
"type": "string"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
},
{
"indexed": false,
"name": "now",
"type": "uint256"
}
],
"name": "HashInvalidated",
"type": "event"
}
]
var deedContractABI = [
{
"constant": true,
"inputs": [],
"name": "creationDate",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "destroyDeed",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "newOwner",
"type": "address"
}
],
"name": "setOwner",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "registrar",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "refundRatio",
"type": "uint256"
}
],
"name": "closeDeed",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "newRegistrar",
"type": "address"
}
],
"name": "setRegistrar",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "newValue",
"type": "uint256"
}
],
"name": "setBalance",
"outputs": [],
"payable": true,
"type": "function"
},
{
"inputs": [],
"type": "constructor"
},
{
"payable": true,
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"name": "newOwner",
"type": "address"
}
],
"name": "OwnerChanged",
"type": "event"
},
{
"anonymous": false,
"inputs": [],
"name": "DeedClosed",
"type": "event"
}
]
var fifsRegistrarABI = [
{
"constant": true,
"inputs": [],
"name": "ens",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "bytes32"
}
],
"name": "expiryTimes",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "subnode",
"type": "bytes32"
},
{
"name": "owner",
"type": "address"
}
],
"name": "register",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "rootNode",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"inputs": [
{
"name": "ensAddr",
"type": "address"
},
{
"name": "node",
"type": "bytes32"
}
],
"type": "constructor"
}
]
var resolverContractABI = [
{
"constant": true,
"inputs": [
{
"name": "interfaceID",
"type": "bytes4"
}
],
"name": "supportsInterface",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "addr",
"outputs": [
{
"name": "ret",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "kind",
"type": "bytes32"
}
],
"name": "has",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "addr",
"type": "address"
}
],
"name": "setAddr",
"outputs": [],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "node",
"type": "bytes32"
}
],
"name": "content",
"outputs": [
{
"name": "ret",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "node",
"type": "bytes32"
},
{
"name": "hash",
"type": "bytes32"
}
],
"name": "setContent",
"outputs": [],
"payable": false,
"type": "function"
},
{
"inputs": [
{
"name": "ensAddr",
"type": "address"
}
],
"type": "constructor"
},
{
"payable": false,
"type": "fallback"
}
]
var reverseRegistrarContractABI = [
{
"constant": false,
"inputs": [
{
"name": "owner",
"type": "address"
}
],
"name": "claim",
"outputs": [
{
"name": "node",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "ens",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "addr",
"type": "address"
}
],
"name": "node",
"outputs": [
{
"name": "ret",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "rootNode",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"payable": false,
"type": "function"
},
{
"inputs": [
{
"name": "ensAddr",
"type": "address"
},
{
"name": "node",
"type": "bytes32"
}
],
"payable": false,
"type": "constructor"
}
]
class ENS {
constructor(web3) {
this.web3 = web3;
this.ensContract = web3.eth.contract(ensContractABI);
this.ens = this.ensContract.at('0x112234455c3a32fd11230c42e7bccd4a84e02010');
this.auctionRegistrarContract = web3.eth.contract(auctionRegistrarContractABI);
this.ethRegistrar = this.auctionRegistrarContract.at(this.ens.owner(this.namehash('eth')));
this.deedContract = web3.eth.contract(deedContractABI);
this.fifsRegistrarContract = web3.eth.contract(fifsRegistrarABI);
this.testRegistrar = this.fifsRegistrarContract.at(this.ens.owner(this.namehash('test')));
this.resolverContract = web3.eth.contract(resolverContractABI);
this.publicResolver = this.resolverContract.at('0x4c641fb9bad9b60ef180c31f56051ce826d21a9a');
this.reverseRegistrarContract = web3.eth.contract(reverseRegistrarContractABI);
var reverseRegistrar = this.reverseRegistrarContract.at(this.ens.owner(this.namehash('addr.reverse')));
}
namehash(name) {
var node = '0x0000000000000000000000000000000000000000000000000000000000000000';
if(name != '') {
var labels = name.split(".");
for(var i = labels.length - 1; i >= 0; i--) {
node = this.web3.sha3(node + this.web3.sha3(labels[i]).slice(2), {encoding: 'hex'});
}
}
return node.toString();
}
getAddr(name) {
var node = this.namehash(name)
var resolverAddress = this.ens.resolver(node);
if(resolverAddress == '0x0000000000000000000000000000000000000000') {
return resolverAddress;
}
return this.resolverContract.at(resolverAddress).addr(node);
}
getContent(name) {
var node = this.namehash(name)
var resolverAddress = this.ens.resolver(node);
if(resolverAddress == '0x0000000000000000000000000000000000000000') {
return "0x0000000000000000000000000000000000000000000000000000000000000000";
}
return this.resolverContract.at(resolverAddress).content(node);
}
}
module.exports = ENS
// Private Key
23e4dfb937932332a3cf17f1fc0541e7dc2b7d0bef81289bfa04a251443b5666
// Ethereum Address
0x08cf02070bb9f167556c677da58e6678bbe871fc
// Etherscan Account
https://testnet.etherscan.io/address/0x08cf02070bb9f167556c677da58e6678bbe871fc
@sangheraio
Copy link

This script doesn't seem to be compatible with the new web3.

Error: Cannot find module 'web3/lib/web3/function'.

@analogrytm
Copy link

The new web3 broke a ton of my old code, feels like I'm having to relearn and debug the entire compiling and deploying part all over again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment