[#888] neofs-adm: allow to work with multiple NNS versions

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-10-07 16:14:52 +03:00 committed by Alex Vanin
parent 6571f9214f
commit 31b3c71457
2 changed files with 17 additions and 10 deletions

View file

@ -78,9 +78,8 @@ func dumpContractHashes(cmd *cobra.Command, _ []string) error {
for i := 0; i < irSize; i++ {
ctrHash := "hash is invalid"
bs, err := alphaRes.Stack[i].TryBytes()
if err == nil {
ctrHash = string(bs) // hashes are stored as hex-encoded LE string
if h, err := parseNNSResolveResult(alphaRes.Stack[i]); err == nil {
ctrHash = h.StringLE()
}
_, _ = tw.Write([]byte(fmt.Sprintf("alphabet %d:\t%s\n", i, ctrHash)))
@ -89,9 +88,8 @@ func dumpContractHashes(cmd *cobra.Command, _ []string) error {
for i := range contractList {
ctrHash := "hash is invalid"
bs, err := res.Stack[i].TryBytes()
if err == nil {
ctrHash = string(bs) // hashes are stored as hex-encoded LE string
if h, err := parseNNSResolveResult(res.Stack[i]); err == nil {
ctrHash = h.StringLE()
}
_, _ = tw.Write([]byte(fmt.Sprintf("%s:\t%s\n", contractList[i], ctrHash)))

View file

@ -139,11 +139,20 @@ func nnsResolveHash(c *client.Client, nnsHash util.Uint160, domain string) (util
if len(result.Stack) == 0 {
return util.Uint160{}, errors.New("result stack is empty")
}
arr, ok := result.Stack[len(result.Stack)-1].Value().([]stackitem.Item)
if !ok || len(arr) == 0 {
return util.Uint160{}, errors.New("malformed response")
return parseNNSResolveResult(result.Stack[len(result.Stack)-1])
}
bs, err := arr[0].TryBytes()
// 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]
}
bs, err := res.TryBytes()
if err != nil {
return util.Uint160{}, errors.New("malformed response")
}