From 565ad51b4231e0f2f410cb4ad033e562f37f82ac Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 19 May 2021 18:18:27 +0300 Subject: [PATCH] [#505] morph/container: Change put container API Make `Put` method of the wrapper over Container contract's client to accept three binary parameters: container, key and signature. Create `Put` function similar to the previous `Put` variation, but accepting `Signature` structure instead of binary key and signature. Use this function in Container service server in the place where `Put` method was used. Signed-off-by: Leonard Lyubich --- .../client/container/wrapper/container.go | 48 +++++++++++++------ pkg/services/container/morph/executor.go | 2 +- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/pkg/morph/client/container/wrapper/container.go b/pkg/morph/client/container/wrapper/container.go index 5e437455a..ef19904dd 100644 --- a/pkg/morph/client/container/wrapper/container.go +++ b/pkg/morph/client/container/wrapper/container.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + "github.com/nspcc-dev/neofs-api-go/pkg" "github.com/nspcc-dev/neofs-api-go/pkg/container" "github.com/nspcc-dev/neofs-api-go/pkg/owner" v2refs "github.com/nspcc-dev/neofs-api-go/v2/refs" @@ -17,31 +18,48 @@ var ( errUnsupported = errors.New("unsupported structure version") ) -// Put saves passed container structure in NeoFS system -// through Container contract call. +// Put marshals container, and passes it to Wrapper's Put method +// along with sig.Key() and sig.Sign(). // -// Returns calculated container identifier and any error -// encountered that caused the saving to interrupt. -func (w *Wrapper) Put(cnr *container.Container, pubKey, signature []byte) (*container.ID, error) { - if cnr == nil || len(pubKey) == 0 || len(signature) == 0 { +// Returns error if container is nil. +func Put(w *Wrapper, cnr *container.Container, sig *pkg.Signature) (*container.ID, error) { + if cnr == nil { return nil, errNilArgument } - args := client.PutArgs{} - args.SetPublicKey(pubKey) - args.SetSignature(signature) - - id := container.NewID() - data, err := cnr.Marshal() if err != nil { return nil, fmt.Errorf("can't marshal container: %w", err) } - id.SetSHA256(sha256.Sum256(data)) - args.SetContainer(data) + return w.Put(data, sig.Key(), sig.Sign()) +} - return id, w.client.Put(args) +// Put saves binary container with its key and signature +// in NeoFS system through Container contract call. +// +// Returns calculated container identifier and any error +// encountered that caused the saving to interrupt. +func (w *Wrapper) Put(cnr, key, sig []byte) (*container.ID, error) { + if len(sig) == 0 || len(key) == 0 { + return nil, errNilArgument + } + + var args client.PutArgs + + args.SetContainer(cnr) + args.SetSignature(sig) + args.SetPublicKey(key) + + err := w.client.Put(args) + if err != nil { + return nil, err + } + + id := container.NewID() + id.SetSHA256(sha256.Sum256(cnr)) + + return id, nil } // Get reads the container from NeoFS system by identifier diff --git a/pkg/services/container/morph/executor.go b/pkg/services/container/morph/executor.go index 638c77d60..3e4baed33 100644 --- a/pkg/services/container/morph/executor.go +++ b/pkg/services/container/morph/executor.go @@ -32,7 +32,7 @@ func (s *morphExecutor) Put(ctx context.Context, body *container.PutRequestBody) sig := body.GetSignature() - cid, err := s.wrapper.Put(cnr, sig.GetKey(), sig.GetSign()) + cid, err := wrapper.Put(s.wrapper, cnr, pkg.NewSignatureFromV2(sig)) if err != nil { return nil, err }