2022-03-28 10:23:45 +00:00
|
|
|
package key
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2022-05-18 10:47:25 +00:00
|
|
|
"errors"
|
2022-03-28 10:23:45 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2022-05-24 08:22:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
2022-05-18 10:38:45 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
|
2022-05-24 08:22:23 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-05-18 10:38:45 +00:00
|
|
|
"github.com/spf13/viper"
|
2022-03-28 10:23:45 +00:00
|
|
|
)
|
|
|
|
|
2022-05-18 10:47:25 +00:00
|
|
|
var errCantGenerateKey = errors.New("can't generate new private key")
|
|
|
|
|
2022-05-18 10:38:45 +00:00
|
|
|
// Get returns private key from the following sources:
|
2022-03-28 10:23:45 +00:00
|
|
|
// 1. WIF
|
|
|
|
// 2. Raw binary key
|
|
|
|
// 3. Wallet file
|
|
|
|
// 4. NEP-2 encrypted WIF.
|
|
|
|
// Ideally we want to touch file-system on the last step.
|
|
|
|
// However, asking for NEP-2 password seems to be confusing if we provide a wallet.
|
2022-05-18 10:38:45 +00:00
|
|
|
// This function assumes that all flags were bind to viper in a `PersistentPreRun`.
|
2022-05-24 08:22:23 +00:00
|
|
|
func Get(cmd *cobra.Command) *ecdsa.PrivateKey {
|
|
|
|
pk, err := get()
|
|
|
|
common.ExitOnErr(cmd, "can't fetch private key: %w", err)
|
|
|
|
return pk
|
|
|
|
}
|
|
|
|
|
|
|
|
func get() (*ecdsa.PrivateKey, error) {
|
2022-05-18 10:38:45 +00:00
|
|
|
keyDesc := viper.GetString(commonflags.WalletPath)
|
2022-03-28 10:23:45 +00:00
|
|
|
priv, err := keys.NewPrivateKeyFromWIF(keyDesc)
|
|
|
|
if err == nil {
|
|
|
|
return &priv.PrivateKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := getKeyFromFile(keyDesc)
|
|
|
|
if err == nil {
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := wallet.NewWalletFromFile(keyDesc)
|
|
|
|
if err == nil {
|
2022-05-18 10:38:45 +00:00
|
|
|
return FromWallet(w, viper.GetString(commonflags.Account))
|
2022-03-28 10:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(keyDesc) == nep2Base58Length {
|
|
|
|
return FromNEP2(keyDesc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrInvalidKey
|
|
|
|
}
|
|
|
|
|
2022-05-18 10:47:25 +00:00
|
|
|
// GetOrGenerate is similar to get but generates a new key if commonflags.GenerateKey is set.
|
2022-05-24 08:22:23 +00:00
|
|
|
func GetOrGenerate(cmd *cobra.Command) *ecdsa.PrivateKey {
|
|
|
|
pk, err := getOrGenerate()
|
|
|
|
common.ExitOnErr(cmd, "can't fetch private key: %w", err)
|
|
|
|
return pk
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOrGenerate() (*ecdsa.PrivateKey, error) {
|
2022-05-18 10:47:25 +00:00
|
|
|
if viper.GetBool(commonflags.GenerateKey) {
|
|
|
|
priv, err := keys.NewPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", errCantGenerateKey, err)
|
|
|
|
}
|
|
|
|
return &priv.PrivateKey, nil
|
|
|
|
}
|
2022-05-24 08:22:23 +00:00
|
|
|
return get()
|
2022-05-18 10:47:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 10:23:45 +00:00
|
|
|
func getKeyFromFile(keyPath string) (*ecdsa.PrivateKey, error) {
|
|
|
|
data, err := os.ReadFile(keyPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", ErrInvalidKey, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
priv, err := keys.NewPrivateKeyFromBytes(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %v", ErrInvalidKey, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &priv.PrivateKey, nil
|
|
|
|
}
|