2020-10-27 12:14:06 +00:00
|
|
|
package neofsidcontract
|
|
|
|
|
|
|
|
import (
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2021-04-29 13:17:41 +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"
|
2021-03-12 12:16:36 +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"
|
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-04-27 10:48:40 +00:00
|
|
|
notaryDisabledKey = "notary"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
|
|
|
args := data.([]interface{})
|
|
|
|
notaryDisabled := args[0].(bool)
|
|
|
|
owner := args[1].(interop.Hash160)
|
|
|
|
addrNetmap := args[2].(interop.Hash160)
|
|
|
|
addrContainer := args[3].(interop.Hash160)
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
if !common.HasUpdateAccess(ctx) {
|
|
|
|
panic("only owner can reinitialize contract")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrNetmap) != 20 || len(addrContainer) != 20 {
|
|
|
|
panic("init: incorrect length of contract script hash")
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
storage.Put(ctx, common.OwnerKey, owner)
|
2020-10-27 12:14:06 +00:00
|
|
|
storage.Put(ctx, netmapContractKey, addrNetmap)
|
|
|
|
storage.Put(ctx, containerContractKey, addrContainer)
|
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
// initialize the way to collect signatures
|
|
|
|
storage.Put(ctx, notaryDisabledKey, notaryDisabled)
|
|
|
|
if notaryDisabled {
|
|
|
|
common.InitVote(ctx)
|
2021-05-04 14:02:05 +00:00
|
|
|
runtime.Log("neofsid contract notary disabled")
|
2021-04-27 10:48:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log("neofsid contract initialized")
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
func Migrate(script []byte, manifest []byte) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
if !common.HasUpdateAccess(ctx) {
|
|
|
|
runtime.Log("only owner can update contract")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
management.Update(script, manifest)
|
|
|
|
runtime.Log("neofsid contract updated")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-05-21 11:37:31 +00:00
|
|
|
func AddKey(owner []byte, keys []interop.PublicKey) {
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(owner) != 25 {
|
|
|
|
panic("addKey: incorrect owner")
|
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:17:41 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-04-29 13:17:41 +00:00
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
2021-05-05 13:30:30 +00:00
|
|
|
indirectCall bool
|
2021-04-29 13:17:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
panic("addKey: invocation from non inner ring node")
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:30:30 +00:00
|
|
|
indirectCall = common.FromKnownContract(
|
2021-04-29 13:17:41 +00:00
|
|
|
ctx,
|
|
|
|
runtime.GetCallingScriptHash(),
|
|
|
|
containerContractKey,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("addKey: invocation from non inner ring node")
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:30:30 +00:00
|
|
|
if notaryDisabled && !indirectCall {
|
2021-04-29 13:17:41 +00:00
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := invokeIDKeys(owner, keys, []byte("add"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, owner, info)
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log("addKey: key bound to the owner")
|
|
|
|
}
|
|
|
|
|
2021-05-21 11:37:31 +00:00
|
|
|
func RemoveKey(owner []byte, keys []interop.PublicKey) {
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(owner) != 25 {
|
|
|
|
panic("removeKey: incorrect owner")
|
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:17:41 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-04-29 13:17:41 +00:00
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
panic("removeKey: invocation from non inner ring node")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("removeKey: invocation from non inner ring node")
|
|
|
|
}
|
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-04-29 13:17:41 +00:00
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := invokeIDKeys(owner, keys, []byte("remove"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, owner, info)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Key(owner []byte) [][]byte {
|
|
|
|
if len(owner) != 25 {
|
|
|
|
panic("key: incorrect owner")
|
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
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 {
|
2021-03-12 12:16:36 +00:00
|
|
|
return std.Deserialize(data.([]byte)).(UserInfo)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return UserInfo{Keys: [][]byte{}}
|
|
|
|
}
|
2021-04-29 13:17:41 +00:00
|
|
|
|
|
|
|
func invokeIDKeys(owner []byte, keys []interop.PublicKey, prefix []byte) []byte {
|
|
|
|
prefix = append(prefix, owner...)
|
|
|
|
for i := range keys {
|
|
|
|
prefix = append(prefix, keys[i]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return crypto.Sha256(prefix)
|
|
|
|
}
|