[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-05-31 20:42:32 +03:00 committed by Alex Vanin
parent 8c2d42368a
commit 4713e6b2b8
2 changed files with 97 additions and 0 deletions

View file

@ -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
}

View file

@ -22,15 +22,21 @@ type Client struct {
type Option func(*cfg) type Option func(*cfg)
type cfg struct { type cfg struct {
addKeysMethod,
removeKeysMethod,
keyListingMethod string keyListingMethod string
} }
const ( const (
defaultKeyListingMethod = "key" // default key listing method name defaultKeyListingMethod = "key" // default key listing method name
defaultAddKeysMethod = "addKey"
defaultRemoveKeysMethod = "removeKey"
) )
func defaultConfig() *cfg { func defaultConfig() *cfg {
return &cfg{ return &cfg{
addKeysMethod: defaultAddKeysMethod,
removeKeysMethod: defaultRemoveKeysMethod,
keyListingMethod: defaultKeyListingMethod, 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
}
}
}