resolution

package module
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2023 License: MIT Imports: 18 Imported by: 1

README

Test Lint Go Report Card GoDoc Unstoppable Domains Documentation Get help on Discord

resolution-go

resolution-go is a library for interacting with blockchain domain names. It can be used to retrieve payment addresses and IPFS hashes for decentralized websites.

resolution-go is primarily built and maintained by Unstoppable Domains.

Resolution supports different decentralized domains. Please, refer to the Top Level Domains List

Installing resolution-go

go get github.com/unstoppabledomains/resolution-go/v2

Updating resolution-go

go get -u github.com/unstoppabledomains/resolution-go/v2

Usage

package main

import (
	"fmt"

	"github.com/Zilliqa/gozilliqa-sdk/provider"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/unstoppabledomains/resolution-go/v2"
	"github.com/unstoppabledomains/resolution-go/v2/namingservice"
)

func main() {
	// Resolve .crypto
	uns, _ := resolution.NewUnsBuilder().Build()
	ethAddress, _ := uns.Addr("brad.crypto", "ETH")
	fmt.Println("ETH address for brad.crypto is", ethAddress)

	// Resolve.zil
	zns, _ := resolution.NewZnsBuilder().Build()
	btcAddress, _ := zns.Addr("brad.zil", "BTC")
	fmt.Println("BTC address for brad.zil is", btcAddress)

	// Get locations of domains
	uns, _ = resolution.NewUnsBuilder().Build()
	locations, _ := uns.Locations([]string{"ryan.crypto", "brad.crypto"})
	fmt.Println("Locations for ryan.crypto and brad.crypto are", locations)

	// Detect domain naming service
	namingServices := map[string]resolution.NamingService{namingservice.UNS: uns, namingservice.ZNS: zns}
	domainToDetect := "ryan.crypto"
	namingServiceName, _ := resolution.DetectNamingService(domainToDetect)
	if namingServices[namingServiceName] != nil {
		resolvedAddress, _ := namingServices[namingServiceName].Addr(domainToDetect, "ETH")
		fmt.Println("ETH address for", domainToDetect, "is", resolvedAddress)
	}

	// Set custom Ethereum endpoint for UNS service
	ethContractBackend, _ := ethclient.Dial("https://mainnet.infura.io/v3/c5da69dfac9c4d9d96dd232580d4124e")
	ethL2ContactBackend, _ := ethclient.Dial("https://polygon-mainnet.infura.io/v3/c5da69dfac9c4d9d96dd232580d4124e")
	domainToDetect := "ryan.crypto"
	unsWithCustomBackend, _ := resolution.NewUnsBuilder().SetContractBackend(ethContractBackend).SetL2ContractBackend(ethL2ContactBackend).Build()
	resolvedAddress, _ := unsWithCustomBackend.Addr(domainToDetect, "ETH")
	fmt.Println("ETH address for", domainToDetect, "is", resolvedAddress)

	// Set custom Zilliqa endpoint for ZNS service
	zilliqaProvider := provider.NewProvider("https://api.zilliqa.com")
	znsWithCustomProvider, _ := resolution.NewZnsBuilder().SetProvider(zilliqaProvider).Build()
	domainToDetect := "ryan.crypto"
	resolvedAddress, _ := znsWithCustomProvider.Addr(domainToDetect, "ETH")
	fmt.Println("ETH address for", domainToDetect, "is", resolvedAddress)
}

Custom Ethereum provider configuration

package main

import (
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/unstoppabledomains/resolution-go/v2"
)

func main() {
	var ethereumUrl = "https://mainnet.infura.io/v3/c5da69dfac9c4d9d96dd232580d4124e"
	var ethereumL2Url = "https://polygon-mainnet.infura.io/v3/c5da69dfac9c4d9d96dd232580d4124e"

	var unsBuilder := resolution.NewUnsBuilder()
	var backend, _ := ethclient.Dial(ethereumUrl)
	var backendL2, _ := ethclient.Dial(ethereumL2Url)

	unsBuilder.SetContractBackend(backend)
	unsBuilder.SetL2ContractBackend(backendL2)

	var unsResolution, _ = unsBuilder.Build()
	var znsResolution, _ = resolution.NewZnsBuilder().Build()
}

