From 7ff372967367676418386b5034f256cbb62a01e0 Mon Sep 17 00:00:00 2001 From: ATMackay Date: Thu, 2 May 2024 11:52:18 +0100 Subject: [PATCH 1/3] feat(keystore): Add SignerAddress --- keystore/geth/keystore.go | 17 +++++++++++++++++ keystore/keystore.go | 1 + 2 files changed, 18 insertions(+) diff --git a/keystore/geth/keystore.go b/keystore/geth/keystore.go index 9f779f6..a675713 100644 --- a/keystore/geth/keystore.go +++ b/keystore/geth/keystore.go @@ -13,6 +13,8 @@ import ( keystore "github.com/kilnfi/go-utils/keystore" ) +var _ (keystore.Store) = &KeyStore{} + type KeyStore struct { cfg *Config keys *gethkeystore.KeyStore @@ -70,3 +72,18 @@ func (s *KeyStore) SignTx(_ context.Context, addr gethcommon.Address, tx *gethty func (s *KeyStore) HasAccount(_ context.Context, addr gethcommon.Address) (bool, error) { return s.keys.HasAddress(addr), nil } + +func (s *KeyStore) SignerAddress(_ context.Context) (*keystore.Account, error) { + if s.keys == nil { + return nil, fmt.Errorf("no cached keys") + } + accs := s.keys.Accounts() + if len(accs) < 1 { + return nil, fmt.Errorf("keystore has no accounts") + } + // select first (primary account) address + return &keystore.Account{ + Addr: accs[0].Address, + URL: accs[0].URL, + }, nil +} diff --git a/keystore/keystore.go b/keystore/keystore.go index 568b574..b0d1556 100644 --- a/keystore/keystore.go +++ b/keystore/keystore.go @@ -19,4 +19,5 @@ type Store interface { HasAccount(ctx context.Context, addr gethcommon.Address) (bool, error) SignTx(ctx context.Context, addr gethcommon.Address, tx *gethtypes.Transaction, chainID *big.Int) (*gethtypes.Transaction, error) Import(ctx context.Context, hexkey string) (*Account, error) + SignerAddress(context.Context) (*Account, error) } From f9e7c79e22edc40e2f10b6663d398bac421be9fd Mon Sep 17 00:00:00 2001 From: ATMackay Date: Thu, 2 May 2024 11:56:26 +0100 Subject: [PATCH 2/3] lint --- keystore/geth/keystore.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keystore/geth/keystore.go b/keystore/geth/keystore.go index a675713..5dbf589 100644 --- a/keystore/geth/keystore.go +++ b/keystore/geth/keystore.go @@ -13,7 +13,7 @@ import ( keystore "github.com/kilnfi/go-utils/keystore" ) -var _ (keystore.Store) = &KeyStore{} +var _ keystore.Store = &KeyStore{} type KeyStore struct { cfg *Config From 99a477fa2a2293196326dd4dab1cb8d73d600eff Mon Sep 17 00:00:00 2001 From: ATMackay Date: Thu, 2 May 2024 11:58:29 +0100 Subject: [PATCH 3/3] simplify --- keystore/geth/keystore.go | 11 ++++------- keystore/keystore.go | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/keystore/geth/keystore.go b/keystore/geth/keystore.go index 5dbf589..95cc3d1 100644 --- a/keystore/geth/keystore.go +++ b/keystore/geth/keystore.go @@ -73,17 +73,14 @@ func (s *KeyStore) HasAccount(_ context.Context, addr gethcommon.Address) (bool, return s.keys.HasAddress(addr), nil } -func (s *KeyStore) SignerAddress(_ context.Context) (*keystore.Account, error) { +func (s *KeyStore) SignerAddress(_ context.Context) (gethcommon.Address, error) { if s.keys == nil { - return nil, fmt.Errorf("no cached keys") + return gethcommon.Address{}, fmt.Errorf("no cached keys") } accs := s.keys.Accounts() if len(accs) < 1 { - return nil, fmt.Errorf("keystore has no accounts") + return gethcommon.Address{}, fmt.Errorf("keystore has no accounts") } // select first (primary account) address - return &keystore.Account{ - Addr: accs[0].Address, - URL: accs[0].URL, - }, nil + return accs[0].Address, nil } diff --git a/keystore/keystore.go b/keystore/keystore.go index b0d1556..a0f40dc 100644 --- a/keystore/keystore.go +++ b/keystore/keystore.go @@ -19,5 +19,5 @@ type Store interface { HasAccount(ctx context.Context, addr gethcommon.Address) (bool, error) SignTx(ctx context.Context, addr gethcommon.Address, tx *gethtypes.Transaction, chainID *big.Int) (*gethtypes.Transaction, error) Import(ctx context.Context, hexkey string) (*Account, error) - SignerAddress(context.Context) (*Account, error) + SignerAddress(context.Context) (gethcommon.Address, error) }