2021-07-22 09:46:38 +00:00
|
|
|
/*
|
|
|
|
Package nns contains non-divisible non-fungible NEP11-compatible token
|
|
|
|
implementation. This token is a compatible analogue of C# Neo Name Service
|
|
|
|
token and is aimed to serve as a domain name service for Neo smart-contracts,
|
|
|
|
thus it's NeoNameService. This token can be minted with new domain name
|
|
|
|
registration, the domain name itself is your NFT. Corresponding domain root
|
2022-04-14 11:56:51 +00:00
|
|
|
must be added by committee before a new domain name can be registered.
|
2021-07-22 09:46:38 +00:00
|
|
|
*/
|
|
|
|
package nns
|
|
|
|
|
|
|
|
import (
|
2023-03-07 11:06:21 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/common"
|
2021-07-22 09:46:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/crypto"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/neo"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Prefixes used for contract data storage.
|
|
|
|
const (
|
|
|
|
// prefixTotalSupply contains total supply of minted domains.
|
|
|
|
prefixTotalSupply byte = 0x00
|
2022-04-14 11:56:51 +00:00
|
|
|
// prefixBalance contains map from the owner to their balance.
|
2021-07-22 09:46:38 +00:00
|
|
|
prefixBalance byte = 0x01
|
|
|
|
// prefixAccountToken contains map from (owner + token key) to token ID,
|
|
|
|
// where token key = hash160(token ID) and token ID = domain name.
|
|
|
|
prefixAccountToken byte = 0x02
|
|
|
|
// prefixRegisterPrice contains price for new domain name registration.
|
|
|
|
prefixRegisterPrice byte = 0x10
|
|
|
|
// prefixRoot contains set of roots (map from root to 0).
|
|
|
|
prefixRoot byte = 0x20
|
|
|
|
// prefixName contains map from token key to token where token is domain
|
|
|
|
// NameState structure.
|
|
|
|
prefixName byte = 0x21
|
|
|
|
// prefixRecord contains map from (token key + hash160(token name) + record type)
|
|
|
|
// to record.
|
|
|
|
prefixRecord byte = 0x22
|
2024-08-16 13:38:54 +00:00
|
|
|
//prefixGlobalDomain contains a flag indicating that this domain was created using GlobalDomain.
|
|
|
|
//This is necessary to distinguish it from regular CNAME records.
|
|
|
|
prefixGlobalDomain byte = 0x23
|
2021-07-22 09:46:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Values constraints.
|
|
|
|
const (
|
|
|
|
// maxRegisterPrice is the maximum price of register method.
|
2022-07-06 09:22:43 +00:00
|
|
|
maxRegisterPrice = int64(1_0000_0000_0000)
|
2021-07-22 09:46:38 +00:00
|
|
|
// maxRootLength is the maximum domain root length.
|
|
|
|
maxRootLength = 16
|
|
|
|
// maxDomainNameFragmentLength is the maximum length of the domain name fragment.
|
2022-04-04 19:06:09 +00:00
|
|
|
maxDomainNameFragmentLength = 63
|
2021-07-22 09:46:38 +00:00
|
|
|
// minDomainNameLength is minimum domain length.
|
2024-01-25 13:20:33 +00:00
|
|
|
minDomainNameLength = 2
|
2021-07-22 09:46:38 +00:00
|
|
|
// maxDomainNameLength is maximum domain length.
|
|
|
|
maxDomainNameLength = 255
|
|
|
|
// maxTXTRecordLength is the maximum length of the TXT domain record.
|
|
|
|
maxTXTRecordLength = 255
|
|
|
|
)
|
|
|
|
|
|
|
|
// Other constants.
|
|
|
|
const (
|
|
|
|
// defaultRegisterPrice is the default price for new domain registration.
|
|
|
|
defaultRegisterPrice = 10_0000_0000
|
2021-09-28 12:40:14 +00:00
|
|
|
// millisecondsInYear is amount of milliseconds per year.
|
2022-07-06 09:22:43 +00:00
|
|
|
millisecondsInYear = int64(365 * 24 * 3600 * 1000)
|
2024-06-18 13:50:09 +00:00
|
|
|
// errInvalidDomainName is an error message for invalid domain name format.
|
|
|
|
errInvalidDomainName = "invalid domain name format"
|
2021-07-22 09:46:38 +00:00
|
|
|
)
|
|
|
|
|
2024-08-16 13:38:54 +00:00
|
|
|
const (
|
|
|
|
// Cnametgt is a special TXT record ensuring all created subdomains point to the global domain - the value of this variable.
|
|
|
|
//It is guaranteed that two domains cannot point to the same global domain.
|
|
|
|
Cnametgt = "cnametgt"
|
|
|
|
)
|
|
|
|
|
2021-09-15 12:42:57 +00:00
|
|
|
// RecordState is a type that registered entities are saved to.
|
|
|
|
type RecordState struct {
|
|
|
|
Name string
|
|
|
|
Type RecordType
|
|
|
|
Data string
|
2021-09-28 12:40:14 +00:00
|
|
|
ID byte
|
2021-09-15 12:42:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 09:46:38 +00:00
|
|
|
// Update updates NameService contract.
|
2023-11-07 12:18:48 +00:00
|
|
|
func Update(nef []byte, manifest string, data any) {
|
2021-07-22 09:46:38 +00:00
|
|
|
checkCommittee()
|
2021-09-16 09:45:24 +00:00
|
|
|
// Calculating keys and serializing requires calling
|
|
|
|
// std and crypto contracts. This can be helpful on update
|
|
|
|
// thus we provide `AllowCall` to management.Update.
|
|
|
|
// management.Update(nef, []byte(manifest))
|
2023-06-19 08:17:51 +00:00
|
|
|
management.UpdateWithData(nef, []byte(manifest), common.AppendVersion(data))
|
2021-12-10 10:40:20 +00:00
|
|
|
runtime.Log("nns contract updated")
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// _deploy initializes defaults (total supply and registration price) on contract deploy.
|
2023-11-07 12:18:48 +00:00
|
|
|
func _deploy(data any, isUpdate bool) {
|
2021-07-22 09:46:38 +00:00
|
|
|
if isUpdate {
|
2023-11-07 12:18:48 +00:00
|
|
|
args := data.([]any)
|
2021-12-27 08:49:30 +00:00
|
|
|
common.CheckVersion(args[len(args)-1].(int))
|
2021-07-22 09:46:38 +00:00
|
|
|
return
|
|
|
|
}
|
2021-11-23 09:18:34 +00:00
|
|
|
|
2021-07-22 09:46:38 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
storage.Put(ctx, []byte{prefixTotalSupply}, 0)
|
|
|
|
storage.Put(ctx, []byte{prefixRegisterPrice}, defaultRegisterPrice)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Symbol returns NeoNameService symbol.
|
|
|
|
func Symbol() string {
|
|
|
|
return "NNS"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decimals returns NeoNameService decimals.
|
|
|
|
func Decimals() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Version returns the version of the contract.
|
2021-10-22 12:08:51 +00:00
|
|
|
func Version() int {
|
|
|
|
return common.Version
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// TotalSupply returns the overall number of domains minted by NeoNameService contract.
|
2021-07-22 09:46:38 +00:00
|
|
|
func TotalSupply() int {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return getTotalSupply(ctx)
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// OwnerOf returns the owner of the specified domain.
|
2021-07-22 09:46:38 +00:00
|
|
|
func OwnerOf(tokenID []byte) interop.Hash160 {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
ns := getNameState(ctx, tokenID)
|
|
|
|
return ns.Owner
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Properties returns a domain name and an expiration date of the specified domain.
|
2023-11-07 12:18:48 +00:00
|
|
|
func Properties(tokenID []byte) map[string]any {
|
2021-07-22 09:46:38 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
ns := getNameState(ctx, tokenID)
|
2023-11-07 12:18:48 +00:00
|
|
|
return map[string]any{
|
2021-07-22 09:46:38 +00:00
|
|
|
"name": ns.Name,
|
|
|
|
"expiration": ns.Expiration,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// BalanceOf returns the overall number of domains owned by the specified owner.
|
2021-07-22 09:46:38 +00:00
|
|
|
func BalanceOf(owner interop.Hash160) int {
|
|
|
|
if !isValid(owner) {
|
|
|
|
panic(`invalid owner`)
|
|
|
|
}
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
balance := storage.Get(ctx, append([]byte{prefixBalance}, owner...))
|
|
|
|
if balance == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return balance.(int)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tokens returns iterator over a set of all registered domain names.
|
|
|
|
func Tokens() iterator.Iterator {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return storage.Find(ctx, []byte{prefixName}, storage.ValuesOnly|storage.DeserializeValues|storage.PickField1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TokensOf returns iterator over minted domains owned by the specified owner.
|
|
|
|
func TokensOf(owner interop.Hash160) iterator.Iterator {
|
|
|
|
if !isValid(owner) {
|
|
|
|
panic(`invalid owner`)
|
|
|
|
}
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return storage.Find(ctx, append([]byte{prefixAccountToken}, owner...), storage.ValuesOnly)
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Transfer transfers the domain with the specified name to a new owner.
|
2023-11-07 12:18:48 +00:00
|
|
|
func Transfer(to interop.Hash160, tokenID []byte, data any) bool {
|
2021-07-22 09:46:38 +00:00
|
|
|
if !isValid(to) {
|
|
|
|
panic(`invalid receiver`)
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
tokenKey = getTokenKey(tokenID)
|
|
|
|
ctx = storage.GetContext()
|
|
|
|
)
|
|
|
|
ns := getNameStateWithKey(ctx, tokenKey)
|
|
|
|
from := ns.Owner
|
|
|
|
if !runtime.CheckWitness(from) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !util.Equals(from, to) {
|
|
|
|
// update token info
|
|
|
|
ns.Owner = to
|
|
|
|
ns.Admin = nil
|
|
|
|
putNameStateWithKey(ctx, tokenKey, ns)
|
|
|
|
|
|
|
|
// update `from` balance
|
|
|
|
updateBalance(ctx, tokenID, from, -1)
|
|
|
|
|
|
|
|
// update `to` balance
|
|
|
|
updateBalance(ctx, tokenID, to, +1)
|
|
|
|
}
|
|
|
|
postTransfer(from, to, tokenID, data)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Roots returns iterator over a set of NameService roots.
|
|
|
|
func Roots() iterator.Iterator {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return storage.Find(ctx, []byte{prefixRoot}, storage.KeysOnly|storage.RemovePrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPrice sets the domain registration price.
|
2022-07-06 09:22:43 +00:00
|
|
|
func SetPrice(price int64) {
|
2021-07-22 09:46:38 +00:00
|
|
|
checkCommittee()
|
|
|
|
if price < 0 || price > maxRegisterPrice {
|
|
|
|
panic("The price is out of range.")
|
|
|
|
}
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
storage.Put(ctx, []byte{prefixRegisterPrice}, price)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPrice returns the domain registration price.
|
|
|
|
func GetPrice() int {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return storage.Get(ctx, []byte{prefixRegisterPrice}).(int)
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// IsAvailable checks whether the provided domain name is available.
|
2021-07-22 09:46:38 +00:00
|
|
|
func IsAvailable(name string) bool {
|
2023-12-12 17:22:33 +00:00
|
|
|
fragments := splitAndCheck(name)
|
2021-07-22 09:46:38 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-10-04 10:26:23 +00:00
|
|
|
l := len(fragments)
|
|
|
|
if storage.Get(ctx, append([]byte{prefixRoot}, []byte(fragments[l-1])...)) == nil {
|
|
|
|
if l != 1 {
|
|
|
|
panic("TLD not found")
|
|
|
|
}
|
2021-07-22 09:46:38 +00:00
|
|
|
return true
|
|
|
|
}
|
2024-08-16 13:38:54 +00:00
|
|
|
|
2024-04-15 23:56:31 +00:00
|
|
|
checkParentExists(ctx, fragments)
|
2024-08-16 13:38:54 +00:00
|
|
|
checkAvailableGlobalDomain(ctx, name)
|
2023-12-12 17:22:33 +00:00
|
|
|
return storage.Get(ctx, append([]byte{prefixName}, getTokenKey([]byte(name))...)) == nil
|
2021-10-04 10:26:23 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 13:38:54 +00:00
|
|
|
// checkAvailableGlobalDomain - triggers a panic if the global domain name is occupied.
|
|
|
|
func checkAvailableGlobalDomain(ctx storage.Context, domain string) {
|
|
|
|
globalDomain := getGlobalDomain(ctx, domain)
|
|
|
|
if globalDomain == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nsBytes := storage.Get(ctx, append([]byte{prefixName}, getTokenKey([]byte(globalDomain))...))
|
|
|
|
if nsBytes != nil {
|
|
|
|
panic("global domain is already taken: " + globalDomain + ". Domain: " + domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getGlobalDomain returns the global domain.
|
|
|
|
func getGlobalDomain(ctx storage.Context, domain string) string {
|
|
|
|
index := std.MemorySearch([]byte(domain), []byte("."))
|
|
|
|
|
|
|
|
if index == -1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
name := domain[index+1:]
|
|
|
|
if name == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return extractCnametgt(ctx, name, domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractCnametgt returns the value of the Cnametgt TXT record.
|
|
|
|
func extractCnametgt(ctx storage.Context, name, domain string) string {
|
|
|
|
fragments := splitAndCheck(domain)
|
|
|
|
|
|
|
|
tokenID := []byte(tokenIDFromName(name))
|
|
|
|
records := getRecordsByType(ctx, tokenID, name, TXT)
|
|
|
|
|
|
|
|
if records == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
globalDomain := ""
|
|
|
|
for _, name := range records {
|
|
|
|
fragments := std.StringSplit(name, "=")
|
|
|
|
if len(fragments) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if fragments[0] == Cnametgt {
|
|
|
|
globalDomain = fragments[1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if globalDomain == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fragments[0] + "." + globalDomain
|
|
|
|
}
|
|
|
|
|
2024-04-15 23:56:31 +00:00
|
|
|
// checkParentExists panics if any domain from fragments doesn't exist or is expired.
|
|
|
|
func checkParentExists(ctx storage.Context, fragments []string) {
|
|
|
|
if dom := parentExpired(ctx, fragments); dom != "" {
|
|
|
|
panic("domain does not exist or is expired: " + dom)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parentExpired returns domain from fragments that doesn't exist or is expired.
|
2021-10-04 10:26:23 +00:00
|
|
|
// first denotes the deepest subdomain to check.
|
2024-04-15 23:56:31 +00:00
|
|
|
func parentExpired(ctx storage.Context, fragments []string) string {
|
2022-07-06 09:22:43 +00:00
|
|
|
now := int64(runtime.GetTime())
|
2021-10-04 10:26:23 +00:00
|
|
|
last := len(fragments) - 1
|
|
|
|
name := fragments[last]
|
2023-12-12 17:22:33 +00:00
|
|
|
for i := last; i > 0; i-- {
|
2021-10-04 10:26:23 +00:00
|
|
|
if i != last {
|
|
|
|
name = fragments[i] + "." + name
|
|
|
|
}
|
|
|
|
nsBytes := storage.Get(ctx, append([]byte{prefixName}, getTokenKey([]byte(name))...))
|
|
|
|
if nsBytes == nil {
|
2024-04-15 23:56:31 +00:00
|
|
|
return name
|
2021-10-04 10:26:23 +00:00
|
|
|
}
|
|
|
|
ns := std.Deserialize(nsBytes.([]byte)).(NameState)
|
|
|
|
if now >= ns.Expiration {
|
2024-04-15 23:56:31 +00:00
|
|
|
return name
|
2021-10-04 10:26:23 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-15 23:56:31 +00:00
|
|
|
return ""
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Register registers a new domain with the specified owner and name if it's available.
|
2021-09-28 08:58:18 +00:00
|
|
|
func Register(name string, owner interop.Hash160, email string, refresh, retry, expire, ttl int) bool {
|
2024-08-16 13:38:54 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
return register(ctx, name, owner, email, refresh, retry, expire, ttl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register registers a new domain with the specified owner and name if it's available.
|
|
|
|
func register(ctx storage.Context, name string, owner interop.Hash160, email string, refresh, retry, expire, ttl int) bool {
|
2023-12-12 17:22:33 +00:00
|
|
|
fragments := splitAndCheck(name)
|
2021-10-04 10:26:23 +00:00
|
|
|
l := len(fragments)
|
|
|
|
tldKey := append([]byte{prefixRoot}, []byte(fragments[l-1])...)
|
2024-08-16 13:38:54 +00:00
|
|
|
|
2021-10-04 10:26:23 +00:00
|
|
|
tldBytes := storage.Get(ctx, tldKey)
|
|
|
|
if l == 1 {
|
|
|
|
checkCommittee()
|
|
|
|
if tldBytes != nil {
|
|
|
|
panic("TLD already exists")
|
|
|
|
}
|
|
|
|
storage.Put(ctx, tldKey, 0)
|
|
|
|
} else {
|
|
|
|
if tldBytes == nil {
|
|
|
|
panic("TLD not found")
|
|
|
|
}
|
2024-04-15 23:56:31 +00:00
|
|
|
checkParentExists(ctx, fragments)
|
|
|
|
|
2021-11-19 13:35:58 +00:00
|
|
|
parentKey := getTokenKey([]byte(name[len(fragments[0])+1:]))
|
2021-10-04 11:47:42 +00:00
|
|
|
nsBytes := storage.Get(ctx, append([]byte{prefixName}, parentKey...))
|
|
|
|
ns := std.Deserialize(nsBytes.([]byte)).(NameState)
|
|
|
|
ns.checkAdmin()
|
2021-11-22 09:42:28 +00:00
|
|
|
|
|
|
|
parentRecKey := append([]byte{prefixRecord}, parentKey...)
|
|
|
|
it := storage.Find(ctx, parentRecKey, storage.ValuesOnly|storage.DeserializeValues)
|
|
|
|
suffix := []byte(name)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
r := iterator.Value(it).(RecordState)
|
|
|
|
ind := std.MemorySearchLastIndex([]byte(r.Name), suffix, len(r.Name))
|
|
|
|
if ind > 0 && ind+len(suffix) == len(r.Name) {
|
|
|
|
panic("parent domain has conflicting records: " + r.Name)
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !isValid(owner) {
|
|
|
|
panic("invalid owner")
|
|
|
|
}
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckOwnerWitness(owner)
|
2021-07-22 09:46:38 +00:00
|
|
|
runtime.BurnGas(GetPrice())
|
|
|
|
var (
|
|
|
|
tokenKey = getTokenKey([]byte(name))
|
|
|
|
oldOwner interop.Hash160
|
|
|
|
)
|
|
|
|
nsBytes := storage.Get(ctx, append([]byte{prefixName}, tokenKey...))
|
|
|
|
if nsBytes != nil {
|
|
|
|
ns := std.Deserialize(nsBytes.([]byte)).(NameState)
|
2022-07-06 09:22:43 +00:00
|
|
|
if int64(runtime.GetTime()) < ns.Expiration {
|
2021-07-22 09:46:38 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
oldOwner = ns.Owner
|
|
|
|
updateBalance(ctx, []byte(name), oldOwner, -1)
|
|
|
|
} else {
|
|
|
|
updateTotalSupply(ctx, +1)
|
|
|
|
}
|
|
|
|
ns := NameState{
|
2022-08-18 12:17:16 +00:00
|
|
|
Owner: owner,
|
|
|
|
Name: name,
|
|
|
|
// NNS expiration is in milliseconds
|
|
|
|
Expiration: int64(runtime.GetTime() + expire*1000),
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
2024-08-16 13:38:54 +00:00
|
|
|
checkAvailableGlobalDomain(ctx, name)
|
|
|
|
|
2021-07-22 09:46:38 +00:00
|
|
|
putNameStateWithKey(ctx, tokenKey, ns)
|
2021-09-28 08:58:18 +00:00
|
|
|
putSoaRecord(ctx, name, email, refresh, retry, expire, ttl)
|
2021-07-22 09:46:38 +00:00
|
|
|
updateBalance(ctx, []byte(name), owner, +1)
|
|
|
|
postTransfer(oldOwner, owner, []byte(name), nil)
|
2024-09-06 13:37:35 +00:00
|
|
|
runtime.Notify("RegisterDomain", name)
|
2021-07-22 09:46:38 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renew increases domain expiration date.
|
2022-07-06 09:22:43 +00:00
|
|
|
func Renew(name string) int64 {
|
2024-06-18 13:50:09 +00:00
|
|
|
checkDomainNameLength(name)
|
2021-07-22 09:46:38 +00:00
|
|
|
runtime.BurnGas(GetPrice())
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
ns := getNameState(ctx, []byte(name))
|
2022-08-23 09:06:05 +00:00
|
|
|
ns.checkAdmin()
|
2021-09-28 12:40:14 +00:00
|
|
|
ns.Expiration += millisecondsInYear
|
2021-07-22 09:46:38 +00:00
|
|
|
putNameState(ctx, ns)
|
|
|
|
return ns.Expiration
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// UpdateSOA updates soa record.
|
2021-09-28 12:40:14 +00:00
|
|
|
func UpdateSOA(name, email string, refresh, retry, expire, ttl int) {
|
2024-06-18 13:50:09 +00:00
|
|
|
checkDomainNameLength(name)
|
2021-09-28 12:40:14 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
ns := getNameState(ctx, []byte(name))
|
|
|
|
ns.checkAdmin()
|
|
|
|
putSoaRecord(ctx, name, email, refresh, retry, expire, ttl)
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:46:38 +00:00
|
|
|
// SetAdmin updates domain admin.
|
|
|
|
func SetAdmin(name string, admin interop.Hash160) {
|
2024-06-18 13:50:09 +00:00
|
|
|
checkDomainNameLength(name)
|
2021-07-22 09:46:38 +00:00
|
|
|
if admin != nil && !runtime.CheckWitness(admin) {
|
|
|
|
panic("not witnessed by admin")
|
|
|
|
}
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
ns := getNameState(ctx, []byte(name))
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckOwnerWitness(ns.Owner)
|
2021-07-22 09:46:38 +00:00
|
|
|
ns.Admin = admin
|
|
|
|
putNameState(ctx, ns)
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// SetRecord adds a new record of the specified type to the provided domain.
|
2021-09-28 12:40:14 +00:00
|
|
|
func SetRecord(name string, typ RecordType, id byte, data string) {
|
2021-07-22 09:46:38 +00:00
|
|
|
tokenID := []byte(tokenIDFromName(name))
|
2021-09-28 12:40:14 +00:00
|
|
|
if !checkBaseRecords(typ, data) {
|
|
|
|
panic("invalid record data")
|
|
|
|
}
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
ns := getNameState(ctx, tokenID)
|
|
|
|
ns.checkAdmin()
|
|
|
|
putRecord(ctx, tokenID, name, typ, id, data)
|
|
|
|
updateSoaSerial(ctx, tokenID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkBaseRecords(typ RecordType, data string) bool {
|
2021-07-22 09:46:38 +00:00
|
|
|
switch typ {
|
|
|
|
case A:
|
2021-09-28 12:40:14 +00:00
|
|
|
return checkIPv4(data)
|
2021-07-22 09:46:38 +00:00
|
|
|
case CNAME:
|
2023-12-12 17:22:33 +00:00
|
|
|
return splitAndCheck(data) != nil
|
2021-07-22 09:46:38 +00:00
|
|
|
case TXT:
|
2021-09-28 12:40:14 +00:00
|
|
|
return len(data) <= maxTXTRecordLength
|
2021-07-22 09:46:38 +00:00
|
|
|
case AAAA:
|
2021-09-28 12:40:14 +00:00
|
|
|
return checkIPv6(data)
|
2021-07-22 09:46:38 +00:00
|
|
|
default:
|
|
|
|
panic("unsupported record type")
|
|
|
|
}
|
2021-09-28 12:40:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// AddRecord adds a new record of the specified type to the provided domain.
|
2021-09-28 12:40:14 +00:00
|
|
|
func AddRecord(name string, typ RecordType, data string) {
|
|
|
|
tokenID := []byte(tokenIDFromName(name))
|
|
|
|
if !checkBaseRecords(typ, data) {
|
2021-07-22 09:46:38 +00:00
|
|
|
panic("invalid record data")
|
|
|
|
}
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
ns := getNameState(ctx, tokenID)
|
|
|
|
ns.checkAdmin()
|
2021-09-28 12:40:14 +00:00
|
|
|
addRecord(ctx, tokenID, name, typ, data)
|
|
|
|
updateSoaSerial(ctx, tokenID)
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 08:54:55 +00:00
|
|
|
// GetRecords returns domain record of the specified type if it exists or an empty
|
2021-07-22 09:46:38 +00:00
|
|
|
// string if not.
|
2021-09-28 08:54:55 +00:00
|
|
|
func GetRecords(name string, typ RecordType) []string {
|
2021-07-22 09:46:38 +00:00
|
|
|
tokenID := []byte(tokenIDFromName(name))
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
_ = getNameState(ctx, tokenID) // ensure not expired
|
2021-09-28 12:40:14 +00:00
|
|
|
return getRecordsByType(ctx, tokenID, name, typ)
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 12:40:14 +00:00
|
|
|
// DeleteRecords removes domain records with the specified type.
|
2021-09-28 08:54:55 +00:00
|
|
|
func DeleteRecords(name string, typ RecordType) {
|
2024-08-16 13:37:17 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
deleteRecords(ctx, name, typ)
|
|
|
|
}
|
|
|
|
|
2024-08-16 13:38:54 +00:00
|
|
|
// DeleteRecords removes domain records with the specified type.
|
2024-08-16 13:37:17 +00:00
|
|
|
func deleteRecords(ctx storage.Context, name string, typ RecordType) {
|
2021-09-28 12:40:14 +00:00
|
|
|
if typ == SOA {
|
|
|
|
panic("you cannot delete soa record")
|
|
|
|
}
|
2021-07-22 09:46:38 +00:00
|
|
|
tokenID := []byte(tokenIDFromName(name))
|
|
|
|
ns := getNameState(ctx, tokenID)
|
|
|
|
ns.checkAdmin()
|
2024-08-16 13:38:54 +00:00
|
|
|
|
|
|
|
globalDomainStorage := append([]byte{prefixGlobalDomain}, getTokenKey([]byte(name))...)
|
|
|
|
globalDomainRaw := storage.Get(ctx, globalDomainStorage)
|
|
|
|
globalDomain := globalDomainRaw.(string)
|
|
|
|
if globalDomainRaw != nil && globalDomain != "" {
|
|
|
|
deleteDomain(ctx, globalDomain)
|
|
|
|
}
|
|
|
|
|
2021-09-28 12:40:14 +00:00
|
|
|
recordsKey := getRecordsKeyByType(tokenID, name, typ)
|
2021-09-28 08:54:55 +00:00
|
|
|
records := storage.Find(ctx, recordsKey, storage.KeysOnly)
|
|
|
|
for iterator.Next(records) {
|
|
|
|
r := iterator.Value(records).(string)
|
|
|
|
storage.Delete(ctx, r)
|
|
|
|
}
|
2021-09-28 12:40:14 +00:00
|
|
|
updateSoaSerial(ctx, tokenID)
|
2024-09-06 13:37:35 +00:00
|
|
|
runtime.Notify("DeleteRecords", name, typ)
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 14:50:26 +00:00
|
|
|
// DeleteRecord delete a record of the specified type by data in the provided domain.
|
|
|
|
// Returns false if the record was not found.
|
|
|
|
func DeleteRecord(name string, typ RecordType, data string) bool {
|
|
|
|
tokenID := []byte(tokenIDFromName(name))
|
|
|
|
if !checkBaseRecords(typ, data) {
|
|
|
|
panic("invalid record data")
|
|
|
|
}
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
ns := getNameState(ctx, tokenID)
|
|
|
|
ns.checkAdmin()
|
|
|
|
return deleteRecord(ctx, tokenID, name, typ, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteRecord(ctx storage.Context, tokenId []byte, name string, typ RecordType, data string) bool {
|
|
|
|
recordsKey := getRecordsKeyByType(tokenId, name, typ)
|
|
|
|
|
|
|
|
var previousKey any
|
|
|
|
it := storage.Find(ctx, recordsKey, storage.KeysOnly)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
key := iterator.Value(it).([]byte)
|
|
|
|
ss := storage.Get(ctx, key).([]byte)
|
|
|
|
|
|
|
|
ns := std.Deserialize(ss).(RecordState)
|
|
|
|
if ns.Name == name && ns.Type == typ && ns.Data == data {
|
|
|
|
previousKey = key
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if previousKey != nil {
|
|
|
|
data := storage.Get(ctx, key)
|
|
|
|
storage.Put(ctx, previousKey, data)
|
|
|
|
previousKey = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if previousKey == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Delete(ctx, previousKey)
|
|
|
|
runtime.Notify("DeleteRecord", name, typ)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-08-16 13:37:17 +00:00
|
|
|
// DeleteDomain deletes the domain with the given name.
|
|
|
|
func DeleteDomain(name string) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
deleteDomain(ctx, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteDomain(ctx storage.Context, name string) {
|
2024-09-27 14:51:13 +00:00
|
|
|
it := Tokens()
|
|
|
|
for iterator.Next(it) {
|
|
|
|
domain := iterator.Value(it)
|
|
|
|
if std.MemorySearch([]byte(domain.(string)), []byte(name)) > 0 {
|
|
|
|
panic("can't delete a domain that has subdomains")
|
|
|
|
}
|
2024-08-16 13:37:17 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 14:51:13 +00:00
|
|
|
nsKey := append([]byte{prefixName}, getTokenKey([]byte(name))...)
|
|
|
|
nsRaw := storage.Get(ctx, nsKey)
|
|
|
|
if nsRaw == nil {
|
|
|
|
panic("domain not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
ns := std.Deserialize(nsRaw.([]byte)).(NameState)
|
|
|
|
ns.checkAdmin()
|
|
|
|
|
|
|
|
globalNSKey := append([]byte{prefixGlobalDomain}, getTokenKey([]byte(name))...)
|
|
|
|
globalDomainRaw := storage.Get(ctx, globalNSKey)
|
2024-08-16 13:38:54 +00:00
|
|
|
globalDomain := globalDomainRaw.(string)
|
|
|
|
if globalDomainRaw != nil && globalDomain != "" {
|
|
|
|
deleteDomain(ctx, globalDomain)
|
|
|
|
}
|
|
|
|
|
2024-08-16 13:37:17 +00:00
|
|
|
deleteRecords(ctx, name, CNAME)
|
|
|
|
deleteRecords(ctx, name, TXT)
|
|
|
|
deleteRecords(ctx, name, A)
|
|
|
|
deleteRecords(ctx, name, AAAA)
|
2024-09-27 14:51:13 +00:00
|
|
|
storage.Delete(ctx, nsKey)
|
|
|
|
storage.Delete(ctx, append([]byte{prefixRoot}, []byte(name)...))
|
2024-09-06 13:37:35 +00:00
|
|
|
runtime.Notify("DeleteDomain", name)
|
2024-08-16 13:37:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 09:46:38 +00:00
|
|
|
// Resolve resolves given name (not more then three redirects are allowed).
|
2021-09-28 08:54:55 +00:00
|
|
|
func Resolve(name string, typ RecordType) []string {
|
2021-07-22 09:46:38 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-09-28 08:54:55 +00:00
|
|
|
return resolve(ctx, nil, name, typ, 2)
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// GetAllRecords returns an Iterator with RecordState items for the given name.
|
2021-09-15 12:42:57 +00:00
|
|
|
func GetAllRecords(name string) iterator.Iterator {
|
|
|
|
tokenID := []byte(tokenIDFromName(name))
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
_ = getNameState(ctx, tokenID) // ensure not expired
|
|
|
|
recordsKey := getRecordsKey(tokenID, name)
|
|
|
|
return storage.Find(ctx, recordsKey, storage.ValuesOnly|storage.DeserializeValues)
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:46:38 +00:00
|
|
|
// updateBalance updates account's balance and account's tokens.
|
|
|
|
func updateBalance(ctx storage.Context, tokenId []byte, acc interop.Hash160, diff int) {
|
|
|
|
balanceKey := append([]byte{prefixBalance}, acc...)
|
|
|
|
var balance int
|
|
|
|
if b := storage.Get(ctx, balanceKey); b != nil {
|
2023-10-24 10:17:48 +00:00
|
|
|
balance = common.FromFixedWidth64(b.([]byte))
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
balance += diff
|
|
|
|
if balance == 0 {
|
|
|
|
storage.Delete(ctx, balanceKey)
|
|
|
|
} else {
|
2023-10-24 10:17:48 +00:00
|
|
|
storage.Put(ctx, balanceKey, common.ToFixedWidth64(balance))
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tokenKey := getTokenKey(tokenId)
|
|
|
|
accountTokenKey := append(append([]byte{prefixAccountToken}, acc...), tokenKey...)
|
|
|
|
if diff < 0 {
|
|
|
|
storage.Delete(ctx, accountTokenKey)
|
|
|
|
} else {
|
|
|
|
storage.Put(ctx, accountTokenKey, tokenId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// postTransfer sends Transfer notification to the network and calls onNEP11Payment
|
|
|
|
// method.
|
2023-11-07 12:18:48 +00:00
|
|
|
func postTransfer(from, to interop.Hash160, tokenID []byte, data any) {
|
2021-07-22 09:46:38 +00:00
|
|
|
runtime.Notify("Transfer", from, to, 1, tokenID)
|
|
|
|
if management.GetContract(to) != nil {
|
|
|
|
contract.Call(to, "onNEP11Payment", contract.All, from, 1, tokenID, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTotalSupply returns total supply from storage.
|
|
|
|
func getTotalSupply(ctx storage.Context) int {
|
|
|
|
val := storage.Get(ctx, []byte{prefixTotalSupply})
|
2023-10-24 10:17:48 +00:00
|
|
|
return common.FromFixedWidth64(val.([]byte))
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// updateTotalSupply adds the specified diff to the total supply.
|
2021-07-22 09:46:38 +00:00
|
|
|
func updateTotalSupply(ctx storage.Context, diff int) {
|
|
|
|
tsKey := []byte{prefixTotalSupply}
|
|
|
|
ts := getTotalSupply(ctx)
|
2023-10-24 10:17:48 +00:00
|
|
|
storage.Put(ctx, tsKey, common.ToFixedWidth64(ts+diff))
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getTokenKey computes hash160 from the given tokenID.
|
|
|
|
func getTokenKey(tokenID []byte) []byte {
|
|
|
|
return crypto.Ripemd160(tokenID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getNameState returns domain name state by the specified tokenID.
|
|
|
|
func getNameState(ctx storage.Context, tokenID []byte) NameState {
|
|
|
|
tokenKey := getTokenKey(tokenID)
|
2021-10-04 13:18:05 +00:00
|
|
|
ns := getNameStateWithKey(ctx, tokenKey)
|
|
|
|
fragments := std.StringSplit(string(tokenID), ".")
|
2024-04-15 23:56:31 +00:00
|
|
|
checkParentExists(ctx, fragments)
|
2021-10-04 13:18:05 +00:00
|
|
|
return ns
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getNameStateWithKey returns domain name state by the specified token key.
|
|
|
|
func getNameStateWithKey(ctx storage.Context, tokenKey []byte) NameState {
|
|
|
|
nameKey := append([]byte{prefixName}, tokenKey...)
|
|
|
|
nsBytes := storage.Get(ctx, nameKey)
|
|
|
|
if nsBytes == nil {
|
|
|
|
panic("token not found")
|
|
|
|
}
|
|
|
|
ns := std.Deserialize(nsBytes.([]byte)).(NameState)
|
|
|
|
ns.ensureNotExpired()
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
|
|
|
|
// putNameState stores domain name state.
|
|
|
|
func putNameState(ctx storage.Context, ns NameState) {
|
|
|
|
tokenKey := getTokenKey([]byte(ns.Name))
|
|
|
|
putNameStateWithKey(ctx, tokenKey, ns)
|
|
|
|
}
|
|
|
|
|
|
|
|
// putNameStateWithKey stores domain name state with the specified token key.
|
|
|
|
func putNameStateWithKey(ctx storage.Context, tokenKey []byte, ns NameState) {
|
|
|
|
nameKey := append([]byte{prefixName}, tokenKey...)
|
|
|
|
nsBytes := std.Serialize(ns)
|
|
|
|
storage.Put(ctx, nameKey, nsBytes)
|
|
|
|
}
|
|
|
|
|
2021-09-28 12:40:14 +00:00
|
|
|
// getRecordsByType returns domain record.
|
|
|
|
func getRecordsByType(ctx storage.Context, tokenId []byte, name string, typ RecordType) []string {
|
|
|
|
recordsKey := getRecordsKeyByType(tokenId, name, typ)
|
2021-09-28 08:54:55 +00:00
|
|
|
|
|
|
|
var result []string
|
2021-09-28 12:40:14 +00:00
|
|
|
records := storage.Find(ctx, recordsKey, storage.ValuesOnly|storage.DeserializeValues)
|
2021-09-28 08:54:55 +00:00
|
|
|
for iterator.Next(records) {
|
2021-09-28 12:40:14 +00:00
|
|
|
r := iterator.Value(records).(RecordState)
|
|
|
|
if r.Type == typ {
|
|
|
|
result = append(result, r.Data)
|
2021-09-28 08:54:55 +00:00
|
|
|
}
|
2021-09-15 12:42:57 +00:00
|
|
|
}
|
2021-09-28 08:54:55 +00:00
|
|
|
return result
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// putRecord stores domain record.
|
2021-09-28 12:40:14 +00:00
|
|
|
func putRecord(ctx storage.Context, tokenId []byte, name string, typ RecordType, id byte, data string) {
|
|
|
|
recordKey := getIdRecordKey(tokenId, name, typ, id)
|
|
|
|
recBytes := storage.Get(ctx, recordKey)
|
|
|
|
if recBytes == nil {
|
|
|
|
panic("invalid record id")
|
|
|
|
}
|
|
|
|
|
|
|
|
storeRecord(ctx, recordKey, name, typ, id, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// addRecord stores domain record.
|
|
|
|
func addRecord(ctx storage.Context, tokenId []byte, name string, typ RecordType, data string) {
|
|
|
|
recordsKey := getRecordsKeyByType(tokenId, name, typ)
|
|
|
|
|
|
|
|
var id byte
|
2021-11-17 14:10:07 +00:00
|
|
|
records := storage.Find(ctx, recordsKey, storage.ValuesOnly|storage.DeserializeValues)
|
2021-09-28 12:40:14 +00:00
|
|
|
for iterator.Next(records) {
|
|
|
|
id++
|
2021-11-17 14:10:07 +00:00
|
|
|
|
|
|
|
r := iterator.Value(records).(RecordState)
|
|
|
|
if r.Name == name && r.Type == typ && r.Data == data {
|
|
|
|
panic("record already exists")
|
|
|
|
}
|
2021-09-28 12:40:14 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 13:38:54 +00:00
|
|
|
globalDomainKey := append([]byte{prefixGlobalDomain}, getTokenKey([]byte(name))...)
|
|
|
|
globalDomainStorage := storage.Get(ctx, globalDomainKey)
|
|
|
|
globalDomain := getGlobalDomain(ctx, name)
|
|
|
|
|
|
|
|
if globalDomainStorage == nil && typ == TXT {
|
|
|
|
if globalDomain != "" {
|
|
|
|
checkAvailableGlobalDomain(ctx, name)
|
|
|
|
nsOriginal := getNameState(ctx, []byte(tokenIDFromName(name)))
|
|
|
|
ns := NameState{
|
|
|
|
Name: globalDomain,
|
|
|
|
Owner: nsOriginal.Owner,
|
|
|
|
Expiration: nsOriginal.Expiration,
|
|
|
|
Admin: nsOriginal.Admin,
|
|
|
|
}
|
|
|
|
|
|
|
|
putNameStateWithKey(ctx, getTokenKey([]byte(globalDomain)), ns)
|
|
|
|
storage.Put(ctx, globalDomainKey, globalDomain)
|
|
|
|
|
|
|
|
var oldOwner interop.Hash160
|
|
|
|
updateBalance(ctx, []byte(name), nsOriginal.Owner, +1)
|
|
|
|
postTransfer(oldOwner, nsOriginal.Owner, []byte(name), nil)
|
|
|
|
putCnameRecord(ctx, globalDomain, name)
|
|
|
|
} else {
|
|
|
|
storage.Put(ctx, globalDomainKey, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 12:40:14 +00:00
|
|
|
if typ == CNAME && id != 0 {
|
|
|
|
panic("you shouldn't have more than one CNAME record")
|
|
|
|
}
|
|
|
|
|
|
|
|
recordKey := append(recordsKey, id) // the same as getIdRecordKey
|
|
|
|
storeRecord(ctx, recordKey, name, typ, id, data)
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// storeRecord puts record to storage.
|
2021-09-28 12:40:14 +00:00
|
|
|
func storeRecord(ctx storage.Context, recordKey []byte, name string, typ RecordType, id byte, data string) {
|
2021-09-15 12:42:57 +00:00
|
|
|
rs := RecordState{
|
|
|
|
Name: name,
|
|
|
|
Type: typ,
|
2021-09-28 12:40:14 +00:00
|
|
|
Data: data,
|
|
|
|
ID: id,
|
2021-09-15 12:42:57 +00:00
|
|
|
}
|
|
|
|
recBytes := std.Serialize(rs)
|
|
|
|
storage.Put(ctx, recordKey, recBytes)
|
2024-09-06 13:37:35 +00:00
|
|
|
runtime.Notify("AddRecord", name, typ)
|
2021-09-15 12:42:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 08:58:18 +00:00
|
|
|
// putSoaRecord stores soa domain record.
|
|
|
|
func putSoaRecord(ctx storage.Context, name, email string, refresh, retry, expire, ttl int) {
|
2021-09-28 12:40:14 +00:00
|
|
|
var id byte
|
2021-09-28 08:58:18 +00:00
|
|
|
tokenId := []byte(tokenIDFromName(name))
|
2021-09-28 12:40:14 +00:00
|
|
|
recordKey := getIdRecordKey(tokenId, name, SOA, id)
|
2021-09-28 08:58:18 +00:00
|
|
|
rs := RecordState{
|
|
|
|
Name: name,
|
|
|
|
Type: SOA,
|
2021-09-28 12:40:14 +00:00
|
|
|
ID: id,
|
2021-09-28 08:58:18 +00:00
|
|
|
Data: name + " " + email + " " +
|
|
|
|
std.Itoa(runtime.GetTime(), 10) + " " +
|
|
|
|
std.Itoa(refresh, 10) + " " +
|
|
|
|
std.Itoa(retry, 10) + " " +
|
|
|
|
std.Itoa(expire, 10) + " " +
|
|
|
|
std.Itoa(ttl, 10),
|
|
|
|
}
|
|
|
|
recBytes := std.Serialize(rs)
|
|
|
|
storage.Put(ctx, recordKey, recBytes)
|
2024-09-06 13:37:35 +00:00
|
|
|
runtime.Notify("AddRecord", name, SOA)
|
2021-09-28 08:58:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 13:38:54 +00:00
|
|
|
// putCnameRecord stores CNAME domain record.
|
|
|
|
func putCnameRecord(ctx storage.Context, name, data string) {
|
|
|
|
var id byte
|
|
|
|
tokenId := []byte(tokenIDFromName(name))
|
|
|
|
recordKey := getIdRecordKey(tokenId, name, CNAME, id)
|
|
|
|
|
|
|
|
rs := RecordState{
|
|
|
|
Name: name,
|
|
|
|
Type: CNAME,
|
|
|
|
ID: id,
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
recBytes := std.Serialize(rs)
|
|
|
|
storage.Put(ctx, recordKey, recBytes)
|
2024-09-06 13:37:35 +00:00
|
|
|
runtime.Notify("AddRecord", name, CNAME)
|
2024-08-16 13:38:54 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 12:40:14 +00:00
|
|
|
// updateSoaSerial stores soa domain record.
|
|
|
|
func updateSoaSerial(ctx storage.Context, tokenId []byte) {
|
|
|
|
var id byte
|
|
|
|
recordKey := getIdRecordKey(tokenId, string(tokenId), SOA, id)
|
|
|
|
|
|
|
|
recBytes := storage.Get(ctx, recordKey)
|
|
|
|
if recBytes == nil {
|
2024-08-16 13:38:54 +00:00
|
|
|
return
|
2021-09-28 12:40:14 +00:00
|
|
|
}
|
|
|
|
rec := std.Deserialize(recBytes.([]byte)).(RecordState)
|
|
|
|
|
|
|
|
split := std.StringSplitNonEmpty(rec.Data, " ")
|
|
|
|
if len(split) != 7 {
|
|
|
|
panic("invalid soa record")
|
|
|
|
}
|
|
|
|
split[2] = std.Itoa(runtime.GetTime(), 10) // update serial
|
|
|
|
rec.Data = split[0] + " " + split[1] + " " +
|
|
|
|
split[2] + " " + split[3] + " " +
|
|
|
|
split[4] + " " + split[5] + " " +
|
|
|
|
split[6]
|
|
|
|
|
|
|
|
recBytes = std.Serialize(rec)
|
|
|
|
storage.Put(ctx, recordKey, recBytes)
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// getRecordsKey returns the prefix used to store domain records of different types.
|
2021-09-15 12:42:57 +00:00
|
|
|
func getRecordsKey(tokenId []byte, name string) []byte {
|
|
|
|
recordKey := append([]byte{prefixRecord}, getTokenKey(tokenId)...)
|
|
|
|
return append(recordKey, getTokenKey([]byte(name))...)
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// getRecordsKeyByType returns the key used to store domain records.
|
2021-09-28 12:40:14 +00:00
|
|
|
func getRecordsKeyByType(tokenId []byte, name string, typ RecordType) []byte {
|
2021-09-15 12:42:57 +00:00
|
|
|
recordKey := getRecordsKey(tokenId, name)
|
2021-09-28 12:40:14 +00:00
|
|
|
return append(recordKey, byte(typ))
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// getIdRecordKey returns the key used to store domain records.
|
2021-09-28 12:40:14 +00:00
|
|
|
func getIdRecordKey(tokenId []byte, name string, typ RecordType, id byte) []byte {
|
|
|
|
recordKey := getRecordsKey(tokenId, name)
|
|
|
|
return append(recordKey, byte(typ), id)
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// isValid returns true if the provided address is a valid Uint160.
|
|
|
|
func isValid(address interop.Hash160) bool {
|
2021-11-29 16:43:01 +00:00
|
|
|
return address != nil && len(address) == interop.Hash160Len
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkCommittee panics if the script container is not signed by the committee.
|
|
|
|
func checkCommittee() {
|
|
|
|
committee := neo.GetCommittee()
|
|
|
|
if committee == nil {
|
|
|
|
panic("failed to get committee")
|
|
|
|
}
|
|
|
|
l := len(committee)
|
|
|
|
committeeMultisig := contract.CreateMultisigAccount(l-(l-1)/2, committee)
|
|
|
|
if committeeMultisig == nil || !runtime.CheckWitness(committeeMultisig) {
|
|
|
|
panic("not witnessed by committee")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkFragment validates root or a part of domain name.
|
2021-11-26 14:05:06 +00:00
|
|
|
// 1. Root domain must start with a letter.
|
2022-04-14 11:56:51 +00:00
|
|
|
// 2. All other fragments must start and end with a letter or a digit.
|
2021-07-22 09:46:38 +00:00
|
|
|
func checkFragment(v string, isRoot bool) bool {
|
|
|
|
maxLength := maxDomainNameFragmentLength
|
|
|
|
if isRoot {
|
|
|
|
maxLength = maxRootLength
|
|
|
|
}
|
|
|
|
if len(v) == 0 || len(v) > maxLength {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
c := v[0]
|
|
|
|
if isRoot {
|
|
|
|
if !(c >= 'a' && c <= 'z') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !isAlNum(c) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 14:05:06 +00:00
|
|
|
for i := 1; i < len(v)-1; i++ {
|
|
|
|
if v[i] != '-' && !isAlNum(v[i]) {
|
2021-07-22 09:46:38 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 14:05:06 +00:00
|
|
|
return isAlNum(v[len(v)-1])
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// isAlNum checks whether provided char is a lowercase letter or a number.
|
|
|
|
func isAlNum(c uint8) bool {
|
|
|
|
return c >= 'a' && c <= 'z' || c >= '0' && c <= '9'
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitAndCheck splits domain name into parts and validates it.
|
2023-12-12 17:22:33 +00:00
|
|
|
func splitAndCheck(name string) []string {
|
2024-06-18 13:50:09 +00:00
|
|
|
checkDomainNameLength(name)
|
2021-07-22 09:46:38 +00:00
|
|
|
fragments := std.StringSplit(name, ".")
|
2024-06-18 13:50:09 +00:00
|
|
|
l := len(fragments)
|
2021-07-22 09:46:38 +00:00
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
if !checkFragment(fragments[i], i == l-1) {
|
2024-06-18 13:50:09 +00:00
|
|
|
panic(errInvalidDomainName + " '" + name + "': invalid fragment '" + fragments[i] + "'")
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fragments
|
|
|
|
}
|
|
|
|
|
2024-06-18 13:50:09 +00:00
|
|
|
// checkDomainNameLength panics if domain name length is out of boundaries.
|
|
|
|
func checkDomainNameLength(name string) {
|
|
|
|
l := len(name)
|
|
|
|
if l > maxDomainNameLength {
|
|
|
|
panic(errInvalidDomainName + " '" + name + "': domain name too long: got = " + std.Itoa(l, 10) + ", max = " + std.Itoa(maxDomainNameLength, 10))
|
|
|
|
}
|
|
|
|
if l < minDomainNameLength {
|
|
|
|
panic(errInvalidDomainName + " '" + name + "': domain name too short: got = " + std.Itoa(l, 10) + ", min = " + std.Itoa(minDomainNameLength, 10))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 09:46:38 +00:00
|
|
|
// checkIPv4 checks record on IPv4 compliance.
|
|
|
|
func checkIPv4(data string) bool {
|
|
|
|
l := len(data)
|
|
|
|
if l < 7 || 15 < l {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fragments := std.StringSplit(data, ".")
|
|
|
|
if len(fragments) != 4 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
numbers := make([]int, 4)
|
|
|
|
for i, f := range fragments {
|
|
|
|
if len(f) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
number := std.Atoi10(f)
|
|
|
|
if number < 0 || 255 < number {
|
|
|
|
panic("not a byte")
|
|
|
|
}
|
|
|
|
if number > 0 && f[0] == '0' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if number == 0 && len(f) > 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
numbers[i] = number
|
|
|
|
}
|
|
|
|
n0 := numbers[0]
|
|
|
|
n1 := numbers[1]
|
|
|
|
n3 := numbers[3]
|
|
|
|
if n0 == 0 ||
|
|
|
|
n0 == 10 ||
|
|
|
|
n0 == 127 ||
|
|
|
|
n0 >= 224 ||
|
|
|
|
(n0 == 169 && n1 == 254) ||
|
|
|
|
(n0 == 172 && 16 <= n1 && n1 <= 31) ||
|
|
|
|
(n0 == 192 && n1 == 168) ||
|
|
|
|
n3 == 0 ||
|
|
|
|
n3 == 255 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkIPv6 checks record on IPv6 compliance.
|
|
|
|
func checkIPv6(data string) bool {
|
|
|
|
l := len(data)
|
|
|
|
if l < 2 || 39 < l {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fragments := std.StringSplit(data, ":")
|
|
|
|
l = len(fragments)
|
|
|
|
if l < 3 || 8 < l {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var hasEmpty bool
|
|
|
|
nums := make([]int, 8)
|
|
|
|
for i, f := range fragments {
|
|
|
|
if len(f) == 0 {
|
|
|
|
if i == 0 {
|
2021-09-15 12:42:57 +00:00
|
|
|
if len(fragments[1]) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
2021-07-22 09:46:38 +00:00
|
|
|
nums[i] = 0
|
|
|
|
} else if i == l-1 {
|
2021-09-15 12:42:57 +00:00
|
|
|
if len(fragments[i-1]) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
2021-07-22 09:46:38 +00:00
|
|
|
nums[7] = 0
|
|
|
|
} else if hasEmpty {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
hasEmpty = true
|
|
|
|
endIndex := 9 - l + i
|
|
|
|
for j := i; j < endIndex; j++ {
|
|
|
|
nums[j] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(f) > 4 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
n := std.Atoi(f, 16)
|
|
|
|
if 65535 < n {
|
|
|
|
panic("fragment overflows uint16: " + f)
|
|
|
|
}
|
|
|
|
idx := i
|
|
|
|
if hasEmpty {
|
|
|
|
idx = i + 8 - l
|
|
|
|
}
|
|
|
|
nums[idx] = n
|
|
|
|
}
|
|
|
|
}
|
2021-09-15 12:42:57 +00:00
|
|
|
if l < 8 && !hasEmpty {
|
|
|
|
return false
|
|
|
|
}
|
2021-07-22 09:46:38 +00:00
|
|
|
|
|
|
|
f0 := nums[0]
|
|
|
|
if f0 < 0x2000 || f0 == 0x2002 || f0 == 0x3ffe || f0 > 0x3fff { // IPv6 Global Unicast https://www.iana.org/assignments/ipv6-address-space/ipv6-address-space.xhtml
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if f0 == 0x2001 {
|
|
|
|
f1 := nums[1]
|
|
|
|
if f1 < 0x200 || f1 == 0xdb8 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// tokenIDFromName returns token ID (domain.root) from the provided name.
|
2021-07-22 09:46:38 +00:00
|
|
|
func tokenIDFromName(name string) string {
|
2023-12-12 17:22:33 +00:00
|
|
|
fragments := splitAndCheck(name)
|
2021-11-19 13:35:58 +00:00
|
|
|
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
sum := 0
|
|
|
|
l := len(fragments) - 1
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
tokenKey := getTokenKey([]byte(name[sum:]))
|
|
|
|
nameKey := append([]byte{prefixName}, tokenKey...)
|
|
|
|
nsBytes := storage.Get(ctx, nameKey)
|
|
|
|
if nsBytes != nil {
|
|
|
|
ns := std.Deserialize(nsBytes.([]byte)).(NameState)
|
2022-07-06 09:22:43 +00:00
|
|
|
if int64(runtime.GetTime()) < ns.Expiration {
|
2021-11-19 13:35:58 +00:00
|
|
|
return name[sum:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sum += len(fragments[i]) + 1
|
2021-10-04 10:26:23 +00:00
|
|
|
}
|
2021-11-19 13:35:58 +00:00
|
|
|
return name
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// resolve resolves the provided name using record with the specified type and given
|
2021-07-22 09:46:38 +00:00
|
|
|
// maximum redirections constraint.
|
2021-09-28 08:54:55 +00:00
|
|
|
func resolve(ctx storage.Context, res []string, name string, typ RecordType, redirect int) []string {
|
2021-07-22 09:46:38 +00:00
|
|
|
if redirect < 0 {
|
|
|
|
panic("invalid redirect")
|
|
|
|
}
|
2021-10-16 09:47:52 +00:00
|
|
|
if len(name) == 0 {
|
|
|
|
panic("invalid name")
|
|
|
|
}
|
|
|
|
if name[len(name)-1] == '.' {
|
|
|
|
name = name[:len(name)-1]
|
|
|
|
}
|
2021-09-28 08:54:55 +00:00
|
|
|
records := getAllRecords(ctx, name)
|
2021-07-22 09:46:38 +00:00
|
|
|
cname := ""
|
|
|
|
for iterator.Next(records) {
|
2021-09-28 12:40:14 +00:00
|
|
|
r := iterator.Value(records).(RecordState)
|
|
|
|
if r.Type == typ {
|
|
|
|
res = append(res, r.Data)
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
2021-09-28 12:40:14 +00:00
|
|
|
if r.Type == CNAME {
|
|
|
|
cname = r.Data
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-28 08:54:55 +00:00
|
|
|
if cname == "" || typ == CNAME {
|
|
|
|
return res
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
2021-09-28 08:54:55 +00:00
|
|
|
|
|
|
|
res = append(res, cname)
|
|
|
|
return resolve(ctx, res, cname, typ, redirect-1)
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 08:54:55 +00:00
|
|
|
// getAllRecords returns iterator over the set of records corresponded with the
|
2021-07-22 09:46:38 +00:00
|
|
|
// specified name.
|
2021-09-28 08:54:55 +00:00
|
|
|
func getAllRecords(ctx storage.Context, name string) iterator.Iterator {
|
2021-07-22 09:46:38 +00:00
|
|
|
tokenID := []byte(tokenIDFromName(name))
|
|
|
|
_ = getNameState(ctx, tokenID)
|
2021-09-15 12:42:57 +00:00
|
|
|
recordsKey := getRecordsKey(tokenID, name)
|
2021-09-28 12:40:14 +00:00
|
|
|
return storage.Find(ctx, recordsKey, storage.ValuesOnly|storage.DeserializeValues)
|
2021-07-22 09:46:38 +00:00
|
|
|
}
|