Contributions

Contributions to this library are more than welcome. The easiest way to contribute is through GitHub issues and pull requests.

Free advertising for integrated apps

Once your app has a working Unstoppable Domains integration, register it here. Registered apps appear on the Unstoppable Domains homepage and Applications page — putting your app in front of tens of thousands of potential customers per day.

Also, every week we select a newly-integrated app to feature in the Unstoppable Update newsletter. This newsletter is delivered to straight into the inbox of ~100,000 crypto fanatics — all of whom could be new customers to grow your business.

Get help

Join our discord community and ask questions.

Help us improve

We're always looking for ways to improve how developers use and integrate our products into their applications. We'd love to hear about your experience to help us improve by taking our survey.

Documentation

Index

Constants

View Source
const (
	Mainnet string = "mainnet"
	Polygon string = "polygon"
	Mumbai  string = "mumbai"
	Goerli  string = "goerli"
)
View Source
const (
	Layer1 string = "Layer 1"
	Layer2 string = "Layer 2"
)

Variables

View Source
var NetworkNameToId = map[string]int{
	Mainnet: 1,
	Polygon: 137,
	Mumbai:  80001,
	Goerli:  5,
}
View Source
var NetworkProviders = map[string]string{
	Mainnet: "https://mainnet.infura.io/v3/c5da69dfac9c4d9d96dd232580d4124e",
	Goerli:  "https://goerli.infura.io/v3/c5da69dfac9c4d9d96dd232580d4124e",
	Polygon: "https://polygon-mainnet.infura.io/v3/c5da69dfac9c4d9d96dd232580d4124e",
	Mumbai:  "https://polygon-mumbai.infura.io/v3/c5da69dfac9c4d9d96dd232580d4124e",
}

Functions

func DetectNamingService

func DetectNamingService(domainName string) (string, error)

DetectNamingService helper to detect naming service type for provided domain. Returns ZNS or UNS for valid domain and error if domain is not valid or not supported by resolution-go library.

func ZnsNameHash

func ZnsNameHash(domainName string) (string, error)

ZnsNameHash Namehash for .zil domains

Types

type DomainNotConfiguredError

type DomainNotConfiguredError struct {
	DomainName string
	Layer      string
}

DomainNotConfiguredError Error when domain does not have a resolver set

func (*DomainNotConfiguredError) Error

func (e *DomainNotConfiguredError) Error() string

type DomainNotRegisteredError

type DomainNotRegisteredError struct {
	DomainName string
	Namehash   string
}

DomainNotRegisteredError Error when domain is missing an owner

func (*DomainNotRegisteredError) Error

func (e *DomainNotRegisteredError) Error() string

type DomainNotSupportedError

type DomainNotSupportedError struct {
	DomainName string
}

DomainNotSupportedError Error when domain is not supported by the naming service

func (*DomainNotSupportedError) Error

func (e *DomainNotSupportedError) Error() string

type InvalidDomainNameReturnedError

type InvalidDomainNameReturnedError struct {
	Namehash   string
	DomainName string
}

InvalidDomainNameReturnedError Error when ERC721 metadata provides returns incorrect domain name

func (*InvalidDomainNameReturnedError) Error

type MetadataClient

type MetadataClient interface {
	Get(url string) (resp *http.Response, err error)
}

type MethodIsNotSupportedError

type MethodIsNotSupportedError struct {
	NamingServiceName string
}

MethodIsNotSupportedError Error when naming services does not support called method

func (*MethodIsNotSupportedError) Error

func (e *MethodIsNotSupportedError) Error() string

type NamingService

