[#261] pkg/client: Provide signing key in call options

Allow to reuse underlying connection for requests
with different key. If no key is specified the one
provided on client creation is used.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-03-10 15:15:54 +03:00 committed by Leonard Lyubich
parent 74769323be
commit 64505180b4
6 changed files with 49 additions and 27 deletions

View file

@ -1,10 +1,12 @@
package client
import (
"crypto/ecdsa"
"fmt"
"time"
"github.com/nspcc-dev/neofs-api-go/pkg"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-api-go/pkg/token"
v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
v2container "github.com/nspcc-dev/neofs-api-go/v2/container"
@ -29,6 +31,7 @@ type (
xHeaders []*pkg.XHeader
ttl uint32
epoch uint64
key *ecdsa.PrivateKey
session *token.SessionToken
bearer *token.BearerToken
}
@ -69,6 +72,7 @@ func (c Client) defaultCallOptions() callOptions {
return callOptions{
ttl: 2,
version: pkg.SDKVersion(),
key: c.key,
session: c.sessionToken,
bearer: c.bearerToken,
}
@ -100,6 +104,13 @@ func WithTTL(ttl uint32) CallOption {
})
}
// WithKey sets client's key for the next request.
func WithKey(key *ecdsa.PrivateKey) CallOption {
return newFuncCallOption(func(option *callOptions) {
option.key = key
})
}
func WithEpoch(epoch uint64) CallOption {
return newFuncCallOption(func(option *callOptions) {
option.epoch = epoch
@ -178,3 +189,14 @@ func WithDialTimeout(dur time.Duration) Option {
option.dialTimeout = dur
})
}
func newOwnerIDFromKey(key *ecdsa.PublicKey) (*owner.ID, error) {
w, err := owner.NEO3WalletFromPublicKey(key)
if err != nil {
return nil, err
}
ownerID := new(owner.ID)
ownerID.SetNeo3Wallet(w)
return ownerID, nil
}