github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/rpcclient/nns/record.go (about)

     1  package nns
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"math/big"
     7  
     8  	"github.com/nspcc-dev/neo-go/pkg/smartcontract"
     9  	"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
    10  )
    11  
    12  // RecordState is a type that registered entities are saved as.
    13  type RecordState struct {
    14  	Name string
    15  	Type RecordType
    16  	Data string
    17  }
    18  
    19  // RecordType is domain name service record types.
    20  type RecordType byte
    21  
    22  // Ensure RecordType implements smartcontract.Convertible for proper handling as
    23  // a parameter to invoker.Invoker methods.
    24  var _ = smartcontract.Convertible(RecordType(0))
    25  
    26  // Record types are defined in [RFC 1035](https://tools.ietf.org/html/rfc1035)
    27  const (
    28  	// A represents address record type.
    29  	A RecordType = 1
    30  	// CNAME represents canonical name record type.
    31  	CNAME RecordType = 5
    32  	// TXT represents text record type.
    33  	TXT RecordType = 16
    34  )
    35  
    36  // Record types are defined in [RFC 3596](https://tools.ietf.org/html/rfc3596)
    37  const (
    38  	// AAAA represents IPv6 address record type.
    39  	AAAA RecordType = 28
    40  )
    41  
    42  // ToSCParameter implements smartcontract.Convertible interface.
    43  func (r RecordType) ToSCParameter() (smartcontract.Parameter, error) {
    44  	return smartcontract.Parameter{
    45  		Type:  smartcontract.IntegerType,
    46  		Value: big.NewInt(int64(r)),
    47  	}, nil
    48  }
    49  
    50  // FromStackItem fills RecordState with data from the given stack item if it can
    51  // be correctly converted to RecordState.
    52  func (r *RecordState) FromStackItem(itm stackitem.Item) error {
    53  	rs, ok := itm.Value().([]stackitem.Item)
    54  	if !ok {
    55  		return errors.New("not a struct")
    56  	}
    57  	if len(rs) != 3 {
    58  		return errors.New("wrong number of elements")
    59  	}
    60  	name, err := rs[0].TryBytes()
    61  	if err != nil {
    62  		return fmt.Errorf("bad name: %w", err)
    63  	}
    64  	typ, err := rs[1].TryInteger()
    65  	if err != nil {
    66  		return fmt.Errorf("bad type: %w", err)
    67  	}
    68  	data, err := rs[2].TryBytes()
    69  	if err != nil {
    70  		return fmt.Errorf("bad data: %w", err)
    71  	}
    72  	u64Typ := typ.Uint64()
    73  	if !typ.IsUint64() || u64Typ > 255 {
    74  		return errors.New("bad type")
    75  	}
    76  	r.Name = string(name)
    77  	r.Type = RecordType(u64Typ)
    78  	r.Data = string(data)
    79  	return nil
    80  }