onens

package module
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2023 License: Apache-2.0 Imports: 38 Imported by: 1

README

go-1ns

Tag License GoDoc Travis CI codecov.io Go Report Card

Go module to simplify interacting with the 1 Name Service contracts. Initial version copied from go-ens

Table of Contents

Install

go-1ns is a standard Go module which can be installed with:

go get github.com/jw-1ns/go-1ns

Usage

go-1ns provides simple access to the 1 Name Service (1ns) contracts.

Resolution

The most commonly-used feature of 1ns is resolution: converting an 1ns name to an Ethereum address. go-1ns provides a simple call to allow this:

address, err := onens.Resolve(client, domain)

where client is a connection to an Harmony client and domain is the fully-qualified name you wish to resolve (e.g. foo.mydomain.country) (full examples for using this are given in the Example section below).

The reverse process, converting an address to an 1ns name, is just as simple:

domain, err := onens.ReverseResolve(client, address)

Note that if the address does not have a reverse resolution this will return "". If you just want a string version of an address for on-screen display then you can use onens.Format(), for example:

fmt.Printf("The address is %s\n", onens.Format(client, address))

This will carry out reverse resolution of the address and print the name if present; if not it will print a formatted version of the address.

Management of names

A top-level name is one that sits directly underneath .country, for example mydomain.country. Lower-level names, such as foo.mydomain.country are covered in the following section. go-1ns provides a simplified Name interface to manage top-level, removing the requirement to understand registrars, controllers, etc.

Starting out with names in go-1ns is easy:

client, err := ethclient.Dial("https://api.s0.t.hmny.io")
name, err := onens.NewName(client, "mydomain.country")

Addresses can be set and obtained using the address functions, for example to get an address:

COIN_TYPE_ETHEREUM := uint64(60)
address, err := name.Address(COIN_TYPE_ETHEREUM)

1ns supports addresses for multiple coin types; values of coin types can be found at https://github.com/satoshilabs/slips/blob/master/slip-0044.md

Registering and extending names

Most operations on a domain will involve setting resolvers and resolver information.

Management of subdomains

Because subdomains have their own registrars they do not work with the Name interface.

Example
package main

import (
	"fmt"

	"github.com/ethereum/go-ethereum/ethclient"
	onens "github.com/jw-1ns/go-1ns"
)

func main() {
	// Replace SECRET with your own access token for this example to work.
	client, err := ethclient.Dial("https://api.s0.t.hmny.io")
	if err != nil {
		panic(err)
	}

	// Resolve a name to an address.
	domain := "test.country"
	address, err := onens.Resolve(client, domain)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Address of %s is %s\n", domain, address.Hex())

	// Reverse resolve an address to a name.
	reverse, err := onens.ReverseResolve(client, address)
	if err != nil {
		panic(err)
	}
	if reverse == "" {
		fmt.Printf("%s has no reverse lookup\n", address.Hex())
	} else {
		fmt.Printf("Name of %s is %s\n", address.Hex(), reverse)
	}
}

Maintainers

John Whitton: @john_whitton.

Contribute

Contributions welcome. Please check out the issues.

License

Apache-2.0 © 2022 John Whitton

Documentation

Index

Constants

This section is empty.

Variables

View Source
var UnknownAddress = common.HexToAddress("00")

UnknownAddress is the address to which unknown entries resolve

Functions

func ContenthashToString

func ContenthashToString(bytes []byte) (string, error)

ContenthashToString turns EIP-1577 binary format in to EIP-1577 text format

func CreateRegistrySession

func CreateRegistrySession(chainID *big.Int, wallet *accounts.Wallet, account *accounts.Account, passphrase string, contract *registry.Contract, gasPrice *big.Int) *registry.ContractSession

CreateRegistrySession creates a session suitable for multiple calls

func DNSWireFormat added in v0.1.3

func DNSWireFormat(domain string) []byte

DNSWireFormat turns a domain name in to wire format

func DNSWireFormatDomainHash added in v0.1.3

func DNSWireFormatDomainHash(domain string) (hash [32]byte)

DNSWireFormatDomainHash hashes a domain name in wire format

func Domain

func Domain(domain string) string

