[#562] config: parse key on config load

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-06-03 18:24:09 +03:00 committed by Alex Vanin
parent 9142c778c7
commit 2f020a500d
6 changed files with 20 additions and 8 deletions

View file

@ -3,6 +3,7 @@ package nodeconfig
import (
"errors"
"fmt"
"io/ioutil"
"strconv"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
@ -26,16 +27,26 @@ var (
// from "node" section.
//
// Panics if value is not a non-empty string.
func Key(c *config.Config) string {
func Key(c *config.Config) *keys.PrivateKey {
v := config.StringSafe(c.Sub(subsection), "key")
if v == "" {
panic(errKeyNotSet)
}
// TODO: add string -> `ecdsa.PrivateKey` parsing logic
// after https://github.com/nspcc-dev/neofs-node/pull/569.
var (
key *keys.PrivateKey
err error
data []byte
)
if data, err = ioutil.ReadFile(v); err == nil {
key, err = keys.NewPrivateKeyFromBytes(data)
}
return v
if err != nil {
panic(fmt.Errorf("can't read key: %w", err))
}
return key
}
// Wallet returns value of node private key from "node" section.

View file

@ -49,7 +49,7 @@ func TestNodeSection(t *testing.T) {
expectedAddr, err := network.AddressFromString("s01.neofs.devenv:8080")
require.NoError(t, err)
require.Equal(t, "path/hex/WIF", key)
require.Equal(t, "NbUgTSFvPmsRxmGeWpuuGeJUoRoi6PErcM", key.Address())
require.Equal(t, true, addr.Equal(expectedAddr))
require.Equal(t, true, relay)

View file

@ -0,0 +1 @@
с╨─ЧдФ╕Я?вьэЁ╡²Оv─/l⌠║еы

View file

@ -7,7 +7,7 @@ NEOFS_METRICS_ADDRESS=127.0.0.1:9090
NEOFS_METRICS_SHUTDOWN_TIMEOUT=15s
# Node section
NEOFS_NODE_KEY=path/hex/WIF
NEOFS_NODE_KEY=./wallet.key
NEOFS_NODE_WALLET_PATH=./wallet.json
NEOFS_NODE_WALLET_ADDRESS=NcpJzXcSDrh5CCizf4K9Ro6w4t59J5LKzz
NEOFS_NODE_WALLET_PASSWORD=password

View file

@ -11,7 +11,7 @@
"shutdown_timeout": "15s"
},
"node": {
"key": "path/hex/WIF",
"key": "./wallet.key",
"wallet": {
"path": "./wallet.json",
"address": "NcpJzXcSDrh5CCizf4K9Ro6w4t59J5LKzz",

View file

@ -10,7 +10,7 @@ metrics:
shutdown_timeout: 15s
node:
key: path/hex/WIF
key: ./wallet.key
wallet:
path: "./wallet.json"
address: "NcpJzXcSDrh5CCizf4K9Ro6w4t59J5LKzz"