type NamingService interface {
	// Records Retrieve records of domain.
	// Keys must be provided in raw format according to specification.
	// Keys specification: https://docs.unstoppabledomains.com/domain-registry-essentials/records-reference.
	// Supported keys reference: https://github.com/unstoppabledomains/dot-crypto/blob/master/src/supported-keys/supported-keys.json.
	// It returns key-value map of specified keys set on provided domain. Map can contain empty strings if keys are not found.
	Records(domainName string, keys []string) (map[string]string, error)

	// Record Retrieve single record of domain.
	// Keys must be provided in raw format according to specification.
	// Keys specification: https://docs.unstoppabledomains.com/domain-registry-essentials/records-reference.
	// Supported keys reference: https://github.com/unstoppabledomains/dot-crypto/blob/master/src/supported-keys/supported-keys.json.
	// Returns result in string or empty string if no result found.
	Record(domainName string, key string) (string, error)

	// Addr Retrieve the value of domain's currency ticker.
	// Ticker must contain cryptocurrency like: BTC, ETH.
	// Returns result in string or empty string if no result found.
	Addr(domainName string, ticker string) (string, error)

	// AddrVersion Retrieve the version value of domain's currency ticker.
	// This method should be used to query multi-chain currency like USDT.
	// Returns result in string or empty string if no result found.
	AddrVersion(domainName string, ticker string, version string) (string, error)

	// Email Retrieve the email of domain.
	// Returns result in string or empty string if no result found.
	Email(domainName string) (string, error)

	// Resolver Retrieve the resolver address.
	// Returns result or DomainNotConfiguredError if resolver is not found.
	Resolver(domainName string) (string, error)

	// Owner Retrieve the owner address.
	// Returns result or DomainNotRegisteredError if owner is not found.
	Owner(domainName string) (string, error)

	// IpfsHash Retrieve hash of IPFS website attached to domain.
	IpfsHash(domainName string) (string, error)

	// HTTPUrl Retrieve the http redirect url of a domain.
	HTTPUrl(domainName string) (string, error)

	// AllRecords Retrieve all records of a domain.
	// Returns result in string or empty string record is not found.
	// Deprecated: This method will be removed in future releases
	AllRecords(domainName string) (map[string]string, error)

	// Locations Retrieve locations of domains
	// Returns key-value map of domain names to location
	Locations(domainNames []string) (map[string]namingservice.Location, error)

	// DNS Retrieve the DNS records of a domain.
	// Returns a set of valid and filtered non-empty DNS records attached to domain.
	DNS(domainName string, types []dnsrecords.Type) ([]dnsrecords.Record, error)

	// IsSupportedDomain checks whether domain name is supported by the naming service.
	IsSupportedDomain(domainName string) (bool, error)

	// TokenURI returns ERC721 metadata token URI
	TokenURI(domainName string) (string, error)

	// TokenURIMetadata returns ERC721 metadata
	TokenURIMetadata(domainName string) (TokenMetadata, error)

	// Unhash returns a domain name from a hash using TokenMetadata.Name field and ensures it indeed matches the given hash.
	// domainHash should be in hex numeric string format, for example: "0x29bf1b111e709f0953848df35e419490fbad5d316690e4de61adc52695ddf9f3"
	// ERC721 Token ID could be passed to this method but should be converted to hex numeric string before usage.
	//
	// Examples of usage:
	//
	// domainName, err := NamingService.Unhash("0x29bf1b111e709f0953848df35e419490fbad5d316690e4de61adc52695ddf9f3")
	// domainName, err := NamingService.Unhash("0x691f36df38168d9297e784f45a87257a70c58c4040d469c6d0b91d253a837e32")
	//
	// Usage with ERC721 token id:
	//
	// var erc721TokenID big.Int
	// erc721TokenID.SetString("47548000072528700265403562077742902343248290986511625310517899838602191535666", 10)
	// domainHash := hex.EncodeToString(erc721TokenID.Bytes())
	// domainName, err := NamingService.Unhash(domainHash)
	//
	Unhash(domainHash string) (string, error)

	// Namehash returns a namehash of a domain following the EIP-137 standard
	Namehash(domainName string) (string, error)
}

