776 lines
23 KiB
Go
776 lines
23 KiB
Go
package client
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/commonclient"
|
|
"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/neorpc/result"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
)
|
|
|
|
type (
|
|
Client struct {
|
|
act *actor.Actor
|
|
acc *wallet.Account
|
|
contract util.Uint160
|
|
}
|
|
|
|
Options struct {
|
|
// todo add proxy params
|
|
}
|
|
)
|
|
|
|
type (
|
|
Subject struct {
|
|
PrimaryKey *keys.PublicKey
|
|
AdditionalKeys keys.PublicKeys
|
|
Namespace string
|
|
Name string
|
|
KV map[string]string
|
|
}
|
|
|
|
SubjectExtended struct {
|
|
PrimaryKey *keys.PublicKey
|
|
AdditionalKeys keys.PublicKeys
|
|
Namespace string
|
|
Name string
|
|
KV map[string]string
|
|
Groups []*Group
|
|
}
|
|
|
|
Namespace struct {
|
|
Name string
|
|
}
|
|
|
|
NamespaceExtended struct {
|
|
Name string
|
|
GroupsCount int64
|
|
SubjectsCount int64
|
|
}
|
|
|
|
Group struct {
|
|
Name string
|
|
Namespace string
|
|
}
|
|
|
|
GroupExtended struct {
|
|
Name string
|
|
Namespace string
|
|
SubjectsCount int64
|
|
}
|
|
)
|
|
|
|
const (
|
|
SubjectIAMPathKey = "iam-path"
|
|
SubjectCreatedTimeKey = "ctime"
|
|
SubjectModifiedTimeKey = "mtime"
|
|
)
|
|
|
|
const iteratorBatchSize = 100
|
|
|
|
const (
|
|
addOwnerMethod = "addOwner"
|
|
deleteOwnerMethod = "deleteOwner"
|
|
listOwnersMethod = "listOwners"
|
|
|
|
versionMethod = "version"
|
|
|
|
createSubjectMethod = "createSubject"
|
|
getSubjectMethod = "getSubject"
|
|
getSubjectExtendedMethod = "getSubjectExtended"
|
|
listSubjectsMethod = "listSubjects"
|
|
addSubjectKeyMethod = "addSubjectKey"
|
|
removeSubjectKeyMethod = "removeSubjectKey"
|
|
getSubjectByKeyMethod = "getSubjectByKey"
|
|
getSubjectKeyByNameMethod = "getSubjectKeyByName"
|
|
setSubjectKVMethod = "setSubjectKV"
|
|
setSubjectNameMethod = "setSubjectName"
|
|
deleteSubjectKVMethod = "deleteSubjectKV"
|
|
deleteSubjectMethod = "deleteSubject"
|
|
|
|
createNamespaceMethod = "createNamespace"
|
|
getNamespaceMethod = "getNamespace"
|
|
getNamespaceExtendedMethod = "getNamespaceExtended"
|
|
listNamespacesMethod = "listNamespaces"
|
|
addSubjectToNamespaceMethod = "addSubjectToNamespace"
|
|
removeSubjectFromNamespaceMethod = "removeSubjectFromNamespace"
|
|
listNamespaceSubjectsMethod = "listNamespaceSubjects"
|
|
|
|
createGroupMethod = "createGroup"
|
|
getGroupMethod = "getGroup"
|
|
getGroupExtendedMethod = "getGroupExtended"
|
|
listGroupsMethod = "listGroups"
|
|
addSubjectToGroupMethod = "addSubjectToGroup"
|
|
removeSubjectFromGroupMethod = "removeSubjectFromGroup"
|
|
listGroupSubjectsMethod = "listGroupSubjects"
|
|
deleteGroupMethod = "deleteGroup"
|
|
)
|
|
|
|
// New creates a new Client. Options can be nil.
|
|
func New(ra actor.RPCActor, acc *wallet.Account, contract util.Uint160, _ *Options) (*Client, error) {
|
|
act, err := actor.NewSimple(ra, acc)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("init actor: %w", err)
|
|
}
|
|
|
|
return &Client{
|
|
act: act,
|
|
acc: acc,
|
|
contract: contract,
|
|
}, nil
|
|
}
|
|
|
|
// StartTx inits transaction.
|
|
func (c Client) StartTx() *commonclient.Transaction {
|
|
return commonclient.NewTransaction(c.contract)
|
|
}
|
|
|
|
// SendTx sends provided transaction to blockchain.
|
|
func (c Client) SendTx(txn *commonclient.Transaction) (tx util.Uint256, vub uint32, err error) {
|
|
txBytes, err := txn.Bytes()
|
|
if err != nil {
|
|
return util.Uint256{}, 0, err
|
|
}
|
|
|
|
return c.act.SendRun(txBytes)
|
|
}
|
|
|
|
// Version returns version of contract.
|
|
func (c Client) Version() (int64, error) {
|
|
return unwrap.Int64(c.act.Call(c.contract, versionMethod))
|
|
}
|
|
|
|
// AddOwner adds new address that can perform write operations on contract.
|
|
// Must be invoked by committee.
|
|
func (c Client) AddOwner(owner util.Uint160) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.AddOwnerCall(owner)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// AddOwnerCall provides args for AddOwner to use in commonclient.Transaction.
|
|
func (c Client) AddOwnerCall(owner util.Uint160) (method string, args []any) {
|
|
return addOwnerMethod, []any{owner}
|
|
}
|
|
|
|
// DeleteOwner removes address from list of that can perform write operations on contract.
|
|
// Must be invoked by committee.
|
|
func (c Client) DeleteOwner(owner util.Uint160) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.DeleteOwnerCall(owner)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// DeleteOwnerCall provides args for DeleteOwner to use in commonclient.Transaction.
|
|
func (c Client) DeleteOwnerCall(owner util.Uint160) (method string, args []any) {
|
|
return deleteOwnerMethod, []any{owner}
|
|
}
|
|
|
|
// ListOwners returns list of address that can perform write operations on contract.
|
|
func (c Client) ListOwners() ([]util.Uint160, error) {
|
|
return unwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listOwnersMethod))
|
|
}
|
|
|
|
// CreateSubject creates new subject using public key.
|
|
// Must be invoked by contract owner.
|
|
func (c Client) CreateSubject(key *keys.PublicKey) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.CreateSubjectCall(key)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// CreateSubjectCall provides args for CreateSubject to use in commonclient.Transaction.
|
|
func (c Client) CreateSubjectCall(key *keys.PublicKey) (method string, args []any) {
|
|
return createSubjectMethod, []any{key.Bytes()}
|
|
}
|
|
|
|
// GetSubject gets subject by address.
|
|
func (c Client) GetSubject(addr util.Uint160) (*Subject, error) {
|
|
items, err := unwrap.Array(c.act.Call(c.contract, getSubjectMethod, addr))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return parseSubject(items)
|
|
}
|
|
|
|
// GetSubjectExtended gets extended subject by address.
|
|
func (c Client) GetSubjectExtended(addr util.Uint160) (*SubjectExtended, error) {
|
|
items, err := unwrap.Array(c.act.Call(c.contract, getSubjectExtendedMethod, addr))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return parseSubjectExtended(items)
|
|
}
|
|
|
|
// ListSubjects gets all subjects.
|
|
func (c Client) ListSubjects() ([]util.Uint160, error) {
|
|
return unwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listSubjectsMethod))
|
|
}
|
|
|
|
// AddSubjectKey adds extra public key to subject.
|
|
// Must be invoked by contract owner.
|
|
func (c Client) AddSubjectKey(addr util.Uint160, key *keys.PublicKey) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.AddSubjectKeyCall(addr, key)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// AddSubjectKeyCall provides args for AddSubjectKey to use in commonclient.Transaction.
|
|
func (c Client) AddSubjectKeyCall(addr util.Uint160, key *keys.PublicKey) (method string, args []any) {
|
|
return addSubjectKeyMethod, []any{addr, key.Bytes()}
|
|
}
|
|
|
|
// RemoveSubjectKey removes extra public key from subject.
|
|
// Must be invoked by contract owner.
|
|
func (c Client) RemoveSubjectKey(addr util.Uint160, key *keys.PublicKey) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.RemoveSubjectKeyCall(addr, key)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// RemoveSubjectKeyCall provides args for RemoveSubjectKey to use in commonclient.Transaction.
|
|
func (c Client) RemoveSubjectKeyCall(addr util.Uint160, key *keys.PublicKey) (method string, args []any) {
|
|
return removeSubjectKeyMethod, []any{addr, key.Bytes()}
|
|
}
|
|
|
|
// SetSubjectKV updates subject kv map.
|
|
// Must be invoked by contract owner.
|
|
// You can use some predefined key constants: SubjectLoginKey, SubjectIAMPathKey, SubjectCreatedTimeKey, SubjectModifiedTimeKey.
|
|
func (c Client) SetSubjectKV(addr util.Uint160, key, val string) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.SetSubjectKVCall(addr, key, val)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// SetSubjectKVCall provides args for SetSubjectLogin to use in commonclient.Transaction.
|
|
func (c Client) SetSubjectKVCall(addr util.Uint160, key, val string) (method string, args []any) {
|
|
return setSubjectKVMethod, []any{addr, key, val}
|
|
}
|
|
|
|
// SetSubjectName updates subject name.
|
|
// Must be invoked by contract owner.
|
|
func (c Client) SetSubjectName(addr util.Uint160, name string) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.SetSubjectNameCall(addr, name)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// SetSubjectNameCall provides args for SetSubjectLogin to use in commonclient.Transaction.
|
|
func (c Client) SetSubjectNameCall(addr util.Uint160, name string) (method string, args []any) {
|
|
return setSubjectNameMethod, []any{addr, name}
|
|
}
|
|
|
|
// DeleteSubjectKV removes subject kv map.
|
|
// Must be invoked by contract owner.
|
|
func (c Client) DeleteSubjectKV(addr util.Uint160, key string) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.DeleteSubjectKVCall(addr, key)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// DeleteSubjectKVCall provides args for SetSubjectLogin to use in commonclient.Transaction.
|
|
func (c Client) DeleteSubjectKVCall(addr util.Uint160, key string) (method string, args []any) {
|
|
return deleteSubjectKVMethod, []any{addr, key}
|
|
}
|
|
|
|
// GetSubjectByKey gets subject by its primary or additional keys.
|
|
func (c Client) GetSubjectByKey(key *keys.PublicKey) (*Subject, error) {
|
|
items, err := unwrap.Array(c.act.Call(c.contract, getSubjectByKeyMethod, key.Bytes()))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return parseSubject(items)
|
|
}
|
|
|
|
// GetSubjectKeyByName gets subject public key by its name (namespace scope).
|
|
func (c Client) GetSubjectKeyByName(namespace, subjectName string) (*keys.PublicKey, error) {
|
|
return unwrap.PublicKey(c.act.Call(c.contract, getSubjectKeyByNameMethod, namespace, subjectName))
|
|
}
|
|
|
|
// DeleteSubject delete subject and removes it from related namespaces and groups.
|
|
// Must be invoked by contract owner.
|
|
func (c Client) DeleteSubject(addr util.Uint160) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.DeleteSubjectCall(addr)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// DeleteSubjectCall provides args for DeleteSubject to use in commonclient.Transaction.
|
|
func (c Client) DeleteSubjectCall(addr util.Uint160) (method string, args []any) {
|
|
return deleteSubjectMethod, []any{addr}
|
|
}
|
|
|
|
// CreateNamespace create new namespace.
|
|
// Must be invoked by contract owner.
|
|
func (c Client) CreateNamespace(namespace string) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.CreateNamespaceCall(namespace)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// CreateNamespaceCall provides args for CreateNamespace to use in commonclient.Transaction.
|
|
func (c Client) CreateNamespaceCall(namespace string) (method string, args []any) {
|
|
return createNamespaceMethod, []any{namespace}
|
|
}
|
|
|
|
// GetNamespace gets namespace.
|
|
func (c Client) GetNamespace(namespace string) (*Namespace, error) {
|
|
items, err := unwrap.Array(c.act.Call(c.contract, getNamespaceMethod, namespace))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return parseNamespace(items)
|
|
}
|
|
|
|
// GetNamespaceExtended gets extended namespace.
|
|
func (c Client) GetNamespaceExtended(namespace string) (*NamespaceExtended, error) {
|
|
items, err := unwrap.Array(c.act.Call(c.contract, getNamespaceExtendedMethod, namespace))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return parseNamespaceExtended(items)
|
|
}
|
|
|
|
// ListNamespaces gets all namespaces.
|
|
func (c Client) ListNamespaces() ([]*Namespace, error) {
|
|
items, err := commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listNamespacesMethod)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return parseNamespaces(items)
|
|
}
|
|
|
|
// AddSubjectToNamespace adds a subject to namespace.
|
|
// Must be invoked by contract owner.
|
|
func (c Client) AddSubjectToNamespace(addr util.Uint160, namespace string) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.AddSubjectToNamespaceCall(addr, namespace)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// AddSubjectToNamespaceCall provides args for AddSubjectToNamespace to use in commonclient.Transaction.
|
|
func (c Client) AddSubjectToNamespaceCall(addr util.Uint160, namespace string) (method string, args []any) {
|
|
return addSubjectToNamespaceMethod, []any{addr, namespace}
|
|
}
|
|
|
|
// RemoveSubjectFromNamespace removes a subject from namespace.
|
|
// Must be invoked by contract owner.
|
|
func (c Client) RemoveSubjectFromNamespace(addr util.Uint160) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.RemoveSubjectFromNamespaceCall(addr)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// RemoveSubjectFromNamespaceCall provides args for RemoveSubjectFromNamespace to use in commonclient.Transaction.
|
|
func (c Client) RemoveSubjectFromNamespaceCall(addr util.Uint160) (method string, args []any) {
|
|
return removeSubjectFromNamespaceMethod, []any{addr}
|
|
}
|
|
|
|
// ListNamespaceSubjects gets all subjects from namespace.
|
|
func (c Client) ListNamespaceSubjects(namespace string) ([]util.Uint160, error) {
|
|
return unwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listNamespaceSubjectsMethod, namespace))
|
|
}
|
|
|
|
// CreateGroup creates a new group in specific namespace.
|
|
// Must be invoked by contract owner.
|
|
func (c Client) CreateGroup(namespace, group string) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.CreateGroupCall(namespace, group)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// CreateGroupCall provides args for CreateGroup to use in commonclient.Transaction.
|
|
func (c Client) CreateGroupCall(namespace, group string) (method string, args []any) {
|
|
return createGroupMethod, []any{namespace, group}
|
|
}
|
|
|
|
// GetGroup gets group.
|
|
func (c Client) GetGroup(namespace, group string) (*Group, error) {
|
|
items, err := unwrap.Array(c.act.Call(c.contract, getGroupMethod, namespace, group))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return parseGroup(items)
|
|
}
|
|
|
|
// GetGroupExtended gets extended group.
|
|
func (c Client) GetGroupExtended(namespace, group string) (*GroupExtended, error) {
|
|
items, err := unwrap.Array(c.act.Call(c.contract, getGroupExtendedMethod, namespace, group))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return parseGroupExtended(items)
|
|
}
|
|
|
|
// ListGroups gets all groups in specific namespace.
|
|
func (c Client) ListGroups(namespace string) ([]*Group, error) {
|
|
items, err := commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listGroupsMethod, namespace)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return parseGroups(items)
|
|
}
|
|
|
|
// AddSubjectToGroup adds a new subject to group.
|
|
// Must be invoked by contract owner.
|
|
func (c Client) AddSubjectToGroup(addr util.Uint160, group string) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.AddSubjectToGroupCall(addr, group)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// AddSubjectToGroupCall provides args for AddSubjectToGroup to use in commonclient.Transaction.
|
|
func (c Client) AddSubjectToGroupCall(addr util.Uint160, group string) (method string, args []any) {
|
|
return addSubjectToGroupMethod, []any{addr, group}
|
|
}
|
|
|
|
// RemoveSubjectFromGroup removes subject from group.
|
|
// Must be invoked by contract owner.
|
|
func (c Client) RemoveSubjectFromGroup(addr util.Uint160, namespace, group string) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.RemoveSubjectFromGroupCall(addr, namespace, group)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// RemoveSubjectFromGroupCall provides args for RemoveSubjectFromGroup to use in commonclient.Transaction.
|
|
func (c Client) RemoveSubjectFromGroupCall(addr util.Uint160, namespace, group string) (method string, args []any) {
|
|
return removeSubjectFromGroupMethod, []any{addr, namespace, group}
|
|
}
|
|
|
|
// ListGroupSubjects gets all subjects in specific group.
|
|
func (c Client) ListGroupSubjects(namespace, group string) ([]util.Uint160, error) {
|
|
return unwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listGroupSubjectsMethod, namespace, group))
|
|
}
|
|
|
|
// DeleteGroup deletes group.
|
|
// Must be invoked by contract owner.
|
|
func (c Client) DeleteGroup(namespace, group string) (tx util.Uint256, vub uint32, err error) {
|
|
method, args := c.DeleteGroupCall(namespace, group)
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
}
|
|
|
|
// DeleteGroupCall provides args for DeleteGroup to use in commonclient.Transaction.
|
|
func (c Client) DeleteGroupCall(namespace, group string) (method string, args []any) {
|
|
return deleteGroupMethod, []any{namespace, group}
|
|
}
|
|
|
|
// ListNonEmptyNamespaces gets namespaces that contain at least one subject.
|
|
func (c Client) ListNonEmptyNamespaces() ([]string, error) {
|
|
namespaces, err := c.ListNamespaces()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var res []string
|
|
|
|
for _, namespace := range namespaces {
|
|
nsExt, err := c.GetNamespaceExtended(namespace.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if nsExt.SubjectsCount > 0 {
|
|
res = append(res, nsExt.Name)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
// Wait invokes underlying wait method on actor.Actor.
|
|
func (c Client) Wait(tx util.Uint256, vub uint32, err error) (*state.AppExecResult, error) {
|
|
return c.act.Wait(tx, vub, err)
|
|
}
|
|
|
|
// ListNonEmptyGroups gets groups that contain at least one subject.
|
|
func (c Client) ListNonEmptyGroups(namespace string) ([]string, error) {
|
|
groups, err := c.ListGroups(namespace)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var res []string
|
|
|
|
for _, group := range groups {
|
|
groupExt, err := c.GetGroupExtended(namespace, group.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if groupExt.SubjectsCount > 0 {
|
|
res = append(res, groupExt.Name)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func unwrapArrayOfUint160(items []stackitem.Item, err error) ([]util.Uint160, error) {
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return unwrap.ArrayOfUint160(makeValidRes(stackitem.NewArray(items)))
|
|
}
|
|
|
|
func makeValidRes(item stackitem.Item) (*result.Invoke, error) {
|
|
return &result.Invoke{
|
|
Stack: []stackitem.Item{item},
|
|
State: vmstate.Halt.String(),
|
|
}, nil
|
|
}
|
|
|
|
func parseSubject(structArr []stackitem.Item) (*Subject, error) {
|
|
if len(structArr) < 5 {
|
|
return nil, errors.New("invalid response subject struct")
|
|
}
|
|
|
|
var (
|
|
err error
|
|
subj Subject
|
|
)
|
|
|
|
subj.PrimaryKey, err = unwrap.PublicKey(makeValidRes(structArr[0]))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !structArr[1].Equals(stackitem.Null{}) {
|
|
subj.AdditionalKeys, err = unwrap.ArrayOfPublicKeys(makeValidRes(structArr[1]))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if !structArr[2].Equals(stackitem.Null{}) {
|
|
subj.Namespace, err = unwrap.UTF8String(makeValidRes(structArr[2]))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
if !structArr[2].Equals(stackitem.Null{}) {
|
|
subj.Name, err = unwrap.UTF8String(makeValidRes(structArr[3]))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
subj.KV, err = parseMap(structArr[4])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &subj, nil
|
|
}
|
|
|
|
func parseSubjectExtended(structArr []stackitem.Item) (*SubjectExtended, error) {
|
|
if len(structArr) < 6 {
|
|
return nil, errors.New("invalid response subject extended struct")
|
|
}
|
|
|
|
var (
|
|
err error
|
|
subj SubjectExtended
|
|
)
|
|
|
|
subj.PrimaryKey, err = unwrap.PublicKey(makeValidRes(structArr[0]))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !structArr[1].Equals(stackitem.Null{}) {
|
|
subj.AdditionalKeys, err = unwrap.ArrayOfPublicKeys(makeValidRes(structArr[1]))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
nsBytes, err := structArr[2].TryBytes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
subj.Namespace = string(nsBytes)
|
|
|
|
nameBytes, err := structArr[3].TryBytes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
subj.Name = string(nameBytes)
|
|
|
|
subj.KV, err = parseMap(structArr[4])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !structArr[5].Equals(stackitem.Null{}) {
|
|
groupItems, ok := structArr[5].Value().([]stackitem.Item)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid groups field")
|
|
}
|
|
|
|
subj.Groups, err = parseGroups(groupItems)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return &subj, nil
|
|
}
|
|
|
|
func parseMap(item stackitem.Item) (map[string]string, error) {
|
|
if item.Equals(stackitem.Null{}) {
|
|
return nil, nil
|
|
}
|
|
|
|
metaMap, err := unwrap.Map(makeValidRes(item))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
meta, ok := metaMap.Value().([]stackitem.MapElement)
|
|
if !ok {
|
|
return nil, errors.New("invalid map type")
|
|
}
|
|
|
|
res := make(map[string]string, len(meta))
|
|
for _, element := range meta {
|
|
key, err := element.Key.TryBytes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
val, err := element.Value.TryBytes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res[string(key)] = string(val)
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func parseNamespace(structArr []stackitem.Item) (*Namespace, error) {
|
|
if len(structArr) < 1 {
|
|
return nil, errors.New("invalid response namespace struct")
|
|
}
|
|
|
|
name, err := structArr[0].TryBytes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Namespace{Name: string(name)}, nil
|
|
}
|
|
|
|
func parseNamespaceExtended(structArr []stackitem.Item) (*NamespaceExtended, error) {
|
|
if len(structArr) < 3 {
|
|
return nil, errors.New("invalid response namespace extended struct")
|
|
}
|
|
|
|
name, err := structArr[0].TryBytes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
groupCount, err := structArr[1].TryInteger()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
subjectCount, err := structArr[2].TryInteger()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &NamespaceExtended{
|
|
Name: string(name),
|
|
GroupsCount: groupCount.Int64(),
|
|
SubjectsCount: subjectCount.Int64(),
|
|
}, nil
|
|
}
|
|
|
|
func parseNamespaces(items []stackitem.Item) ([]*Namespace, error) {
|
|
var err error
|
|
res := make([]*Namespace, len(items))
|
|
|
|
for i := 0; i < len(items); i++ {
|
|
arr, ok := items[i].Value().([]stackitem.Item)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid namespace type")
|
|
}
|
|
res[i], err = parseNamespace(arr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func parseGroup(structArr []stackitem.Item) (*Group, error) {
|
|
if len(structArr) < 2 {
|
|
return nil, errors.New("invalid response group struct")
|
|
}
|
|
|
|
name, err := structArr[0].TryBytes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
namespace, err := structArr[1].TryBytes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Group{
|
|
Name: string(name),
|
|
Namespace: string(namespace),
|
|
}, nil
|
|
}
|
|
|
|
func parseGroupExtended(structArr []stackitem.Item) (*GroupExtended, error) {
|
|
if len(structArr) < 3 {
|
|
return nil, errors.New("invalid response group extended struct")
|
|
}
|
|
|
|
name, err := structArr[0].TryBytes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
namespace, err := structArr[1].TryBytes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
subjectCount, err := structArr[2].TryInteger()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &GroupExtended{
|
|
Name: string(name),
|
|
Namespace: string(namespace),
|
|
SubjectsCount: subjectCount.Int64(),
|
|
}, nil
|
|
}
|
|
|
|
func parseGroups(items []stackitem.Item) ([]*Group, error) {
|
|
var err error
|
|
res := make([]*Group, len(items))
|
|
|
|
for i := 0; i < len(items); i++ {
|
|
arr, ok := items[i].Value().([]stackitem.Item)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid group type")
|
|
}
|
|
res[i], err = parseGroup(arr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|