2021-07-04 11:00:56 +00:00
|
|
|
package neofsid
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2021-06-30 10:59:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
2021-11-16 14:09:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
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 (
|
|
|
|
netmapContractKey = "netmapScriptHash"
|
|
|
|
containerContractKey = "containerScriptHash"
|
2021-04-27 10:48:40 +00:00
|
|
|
notaryDisabledKey = "notary"
|
2021-11-16 14:09:21 +00:00
|
|
|
ownerKeysPrefix = 'o'
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
2021-11-16 14:09:21 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-06-03 07:49:07 +00:00
|
|
|
if isUpdate {
|
2021-11-16 14:09:21 +00:00
|
|
|
it := storage.Find(ctx, []byte{}, storage.None)
|
|
|
|
for iterator.Next(it) {
|
|
|
|
kv := iterator.Value(it).([][]byte)
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2021-11-16 14:09:21 +00:00
|
|
|
if len(kv[0]) == 25 {
|
|
|
|
info := std.Deserialize(kv[1]).(UserInfo)
|
|
|
|
key := append([]byte{ownerKeysPrefix}, kv[0]...)
|
|
|
|
for i := range info.Keys {
|
|
|
|
storage.Put(ctx, append(key, info.Keys[i]...), []byte{1})
|
|
|
|
}
|
|
|
|
storage.Delete(ctx, kv[0])
|
|
|
|
}
|
|
|
|
}
|
2021-06-03 07:49:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
args := data.([]interface{})
|
|
|
|
notaryDisabled := args[0].(bool)
|
2021-09-20 15:41:46 +00:00
|
|
|
addrNetmap := args[1].(interop.Hash160)
|
|
|
|
addrContainer := args[2].(interop.Hash160)
|
2021-05-12 08:31:07 +00:00
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(addrNetmap) != 20 || len(addrContainer) != 20 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect length of contract script hash")
|
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-09-21 08:02:24 +00:00
|
|
|
// Update method updates contract source code and manifest. Can be invoked
|
2021-09-20 15:41:46 +00:00
|
|
|
// only by committee.
|
2021-09-21 12:58:37 +00:00
|
|
|
func Update(script []byte, manifest []byte, data interface{}) {
|
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
|
|
|
}
|
|
|
|
|
2021-11-09 10:55:21 +00:00
|
|
|
contract.Call(interop.Hash160(management.Hash), "update",
|
|
|
|
contract.All, script, manifest, common.AppendVersion(data))
|
2021-02-11 15:55:32 +00:00
|
|
|
runtime.Log("neofsid contract updated")
|
|
|
|
}
|
|
|
|
|
2021-07-13 18:08:27 +00:00
|
|
|
// AddKey binds list of provided public keys to OwnerID. Can be invoked only by
|
|
|
|
// Alphabet nodes.
|
|
|
|
//
|
|
|
|
// This method panics if OwnerID is not 25 byte or public key is not 33 byte long.
|
|
|
|
// If key is already bound, ignores it.
|
2021-05-21 11:37:31 +00:00
|
|
|
func AddKey(owner []byte, keys []interop.PublicKey) {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(owner) != 25 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect owner")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("invocation from non inner ring node")
|
2021-04-29 13:17:41 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 14:09:21 +00:00
|
|
|
for i := range keys {
|
|
|
|
if len(keys[i]) != 33 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect public key")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
2021-11-16 14:09:21 +00:00
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-11-16 14:09:21 +00:00
|
|
|
ownerKey := append([]byte{ownerKeysPrefix}, owner...)
|
|
|
|
for i := range keys {
|
|
|
|
stKey := append(ownerKey, keys[i]...)
|
|
|
|
storage.Put(ctx, stKey, []byte{1})
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
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-11-30 09:02:59 +00:00
|
|
|
runtime.Log("key bound to the owner")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:08:27 +00:00
|
|
|
// RemoveKey unbinds provided public keys from OwnerID. Can be invoked only by
|
|
|
|
// Alphabet nodes.
|
|
|
|
//
|
|
|
|
// This method panics if OwnerID is not 25 byte or public key is not 33 byte long.
|
|
|
|
// If key is already unbound, ignores it.
|
2021-05-21 11:37:31 +00:00
|
|
|
func RemoveKey(owner []byte, keys []interop.PublicKey) {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(owner) != 25 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect owner")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("invocation from non inner ring node")
|
2021-04-29 13:17:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 14:09:21 +00:00
|
|
|
for i := range keys {
|
|
|
|
if len(keys[i]) != 33 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect public key")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 14:09:21 +00:00
|
|
|
ownerKey := append([]byte{ownerKeysPrefix}, owner...)
|
|
|
|
for i := range keys {
|
|
|
|
stKey := append(ownerKey, keys[i]...)
|
|
|
|
storage.Delete(ctx, stKey)
|
|
|
|
}
|
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)
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:08:27 +00:00
|
|
|
// Key method returns list of 33-byte public keys bound with OwnerID.
|
|
|
|
//
|
|
|
|
// This method panics if owner is not 25 byte long.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Key(owner []byte) [][]byte {
|
2021-11-29 17:58:27 +00:00
|
|
|
// V2 format
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(owner) != 25 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect owner")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2021-11-16 14:09:21 +00:00
|
|
|
ownerKey := append([]byte{ownerKeysPrefix}, owner...)
|
|
|
|
info := getUserInfo(ctx, ownerKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
return info.Keys
|
|
|
|
}
|
|
|
|
|
2021-07-13 18:08:27 +00:00
|
|
|
// Version returns version of the contract.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Version() int {
|
2021-07-29 11:44:53 +00:00
|
|
|
return common.Version
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getUserInfo(ctx storage.Context, key interface{}) UserInfo {
|
2021-11-16 14:09:21 +00:00
|
|
|
it := storage.Find(ctx, key, storage.KeysOnly|storage.RemovePrefix)
|
|
|
|
pubs := [][]byte{}
|
|
|
|
for iterator.Next(it) {
|
|
|
|
pub := iterator.Value(it).([]byte)
|
|
|
|
pubs = append(pubs, pub)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 14:09:21 +00:00
|
|
|
return UserInfo{Keys: pubs}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
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)
|
|
|
|
}
|