2020-07-24 13:54:03 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2022-01-31 13:34:01 +00:00
|
|
|
"crypto/sha256"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2021-11-09 20:52:29 +00:00
|
|
|
|
2022-05-16 13:15:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2022-06-22 10:55:31 +00:00
|
|
|
containercore "github.com/nspcc-dev/neofs-node/pkg/core/container"
|
2021-11-09 20:52:29 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
2022-01-31 13:34:01 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// Put marshals container, and passes it to Wrapper's Put method
|
|
|
|
// along with sig.Key() and sig.Sign().
|
|
|
|
//
|
|
|
|
// Returns error if container is nil.
|
2022-06-22 10:55:31 +00:00
|
|
|
func Put(c *Client, cnr containercore.Container) (*cid.ID, error) {
|
|
|
|
if cnr.Value == nil {
|
2022-01-31 13:34:01 +00:00
|
|
|
return nil, errNilArgument
|
|
|
|
}
|
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
data, err := cnr.Value.Marshal()
|
2022-01-31 13:34:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't marshal container: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
name, zone := container.GetNativeNameWithZone(cnr.Value)
|
2021-05-25 09:25:55 +00:00
|
|
|
|
2022-05-16 13:15:31 +00:00
|
|
|
var prm PutPrm
|
|
|
|
prm.SetContainer(data)
|
|
|
|
prm.SetName(name)
|
|
|
|
prm.SetZone(zone)
|
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
if cnr.Session != nil {
|
|
|
|
prm.SetToken(cnr.Session.Marshal())
|
2022-05-18 15:20:08 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
// TODO(@cthulhu-rider): #1387 implement and use another approach to avoid conversion
|
|
|
|
var sigV2 refs.Signature
|
|
|
|
cnr.Signature.WriteToV2(&sigV2)
|
2022-05-16 13:15:31 +00:00
|
|
|
|
2022-06-22 10:55:31 +00:00
|
|
|
prm.SetKey(sigV2.GetKey())
|
|
|
|
prm.SetSignature(sigV2.GetSign())
|
2022-05-16 13:15:31 +00:00
|
|
|
|
|
|
|
err = c.Put(prm)
|
2022-01-31 13:34:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-12 14:46:45 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
var id cid.ID
|
2022-01-31 13:34:01 +00:00
|
|
|
id.SetSHA256(sha256.Sum256(data))
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
return &id, nil
|
2022-01-31 13:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutPrm groups parameters of Put operation.
|
|
|
|
type PutPrm struct {
|
|
|
|
cnr []byte
|
|
|
|
key []byte
|
|
|
|
sig []byte
|
|
|
|
token []byte
|
|
|
|
name string
|
|
|
|
zone string
|
2021-11-12 15:14:55 +00:00
|
|
|
|
|
|
|
client.InvokePrmOptional
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// SetContainer sets container data.
|
|
|
|
func (p *PutPrm) SetContainer(cnr []byte) {
|
|
|
|
p.cnr = cnr
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// SetKey sets public key.
|
|
|
|
func (p *PutPrm) SetKey(key []byte) {
|
|
|
|
p.key = key
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// SetSignature sets signature.
|
|
|
|
func (p *PutPrm) SetSignature(sig []byte) {
|
|
|
|
p.sig = sig
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// SetToken sets session token.
|
|
|
|
func (p *PutPrm) SetToken(token []byte) {
|
|
|
|
p.token = token
|
2021-05-25 15:34:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// SetName sets native name.
|
|
|
|
func (p *PutPrm) SetName(name string) {
|
|
|
|
p.name = name
|
2021-10-12 14:46:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
// SetZone sets zone.
|
|
|
|
func (p *PutPrm) SetZone(zone string) {
|
|
|
|
p.zone = zone
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put saves binary container with its session token, key and signature
|
|
|
|
// in NeoFS system through Container contract call.
|
|
|
|
//
|
|
|
|
// Returns calculated container identifier and any error
|
|
|
|
// encountered that caused the saving to interrupt.
|
|
|
|
//
|
|
|
|
// If TryNotary is provided, calls notary contract.
|
|
|
|
func (c *Client) Put(p PutPrm) error {
|
|
|
|
if len(p.sig) == 0 || len(p.key) == 0 {
|
|
|
|
return errNilArgument
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:46:45 +00:00
|
|
|
var (
|
|
|
|
method string
|
2021-11-09 20:52:29 +00:00
|
|
|
prm client.InvokePrm
|
2021-05-18 08:12:51 +00:00
|
|
|
)
|
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
if p.name != "" {
|
|
|
|
method = putNamedMethod
|
|
|
|
prm.SetArgs(p.cnr, p.sig, p.key, p.token, p.name, p.zone)
|
2021-11-09 20:52:29 +00:00
|
|
|
} else {
|
2022-01-29 13:06:36 +00:00
|
|
|
method = putMethod
|
2022-01-31 13:34:01 +00:00
|
|
|
prm.SetArgs(p.cnr, p.sig, p.key, p.token)
|
2021-10-12 14:46:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 20:52:29 +00:00
|
|
|
prm.SetMethod(method)
|
2022-01-31 13:34:01 +00:00
|
|
|
prm.InvokePrmOptional = p.InvokePrmOptional
|
2021-11-09 20:52:29 +00:00
|
|
|
|
|
|
|
err := c.client.Invoke(prm)
|
2021-05-18 08:12:51 +00:00
|
|
|
if err != nil {
|
2021-10-12 14:46:45 +00:00
|
|
|
return fmt.Errorf("could not invoke method (%s): %w", method, err)
|
2021-05-18 08:12:51 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|