From 4713e6b2b8da76f1817223cd94fe9ef257e66d55 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 31 May 2021 20:42:32 +0300 Subject: [PATCH] [#556] morph/neofsid: Add Client methods to add/remove keys Implement `AddKeys` / `RemoveKeys` methods to call `addKey` / `removeKey` methods of NeoFS ID contract. Signed-off-by: Leonard Lyubich --- pkg/morph/client/neofsid/addrm_keys.go | 63 ++++++++++++++++++++++++++ pkg/morph/client/neofsid/client.go | 34 ++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 pkg/morph/client/neofsid/addrm_keys.go diff --git a/pkg/morph/client/neofsid/addrm_keys.go b/pkg/morph/client/neofsid/addrm_keys.go new file mode 100644 index 000000000..a7d7e3ef8 --- /dev/null +++ b/pkg/morph/client/neofsid/addrm_keys.go @@ -0,0 +1,63 @@ +package neofsid + +import ( + "fmt" +) + +// AddKeysArgs groups the arguments +// of key binding call. +type AddKeysArgs struct { + commonBindArgs +} + +// RemoveKeysArgs groups the arguments +// of key unbinding call. +type RemoveKeysArgs struct { + commonBindArgs +} + +type commonBindArgs struct { + ownerID []byte // NeoFS account identifier + + keys [][]byte // list of serialized public keys +} + +// SetOwnerID sets NeoFS account identifier. +func (x *commonBindArgs) SetOwnerID(v []byte) { + x.ownerID = v +} + +// SetKeys sets list of public keys in a binary format. +func (x *commonBindArgs) SetKeys(v [][]byte) { + x.keys = v +} + +// AddKeys invokes the call of key adding method +// of NeoFS contract. +func (x *Client) AddKeys(args AddKeysArgs) error { + err := x.client.Invoke( + x.addKeysMethod, + args.ownerID, + args.keys, + ) + if err != nil { + return fmt.Errorf("could not invoke method (%s): %w", x.addKeysMethod, err) + } + + return nil +} + +// RemoveKeys invokes the call of key removing method +// of NeoFS contract. +func (x *Client) RemoveKeys(args RemoveKeysArgs) error { + err := x.client.Invoke( + x.removeKeysMethod, + args.ownerID, + args.keys, + ) + if err != nil { + return fmt.Errorf("could not invoke method (%s): %w", x.removeKeysMethod, err) + } + + return nil +} diff --git a/pkg/morph/client/neofsid/client.go b/pkg/morph/client/neofsid/client.go index 06008d224..2e6c97525 100644 --- a/pkg/morph/client/neofsid/client.go +++ b/pkg/morph/client/neofsid/client.go @@ -22,15 +22,21 @@ type Client struct { type Option func(*cfg) type cfg struct { + addKeysMethod, + removeKeysMethod, keyListingMethod string } const ( defaultKeyListingMethod = "key" // default key listing method name + defaultAddKeysMethod = "addKey" + defaultRemoveKeysMethod = "removeKey" ) func defaultConfig() *cfg { return &cfg{ + addKeysMethod: defaultAddKeysMethod, + removeKeysMethod: defaultRemoveKeysMethod, keyListingMethod: defaultKeyListingMethod, } } @@ -76,3 +82,31 @@ func WithKeyListingMethod(n string) Option { } } } + +// WithAddKeysMethod returns a client constructor option that +// specifies the method name of adding key operation. +// +// Ignores empty value. +// +// If option not provided, "addKey" is used. +func WithAddKeysMethod(n string) Option { + return func(c *cfg) { + if n != "" { + c.addKeysMethod = n + } + } +} + +// WithRemoveKeysMethod returns a client constructor option that +// specifies the method name of removing key operation. +// +// Ignores empty value. +// +// If option not provided, "removeKey" is used. +func WithRemoveKeysMethod(n string) Option { + return func(c *cfg) { + if n != "" { + c.removeKeysMethod = n + } + } +}