[#119] frostfsid: Add 'getSubjectKV'

Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
This commit is contained in:
Alexander Chuprov 2024-10-28 12:33:25 +03:00
parent 5f956751d4
commit 96b7ecc215
Signed by: achuprov
GPG key ID: 2D916FFD803B0EDD
4 changed files with 41 additions and 0 deletions

View file

@ -96,6 +96,7 @@ const (
createSubjectMethod = "createSubject"
getSubjectMethod = "getSubject"
getSubjectExtendedMethod = "getSubjectExtended"
getSubjectKVMethod = "getSubjectKV"
listSubjectsMethod = "listSubjects"
addSubjectKeyMethod = "addSubjectKey"
removeSubjectKeyMethod = "removeSubjectKey"
@ -266,6 +267,16 @@ func (c Client) GetSubjectExtended(addr util.Uint160) (*SubjectExtended, error)
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.
func (c Client) ListSubjects() ([]util.Uint160, error) {
return UnwrapArrayOfUint160(commonclient.ReadIteratorItems(c.act, iteratorBatchSize, c.contract, listSubjectsMethod))

View file

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

View file

@ -472,6 +472,30 @@ func GetSubjectKeyByName(ns, name string) interop.PublicKey {
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)
for k, v := range sbj.KV {
if k == name {
return v
}
}
return ""
}
func ListSubjects() iterator.Iterator {
ctx := storage.GetReadOnlyContext()
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))
}
// 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.
func (c *ContractReader) GetSubjectKeyByName(ns string, name string) (*keys.PublicKey, error) {
return unwrap.PublicKey(c.invoker.Call(c.hash, "getSubjectKeyByName", ns, name))