2019-10-01 00:33:00 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-03-17 18:47:23 +00:00
|
|
|
package operator
|
2019-10-01 00:33:00 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2021-01-18 14:33:13 +00:00
|
|
|
"strings"
|
2019-10-01 00:33:00 +01:00
|
|
|
|
2021-01-18 14:33:13 +00:00
|
|
|
"github.com/spf13/pflag"
|
2019-10-01 00:33:00 +01:00
|
|
|
"go.uber.org/zap"
|
2021-01-18 14:33:13 +00:00
|
|
|
|
|
|
|
"storj.io/storj/private/nodeoperator"
|
2019-10-01 00:33:00 +01:00
|
|
|
)
|
|
|
|
|
2021-03-17 18:47:23 +00:00
|
|
|
// Config defines properties related to storage node operator metadata.
|
|
|
|
type Config struct {
|
2021-01-18 14:33:13 +00:00
|
|
|
Email string `user:"true" help:"operator email address" default:""`
|
|
|
|
Wallet string `user:"true" help:"operator wallet address" default:""`
|
|
|
|
WalletFeatures WalletFeatures `user:"true" help:"operator wallet features" default:""`
|
2019-10-01 00:33:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify verifies whether operator config is valid.
|
2021-03-17 18:47:23 +00:00
|
|
|
func (c Config) Verify(log *zap.Logger) error {
|
2019-10-01 00:33:00 +01:00
|
|
|
if err := isOperatorEmailValid(log, c.Email); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := isOperatorWalletValid(log, c.Wallet); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-18 14:33:13 +00:00
|
|
|
if err := isOperatorWalletFeaturesValid(log, c.WalletFeatures); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-01 00:33:00 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isOperatorEmailValid(log *zap.Logger, email string) error {
|
|
|
|
if email == "" {
|
2020-04-13 10:31:17 +01:00
|
|
|
log.Warn("Operator email address isn't specified.")
|
2019-10-01 00:33:00 +01:00
|
|
|
} else {
|
2020-04-13 10:31:17 +01:00
|
|
|
log.Info("Operator email", zap.String("Address", email))
|
2019-10-01 00:33:00 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isOperatorWalletValid(log *zap.Logger, wallet string) error {
|
|
|
|
if wallet == "" {
|
|
|
|
return fmt.Errorf("operator wallet address isn't specified")
|
|
|
|
}
|
|
|
|
r := regexp.MustCompile("^0x[a-fA-F0-9]{40}$")
|
|
|
|
if match := r.MatchString(wallet); !match {
|
|
|
|
return fmt.Errorf("operator wallet address isn't valid")
|
|
|
|
}
|
|
|
|
|
2020-04-13 10:31:17 +01:00
|
|
|
log.Info("Operator wallet", zap.String("Address", wallet))
|
2019-10-01 00:33:00 +01:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-18 14:33:13 +00:00
|
|
|
|
|
|
|
// isOperatorWalletFeaturesValid checks if wallet features list does not exceed length limits.
|
|
|
|
func isOperatorWalletFeaturesValid(log *zap.Logger, features WalletFeatures) error {
|
|
|
|
return nodeoperator.DefaultWalletFeaturesValidation.Validate(features)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure WalletFeatures implements pflag.Value.
|
|
|
|
var _ pflag.Value = (*WalletFeatures)(nil)
|
|
|
|
|
|
|
|
// WalletFeatures payout opt-in wallet features list.
|
|
|
|
type WalletFeatures []string
|
|
|
|
|
|
|
|
// String returns the comma separated list of wallet features.
|
|
|
|
func (features WalletFeatures) String() string {
|
|
|
|
return strings.Join(features, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set implements pflag.Value by parsing a comma separated list of wallet features.
|
|
|
|
func (features *WalletFeatures) Set(value string) error {
|
|
|
|
if value != "" {
|
|
|
|
*features = strings.Split(value, ",")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type of the pflag.Value.
|
|
|
|
func (features WalletFeatures) Type() string {
|
|
|
|
return "wallet-features"
|
|
|
|
}
|