2021-07-22 09:52:44 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
2021-11-29 12:58:45 +00:00
|
|
|
"encoding/hex"
|
2021-10-07 12:12:15 +00:00
|
|
|
"errors"
|
2021-07-22 09:52:44 +00:00
|
|
|
"fmt"
|
2021-07-28 14:36:15 +00:00
|
|
|
"strconv"
|
2021-11-29 10:15:03 +00:00
|
|
|
"strings"
|
2021-07-22 09:52:44 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2021-11-29 12:58:45 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-07-22 09:52:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2022-07-28 16:22:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
2021-07-22 09:52:44 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2021-10-07 12:12:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-07-18 08:33:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
2022-03-27 17:30:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/nns"
|
2022-01-29 11:15:05 +00:00
|
|
|
morphClient "github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2021-07-22 09:52:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultNameServiceDomainPrice = 10_0000_0000
|
|
|
|
const defaultRegisterSysfee = 10_0000_0000 + defaultNameServiceDomainPrice
|
|
|
|
|
|
|
|
func (c *initializeContext) setNNS() error {
|
2021-11-24 07:21:24 +00:00
|
|
|
nnsCs, err := c.Client.GetContractStateByID(1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-22 09:52:44 +00:00
|
|
|
|
2021-10-07 12:25:34 +00:00
|
|
|
ok, err := c.nnsRootRegistered(nnsCs.Hash)
|
2021-07-22 09:52:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !ok {
|
|
|
|
bw := io.NewBufBinWriter()
|
2021-11-24 07:21:24 +00:00
|
|
|
emit.AppCall(bw.BinWriter, nnsCs.Hash, "register", callflag.All,
|
|
|
|
"neofs", c.CommitteeAcc.Contract.ScriptHash(),
|
|
|
|
"ops@nspcc.ru", int64(3600), int64(600), int64(604800), int64(3600))
|
|
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
2022-04-05 14:21:11 +00:00
|
|
|
if err := c.sendCommitteeTx(bw.Bytes(), -1, true); err != nil {
|
2021-07-22 09:52:44 +00:00
|
|
|
return fmt.Errorf("can't add domain root to NNS: %w", err)
|
|
|
|
}
|
|
|
|
if err := c.awaitTx(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:19:56 +00:00
|
|
|
alphaCs := c.getContract(alphabetContract)
|
2021-07-30 11:26:44 +00:00
|
|
|
for i, acc := range c.Accounts {
|
2021-07-28 14:36:15 +00:00
|
|
|
alphaCs.Hash = state.CreateContractHash(acc.Contract.ScriptHash(), alphaCs.NEF.Checksum, alphaCs.Manifest.Name)
|
|
|
|
|
|
|
|
domain := getAlphabetNNSDomain(i)
|
2021-10-07 12:25:34 +00:00
|
|
|
if err := c.nnsRegisterDomain(nnsCs.Hash, alphaCs.Hash, domain); err != nil {
|
2021-07-28 14:36:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-08-10 08:38:48 +00:00
|
|
|
c.Command.Printf("NNS: Set %s -> %s\n", domain, alphaCs.Hash.StringLE())
|
2021-07-28 14:36:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 09:52:44 +00:00
|
|
|
for _, ctrName := range contractList {
|
2021-10-25 13:19:56 +00:00
|
|
|
cs := c.getContract(ctrName)
|
2021-07-22 09:52:44 +00:00
|
|
|
|
|
|
|
domain := ctrName + ".neofs"
|
2021-10-07 12:25:34 +00:00
|
|
|
if err := c.nnsRegisterDomain(nnsCs.Hash, cs.Hash, domain); err != nil {
|
2021-07-22 09:52:44 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-08-10 08:38:48 +00:00
|
|
|
c.Command.Printf("NNS: Set %s -> %s\n", domain, cs.Hash.StringLE())
|
2021-08-06 11:28:07 +00:00
|
|
|
}
|
2021-07-22 09:52:44 +00:00
|
|
|
|
2022-02-02 14:19:27 +00:00
|
|
|
groupKey := c.ContractWallet.Accounts[0].PrivateKey().PublicKey()
|
|
|
|
err = c.updateNNSGroup(nnsCs.Hash, groupKey)
|
2021-11-29 12:58:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-29 11:15:05 +00:00
|
|
|
c.Command.Printf("NNS: Set %s -> %s\n", morphClient.NNSGroupKeyName, hex.EncodeToString(groupKey.Bytes()))
|
2021-11-29 12:58:45 +00:00
|
|
|
|
2021-08-06 11:28:07 +00:00
|
|
|
return c.awaitTx()
|
|
|
|
}
|
|
|
|
|
2021-11-29 12:58:45 +00:00
|
|
|
func (c *initializeContext) updateNNSGroup(nnsHash util.Uint160, pub *keys.PublicKey) error {
|
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
sysFee, err := c.emitUpdateNNSGroupScript(bw, nnsHash, pub)
|
2022-05-12 15:38:57 +00:00
|
|
|
if err != nil || sysFee == 0 {
|
2021-11-29 12:58:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-04-05 14:21:11 +00:00
|
|
|
return c.sendCommitteeTx(bw.Bytes(), sysFee, true)
|
2021-11-29 12:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *initializeContext) emitUpdateNNSGroupScript(bw *io.BufBinWriter, nnsHash util.Uint160, pub *keys.PublicKey) (int64, error) {
|
2022-04-07 08:10:35 +00:00
|
|
|
isAvail, err := nnsIsAvailable(c.Client, nnsHash, morphClient.NNSGroupKeyName)
|
2021-11-29 12:58:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isAvail {
|
2022-01-29 11:15:05 +00:00
|
|
|
currentPub, err := nnsResolveKey(c.Client, nnsHash, morphClient.NNSGroupKeyName)
|
2021-11-29 12:58:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if pub.Equal(currentPub) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sysFee := int64(native.GASFactor)
|
|
|
|
if isAvail {
|
|
|
|
emit.AppCall(bw.BinWriter, nnsHash, "register", callflag.All,
|
2022-01-29 11:15:05 +00:00
|
|
|
morphClient.NNSGroupKeyName, c.CommitteeAcc.Contract.ScriptHash(),
|
2021-11-29 12:58:45 +00:00
|
|
|
"ops@nspcc.ru", int64(3600), int64(600), int64(604800), int64(3600))
|
|
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
|
|
|
sysFee += defaultRegisterSysfee
|
|
|
|
}
|
|
|
|
emit.AppCall(bw.BinWriter, nnsHash, "addRecord", callflag.All,
|
|
|
|
"group.neofs", int64(nns.TXT), hex.EncodeToString(pub.Bytes()))
|
|
|
|
|
|
|
|
return sysFee, bw.Err
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:28:07 +00:00
|
|
|
func getAlphabetNNSDomain(i int) string {
|
|
|
|
return alphabetContract + strconv.FormatUint(uint64(i), 10) + ".neofs"
|
|
|
|
}
|
|
|
|
|
2021-12-27 10:26:22 +00:00
|
|
|
func (c *initializeContext) nnsRegisterDomainScript(nnsHash, expectedHash util.Uint160, domain string) ([]byte, error) {
|
2022-04-07 08:10:35 +00:00
|
|
|
ok, err := nnsIsAvailable(c.Client, nnsHash, domain)
|
2021-08-06 11:28:07 +00:00
|
|
|
if err != nil {
|
2021-12-27 10:26:22 +00:00
|
|
|
return nil, err
|
2021-08-06 11:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
if ok {
|
|
|
|
emit.AppCall(bw.BinWriter, nnsHash, "register", callflag.All,
|
2021-10-07 12:12:15 +00:00
|
|
|
domain, c.CommitteeAcc.Contract.ScriptHash(),
|
|
|
|
"ops@nspcc.ru", int64(3600), int64(600), int64(604800), int64(3600))
|
2021-07-22 09:52:44 +00:00
|
|
|
emit.Opcodes(bw.BinWriter, opcode.ASSERT)
|
2021-08-06 11:28:07 +00:00
|
|
|
} else {
|
2021-10-07 12:12:15 +00:00
|
|
|
s, err := nnsResolveHash(c.Client, nnsHash, domain)
|
2021-08-06 11:28:07 +00:00
|
|
|
if err != nil {
|
2021-12-27 10:26:22 +00:00
|
|
|
return nil, err
|
2021-07-22 09:52:44 +00:00
|
|
|
}
|
2021-11-24 07:27:23 +00:00
|
|
|
if s == expectedHash {
|
2021-12-27 10:26:22 +00:00
|
|
|
return nil, nil
|
2021-08-06 11:28:07 +00:00
|
|
|
}
|
2021-07-22 09:52:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 12:12:15 +00:00
|
|
|
emit.AppCall(bw.BinWriter, nnsHash, "addRecord", callflag.All,
|
2021-08-06 11:28:07 +00:00
|
|
|
domain, int64(nns.TXT), expectedHash.StringLE())
|
|
|
|
|
|
|
|
if bw.Err != nil {
|
|
|
|
panic(bw.Err)
|
|
|
|
}
|
2021-12-27 10:26:22 +00:00
|
|
|
return bw.Bytes(), nil
|
|
|
|
}
|
2021-08-06 11:28:07 +00:00
|
|
|
|
2021-12-27 10:26:22 +00:00
|
|
|
func (c *initializeContext) nnsRegisterDomain(nnsHash, expectedHash util.Uint160, domain string) error {
|
|
|
|
script, err := c.nnsRegisterDomainScript(nnsHash, expectedHash, domain)
|
|
|
|
if script == nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-06 11:28:07 +00:00
|
|
|
sysFee := int64(defaultRegisterSysfee + native.GASFactor)
|
2022-04-05 14:21:11 +00:00
|
|
|
return c.sendCommitteeTx(script, sysFee, true)
|
2021-07-22 09:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *initializeContext) nnsRootRegistered(nnsHash util.Uint160) (bool, error) {
|
2022-04-07 12:47:13 +00:00
|
|
|
params := []interface{}{"name.neofs"}
|
|
|
|
res, err := invokeFunction(c.Client, nnsHash, "isAvailable", params, nil)
|
2021-07-22 09:52:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2022-07-18 08:33:53 +00:00
|
|
|
return res.State == vmstate.Halt.String(), nil
|
2021-07-22 09:52:44 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 10:15:03 +00:00
|
|
|
var errMissingNNSRecord = errors.New("missing NNS record")
|
|
|
|
|
|
|
|
// Returns errMissingNNSRecord if invocation fault exception contains "token not found".
|
2022-04-07 08:10:35 +00:00
|
|
|
func nnsResolveHash(c Client, nnsHash util.Uint160, domain string) (util.Uint160, error) {
|
2021-11-29 12:58:45 +00:00
|
|
|
item, err := nnsResolve(c, nnsHash, domain)
|
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, err
|
|
|
|
}
|
|
|
|
return parseNNSResolveResult(item)
|
|
|
|
}
|
|
|
|
|
2022-04-07 08:10:35 +00:00
|
|
|
func nnsResolve(c Client, nnsHash util.Uint160, domain string) (stackitem.Item, error) {
|
2022-04-07 12:47:13 +00:00
|
|
|
result, err := invokeFunction(c, nnsHash, "resolve", []interface{}{domain, int64(nns.TXT)}, nil)
|
2021-08-06 11:28:07 +00:00
|
|
|
if err != nil {
|
2021-11-29 12:58:45 +00:00
|
|
|
return nil, fmt.Errorf("`resolve`: %w", err)
|
2021-08-06 11:28:07 +00:00
|
|
|
}
|
2022-07-18 08:33:53 +00:00
|
|
|
if result.State != vmstate.Halt.String() {
|
2021-11-29 10:15:03 +00:00
|
|
|
if strings.Contains(result.FaultException, "token not found") {
|
2021-11-29 12:58:45 +00:00
|
|
|
return nil, errMissingNNSRecord
|
2021-11-29 10:15:03 +00:00
|
|
|
}
|
2021-11-29 12:58:45 +00:00
|
|
|
return nil, fmt.Errorf("invocation failed: %s", result.FaultException)
|
2021-10-07 12:12:15 +00:00
|
|
|
}
|
|
|
|
if len(result.Stack) == 0 {
|
2021-11-29 12:58:45 +00:00
|
|
|
return nil, errors.New("result stack is empty")
|
2021-10-07 12:12:15 +00:00
|
|
|
}
|
2021-11-29 12:58:45 +00:00
|
|
|
return result.Stack[len(result.Stack)-1], nil
|
2021-10-07 13:14:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 08:10:35 +00:00
|
|
|
func nnsResolveKey(c Client, nnsHash util.Uint160, domain string) (*keys.PublicKey, error) {
|
2021-11-29 13:45:25 +00:00
|
|
|
item, err := nnsResolve(c, nnsHash, domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
arr, ok := item.Value().([]stackitem.Item)
|
|
|
|
if !ok || len(arr) == 0 {
|
|
|
|
return nil, errors.New("NNS record is missing")
|
|
|
|
}
|
|
|
|
bs, err := arr[0].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("malformed response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys.NewPublicKeyFromString(string(bs))
|
|
|
|
}
|
|
|
|
|
2021-10-07 13:14:52 +00:00
|
|
|
// parseNNSResolveResult parses the result of resolving NNS record.
|
|
|
|
// It works with multiple formats (corresponding to multiple NNS versions).
|
|
|
|
// If array of hashes is provided, it returns only the first one.
|
|
|
|
func parseNNSResolveResult(res stackitem.Item) (util.Uint160, error) {
|
|
|
|
if arr, ok := res.Value().([]stackitem.Item); ok {
|
|
|
|
if len(arr) == 0 {
|
|
|
|
return util.Uint160{}, errors.New("NNS record is missing")
|
|
|
|
}
|
|
|
|
res = arr[0]
|
2021-10-07 12:12:15 +00:00
|
|
|
}
|
2021-10-07 13:14:52 +00:00
|
|
|
bs, err := res.TryBytes()
|
2021-10-07 12:12:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.Uint160{}, errors.New("malformed response")
|
|
|
|
}
|
|
|
|
return util.Uint160DecodeStringLE(string(bs))
|
2021-07-28 14:36:15 +00:00
|
|
|
}
|
2022-04-07 08:10:35 +00:00
|
|
|
|
2022-04-07 12:47:13 +00:00
|
|
|
var errNNSIsAvailableInvalid = errors.New("`isAvailable`: invalid response")
|
|
|
|
|
2022-04-07 08:10:35 +00:00
|
|
|
func nnsIsAvailable(c Client, nnsHash util.Uint160, name string) (bool, error) {
|
|
|
|
switch ct := c.(type) {
|
2022-07-28 16:22:32 +00:00
|
|
|
case *rpcclient.Client:
|
2022-04-07 08:10:35 +00:00
|
|
|
return ct.NNSIsAvailable(nnsHash, name)
|
|
|
|
default:
|
2022-04-07 12:47:13 +00:00
|
|
|
res, err := invokeFunction(c, nnsHash, "isAvailable", []interface{}{name}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if len(res.Stack) == 0 {
|
|
|
|
return false, errNNSIsAvailableInvalid
|
|
|
|
}
|
|
|
|
b, err := res.Stack[0].TryBool()
|
|
|
|
if err != nil {
|
|
|
|
return b, errNNSIsAvailableInvalid
|
|
|
|
}
|
|
|
|
return b, nil
|
2022-04-07 08:10:35 +00:00
|
|
|
}
|
|
|
|
}
|