Skip to content

Commit

Permalink
fix: use simple encrypted keysigner
Browse files Browse the repository at this point in the history
  • Loading branch information
Alok committed Jun 4, 2024
1 parent 6185764 commit 9e2970d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 114 deletions.
4 changes: 2 additions & 2 deletions oracle/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func newLogger(lvl, logFmt, tags string, sink io.Writer) (*slog.Logger, error) {

func setupKeySigner(c *cli.Context) (keysigner.KeySigner, error) {
if c.IsSet(optionKeystorePath.Name) {
return keysigner.NewKeystoreSigner(c.String(optionKeystorePath.Name), c.String(optionKeystorePassword.Name))
return keysigner.NewEncryptedSigner(c.String(optionKeystorePath.Name), c.String(optionKeystorePassword.Name))
}
return keysigner.NewPrivateKeySigner(c.String(optionPrivKeyFile.Name))
return keysigner.NewSigner(c.String(optionPrivKeyFile.Name))
}
4 changes: 2 additions & 2 deletions p2p/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ func launchNodeWithConfig(c *cli.Context) error {

func newKeySigner(c *cli.Context) (ks.KeySigner, error) {
if c.IsSet(optionKeystorePath.Name) {
return ks.NewKeystoreSigner(c.String(optionKeystorePath.Name), c.String(optionKeystorePassword.Name))
return ks.NewEncryptedSigner(c.String(optionKeystorePath.Name), c.String(optionKeystorePassword.Name))
}
return ks.NewPrivateKeySigner(c.String(optionPrivKeyFile.Name))
return ks.NewSigner(c.String(optionPrivKeyFile.Name))
}
1 change: 0 additions & 1 deletion p2p/pkg/p2p/libp2p/libp2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func New(opts *Options) (*Service, error) {
if err != nil {
return nil, fmt.Errorf("failed to get priv key: %w", err)
}
defer opts.KeySigner.ZeroPrivateKey(privKey)

padded32BytePrivKey := util.PadKeyTo32Bytes(privKey.D)
libp2pKey, err := libp2pcrypto.UnmarshalSecp256k1PrivateKey(padded32BytePrivKey)
Expand Down
20 changes: 17 additions & 3 deletions x/contracts/txmonitor/txmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (

var (
ErrTxnCancelled = errors.New("transaction was cancelled")
ErrTxnFailed = errors.New("transaction failed")
ErrMonitorClosed = errors.New("monitor was closed")
)

Expand Down Expand Up @@ -222,7 +223,6 @@ func (m *Monitor) Sent(ctx context.Context, tx *types.Transaction) {
r := <-res
status := "success"
if r.Err != nil {
m.logger.Error("transaction failed", "err", r.Err)
status = fmt.Sprintf("failed: %v", r.Err)
}
if err := m.saver.Update(context.Background(), tx.Hash(), status); err != nil {
Expand Down Expand Up @@ -353,13 +353,27 @@ func (m *Monitor) check(ctx context.Context, newBlock uint64, lastNonce uint64)
m.notify(nonce, txHashes[start+i], Result{nil, ErrTxnCancelled})
continue
}
m.logger.Error("failed to get receipt", "error", r.Err, "txHash", txHashes[start+i])
continue
}
if r.Receipt.Status != types.ReceiptStatusSuccessful {
tt, err := m.helper.TraceTransaction(ctx, txHashes[start+i])
if err != nil {
m.logger.Error("retrieving transaction trace failed", "error", err)
m.logger.Error(
"retrieving transaction trace failed",
"error", err,
"txHash", txHashes[start+i],
)
} else {
m.logger.Error("transaction failed",
"transaction_trace", tt,
"txHash", txHashes[start+i],
)
}
m.logger.Error("failed to get receipt", "error", r.Err, "transaction_trace", tt)
m.notify(nonce, txHashes[start+i], Result{r.Receipt, ErrTxnFailed})
continue
}

m.notify(nonce, txHashes[start+i], Result{r.Receipt, nil})
}
}
Expand Down
1 change: 0 additions & 1 deletion x/keysigner/keysigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type KeySigner interface {
SignTx(tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
GetAddress() common.Address
GetPrivateKey() (*ecdsa.PrivateKey, error)
ZeroPrivateKey(key *ecdsa.PrivateKey)
GetAuth(chainID *big.Int) (*bind.TransactOpts, error)
GetAuthWithCtx(ctx context.Context, chainID *big.Int) (*bind.TransactOpts, error)
}
91 changes: 0 additions & 91 deletions x/keysigner/keystoresigner.go

This file was deleted.

41 changes: 27 additions & 14 deletions x/keysigner/privatekeysigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
"github.com/ethereum/go-ethereum/crypto"
)

type PrivateKeySigner struct {
type Signer struct {
path string
privKey *ecdsa.PrivateKey
}

func NewPrivateKeySigner(path string) (*PrivateKeySigner, error) {
func NewSigner(path string) (*Signer, error) {
privKeyFile, err := resolveFilePath(path)
if err != nil {
return nil, fmt.Errorf("failed to get private key file path: %w", err)
Expand All @@ -36,33 +36,50 @@ func NewPrivateKeySigner(path string) (*PrivateKeySigner, error) {
return nil, fmt.Errorf("failed to load private key from file '%s': %w", privKeyFile, err)
}

return &PrivateKeySigner{
return &Signer{
path: privKeyFile,
privKey: privKey,
}, nil
}

func (pks *PrivateKeySigner) SignHash(hash []byte) ([]byte, error) {
func NewEncryptedSigner(path, password string) (*Signer, error) {
privKeyFile, err := resolveFilePath(path)
if err != nil {
return nil, fmt.Errorf("failed to get private key file path: %w", err)
}

privKey, err := extractPrivateKey(privKeyFile, password)
if err != nil {
return nil, fmt.Errorf("failed to extract private key: %w", err)
}

return &Signer{
path: privKeyFile,
privKey: privKey,
}, nil
}

func (pks *Signer) SignHash(hash []byte) ([]byte, error) {
return crypto.Sign(hash, pks.privKey)
}

func (pks *PrivateKeySigner) SignTx(tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
func (pks *Signer) SignTx(tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
return types.SignTx(tx, types.NewLondonSigner(chainID), pks.privKey)
}

func (pks *PrivateKeySigner) GetAddress() common.Address {
func (pks *Signer) GetAddress() common.Address {
return crypto.PubkeyToAddress(pks.privKey.PublicKey)
}

func (pks *PrivateKeySigner) GetPrivateKey() (*ecdsa.PrivateKey, error) {
func (pks *Signer) GetPrivateKey() (*ecdsa.PrivateKey, error) {
return pks.privKey, nil
}

func (pks *PrivateKeySigner) GetAuth(chainID *big.Int) (*bind.TransactOpts, error) {
func (pks *Signer) GetAuth(chainID *big.Int) (*bind.TransactOpts, error) {
return bind.NewKeyedTransactorWithChainID(pks.privKey, chainID)
}

func (pks *PrivateKeySigner) GetAuthWithCtx(ctx context.Context, chainID *big.Int) (*bind.TransactOpts, error) {
func (pks *Signer) GetAuthWithCtx(ctx context.Context, chainID *big.Int) (*bind.TransactOpts, error) {
opts, err := pks.GetAuth(chainID)
if err != nil {
return nil, err
Expand All @@ -71,11 +88,7 @@ func (pks *PrivateKeySigner) GetAuthWithCtx(ctx context.Context, chainID *big.In
return opts, nil
}

// ZeroPrivateKey does nothing because the private key for PKS persists in memory
// and should not be deleted.
func (pks *PrivateKeySigner) ZeroPrivateKey(key *ecdsa.PrivateKey) {}

func (pks *PrivateKeySigner) String() string {
func (pks *Signer) String() string {
return pks.path
}

Expand Down

0 comments on commit 9e2970d

Please sign in to comment.