2023-11-07 10:58:16 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/commonclient"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2023-12-15 14:04:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2023-11-07 10:58:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2024-08-27 08:33:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2023-11-07 10:58:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
2023-12-15 14:04:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/notary"
|
2023-11-07 10:58:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
|
2023-12-29 11:59:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/waiter"
|
2024-08-27 08:33:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2023-11-07 10:58:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2024-08-27 08:33:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
2023-11-07 10:58:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Client struct {
|
|
|
|
act *actor.Actor
|
|
|
|
contract util.Uint160
|
|
|
|
}
|
|
|
|
|
|
|
|
Options struct {
|
2023-12-15 14:04:54 +00:00
|
|
|
ProxyContract util.Uint160
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2023-11-09 08:01:17 +00:00
|
|
|
ID int64
|
2023-11-07 10:58:16 +00:00
|
|
|
Name string
|
|
|
|
Namespace string
|
2023-11-09 08:01:17 +00:00
|
|
|
KV map[string]string
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GroupExtended struct {
|
2023-11-09 08:01:17 +00:00
|
|
|
ID int64
|
2023-11-07 10:58:16 +00:00
|
|
|
Name string
|
|
|
|
Namespace string
|
2023-11-09 08:01:17 +00:00
|
|
|
KV map[string]string
|
2023-11-07 10:58:16 +00:00
|
|
|
SubjectsCount int64
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-11-09 08:01:17 +00:00
|
|
|
IAMPathKey = "iam-path"
|
|
|
|
IAMARNKey = "iam-arn"
|
|
|
|
IAMCreatedTimeKey = "ctime"
|
|
|
|
IAMModifiedTimeKey = "mtime"
|
2023-11-07 10:58:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const iteratorBatchSize = 100
|
|
|
|
|
|
|
|
const (
|
2023-11-28 08:47:12 +00:00
|
|
|
getAdminMethod = "getAdmin"
|
|
|
|
setAdminMethod = "setAdmin"
|
|
|
|
clearAdminMethod = "clearAdmin"
|
2023-11-07 10:58:16 +00:00
|
|
|
|
|
|
|
versionMethod = "version"
|
|
|
|
|
|
|
|
createSubjectMethod = "createSubject"
|
|
|
|
getSubjectMethod = "getSubject"
|
|
|
|
getSubjectExtendedMethod = "getSubjectExtended"
|
|
|
|
listSubjectsMethod = "listSubjects"
|
|
|
|
addSubjectKeyMethod = "addSubjectKey"
|
|
|
|
removeSubjectKeyMethod = "removeSubjectKey"
|
|
|
|
getSubjectByKeyMethod = "getSubjectByKey"
|
2023-12-07 12:02:57 +00:00
|
|
|
getSubjectByNameMethod = "getSubjectByName"
|
2023-11-07 10:58:16 +00:00
|
|
|
getSubjectKeyByNameMethod = "getSubjectKeyByName"
|
|
|
|
setSubjectKVMethod = "setSubjectKV"
|
|
|
|
setSubjectNameMethod = "setSubjectName"
|
|
|
|
deleteSubjectKVMethod = "deleteSubjectKV"
|
|
|
|
deleteSubjectMethod = "deleteSubject"
|
|
|
|
|
2023-12-07 09:23:18 +00:00
|
|
|
createNamespaceMethod = "createNamespace"
|
|
|
|
getNamespaceMethod = "getNamespace"
|
|
|
|
getNamespaceExtendedMethod = "getNamespaceExtended"
|
|
|
|
listNamespacesMethod = "listNamespaces"
|
|
|
|
listNamespaceSubjectsMethod = "listNamespaceSubjects"
|
2023-11-07 10:58:16 +00:00
|
|
|
|
|
|
|
createGroupMethod = "createGroup"
|
|
|
|
getGroupMethod = "getGroup"
|
|
|
|
getGroupExtendedMethod = "getGroupExtended"
|
2023-11-09 08:01:17 +00:00
|
|
|
getGroupIDByNameMethod = "getGroupIDByName"
|
2024-01-29 11:10:43 +00:00
|
|
|
getGroupByNameMethod = "getGroupByName"
|
2023-11-09 08:01:17 +00:00
|
|
|
setGroupNameMethod = "setGroupName"
|
|
|
|
setGroupKVMethod = "setGroupKV"
|
|
|
|
deleteGroupKVMethod = "deleteGroupKV"
|
2023-11-07 10:58:16 +00:00
|
|
|
listGroupsMethod = "listGroups"
|
|
|
|
addSubjectToGroupMethod = "addSubjectToGroup"
|
|
|
|
removeSubjectFromGroupMethod = "removeSubjectFromGroup"
|
|
|
|
listGroupSubjectsMethod = "listGroupSubjects"
|
|
|
|
deleteGroupMethod = "deleteGroup"
|
|
|
|
)
|
|
|
|
|
2023-12-15 14:04:54 +00:00
|
|
|
// 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)
|
2023-11-07 10:58:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("init actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
act: act,
|
|
|
|
contract: contract,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-01-12 13:17:31 +00:00
|
|
|
// NewSimple creates a new Client using exising actor.Actor.
|
|
|
|
func NewSimple(act *actor.Actor, contract util.Uint160) *Client {
|
|
|
|
return &Client{
|
|
|
|
act: act,
|
|
|
|
contract: contract,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 10:58:16 +00:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:47:12 +00:00
|
|
|
// SetAdmin sets address that can perform write operations on contract.
|
2023-11-07 10:58:16 +00:00
|
|
|
// Must be invoked by committee.
|
2023-11-28 08:47:12 +00:00
|
|
|
func (c Client) SetAdmin(owner util.Uint160) (tx util.Uint256, vub uint32, err error) {
|
|
|
|
method, args := c.SetAdminCall(owner)
|
2023-11-07 10:58:16 +00:00
|
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:47:12 +00:00
|
|
|
// 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}
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 08:47:12 +00:00
|
|
|
// ClearAdmin removes address that can perform write operations on contract.
|
2023-11-07 10:58:16 +00:00
|
|
|
// Must be invoked by committee.
|
2023-11-28 08:47:12 +00:00
|
|
|
func (c Client) ClearAdmin() (tx util.Uint256, vub uint32, err error) {
|
|
|
|
method, args := c.ClearAdminCall()
|
2023-11-07 10:58:16 +00:00
|
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:47:12 +00:00
|
|
|
// ClearAdminCall provides args for ClearAdmin to use in commonclient.Transaction.
|
|
|
|
func (c Client) ClearAdminCall() (method string, args []any) {
|
|
|
|
return clearAdminMethod, nil
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 08:47:12 +00:00
|
|
|
// 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
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 09:23:18 +00:00
|
|
|
// CreateSubject creates new subject using public key and namespace.
|
2023-11-07 10:58:16 +00:00
|
|
|
// Must be invoked by contract owner.
|
2023-12-07 09:23:18 +00:00
|
|
|
func (c Client) CreateSubject(ns string, key *keys.PublicKey) (tx util.Uint256, vub uint32, err error) {
|
|
|
|
method, args := c.CreateSubjectCall(ns, key)
|
2023-11-07 10:58:16 +00:00
|
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSubjectCall provides args for CreateSubject to use in commonclient.Transaction.
|
2023-12-07 09:23:18 +00:00
|
|
|
func (c Client) CreateSubjectCall(ns string, key *keys.PublicKey) (method string, args []any) {
|
|
|
|
return createSubjectMethod, []any{ns, key.Bytes()}
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-06-21 11:33:04 +00:00
|
|
|
return ParseSubject(items)
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-06-21 11:33:04 +00:00
|
|
|
return ParseSubjectExtended(items)
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListSubjects gets all subjects.
|
|
|
|
func (c Client) ListSubjects() ([]util.Uint160, error) {
|
2024-06-21 11:33:04 +00:00
|
|
|
return UnwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listSubjectsMethod))
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 08:33:14 +00:00
|
|
|
// ListFullSubjects gets list of subjects.
|
|
|
|
func (c Client) ListFullSubjects(hashes []util.Uint160) ([]*Subject, error) {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
|
|
|
|
for _, hash := range hashes {
|
|
|
|
emit.AppCall(w.BinWriter, c.contract, getSubjectMethod, callflag.All, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
invoker, err := c.act.Run(w.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if invoker.State != vmstate.Halt.String() {
|
|
|
|
return nil, fmt.Errorf("invocation failed: %s", invoker.FaultException)
|
|
|
|
}
|
|
|
|
subjects := make([]*Subject, 0, len(invoker.Stack))
|
|
|
|
|
|
|
|
for _, item := range invoker.Stack {
|
|
|
|
arr, ok := item.Value().([]stackitem.Item)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid subject")
|
|
|
|
}
|
|
|
|
|
|
|
|
subject, err := ParseSubject(arr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
subjects = append(subjects, subject)
|
|
|
|
}
|
|
|
|
|
|
|
|
return subjects, nil
|
|
|
|
}
|
|
|
|
|
2023-11-07 10:58:16 +00:00
|
|
|
// 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.
|
2023-11-09 08:01:17 +00:00
|
|
|
// You can use some predefined key constants: IAMPathKey, IAMARNKey, IAMCreatedTimeKey, IAMModifiedTimeKey.
|
2023-11-07 10:58:16 +00:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
// SetSubjectKVCall provides args for SetSubjectKV to use in commonclient.Transaction.
|
2023-11-07 10:58:16 +00:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
// SetSubjectNameCall provides args for SetSubjectName to use in commonclient.Transaction.
|
2023-11-07 10:58:16 +00:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
// DeleteSubjectKVCall provides args for DeleteSubjectKV to use in commonclient.Transaction.
|
2023-11-07 10:58:16 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-06-21 11:33:04 +00:00
|
|
|
return ParseSubject(items)
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
2023-12-07 12:02:57 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-06-21 11:33:04 +00:00
|
|
|
return ParseSubject(items)
|
2023-12-07 12:02:57 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:58:16 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-06-21 11:33:04 +00:00
|
|
|
return ParseNamespace(items)
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-06-21 11:33:04 +00:00
|
|
|
return ParseNamespaceExtended(items)
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-06-21 11:33:04 +00:00
|
|
|
return ParseNamespaces(items)
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListNamespaceSubjects gets all subjects from namespace.
|
|
|
|
func (c Client) ListNamespaceSubjects(namespace string) ([]util.Uint160, error) {
|
2024-06-21 11:33:04 +00:00
|
|
|
return UnwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listNamespaceSubjectsMethod, namespace))
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2023-11-09 08:01:17 +00:00
|
|
|
func (c Client) GetGroup(namespace string, groupID int64) (*Group, error) {
|
|
|
|
items, err := unwrap.Array(c.act.Call(c.contract, getGroupMethod, namespace, groupID))
|
2023-11-07 10:58:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-21 11:33:04 +00:00
|
|
|
return ParseGroup(items)
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetGroupExtended gets extended group.
|
2023-11-09 08:01:17 +00:00
|
|
|
func (c Client) GetGroupExtended(namespace string, groupID int64) (*GroupExtended, error) {
|
|
|
|
items, err := unwrap.Array(c.act.Call(c.contract, getGroupExtendedMethod, namespace, groupID))
|
2023-11-07 10:58:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-21 11:33:04 +00:00
|
|
|
return ParseGroupExtended(items)
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
2024-01-29 11:10:43 +00:00
|
|
|
// GetGroupByName gets group by its name (namespace scope).
|
|
|
|
func (c Client) GetGroupByName(namespace, groupName string) (*Group, error) {
|
|
|
|
items, err := unwrap.Array(c.act.Call(c.contract, getGroupByNameMethod, namespace, groupName))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-21 11:33:04 +00:00
|
|
|
return ParseGroup(items)
|
2024-01-29 11:10:43 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:58:16 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-06-21 11:33:04 +00:00
|
|
|
return ParseGroups(items)
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddSubjectToGroup adds a new subject to group.
|
|
|
|
// Must be invoked by contract owner.
|
2023-11-09 08:01:17 +00:00
|
|
|
func (c Client) AddSubjectToGroup(addr util.Uint160, groupID int64) (tx util.Uint256, vub uint32, err error) {
|
|
|
|
method, args := c.AddSubjectToGroupCall(addr, groupID)
|
2023-11-07 10:58:16 +00:00
|
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddSubjectToGroupCall provides args for AddSubjectToGroup to use in commonclient.Transaction.
|
2023-11-09 08:01:17 +00:00
|
|
|
func (c Client) AddSubjectToGroupCall(addr util.Uint160, groupID int64) (method string, args []any) {
|
|
|
|
return addSubjectToGroupMethod, []any{addr, groupID}
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveSubjectFromGroup removes subject from group.
|
|
|
|
// Must be invoked by contract owner.
|
2023-11-09 08:01:17 +00:00
|
|
|
func (c Client) RemoveSubjectFromGroup(addr util.Uint160, groupID int64) (tx util.Uint256, vub uint32, err error) {
|
|
|
|
method, args := c.RemoveSubjectFromGroupCall(addr, groupID)
|
2023-11-07 10:58:16 +00:00
|
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveSubjectFromGroupCall provides args for RemoveSubjectFromGroup to use in commonclient.Transaction.
|
2023-11-09 08:01:17 +00:00
|
|
|
func (c Client) RemoveSubjectFromGroupCall(addr util.Uint160, groupID int64) (method string, args []any) {
|
|
|
|
return removeSubjectFromGroupMethod, []any{addr, groupID}
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListGroupSubjects gets all subjects in specific group.
|
2023-11-09 08:01:17 +00:00
|
|
|
func (c Client) ListGroupSubjects(namespace string, groupID int64) ([]util.Uint160, error) {
|
2024-06-21 11:33:04 +00:00
|
|
|
return UnwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listGroupSubjectsMethod, namespace, groupID))
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteGroup deletes group.
|
|
|
|
// Must be invoked by contract owner.
|
2023-11-09 08:01:17 +00:00
|
|
|
func (c Client) DeleteGroup(namespace string, groupID int64) (tx util.Uint256, vub uint32, err error) {
|
|
|
|
method, args := c.DeleteGroupCall(namespace, groupID)
|
2023-11-07 10:58:16 +00:00
|
|
|
return c.act.SendCall(c.contract, method, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteGroupCall provides args for DeleteGroup to use in commonclient.Transaction.
|
2023-11-09 08:01:17 +00:00
|
|
|
func (c Client) DeleteGroupCall(namespace string, groupID int64) (method string, args []any) {
|
|
|
|
return deleteGroupMethod, []any{namespace, groupID}
|
2023-11-07 10:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2023-12-07 12:02:57 +00:00
|
|
|
// Notice that "already exists" err value is treated as an error by this routine unlike actor.Waiter.
|
2023-11-07 10:58:16 +00:00
|
|
|
func (c Client) Wait(tx util.Uint256, vub uint32, err error) (*state.AppExecResult, error) {
|
2023-12-07 12:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-07 10:58:16 +00:00
|
|
|
return c.act.Wait(tx, vub, err)
|
|
|
|
}
|
|
|
|
|
2023-12-29 11:59:14 +00:00
|
|
|
// Waiter returns underlying waiter.Waiter.
|
|
|
|
func (c Client) Waiter() waiter.Waiter {
|
2023-12-07 12:02:57 +00:00
|
|
|
return c.act
|
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
2023-11-07 10:58:16 +00:00
|
|
|
// 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 {
|
2023-11-09 08:01:17 +00:00
|
|
|
groupExt, err := c.GetGroupExtended(namespace, group.ID)
|
2023-11-07 10:58:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if groupExt.SubjectsCount > 0 {
|
|
|
|
res = append(res, groupExt.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|