[#119] frostfsid: Add 'getSubjectKV'

Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
Alexander Chuprov 2024-10-28 15:51:04 +03:00
parent 5f956751d4
commit 5aaf4b08fe
Signed by: achuprov
GPG key ID: 2D916FFD803B0EDD
4 changed files with 45 additions and 0 deletions

View file

@ -96,6 +96,7 @@ const (
createSubjectMethod = "createSubject" createSubjectMethod = "createSubject"
getSubjectMethod = "getSubject" getSubjectMethod = "getSubject"
getSubjectExtendedMethod = "getSubjectExtended" getSubjectExtendedMethod = "getSubjectExtended"
getSubjectKVMethod = "getSubjectKV"
listSubjectsMethod = "listSubjects" listSubjectsMethod = "listSubjects"
addSubjectKeyMethod = "addSubjectKey" addSubjectKeyMethod = "addSubjectKey"
removeSubjectKeyMethod = "removeSubjectKey" removeSubjectKeyMethod = "removeSubjectKey"
@ -266,6 +267,16 @@ func (c Client) GetSubjectExtended(addr util.Uint160) (*SubjectExtended, error)
return ParseSubjectExtended(items) return ParseSubjectExtended(items)
} }
// GetSubjectKV invokes `getSubjectKV` method of contract.
func (c Client) GetSubjectKV(addr util.Uint160, name string) (string, error) {
value, err := unwrap.UTF8String(c.act.Call(c.contract, getSubjectKVMethod, addr, name))
if err != nil {
return "", err
}
return value, nil
}
// ListSubjects gets all subjects. // ListSubjects gets all subjects.
func (c Client) ListSubjects() ([]util.Uint160, error) { func (c Client) ListSubjects() ([]util.Uint160, error) {
return UnwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listSubjectsMethod)) return UnwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listSubjectsMethod))

View file

@ -7,6 +7,7 @@ safemethods:
- "getGroupByName" - "getGroupByName"
- "getNamespace" - "getNamespace"
- "getNamespaceExtended" - "getNamespaceExtended"
- "getSubjectKV"
- "getSubject" - "getSubject"
- "getSubjectExtended" - "getSubjectExtended"
- "getSubjectByKey" - "getSubjectByKey"

View file

@ -472,6 +472,34 @@ func GetSubjectKeyByName(ns, name string) interop.PublicKey {
return subjKey return subjKey
} }
// GetSubjectKV GetSubjectKey returns the value associated with the key for the subject.
func GetSubjectKV(addr interop.Hash160, name string) string {
if len(addr) != interop.Hash160Len {
panic("incorrect address length")
}
ctx := storage.GetReadOnlyContext()
sKey := subjectKeyFromAddr(addr)
data := storage.Get(ctx, sKey).([]byte)
if data == nil {
return ""
}
sbj := std.Deserialize(data).(Subject)
if sbj.KV == nil {
return ""
}
for k, v := range sbj.KV {
if k == name {
return v
}
}
return ""
}
func ListSubjects() iterator.Iterator { func ListSubjects() iterator.Iterator {
ctx := storage.GetReadOnlyContext() ctx := storage.GetReadOnlyContext()
return storage.Find(ctx, []byte{subjectKeysPrefix}, storage.KeysOnly|storage.RemovePrefix) return storage.Find(ctx, []byte{subjectKeysPrefix}, storage.KeysOnly|storage.RemovePrefix)

View file

@ -222,6 +222,11 @@ func (c *ContractReader) GetSubjectExtended(addr util.Uint160) ([]stackitem.Item
return unwrap.Array(c.invoker.Call(c.hash, "getSubjectExtended", addr)) return unwrap.Array(c.invoker.Call(c.hash, "getSubjectExtended", addr))
} }
// GetSubjectKV invokes `getSubjectKV` method of contract.
func (c *ContractReader) GetSubjectKV(addr util.Uint160, name string) (string, error) {
return unwrap.UTF8String(c.invoker.Call(c.hash, "getSubjectKV", addr, name))
}
// GetSubjectKeyByName invokes `getSubjectKeyByName` method of contract. // GetSubjectKeyByName invokes `getSubjectKeyByName` method of contract.
func (c *ContractReader) GetSubjectKeyByName(ns string, name string) (*keys.PublicKey, error) { func (c *ContractReader) GetSubjectKeyByName(ns string, name string) (*keys.PublicKey, error) {
return unwrap.PublicKey(c.invoker.Call(c.hash, "getSubjectKeyByName", ns, name)) return unwrap.PublicKey(c.invoker.Call(c.hash, "getSubjectKeyByName", ns, name))