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"
|
2022-08-31 15:05:09 +00:00
|
|
|
"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
|
|
|
|
2022-04-12 17:34:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2022-07-29 08:01:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
2022-08-31 12:39:28 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
2021-12-10 11:50:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-08-31 15:05:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-04-22 13:49:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/nns"
|
2022-10-27 14:08:45 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
2021-12-10 11:50:10 +00:00
|
|
|
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-08-31 12:39:28 +00:00
|
|
|
invoker interface {
|
|
|
|
Call(contract util.Uint160, operation string, params ...interface{}) (*result.Invoke, error)
|
|
|
|
}
|
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-08-31 12:39:28 +00:00
|
|
|
// multiSchemeClient unites invoker.RPCInvoke and common interface of
|
|
|
|
// rpcclient.Client and rpcclient.WSClient. Interface is anonymous
|
2022-04-12 17:34:32 +00:00
|
|
|
// according to assumption that common interface of these client types
|
|
|
|
// is not required by design and may diverge with changes.
|
|
|
|
var multiSchemeClient interface {
|
2022-08-31 12:39:28 +00:00
|
|
|
invoker.RPCInvoke
|
2022-04-12 17:34:32 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2022-08-31 12:39:28 +00:00
|
|
|
var err error
|
2022-04-12 17:34:32 +00:00
|
|
|
|
|
|
|
uri, err := url.Parse(address)
|
2022-04-13 07:23:06 +00:00
|
|
|
if err == nil && (uri.Scheme == "ws" || uri.Scheme == "wss") {
|
2022-08-31 12:39:28 +00:00
|
|
|
multiSchemeClient, err = rpcclient.NewWS(context.Background(), address, rpcclient.Options{})
|
2022-04-12 17:34:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create Neo WebSocket client: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-31 12:39:28 +00:00
|
|
|
multiSchemeClient, err = rpcclient.New(context.Background(), address, rpcclient.Options{})
|
2022-04-12 17:34:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create Neo HTTP client: %w", err)
|
|
|
|
}
|
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-08-31 12:39:28 +00:00
|
|
|
n.invoker = invoker.New(multiSchemeClient, nil)
|
2022-03-18 08:32:00 +00:00
|
|
|
n.nnsContract = nnsContract.Hash
|
|
|
|
|
|
|
|
return nil
|
2021-12-10 11:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 14:08:45 +00:00
|
|
|
// ResolveContainerDomain looks up for NNS TXT records for the given container domain
|
2022-03-18 08:32:00 +00:00
|
|
|
// by calling `resolve` method of NNS contract. Returns the first record which represents
|
|
|
|
// valid container ID in a string format. Otherwise, returns an error.
|
|
|
|
//
|
2022-10-27 14:08:45 +00:00
|
|
|
// ResolveContainerDomain MUST NOT be called before successful Dial.
|
2022-03-18 08:32:00 +00:00
|
|
|
//
|
|
|
|
// See also https://docs.neo.org/docs/en-us/reference/nns.html.
|
2022-10-27 14:08:45 +00:00
|
|
|
func (n *NNS) ResolveContainerDomain(domain container.Domain) (cid.ID, error) {
|
2022-08-31 15:05:09 +00:00
|
|
|
item, err := unwrap.Item(n.invoker.Call(n.nnsContract, "resolve",
|
2022-10-27 14:08:45 +00:00
|
|
|
domain.Name()+"."+domain.Zone(), int64(nns.TXT),
|
2022-08-31 12:39:28 +00:00
|
|
|
))
|
2021-12-10 11:50:10 +00:00
|
|
|
if err != nil {
|
2022-08-31 12:39:28 +00:00
|
|
|
return cid.ID{}, fmt.Errorf("contract invocation: %w", err)
|
2021-12-10 11:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 15:05:09 +00:00
|
|
|
if _, ok := item.(stackitem.Null); !ok {
|
|
|
|
arr, ok := item.Value().([]stackitem.Item)
|
|
|
|
if !ok {
|
|
|
|
// unexpected for types from stackitem package
|
|
|
|
return cid.ID{}, errors.New("invalid cast to stack item slice")
|
2021-12-10 11:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 15:05:09 +00:00
|
|
|
var id cid.ID
|
|
|
|
|
|
|
|
for i := range arr {
|
|
|
|
bs, err := arr[i].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return cid.ID{}, fmt.Errorf("convert array item to byte slice: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = id.DecodeString(string(bs))
|
|
|
|
if err == nil {
|
|
|
|
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
|
|
|
}
|