2023-01-11 07:33:14 +00:00
|
|
|
package frostfsid
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-07 11:06:21 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/common"
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2023-11-07 10:54:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2023-11-09 08:01:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/convert"
|
2021-11-16 14:09:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2023-11-07 10:54:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/crypto"
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
2023-11-07 10:54:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
2020-10-27 12:14:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2024-05-24 18:32:20 +00:00
|
|
|
// Subject represents a subject entity.
|
|
|
|
//
|
|
|
|
// Fields:
|
|
|
|
// - Namespace: a string representing the namespace of the subject.
|
|
|
|
// - Name: a string representing the name of the subject.
|
|
|
|
// The name must match the following regex pattern: ^[\w+=,.@-]{1,64}$
|
|
|
|
// The Subject is stored in the storage as a hash(namespace) + hash(name).
|
2023-11-07 10:54:48 +00:00
|
|
|
Subject struct {
|
|
|
|
PrimaryKey interop.PublicKey
|
|
|
|
AdditionalKeys []interop.PublicKey
|
|
|
|
Namespace string
|
|
|
|
Name string
|
|
|
|
KV map[string]string
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
SubjectExtended struct {
|
|
|
|
PrimaryKey interop.PublicKey
|
|
|
|
AdditionalKeys []interop.PublicKey
|
|
|
|
Namespace string
|
|
|
|
Name string
|
|
|
|
KV map[string]string
|
|
|
|
Groups []Group
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// Namespace represents a namespace.
|
|
|
|
//
|
|
|
|
// Fields:
|
|
|
|
// - Name: a string representing the name of the namespace.
|
|
|
|
// The custom name must match the following regex pattern:
|
|
|
|
// (^$)|(^[a-z0-9]{1,2}$)|(^[a-z0-9][a-z0-9-]{1,48}[a-z0-9]$)
|
|
|
|
// An empty Name is considered the <root> namespace.
|
|
|
|
//
|
|
|
|
// The Namespace is stored in the storage as a hash(name).
|
2023-11-07 10:54:48 +00:00
|
|
|
Namespace struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
NamespaceExtended struct {
|
|
|
|
Name string
|
|
|
|
GroupsCount int
|
|
|
|
SubjectsCount int
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// Group represents a group entity.
|
|
|
|
//
|
|
|
|
// Fields:
|
|
|
|
// - ID: an integer representing the unique identifier of the group.
|
|
|
|
// The ID is generated upon creation and is the sequential number of the group within the namespace.
|
|
|
|
// It cannot be changed once set.
|
|
|
|
// - Name: a string representing the name of the group.
|
|
|
|
// The name must match the following regex pattern: [\w+=,.@-]{1,128}$
|
|
|
|
// - Namespace: a string representing the namespace of the group.
|
|
|
|
// A group exists only within one namespace.
|
|
|
|
//
|
|
|
|
// The group is stored in the storage as a hash(namespace) + hash(name).
|
2023-11-07 10:54:48 +00:00
|
|
|
Group struct {
|
2023-11-09 08:01:17 +00:00
|
|
|
ID int
|
2023-11-07 10:54:48 +00:00
|
|
|
Name string
|
|
|
|
Namespace string
|
2023-11-09 08:01:17 +00:00
|
|
|
KV map[string]string
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GroupExtended struct {
|
2023-11-09 08:01:17 +00:00
|
|
|
ID int
|
2023-11-07 10:54:48 +00:00
|
|
|
Name string
|
|
|
|
Namespace string
|
2023-11-09 08:01:17 +00:00
|
|
|
KV map[string]string
|
2023-11-07 10:54:48 +00:00
|
|
|
SubjectsCount int
|
|
|
|
}
|
2021-11-30 11:27:12 +00:00
|
|
|
)
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
const (
|
2023-11-28 08:47:12 +00:00
|
|
|
adminKey = 'o'
|
2023-11-07 10:54:48 +00:00
|
|
|
subjectKeysPrefix = 's'
|
|
|
|
additionalKeysPrefix = 'a'
|
|
|
|
namespaceKeysPrefix = 'n'
|
|
|
|
namespaceSubjectsKeysPrefix = 'N'
|
|
|
|
namespaceSubjectsNamesPrefix = 'l'
|
|
|
|
groupKeysPrefix = 'g'
|
|
|
|
groupSubjectsKeysPrefix = 'G'
|
2023-11-09 08:01:17 +00:00
|
|
|
groupCounterKey = 'c'
|
|
|
|
namespaceGroupsNamesPrefix = 'm'
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2023-11-07 12:18:48 +00:00
|
|
|
func _deploy(data any, isUpdate bool) {
|
2021-11-16 14:09:21 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
args := data.(struct {
|
2023-11-28 08:47:12 +00:00
|
|
|
admin interop.Hash160
|
2021-11-29 16:34:11 +00:00
|
|
|
})
|
2021-05-12 08:31:07 +00:00
|
|
|
|
2023-11-28 08:47:12 +00:00
|
|
|
if args.admin != nil {
|
|
|
|
if len(args.admin) != interop.Hash160Len {
|
|
|
|
panic("incorrect length of owner address")
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
2023-11-28 08:47:12 +00:00
|
|
|
storage.Put(ctx, adminKey, args.admin)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
storage.Put(ctx, groupCounterKey, 0)
|
2023-12-07 09:23:18 +00:00
|
|
|
storage.Put(ctx, namespaceKey(""), std.Serialize(Namespace{}))
|
2023-11-09 08:01:17 +00:00
|
|
|
|
2023-01-11 07:33:14 +00:00
|
|
|
runtime.Log("frostfsid contract initialized")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// SetAdmin sets the admin address for the contract.
|
2023-11-28 08:47:12 +00:00
|
|
|
func SetAdmin(addr interop.Hash160) {
|
2023-11-07 10:54:48 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
if !common.HasUpdateAccess() {
|
|
|
|
panic("not witnessed")
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:47:12 +00:00
|
|
|
storage.Put(ctx, adminKey, addr)
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// ClearAdmin removes the admin address from the contract storage.
|
2023-11-28 08:47:12 +00:00
|
|
|
func ClearAdmin() {
|
2023-11-07 10:54:48 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
if !common.HasUpdateAccess() {
|
|
|
|
panic("not witnessed")
|
|
|
|
}
|
|
|
|
|
2023-11-28 08:47:12 +00:00
|
|
|
storage.Delete(ctx, adminKey)
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// GetAdmin retrieves the admin address from the contract storage.
|
2023-11-28 08:47:12 +00:00
|
|
|
func GetAdmin() interop.Hash160 {
|
2023-11-07 10:54:48 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2023-11-28 08:47:12 +00:00
|
|
|
return storage.Get(ctx, adminKey).(interop.Hash160)
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Update method updates contract source code and manifest. It can be invoked
|
2021-09-20 15:41:46 +00:00
|
|
|
// only by committee.
|
2023-11-07 12:18:48 +00:00
|
|
|
func Update(script []byte, manifest []byte, data any) {
|
2021-09-20 15:41:46 +00:00
|
|
|
if !common.HasUpdateAccess() {
|
|
|
|
panic("only committee can update contract")
|
2021-02-11 15:55:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-19 08:17:51 +00:00
|
|
|
management.UpdateWithData(script, manifest, common.AppendVersion(data))
|
2023-01-11 07:33:14 +00:00
|
|
|
runtime.Log("frostfsid contract updated")
|
2021-02-11 15:55:32 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
// Version returns the version of the contract.
|
|
|
|
func Version() int {
|
|
|
|
return common.Version
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// CreateSubject creates a new subject in the specified namespace with the provided public key.
|
2023-12-07 09:23:18 +00:00
|
|
|
func CreateSubject(ns string, key interop.PublicKey) {
|
2023-11-07 10:54:48 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
|
|
|
if len(key) != interop.PublicKeyCompressedLen {
|
|
|
|
panic("incorrect public key length")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
addr := contract.CreateStandardAccount(key)
|
|
|
|
sKey := subjectKeyFromAddr(addr)
|
|
|
|
data := storage.Get(ctx, sKey).([]byte)
|
|
|
|
if data != nil {
|
|
|
|
panic("subject already exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
saPrefix := subjectAdditionalPrefix(key)
|
|
|
|
it := storage.Find(ctx, saPrefix, storage.KeysOnly)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
panic("key is occupied")
|
|
|
|
}
|
|
|
|
|
2023-12-07 09:23:18 +00:00
|
|
|
nsKey := namespaceKey(ns)
|
|
|
|
data = storage.Get(ctx, nsKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("namespace not found")
|
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
subj := Subject{
|
|
|
|
PrimaryKey: key,
|
2023-12-07 09:23:18 +00:00
|
|
|
Namespace: ns,
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, sKey, std.Serialize(subj))
|
2023-12-07 09:23:18 +00:00
|
|
|
|
|
|
|
nsSubjKey := namespaceSubjectKey(ns, addr)
|
|
|
|
storage.Put(ctx, nsSubjKey, []byte{1})
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
runtime.Notify("CreateSubject", interop.Hash160(addr))
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// AddSubjectKey adds an additional public key to a subject with the specified address.
|
2023-11-07 10:54:48 +00:00
|
|
|
func AddSubjectKey(addr interop.Hash160, key interop.PublicKey) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
|
|
|
if len(addr) != interop.Hash160Len {
|
|
|
|
panic("incorrect address length")
|
|
|
|
}
|
|
|
|
if len(key) != interop.PublicKeyCompressedLen {
|
|
|
|
panic("incorrect public key length")
|
|
|
|
}
|
|
|
|
|
|
|
|
saKey := subjectAdditionalKey(key, addr)
|
|
|
|
data := storage.Get(ctx, saKey).([]byte)
|
|
|
|
if data != nil {
|
|
|
|
panic("key already added")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, saKey, []byte{1})
|
|
|
|
|
|
|
|
sKey := subjectKeyFromAddr(addr)
|
|
|
|
data = storage.Get(ctx, sKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("address not found")
|
|
|
|
}
|
|
|
|
subject := std.Deserialize(data).(Subject)
|
|
|
|
subject.AdditionalKeys = append(subject.AdditionalKeys, key)
|
|
|
|
|
|
|
|
storage.Put(ctx, sKey, std.Serialize(subject))
|
|
|
|
runtime.Notify("AddSubjectKey", addr, key)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// RemoveSubjectKey removes an additional public key from the subject with the specified address.
|
2023-11-07 10:54:48 +00:00
|
|
|
func RemoveSubjectKey(addr interop.Hash160, key interop.PublicKey) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
|
|
|
if len(addr) != interop.Hash160Len {
|
|
|
|
panic("incorrect address length")
|
|
|
|
}
|
|
|
|
if len(key) != interop.PublicKeyCompressedLen {
|
|
|
|
panic("incorrect public key length")
|
|
|
|
}
|
|
|
|
|
|
|
|
saKey := subjectAdditionalKey(key, addr)
|
|
|
|
data := storage.Get(ctx, saKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("key already removed")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Delete(ctx, saKey)
|
|
|
|
|
|
|
|
sKey := subjectKeyFromAddr(addr)
|
|
|
|
data = storage.Get(ctx, sKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("address not found")
|
|
|
|
}
|
|
|
|
subject := std.Deserialize(data).(Subject)
|
|
|
|
|
|
|
|
var additionalKeys []interop.PublicKey
|
|
|
|
for i := 0; i < len(subject.AdditionalKeys); i++ {
|
|
|
|
if !common.BytesEqual(subject.AdditionalKeys[i], key) {
|
|
|
|
additionalKeys = append(additionalKeys, subject.AdditionalKeys[i])
|
2021-11-30 08:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-07 10:54:48 +00:00
|
|
|
subject.AdditionalKeys = additionalKeys
|
|
|
|
|
|
|
|
storage.Put(ctx, sKey, std.Serialize(subject))
|
|
|
|
runtime.Notify("RemoveSubjectKey", addr, key)
|
|
|
|
}
|
2021-11-30 08:59:52 +00:00
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// SetSubjectName sets a new name for the subject with the specified address.
|
2023-11-07 10:54:48 +00:00
|
|
|
func SetSubjectName(addr interop.Hash160, name string) {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2023-11-07 10:54:48 +00:00
|
|
|
checkContractOwner(ctx)
|
2021-11-30 08:59:52 +00:00
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
if len(addr) != interop.Hash160Len {
|
|
|
|
panic("incorrect address length")
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
sKey := subjectKeyFromAddr(addr)
|
|
|
|
data := storage.Get(ctx, sKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("subject not found")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
subject := std.Deserialize(data).(Subject)
|
|
|
|
oldName := subject.Name
|
|
|
|
subject.Name = name
|
|
|
|
storage.Put(ctx, sKey, std.Serialize(subject))
|
|
|
|
|
|
|
|
updateNamespaceSubjectName(ctx, subject, oldName)
|
|
|
|
|
|
|
|
runtime.Notify("SetSubjectName", addr, name)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// SetSubjectKV sets a key-value pair for the subject with the specified address.
|
2023-11-07 10:54:48 +00:00
|
|
|
func SetSubjectKV(addr interop.Hash160, key, val string) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
|
|
|
if len(addr) != interop.Hash160Len {
|
|
|
|
panic("incorrect address length")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
sKey := subjectKeyFromAddr(addr)
|
|
|
|
data := storage.Get(ctx, sKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("subject not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
subject := std.Deserialize(data).(Subject)
|
|
|
|
if subject.KV == nil {
|
|
|
|
subject.KV = map[string]string{}
|
|
|
|
}
|
|
|
|
subject.KV[key] = val
|
|
|
|
|
|
|
|
storage.Put(ctx, sKey, std.Serialize(subject))
|
|
|
|
runtime.Notify("SetSubjectKV", addr, key, val)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// DeleteSubjectKV deletes a key-value pair from the subject with the specified address.
|
2023-11-07 10:54:48 +00:00
|
|
|
func DeleteSubjectKV(addr interop.Hash160, key string) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
|
|
|
if len(addr) != interop.Hash160Len {
|
|
|
|
panic("incorrect address length")
|
|
|
|
}
|
|
|
|
|
|
|
|
sKey := subjectKeyFromAddr(addr)
|
|
|
|
data := storage.Get(ctx, sKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("subject not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
subject := std.Deserialize(data).(Subject)
|
|
|
|
delete(subject.KV, key)
|
|
|
|
|
|
|
|
storage.Put(ctx, sKey, std.Serialize(subject))
|
|
|
|
runtime.Notify("DeleteSubjectKV", addr, key)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// DeleteSubject deletes the subject with the specified address.
|
2023-11-07 10:54:48 +00:00
|
|
|
func DeleteSubject(addr interop.Hash160) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
|
|
|
if len(addr) != interop.Hash160Len {
|
|
|
|
panic("incorrect address length")
|
|
|
|
}
|
|
|
|
|
|
|
|
sKey := subjectKeyFromAddr(addr)
|
|
|
|
data := storage.Get(ctx, sKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
subj := std.Deserialize(data).(Subject)
|
|
|
|
|
|
|
|
for i := 0; i < len(subj.AdditionalKeys); i++ {
|
|
|
|
storage.Delete(ctx, subjectAdditionalKey(subj.AdditionalKeys[i], addr))
|
|
|
|
}
|
|
|
|
storage.Delete(ctx, sKey)
|
|
|
|
|
2023-12-07 09:23:18 +00:00
|
|
|
removeSubjectFromNamespace(ctx, subj.Namespace, addr)
|
2023-11-07 10:54:48 +00:00
|
|
|
deleteNamespaceSubjectName(ctx, subj.Namespace, subj.Name)
|
|
|
|
|
|
|
|
runtime.Notify("DeleteSubject", addr)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// GetSubject retrieves the subject with the specified address.
|
2023-11-07 10:54:48 +00:00
|
|
|
func GetSubject(addr interop.Hash160) Subject {
|
|
|
|
if len(addr) != interop.Hash160Len {
|
|
|
|
panic("incorrect address length")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
sKey := subjectKeyFromAddr(addr)
|
|
|
|
data := storage.Get(ctx, sKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("subject not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return std.Deserialize(data).(Subject)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// GetSubjectExtended retrieves the extended information of the subject with the specified address.
|
2023-11-07 10:54:48 +00:00
|
|
|
func GetSubjectExtended(addr interop.Hash160) SubjectExtended {
|
2023-12-07 09:23:18 +00:00
|
|
|
subj := GetSubject(addr)
|
2023-11-07 10:54:48 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
|
|
|
var groups []Group
|
2023-12-07 09:23:18 +00:00
|
|
|
nsHash := ripemd160Hash(subj.Namespace)
|
|
|
|
it := storage.Find(ctx, groupPrefixFromHash(nsHash), storage.KeysOnly|storage.RemovePrefix)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
groupHash := iterator.Value(it).([]byte)
|
|
|
|
data := storage.Get(ctx, groupSubjectKeyFromHashes(nsHash, groupHash, addr)).([]byte)
|
|
|
|
if data != nil {
|
|
|
|
data = storage.Get(ctx, groupKeyFromHashes(nsHash, groupHash)).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("group not found")
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
2023-12-07 09:23:18 +00:00
|
|
|
groups = append(groups, std.Deserialize(data).(Group))
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subjExt := SubjectExtended{
|
|
|
|
PrimaryKey: subj.PrimaryKey,
|
|
|
|
AdditionalKeys: subj.AdditionalKeys,
|
|
|
|
Namespace: subj.Namespace,
|
|
|
|
Name: subj.Name,
|
|
|
|
KV: subj.KV,
|
|
|
|
Groups: groups,
|
|
|
|
}
|
|
|
|
|
|
|
|
return subjExt
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// GetSubjectByKey retrieves the subject associated with the provided public key.
|
2023-11-07 10:54:48 +00:00
|
|
|
func GetSubjectByKey(key interop.PublicKey) Subject {
|
|
|
|
if len(key) != interop.PublicKeyCompressedLen {
|
|
|
|
panic("incorrect key length")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
|
|
|
sKey := subjectKey(key)
|
|
|
|
data := storage.Get(ctx, sKey).([]byte)
|
|
|
|
if data != nil {
|
|
|
|
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)
|
|
|
|
sKey = subjectKeyFromAddr(addr)
|
|
|
|
data = storage.Get(ctx, sKey).([]byte)
|
|
|
|
if data != nil {
|
|
|
|
return std.Deserialize(data).(Subject)
|
2021-11-30 08:59:52 +00:00
|
|
|
}
|
2023-11-07 10:54:48 +00:00
|
|
|
break
|
2021-11-30 08:59:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
panic("subject not found")
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// GetSubjectByName retrieves the subject with the specified name within the given namespace.
|
2023-12-07 12:02:57 +00:00
|
|
|
func GetSubjectByName(ns, name string) Subject {
|
|
|
|
key := GetSubjectKeyByName(ns, name)
|
|
|
|
addr := contract.CreateStandardAccount(key)
|
|
|
|
return GetSubject(addr)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// GetSubjectKeyByName retrieves the public key of the subject with the specified namespace and name.
|
2023-11-07 10:54:48 +00:00
|
|
|
func GetSubjectKeyByName(ns, name string) interop.PublicKey {
|
2023-12-07 09:23:18 +00:00
|
|
|
if name == "" {
|
|
|
|
panic("invalid or name")
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
|
|
|
nsSubjNameKey := namespaceSubjectNameKey(ns, name)
|
|
|
|
subjKey := storage.Get(ctx, nsSubjNameKey).(interop.PublicKey)
|
|
|
|
if subjKey == nil {
|
|
|
|
panic("subject not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return subjKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListSubjects() iterator.Iterator {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return storage.Find(ctx, []byte{subjectKeysPrefix}, storage.KeysOnly|storage.RemovePrefix)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// CreateNamespace creates a new namespace with the specified name.
|
2023-11-07 10:54:48 +00:00
|
|
|
func CreateNamespace(ns string) {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2023-11-07 10:54:48 +00:00
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
|
|
|
nsKey := namespaceKey(ns)
|
|
|
|
data := storage.Get(ctx, nsKey).([]byte)
|
|
|
|
if data != nil {
|
|
|
|
panic("namespace already exists")
|
2021-11-30 08:59:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
namespace := Namespace{
|
|
|
|
Name: ns,
|
2021-04-29 13:17:41 +00:00
|
|
|
}
|
2023-11-07 10:54:48 +00:00
|
|
|
|
|
|
|
storage.Put(ctx, nsKey, std.Serialize(namespace))
|
|
|
|
runtime.Notify("CreateNamespace", ns)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// GetNamespace retrieves the namespace with the specified name.
|
2023-11-07 10:54:48 +00:00
|
|
|
func GetNamespace(ns string) Namespace {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
nsKey := namespaceKey(ns)
|
|
|
|
data := storage.Get(ctx, nsKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("namespace not found")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
return std.Deserialize(data).(Namespace)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// GetNamespaceExtended retrieves extended information about the namespace.
|
2023-11-07 10:54:48 +00:00
|
|
|
func GetNamespaceExtended(ns string) NamespaceExtended {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2023-11-07 10:54:48 +00:00
|
|
|
nsKey := namespaceKey(ns)
|
|
|
|
data := storage.Get(ctx, nsKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("namespace not found")
|
|
|
|
}
|
|
|
|
namespace := std.Deserialize(data).(Namespace)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
nsExtended := NamespaceExtended{
|
|
|
|
Name: namespace.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
it := storage.Find(ctx, groupPrefix(ns), storage.KeysOnly)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
nsExtended.GroupsCount += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
it = storage.Find(ctx, namespaceSubjectPrefix(ns), storage.KeysOnly)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
nsExtended.SubjectsCount += 1
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
return nsExtended
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
func ListNamespaces() iterator.Iterator {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return storage.Find(ctx, []byte{namespaceKeysPrefix}, storage.ValuesOnly|storage.DeserializeValues)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListNamespaceSubjects(ns string) iterator.Iterator {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return storage.Find(ctx, namespaceSubjectPrefix(ns), storage.KeysOnly|storage.RemovePrefix)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// CreateGroup creates a new group within the specified namespace.
|
2023-11-09 08:01:17 +00:00
|
|
|
func CreateGroup(ns, group string) int {
|
2023-11-07 10:54:48 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
|
|
|
if group == "" {
|
|
|
|
panic("invalid group name")
|
|
|
|
}
|
|
|
|
|
|
|
|
nsKey := namespaceKey(ns)
|
|
|
|
data := storage.Get(ctx, nsKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("namespace not found")
|
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
groupCountID := storage.Get(ctx, groupCounterKey).(int)
|
|
|
|
groupCountID++
|
|
|
|
storage.Put(ctx, groupCounterKey, groupCountID)
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
gr := Group{
|
2023-11-09 08:01:17 +00:00
|
|
|
ID: groupCountID,
|
2023-11-07 10:54:48 +00:00
|
|
|
Name: group,
|
|
|
|
Namespace: ns,
|
|
|
|
}
|
2023-11-09 08:01:17 +00:00
|
|
|
|
|
|
|
setNamespaceGroupName(ctx, gr)
|
|
|
|
|
|
|
|
gKey := groupKey(ns, groupCountID)
|
2023-11-07 10:54:48 +00:00
|
|
|
storage.Put(ctx, gKey, std.Serialize(gr))
|
|
|
|
|
|
|
|
runtime.Notify("CreateGroup", ns, group)
|
2023-11-09 08:01:17 +00:00
|
|
|
|
|
|
|
return groupCountID
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// GetGroup retrieves the group with the specified ID within the given namespace.
|
2023-11-09 08:01:17 +00:00
|
|
|
func GetGroup(ns string, groupID int) Group {
|
2023-11-07 10:54:48 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2023-11-09 08:01:17 +00:00
|
|
|
gKey := groupKey(ns, groupID)
|
2023-11-07 10:54:48 +00:00
|
|
|
data := storage.Get(ctx, gKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("group not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return std.Deserialize(data).(Group)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// GetGroupExtended retrieves extended information about the group, including the count of subjects in the group.
|
2023-11-09 08:01:17 +00:00
|
|
|
func GetGroupExtended(ns string, groupID int) GroupExtended {
|
2023-11-07 10:54:48 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2023-11-09 08:01:17 +00:00
|
|
|
gKey := groupKey(ns, groupID)
|
2023-11-07 10:54:48 +00:00
|
|
|
data := storage.Get(ctx, gKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("group not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
gr := std.Deserialize(data).(Group)
|
|
|
|
|
|
|
|
grExtended := GroupExtended{
|
2023-11-09 08:01:17 +00:00
|
|
|
ID: gr.ID,
|
2023-11-07 10:54:48 +00:00
|
|
|
Name: gr.Name,
|
|
|
|
Namespace: gr.Namespace,
|
2023-11-09 08:01:17 +00:00
|
|
|
KV: gr.KV,
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
it := storage.Find(ctx, groupSubjectPrefix(ns, groupID), storage.KeysOnly)
|
2023-11-07 10:54:48 +00:00
|
|
|
for iterator.Next(it) {
|
|
|
|
grExtended.SubjectsCount += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return grExtended
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// GetGroupIDByName retrieves the ID of the group with the specified name within the given namespace.
|
2023-11-09 08:01:17 +00:00
|
|
|
func GetGroupIDByName(ns, name string) int {
|
2023-12-07 09:23:18 +00:00
|
|
|
if name == "" {
|
|
|
|
panic("invalid name")
|
2023-11-09 08:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
|
|
|
nsGroupNameKey := namespaceGroupNameKey(ns, name)
|
|
|
|
groupIDRaw := storage.Get(ctx, nsGroupNameKey).([]byte)
|
|
|
|
if groupIDRaw == nil {
|
|
|
|
panic("group not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return std.Deserialize(groupIDRaw).(int)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// GetGroupByName retrieves the group with the specified name within the given namespace.
|
2024-01-29 11:10:43 +00:00
|
|
|
func GetGroupByName(ns, name string) Group {
|
|
|
|
groupID := GetGroupIDByName(ns, name)
|
|
|
|
gKey := groupKey(ns, groupID)
|
|
|
|
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
data := storage.Get(ctx, gKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("group not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return std.Deserialize(data).(Group)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// SetGroupName sets a new name for the group with the specified ID within the given namespace.
|
2023-11-09 08:01:17 +00:00
|
|
|
func SetGroupName(ns string, groupID int, name string) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
|
|
|
gKey := groupKey(ns, groupID)
|
|
|
|
data := storage.Get(ctx, gKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("group not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
gr := std.Deserialize(data).(Group)
|
|
|
|
oldName := gr.Name
|
|
|
|
gr.Name = name
|
|
|
|
storage.Put(ctx, gKey, std.Serialize(gr))
|
|
|
|
|
|
|
|
updateNamespaceGroupName(ctx, gr, oldName)
|
|
|
|
|
|
|
|
runtime.Notify("SetGroupName", ns, groupID, name)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// SetGroupKV sets a key-value pair for the group with the specified ID within the given namespace.
|
2023-11-09 08:01:17 +00:00
|
|
|
func SetGroupKV(ns string, groupID int, key, val string) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
|
|
|
gKey := groupKey(ns, groupID)
|
|
|
|
data := storage.Get(ctx, gKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("group not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
gr := std.Deserialize(data).(Group)
|
|
|
|
if gr.KV == nil {
|
|
|
|
gr.KV = map[string]string{}
|
|
|
|
}
|
|
|
|
gr.KV[key] = val
|
|
|
|
|
|
|
|
storage.Put(ctx, gKey, std.Serialize(gr))
|
|
|
|
runtime.Notify("SetGroupKV", ns, groupID, key, val)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// DeleteGroupKV deletes a key-value pair from the group with the specified ID within the given namespace.
|
2023-11-09 08:01:17 +00:00
|
|
|
func DeleteGroupKV(ns string, groupID int, key string) {
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
|
|
|
gKey := groupKey(ns, groupID)
|
|
|
|
data := storage.Get(ctx, gKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("group not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
gr := std.Deserialize(data).(Group)
|
|
|
|
delete(gr.KV, key)
|
|
|
|
|
|
|
|
storage.Put(ctx, gKey, std.Serialize(gr))
|
|
|
|
runtime.Notify("DeleteGroupKV", ns, groupID, key)
|
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
func ListGroups(ns string) iterator.Iterator {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return storage.Find(ctx, groupPrefix(ns), storage.ValuesOnly|storage.DeserializeValues)
|
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// AddSubjectToGroup adds a subject to a group with the specified ID.
|
2023-11-09 08:01:17 +00:00
|
|
|
func AddSubjectToGroup(addr interop.Hash160, groupID int) {
|
2023-11-07 10:54:48 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
|
|
|
if len(addr) != interop.Hash160Len {
|
|
|
|
panic("invalid address length")
|
|
|
|
}
|
|
|
|
|
|
|
|
sKey := subjectKeyFromAddr(addr)
|
|
|
|
data := storage.Get(ctx, sKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("subject not found")
|
|
|
|
}
|
|
|
|
subject := std.Deserialize(data).(Subject)
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
gKey := groupKey(subject.Namespace, groupID)
|
2023-11-07 10:54:48 +00:00
|
|
|
data = storage.Get(ctx, gKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("group not found")
|
|
|
|
}
|
2023-12-07 09:23:18 +00:00
|
|
|
group := std.Deserialize(data).(Group)
|
|
|
|
|
|
|
|
if group.Namespace != subject.Namespace {
|
|
|
|
panic("subject and group must be in the same namespace")
|
|
|
|
}
|
2023-11-07 10:54:48 +00:00
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
gsKey := groupSubjectKey(subject.Namespace, groupID, addr)
|
2023-11-07 10:54:48 +00:00
|
|
|
storage.Put(ctx, gsKey, []byte{1})
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
runtime.Notify("AddSubjectToGroup", addr, subject.Namespace, groupID)
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// RemoveSubjectFromGroup removes a subject from a group with the specified ID.
|
2023-11-09 08:01:17 +00:00
|
|
|
func RemoveSubjectFromGroup(addr interop.Hash160, groupID int) {
|
2023-11-07 10:54:48 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
|
|
|
if len(addr) != interop.Hash160Len {
|
|
|
|
panic("invalid address length")
|
|
|
|
}
|
|
|
|
|
|
|
|
sKey := subjectKeyFromAddr(addr)
|
|
|
|
data := storage.Get(ctx, sKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("subject not found")
|
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
subject := std.Deserialize(data).(Subject)
|
|
|
|
|
|
|
|
gKey := groupKey(subject.Namespace, groupID)
|
2023-11-07 10:54:48 +00:00
|
|
|
data = storage.Get(ctx, gKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("group not found")
|
|
|
|
}
|
2023-12-07 09:23:18 +00:00
|
|
|
group := std.Deserialize(data).(Group)
|
|
|
|
|
|
|
|
if group.Namespace != subject.Namespace {
|
|
|
|
panic("subject and group must be in the same namespace")
|
|
|
|
}
|
2023-11-07 10:54:48 +00:00
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
gsKey := groupSubjectKey(subject.Namespace, groupID, addr)
|
2023-11-07 10:54:48 +00:00
|
|
|
storage.Delete(ctx, gsKey)
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
runtime.Notify("RemoveSubjectFromGroup", addr, subject.Namespace, groupID)
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
func ListGroupSubjects(ns string, groupID int) iterator.Iterator {
|
2023-11-07 10:54:48 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2023-11-09 08:01:17 +00:00
|
|
|
return storage.Find(ctx, groupSubjectPrefix(ns, groupID), storage.KeysOnly|storage.RemovePrefix)
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 18:32:20 +00:00
|
|
|
// DeleteGroup deletes the group with the specified ID within the given namespace.
|
2023-11-09 08:01:17 +00:00
|
|
|
func DeleteGroup(ns string, groupID int) {
|
2023-11-07 10:54:48 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
checkContractOwner(ctx)
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
gKey := groupKey(ns, groupID)
|
2023-11-07 10:54:48 +00:00
|
|
|
data := storage.Get(ctx, gKey).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("group not found")
|
|
|
|
}
|
2023-11-09 08:01:17 +00:00
|
|
|
gr := std.Deserialize(data).(Group)
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
storage.Delete(ctx, gKey)
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
it := storage.Find(ctx, groupSubjectPrefix(ns, groupID), storage.KeysOnly)
|
2023-11-07 10:54:48 +00:00
|
|
|
for iterator.Next(it) {
|
|
|
|
gsKey := iterator.Value(it).([]byte)
|
|
|
|
storage.Delete(ctx, gsKey)
|
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
deleteNamespaceGroupName(ctx, ns, gr.Name)
|
|
|
|
|
|
|
|
runtime.Notify("DeleteGroup", ns, groupID)
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkContractOwner(ctx storage.Context) {
|
2023-11-28 08:47:12 +00:00
|
|
|
addr := storage.Get(ctx, adminKey)
|
|
|
|
if addr != nil && runtime.CheckWitness(addr.(interop.Hash160)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if common.HasUpdateAccess() {
|
|
|
|
return
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
panic("not witnessed")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
func removeSubjectFromNamespace(ctx storage.Context, ns string, addr interop.Hash160) {
|
|
|
|
nsSubjKey := namespaceSubjectKey(ns, addr)
|
|
|
|
storage.Delete(ctx, nsSubjKey)
|
|
|
|
|
|
|
|
nsHash := ripemd160Hash(ns)
|
|
|
|
it := storage.Find(ctx, groupPrefixFromHash(nsHash), storage.KeysOnly|storage.RemovePrefix)
|
2021-11-16 14:09:21 +00:00
|
|
|
for iterator.Next(it) {
|
2023-11-07 10:54:48 +00:00
|
|
|
groupHash := iterator.Value(it).([]byte)
|
|
|
|
storage.Delete(ctx, groupSubjectKeyFromHashes(nsHash, groupHash, addr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setNamespaceSubjectName(ctx storage.Context, subj Subject) {
|
|
|
|
if subj.Name == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSubjNameKey := namespaceSubjectNameKey(subj.Namespace, subj.Name)
|
|
|
|
subjKey := storage.Get(ctx, nsSubjNameKey).(interop.PublicKey)
|
|
|
|
if subjKey == nil {
|
|
|
|
storage.Put(ctx, nsSubjNameKey, subj.PrimaryKey)
|
|
|
|
} else if !common.BytesEqual(subjKey, subj.PrimaryKey) {
|
|
|
|
panic("subject name is not available in the current namespace")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteNamespaceSubjectName(ctx storage.Context, ns, subjName string) {
|
|
|
|
if subjName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSubjNameKey := namespaceSubjectNameKey(ns, subjName)
|
|
|
|
storage.Delete(ctx, nsSubjNameKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateNamespaceSubjectName(ctx storage.Context, subj Subject, oldName string) {
|
|
|
|
if subj.Name == oldName {
|
|
|
|
return
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
deleteNamespaceSubjectName(ctx, subj.Namespace, oldName)
|
|
|
|
setNamespaceSubjectName(ctx, subj)
|
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
func updateNamespaceGroupName(ctx storage.Context, gr Group, oldName string) {
|
|
|
|
if gr.Name == oldName {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteNamespaceGroupName(ctx, gr.Namespace, oldName)
|
|
|
|
setNamespaceGroupName(ctx, gr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteNamespaceGroupName(ctx storage.Context, ns, grName string) {
|
|
|
|
if grName == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGroupNameKey := namespaceGroupNameKey(ns, grName)
|
|
|
|
storage.Delete(ctx, nsGroupNameKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setNamespaceGroupName(ctx storage.Context, gr Group) {
|
|
|
|
if gr.Name == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGroupNameKey := namespaceGroupNameKey(gr.Namespace, gr.Name)
|
|
|
|
groupIDRaw := storage.Get(ctx, nsGroupNameKey).([]byte)
|
|
|
|
if groupIDRaw == nil {
|
|
|
|
storage.Put(ctx, nsGroupNameKey, std.Serialize(gr.ID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
groupID := std.Deserialize(groupIDRaw).(int)
|
|
|
|
if groupID != gr.ID {
|
|
|
|
panic("group name is not available in the current namespace")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
func subjectKey(key interop.PublicKey) []byte {
|
|
|
|
addr := contract.CreateStandardAccount(key)
|
|
|
|
return subjectKeyFromAddr(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func subjectKeyFromAddr(addr interop.Hash160) []byte {
|
|
|
|
return append([]byte{subjectKeysPrefix}, addr...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func subjectAdditionalKey(additionalKey interop.PublicKey, primeAddr interop.Hash160) []byte {
|
|
|
|
return append(subjectAdditionalPrefix(additionalKey), primeAddr...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func subjectAdditionalPrefix(additionalKey interop.PublicKey) []byte {
|
|
|
|
addr := contract.CreateStandardAccount(additionalKey)
|
|
|
|
return append([]byte{additionalKeysPrefix}, addr...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func namespaceKey(ns string) []byte {
|
|
|
|
return namespaceKeyFromHash(ripemd160Hash(ns))
|
|
|
|
}
|
2023-11-28 08:46:41 +00:00
|
|
|
|
2023-11-07 10:54:48 +00:00
|
|
|
func namespaceKeyFromHash(ns []byte) []byte {
|
|
|
|
return append([]byte{namespaceKeysPrefix}, ns...)
|
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
func groupKey(ns string, groupID int) []byte {
|
2023-11-07 10:54:48 +00:00
|
|
|
prefix := groupPrefix(ns)
|
2023-11-09 08:01:17 +00:00
|
|
|
return append(prefix, idToBytes(groupID)...)
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
func groupKeyFromHashes(nsHash []byte, groupIDBytes []byte) []byte {
|
2023-11-07 10:54:48 +00:00
|
|
|
prefix := groupPrefixFromHash(nsHash)
|
2023-11-09 08:01:17 +00:00
|
|
|
return append(prefix, groupIDBytes...)
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func groupPrefix(ns string) []byte {
|
|
|
|
return groupPrefixFromHash(ripemd160Hash(ns))
|
|
|
|
}
|
|
|
|
|
|
|
|
func groupPrefixFromHash(nsHash []byte) []byte {
|
|
|
|
return append([]byte{groupKeysPrefix}, nsHash...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ripemd160Hash(data string) []byte {
|
|
|
|
return crypto.Ripemd160([]byte(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func namespaceSubjectKey(ns string, addr interop.Hash160) []byte {
|
|
|
|
prefix := namespaceSubjectPrefix(ns)
|
|
|
|
return append(prefix, addr...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func namespaceSubjectPrefix(ns string) []byte {
|
|
|
|
nsHash := ripemd160Hash(ns)
|
|
|
|
return append([]byte{namespaceSubjectsKeysPrefix}, nsHash...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func namespaceSubjectNameKey(ns, subjName string) []byte {
|
|
|
|
nsHash := ripemd160Hash(ns)
|
|
|
|
nameHash := ripemd160Hash(subjName)
|
|
|
|
return append([]byte{namespaceSubjectsNamesPrefix}, append(nsHash, nameHash...)...)
|
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
func namespaceGroupNameKey(ns, groupName string) []byte {
|
|
|
|
nsHash := ripemd160Hash(ns)
|
|
|
|
nameHash := ripemd160Hash(groupName)
|
|
|
|
return append([]byte{namespaceGroupsNamesPrefix}, append(nsHash, nameHash...)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func groupSubjectKey(ns string, groupID int, addr interop.Hash160) []byte {
|
|
|
|
prefix := groupSubjectPrefix(ns, groupID)
|
2023-11-07 10:54:48 +00:00
|
|
|
return append(prefix, addr...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func groupSubjectKeyFromHashes(nsHash, groupHash []byte, addr interop.Hash160) []byte {
|
|
|
|
prefix := groupSubjectPrefixFromHashes(nsHash, groupHash)
|
|
|
|
return append(prefix, addr...)
|
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
func groupSubjectPrefix(ns string, groupID int) []byte {
|
2023-11-07 10:54:48 +00:00
|
|
|
nsHash := ripemd160Hash(ns)
|
2023-11-09 08:01:17 +00:00
|
|
|
return groupSubjectPrefixFromHashes(nsHash, idToBytes(groupID))
|
2023-11-07 10:54:48 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 08:01:17 +00:00
|
|
|
func groupSubjectPrefixFromHashes(nsHash, groupIDBytes []byte) []byte {
|
|
|
|
return append([]byte{groupSubjectsKeysPrefix}, append(nsHash, groupIDBytes...)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// idToBytes converts i64 value to BE bytes. Panics if value is bigger than i64.
|
|
|
|
func idToBytes(itemID int) []byte {
|
|
|
|
b := convert.ToBytes(itemID)
|
|
|
|
ln := len(b)
|
|
|
|
if ln > 8 {
|
|
|
|
panic("item ID exceeds 8 byte limit")
|
|
|
|
}
|
|
|
|
zeros := make([]byte, 8-ln)
|
|
|
|
return append(b, zeros...)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|