diff --git a/accounts/event/feed_test.go b/accounts/event/feed_test.go index 1c4e2bc078..f6a8ebc43f 100644 --- a/accounts/event/feed_test.go +++ b/accounts/event/feed_test.go @@ -66,7 +66,7 @@ func checkPanic(want error, fn func()) (err error) { if panicErr == nil { err = errors.New("didn't panic") } else if !reflect.DeepEqual(panicErr, want) { - err = fmt.Errorf("panicked with wrong error: got %w, want %w", panicErr, want) + err = fmt.Errorf("panicked with wrong error: got %v, want %w", panicErr, want) } }() diff --git a/accounts/keystore/account_cache_test.go b/accounts/keystore/account_cache_test.go index 334bf5f0b6..bbccae0dfd 100644 --- a/accounts/keystore/account_cache_test.go +++ b/accounts/keystore/account_cache_test.go @@ -350,7 +350,7 @@ func TestUpdatedKeyfileContents(t *testing.T) { t.Parallel() // Create a temporary keystore to test with - dir, err := filepath.Abs(filepath.Join(fmt.Sprintf("eth-keystore-updatedkeyfilecontents-test-%d-%d", os.Getpid(), rand.Int()))) + dir, err := filepath.Abs(filepath.Join(fmt.Sprintf("eth-keystore-updatedkeyfilecontents-test-%d-%d", os.Getpid(), rand.Int()))) //nolint:gocritic require.NoError(t, err) ks := NewKeyStore(dir, LightScryptN, LightScryptP, hclog.NewNullLogger()) diff --git a/accounts/keystore/key.go b/accounts/keystore/key.go index 33813de308..7eb03dfd07 100644 --- a/accounts/keystore/key.go +++ b/accounts/keystore/key.go @@ -76,7 +76,7 @@ type cipherparamsJSON struct { // TO DO marshall private key func (k *Key) MarshalJSON() (j []byte, err error) { - privKey, err := crypto.MarshalECDSAPrivateKey(k.PrivateKey) //get more time for this + privKey, err := crypto.MarshalECDSAPrivateKey(k.PrivateKey) // get more time for this if err != nil { return nil, err } @@ -135,7 +135,7 @@ func newKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *Key { key := &Key{ ID: id, - Address: crypto.PubKeyToAddress(&privateKeyECDSA.PublicKey), //TO DO get more time for this pointer + Address: crypto.PubKeyToAddress(&privateKeyECDSA.PublicKey), // TO DO get more time for this pointer PrivateKey: privateKeyECDSA, } @@ -192,7 +192,7 @@ func writeTemporaryKeyFile(file string, content []byte) (string, error) { } func newKey(rand io.Reader) (*Key, error) { - privateKeyECDSA, err := crypto.GenerateECDSAPrivateKey() //TO DO maybe not valid + privateKeyECDSA, err := crypto.GenerateECDSAPrivateKey() // TO DO maybe not valid if err != nil { return nil, err } diff --git a/accounts/keystore/keystore.go b/accounts/keystore/keystore.go index aef76138d6..c96459165a 100644 --- a/accounts/keystore/keystore.go +++ b/accounts/keystore/keystore.go @@ -28,7 +28,7 @@ var ( // already present in the keystore. ErrAccountAlreadyExists = errors.New("account already exists") - DefaultStorage, _ = filepath.Abs(filepath.Join("data-storage")) + DefaultStorage, _ = filepath.Abs(filepath.Join("data-storage")) //nolint:gocritic ) var KeyStoreType = reflect.TypeOf(&KeyStore{}) diff --git a/accounts/keystore/plain.go b/accounts/keystore/plain.go index babe8dd987..e0670ee221 100644 --- a/accounts/keystore/plain.go +++ b/accounts/keystore/plain.go @@ -43,11 +43,11 @@ func (ks keyStorePlain) GetKey(addr types.Address, filename, auth string) (*Key, return nil, err } - key.Address = types.StringToAddress(dat["address"].(string)) + key.Address = types.StringToAddress(dat["address"].(string)) //nolint:forcetypeassert - key.ID = uuid.MustParse(dat["id"].(string)) + key.ID = uuid.MustParse(dat["id"].(string)) //nolint:forcetypeassert - key.PrivateKey, err = crypto.BytesToECDSAPrivateKey([]byte(dat["privatekey"].(string))) + key.PrivateKey, err = crypto.BytesToECDSAPrivateKey([]byte(dat["privatekey"].(string))) //nolint:forcetypeassert if err != nil { return nil, err } diff --git a/accounts/manager.go b/accounts/manager.go index 87f137878a..196587cff6 100644 --- a/accounts/manager.go +++ b/accounts/manager.go @@ -7,6 +7,7 @@ import ( "github.com/0xPolygon/polygon-edge/accounts/event" "github.com/0xPolygon/polygon-edge/types" + "github.com/hashicorp/go-hclog" ) const managerSubBufferSize = 50 @@ -33,11 +34,13 @@ type Manager struct { quit chan chan error + logger hclog.Logger + term chan struct{} lock sync.RWMutex } -func NewManager(config *Config, backends ...Backend) *Manager { +func NewManager(config *Config, logger hclog.Logger, backends ...Backend) *Manager { var wallets []Wallet for _, backend := range backends { diff --git a/command/accounts/import/import.go b/command/accounts/import/import.go index 5df68ca4be..d06aa7012e 100644 --- a/command/accounts/import/import.go +++ b/command/accounts/import/import.go @@ -76,7 +76,7 @@ func runCommand(cmd *cobra.Command, _ []string) { scryptP = keystore.LightScryptP } - am := accounts.NewManager(&accounts.Config{}) + am := accounts.NewManager(&accounts.Config{}, nil) am.AddBackend(keystore.NewKeyStore(params.KeyDir, scryptN, scryptP, nil)) @@ -99,7 +99,7 @@ func runCommand(cmd *cobra.Command, _ []string) { outputter.SetError(fmt.Errorf("keystore is not available")) } - ks := backends[0].(*keystore.KeyStore) + ks := backends[0].(*keystore.KeyStore) //nolint:forcetypeassert acct, err := ks.ImportECDSA(privKey, params.Passphrase) if err != nil { diff --git a/crypto/crypto.go b/crypto/crypto.go index 47f09d19ca..4716ec59d0 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -370,13 +370,13 @@ func LatestSignerForChainID(chaidID uint64) TxSigner { return NewLondonSigner(chaidID) } -func DToECDSA(D []byte, strict bool) (*ecdsa.PrivateKey, error) { +func DToECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) { priv := new(ecdsa.PrivateKey) priv.PublicKey.Curve = btcec.S256() - if strict && 8*len(D) != priv.Params().BitSize { + if strict && 8*len(d) != priv.Params().BitSize { return nil, fmt.Errorf("invalid length, need %d bits", priv.Params().BitSize) } - priv.D = new(big.Int).SetBytes(D) + priv.D = new(big.Int).SetBytes(d) if priv.D.Cmp(secp256k1N) >= 0 { return nil, errors.New("invalid private key, >= N") @@ -386,11 +386,10 @@ func DToECDSA(D []byte, strict bool) (*ecdsa.PrivateKey, error) { return nil, errors.New("invalid private key, zero or negative") } - priv.PublicKey.X, priv.PublicKey.Y = btcec.S256().ScalarBaseMult(D) + priv.PublicKey.X, priv.PublicKey.Y = btcec.S256().ScalarBaseMult(d) if priv.PublicKey.X == nil { return nil, errors.New("invalid private key") } return priv, nil - } diff --git a/jsonrpc/dispatcher.go b/jsonrpc/dispatcher.go index c1567fc1e5..335009f3f6 100644 --- a/jsonrpc/dispatcher.go +++ b/jsonrpc/dispatcher.go @@ -11,6 +11,7 @@ import ( "time" "unicode" + "github.com/0xPolygon/polygon-edge/accounts" "github.com/armon/go-metrics" "github.com/hashicorp/go-hclog" jsonIter "github.com/json-iterator/go" @@ -75,7 +76,7 @@ func (dp dispatcherParams) isExceedingBatchLengthLimit(value uint64) bool { func newDispatcher( logger hclog.Logger, store JSONRPCStore, - params *dispatcherParams, + params *dispatcherParams, manager *accounts.Manager, ) (*Dispatcher, error) { d := &Dispatcher{ logger: logger.Named("dispatcher"), @@ -87,20 +88,21 @@ func newDispatcher( go d.filterManager.Run() } - if err := d.registerEndpoints(store); err != nil { + if err := d.registerEndpoints(store, manager); err != nil { return nil, err } return d, nil } -func (d *Dispatcher) registerEndpoints(store JSONRPCStore) error { +func (d *Dispatcher) registerEndpoints(store JSONRPCStore, manager *accounts.Manager) error { d.endpoints.Eth = &Eth{ d.logger, store, d.params.chainID, d.filterManager, d.params.priceLimit, + manager, } d.endpoints.Net = &Net{ store, diff --git a/jsonrpc/dispatcher_test.go b/jsonrpc/dispatcher_test.go index 193e4a069e..46c34c013c 100644 --- a/jsonrpc/dispatcher_test.go +++ b/jsonrpc/dispatcher_test.go @@ -562,7 +562,7 @@ func TestDispatcher_WebsocketConnection_Unsubscribe(t *testing.T) { func newTestDispatcher(tb testing.TB, logger hclog.Logger, store JSONRPCStore, params *dispatcherParams) *Dispatcher { tb.Helper() - d, err := newDispatcher(logger, store, params) + d, err := newDispatcher(logger, store, params, nil) require.NoError(tb, err) return d diff --git a/jsonrpc/eth_endpoint.go b/jsonrpc/eth_endpoint.go index c2b7e66f09..bb69cdad3c 100644 --- a/jsonrpc/eth_endpoint.go +++ b/jsonrpc/eth_endpoint.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/go-hclog" + "github.com/0xPolygon/polygon-edge/accounts" "github.com/0xPolygon/polygon-edge/chain" "github.com/0xPolygon/polygon-edge/gasprice" "github.com/0xPolygon/polygon-edge/helper/common" @@ -101,6 +102,7 @@ type Eth struct { chainID uint64 filterManager *FilterManager priceLimit uint64 + accManager *accounts.Manager } // ChainId returns the chain id of the client diff --git a/jsonrpc/eth_endpoint_test.go b/jsonrpc/eth_endpoint_test.go index 1dc5d905fe..0e3b456669 100644 --- a/jsonrpc/eth_endpoint_test.go +++ b/jsonrpc/eth_endpoint_test.go @@ -288,13 +288,13 @@ func TestEth_TxnType(t *testing.T) { func newTestEthEndpoint(store testStore) *Eth { return &Eth{ - hclog.NewNullLogger(), store, 100, nil, 0, + hclog.NewNullLogger(), store, 100, nil, 0, nil, } } func newTestEthEndpointWithPriceLimit(store testStore, priceLimit uint64) *Eth { return &Eth{ - hclog.NewNullLogger(), store, 100, nil, priceLimit, + hclog.NewNullLogger(), store, 100, nil, priceLimit, nil, } } diff --git a/jsonrpc/jsonrpc.go b/jsonrpc/jsonrpc.go index 54f85fef8b..0199ca7285 100644 --- a/jsonrpc/jsonrpc.go +++ b/jsonrpc/jsonrpc.go @@ -10,6 +10,7 @@ import ( "sync" "time" + "github.com/0xPolygon/polygon-edge/accounts" "github.com/0xPolygon/polygon-edge/secrets" "github.com/0xPolygon/polygon-edge/versioning" "github.com/gorilla/websocket" @@ -59,7 +60,7 @@ type Config struct { } // NewJSONRPC returns the JSONRPC http server -func NewJSONRPC(logger hclog.Logger, config *Config) (*JSONRPC, error) { +func NewJSONRPC(logger hclog.Logger, config *Config, manager *accounts.Manager) (*JSONRPC, error) { d, err := newDispatcher( logger, config.Store, @@ -71,6 +72,7 @@ func NewJSONRPC(logger hclog.Logger, config *Config) (*JSONRPC, error) { blockRangeLimit: config.BlockRangeLimit, concurrentRequestsDebug: config.ConcurrentRequestsDebug, }, + manager, ) if err != nil { diff --git a/jsonrpc/jsonrpc_test.go b/jsonrpc/jsonrpc_test.go index e941278978..0f17aa0a93 100644 --- a/jsonrpc/jsonrpc_test.go +++ b/jsonrpc/jsonrpc_test.go @@ -111,5 +111,5 @@ func newTestJSONRPC(t *testing.T) (*JSONRPC, error) { Addr: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: port}, } - return NewJSONRPC(hclog.NewNullLogger(), config) + return NewJSONRPC(hclog.NewNullLogger(), config, nil) } diff --git a/server/server.go b/server/server.go index de70b8f76c..e5bf164d63 100644 --- a/server/server.go +++ b/server/server.go @@ -17,6 +17,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" "google.golang.org/grpc" + "github.com/0xPolygon/polygon-edge/accounts" "github.com/0xPolygon/polygon-edge/archive" "github.com/0xPolygon/polygon-edge/blockchain" "github.com/0xPolygon/polygon-edge/blockchain/storagev2" @@ -76,6 +77,8 @@ type Server struct { // libp2p network network *network.Server + accManager *accounts.Manager + // transaction pool txpool *txpool.TxPool @@ -207,6 +210,11 @@ func NewServer(config *Config) (*Server, error) { m.network = network } + // setup account manager + { + m.accManager = accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: true}, nil) + } + // start blockchain object stateStorage, err := itrie.NewLevelDBStorage(filepath.Join(m.config.DataDir, "trie"), logger) if err != nil { @@ -899,7 +907,7 @@ func (s *Server) setupJSONRPC() error { SecretsManager: s.secretsManager, } - srv, err := jsonrpc.NewJSONRPC(s.logger, conf) + srv, err := jsonrpc.NewJSONRPC(s.logger, conf, s.accManager) if err != nil { return err }