frostfsid: Return subject by an additional address #129
2 changed files with 27 additions and 11 deletions
|
@ -416,9 +416,14 @@ func GetSubject(addr interop.Hash160) Subject {
|
|||
ctx := storage.GetReadOnlyContext()
|
||||
sKey := subjectKeyFromAddr(addr)
|
||||
data := storage.Get(ctx, sKey).([]byte)
|
||||
if data == nil {
|
||||
a := getPrimaryAddr(ctx, addr)
|
||||
sKey = subjectKeyFromAddr(a)
|
||||
data = storage.Get(ctx, sKey).([]byte)
|
||||
if data == nil {
|
||||
panic("subject not found")
|
||||
}
|
||||
}
|
||||
|
||||
return std.Deserialize(data).(Subject)
|
||||
}
|
||||
|
@ -469,21 +474,25 @@ func GetSubjectByKey(key interop.PublicKey) Subject {
|
|||
return std.Deserialize(data).(Subject)
|
||||
}
|
||||
|
||||
saPrefix := subjectAdditionalPrefix(key)
|
||||
it := storage.Find(ctx, saPrefix, storage.KeysOnly|storage.RemovePrefix)
|
||||
for iterator.Next(it) {
|
||||
addr := iterator.Value(it).([]byte)
|
||||
addr := getPrimaryAddr(ctx, contract.CreateStandardAccount(key))
|
||||
sKey = subjectKeyFromAddr(addr)
|
||||
data = storage.Get(ctx, sKey).([]byte)
|
||||
if data != nil {
|
||||
return std.Deserialize(data).(Subject)
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
panic("subject not found")
|
||||
}
|
||||
|
||||
func getPrimaryAddr(ctx storage.Context, addr interop.Hash160) interop.Hash160 {
|
||||
saPrefix := append([]byte{additionalKeysPrefix}, addr...)
|
||||
it := storage.Find(ctx, saPrefix, storage.KeysOnly|storage.RemovePrefix)
|
||||
if iterator.Next(it) {
|
||||
return iterator.Value(it).([]byte)
|
||||
}
|
||||
panic("subject not found")
|
||||
}
|
||||
|
||||
// GetSubjectByName retrieves the subject with the specified name within the given namespace.
|
||||
func GetSubjectByName(ns, name string) Subject {
|
||||
key := GetSubjectKeyByName(ns, name)
|
||||
|
|
|
@ -238,6 +238,13 @@ func TestFrostFSID_SubjectManagement(t *testing.T) {
|
|||
subj = parseSubject(t, s)
|
||||
require.True(t, subjKey.PublicKey().Equal(&subj.PrimaryKey), "keys must be the same")
|
||||
|
||||
t.Run("with GetSubject", func(t *testing.T) {
|
||||
s, err = anonInvoker.TestInvoke(t, getSubjectMethod, subjKey.PublicKey().GetScriptHash())
|
||||
require.NoError(t, err)
|
||||
subj = parseSubject(t, s)
|
||||
require.True(t, subjKey.PublicKey().Equal(&subj.PrimaryKey), "keys must be the same")
|
||||
})
|
||||
|
||||
t.Run("remove subject key", func(t *testing.T) {
|
||||
anonInvoker.InvokeFail(t, notWitnessedError, removeSubjectKeyMethod, subjKeyAddr, subjNewKey.PublicKey().Bytes())
|
||||
invoker.Invoke(t, stackitem.Null{}, removeSubjectKeyMethod, subjKeyAddr, subjNewKey.PublicKey().Bytes())
|
||||
|
|
Loading…
Reference in a new issue