2020-10-27 12:14:06 +00:00
|
|
|
package neofsidcontract
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/binary"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/crypto"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
2021-02-02 16:39:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/common"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
UserInfo struct {
|
|
|
|
Keys [][]byte
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-02-02 17:36:20 +00:00
|
|
|
version = 1
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
netmapContractKey = "netmapScriptHash"
|
|
|
|
containerContractKey = "containerScriptHash"
|
|
|
|
)
|
|
|
|
|
2021-02-11 16:20:38 +00:00
|
|
|
var ctx storage.Context
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
ctx = storage.GetContext()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init(addrNetmap, addrContainer []byte) {
|
|
|
|
if storage.Get(ctx, netmapContractKey) != nil {
|
|
|
|
panic("init: contract already deployed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrNetmap) != 20 || len(addrContainer) != 20 {
|
|
|
|
panic("init: incorrect length of contract script hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, netmapContractKey, addrNetmap)
|
|
|
|
storage.Put(ctx, containerContractKey, addrContainer)
|
|
|
|
|
|
|
|
runtime.Log("neofsid contract initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddKey(owner []byte, keys [][]byte) bool {
|
|
|
|
var (
|
|
|
|
n int // number of votes for inner ring invoke
|
|
|
|
id []byte // ballot key of the inner ring invocation
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(owner) != 25 {
|
|
|
|
panic("addKey: incorrect owner")
|
|
|
|
}
|
|
|
|
|
2021-02-02 19:20:31 +00:00
|
|
|
innerRing := irList()
|
2020-10-27 12:14:06 +00:00
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("addKey: invocation from non inner ring node")
|
|
|
|
}
|
|
|
|
|
|
|
|
info := getUserInfo(ctx, owner)
|
|
|
|
|
|
|
|
addLoop:
|
|
|
|
for i := 0; i < len(keys); i++ {
|
|
|
|
pubKey := keys[i]
|
|
|
|
if len(pubKey) != 33 {
|
|
|
|
panic("addKey: incorrect public key")
|
|
|
|
}
|
|
|
|
|
|
|
|
for j := range info.Keys {
|
|
|
|
key := info.Keys[j]
|
2021-02-02 17:36:20 +00:00
|
|
|
if common.BytesEqual(key, pubKey) {
|
2020-10-27 12:14:06 +00:00
|
|
|
continue addLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Keys = append(info.Keys, pubKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
fromKnownContract := fromKnownContract(runtime.GetCallingScriptHash())
|
|
|
|
if fromKnownContract {
|
|
|
|
n = threshold
|
|
|
|
runtime.Log("addKey: processed indirect invoke")
|
|
|
|
} else {
|
|
|
|
id := invokeIDKeys(owner, keys, []byte("add"))
|
2021-02-02 17:36:20 +00:00
|
|
|
n = common.Vote(ctx, id, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n < threshold {
|
|
|
|
runtime.Log("addKey: processed invoke from inner ring")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
common.SetSerialized(ctx, owner, info)
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log("addKey: key bound to the owner")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveKey(owner []byte, keys [][]byte) bool {
|
|
|
|
if len(owner) != 25 {
|
|
|
|
panic("removeKey: incorrect owner")
|
|
|
|
}
|
|
|
|
|
2021-02-02 19:20:31 +00:00
|
|
|
innerRing := irList()
|
2020-10-27 12:14:06 +00:00
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("removeKey: invocation from non inner ring node")
|
|
|
|
}
|
|
|
|
|
|
|
|
id := invokeIDKeys(owner, keys, []byte("remove"))
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, id, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
if n < threshold {
|
|
|
|
runtime.Log("removeKey: processed invoke from inner ring")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, id)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
info := getUserInfo(ctx, owner)
|
|
|
|
var leftKeys [][]byte
|
|
|
|
|
|
|
|
rmLoop:
|
|
|
|
for i := range info.Keys {
|
|
|
|
key := info.Keys[i]
|
|
|
|
|
|
|
|
for j := 0; j < len(keys); j++ {
|
|
|
|
pubKey := keys[j]
|
|
|
|
if len(pubKey) != 33 {
|
|
|
|
panic("removeKey: incorrect public key")
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
if common.BytesEqual(key, pubKey) {
|
2020-10-27 12:14:06 +00:00
|
|
|
continue rmLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
leftKeys = append(leftKeys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
info.Keys = leftKeys
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, owner, info)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func Key(owner []byte) [][]byte {
|
|
|
|
if len(owner) != 25 {
|
|
|
|
panic("key: incorrect owner")
|
|
|
|
}
|
|
|
|
|
|
|
|
info := getUserInfo(ctx, owner)
|
|
|
|
|
|
|
|
return info.Keys
|
|
|
|
}
|
|
|
|
|
|
|
|
func Version() int {
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUserInfo(ctx storage.Context, key interface{}) UserInfo {
|
|
|
|
data := storage.Get(ctx, key)
|
|
|
|
if data != nil {
|
|
|
|
return binary.Deserialize(data.([]byte)).(UserInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
return UserInfo{Keys: [][]byte{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func invokeIDKeys(owner []byte, keys [][]byte, prefix []byte) []byte {
|
|
|
|
prefix = append(prefix, owner...)
|
|
|
|
for i := range keys {
|
|
|
|
prefix = append(prefix, keys[i]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return crypto.SHA256(prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromKnownContract(caller []byte) bool {
|
|
|
|
containerContractAddr := storage.Get(ctx, containerContractKey).([]byte)
|
2021-02-02 17:36:20 +00:00
|
|
|
if common.BytesEqual(caller, containerContractAddr) {
|
2020-10-27 12:14:06 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2021-02-02 19:20:31 +00:00
|
|
|
|
|
|
|
func irList() []common.IRNode {
|
|
|
|
return common.InnerRingListViaStorage(ctx, netmapContractKey)
|
|
|
|
}
|