2023-12-05 09:12:35 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
2024-08-30 12:05:32 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-12-05 09:12:35 +00:00
|
|
|
"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(contractHash, rpcAddress 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(rpcAddress); err != nil {
|
|
|
|
return util.Uint160{}, fmt.Errorf("dial nns %s: %w", rpcAddress, err)
|
|
|
|
}
|
2024-03-01 14:29:56 +00:00
|
|
|
defer nns.Close()
|
2023-12-05 09:12:35 +00:00
|
|
|
|
|
|
|
return nns.ResolveContractHash(domain)
|
|
|
|
}
|
2024-08-30 12:05:32 +00:00
|
|
|
|
|
|
|
// ResolveContainerID determine container id by resolving NNS name.
|
|
|
|
func ResolveContainerID(containerID, rpcAddress string) (cid.ID, error) {
|
|
|
|
var cnrID cid.ID
|
|
|
|
if err := cnrID.DecodeString(containerID); err == nil {
|
|
|
|
return cnrID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
splitName := strings.Split(containerID, ".")
|
|
|
|
if len(splitName) != 2 {
|
|
|
|
return cid.ID{}, fmt.Errorf("invalid container name: '%s'", containerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
var domain container.Domain
|
|
|
|
domain.SetName(splitName[0])
|
|
|
|
domain.SetZone(splitName[1])
|
|
|
|
|
|
|
|
var nns ns.NNS
|
|
|
|
if err := nns.Dial(rpcAddress); err != nil {
|
|
|
|
return cid.ID{}, fmt.Errorf("dial nns '%s': %w", rpcAddress, err)
|
|
|
|
}
|
|
|
|
defer nns.Close()
|
|
|
|
|
|
|
|
return nns.ResolveContainerDomain(domain)
|
|
|
|
}
|