2021-11-18 11:31:58 +00:00
|
|
|
package subnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2021-11-21 19:55:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2021-11-18 11:31:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
"github.com/nspcc-dev/neofs-contract/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-11-24 10:33:24 +00:00
|
|
|
// ErrInvalidSubnetID is thrown when subnet id is not a slice of 5 bytes.
|
2021-11-18 12:21:14 +00:00
|
|
|
ErrInvalidSubnetID = "invalid subnet ID"
|
2021-11-24 10:33:24 +00:00
|
|
|
// ErrInvalidGroupID is thrown when group id is not a slice of 5 bytes.
|
|
|
|
ErrInvalidGroupID = "invalid group ID"
|
2021-11-18 12:21:14 +00:00
|
|
|
// ErrInvalidOwner is thrown when owner has invalid format.
|
|
|
|
ErrInvalidOwner = "invalid owner"
|
2021-11-21 19:55:39 +00:00
|
|
|
// ErrInvalidAdmin is thrown when admin has invalid format.
|
|
|
|
ErrInvalidAdmin = "invalid administrator"
|
2021-11-18 12:21:14 +00:00
|
|
|
// ErrAlreadyExists is thrown when id already exists.
|
|
|
|
ErrAlreadyExists = "subnet id already exists"
|
2021-11-23 15:36:45 +00:00
|
|
|
// ErrNotExist is thrown when id doesn't exist.
|
|
|
|
ErrNotExist = "subnet id doesn't exist"
|
2021-11-23 10:56:56 +00:00
|
|
|
// ErrInvalidUser is thrown when user has invalid format.
|
|
|
|
ErrInvalidUser = "invalid user"
|
2021-11-23 09:54:32 +00:00
|
|
|
// ErrInvalidNode is thrown when node has invalid format.
|
|
|
|
ErrInvalidNode = "invalid node key"
|
2021-11-22 14:20:00 +00:00
|
|
|
// ErrNodeAdmNotExist is thrown when node admin is not found.
|
|
|
|
ErrNodeAdmNotExist = "node admin not found"
|
2021-11-23 10:20:07 +00:00
|
|
|
// ErrClientAdmNotExist is thrown when client admin is not found.
|
|
|
|
ErrClientAdmNotExist = "client admin not found"
|
2021-11-22 18:35:07 +00:00
|
|
|
// ErrNodeNotExist is thrown when node is not found.
|
|
|
|
ErrNodeNotExist = "node not found"
|
2021-11-23 11:15:16 +00:00
|
|
|
// ErrUserNotExist is thrown when user is not found.
|
|
|
|
ErrUserNotExist = "user not found"
|
2021-11-22 16:55:07 +00:00
|
|
|
// ErrAccessDenied is thrown when operation is denied for caller.
|
|
|
|
ErrAccessDenied = "access denied"
|
2021-11-24 10:03:02 +00:00
|
|
|
)
|
2021-11-18 12:21:14 +00:00
|
|
|
|
2021-11-24 10:03:02 +00:00
|
|
|
const (
|
2021-11-21 19:55:39 +00:00
|
|
|
nodeAdminPrefix = 'a'
|
2021-11-23 10:56:56 +00:00
|
|
|
infoPrefix = 'i'
|
2021-11-21 19:55:39 +00:00
|
|
|
clientAdminPrefix = 'm'
|
|
|
|
nodePrefix = 'n'
|
2021-11-23 10:56:56 +00:00
|
|
|
ownerPrefix = 'o'
|
|
|
|
userPrefix = 'u'
|
2021-11-18 11:31:58 +00:00
|
|
|
notaryDisabledKey = 'z'
|
|
|
|
)
|
|
|
|
|
2021-11-23 10:56:56 +00:00
|
|
|
const (
|
2021-11-24 17:26:33 +00:00
|
|
|
userIDSize = 27
|
2021-11-24 10:03:02 +00:00
|
|
|
subnetIDSize = 5
|
|
|
|
groupIDSize = 5
|
2021-11-23 10:56:56 +00:00
|
|
|
)
|
|
|
|
|
2021-11-18 11:31:58 +00:00
|
|
|
// _deploy function sets up initial list of inner ring public keys.
|
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
|
|
|
if isUpdate {
|
2022-01-24 08:30:03 +00:00
|
|
|
//args := data.([]interface{})
|
|
|
|
//common.CheckVersion(args[len(args)-1].(int))
|
2021-11-18 11:31:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
args := data.(struct {
|
|
|
|
notaryDisabled bool
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
storage.Put(ctx, []byte{notaryDisabledKey}, args.notaryDisabled)
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:56:21 +00:00
|
|
|
// Update method updates contract source code and manifest. Can be invoked
|
|
|
|
// only by committee.
|
|
|
|
func Update(script []byte, manifest []byte, data interface{}) {
|
|
|
|
if !common.HasUpdateAccess() {
|
|
|
|
panic("only committee can update contract")
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:30:03 +00:00
|
|
|
contract.Call(interop.Hash160(management.Hash), "update", contract.All,
|
|
|
|
script, manifest, common.AppendVersion(data))
|
2021-11-21 14:56:21 +00:00
|
|
|
runtime.Log("subnet contract updated")
|
|
|
|
}
|
|
|
|
|
2021-11-18 12:21:14 +00:00
|
|
|
// Put creates new subnet with the specified owner and info.
|
|
|
|
func Put(id []byte, ownerKey interop.PublicKey, info []byte) {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:03:02 +00:00
|
|
|
if len(id) != subnetIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidSubnetID)
|
2021-11-18 12:21:14 +00:00
|
|
|
}
|
|
|
|
if len(ownerKey) != interop.PublicKeyCompressedLen {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidOwner)
|
2021-11-18 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
stKey := append([]byte{ownerPrefix}, id...)
|
|
|
|
if storage.Get(ctx, stKey) != nil {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrAlreadyExists)
|
2021-11-18 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet := common.AlphabetNodes()
|
|
|
|
nodeKey := common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckWitness(ownerKey)
|
2021-11-24 17:26:33 +00:00
|
|
|
runtime.Notify("Put", id, ownerKey, info)
|
2021-11-18 12:21:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{ownerKey, info}, []byte("put"))
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
} else {
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckOwnerWitness(ownerKey)
|
2021-11-18 12:21:14 +00:00
|
|
|
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
2021-11-18 12:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, stKey, ownerKey)
|
|
|
|
stKey[0] = infoPrefix
|
|
|
|
storage.Put(ctx, stKey, info)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns info about subnet with the specified id.
|
|
|
|
func Get(id []byte) []byte {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(id) != subnetIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidSubnetID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 14:20:00 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2021-11-18 12:21:14 +00:00
|
|
|
key := append([]byte{infoPrefix}, id...)
|
|
|
|
raw := storage.Get(ctx, key)
|
|
|
|
if raw == nil {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrNotExist)
|
2021-11-18 12:21:14 +00:00
|
|
|
}
|
|
|
|
return raw.([]byte)
|
|
|
|
}
|
|
|
|
|
2021-11-18 12:24:32 +00:00
|
|
|
// Delete deletes subnet with the specified id.
|
|
|
|
func Delete(id []byte) {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(id) != subnetIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidSubnetID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 12:24:32 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
key := append([]byte{ownerPrefix}, id...)
|
|
|
|
raw := storage.Get(ctx, key)
|
|
|
|
if raw == nil {
|
2021-11-24 18:09:13 +00:00
|
|
|
return
|
2021-11-18 12:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
owner := raw.([]byte)
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckOwnerWitness(owner)
|
2021-11-18 12:24:32 +00:00
|
|
|
|
|
|
|
storage.Delete(ctx, key)
|
|
|
|
|
|
|
|
key[0] = infoPrefix
|
|
|
|
storage.Delete(ctx, key)
|
2021-11-22 12:19:33 +00:00
|
|
|
|
2021-11-24 18:09:13 +00:00
|
|
|
key[0] = nodeAdminPrefix
|
|
|
|
deleteByPrefix(ctx, key)
|
|
|
|
|
|
|
|
key[0] = nodePrefix
|
|
|
|
deleteByPrefix(ctx, key)
|
|
|
|
|
|
|
|
key[0] = clientAdminPrefix
|
|
|
|
deleteByPrefix(ctx, key)
|
|
|
|
|
|
|
|
key[0] = userPrefix
|
|
|
|
deleteByPrefix(ctx, key)
|
|
|
|
|
2021-11-24 17:26:33 +00:00
|
|
|
runtime.Notify("Delete", id)
|
2021-11-18 12:24:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 19:55:39 +00:00
|
|
|
// AddNodeAdmin adds new node administrator to the specified subnetwork.
|
|
|
|
func AddNodeAdmin(subnetID []byte, adminKey interop.PublicKey) {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(subnetID) != subnetIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidSubnetID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-21 19:55:39 +00:00
|
|
|
if len(adminKey) != interop.PublicKeyCompressedLen {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidAdmin)
|
2021-11-21 19:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrNotExist)
|
2021-11-21 19:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
owner := rawOwner.([]byte)
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckOwnerWitness(owner)
|
2021-11-21 19:55:39 +00:00
|
|
|
|
|
|
|
stKey[0] = nodeAdminPrefix
|
|
|
|
|
2021-11-22 16:55:07 +00:00
|
|
|
if keyInList(ctx, adminKey, stKey) {
|
2021-11-24 18:09:13 +00:00
|
|
|
return
|
2021-11-21 19:55:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
putKeyInList(ctx, adminKey, stKey)
|
2021-11-21 19:55:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 14:20:00 +00:00
|
|
|
// RemoveNodeAdmin removes node administrator from the specified subnetwork.
|
|
|
|
// Must be called by subnet owner only.
|
|
|
|
func RemoveNodeAdmin(subnetID []byte, adminKey interop.PublicKey) {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(subnetID) != subnetIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidSubnetID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 14:20:00 +00:00
|
|
|
if len(adminKey) != interop.PublicKeyCompressedLen {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidAdmin)
|
2021-11-22 14:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
2021-11-22 14:20:00 +00:00
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
2021-11-22 14:20:00 +00:00
|
|
|
if rawOwner == nil {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrNotExist)
|
2021-11-22 14:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
owner := rawOwner.([]byte)
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckOwnerWitness(owner)
|
2021-11-22 14:20:00 +00:00
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
stKey[0] = nodeAdminPrefix
|
2021-11-22 14:20:00 +00:00
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
if !keyInList(ctx, adminKey, stKey) {
|
2021-11-24 18:09:13 +00:00
|
|
|
return
|
2021-11-22 14:20:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
deleteKeyFromList(ctx, adminKey, stKey)
|
2021-11-22 14:20:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 16:55:07 +00:00
|
|
|
// AddNode adds node to the specified subnetwork.
|
|
|
|
// Must be called by subnet's owner or node administrator
|
|
|
|
// only.
|
|
|
|
func AddNode(subnetID []byte, node interop.PublicKey) {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(subnetID) != subnetIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidSubnetID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 16:55:07 +00:00
|
|
|
if len(node) != interop.PublicKeyCompressedLen {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidNode)
|
2021-11-22 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrNotExist)
|
2021-11-22 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
stKey[0] = nodeAdminPrefix
|
2021-11-22 16:55:07 +00:00
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
owner := rawOwner.([]byte)
|
2021-11-22 16:55:07 +00:00
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
if !calledByOwnerOrAdmin(ctx, owner, stKey) {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrAccessDenied)
|
2021-11-22 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = nodePrefix
|
|
|
|
|
|
|
|
if keyInList(ctx, node, stKey) {
|
2021-11-24 18:09:13 +00:00
|
|
|
return
|
2021-11-22 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
putKeyInList(ctx, node, stKey)
|
2021-11-22 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:35:07 +00:00
|
|
|
// RemoveNode removes node from the specified subnetwork.
|
|
|
|
// Must be called by subnet's owner or node administrator
|
|
|
|
// only.
|
|
|
|
func RemoveNode(subnetID []byte, node interop.PublicKey) {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(subnetID) != subnetIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidSubnetID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:35:07 +00:00
|
|
|
if len(node) != interop.PublicKeyCompressedLen {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidNode)
|
2021-11-22 18:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrNotExist)
|
2021-11-22 18:35:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
stKey[0] = nodeAdminPrefix
|
2021-11-22 18:35:07 +00:00
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
owner := rawOwner.([]byte)
|
2021-11-22 18:35:07 +00:00
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
if !calledByOwnerOrAdmin(ctx, owner, stKey) {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrAccessDenied)
|
2021-11-22 18:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = nodePrefix
|
|
|
|
|
|
|
|
if !keyInList(ctx, node, stKey) {
|
2021-11-24 18:09:13 +00:00
|
|
|
return
|
2021-11-22 18:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
storage.Delete(ctx, append(stKey, node...))
|
|
|
|
|
2021-11-24 17:26:33 +00:00
|
|
|
runtime.Notify("RemoveNode", subnetID, node)
|
2021-11-22 18:35:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:59:49 +00:00
|
|
|
// NodeAllowed checks if node is included in the
|
|
|
|
// specified subnet or not.
|
|
|
|
func NodeAllowed(subnetID []byte, node interop.PublicKey) bool {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(subnetID) != subnetIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidSubnetID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 18:59:49 +00:00
|
|
|
if len(node) != interop.PublicKeyCompressedLen {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidNode)
|
2021-11-22 18:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrNotExist)
|
2021-11-22 18:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = nodePrefix
|
|
|
|
|
|
|
|
return storage.Get(ctx, append(stKey, node...)) != nil
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
// AddClientAdmin adds new client administrator of the specified group in the specified subnetwork.
|
|
|
|
// Must be called by owner only.
|
|
|
|
func AddClientAdmin(subnetID []byte, groupID []byte, adminPublicKey interop.PublicKey) {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(subnetID) != subnetIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidSubnetID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(groupID) != groupIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidGroupID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
if len(adminPublicKey) != interop.PublicKeyCompressedLen {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidAdmin)
|
2021-11-23 09:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrNotExist)
|
2021-11-23 09:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
owner := rawOwner.([]byte)
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckOwnerWitness(owner)
|
2021-11-23 09:54:32 +00:00
|
|
|
|
|
|
|
stKey[0] = clientAdminPrefix
|
|
|
|
stKey = append(stKey, groupID...)
|
|
|
|
|
|
|
|
if keyInList(ctx, adminPublicKey, stKey) {
|
2021-11-24 18:09:13 +00:00
|
|
|
return
|
2021-11-23 09:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
putKeyInList(ctx, adminPublicKey, stKey)
|
|
|
|
}
|
|
|
|
|
2021-11-23 10:20:07 +00:00
|
|
|
// RemoveClientAdmin removes client administrator from the
|
|
|
|
// specified group in the specified subnetwork.
|
|
|
|
// Must be called by owner only.
|
|
|
|
func RemoveClientAdmin(subnetID []byte, groupID []byte, adminPublicKey interop.PublicKey) {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(subnetID) != subnetIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidSubnetID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(groupID) != groupIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidGroupID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 10:20:07 +00:00
|
|
|
if len(adminPublicKey) != interop.PublicKeyCompressedLen {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidAdmin)
|
2021-11-23 10:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrNotExist)
|
2021-11-23 10:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
owner := rawOwner.([]byte)
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckOwnerWitness(owner)
|
2021-11-23 10:20:07 +00:00
|
|
|
|
|
|
|
stKey[0] = clientAdminPrefix
|
|
|
|
stKey = append(stKey, groupID...)
|
|
|
|
|
|
|
|
if !keyInList(ctx, adminPublicKey, stKey) {
|
2021-11-24 18:09:13 +00:00
|
|
|
return
|
2021-11-23 10:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteKeyFromList(ctx, adminPublicKey, stKey)
|
|
|
|
}
|
|
|
|
|
2021-11-23 10:56:56 +00:00
|
|
|
// AddUser adds user to the specified subnetwork and group.
|
|
|
|
// Must be called by the owner or the group's admin only.
|
|
|
|
func AddUser(subnetID []byte, groupID []byte, userID []byte) {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(subnetID) != subnetIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidSubnetID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-23 10:56:56 +00:00
|
|
|
if len(userID) != userIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidUser)
|
2021-11-23 10:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(groupID) != groupIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidGroupID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 10:56:56 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrNotExist)
|
2021-11-23 10:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
stKey[0] = clientAdminPrefix
|
2021-11-23 10:56:56 +00:00
|
|
|
stKey = append(stKey, groupID...)
|
|
|
|
|
|
|
|
owner := rawOwner.([]byte)
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
if !calledByOwnerOrAdmin(ctx, owner, stKey) {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrAccessDenied)
|
2021-11-23 10:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = userPrefix
|
|
|
|
|
|
|
|
if keyInList(ctx, userID, stKey) {
|
2021-11-24 18:09:13 +00:00
|
|
|
return
|
2021-11-23 10:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
putKeyInList(ctx, userID, stKey)
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
// RemoveUser removes user from the specified subnetwork and group.
|
|
|
|
// Must be called by the owner or the group's admin only.
|
|
|
|
func RemoveUser(subnetID []byte, groupID []byte, userID []byte) {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(subnetID) != subnetIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidSubnetID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(groupID) != groupIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidGroupID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-23 11:15:16 +00:00
|
|
|
if len(userID) != userIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidUser)
|
2021-11-23 11:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
|
|
|
|
rawOwner := storage.Get(ctx, stKey)
|
|
|
|
if rawOwner == nil {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrNotExist)
|
2021-11-23 11:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = clientAdminPrefix
|
|
|
|
stKey = append(stKey, groupID...)
|
|
|
|
|
|
|
|
owner := rawOwner.([]byte)
|
|
|
|
|
|
|
|
if !calledByOwnerOrAdmin(ctx, owner, stKey) {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrAccessDenied)
|
2021-11-23 11:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = userPrefix
|
|
|
|
|
|
|
|
if !keyInList(ctx, userID, stKey) {
|
2021-11-24 18:09:13 +00:00
|
|
|
return
|
2021-11-23 11:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deleteKeyFromList(ctx, userID, stKey)
|
|
|
|
}
|
|
|
|
|
2021-11-23 13:19:58 +00:00
|
|
|
// UserAllowed returns bool that indicates if node is included in the
|
|
|
|
// specified subnet or not.
|
|
|
|
func UserAllowed(subnetID []byte, user []byte) bool {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-24 10:33:24 +00:00
|
|
|
if len(subnetID) != subnetIDSize {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrInvalidSubnetID)
|
2021-11-24 10:33:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 13:19:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
|
|
|
stKey := append([]byte{ownerPrefix}, subnetID...)
|
|
|
|
if storage.Get(ctx, stKey) == nil {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic(ErrNotExist)
|
2021-11-23 13:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stKey[0] = userPrefix
|
|
|
|
prefixLen := len(stKey) + groupIDSize
|
|
|
|
|
|
|
|
iter := storage.Find(ctx, stKey, storage.KeysOnly)
|
|
|
|
for iterator.Next(iter) {
|
|
|
|
key := iterator.Value(iter).([]byte)
|
|
|
|
if common.BytesEqual(user, key[prefixLen:]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-18 11:31:58 +00:00
|
|
|
// Version returns version of the contract.
|
|
|
|
func Version() int {
|
|
|
|
return common.Version
|
|
|
|
}
|
2021-11-22 16:55:07 +00:00
|
|
|
|
|
|
|
func keyInList(ctx storage.Context, searchedKey interop.PublicKey, prefix []byte) bool {
|
2021-11-23 09:54:32 +00:00
|
|
|
return storage.Get(ctx, append(prefix, searchedKey...)) != nil
|
|
|
|
}
|
2021-11-22 16:55:07 +00:00
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
func putKeyInList(ctx storage.Context, keyToPut interop.PublicKey, prefix []byte) {
|
|
|
|
storage.Put(ctx, append(prefix, keyToPut...), []byte{1})
|
|
|
|
}
|
2021-11-22 16:55:07 +00:00
|
|
|
|
2021-11-23 09:54:32 +00:00
|
|
|
func deleteKeyFromList(ctx storage.Context, keyToDelete interop.PublicKey, prefix []byte) {
|
|
|
|
storage.Delete(ctx, append(prefix, keyToDelete...))
|
2021-11-22 16:55:07 +00:00
|
|
|
}
|
2021-11-23 11:15:16 +00:00
|
|
|
|
2021-11-24 18:09:13 +00:00
|
|
|
func deleteByPrefix(ctx storage.Context, prefix []byte) {
|
|
|
|
iter := storage.Find(ctx, prefix, storage.KeysOnly)
|
|
|
|
for iterator.Next(iter) {
|
|
|
|
k := iterator.Value(iter).([]byte)
|
|
|
|
storage.Delete(ctx, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:15:16 +00:00
|
|
|
func calledByOwnerOrAdmin(ctx storage.Context, owner []byte, adminPrefix []byte) bool {
|
|
|
|
if runtime.CheckWitness(owner) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-12-10 11:17:23 +00:00
|
|
|
iter := storage.Find(ctx, adminPrefix, storage.KeysOnly|storage.RemovePrefix)
|
2021-11-23 11:15:16 +00:00
|
|
|
for iterator.Next(iter) {
|
|
|
|
key := iterator.Value(iter).([]byte)
|
2021-12-10 11:17:23 +00:00
|
|
|
if runtime.CheckWitness(key) {
|
2021-11-23 11:15:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|