From 4c932ae930fb57a4f3d29615155c69f71c4e6dcf Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 20 Apr 2021 17:40:18 +0300 Subject: [PATCH] [#74] processing: Add processing contract Processing contract deployed in main chain and processes multi signature invocations of NeoFS contract by verifying multi signature and paying for it. Signed-off-by: Alex Vanin --- Makefile | 2 +- processing/config.yml | 2 + processing/processing_contract.go | 69 +++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 processing/config.yml create mode 100644 processing/processing_contract.go diff --git a/Makefile b/Makefile index e4fe89e..4e88207 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ sidechain: alphabet morph alphabet_sc = alphabet morph_sc = audit balance container neofsid netmap proxy reputation -mainnet_sc = neofs +mainnet_sc = neofs processing define sc_template $(2)$(1)/$(1)_contract.nef: $(2)$(1)/$(1)_contract.go diff --git a/processing/config.yml b/processing/config.yml new file mode 100644 index 0000000..87bb0ac --- /dev/null +++ b/processing/config.yml @@ -0,0 +1,2 @@ +name: "NeoFS Multi Signature Processing" +safemethods: ["verify", "version"] diff --git a/processing/processing_contract.go b/processing/processing_contract.go new file mode 100644 index 0000000..fe52eb4 --- /dev/null +++ b/processing/processing_contract.go @@ -0,0 +1,69 @@ +package processingcontract + +import ( + "github.com/nspcc-dev/neo-go/pkg/interop" + "github.com/nspcc-dev/neo-go/pkg/interop/contract" + "github.com/nspcc-dev/neo-go/pkg/interop/native/gas" + "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 ( + version = 1 + + neofsContractKey = "neofsScriptHash" + + multiaddrMethod = "alphabetAddress" +) + +func OnNEP17Payment(from interop.Hash160, amount int, data interface{}) { + caller := runtime.GetCallingScriptHash() + if !common.BytesEqual(caller, []byte(gas.Hash)) { + panic("onNEP17Payment: processing contract accepts GAS only") + } +} + +func Init(owner, addrNeoFS interop.Hash160) { + ctx := storage.GetContext() + + if !common.HasUpdateAccess(ctx) { + panic("only owner can reinitialize contract") + } + + if len(addrNeoFS) != 20 { + panic("init: incorrect length of contract script hash") + } + + storage.Put(ctx, common.OwnerKey, owner) + storage.Put(ctx, neofsContractKey, addrNeoFS) + + runtime.Log("processing contract initialized") +} + +func Migrate(script []byte, manifest []byte) bool { + ctx := storage.GetReadOnlyContext() + + if !common.HasUpdateAccess(ctx) { + runtime.Log("only owner can update contract") + return false + } + + management.Update(script, manifest) + runtime.Log("processing contract updated") + + return true +} + +func Verify() bool { + ctx := storage.GetContext() + neofsContractAddr := storage.Get(ctx, neofsContractKey).(interop.Hash160) + multiaddr := contract.Call(neofsContractAddr, multiaddrMethod, contract.ReadOnly).(interop.Hash160) + + return runtime.CheckWitness(multiaddr) +} + +func Version() int { + return version +}