From 5cab0026c3c4edc3ec7c0fb03cd8f3d2a41e987d Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 31 May 2021 11:55:40 +0300 Subject: [PATCH] [#562] pkg/morph: remove neofs-crypto uses Signed-off-by: Evgenii Stratonikov --- cmd/neofs-node/morph.go | 2 +- pkg/innerring/innerring.go | 2 +- pkg/morph/client/audit/wrapper/result_test.go | 8 ++--- pkg/morph/client/constructor.go | 20 +++-------- pkg/morph/event/neofs/ir_update_test.go | 33 ++++++++----------- pkg/morph/event/netmap/update_peer_test.go | 17 ++++------ 6 files changed, 30 insertions(+), 52 deletions(-) diff --git a/cmd/neofs-node/morph.go b/cmd/neofs-node/morph.go index 59e5b15a4..07adc34b9 100644 --- a/cmd/neofs-node/morph.go +++ b/cmd/neofs-node/morph.go @@ -30,7 +30,7 @@ func initMorphComponents(c *cfg) { }) for i := range addresses { - cli, err := client.New(&c.key.PrivateKey, addresses[i], client.WithDialTimeout(dialTimeout)) + cli, err := client.New(c.key, addresses[i], client.WithDialTimeout(dialTimeout)) if err == nil { c.log.Info("neo RPC connection established", zap.String("endpoint", addresses[i])) diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index aba503589..d26a8e6e2 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -742,7 +742,7 @@ func createListener(ctx context.Context, p *chainParams) (event.Listener, error) func createClient(ctx context.Context, p *chainParams) (*client.Client, error) { return client.New( - &p.key.PrivateKey, + p.key, p.cfg.GetString(p.name+".endpoint.client"), client.WithContext(ctx), client.WithLogger(p.log), diff --git a/pkg/morph/client/audit/wrapper/result_test.go b/pkg/morph/client/audit/wrapper/result_test.go index 343752863..2e54c270b 100644 --- a/pkg/morph/client/audit/wrapper/result_test.go +++ b/pkg/morph/client/audit/wrapper/result_test.go @@ -4,10 +4,10 @@ import ( "testing" "time" + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/util" auditAPI "github.com/nspcc-dev/neofs-api-go/pkg/audit" cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test" - crypto "github.com/nspcc-dev/neofs-crypto" "github.com/nspcc-dev/neofs-node/pkg/morph/client" auditWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit/wrapper" "github.com/stretchr/testify/require" @@ -21,11 +21,9 @@ func TestAuditResults(t *testing.T) { sAuditHash := "cdfb3dab86e6d60e8a143d9e2ecb0b188f3dc2eb" irKeyWIF := "L3o221BojgcCPYgdbXsm6jn7ayTZ72xwREvBHXKknR8VJ3G4WmjB" - key, err := crypto.WIFDecode(irKeyWIF) + key, err := keys.NewPrivateKeyFromWIF(irKeyWIF) require.NoError(t, err) - pubKey := crypto.MarshalPublicKey(&key.PublicKey) - auditHash, err := util.Uint160DecodeStringLE(sAuditHash) require.NoError(t, err) @@ -39,7 +37,7 @@ func TestAuditResults(t *testing.T) { auditRes := auditAPI.NewResult() auditRes.SetAuditEpoch(epoch) - auditRes.SetPublicKey(pubKey) + auditRes.SetPublicKey(key.PublicKey().Bytes()) auditRes.SetContainerID(id) require.NoError(t, auditClientWrapper.PutAuditResult(auditRes)) diff --git a/pkg/morph/client/constructor.go b/pkg/morph/client/constructor.go index 10915a219..e2d89f4ba 100644 --- a/pkg/morph/client/constructor.go +++ b/pkg/morph/client/constructor.go @@ -2,7 +2,6 @@ package client import ( "context" - "crypto/ecdsa" "time" "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" @@ -10,7 +9,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/rpc/client" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/wallet" - crypto "github.com/nspcc-dev/neofs-crypto" "github.com/nspcc-dev/neofs-node/pkg/util/logger" "go.uber.org/zap" ) @@ -47,7 +45,7 @@ func defaultConfig() *cfg { // New creates, initializes and returns the Client instance. // -// If private key is nil, crypto.ErrEmptyPrivateKey is returned. +// If private key is nil, it panics. // // Other values are set according to provided options, or by default: // * client context: Background; @@ -58,22 +56,12 @@ func defaultConfig() *cfg { // If desired option satisfies the default value, it can be omitted. // If multiple options of the same config value are supplied, // the option with the highest index in the arguments will be used. -func New(key *ecdsa.PrivateKey, endpoint string, opts ...Option) (*Client, error) { +func New(key *keys.PrivateKey, endpoint string, opts ...Option) (*Client, error) { if key == nil { - return nil, crypto.ErrEmptyPrivateKey + panic("empty private key") } - privKeyBytes := crypto.MarshalPrivateKey(key) - - wif, err := keys.WIFEncode(privKeyBytes, keys.WIFVersion, true) - if err != nil { - return nil, err - } - - account, err := wallet.NewAccountFromWIF(wif) - if err != nil { - return nil, err - } + account := wallet.NewAccountFromPrivateKey(key) // build default configuration cfg := defaultConfig() diff --git a/pkg/morph/event/neofs/ir_update_test.go b/pkg/morph/event/neofs/ir_update_test.go index 8782b361f..d9e484f76 100644 --- a/pkg/morph/event/neofs/ir_update_test.go +++ b/pkg/morph/event/neofs/ir_update_test.go @@ -1,24 +1,26 @@ package neofs import ( - "crypto/ecdsa" - "crypto/elliptic" "testing" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" - crypto "github.com/nspcc-dev/neofs-crypto" - "github.com/nspcc-dev/neofs-crypto/test" "github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/stretchr/testify/require" ) +func genKey(t *testing.T) *keys.PrivateKey { + priv, err := keys.NewPrivateKey() + require.NoError(t, err) + return priv +} + func TestParseUpdateInnerRing(t *testing.T) { var ( - publicKeys = []*ecdsa.PublicKey{ - &test.DecodeKey(1).PublicKey, - &test.DecodeKey(2).PublicKey, - &test.DecodeKey(3).PublicKey, + publicKeys = []*keys.PublicKey{ + genKey(t).PublicKey(), + genKey(t).PublicKey(), + genKey(t).PublicKey(), } ) @@ -43,22 +45,15 @@ func TestParseUpdateInnerRing(t *testing.T) { t.Run("correct", func(t *testing.T) { ev, err := ParseUpdateInnerRing([]stackitem.Item{ stackitem.NewArray([]stackitem.Item{ - stackitem.NewByteArray(crypto.MarshalPublicKey(publicKeys[0])), - stackitem.NewByteArray(crypto.MarshalPublicKey(publicKeys[1])), - stackitem.NewByteArray(crypto.MarshalPublicKey(publicKeys[2])), + stackitem.NewByteArray(publicKeys[0].Bytes()), + stackitem.NewByteArray(publicKeys[1].Bytes()), + stackitem.NewByteArray(publicKeys[2].Bytes()), }), }) require.NoError(t, err) - expKeys := make([]*keys.PublicKey, len(publicKeys)) - for i := range publicKeys { - expKeys[i], err = keys.NewPublicKeyFromBytes( - crypto.MarshalPublicKey(publicKeys[i]), elliptic.P256()) - require.NoError(t, err) - } - require.Equal(t, UpdateInnerRing{ - keys: expKeys, + keys: publicKeys, }, ev) }) } diff --git a/pkg/morph/event/netmap/update_peer_test.go b/pkg/morph/event/netmap/update_peer_test.go index f4622c644..1da93ea8a 100644 --- a/pkg/morph/event/netmap/update_peer_test.go +++ b/pkg/morph/event/netmap/update_peer_test.go @@ -1,22 +1,22 @@ package netmap import ( - "crypto/elliptic" "math/big" "testing" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-api-go/pkg/netmap" - crypto "github.com/nspcc-dev/neofs-crypto" - "github.com/nspcc-dev/neofs-crypto/test" "github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/stretchr/testify/require" ) func TestParseUpdatePeer(t *testing.T) { + priv, err := keys.NewPrivateKey() + require.NoError(t, err) + var ( - publicKey = &test.DecodeKey(-1).PublicKey + publicKey = priv.PublicKey() state = netmap.NodeStateOffline ) @@ -39,7 +39,7 @@ func TestParseUpdatePeer(t *testing.T) { t.Run("wrong second parameter type", func(t *testing.T) { _, err := ParseUpdatePeer([]stackitem.Item{ - stackitem.NewByteArray(crypto.MarshalPublicKey(publicKey)), + stackitem.NewByteArray(publicKey.Bytes()), stackitem.NewMap(), }) @@ -49,15 +49,12 @@ func TestParseUpdatePeer(t *testing.T) { t.Run("correct behavior", func(t *testing.T) { ev, err := ParseUpdatePeer([]stackitem.Item{ stackitem.NewBigInteger(new(big.Int).SetInt64(int64(state.ToV2()))), - stackitem.NewByteArray(crypto.MarshalPublicKey(publicKey)), + stackitem.NewByteArray(publicKey.Bytes()), }) require.NoError(t, err) - expectedKey, err := keys.NewPublicKeyFromBytes(crypto.MarshalPublicKey(publicKey), elliptic.P256()) - require.NoError(t, err) - require.Equal(t, UpdatePeer{ - publicKey: expectedKey, + publicKey: publicKey, status: state, }, ev) })