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/core/transaction" "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/notary" "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" "github.com/nspcc-dev/neo-go/pkg/rpcclient/waiter" "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 contract util.Uint160 } Options struct { ProxyContract util.Uint160 } ) 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 { ID int64 Name string Namespace string KV map[string]string } GroupExtended struct { ID int64 Name string Namespace string KV map[string]string SubjectsCount int64 } ) const ( IAMPathKey = "iam-path" IAMARNKey = "iam-arn" IAMCreatedTimeKey = "ctime" IAMModifiedTimeKey = "mtime" ) const iteratorBatchSize = 100 const ( getAdminMethod = "getAdmin" setAdminMethod = "setAdmin" clearAdminMethod = "clearAdmin" versionMethod = "version" createSubjectMethod = "createSubject" getSubjectMethod = "getSubject" getSubjectExtendedMethod = "getSubjectExtended" listSubjectsMethod = "listSubjects" addSubjectKeyMethod = "addSubjectKey" removeSubjectKeyMethod = "removeSubjectKey" getSubjectByKeyMethod = "getSubjectByKey" getSubjectByNameMethod = "getSubjectByName" getSubjectKeyByNameMethod = "getSubjectKeyByName" setSubjectKVMethod = "setSubjectKV" setSubjectNameMethod = "setSubjectName" deleteSubjectKVMethod = "deleteSubjectKV" deleteSubjectMethod = "deleteSubject" createNamespaceMethod = "createNamespace" getNamespaceMethod = "getNamespace" getNamespaceExtendedMethod = "getNamespaceExtended" listNamespacesMethod = "listNamespaces" listNamespaceSubjectsMethod = "listNamespaceSubjects" createGroupMethod = "createGroup" getGroupMethod = "getGroup" getGroupExtendedMethod = "getGroupExtended" getGroupIDByNameMethod = "getGroupIDByName" setGroupNameMethod = "setGroupName" setGroupKVMethod = "setGroupKV" deleteGroupKVMethod = "deleteGroupKV" listGroupsMethod = "listGroups" addSubjectToGroupMethod = "addSubjectToGroup" removeSubjectFromGroupMethod = "removeSubjectFromGroup" listGroupSubjectsMethod = "listGroupSubjects" deleteGroupMethod = "deleteGroup" ) // New creates a new Client. Options can be empty. func New(ra actor.RPCActor, acc *wallet.Account, contract util.Uint160, opt Options) (*Client, error) { signers := []actor.SignerAccount{{ Signer: transaction.Signer{ Account: acc.Contract.ScriptHash(), Scopes: transaction.CalledByEntry, }, Account: acc, }} if !opt.ProxyContract.Equals(util.Uint160{}) { signers = append([]actor.SignerAccount{{ Signer: transaction.Signer{ Account: opt.ProxyContract, Scopes: transaction.CustomContracts, AllowedContracts: []util.Uint160{contract}, }, Account: notary.FakeContractAccount(opt.ProxyContract), }}, signers...) } act, err := actor.New(ra, signers) if err != nil { return nil, fmt.Errorf("init actor: %w", err) } return &Client{ act: act, contract: contract, }, nil } // NewSimple creates a new Client using exising actor.Actor. func NewSimple(act *actor.Actor, contract util.Uint160) *Client { return &Client{ act: act, contract: contract, } } // 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)) } // SetAdmin sets address that can perform write operations on contract. // Must be invoked by committee. func (c Client) SetAdmin(owner util.Uint160) (tx util.Uint256, vub uint32, err error) { method, args := c.SetAdminCall(owner) return c.act.SendCall(c.contract, method, args...) } // SetAdminCall provides args for SetAdmin to use in commonclient.Transaction. func (c Client) SetAdminCall(owner util.Uint160) (method string, args []any) { return setAdminMethod, []any{owner} } // ClearAdmin removes address that can perform write operations on contract. // Must be invoked by committee. func (c Client) ClearAdmin() (tx util.Uint256, vub uint32, err error) { method, args := c.ClearAdminCall() return c.act.SendCall(c.contract, method, args...) } // ClearAdminCall provides args for ClearAdmin to use in commonclient.Transaction. func (c Client) ClearAdminCall() (method string, args []any) { return clearAdminMethod, nil } // GetAdmin returns address that can perform write operations on contract. // Second return values is true iff admin is set. func (c Client) GetAdmin() (util.Uint160, bool, error) { item, err := unwrap.Item(c.act.Call(c.contract, getAdminMethod)) if err != nil { return util.Uint160{}, false, err } if item.Value() == nil { return util.Uint160{}, false, nil } bs, err := item.TryBytes() if err != nil { return util.Uint160{}, true, err } u, err := util.Uint160DecodeBytesBE(bs) return u, true, err } // CreateSubject creates new subject using public key and namespace. // Must be invoked by contract owner. func (c Client) CreateSubject(ns string, key *keys.PublicKey) (tx util.Uint256, vub uint32, err error) { method, args := c.CreateSubjectCall(ns, key) return c.act.SendCall(c.contract, method, args...) } // CreateSubjectCall provides args for CreateSubject to use in commonclient.Transaction. func (c Client) CreateSubjectCall(ns string, key *keys.PublicKey) (method string, args []any) { return createSubjectMethod, []any{ns, 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: IAMPathKey, IAMARNKey, IAMCreatedTimeKey, IAMModifiedTimeKey. 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 SetSubjectKV 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 SetSubjectName 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 DeleteSubjectKV 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) } // GetSubjectByName gets subject by its name (namespace scope). func (c Client) GetSubjectByName(namespace, subjectName string) (*Subject, error) { items, err := unwrap.Array(c.act.Call(c.contract, getSubjectByNameMethod, namespace, subjectName)) 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) } // 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 string, groupID int64) (*Group, error) { items, err := unwrap.Array(c.act.Call(c.contract, getGroupMethod, namespace, groupID)) if err != nil { return nil, err } return parseGroup(items) } // GetGroupExtended gets extended group. func (c Client) GetGroupExtended(namespace string, groupID int64) (*GroupExtended, error) { items, err := unwrap.Array(c.act.Call(c.contract, getGroupExtendedMethod, namespace, groupID)) if err != nil { return nil, err } return parseGroupExtended(items) } // SetGroupName updates subject name. // Must be invoked by contract owner. func (c Client) SetGroupName(namespace string, groupID int64, name string) (tx util.Uint256, vub uint32, err error) { method, args := c.SetGroupNameCall(namespace, groupID, name) return c.act.SendCall(c.contract, method, args...) } // SetGroupNameCall provides args for SetGroupName to use in commonclient.Transaction. func (c Client) SetGroupNameCall(namespace string, groupID int64, name string) (method string, args []any) { return setGroupNameMethod, []any{namespace, groupID, name} } // SetGroupKV updates group kv map. // Must be invoked by contract owner. // You can use some predefined key constants: IAMPathKey, IAMARNKey, IAMCreatedTimeKey, IAMModifiedTimeKey. func (c Client) SetGroupKV(namespace string, groupID int64, key, val string) (tx util.Uint256, vub uint32, err error) { method, args := c.SetGroupKVCall(namespace, groupID, key, val) return c.act.SendCall(c.contract, method, args...) } // SetGroupKVCall provides args for SetGroupKV to use in commonclient.Transaction. func (c Client) SetGroupKVCall(namespace string, groupID int64, key, val string) (method string, args []any) { return setGroupKVMethod, []any{namespace, groupID, key, val} } // DeleteGroupKV removes group kv map. // Must be invoked by contract owner. func (c Client) DeleteGroupKV(namespace string, groupID int64, key string) (tx util.Uint256, vub uint32, err error) { method, args := c.DeleteGroupKVCall(namespace, groupID, key) return c.act.SendCall(c.contract, method, args...) } // DeleteGroupKVCall provides args for DeleteGroupKV to use in commonclient.Transaction. func (c Client) DeleteGroupKVCall(namespace string, groupID int64, key string) (method string, args []any) { return deleteGroupKVMethod, []any{namespace, groupID, key} } // GetGroupIDByName gets group id its name (namespace scope). func (c Client) GetGroupIDByName(namespace, groupName string) (int64, error) { return unwrap.Int64(c.act.Call(c.contract, getGroupIDByNameMethod, namespace, groupName)) } // 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, groupID int64) (tx util.Uint256, vub uint32, err error) { method, args := c.AddSubjectToGroupCall(addr, groupID) 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, groupID int64) (method string, args []any) { return addSubjectToGroupMethod, []any{addr, groupID} } // RemoveSubjectFromGroup removes subject from group. // Must be invoked by contract owner. func (c Client) RemoveSubjectFromGroup(addr util.Uint160, groupID int64) (tx util.Uint256, vub uint32, err error) { method, args := c.RemoveSubjectFromGroupCall(addr, groupID) 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, groupID int64) (method string, args []any) { return removeSubjectFromGroupMethod, []any{addr, groupID} } // ListGroupSubjects gets all subjects in specific group. func (c Client) ListGroupSubjects(namespace string, groupID int64) ([]util.Uint160, error) { return unwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listGroupSubjectsMethod, namespace, groupID)) } // DeleteGroup deletes group. // Must be invoked by contract owner. func (c Client) DeleteGroup(namespace string, groupID int64) (tx util.Uint256, vub uint32, err error) { method, args := c.DeleteGroupCall(namespace, groupID) return c.act.SendCall(c.contract, method, args...) } // DeleteGroupCall provides args for DeleteGroup to use in commonclient.Transaction. func (c Client) DeleteGroupCall(namespace string, groupID int64) (method string, args []any) { return deleteGroupMethod, []any{namespace, groupID} } // 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. // Notice that "already exists" err value is treated as an error by this routine unlike actor.Waiter. func (c Client) Wait(tx util.Uint256, vub uint32, err error) (*state.AppExecResult, error) { if err != nil { return nil, err } return c.act.Wait(tx, vub, err) } // Waiter returns underlying waiter.Waiter. func (c Client) Waiter() waiter.Waiter { return c.act } // ParseGroupID fetch groupID from stack after creating group method invocation. func (c Client) ParseGroupID(res *state.AppExecResult, err error) (int64, error) { if err != nil { return 0, err } return unwrap.Int64(makeResFromAppExec(res)) } // 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.ID) 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 makeResFromAppExec(res *state.AppExecResult) (*result.Invoke, error) { return &result.Invoke{ Stack: res.Stack, State: res.VMState.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) < 4 { return nil, errors.New("invalid response group struct") } groupID, err := structArr[0].TryInteger() if err != nil { return nil, err } name, err := structArr[1].TryBytes() if err != nil { return nil, err } namespace, err := structArr[2].TryBytes() if err != nil { return nil, err } kvs, err := parseMap(structArr[3]) if err != nil { return nil, err } return &Group{ ID: groupID.Int64(), Name: string(name), Namespace: string(namespace), KV: kvs, }, nil } func parseGroupExtended(structArr []stackitem.Item) (*GroupExtended, error) { if len(structArr) < 5 { return nil, errors.New("invalid response group extended struct") } groupID, err := structArr[0].TryInteger() if err != nil { return nil, err } name, err := structArr[1].TryBytes() if err != nil { return nil, err } namespace, err := structArr[2].TryBytes() if err != nil { return nil, err } kvs, err := parseMap(structArr[3]) if err != nil { return nil, err } subjectCount, err := structArr[4].TryInteger() if err != nil { return nil, err } return &GroupExtended{ ID: groupID.Int64(), Name: string(name), Namespace: string(namespace), KV: kvs, 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 }