2022-04-12 08:25:18 +00:00
|
|
|
package ns
|
2021-12-10 11:50:10 +00:00
|
|
|
|
|
|
|
import (
|
2022-03-18 08:32:00 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2021-12-10 11:50:10 +00:00
|
|
|
"fmt"
|
2022-04-12 17:34:32 +00:00
|
|
|
"net/url"
|
2021-12-10 11:50:10 +00:00
|
|
|
|
|
|
|
nns "github.com/nspcc-dev/neo-go/examples/nft-nd-nns"
|
2022-04-12 17:34:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2022-03-18 08:32:00 +00:00
|
|
|
neoclient "github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
2021-12-10 11:50:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
)
|
|
|
|
|
2022-03-18 08:32:00 +00:00
|
|
|
// NNS looks up NeoFS names using Neo Name Service.
|
|
|
|
//
|
|
|
|
// Instances are created with a variable declaration. Before work, the connection
|
2022-04-12 17:34:32 +00:00
|
|
|
// to the NNS server MUST be established using Dial method.
|
2022-03-18 08:32:00 +00:00
|
|
|
type NNS struct {
|
|
|
|
nnsContract util.Uint160
|
|
|
|
|
2022-04-12 17:34:32 +00:00
|
|
|
neoClient neoClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// represents virtual connection to Neo network used by NNS.Dial.
|
|
|
|
type neoClient interface {
|
|
|
|
// calls specified method of the Neo smart contract with provided parameters.
|
|
|
|
call(contract util.Uint160, method string, prm []smartcontract.Parameter) (*result.Invoke, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// implements neoClient using Neo HTTP client.
|
|
|
|
//
|
|
|
|
// note: see NNS.Dial to realize why this isn't defined as type wrapper like neoWebSocket.
|
|
|
|
type neoHTTP struct {
|
|
|
|
*neoclient.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *neoHTTP) call(contract util.Uint160, method string, prm []smartcontract.Parameter) (*result.Invoke, error) {
|
|
|
|
return x.Client.InvokeFunction(contract, method, prm, nil)
|
2021-12-10 11:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 17:34:32 +00:00
|
|
|
// implements neoClient using Neo WebSocket client.
|
|
|
|
type neoWebSocket neoclient.WSClient
|
2022-03-18 08:32:00 +00:00
|
|
|
|
2022-04-12 17:34:32 +00:00
|
|
|
func (x *neoWebSocket) call(contract util.Uint160, method string, prm []smartcontract.Parameter) (*result.Invoke, error) {
|
2022-04-12 08:19:32 +00:00
|
|
|
return (*neoclient.WSClient)(x).InvokeFunction(contract, method, prm, nil)
|
2021-12-10 11:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:32:00 +00:00
|
|
|
// Dial connects to the address of the NNS server. If fails, the instance
|
2022-04-12 17:34:32 +00:00
|
|
|
// MUST NOT be used.
|
|
|
|
//
|
2022-04-13 07:23:06 +00:00
|
|
|
// If URL address scheme is 'ws' or 'wss', then WebSocket protocol is used,
|
|
|
|
// otherwise HTTP.
|
2022-03-18 08:32:00 +00:00
|
|
|
func (n *NNS) Dial(address string) error {
|
2022-04-12 17:34:32 +00:00
|
|
|
// multiSchemeClient unites neoClient and common interface of
|
|
|
|
// neoclient.Client and neoclient.WSClient. Interface is anonymous
|
|
|
|
// according to assumption that common interface of these client types
|
|
|
|
// is not required by design and may diverge with changes.
|
|
|
|
var multiSchemeClient interface {
|
|
|
|
neoClient
|
|
|
|
// Init turns client to "ready-to-work" state.
|
|
|
|
Init() error
|
|
|
|
// GetContractStateByID returns state of the NNS contract on 1 input.
|
|
|
|
GetContractStateByID(int32) (*state.Contract, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
uri, err := url.Parse(address)
|
2022-04-13 07:23:06 +00:00
|
|
|
if err == nil && (uri.Scheme == "ws" || uri.Scheme == "wss") {
|
2022-04-12 17:34:32 +00:00
|
|
|
cWebSocket, err := neoclient.NewWS(context.Background(), address, neoclient.Options{})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create Neo WebSocket client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
multiSchemeClient = (*neoWebSocket)(cWebSocket)
|
|
|
|
} else {
|
|
|
|
cHTTP, err := neoclient.New(context.Background(), address, neoclient.Options{})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create Neo HTTP client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if neoHTTP is defined as type wrapper
|
|
|
|
// type neoHTTP neoclient.Client
|
|
|
|
// then next assignment causes compilation error
|
|
|
|
// multiSchemeClient = (*neoHTTP)(cHTTP)
|
|
|
|
multiSchemeClient = &neoHTTP{
|
|
|
|
Client: cHTTP,
|
|
|
|
}
|
2022-03-18 08:32:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 17:34:32 +00:00
|
|
|
if err = multiSchemeClient.Init(); err != nil {
|
|
|
|
return fmt.Errorf("initialize Neo client: %w", err)
|
2022-03-18 08:32:00 +00:00
|
|
|
}
|
2021-12-10 11:50:10 +00:00
|
|
|
|
2022-04-12 17:34:32 +00:00
|
|
|
nnsContract, err := multiSchemeClient.GetContractStateByID(1)
|
2021-12-10 11:50:10 +00:00
|
|
|
if err != nil {
|
2022-03-18 08:32:00 +00:00
|
|
|
return fmt.Errorf("get NNS contract state: %w", err)
|
2021-12-10 11:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 17:34:32 +00:00
|
|
|
n.neoClient = multiSchemeClient
|
2022-03-18 08:32:00 +00:00
|
|
|
n.nnsContract = nnsContract.Hash
|
|
|
|
|
|
|
|
return nil
|
2021-12-10 11:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:32:00 +00:00
|
|
|
// ResolveContainerName looks up for NNS TXT records for the given container name
|
|
|
|
// by calling `resolve` method of NNS contract. Returns the first record which represents
|
|
|
|
// valid container ID in a string format. Otherwise, returns an error.
|
|
|
|
//
|
|
|
|
// ResolveContainerName MUST NOT be called before successful Dial.
|
|
|
|
//
|
|
|
|
// See also https://docs.neo.org/docs/en-us/reference/nns.html.
|
2022-04-11 16:25:14 +00:00
|
|
|
func (n *NNS) ResolveContainerName(name string) (cid.ID, error) {
|
2022-04-12 17:34:32 +00:00
|
|
|
res, err := n.neoClient.call(n.nnsContract, "resolve", []smartcontract.Parameter{
|
2021-12-10 11:50:10 +00:00
|
|
|
{
|
|
|
|
Type: smartcontract.StringType,
|
2022-03-18 08:32:00 +00:00
|
|
|
Value: name + ".container",
|
2021-12-10 11:50:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: smartcontract.IntegerType,
|
|
|
|
Value: int64(nns.TXT),
|
|
|
|
},
|
2022-03-18 08:32:00 +00:00
|
|
|
})
|
2021-12-10 11:50:10 +00:00
|
|
|
if err != nil {
|
2022-04-11 16:25:14 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("invoke NNS contract: %w", err)
|
2021-12-10 11:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:32:00 +00:00
|
|
|
if res.State != vm.HaltState.String() {
|
2022-04-11 16:25:14 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("NNS contract fault exception: %s", res.FaultException)
|
2022-03-18 08:32:00 +00:00
|
|
|
} else if len(res.Stack) == 0 {
|
2022-04-11 16:25:14 +00:00
|
|
|
return cid.ID{}, errors.New("empty stack in invocation result")
|
2021-12-10 11:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:32:00 +00:00
|
|
|
itemArr, err := res.Stack[len(res.Stack)-1].Convert(stackitem.ArrayT) // top stack element is last in the array
|
2021-12-10 11:50:10 +00:00
|
|
|
if err != nil {
|
2022-04-11 16:25:14 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("convert stack item to %s", stackitem.ArrayT)
|
2021-12-10 11:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:32:00 +00:00
|
|
|
if _, ok := itemArr.(stackitem.Null); !ok {
|
|
|
|
arr, ok := itemArr.Value().([]stackitem.Item)
|
|
|
|
if !ok {
|
|
|
|
// unexpected for types from stackitem package
|
2022-04-11 16:25:14 +00:00
|
|
|
return cid.ID{}, errors.New("invalid cast to stack item slice")
|
2021-12-10 11:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 08:32:00 +00:00
|
|
|
var id cid.ID
|
2021-12-10 11:50:10 +00:00
|
|
|
|
2022-03-18 08:32:00 +00:00
|
|
|
for i := range arr {
|
|
|
|
bs, err := arr[i].TryBytes()
|
|
|
|
if err != nil {
|
2022-04-11 16:25:14 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("convert array item to byte slice: %w", err)
|
2022-03-18 08:32:00 +00:00
|
|
|
}
|
2021-12-10 11:50:10 +00:00
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
err = id.DecodeString(string(bs))
|
2022-03-18 08:32:00 +00:00
|
|
|
if err == nil {
|
2022-04-11 16:25:14 +00:00
|
|
|
return id, nil
|
2022-03-18 08:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-10 11:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 16:25:14 +00:00
|
|
|
return cid.ID{}, errNotFound
|
2021-12-10 11:50:10 +00:00
|
|
|
}
|