2016-08-26 12:00:25 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package ens
|
|
|
|
|
2019-03-20 08:33:24 +00:00
|
|
|
//go:generate abigen --sol contract/ENS.sol --pkg contract --out contract/ens.go
|
|
|
|
//go:generate abigen --sol contract/ENSRegistry.sol --exc contract/ENS.sol:ENS --pkg contract --out contract/ensregistry.go
|
|
|
|
//go:generate abigen --sol contract/FIFSRegistrar.sol --exc contract/ENS.sol:ENS --pkg contract --out contract/fifsregistrar.go
|
|
|
|
//go:generate abigen --sol contract/PublicResolver.sol --exc contract/ENS.sol:ENS --pkg contract --out contract/publicresolver.go
|
2016-08-26 12:00:25 +00:00
|
|
|
|
|
|
|
import (
|
2019-03-20 08:33:24 +00:00
|
|
|
"encoding/binary"
|
2016-08-26 12:00:25 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-08-29 17:34:44 +00:00
|
|
|
"github.com/ethereum/go-ethereum/contracts/ens/contract"
|
2019-03-20 08:33:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/contracts/ens/fallback_contract"
|
2016-08-26 12:00:25 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
)
|
|
|
|
|
2017-06-17 22:25:39 +00:00
|
|
|
var (
|
2019-03-20 08:33:24 +00:00
|
|
|
MainNetAddress = common.HexToAddress("0x314159265dD8dbb310642f98f50C066173C1259b")
|
|
|
|
TestNetAddress = common.HexToAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010")
|
|
|
|
contentHash_Interface_Id [4]byte
|
2017-06-17 22:25:39 +00:00
|
|
|
)
|
|
|
|
|
2019-03-20 08:33:24 +00:00
|
|
|
const contentHash_Interface_Id_Spec = 0xbc1c58d1
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
binary.BigEndian.PutUint32(contentHash_Interface_Id[:], contentHash_Interface_Id_Spec)
|
|
|
|
}
|
|
|
|
|
2018-04-30 23:05:24 +00:00
|
|
|
// ENS is the swarm domain name registry and resolver
|
2016-08-26 12:00:25 +00:00
|
|
|
type ENS struct {
|
|
|
|
*contract.ENSSession
|
|
|
|
contractBackend bind.ContractBackend
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewENS creates a struct exposing convenient high-level operations for interacting with
|
|
|
|
// the Ethereum Name Service.
|
|
|
|
func NewENS(transactOpts *bind.TransactOpts, contractAddr common.Address, contractBackend bind.ContractBackend) (*ENS, error) {
|
|
|
|
ens, err := contract.NewENS(contractAddr, contractBackend)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ENS{
|
|
|
|
&contract.ENSSession{
|
|
|
|
Contract: ens,
|
|
|
|
TransactOpts: *transactOpts,
|
|
|
|
},
|
|
|
|
contractBackend,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-06-12 12:45:17 +00:00
|
|
|
// DeployENS deploys an instance of the ENS nameservice, with a 'first-in, first-served' root registrar.
|
2018-01-08 12:15:57 +00:00
|
|
|
func DeployENS(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend) (common.Address, *ENS, error) {
|
2018-04-30 23:05:24 +00:00
|
|
|
// Deploy the ENS registry
|
2019-03-20 08:33:24 +00:00
|
|
|
ensAddr, _, _, err := contract.DeployENSRegistry(transactOpts, contractBackend)
|
2016-08-26 12:00:25 +00:00
|
|
|
if err != nil {
|
2018-01-08 12:15:57 +00:00
|
|
|
return ensAddr, nil, err
|
2016-08-26 12:00:25 +00:00
|
|
|
}
|
|
|
|
ens, err := NewENS(transactOpts, ensAddr, contractBackend)
|
|
|
|
if err != nil {
|
2018-01-08 12:15:57 +00:00
|
|
|
return ensAddr, nil, err
|
2016-08-26 12:00:25 +00:00
|
|
|
}
|
2018-04-30 23:05:24 +00:00
|
|
|
// Deploy the registrar
|
2016-08-26 12:00:25 +00:00
|
|
|
regAddr, _, _, err := contract.DeployFIFSRegistrar(transactOpts, contractBackend, ensAddr, [32]byte{})
|
|
|
|
if err != nil {
|
2018-01-08 12:15:57 +00:00
|
|
|
return ensAddr, nil, err
|
2016-08-26 12:00:25 +00:00
|
|
|
}
|
2018-04-30 23:05:24 +00:00
|
|
|
// Set the registrar as owner of the ENS root
|
2018-01-08 12:15:57 +00:00
|
|
|
if _, err = ens.SetOwner([32]byte{}, regAddr); err != nil {
|
|
|
|
return ensAddr, nil, err
|
2016-08-26 12:00:25 +00:00
|
|
|
}
|
2018-01-08 12:15:57 +00:00
|
|
|
return ensAddr, ens, nil
|
2016-08-26 12:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ensParentNode(name string) (common.Hash, common.Hash) {
|
|
|
|
parts := strings.SplitN(name, ".", 2)
|
|
|
|
label := crypto.Keccak256Hash([]byte(parts[0]))
|
|
|
|
if len(parts) == 1 {
|
|
|
|
return [32]byte{}, label
|
|
|
|
}
|
2018-04-30 23:05:24 +00:00
|
|
|
parentNode, parentLabel := ensParentNode(parts[1])
|
|
|
|
return crypto.Keccak256Hash(parentNode[:], parentLabel[:]), label
|
2016-08-26 12:00:25 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 09:21:17 +00:00
|
|
|
func EnsNode(name string) common.Hash {
|
2016-08-26 12:00:25 +00:00
|
|
|
parentNode, parentLabel := ensParentNode(name)
|
|
|
|
return crypto.Keccak256Hash(parentNode[:], parentLabel[:])
|
|
|
|
}
|
|
|
|
|
2018-04-30 23:05:24 +00:00
|
|
|
func (ens *ENS) getResolver(node [32]byte) (*contract.PublicResolverSession, error) {
|
|
|
|
resolverAddr, err := ens.Resolver(node)
|
2016-08-26 12:00:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-30 23:05:24 +00:00
|
|
|
resolver, err := contract.NewPublicResolver(resolverAddr, ens.contractBackend)
|
2016-08-26 12:00:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &contract.PublicResolverSession{
|
|
|
|
Contract: resolver,
|
2018-04-30 23:05:24 +00:00
|
|
|
TransactOpts: ens.TransactOpts,
|
2016-08-26 12:00:25 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-20 08:33:24 +00:00
|
|
|
func (ens *ENS) getFallbackResolver(node [32]byte) (*fallback_contract.PublicResolverSession, error) {
|
|
|
|
resolverAddr, err := ens.Resolver(node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resolver, err := fallback_contract.NewPublicResolver(resolverAddr, ens.contractBackend)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &fallback_contract.PublicResolverSession{
|
|
|
|
Contract: resolver,
|
|
|
|
TransactOpts: ens.TransactOpts,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-04-30 23:05:24 +00:00
|
|
|
func (ens *ENS) getRegistrar(node [32]byte) (*contract.FIFSRegistrarSession, error) {
|
|
|
|
registrarAddr, err := ens.Owner(node)
|
2016-08-26 12:00:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-30 23:05:24 +00:00
|
|
|
registrar, err := contract.NewFIFSRegistrar(registrarAddr, ens.contractBackend)
|
2016-08-26 12:00:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &contract.FIFSRegistrarSession{
|
|
|
|
Contract: registrar,
|
2018-04-30 23:05:24 +00:00
|
|
|
TransactOpts: ens.TransactOpts,
|
2016-08-26 12:00:25 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve is a non-transactional call that returns the content hash associated with a name.
|
2018-04-30 23:05:24 +00:00
|
|
|
func (ens *ENS) Resolve(name string) (common.Hash, error) {
|
2018-06-14 09:21:17 +00:00
|
|
|
node := EnsNode(name)
|
2016-08-26 12:00:25 +00:00
|
|
|
|
2018-04-30 23:05:24 +00:00
|
|
|
resolver, err := ens.getResolver(node)
|
2016-08-26 12:00:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
2019-03-20 08:33:24 +00:00
|
|
|
|
|
|
|
// IMPORTANT: The old contract is deprecated. This code should be removed latest on June 1st 2019
|
|
|
|
supported, err := resolver.SupportsInterface(contentHash_Interface_Id)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !supported {
|
|
|
|
resolver, err := ens.getFallbackResolver(node)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
ret, err := resolver.Content(node)
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
return common.BytesToHash(ret[:]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// END DEPRECATED CODE
|
|
|
|
|
|
|
|
contentHash, err := resolver.Contenthash(node)
|
2016-08-26 12:00:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
2019-03-20 08:33:24 +00:00
|
|
|
|
|
|
|
return extractContentHash(contentHash)
|
2016-08-26 12:00:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 20:46:41 +00:00
|
|
|
// Addr is a non-transactional call that returns the address associated with a name.
|
2018-04-30 23:05:24 +00:00
|
|
|
func (ens *ENS) Addr(name string) (common.Address, error) {
|
2018-09-28 20:46:41 +00:00
|
|
|
node := EnsNode(name)
|
|
|
|
|
2018-04-30 23:05:24 +00:00
|
|
|
resolver, err := ens.getResolver(node)
|
2018-09-28 20:46:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
ret, err := resolver.Addr(node)
|
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, err
|
|
|
|
}
|
|
|
|
return common.BytesToAddress(ret[:]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress sets the address associated with a name. Only works if the caller
|
|
|
|
// owns the name, and the associated resolver implements a `setAddress` function.
|
2018-04-30 23:05:24 +00:00
|
|
|
func (ens *ENS) SetAddr(name string, addr common.Address) (*types.Transaction, error) {
|
2018-09-28 20:46:41 +00:00
|
|
|
node := EnsNode(name)
|
|
|
|
|
2018-04-30 23:05:24 +00:00
|
|
|
resolver, err := ens.getResolver(node)
|
2018-09-28 20:46:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-30 23:05:24 +00:00
|
|
|
opts := ens.TransactOpts
|
2018-09-28 20:46:41 +00:00
|
|
|
opts.GasLimit = 200000
|
|
|
|
return resolver.Contract.SetAddr(&opts, node, addr)
|
|
|
|
}
|
|
|
|
|
2016-08-26 12:00:25 +00:00
|
|
|
// Register registers a new domain name for the caller, making them the owner of the new name.
|
|
|
|
// Only works if the registrar for the parent domain implements the FIFS registrar protocol.
|
2018-04-30 23:05:24 +00:00
|
|
|
func (ens *ENS) Register(name string) (*types.Transaction, error) {
|
2016-08-26 12:00:25 +00:00
|
|
|
parentNode, label := ensParentNode(name)
|
2018-04-30 23:05:24 +00:00
|
|
|
registrar, err := ens.getRegistrar(parentNode)
|
2016-08-26 12:00:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-30 23:05:24 +00:00
|
|
|
return registrar.Contract.Register(&ens.TransactOpts, label, ens.TransactOpts.From)
|
2016-08-26 12:00:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetContentHash sets the content hash associated with a name. Only works if the caller
|
2019-03-20 08:33:24 +00:00
|
|
|
// owns the name, and the associated resolver implements a `setContenthash` function.
|
|
|
|
func (ens *ENS) SetContentHash(name string, hash []byte) (*types.Transaction, error) {
|
2018-06-14 09:21:17 +00:00
|
|
|
node := EnsNode(name)
|
2016-08-26 12:00:25 +00:00
|
|
|
|
2018-04-30 23:05:24 +00:00
|
|
|
resolver, err := ens.getResolver(node)
|
2016-08-26 12:00:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-20 08:33:24 +00:00
|
|
|
|
2018-04-30 23:05:24 +00:00
|
|
|
opts := ens.TransactOpts
|
2017-11-13 11:47:27 +00:00
|
|
|
opts.GasLimit = 200000
|
2019-03-20 08:33:24 +00:00
|
|
|
|
|
|
|
// IMPORTANT: The old contract is deprecated. This code should be removed latest on June 1st 2019
|
|
|
|
supported, err := resolver.SupportsInterface(contentHash_Interface_Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !supported {
|
|
|
|
resolver, err := ens.getFallbackResolver(node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opts := ens.TransactOpts
|
|
|
|
opts.GasLimit = 200000
|
|
|
|
var b [32]byte
|
|
|
|
copy(b[:], hash)
|
|
|
|
return resolver.Contract.SetContent(&opts, node, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// END DEPRECATED CODE
|
|
|
|
return resolver.Contract.SetContenthash(&opts, node, hash)
|
2016-08-26 12:00:25 +00:00
|
|
|
}
|