2023-10-19 16:33:02 +00:00
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/common"
|
2023-11-20 13:10:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2023-10-19 16:33:02 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2023-11-20 13:10:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
2023-10-19 16:33:02 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Kind represents the object the chain is attached to.
|
|
|
|
// Currently only namespace and container are supported.
|
|
|
|
type Kind byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
Namespace = 'n'
|
|
|
|
Container = 'c'
|
|
|
|
IAM = 'i'
|
|
|
|
)
|
|
|
|
|
2023-11-20 13:10:52 +00:00
|
|
|
const (
|
|
|
|
ownerKeyPrefix = 'o'
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ErrNotAutorized is returned when the none of the transaction signers
|
|
|
|
// belongs to the list of autorized keys.
|
|
|
|
ErrNotAutorized = "none of the signers is not autorized to change the contract"
|
|
|
|
)
|
|
|
|
|
2023-10-19 16:33:02 +00:00
|
|
|
// _deploy function sets up initial list of inner ring public keys.
|
2023-11-07 12:18:48 +00:00
|
|
|
func _deploy(data any, isUpdate bool) {
|
2023-11-20 13:10:52 +00:00
|
|
|
if isUpdate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
args := data.(struct {
|
|
|
|
Admin interop.Hash160
|
|
|
|
})
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
if args.Admin != nil {
|
|
|
|
if len(args.Admin) != 20 {
|
|
|
|
panic("invaliad admin hash length")
|
|
|
|
}
|
2023-11-28 08:50:33 +00:00
|
|
|
storage.Put(ctx, []byte{ownerKeyPrefix}, args.Admin)
|
2023-11-20 13:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkAuthorization(ctx storage.Context) {
|
|
|
|
if runtime.CheckWitness(common.AlphabetAddress()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
admin := getAdmin(ctx)
|
|
|
|
if admin != nil && runtime.CheckWitness(admin) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(ErrNotAutorized)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetAdmin(addr interop.Hash160) {
|
|
|
|
common.CheckAlphabetWitness()
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
storage.Put(ctx, []byte{ownerKeyPrefix}, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAdmin() interop.Hash160 {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
return getAdmin(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAdmin(ctx storage.Context) interop.Hash160 {
|
|
|
|
return storage.Get(ctx, []byte{ownerKeyPrefix}).(interop.Hash160)
|
2023-10-19 16:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func storageKey(prefix Kind, entityName, name string) []byte {
|
|
|
|
ln := len(entityName)
|
|
|
|
key := append([]byte{byte(prefix)}, byte(ln&0xFF), byte(ln>>8))
|
|
|
|
key = append(key, entityName...)
|
|
|
|
return append(key, name...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddChain(entity Kind, entityName, name string, chain []byte) {
|
|
|
|
ctx := storage.GetContext()
|
2023-11-20 13:10:52 +00:00
|
|
|
checkAuthorization(ctx)
|
|
|
|
|
2023-10-19 16:33:02 +00:00
|
|
|
key := storageKey(entity, entityName, name)
|
|
|
|
storage.Put(ctx, key, chain)
|
|
|
|
}
|
|
|
|
|
2023-11-17 08:06:08 +00:00
|
|
|
func GetChain(entity Kind, entityName, name string) []byte {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
key := storageKey(entity, entityName, name)
|
|
|
|
data := storage.Get(ctx, key).([]byte)
|
|
|
|
if data == nil {
|
|
|
|
panic("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2023-10-19 16:33:02 +00:00
|
|
|
func RemoveChain(entity Kind, entityName string, name string) {
|
|
|
|
ctx := storage.GetContext()
|
2023-11-20 13:10:52 +00:00
|
|
|
checkAuthorization(ctx)
|
|
|
|
|
2023-10-19 16:33:02 +00:00
|
|
|
key := storageKey(entity, entityName, name)
|
|
|
|
storage.Delete(ctx, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveChainsByPrefix(entity Kind, entityName string, name string) {
|
|
|
|
ctx := storage.GetContext()
|
2023-11-20 13:10:52 +00:00
|
|
|
checkAuthorization(ctx)
|
|
|
|
|
2023-10-19 16:33:02 +00:00
|
|
|
key := storageKey(entity, entityName, name)
|
|
|
|
it := storage.Find(ctx, key, storage.KeysOnly)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
storage.Delete(ctx, iterator.Value(it).([]byte))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListChains lists all chains for the namespace by prefix.
|
|
|
|
// container may be empty.
|
|
|
|
func ListChains(namespace, container, name string) [][]byte {
|
2023-11-17 12:02:13 +00:00
|
|
|
result := ListChainsByPrefix(Namespace, namespace, name)
|
2023-10-19 16:33:02 +00:00
|
|
|
|
|
|
|
if container != "" {
|
2023-11-17 12:02:13 +00:00
|
|
|
result = append(result, ListChainsByPrefix(Container, container, name)...)
|
2023-10-19 16:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2023-11-17 08:06:08 +00:00
|
|
|
|
|
|
|
// ListChainsByPrefix list all chains for the provided kind and entity by prefix.
|
|
|
|
func ListChainsByPrefix(entity Kind, entityName, prefix string) [][]byte {
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
|
|
|
result := [][]byte{}
|
|
|
|
|
|
|
|
keyPrefix := storageKey(entity, entityName, prefix)
|
|
|
|
it := storage.Find(ctx, keyPrefix, storage.ValuesOnly)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
result = append(result, iterator.Value(it).([]byte))
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|