From d4ac5bdb97a0726a758e99cd7600488a8c742308 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 1 Sep 2022 13:48:14 +0300 Subject: [PATCH] [#1748] neofs-adm: Allow to parse hashes in multiple formats NEO NNS proposal uses addresses. We should eventually use the same, but must stay compatible now. Signed-off-by: Evgenii Stratonikov --- .../internal/modules/morph/initialize_nns.go | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_nns.go b/cmd/neofs-adm/internal/modules/morph/initialize_nns.go index 2110ed5a..b9ef87f5 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize_nns.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize_nns.go @@ -10,6 +10,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/rpcclient" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" @@ -256,17 +257,30 @@ func nnsResolveKey(c Client, nnsHash util.Uint160, domain string) (*keys.PublicK // 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") + arr, ok := res.Value().([]stackitem.Item) + if !ok { + arr = []stackitem.Item{res} + } + if _, ok := res.Value().(stackitem.Null); ok || len(arr) == 0 { + return util.Uint160{}, errors.New("NNS record is missing") + } + for i := range arr { + bs, err := arr[i].TryBytes() + if err != nil { + continue + } + + h, err := util.Uint160DecodeStringLE(string(bs)) + if err == nil { + return h, nil + } + + h, err = address.StringToUint160(string(bs)) + if err == nil { + return h, nil } - res = arr[0] } - bs, err := res.TryBytes() - if err != nil { - return util.Uint160{}, errors.New("malformed response") - } - return util.Uint160DecodeStringLE(string(bs)) + return util.Uint160{}, errors.New("no valid hashes are found") } var errNNSIsAvailableInvalid = errors.New("`isAvailable`: invalid response")