Domain obtains the domain of an ENS name, including subdomains. It does this by removing everything up to and including the first period. For example, 'country' will return ”

'foo.country' will return 'country'
'bar.foo.country' will return 'foo.country'

func DomainLevel

func DomainLevel(name string) (level int)

DomainLevel calculates the level of the domain presented. A top-level domain (e.g. 'country') will be 0, a domain (e.g. 'foo.country') will be 1, a subdomain (e.g. 'bar.foo.country' will be 2, etc.

func DomainPart

func DomainPart(domain string, part int) (string, error)

DomainPart obtains a part of a name Positive parts start at the lowest-level of the domain and work towards the top-level domain. Negative parts start at the top-level domain and work towards the lowest-level domain. For example, with a domain bar.foo.com the following parts will be returned: Number | part

 1 |  bar
 2 |  foo
 3 |  com
-1 |  com
-2 |  foo
-3 |  bar

func LabelHash

func LabelHash(label string) (hash [32]byte, err error)

LabelHash generates a simple hash for a piece of a name.

func NameHash

func NameHash(name string) (hash [32]byte, err error)

NameHash generates a hash from a name that can be used to look up the name in ENS

func NormaliseDomain

func NormaliseDomain(domain string) (string, error)

NormaliseDomain turns ENS domain in to normal form

func NormaliseDomainStrict

func NormaliseDomainStrict(domain string) (string, error)

NormaliseDomainStrict turns ENS domain in to normal form, using strict DNS rules (e.g. no underscores)

func Normalize

func Normalize(input string) (output string, err error)

Normalize normalizes a name according to the ENS rules

func PublicResolverAddress

func PublicResolverAddress(backend bind.ContractBackend) (common.Address, error)

PublicResolverAddress obtains the address of the public resolver for a chain

func RegistrarContractAddress

func RegistrarContractAddress(backend bind.ContractBackend, domain string) (common.Address, error)

RegistrarContractAddress obtains the registrar contract address for a given domain

func RegistryContractAddress

func RegistryContractAddress(backend bind.ContractBackend) (common.Address, error)

RegistryContractAddress obtains the address of the registry contract for a chain. Get the Registry contract address from config

func RegistryContractFromRegistrar

func RegistryContractFromRegistrar(backend bind.ContractBackend, registrar *baseregistrar.Contract) (*registry.Contract, error)

RegistryContractFromRegistrar obtains the registry contract given an existing registrar contract

func Resolve

func Resolve(backend bind.ContractBackend, input string) (address common.Address, err error)

Resolve resolves an ENS name in to an Etheruem address This will return an error if the name is not found or otherwise 0

func SetResolver

func SetResolver(session *registry.ContractSession, name string, resolverAddr *common.Address) (*types.Transaction, error)

SetResolver sets the resolver for a name

func SetSubdomainOwner

func SetSubdomainOwner(session *registry.ContractSession, name string, subdomain string, ownerAddr *common.Address) (*types.Transaction, error)

SetSubdomainOwner sets the owner for a subdomain of a name

func StringToContenthash

func StringToContenthash(text string) ([]byte, error)

StringToContenthash turns EIP-1577 text format in to EIP-1577 binary format

func Tld

func Tld(domain string) string

Tld obtains the top-level domain of an ENS name

func UnqualifiedName

func UnqualifiedName(domain string, root string) (string, error)

UnqualifiedName strips the root from the domain and ensures the result is suitable as a name

Types

type BaseRegistrar

type BaseRegistrar struct {
	Contract     *baseregistrar.Contract
	ContractAddr common.Address
	// contains filtered or unexported fields
}

BaseRegistrar is the structure for the registrar

func NewBaseRegistrar

func NewBaseRegistrar(backend bind.ContractBackend, domain string) (*BaseRegistrar, error)

NewBaseRegistrar obtains the registrar contract for a given domain

func (*BaseRegistrar) Expiry

func (r *BaseRegistrar) Expiry(domain string) (*big.Int, error)

Expiry obtains the unix timestamp at which the registration expires.

func (*BaseRegistrar) Owner

func (r *BaseRegistrar) Owner(domain string) (common.Address, error)

Owner obtains the owner of the underlying token that represents the name.

func (*BaseRegistrar) Reclaim

func (r *BaseRegistrar) Reclaim(opts *bind.TransactOpts, domain string, newOwner common.Address) (*types.Transaction, error)

Reclaim reclaims a domain by the owner

func (*BaseRegistrar) RegisteredWith

func (r *BaseRegistrar) RegisteredWith(domain string) (string, error)

RegisteredWith returns "permanent" or "none" for the registrar on which this name is registered

func (*BaseRegistrar) SetOwner

func (r *BaseRegistrar) SetOwner(opts *bind.TransactOpts, domain string, newOwner common.Address) (*types.Transaction, error)

SetOwner sets the owner of the token holding the name

type DNSResolver added in v0.1.3

type DNSResolver struct {
	Contract     *publicresolver.Contract
	ContractAddr common.Address
	// contains filtered or unexported fields
}

DNSResolver is the structure for the DNS resolver contract

func NewDNSResolver added in v0.1.3

func NewDNSResolver(backend bind.ContractBackend, domain string) (*DNSResolver, error)

NewDNSResolver creates a new DNS resolver for a given domain

func NewDNSResolverAt added in v0.1.3

func NewDNSResolverAt(backend bind.ContractBackend, domain string, address common.Address) (*DNSResolver, error)

NewDNSResolverAt creates a new DNS resolver for a given domain at a given address

func (*DNSResolver) ClearRecords added in v0.1.4

func (r *DNSResolver) ClearRecords(opts *bind.TransactOpts) (*types.Transaction, error)

ClearRecords clears all records for a domain

func (*DNSResolver) HasRecords added in v0.1.3

func (r *DNSResolver) HasRecords(name string) (bool, error)

HasRecords returns true if the given name has any RRsets

func (*DNSResolver) Record added in v0.1.3

func (r *DNSResolver) Record(name string, rrType uint16) ([]byte, error)

Record obtains an RRSet for a name

func (*DNSResolver) SetRecords added in v0.1.3

func (r *DNSResolver) SetRecords(opts *bind.TransactOpts, data []byte) (*types.Transaction, error)

SetRecords sets one or more RRSets

func (*DNSResolver) SetZonehash added in v0.1.3

func (r *DNSResolver) SetZonehash(opts *bind.TransactOpts, zonehash []byte) (*types.Transaction, error)

SetZonehash sets the zone hash of the domain

func (*DNSResolver) Zonehash added in v0.1.3

func (r *DNSResolver) Zonehash() ([]byte, error)

Zonehash returns the zone hash of the domain

type Name

type Name struct {

	// Name is the fully-qualified name of an ENS domain e.g. foo.bar.country
	Name string
	// Domain is the domain of an ENS domain e.g. bar.country
	Domain string
	// Label is the name part of an ENS domain e.g. foo
	Label string
	// contains filtered or unexported fields
}

Name represents an ENS name, for example 'foo.bar.country'.

func NewName

func NewName(backend bind.ContractBackend, name string) (*Name, error)

NewName creates an ENS name structure. Note that this does not create the name on-chain.

func (*Name) Address

func (n *Name) Address(coinType uint64) ([]byte, error)

Address fetches the address of the name for a given coin type. Coin types are defined at https://github.com/satoshilabs/slips/blob/master/slip-0044.md

func (*Name) Controller

func (n *Name) Controller() (common.Address, error)

Controller obtains the controller for this name. The controller can carry out operations on the name such as setting records, but cannot transfer ultimate ownership of the name.

func (*Name) CreateSubdomain

func (n *Name) CreateSubdomain(label string, controller common.Address, opts *bind.TransactOpts) (*types.Transaction, error)

CreateSubdomain creates a subdomain on the name.

func (*Name) Expires

func (n *Name) Expires() (time.Time, error)

Expires obtain the time at which the registration for this name expires.

func (*Name) ExtendRegistration

func (n *Name) ExtendRegistration(opts *bind.TransactOpts) (*types.Transaction, error)

ExtendRegistration sends a transaction that extends the registration of the name.

func (*Name) IsRegistered

func (n *Name) IsRegistered() (bool, error)

IsRegistered returns true if the name is registered in the registrar

func (*Name) Reclaim

func (n *Name) Reclaim(opts *bind.TransactOpts) (*types.Transaction, error)

Reclaim reclaims controller rights by the registrant

func (*Name) RegisterStageOne

func (n *Name) RegisterStageOne(registrant common.Address, duration *big.Int, opts *bind.TransactOpts) (*types.Transaction, [32]byte, error)

RegisterStageOne sends a transaction that starts the registration process.

func (*Name) RegisterStageTwo

func (n *Name) RegisterStageTwo(registrant common.Address, duration *big.Int, secret [32]byte, opts *bind.TransactOpts) (*types.Transaction, error)

RegisterStageTwo sends a transaction that completes the registration process. The registrant must be the same as supplied in RegisterStageOne. The secret is that returned by RegisterStageOne. At least RegistrationInterval() time must have passed since the stage one transaction was mined for this to work.

func (*Name) Registrant

func (n *Name) Registrant() (common.Address, error)

Registrant obtains the registrant for this name.

func (*Name) RegistrationInterval

func (n *Name) RegistrationInterval() (time.Duration, error)

RegistrationInterval obtains the minimum interval between commit and reveal when registering this name.

func (*Name) RentCost

func (n *Name) RentCost() (*big.Int, error)

RentCost returns the cost of rent in Wei-per-second.

func (*Name) ResolverAddress

func (n *Name) ResolverAddress() (common.Address, error)

ResolverAddress fetches the address of the resolver contract for the name.

func (*Name) SetController

func (n *Name) SetController(controller common.Address, opts *bind.TransactOpts) (*types.Transaction, error)

SetController sets the controller for this name. The controller can carry out operations on the name such as setting records, but cannot transfer ultimate ownership of the name.

func (*Name) SetResolverAddress

func (n *Name) SetResolverAddress(address common.Address, opts *bind.TransactOpts) (*types.Transaction, error)

SetResolverAddress sets the resolver contract address for the name.

func (*Name) Transfer

func (n *Name) Transfer(registrant common.Address, opts *bind.TransactOpts) (*types.Transaction, error)

Transfer transfers the registration of this name to a new registrant.

type RegistrarController

type RegistrarController struct {
	Contract     *registrarcontroller.Contract
	ContractAddr common.Address
	// contains filtered or unexported fields
}

RegistrarController is the structure for the registrar controller contract

func NewRegistrarController

func NewRegistrarController(backend bind.ContractBackend, domain string) (*RegistrarController, error)

NewRegistrarController creates a new controller for a given domain

func NewRegistrarControllerAt

func NewRegistrarControllerAt(backend bind.ContractBackend, domain string, address common.Address) (*RegistrarController, error)

NewETHControllerAt creates a .eth controller at a given address

func (*RegistrarController) BaseNode

func (c *RegistrarController) BaseNode(opts *bind.CallOpts) ([32]byte, error)

"function baseNode() view returns (bytes32)", BaseNode retreives the base node

func (*RegistrarController) Basextension

func (c *RegistrarController) Basextension() (string, error)

"function baseExtension() view returns (string)", BaseExtension retrieves the baseExtension

func (*RegistrarController) Commit

func (c *RegistrarController) Commit(opts *bind.TransactOpts, domain string, owner common.Address, duration *big.Int, secret [32]byte) (*types.Transaction, error)

Commit sends a commitment to register a domain. func (_Contract *ContractCaller) MakeCommitment(opts *bind.CallOpts, name string, owner common.Address, duration *big.Int, secret [32]byte, resolver common.Address, data [][]byte, reverseRecord bool, fuses uint32, wrapperExpiry uint64) ([32]byte, error) {

func (*RegistrarController) CommitmentHash

func (c *RegistrarController) CommitmentHash(domain string, owner common.Address, duration *big.Int, secret [32]byte) (common.Hash, error)

"function makeCommitment(string,address,uint256,bytes32,address,bytes[],bool,uint32,uint64) pure returns (bytes32)", CommitmentHash returns the commitment hash for a label/owner/secret tuple

func (*RegistrarController) CommitmentTime

func (c *RegistrarController) CommitmentTime(domain string, owner common.Address, duration *big.Int, secret [32]byte) (*big.Int, error)

CommitmentTime states the time at which a commitment was registered on the blockchain.

func (*RegistrarController) Commitments

func (c *RegistrarController) Commitments(opts *bind.CallOpts, commitment [32]byte) (*big.Int, error)

"function commitments(bytes32) view returns (uint256)" Commitments returns the block timestamp of the commmitment

func (*RegistrarController) IsAvailable

func (c *RegistrarController) IsAvailable(domain string) (bool, error)

"function available(string) view returns (bool)", IsAvailable returns true if the domain is available for registration.

func (*RegistrarController) IsValid

func (c *RegistrarController) IsValid(domain string) (bool, error)

IsValid returns true if the domain is considered valid by the controller.

func (*RegistrarController) MaxCommitmentInterval

func (c *RegistrarController) MaxCommitmentInterval() (*big.Int, error)

"function maxCommitmentAge() view returns (uint256)",

func (*RegistrarController) MinCommitmentInterval

func (c *RegistrarController) MinCommitmentInterval() (*big.Int, error)

"function minCommitmentAge() view returns (uint256)", MinCommitmentInterval returns the minimum time that has to pass between a commit and reveal

func (*RegistrarController) MinRegistrationDuration

func (c *RegistrarController) MinRegistrationDuration() (time.Duration, error)
"function MIN_REGISTRATION_DURATION() view returns (uint256)",

MinRegistrationDuration returns the minimum duration for which a name can be registered

func (*RegistrarController) NameWrapper

func (c *RegistrarController) NameWrapper() (common.Address, error)

"function nameWrapper() view returns (address)",

func (*RegistrarController) Owner

func (c *RegistrarController) Owner() (common.Address, error)

"function owner() view returns (address)",

func (*RegistrarController) Prices

func (c *RegistrarController) Prices() (common.Address, error)

"function prices() view returns (address)",

func (*RegistrarController) RecoverFunds

func (c *RegistrarController) RecoverFunds(opts *bind.TransactOpts, token common.Address, to common.Address, amount *big.Int) (*types.Transaction, error)

"function recoverFunds(address,address,uint256)",

func (*RegistrarController) Register

func (c *RegistrarController) Register(opts *bind.TransactOpts, name string, owner common.Address, duration *big.Int, secret [32]byte, resolver common.Address, data [][]byte, reverseRecord bool, fuses uint32, wrapperExpiry uint64) (*types.Transaction, error)

"function register(string,address,uint256,bytes32,address,bytes[],bool,uint32,uint64) payable",

func (*RegistrarController) Renew

func (c *RegistrarController) Renew(opts *bind.TransactOpts, domain string) (*types.Transaction, error)

Renew renews a registered domain.

func (*RegistrarController) RenewWithFuses

func (c *RegistrarController) RenewWithFuses(opts *bind.TransactOpts, name string, duration *big.Int, fuses uint32, wrapperExpiry uint64) (*types.Transaction, error)

"function renewWithFuses(string,uint256,uint32,uint64) payable",

func (*RegistrarController) RenounceOwnership

func (c *RegistrarController) RenounceOwnership(opts *bind.TransactOpts) (*types.Transaction, error)

"function renounceOwnership()",

func (*RegistrarController) RentCost

func (c *RegistrarController) RentCost(domain string) (*big.Int, error)

RentCost returns the cost of rent in wei-per-second.

func (*RegistrarController) RentPrice

func (c *RegistrarController) RentPrice(opts *bind.CallOpts, name string, duration *big.Int) (registrarcontroller.IPriceOraclePrice, error)

"function rentPrice(string,uint256) view returns (tuple(uint256,uint256))",

func (*RegistrarController) Reveal

func (c *RegistrarController) Reveal(opts *bind.TransactOpts, domain string, owner common.Address, duration *big.Int, secret [32]byte) (*types.Transaction, error)

Reveal reveals a commitment to register a domain.

func (*RegistrarController) ReverseRegistrar

func (c *RegistrarController) ReverseRegistrar() (common.Address, error)

"function reverseRegistrar() view returns (address)",

func (*RegistrarController) SupportsInterface

func (c *RegistrarController) SupportsInterface(opts *bind.CallOpts, interfaceID [4]byte) (bool, error)

"function supportsInterface(bytes4) pure returns (bool)",

func (*RegistrarController) TransferOwnership

func (c *RegistrarController) TransferOwnership(opts *bind.TransactOpts, newOwner common.Address) (*types.Transaction, error)

"function transferOwnership(address)",

func (*RegistrarController) Withdraw

"function withdraw()"

type Registry

type Registry struct {
	Contract     *registry.Contract
	ContractAddr common.Address
	// contains filtered or unexported fields
}

Registry is the structure for the registry contract

func NewRegistry

func NewRegistry(backend bind.ContractBackend) (*Registry, error)

NewRegistry obtains the ENS registry

func (*Registry) Owner

func (r *Registry) Owner(name string) (common.Address, error)

Owner returns the address of the owner of a name

func (*Registry) Resolver

func (r *Registry) Resolver(name string) (*Resolver, error)

Resolver returns the resolver for a name

func (*Registry) ResolverAddress

func (r *Registry) ResolverAddress(name string) (common.Address, error)

ResolverAddress returns the address of the resolver for a name

func (*Registry) SetOwner

func (r *Registry) SetOwner(opts *bind.TransactOpts, name string, address common.Address) (*types.Transaction, error)

SetOwner sets the ownership of a domain

func (*Registry) SetResolver

func (r *Registry) SetResolver(opts *bind.TransactOpts, name string, address common.Address) (*types.Transaction, error)

SetResolver sets the resolver for a name

func (*Registry) SetSubdomainOwner

func (r *Registry) SetSubdomainOwner(opts *bind.TransactOpts, name string, subname string, address common.Address) (*types.Transaction, error)

SetSubdomainOwner sets the ownership of a subdomain, potentially creating it in the process

type Resolver

type Resolver struct {
	Contract     *publicresolver.Contract
	ContractAddr common.Address
	// contains filtered or unexported fields
}

Resolver is the structure for the public resolver contract

func NewResolver

func NewResolver(backend bind.ContractBackend, domain string) (*Resolver, error)

NewResolver obtains an Public resolver for a given domain

func NewResolverAt

func NewResolverAt(backend bind.ContractBackend, domain string, address common.Address) (*Resolver, error)

NewResolverAt obtains an ENS resolver at a given address

func (*Resolver) ABI

func (r *Resolver) ABI(name string) (string, error)

ABI returns the ABI associated with a name

func (*Resolver) Address

func (r *Resolver) Address() (common.Address, error)

Address returns the Ethereum address of the domain

func (*Resolver) Contenthash

func (r *Resolver) Contenthash() ([]byte, error)

Contenthash returns the content hash of the domain

func (*Resolver) InterfaceImplementer

func (r *Resolver) InterfaceImplementer(interfaceID [4]byte) (common.Address, error)

InterfaceImplementer returns the address of the contract that implements the given interface for the given domain

func (*Resolver) MultiAddress

func (r *Resolver) MultiAddress(coinType uint64) ([]byte, error)

MultiAddress returns the address of the domain for a given coin type. The coin type is as per https://github.com/satoshilabs/slips/blob/master/slip-0044.md

func (*Resolver) PubKey

func (r *Resolver) PubKey() ([32]byte, [32]byte, error)

PubKey returns the public key of the domain

func (*Resolver) SetABI

func (r *Resolver) SetABI(opts *bind.TransactOpts, name string, abi string, contentType *big.Int) (*types.Transaction, error)

SetABI sets the ABI associated with a name

func (*Resolver) SetContenthash

func (r *Resolver) SetContenthash(opts *bind.TransactOpts, contenthash []byte) (*types.Transaction, error)

SetContenthash sets the content hash of the domain

func (*Resolver) SetPubKey

func (r *Resolver) SetPubKey(opts *bind.TransactOpts, x [32]byte, y [32]byte) (*types.Transaction, error)

SetPubKey sets the public key of the domain

func (*Resolver) SetText

func (r *Resolver) SetText(opts *bind.TransactOpts, name string, value string) (*types.Transaction, error)

SetText sets the text associated with a name

func (*Resolver) Text

func (r *Resolver) Text(name string) (string, error)

Text obtains the text associated with a name

Directories

Path Synopsis
contracts

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL