From eb26f9267892ea9c12310586ef251fb44d68fec7 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 19 May 2021 12:21:33 +0300 Subject: [PATCH] [#505] neofsid: Implement wrapper over contract client Implement wrapper over NeoFS ID contact's client which allows to which allows you to conveniently interact with the contract. Implement `AccountKeys` method for getting a list of keys by account ID. Signed-off-by: Leonard Lyubich --- pkg/morph/client/neofsid/wrapper/client.go | 23 +++++++++++++ pkg/morph/client/neofsid/wrapper/keys.go | 38 ++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 pkg/morph/client/neofsid/wrapper/client.go create mode 100644 pkg/morph/client/neofsid/wrapper/keys.go diff --git a/pkg/morph/client/neofsid/wrapper/client.go b/pkg/morph/client/neofsid/wrapper/client.go new file mode 100644 index 000000000..720e51e05 --- /dev/null +++ b/pkg/morph/client/neofsid/wrapper/client.go @@ -0,0 +1,23 @@ +package neofsid + +import ( + "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid" +) + +// ClientWrapper is a wrapper over NeoFS ID contract +// client which provides convenient methods for +// working with a contract. +// +// Working ClientWrapper must be created via Wrap. +type ClientWrapper neofsid.Client + +// Wrap creates, initializes and returns the ClientWrapper instance. +// +// If c is nil, panic occurs. +func Wrap(c *neofsid.Client) *ClientWrapper { + if c == nil { + panic("neofs ID client is nil") + } + + return (*ClientWrapper)(c) +} diff --git a/pkg/morph/client/neofsid/wrapper/keys.go b/pkg/morph/client/neofsid/wrapper/keys.go new file mode 100644 index 000000000..4604e7645 --- /dev/null +++ b/pkg/morph/client/neofsid/wrapper/keys.go @@ -0,0 +1,38 @@ +package neofsid + +import ( + "crypto/elliptic" + "fmt" + + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "github.com/nspcc-dev/neofs-api-go/pkg/owner" + "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid" +) + +// AccountKeys requests public keys of NeoFS account from NeoFS ID contract. +func (x *ClientWrapper) AccountKeys(id *owner.ID) (keys.PublicKeys, error) { + var args neofsid.KeyListingArgs + + args.SetOwnerID(id.ToV2().GetValue()) + + res, err := (*neofsid.Client)(x).AccountKeys(args) + if err != nil { + return nil, err + } + + binKeys := res.Keys() + ks := make(keys.PublicKeys, 0, len(binKeys)) + + curve := elliptic.P256() + + for i := range binKeys { + k, err := keys.NewPublicKeyFromBytes(binKeys[i], curve) + if err != nil { + return nil, fmt.Errorf("received invalid key: %w", err) + } + + ks = append(ks, k) + } + + return ks, nil +}