policy-reader/internal/resolver/resolver.go
Denis Kirillov c40ad3627d Add listing user/container chains by name
Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
2024-08-29 09:42:39 +03:00

57 lines
1.5 KiB
Go

package resolver
import (
"fmt"
"strings"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/ns"
"github.com/nspcc-dev/neo-go/pkg/util"
)
// ResolveContractHash determine contract hash by resolving NNS name.
func ResolveContractHash(endpoint, contractHash string) (util.Uint160, error) {
if hash, err := util.Uint160DecodeStringLE(contractHash); err == nil {
return hash, nil
}
splitName := strings.Split(contractHash, ".")
if len(splitName) != 2 {
return util.Uint160{}, fmt.Errorf("invalid contract name: '%s'", contractHash)
}
var domain container.Domain
domain.SetName(splitName[0])
domain.SetZone(splitName[1])
var nns ns.NNS
if err := nns.Dial(endpoint); err != nil {
return util.Uint160{}, fmt.Errorf("dial nns %s: %w", endpoint, err)
}
defer nns.Close()
return nns.ResolveContractHash(domain)
}
// ResolveContainerID determine container id by resolving NNS name.
func ResolveContainerID(endpoint, namespace, containerName string) (cid.ID, error) {
var cnrID cid.ID
if err := cnrID.DecodeString(containerName); err == nil {
return cnrID, nil
}
var domain container.Domain
domain.SetName(containerName)
if namespace != "" {
domain.SetZone(namespace + ".ns")
}
var nns ns.NNS
if err := nns.Dial(endpoint); err != nil {
return cid.ID{}, fmt.Errorf("dial nns %s: %w", endpoint, err)
}
defer nns.Close()
return nns.ResolveContainerDomain(domain)
}