From 8c2d42368a626712c9f598a029cddf7c2e218e63 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 31 May 2021 20:25:34 +0300 Subject: [PATCH] [#556] morph/neofs: Implement wrapper over contract client Implement wrapper over NeoFS contact's client which allows you to conveniently interact with the contract. Implement `ManageKeys` method for binding or unbinding public keys to the NeoFS account. Signed-off-by: Leonard Lyubich --- pkg/morph/client/neofs/wrapper/bind.go | 35 ++++++++++++++++++++++++ pkg/morph/client/neofs/wrapper/client.go | 27 ++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 pkg/morph/client/neofs/wrapper/bind.go create mode 100644 pkg/morph/client/neofs/wrapper/client.go diff --git a/pkg/morph/client/neofs/wrapper/bind.go b/pkg/morph/client/neofs/wrapper/bind.go new file mode 100644 index 000000000..3d29ab4c2 --- /dev/null +++ b/pkg/morph/client/neofs/wrapper/bind.go @@ -0,0 +1,35 @@ +package neofscontract + +import ( + neofscontract "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs" +) + +// ManageKeys binds/unbinds list of public keys from NeoFS account by script hash. +func (x *ClientWrapper) ManageKeys(scriptHash []byte, ks [][]byte, bind bool) error { + type args interface { + SetScriptHash([]byte) + SetKeys([][]byte) + } + + var ( + a args + call func(args) error + ) + + if bind { + a = new(neofscontract.BindKeysArgs) + call = func(a args) error { + return (*neofscontract.Client)(x).BindKeys(*a.(*neofscontract.BindKeysArgs)) + } + } else { + a = new(neofscontract.UnbindKeysArgs) + call = func(a args) error { + return (*neofscontract.Client)(x).UnbindKeys(*a.(*neofscontract.UnbindKeysArgs)) + } + } + + a.SetScriptHash(scriptHash) + a.SetKeys(ks) + + return call(a) +} diff --git a/pkg/morph/client/neofs/wrapper/client.go b/pkg/morph/client/neofs/wrapper/client.go new file mode 100644 index 000000000..2d647f57f --- /dev/null +++ b/pkg/morph/client/neofs/wrapper/client.go @@ -0,0 +1,27 @@ +package neofscontract + +import ( + "fmt" + + "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neofs-node/pkg/morph/client" + neofscontract "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs" +) + +// ClientWrapper is a wrapper over NeoFS contract +// client which provides convenient methods for +// working with a contract. +// +// Working ClientWrapper must be created via NewFromMorph. +type ClientWrapper neofscontract.Client + +// NewFromMorph wraps client to work with NeoFS contract. +func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8) (*ClientWrapper, error) { + sc, err := client.NewStatic(cli, contract, fee, client.TryNotary()) + if err != nil { + return nil, fmt.Errorf("could not create client of NeoFS contract: %w", err) + } + + return (*ClientWrapper)(neofscontract.New(sc)), nil +}