2022-03-28 09:36:04 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2022-06-08 10:13:44 +00:00
|
|
|
"context"
|
2022-03-28 09:36:04 +00:00
|
|
|
"crypto/ecdsa"
|
2022-06-08 10:13:44 +00:00
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
2022-05-18 10:16:04 +00:00
|
|
|
"errors"
|
2022-03-28 09:36:04 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-05-24 08:38:45 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
2022-03-28 09:36:04 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
2022-05-24 08:38:45 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-05-18 10:16:04 +00:00
|
|
|
"github.com/spf13/viper"
|
2022-03-28 09:36:04 +00:00
|
|
|
)
|
|
|
|
|
2022-05-18 10:16:04 +00:00
|
|
|
var errInvalidEndpoint = errors.New("provided RPC endpoint is incorrect")
|
|
|
|
|
|
|
|
// GetSDKClientByFlag returns default neofs-sdk-go client using the specified flag for the address.
|
2022-05-24 08:38:45 +00:00
|
|
|
// On error, outputs to stderr of cmd and exits with non-zero code.
|
|
|
|
func GetSDKClientByFlag(cmd *cobra.Command, key *ecdsa.PrivateKey, endpointFlag string) *client.Client {
|
|
|
|
cli, err := getSDKClientByFlag(key, endpointFlag)
|
|
|
|
if err != nil {
|
|
|
|
common.ExitOnErr(cmd, "can't create API client: %w", err)
|
|
|
|
}
|
|
|
|
return cli
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSDKClientByFlag(key *ecdsa.PrivateKey, endpointFlag string) (*client.Client, error) {
|
2022-05-18 10:16:04 +00:00
|
|
|
var addr network.Address
|
|
|
|
|
|
|
|
err := addr.FromString(viper.GetString(endpointFlag))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %w", errInvalidEndpoint, err)
|
|
|
|
}
|
|
|
|
return GetSDKClient(key, addr)
|
|
|
|
}
|
|
|
|
|
2022-03-28 09:36:04 +00:00
|
|
|
// GetSDKClient returns default neofs-sdk-go client.
|
|
|
|
func GetSDKClient(key *ecdsa.PrivateKey, addr network.Address) (*client.Client, error) {
|
|
|
|
var (
|
|
|
|
c client.Client
|
|
|
|
prmInit client.PrmInit
|
|
|
|
prmDial client.PrmDial
|
|
|
|
)
|
|
|
|
|
|
|
|
prmInit.SetDefaultPrivateKey(*key)
|
|
|
|
prmInit.ResolveNeoFSFailures()
|
2022-05-04 12:34:26 +00:00
|
|
|
prmDial.SetServerURI(addr.URIAddr())
|
2022-03-28 09:36:04 +00:00
|
|
|
|
|
|
|
c.Init(prmInit)
|
|
|
|
|
|
|
|
if err := c.Dial(prmDial); err != nil {
|
|
|
|
return nil, fmt.Errorf("can't init SDK client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &c, nil
|
|
|
|
}
|
2022-06-08 10:13:44 +00:00
|
|
|
|
|
|
|
// GetCurrentEpoch returns current epoch.
|
2022-06-17 07:36:23 +00:00
|
|
|
func GetCurrentEpoch(ctx context.Context, endpoint string) (uint64, error) {
|
2022-06-08 10:13:44 +00:00
|
|
|
var addr network.Address
|
|
|
|
|
|
|
|
if err := addr.FromString(endpoint); err != nil {
|
|
|
|
return 0, fmt.Errorf("can't parse RPC endpoint: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("can't generate key to sign query: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := GetSDKClient(key, addr)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ni, err := c.NetworkInfo(ctx, client.PrmNetworkInfo{})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ni.Info().CurrentEpoch(), nil
|
|
|
|
}
|