NamingService Unstoppable supports multiple naming services (.zil and .crypto). Each naming service implements shared interface and returns similar record types.

type NetworkContracts

type NetworkContracts map[string]contracts

type TokenMetadata

type TokenMetadata struct {
	Name            string                   `json:"name"`
	Description     string                   `json:"description"`
	Image           string                   `json:"image"`
	ExternalUrl     string                   `json:"external_url"`
	ExternalLink    string                   `json:"external_link"`
	ImageData       string                   `json:"image_data"`
	BackgroundColor string                   `json:"background_color"`
	AnimationUrl    string                   `json:"animation_url"`
	YoutubeUrl      string                   `json:"youtube_url"`
	Properties      TokenMetadataProperties  `json:"properties"`
	Attributes      []TokenMetadataAttribute `json:"attributes"`
}

type TokenMetadataAttribute

type TokenMetadataAttribute struct {
	DisplayType string     `json:"display_type"`
	TraitType   string     `json:"trait_type"`
	Value       json.Token `json:"value"`
}

type TokenMetadataProperties

type TokenMetadataProperties struct {
	Records map[string]string `json:"records"`
}

type Uns

type Uns struct {
	// contains filtered or unexported fields
}

Uns is a naming service handles Unstoppable domains resolution.

func (*Uns) Addr

func (c *Uns) Addr(domainName string, ticker string) (string, error)

func (*Uns) AddrVersion

func (c *Uns) AddrVersion(domainName string, ticker string, version string) (string, error)

func (*Uns) AllRecords

func (c *Uns) AllRecords(domainName string) (map[string]string, error)

func (*Uns) DNS

func (c *Uns) DNS(domainName string, types []dnsrecords.Type) ([]dnsrecords.Record, error)

func (*Uns) Data

func (c *Uns) Data(domainName string, keys []string) (*struct {
	Resolver common.Address
	Owner    common.Address
	Values   []string
}, error)

Data Get raw data attached to domain

func (*Uns) Email

func (c *Uns) Email(domainName string) (string, error)

func (*Uns) HTTPUrl

func (c *Uns) HTTPUrl(domainName string) (string, error)

func (*Uns) IpfsHash

func (c *Uns) IpfsHash(domainName string) (string, error)

func (*Uns) IsSupportedDomain

func (c *Uns) IsSupportedDomain(domainName string) (bool, error)

func (*Uns) Locations

func (c *Uns) Locations(domainNames []string) (map[string]namingservice.Location, error)

func (*Uns) Namehash

func (c *Uns) Namehash(domainName string) (string, error)

func (*Uns) Owner

func (c *Uns) Owner(domainName string) (string, error)

func (*Uns) Record

func (c *Uns) Record(domainName string, key string) (string, error)

func (*Uns) Records

func (c *Uns) Records(domainName string, keys []string) (map[string]string, error)

func (*Uns) Resolver

func (c *Uns) Resolver(domainName string) (string, error)

func (*Uns) TokenURI

func (c *Uns) TokenURI(domainName string) (string, error)

func (*Uns) TokenURIMetadata

func (c *Uns) TokenURIMetadata(domainName string) (TokenMetadata, error)

func (*Uns) Unhash

func (c *Uns) Unhash(domainHash string) (string, error)

type UnsBuilder

type UnsBuilder interface {
	// SetContractBackend set Ethereum backend for communication with UNS registry
	SetContractBackend(backend bind.ContractBackend) UnsBuilder

	// SetL2ContractBackend set Ethereum backend for communication with UNS L2registry
	SetL2ContractBackend(backend bind.ContractBackend) UnsBuilder

	// SetMetadataClient set http backend for communication with ERC721 metadata server
	SetMetadataClient(backend MetadataClient) UnsBuilder

	// SetEthereumNetwork set Ethereum network for communication with UNS registry
	SetEthereumNetwork(network string) UnsBuilder

	// SetL2EthereumNetwork set Ethereum network for communication with UNS L2 registry
	SetL2EthereumNetwork(network string) UnsBuilder

	// Build Uns instance
	Build() (*Uns, error)
}

