2021-01-22 12:12:09 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
"net"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2021-03-24 10:16:38 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nnsrecords"
|
2021-01-22 12:12:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NameService represents native NameService contract.
|
|
|
|
type NameService struct {
|
|
|
|
nonfungible
|
|
|
|
NEO *NEO
|
|
|
|
}
|
|
|
|
|
|
|
|
type nameState struct {
|
|
|
|
state.NFTTokenState
|
|
|
|
// Expiration is token expiration height.
|
|
|
|
Expiration uint32
|
|
|
|
// HasAdmin is true if token has admin.
|
|
|
|
HasAdmin bool
|
|
|
|
// Admin is token admin.
|
|
|
|
Admin util.Uint160
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-02-15 15:43:10 +00:00
|
|
|
nameServiceID = -10
|
2021-01-22 12:12:09 +00:00
|
|
|
|
|
|
|
prefixRoots = 10
|
|
|
|
prefixDomainPrice = 22
|
|
|
|
prefixExpiration = 20
|
|
|
|
prefixRecord = 12
|
|
|
|
|
|
|
|
secondsInYear = 365 * 24 * 3600
|
|
|
|
|
|
|
|
// DefaultDomainPrice is the default price of register method.
|
|
|
|
DefaultDomainPrice = 10_00000000
|
|
|
|
// MinDomainNameLength is minimum domain length.
|
|
|
|
MinDomainNameLength = 3
|
|
|
|
// MaxDomainNameLength is maximum domain length.
|
|
|
|
MaxDomainNameLength = 255
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Lookahead is not supported by Go, but it is simple `(?=.{3,255}$)`,
|
|
|
|
// so we check name length explicitly.
|
2021-05-12 15:38:24 +00:00
|
|
|
nameRegex = regexp.MustCompile(`^([a-z0-9]{1,62}\.)+[a-z][a-z0-9]{0,15}$`)
|
|
|
|
ipv4Regex = regexp.MustCompile(`^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$`)
|
2021-02-17 07:42:00 +00:00
|
|
|
ipv6Regex = regexp.MustCompile("(?:^)(([0-9a-f]{1,4}:){7,7}[0-9a-f]{1,4}|([0-9a-f]{1,4}:){1,7}:|([0-9a-f]{1,4}:){1,6}:[0-9a-f]{1,4}|([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}|([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}|([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}|([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}|[0-9a-f]{1,4}:((:[0-9a-f]{1,4}){1,6})|:((:[0-9a-f]{1,4}){1,7}|:))$")
|
2021-01-22 12:12:09 +00:00
|
|
|
rootRegex = regexp.MustCompile("^[a-z][a-z0-9]{0,15}$")
|
|
|
|
)
|
|
|
|
|
|
|
|
// matchName checks if provided name is valid.
|
|
|
|
func matchName(name string) bool {
|
|
|
|
ln := len(name)
|
|
|
|
return MinDomainNameLength <= ln && ln <= MaxDomainNameLength &&
|
|
|
|
nameRegex.Match([]byte(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNameService() *NameService {
|
|
|
|
nf := newNonFungible(nativenames.NameService, nameServiceID, "NNS", 0)
|
|
|
|
nf.getTokenKey = func(tokenID []byte) []byte {
|
|
|
|
return append([]byte{prefixNFTToken}, hash.Hash160(tokenID).BytesBE()...)
|
|
|
|
}
|
|
|
|
nf.newTokenState = func() nftTokenState {
|
|
|
|
return new(nameState)
|
|
|
|
}
|
|
|
|
nf.onTransferred = func(tok nftTokenState) {
|
|
|
|
tok.(*nameState).HasAdmin = false
|
|
|
|
}
|
|
|
|
|
|
|
|
n := &NameService{nonfungible: *nf}
|
2021-02-15 13:40:44 +00:00
|
|
|
defer n.UpdateHash()
|
2021-01-22 12:12:09 +00:00
|
|
|
|
|
|
|
desc := newDescriptor("addRoot", smartcontract.VoidType,
|
|
|
|
manifest.NewParameter("root", smartcontract.StringType))
|
2021-03-05 10:30:16 +00:00
|
|
|
md := newMethodAndPrice(n.addRoot, 1<<15, callflag.States)
|
2021-01-22 12:12:09 +00:00
|
|
|
n.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("setPrice", smartcontract.VoidType,
|
|
|
|
manifest.NewParameter("price", smartcontract.IntegerType))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(n.setPrice, 1<<15, callflag.States)
|
2021-01-22 12:12:09 +00:00
|
|
|
n.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("getPrice", smartcontract.IntegerType)
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(n.getPrice, 1<<15, callflag.ReadStates)
|
2021-01-22 12:12:09 +00:00
|
|
|
n.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("isAvailable", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("name", smartcontract.StringType))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(n.isAvailable, 1<<15, callflag.ReadStates)
|
2021-01-22 12:12:09 +00:00
|
|
|
n.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("register", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("name", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("owner", smartcontract.Hash160Type))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(n.register, 1<<15, callflag.States)
|
2021-01-22 12:12:09 +00:00
|
|
|
n.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("renew", smartcontract.IntegerType,
|
|
|
|
manifest.NewParameter("name", smartcontract.StringType))
|
2021-02-25 15:04:46 +00:00
|
|
|
md = newMethodAndPrice(n.renew, 0, callflag.States)
|
2021-01-22 12:12:09 +00:00
|
|
|
n.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("setAdmin", smartcontract.VoidType,
|
|
|
|
manifest.NewParameter("name", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("admin", smartcontract.Hash160Type))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(n.setAdmin, 1<<15, callflag.States)
|
2021-03-05 11:53:46 +00:00
|
|
|
md.StorageFee = 20
|
2021-01-22 12:12:09 +00:00
|
|
|
n.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("setRecord", smartcontract.VoidType,
|
|
|
|
manifest.NewParameter("name", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("type", smartcontract.IntegerType),
|
|
|
|
manifest.NewParameter("data", smartcontract.StringType))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(n.setRecord, 1<<15, callflag.States)
|
2021-03-05 11:53:46 +00:00
|
|
|
md.StorageFee = 200
|
2021-01-22 12:12:09 +00:00
|
|
|
n.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("getRecord", smartcontract.StringType,
|
|
|
|
manifest.NewParameter("name", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("type", smartcontract.IntegerType))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(n.getRecord, 1<<15, callflag.ReadStates)
|
2021-01-22 12:12:09 +00:00
|
|
|
n.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("deleteRecord", smartcontract.VoidType,
|
|
|
|
manifest.NewParameter("name", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("type", smartcontract.IntegerType))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(n.deleteRecord, 1<<15, callflag.States)
|
2021-01-22 12:12:09 +00:00
|
|
|
n.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("resolve", smartcontract.StringType,
|
|
|
|
manifest.NewParameter("name", smartcontract.StringType),
|
|
|
|
manifest.NewParameter("type", smartcontract.IntegerType))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(n.resolve, 1<<17, callflag.ReadStates)
|
2021-01-22 12:12:09 +00:00
|
|
|
n.AddMethod(md, desc)
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata implements interop.Contract interface.
|
|
|
|
func (n *NameService) Metadata() *interop.ContractMD {
|
|
|
|
return &n.ContractMD
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize implements interop.Contract interface.
|
|
|
|
func (n *NameService) Initialize(ic *interop.Context) error {
|
2021-02-02 15:46:43 +00:00
|
|
|
if err := n.nonfungible.Initialize(ic); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-09 09:26:25 +00:00
|
|
|
if err := setIntWithKey(n.ID, ic.DAO, []byte{prefixDomainPrice}, DefaultDomainPrice); err != nil {
|
2021-01-22 12:12:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
roots := stringList{}
|
2021-02-09 09:26:25 +00:00
|
|
|
return putSerializableToDAO(n.ID, ic.DAO, []byte{prefixRoots}, &roots)
|
2021-01-22 12:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnPersist implements interop.Contract interface.
|
|
|
|
func (n *NameService) OnPersist(ic *interop.Context) error {
|
|
|
|
now := uint32(ic.Block.Timestamp/1000 + 1)
|
|
|
|
keys := []string{}
|
2021-02-09 09:26:25 +00:00
|
|
|
ic.DAO.Seek(n.ID, []byte{prefixExpiration}, func(k, v []byte) {
|
2021-01-22 12:12:09 +00:00
|
|
|
if binary.BigEndian.Uint32(k) >= now {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Removal is done separately because of `Seek` takes storage mutex.
|
|
|
|
keys = append(keys, string(k))
|
|
|
|
})
|
|
|
|
|
|
|
|
var keysToRemove [][]byte
|
|
|
|
key := []byte{prefixExpiration}
|
|
|
|
keyRecord := []byte{prefixRecord}
|
|
|
|
for i := range keys {
|
|
|
|
key[0] = prefixExpiration
|
|
|
|
key = append(key[:1], []byte(keys[i])...)
|
2021-02-09 09:26:25 +00:00
|
|
|
if err := ic.DAO.DeleteStorageItem(n.ID, key); err != nil {
|
2021-01-22 12:12:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
keysToRemove = keysToRemove[:0]
|
|
|
|
key[0] = prefixRecord
|
|
|
|
key = append(key[:1], keys[i][4:]...)
|
2021-02-09 09:26:25 +00:00
|
|
|
ic.DAO.Seek(n.ID, key, func(k, v []byte) {
|
2021-01-22 12:12:09 +00:00
|
|
|
keysToRemove = append(keysToRemove, k)
|
|
|
|
})
|
|
|
|
for i := range keysToRemove {
|
|
|
|
keyRecord = append(keyRecord[:0], key...)
|
|
|
|
keyRecord = append(keyRecord, keysToRemove[i]...)
|
2021-02-09 09:26:25 +00:00
|
|
|
err := ic.DAO.DeleteStorageItem(n.ID, keyRecord)
|
2021-01-22 12:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
key[0] = prefixNFTToken
|
|
|
|
n.burnByKey(ic, key)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostPersist implements interop.Contract interface.
|
|
|
|
func (n *NameService) PostPersist(ic *interop.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) addRoot(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
root := toString(args[0])
|
|
|
|
if !rootRegex.Match([]byte(root)) {
|
|
|
|
panic("invalid root")
|
|
|
|
}
|
|
|
|
|
|
|
|
n.checkCommittee(ic)
|
|
|
|
roots, _ := n.getRootsInternal(ic.DAO)
|
|
|
|
if !roots.add(root) {
|
|
|
|
panic("name already exists")
|
|
|
|
}
|
|
|
|
|
2021-02-09 09:26:25 +00:00
|
|
|
err := putSerializableToDAO(n.ID, ic.DAO, []byte{prefixRoots}, &roots)
|
2021-01-22 12:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var maxPrice = big.NewInt(10000_00000000)
|
|
|
|
|
|
|
|
func (n *NameService) setPrice(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
price := toBigInt(args[0])
|
|
|
|
if price.Sign() <= 0 || price.Cmp(maxPrice) >= 0 {
|
|
|
|
panic("invalid price")
|
|
|
|
}
|
|
|
|
|
|
|
|
n.checkCommittee(ic)
|
2021-03-05 14:06:54 +00:00
|
|
|
err := ic.DAO.PutStorageItem(n.ID, []byte{prefixDomainPrice}, bigint.ToBytes(price))
|
2021-01-22 12:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) getPrice(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
|
|
|
return stackitem.NewBigInteger(n.getPriceInternal(ic.DAO))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) getPriceInternal(d dao.DAO) *big.Int {
|
2021-02-09 09:26:25 +00:00
|
|
|
si := d.GetStorageItem(n.ID, []byte{prefixDomainPrice})
|
2021-03-05 14:06:54 +00:00
|
|
|
return bigint.FromBytes(si)
|
2021-01-22 12:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) parseName(item stackitem.Item) (string, []string, []byte) {
|
|
|
|
name := toName(item)
|
|
|
|
names := strings.Split(name, ".")
|
|
|
|
if len(names) != 2 {
|
|
|
|
panic("invalid name")
|
|
|
|
}
|
|
|
|
return name, names, n.getTokenKey([]byte(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) isAvailable(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
_, names, key := n.parseName(args[0])
|
2021-02-09 09:26:25 +00:00
|
|
|
if ic.DAO.GetStorageItem(n.ID, key) != nil {
|
2021-01-22 12:12:09 +00:00
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
roots, _ := n.getRootsInternal(ic.DAO)
|
|
|
|
_, ok := roots.index(names[1])
|
|
|
|
if !ok {
|
|
|
|
panic("domain is not registered")
|
|
|
|
}
|
|
|
|
return stackitem.NewBool(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) getRootsInternal(d dao.DAO) (stringList, bool) {
|
|
|
|
var sl stringList
|
2021-02-09 09:26:25 +00:00
|
|
|
err := getSerializableFromDAO(n.ID, d, []byte{prefixRoots}, &sl)
|
2021-01-22 12:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
// Roots are being stored in `Initialize()` and thus must always be present.
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return sl, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) register(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
name, names, key := n.parseName(args[0])
|
|
|
|
owner := toUint160(args[1])
|
|
|
|
if !n.checkWitness(ic, owner) {
|
|
|
|
panic("owner is not witnessed")
|
|
|
|
}
|
|
|
|
|
2021-02-09 09:26:25 +00:00
|
|
|
if ic.DAO.GetStorageItem(n.ID, key) != nil {
|
2021-01-22 12:12:09 +00:00
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
roots, _ := n.getRootsInternal(ic.DAO)
|
|
|
|
if _, ok := roots.index(names[1]); !ok {
|
|
|
|
panic("missing root")
|
|
|
|
}
|
|
|
|
if !ic.VM.AddGas(n.getPriceInternal(ic.DAO).Int64()) {
|
|
|
|
panic("insufficient gas")
|
|
|
|
}
|
|
|
|
token := &nameState{
|
|
|
|
NFTTokenState: state.NFTTokenState{
|
|
|
|
Owner: owner,
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
Expiration: uint32(ic.Block.Timestamp/1000 + secondsInYear),
|
|
|
|
}
|
|
|
|
n.mint(ic, token)
|
2021-02-09 09:26:25 +00:00
|
|
|
err := ic.DAO.PutStorageItem(n.ID,
|
2021-01-22 12:12:09 +00:00
|
|
|
makeExpirationKey(token.Expiration, token.ID()),
|
2021-03-05 14:06:54 +00:00
|
|
|
state.StorageItem{0})
|
2021-01-22 12:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return stackitem.NewBool(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) renew(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
_, _, key := n.parseName(args[0])
|
|
|
|
if !ic.VM.AddGas(n.getPriceInternal(ic.DAO).Int64()) {
|
|
|
|
panic("insufficient gas")
|
|
|
|
}
|
|
|
|
token := new(nameState)
|
2021-02-09 09:26:25 +00:00
|
|
|
err := getSerializableFromDAO(n.ID, ic.DAO, key, token)
|
2021-01-22 12:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyExpiration := makeExpirationKey(token.Expiration, token.ID())
|
2021-02-09 09:26:25 +00:00
|
|
|
if err := ic.DAO.DeleteStorageItem(n.ID, keyExpiration); err != nil {
|
2021-01-22 12:12:09 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
token.Expiration += secondsInYear
|
2021-02-09 09:26:25 +00:00
|
|
|
err = putSerializableToDAO(n.ID, ic.DAO, key, token)
|
2021-01-22 12:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint32(key[1:], token.Expiration)
|
2021-03-05 14:06:54 +00:00
|
|
|
err = ic.DAO.PutStorageItem(n.ID, key, state.StorageItem{0})
|
2021-01-22 12:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
bi := new(big.Int).SetUint64(uint64(token.Expiration))
|
|
|
|
return stackitem.NewBigInteger(bi)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) setAdmin(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
_, _, key := n.parseName(args[0])
|
|
|
|
|
|
|
|
var admin util.Uint160
|
|
|
|
_, isNull := args[1].(stackitem.Null)
|
|
|
|
if !isNull {
|
|
|
|
admin = toUint160(args[1])
|
|
|
|
if !n.checkWitness(ic, admin) {
|
|
|
|
panic("not witnessed by admin")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
token := new(nameState)
|
2021-02-09 09:26:25 +00:00
|
|
|
err := getSerializableFromDAO(n.ID, ic.DAO, key, token)
|
2021-01-22 12:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !n.checkWitness(ic, token.Owner) {
|
|
|
|
panic("only owner can set admin")
|
|
|
|
}
|
|
|
|
token.HasAdmin = !isNull
|
|
|
|
token.Admin = admin
|
2021-02-09 09:26:25 +00:00
|
|
|
err = putSerializableToDAO(n.ID, ic.DAO, key, token)
|
2021-01-22 12:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) checkWitness(ic *interop.Context, owner util.Uint160) bool {
|
|
|
|
ok, err := runtime.CheckHashedWitness(ic, owner)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) checkCommittee(ic *interop.Context) {
|
|
|
|
if !n.NEO.checkCommittee(ic) {
|
|
|
|
panic("not witnessed by committee")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) checkAdmin(ic *interop.Context, token *nameState) bool {
|
|
|
|
if n.checkWitness(ic, token.Owner) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return token.HasAdmin && n.checkWitness(ic, token.Admin)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) setRecord(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
name := toName(args[0])
|
|
|
|
rt := toRecordType(args[1])
|
|
|
|
data := toString(args[2])
|
2021-02-17 10:29:18 +00:00
|
|
|
checkName(rt, data)
|
2021-01-22 12:12:09 +00:00
|
|
|
|
|
|
|
domain := toDomain(name)
|
|
|
|
token, _, err := n.tokenState(ic.DAO, []byte(domain))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !n.checkAdmin(ic, token.(*nameState)) {
|
|
|
|
panic("not witnessed by admin")
|
|
|
|
}
|
|
|
|
key := makeRecordKey(domain, name, rt)
|
2021-03-05 14:06:54 +00:00
|
|
|
si := state.StorageItem(data)
|
2021-02-09 09:26:25 +00:00
|
|
|
if err := ic.DAO.PutStorageItem(n.ID, key, si); err != nil {
|
2021-01-22 12:12:09 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:16:38 +00:00
|
|
|
func checkName(rt nnsrecords.Type, name string) {
|
2021-01-22 12:12:09 +00:00
|
|
|
var valid bool
|
|
|
|
switch rt {
|
2021-03-24 10:16:38 +00:00
|
|
|
case nnsrecords.A:
|
2021-01-22 12:12:09 +00:00
|
|
|
// We can't rely on `len(ip) == net.IPv4len` because
|
|
|
|
// IPv4 can be parsed to mapped representation.
|
|
|
|
valid = ipv4Regex.MatchString(name) &&
|
|
|
|
net.ParseIP(name) != nil
|
2021-03-24 10:16:38 +00:00
|
|
|
case nnsrecords.CNAME:
|
2021-01-22 12:12:09 +00:00
|
|
|
valid = matchName(name)
|
2021-03-24 10:16:38 +00:00
|
|
|
case nnsrecords.TXT:
|
2021-01-22 12:12:09 +00:00
|
|
|
valid = utf8.RuneCountInString(name) <= 255
|
2021-03-24 10:16:38 +00:00
|
|
|
case nnsrecords.AAAA:
|
2021-01-22 12:12:09 +00:00
|
|
|
valid = ipv6Regex.MatchString(name) &&
|
|
|
|
net.ParseIP(name) != nil
|
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
panic("invalid name")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) getRecord(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
name := toName(args[0])
|
|
|
|
domain := toDomain(name)
|
|
|
|
rt := toRecordType(args[1])
|
|
|
|
key := makeRecordKey(domain, name, rt)
|
2021-02-09 09:26:25 +00:00
|
|
|
si := ic.DAO.GetStorageItem(n.ID, key)
|
2021-01-22 12:12:09 +00:00
|
|
|
if si == nil {
|
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
2021-03-05 14:06:54 +00:00
|
|
|
return stackitem.NewByteArray(si)
|
2021-01-22 12:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) deleteRecord(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
name := toName(args[0])
|
|
|
|
rt := toRecordType(args[1])
|
|
|
|
domain := toDomain(name)
|
|
|
|
key := n.getTokenKey([]byte(domain))
|
|
|
|
token := new(nameState)
|
2021-02-09 09:26:25 +00:00
|
|
|
err := getSerializableFromDAO(n.ID, ic.DAO, key, token)
|
2021-01-22 12:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !n.checkAdmin(ic, token) {
|
|
|
|
panic("not witnessed by admin")
|
|
|
|
}
|
|
|
|
|
|
|
|
key = makeRecordKey(domain, name, rt)
|
2021-02-09 09:26:25 +00:00
|
|
|
if err := ic.DAO.DeleteStorageItem(n.ID, key); err != nil {
|
2021-01-22 12:12:09 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NameService) resolve(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
name := toString(args[0])
|
|
|
|
rt := toRecordType(args[1])
|
|
|
|
result, ok := n.resolveInternal(ic, name, rt, 2)
|
|
|
|
if !ok {
|
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
return stackitem.NewByteArray([]byte(result))
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:16:38 +00:00
|
|
|
func (n *NameService) resolveInternal(ic *interop.Context, name string, t nnsrecords.Type, redirect int) (string, bool) {
|
2021-01-22 12:12:09 +00:00
|
|
|
if redirect < 0 {
|
|
|
|
panic("invalid redirect")
|
|
|
|
}
|
|
|
|
records := n.getRecordsInternal(ic.DAO, name)
|
|
|
|
if data, ok := records[t]; ok {
|
|
|
|
return data, true
|
|
|
|
}
|
2021-03-24 10:16:38 +00:00
|
|
|
data, ok := records[nnsrecords.CNAME]
|
2021-01-22 12:12:09 +00:00
|
|
|
if !ok {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
return n.resolveInternal(ic, data, t, redirect-1)
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:16:38 +00:00
|
|
|
func (n *NameService) getRecordsInternal(d dao.DAO, name string) map[nnsrecords.Type]string {
|
2021-01-22 12:12:09 +00:00
|
|
|
domain := toDomain(name)
|
|
|
|
key := makeRecordKey(domain, name, 0)
|
|
|
|
key = key[:len(key)-1]
|
2021-03-24 10:16:38 +00:00
|
|
|
res := make(map[nnsrecords.Type]string)
|
2021-02-09 09:26:25 +00:00
|
|
|
d.Seek(n.ID, key, func(k, v []byte) {
|
2021-03-24 10:16:38 +00:00
|
|
|
rt := nnsrecords.Type(k[len(k)-1])
|
2021-03-09 09:09:44 +00:00
|
|
|
res[rt] = string(v)
|
2021-01-22 12:12:09 +00:00
|
|
|
})
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:16:38 +00:00
|
|
|
func makeRecordKey(domain, name string, rt nnsrecords.Type) []byte {
|
2021-01-22 12:12:09 +00:00
|
|
|
key := make([]byte, 1+util.Uint160Size+util.Uint160Size+1)
|
|
|
|
key[0] = prefixRecord
|
|
|
|
i := 1
|
|
|
|
i += copy(key[i:], hash.Hash160([]byte(domain)).BytesBE())
|
|
|
|
i += copy(key[i:], hash.Hash160([]byte(name)).BytesBE())
|
|
|
|
key[i] = byte(rt)
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeExpirationKey(expiration uint32, tokenID []byte) []byte {
|
|
|
|
key := make([]byte, 1+4+util.Uint160Size)
|
|
|
|
key[0] = prefixExpiration
|
|
|
|
binary.BigEndian.PutUint32(key[1:], expiration)
|
|
|
|
copy(key[5:], hash.Hash160(tokenID).BytesBE())
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToMap implements nftTokenState interface.
|
|
|
|
func (s *nameState) ToMap() *stackitem.Map {
|
|
|
|
m := s.NFTTokenState.ToMap()
|
|
|
|
m.Add(stackitem.NewByteArray([]byte("expiration")),
|
|
|
|
stackitem.NewBigInteger(new(big.Int).SetUint64(uint64(s.Expiration))))
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable.
|
|
|
|
func (s *nameState) EncodeBinary(w *io.BinWriter) {
|
|
|
|
stackitem.EncodeBinaryStackItem(s.ToStackItem(), w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable.
|
|
|
|
func (s *nameState) DecodeBinary(r *io.BinReader) {
|
|
|
|
item := stackitem.DecodeBinaryStackItem(r)
|
|
|
|
if r.Err == nil {
|
2021-05-12 18:49:42 +00:00
|
|
|
r.Err = s.FromStackItem(item)
|
2021-01-22 12:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStackItem implements nftTokenState interface.
|
|
|
|
func (s *nameState) ToStackItem() stackitem.Item {
|
|
|
|
item := s.NFTTokenState.ToStackItem().(*stackitem.Struct)
|
|
|
|
exp := new(big.Int).SetUint64(uint64(s.Expiration))
|
|
|
|
item.Append(stackitem.NewBigInteger(exp))
|
|
|
|
if s.HasAdmin {
|
|
|
|
item.Append(stackitem.NewByteArray(s.Admin.BytesBE()))
|
|
|
|
} else {
|
|
|
|
item.Append(stackitem.Null{})
|
|
|
|
}
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromStackItem implements nftTokenState interface.
|
|
|
|
func (s *nameState) FromStackItem(item stackitem.Item) error {
|
|
|
|
err := s.NFTTokenState.FromStackItem(item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
elems := item.Value().([]stackitem.Item)
|
2021-02-16 13:24:05 +00:00
|
|
|
if len(elems) < 4 {
|
2021-01-22 12:12:09 +00:00
|
|
|
return errors.New("invalid stack item")
|
|
|
|
}
|
2021-02-16 13:24:05 +00:00
|
|
|
bi, err := elems[2].TryInteger()
|
2021-01-22 12:12:09 +00:00
|
|
|
if err != nil || !bi.IsUint64() {
|
|
|
|
return errors.New("invalid stack item")
|
|
|
|
}
|
|
|
|
|
2021-02-16 13:24:05 +00:00
|
|
|
_, isNull := elems[3].(stackitem.Null)
|
2021-01-22 12:12:09 +00:00
|
|
|
if !isNull {
|
2021-02-16 13:24:05 +00:00
|
|
|
bs, err := elems[3].TryBytes()
|
2021-01-22 12:12:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u, err := util.Uint160DecodeBytesBE(bs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Admin = u
|
|
|
|
}
|
|
|
|
s.Expiration = uint32(bi.Uint64())
|
|
|
|
s.HasAdmin = !isNull
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
|
|
|
|
func domainFromString(name string) (string, bool) {
|
|
|
|
i := strings.LastIndexAny(name, ".")
|
|
|
|
if i < 0 {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
i = strings.LastIndexAny(name[:i], ".")
|
|
|
|
if i < 0 {
|
|
|
|
return name, true
|
|
|
|
}
|
|
|
|
return name[i+1:], true
|
|
|
|
}
|
|
|
|
|
|
|
|
func toDomain(name string) string {
|
|
|
|
domain, ok := domainFromString(name)
|
|
|
|
if !ok {
|
|
|
|
panic("invalid record")
|
|
|
|
}
|
|
|
|
return domain
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:16:38 +00:00
|
|
|
func toRecordType(item stackitem.Item) nnsrecords.Type {
|
2021-01-22 12:12:09 +00:00
|
|
|
bi, err := item.TryInteger()
|
|
|
|
if err != nil || !bi.IsInt64() {
|
|
|
|
panic("invalid record type")
|
|
|
|
}
|
|
|
|
val := bi.Uint64()
|
|
|
|
if val > math.MaxUint8 {
|
|
|
|
panic("invalid record type")
|
|
|
|
}
|
2021-03-24 10:16:38 +00:00
|
|
|
switch rt := nnsrecords.Type(val); rt {
|
|
|
|
case nnsrecords.A, nnsrecords.CNAME, nnsrecords.TXT, nnsrecords.AAAA:
|
2021-01-22 12:12:09 +00:00
|
|
|
return rt
|
|
|
|
default:
|
|
|
|
panic("invalid record type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toName(item stackitem.Item) string {
|
|
|
|
name := toString(item)
|
|
|
|
if !matchName(name) {
|
|
|
|
panic("invalid name")
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
type stringList []string
|
|
|
|
|
|
|
|
// ToStackItem converts sl to stack item.
|
|
|
|
func (sl stringList) ToStackItem() stackitem.Item {
|
|
|
|
arr := make([]stackitem.Item, len(sl))
|
|
|
|
for i := range sl {
|
|
|
|
arr[i] = stackitem.NewByteArray([]byte(sl[i]))
|
|
|
|
}
|
|
|
|
return stackitem.NewArray(arr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromStackItem converts stack item to string list.
|
|
|
|
func (sl *stringList) FromStackItem(item stackitem.Item) error {
|
|
|
|
arr, ok := item.Value().([]stackitem.Item)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("invalid stack item")
|
|
|
|
}
|
|
|
|
res := make([]string, len(arr))
|
|
|
|
for i := range res {
|
|
|
|
s, err := stackitem.ToString(arr[i])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res[i] = s
|
|
|
|
}
|
|
|
|
*sl = res
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeBinary implements io.Serializable.
|
|
|
|
func (sl stringList) EncodeBinary(w *io.BinWriter) {
|
|
|
|
stackitem.EncodeBinaryStackItem(sl.ToStackItem(), w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements io.Serializable.
|
|
|
|
func (sl *stringList) DecodeBinary(r *io.BinReader) {
|
|
|
|
item := stackitem.DecodeBinaryStackItem(r)
|
|
|
|
if r.Err == nil {
|
2021-05-12 18:49:42 +00:00
|
|
|
r.Err = sl.FromStackItem(item)
|
2021-01-22 12:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sl stringList) index(s string) (int, bool) {
|
|
|
|
index := sort.Search(len(sl), func(i int) bool {
|
|
|
|
return sl[i] >= s
|
|
|
|
})
|
|
|
|
return index, index < len(sl) && sl[index] == s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sl *stringList) add(s string) bool {
|
|
|
|
index, has := sl.index(s)
|
|
|
|
if has {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
*sl = append(*sl, "")
|
|
|
|
copy((*sl)[index+1:], (*sl)[index:])
|
|
|
|
(*sl)[index] = s
|
|
|
|
return true
|
|
|
|
}
|