Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client key for read operations #462

Merged
merged 5 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions audit/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/nspcc-dev/neofs-sdk-go/checksum"
"github.com/nspcc-dev/neofs-sdk-go/client"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
"github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/nspcc-dev/neofs-sdk-go/object/relations"
Expand All @@ -19,7 +20,15 @@ import (
// with information about members collected via HeadReceiver.
//
// Resulting storage group consists of physically stored objects only.
func CollectMembers(ctx context.Context, collector relations.Executor, cnr cid.ID, members []oid.ID, tokens relations.Tokens, calcHomoHash bool) (*storagegroup.StorageGroup, error) {
func CollectMembers(
ctx context.Context,
collector relations.Executor,
cnr cid.ID,
members []oid.ID,
tokens relations.Tokens,
calcHomoHash bool,
signer neofscrypto.Signer,
) (*storagegroup.StorageGroup, error) {
var (
err error
sumPhySize uint64
Expand All @@ -32,14 +41,14 @@ func CollectMembers(ctx context.Context, collector relations.Executor, cnr cid.I
addr.SetContainer(cnr)

for i := range members {
if phyMembers, _, err = relations.Get(ctx, collector, cnr, members[i], tokens); err != nil {
if phyMembers, _, err = relations.Get(ctx, collector, cnr, members[i], tokens, signer); err != nil {
return nil, err
}

var prmHead client.PrmObjectHead
for _, phyMember := range phyMembers {
addr.SetObject(phyMember)
leaf, err := collector.ObjectHead(ctx, addr.Container(), addr.Object(), prmHead)
leaf, err := collector.ObjectHead(ctx, addr.Container(), addr.Object(), signer, prmHead)
if err != nil {
return nil, fmt.Errorf("head phy member '%s': %w", phyMember.EncodeToString(), err)
}
Expand Down
6 changes: 0 additions & 6 deletions client/accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func (x *PrmBalanceGet) SetAccount(id user.ID) {
//
// Return errors:
// - [ErrMissingAccount]
// - [ErrMissingSigner]
func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (accounting.Decimal, error) {
var err error
defer func() {
Expand All @@ -54,11 +53,6 @@ func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (accounting.
return accounting.Decimal{}, err
}

if c.prm.signer == nil {
err = ErrMissingSigner
return accounting.Decimal{}, err
}

// form request body
var accountV2 refs.OwnerID
prm.account.WriteToV2(&accountV2)
Expand Down
11 changes: 1 addition & 10 deletions client/accounting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,17 @@ import (
"context"
"testing"

"github.com/nspcc-dev/neofs-sdk-go/user"
"github.com/stretchr/testify/require"
)

func TestClient_BalanceGet(t *testing.T) {
c := newClient(t, nil, nil)
c := newClient(t, nil)
ctx := context.Background()

t.Run("missing", func(t *testing.T) {
t.Run("account", func(t *testing.T) {
_, err := c.BalanceGet(ctx, PrmBalanceGet{})
require.ErrorIs(t, err, ErrMissingAccount)
})

t.Run("signer", func(t *testing.T) {
var prm PrmBalanceGet
prm.SetAccount(user.ID{})

_, err := c.BalanceGet(ctx, prm)
require.ErrorIs(t, err, ErrMissingSigner)
})
})
}
42 changes: 8 additions & 34 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"fmt"
"time"

"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa"
"github.com/nspcc-dev/neofs-sdk-go/stat"
)

Expand Down Expand Up @@ -51,19 +53,18 @@ type Client struct {
nodeKey []byte
}

var errNonNeoSigner = fmt.Errorf("%w: expected ECDSA_DETERMINISTIC_SHA256 scheme", neofscrypto.ErrIncorrectSigner)

// New creates an instance of Client initialized with the given parameters.
//
// See docs of [PrmInit] methods for details. See also [Client.Dial]/[Client.Close].
//
// Returned errors:
// - [neofscrypto.ErrIncorrectSigner]
func New(prm PrmInit) (*Client, error) {
var c = new(Client)
if prm.signer != nil && prm.signer.Scheme() != neofscrypto.ECDSA_DETERMINISTIC_SHA256 {
return nil, errNonNeoSigner
pk, err := keys.NewPrivateKey()
if err != nil {
return nil, fmt.Errorf("private key: %w", err)
}

prm.signer = neofsecdsa.SignerRFC6979(pk.PrivateKey)
cthulhu-rider marked this conversation as resolved.
Show resolved Hide resolved

c.prm = prm
return c, nil
}
Expand Down Expand Up @@ -139,22 +140,6 @@ func (c *Client) setNeoFSAPIServer(server neoFSAPIServer) {
c.server = server
}

// getSigner returns a signer for requests. Provided signer fromPrm (if any) is prioritized, otherwise
// Client's default is used.
// Returns [ErrMissingSigner] if no signer is provided at all.
func (c *Client) getSigner(fromPrm neofscrypto.Signer) (neofscrypto.Signer, error) {
signer := fromPrm
if signer == nil {
signer = c.prm.signer
}

if signer == nil {
return nil, ErrMissingSigner
}

return signer, nil
}

// Close closes underlying connection to the NeoFS server. Implements io.Closer.
// MUST NOT be called before successful Dial. Can be called concurrently
// with server operations processing on running goroutines: in this case
Expand Down Expand Up @@ -192,17 +177,6 @@ type PrmInit struct {
statisticCallback stat.OperationCallback
}

// SetDefaultSigner sets Client private signer to be used for the protocol
// communication by default.
//
// Optional if you intend to sign every request separately (see Prm* docs), but
// required if you'd like to use this signer for all operations implicitly.
// If specified, MUST be of [neofscrypto.ECDSA_DETERMINISTIC_SHA256] scheme,
// for example, [neofsecdsa.SignerRFC6979] can be used.
func (x *PrmInit) SetDefaultSigner(signer neofscrypto.Signer) {
x.signer = signer
}

// SetResponseInfoCallback makes the Client to pass ResponseMetaInfo from each
// NeoFS server response to f. Nil (default) means ignore response meta info.
func (x *PrmInit) SetResponseInfoCallback(f func(ResponseMetaInfo) error) {
Expand Down
6 changes: 1 addition & 5 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"testing"

apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
"github.com/nspcc-dev/neofs-sdk-go/crypto/test"
"github.com/stretchr/testify/require"
)

Expand All @@ -20,9 +18,8 @@ func init() {
statusErr.SetMessage("test status error")
}

func newClient(t *testing.T, signer neofscrypto.Signer, server neoFSAPIServer) *Client {
func newClient(t *testing.T, server neoFSAPIServer) *Client {
var prm PrmInit
prm.SetDefaultSigner(signer)

c, err := New(prm)
require.NoError(t, err)
Expand All @@ -33,7 +30,6 @@ func newClient(t *testing.T, signer neofscrypto.Signer, server neoFSAPIServer) *

func TestClient_DialContext(t *testing.T) {
var prmInit PrmInit
prmInit.SetDefaultSigner(test.RandomSignerRFC6979(t))

c, err := New(prmInit)
require.NoError(t, err)
Expand Down
Loading
Loading