diff options
Diffstat (limited to 'contracts/ens/ens.go')
-rw-r--r-- | contracts/ens/ens.go | 69 |
1 files changed, 27 insertions, 42 deletions
diff --git a/contracts/ens/ens.go b/contracts/ens/ens.go index b7448c471..b1841ab17 100644 --- a/contracts/ens/ens.go +++ b/contracts/ens/ens.go @@ -35,7 +35,7 @@ var ( TestNetAddress = common.HexToAddress("0x112234455c3a32fd11230c42e7bccd4a84e02010") ) -// swarm domain name registry and resolver +// ENS is the swarm domain name registry and resolver type ENS struct { *contract.ENSSession contractBackend bind.ContractBackend @@ -48,7 +48,6 @@ func NewENS(transactOpts *bind.TransactOpts, contractAddr common.Address, contra if err != nil { return nil, err } - return &ENS{ &contract.ENSSession{ Contract: ens, @@ -60,27 +59,24 @@ func NewENS(transactOpts *bind.TransactOpts, contractAddr common.Address, contra // DeployENS deploys an instance of the ENS nameservice, with a 'first-in, first-served' root registrar. func DeployENS(transactOpts *bind.TransactOpts, contractBackend bind.ContractBackend) (common.Address, *ENS, error) { - // Deploy the ENS registry. + // Deploy the ENS registry ensAddr, _, _, err := contract.DeployENS(transactOpts, contractBackend) if err != nil { return ensAddr, nil, err } - ens, err := NewENS(transactOpts, ensAddr, contractBackend) if err != nil { return ensAddr, nil, err } - - // Deploy the registrar. + // Deploy the registrar regAddr, _, _, err := contract.DeployFIFSRegistrar(transactOpts, contractBackend, ensAddr, [32]byte{}) if err != nil { return ensAddr, nil, err } - // Set the registrar as owner of the ENS root. + // Set the registrar as owner of the ENS root if _, err = ens.SetOwner([32]byte{}, regAddr); err != nil { return ensAddr, nil, err } - return ensAddr, ens, nil } @@ -89,10 +85,9 @@ func ensParentNode(name string) (common.Hash, common.Hash) { label := crypto.Keccak256Hash([]byte(parts[0])) if len(parts) == 1 { return [32]byte{}, label - } else { - parentNode, parentLabel := ensParentNode(parts[1]) - return crypto.Keccak256Hash(parentNode[:], parentLabel[:]), label } + parentNode, parentLabel := ensParentNode(parts[1]) + return crypto.Keccak256Hash(parentNode[:], parentLabel[:]), label } func EnsNode(name string) common.Hash { @@ -100,111 +95,101 @@ func EnsNode(name string) common.Hash { return crypto.Keccak256Hash(parentNode[:], parentLabel[:]) } -func (self *ENS) getResolver(node [32]byte) (*contract.PublicResolverSession, error) { - resolverAddr, err := self.Resolver(node) +func (ens *ENS) getResolver(node [32]byte) (*contract.PublicResolverSession, error) { + resolverAddr, err := ens.Resolver(node) if err != nil { return nil, err } - - resolver, err := contract.NewPublicResolver(resolverAddr, self.contractBackend) + resolver, err := contract.NewPublicResolver(resolverAddr, ens.contractBackend) if err != nil { return nil, err } - return &contract.PublicResolverSession{ Contract: resolver, - TransactOpts: self.TransactOpts, + TransactOpts: ens.TransactOpts, }, nil } -func (self *ENS) getRegistrar(node [32]byte) (*contract.FIFSRegistrarSession, error) { - registrarAddr, err := self.Owner(node) +func (ens *ENS) getRegistrar(node [32]byte) (*contract.FIFSRegistrarSession, error) { + registrarAddr, err := ens.Owner(node) if err != nil { return nil, err } - - registrar, err := contract.NewFIFSRegistrar(registrarAddr, self.contractBackend) + registrar, err := contract.NewFIFSRegistrar(registrarAddr, ens.contractBackend) if err != nil { return nil, err } - return &contract.FIFSRegistrarSession{ Contract: registrar, - TransactOpts: self.TransactOpts, + TransactOpts: ens.TransactOpts, }, nil } // Resolve is a non-transactional call that returns the content hash associated with a name. -func (self *ENS) Resolve(name string) (common.Hash, error) { +func (ens *ENS) Resolve(name string) (common.Hash, error) { node := EnsNode(name) - resolver, err := self.getResolver(node) + resolver, err := ens.getResolver(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 } // Addr is a non-transactional call that returns the address associated with a name. -func (self *ENS) Addr(name string) (common.Address, error) { +func (ens *ENS) Addr(name string) (common.Address, error) { node := EnsNode(name) - resolver, err := self.getResolver(node) + resolver, err := ens.getResolver(node) 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. -func (self *ENS) SetAddr(name string, addr common.Address) (*types.Transaction, error) { +func (ens *ENS) SetAddr(name string, addr common.Address) (*types.Transaction, error) { node := EnsNode(name) - resolver, err := self.getResolver(node) + resolver, err := ens.getResolver(node) if err != nil { return nil, err } - - opts := self.TransactOpts + opts := ens.TransactOpts opts.GasLimit = 200000 return resolver.Contract.SetAddr(&opts, node, addr) } // 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. -func (self *ENS) Register(name string) (*types.Transaction, error) { +func (ens *ENS) Register(name string) (*types.Transaction, error) { parentNode, label := ensParentNode(name) - registrar, err := self.getRegistrar(parentNode) + registrar, err := ens.getRegistrar(parentNode) if err != nil { return nil, err } - return registrar.Contract.Register(&self.TransactOpts, label, self.TransactOpts.From) + return registrar.Contract.Register(&ens.TransactOpts, label, ens.TransactOpts.From) } // SetContentHash sets the content hash associated with a name. Only works if the caller // owns the name, and the associated resolver implements a `setContent` function. -func (self *ENS) SetContentHash(name string, hash common.Hash) (*types.Transaction, error) { +func (ens *ENS) SetContentHash(name string, hash common.Hash) (*types.Transaction, error) { node := EnsNode(name) - resolver, err := self.getResolver(node) + resolver, err := ens.getResolver(node) if err != nil { return nil, err } - - opts := self.TransactOpts + opts := ens.TransactOpts opts.GasLimit = 200000 return resolver.Contract.SetContent(&opts, node, hash) } |