From a9aff8c2238d7f532f353fcae2fa003f1a26da25 Mon Sep 17 00:00:00 2001 From: Alexander Chuprov Date: Fri, 25 Oct 2024 11:19:21 +0300 Subject: [PATCH] [#119] frostfsid: Add 'GetValueForSubjectKey' Signed-off-by: Alexander Chuprov --- frostfsid/config.yml | 1 + frostfsid/frostfsid_contract.go | 24 ++++++++++++++++++++++++ rpcclient/frostfsid/client.go | 5 +++++ 3 files changed, 30 insertions(+) diff --git a/frostfsid/config.yml b/frostfsid/config.yml index 0beb114..d3bdc0c 100644 --- a/frostfsid/config.yml +++ b/frostfsid/config.yml @@ -7,6 +7,7 @@ safemethods: - "getGroupByName" - "getNamespace" - "getNamespaceExtended" + - "getValueForSubjectKey" - "getSubject" - "getSubjectExtended" - "getSubjectByKey" diff --git a/frostfsid/frostfsid_contract.go b/frostfsid/frostfsid_contract.go index d7c2f38..f7cca03 100644 --- a/frostfsid/frostfsid_contract.go +++ b/frostfsid/frostfsid_contract.go @@ -472,6 +472,30 @@ func GetSubjectKeyByName(ns, name string) interop.PublicKey { return subjKey } +// GetValueForSubjectKey GetSubjectKey returns the value associated with the key for the subject. +func GetValueForSubjectKey(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) diff --git a/rpcclient/frostfsid/client.go b/rpcclient/frostfsid/client.go index ddea629..6ddbee1 100644 --- a/rpcclient/frostfsid/client.go +++ b/rpcclient/frostfsid/client.go @@ -227,6 +227,11 @@ func (c *ContractReader) GetSubjectKeyByName(ns string, name string) (*keys.Publ return unwrap.PublicKey(c.invoker.Call(c.hash, "getSubjectKeyByName", ns, name)) } +// GetValueForSubjectKey invokes `getValueForSubjectKey` method of contract. +func (c *ContractReader) GetValueForSubjectKey(addr util.Uint160, name string) (string, error) { + return unwrap.UTF8String(c.invoker.Call(c.hash, "getValueForSubjectKey", addr, name)) +} + // ListGroupSubjects invokes `listGroupSubjects` method of contract. func (c *ContractReader) ListGroupSubjects(ns string, groupID *big.Int) (uuid.UUID, result.Iterator, error) { return unwrap.SessionIterator(c.invoker.Call(c.hash, "listGroupSubjects", ns, groupID))