UnsBuilder is a builder to setup and build instance of Uns

func NewUnsBuilder

func NewUnsBuilder() UnsBuilder

NewUnsBuilder Creates builder to setup new instance of Uns

type UnsConfigurationError

type UnsConfigurationError struct {
	Layer        string
	InvalidField string
}

UnsConfigurationError Error when UNS resolution service is configured incorrectly

func (*UnsConfigurationError) Error

func (e *UnsConfigurationError) Error() string

type UnsService

type UnsService struct {
	Layer string
	// contains filtered or unexported fields
}

Uns is a naming service handles Unstoppable domains resolution.

func (*UnsService) DNS

func (c *UnsService) DNS(domainName string, types []dnsrecords.Type) ([]dnsrecords.Record, error)

type Zns

type Zns struct {
	// contains filtered or unexported fields
}

Zns is a naming service handles .zil domains resolution.

func (*Zns) Addr

func (z *Zns) Addr(domainName string, ticker string) (string, error)

func (*Zns) AddrVersion

func (z *Zns) AddrVersion(domainName string, ticker string, version string) (string, error)

func (*Zns) AllRecords

func (z *Zns) AllRecords(domainName string) (map[string]string, error)

func (*Zns) DNS

func (z *Zns) DNS(domainName string, types []dnsrecords.Type) ([]dnsrecords.Record, error)

func (*Zns) Email

func (z *Zns) Email(domainName string) (string, error)

func (*Zns) HTTPUrl

func (z *Zns) HTTPUrl(domainName string) (string, error)

func (*Zns) IpfsHash

func (z *Zns) IpfsHash(domainName string) (string, error)

func (*Zns) IsSupportedDomain

func (z *Zns) IsSupportedDomain(domainName string) (bool, error)

func (*Zns) Locations

func (z *Zns) Locations(domainNames []string) (map[string]namingservice.Location, error)

func (*Zns) Namehash

func (z *Zns) Namehash(domainName string) (string, error)

func (*Zns) Owner

func (z *Zns) Owner(domainName string) (string, error)

func (*Zns) Record

func (z *Zns) Record(domainName string, key string) (string, error)

func (*Zns) Records

func (z *Zns) Records(domainName string, keys []string) (map[string]string, error)

func (*Zns) Resolver

func (z *Zns) Resolver(domainName string) (string, error)

func (*Zns) State

func (z *Zns) State(domainName string) (*ZnsDomainState, error)

State Get raw data attached to domain.

func (*Zns) TokenURI

func (z *Zns) TokenURI(_ string) (string, error)

func (*Zns) TokenURIMetadata

func (z *Zns) TokenURIMetadata(_ string) (TokenMetadata, error)

func (*Zns) Unhash

func (z *Zns) Unhash(_ string) (string, error)

type ZnsBuilder

type ZnsBuilder interface {
	// SetProvider set Zilliqa blockchain provider to communicate with ZNS registry
	SetProvider(provider ZnsProvider) ZnsBuilder
	// SetProvider set Zilliqa network to communicate with ZNS registry
	SetNetwork(network string) ZnsBuilder
	// Build Zns instance
	Build() (*Zns, error)
}

ZnsBuilder is a builder to setup and build instance of Zns service.

func NewZnsBuilder

func NewZnsBuilder() ZnsBuilder

NewZnsBuilder Creates ZNS builder instance.

type ZnsDomainState

type ZnsDomainState struct {
	Resolver string
	Owner    string
	Records  map[string]string
}

ZnsDomainState State of ZNS domain

type ZnsProvider

type ZnsProvider interface {
	GetSmartContractSubState(contractAddress string, params ...interface{}) (string, error)
}

ZnsProvider ZnsProvider

Directories

Path Synopsis
cns
uns

Jump to

Keyboard shortcuts

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