Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rename swarm key #4647

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions cmd/bee/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,15 +468,25 @@ func (c *command) configureSigner(cmd *cobra.Command, logger log.Logger) (config
return nil, err
}
} else {
swarmPrivateKey, _, err := keystore.Key("swarm", password, crypto.EDGSecp256_K1)
oldSwarmKeyExists, err := keystore.Exists("swarm")
if err != nil {
return nil, fmt.Errorf("swarm key: %w", err)
return nil, err
}
if oldSwarmKeyExists {
err := keystore.RenameKey("swarm", "wallet")
if err != nil {
return nil, err
}
}
walletPrivateKey, _, err := keystore.Key("wallet", password, crypto.EDGSecp256_K1)
if err != nil {
return nil, fmt.Errorf("swarm wallet key: %w", err)
}
signer = crypto.NewDefaultSigner(swarmPrivateKey)
publicKey = &swarmPrivateKey.PublicKey
signer = crypto.NewDefaultSigner(walletPrivateKey)
publicKey = &walletPrivateKey.PublicKey
}

logger.Info("swarm public key", "public_key", hex.EncodeToString(crypto.EncodeSecp256k1PublicKey(publicKey)))
logger.Info("swarm wallet public key", "public_key", hex.EncodeToString(crypto.EncodeSecp256k1PublicKey(publicKey)))

libp2pPrivateKey, created, err := keystore.Key(libp2pPKFilename, password, crypto.EDGSecp256_R1)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/keystore/file/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ func (s *Service) Key(name, password string, edg keystore.EDG) (pk *ecdsa.Privat
return pk, false, nil
}

func (s *Service) RenameKey(oldName, newName string) error {
oldFilename := s.keyFilename(oldName)
newFilename := s.keyFilename(newName)

err := os.Rename(oldFilename, newFilename)
if err != nil {
return err
}

return nil
}

func (s *Service) keyFilename(name string) string {
return filepath.Join(s.dir, fmt.Sprintf("%s.key", name))
}
2 changes: 2 additions & 0 deletions pkg/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ type Service interface {
Exists(name string) (bool, error)
// SetKey generates and persists a new private key
SetKey(name, password string, edg EDG) (*ecdsa.PrivateKey, error)
// RenameKey renames a key
RenameKey(oldName, newName string) error
}
15 changes: 15 additions & 0 deletions pkg/keystore/mem/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ func (s *Service) Key(name, password string, edg keystore.EDG) (pk *ecdsa.Privat
return k.pk, created, nil
}

func (s *Service) RenameKey(oldName, newName string) error {
s.mu.Lock()
defer s.mu.Unlock()

k, ok := s.m[oldName]
if !ok {
return fmt.Errorf("key with name %s does not exist", oldName)
}

delete(s.m, oldName)
s.m[newName] = k

return nil
}

type key struct {
pk *ecdsa.PrivateKey
password string
Expand Down
23 changes: 19 additions & 4 deletions pkg/keystore/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func Service(t *testing.T, s keystore.Service) {
t.Helper()

exists, err := s.Exists("swarm")
exists, err := s.Exists("wallet")
if err != nil {
t.Fatal(err)
}
Expand All @@ -28,7 +28,7 @@ func Service(t *testing.T, s keystore.Service) {
}

edg := crypto.EDGSecp256_K1
// create a new swarm key
// create a new swarm wallet key
k1, created, err := s.Key("swarm", "pass123456", edg)
if err != nil {
t.Fatal(err)
Expand All @@ -46,8 +46,23 @@ func Service(t *testing.T, s keystore.Service) {
t.Fatal("should exist")
}

// rename swarm key to wallet
err = s.RenameKey("swarm", "wallet")
if err != nil {
t.Fatal(err)
}

exists, err = s.Exists("swarm")
if err != nil {
t.Fatal(err)
}

if exists {
t.Fatal("shouldn't exist")
}

// get swarm key
k2, created, err := s.Key("swarm", "pass123456", edg)
k2, created, err := s.Key("wallet", "pass123456", edg)
if err != nil {
t.Fatal(err)
}
Expand All @@ -59,7 +74,7 @@ func Service(t *testing.T, s keystore.Service) {
}

// invalid password
_, _, err = s.Key("swarm", "invalid password", edg)
_, _, err = s.Key("wallet", "invalid password", edg)
if !errors.Is(err, keystore.ErrInvalidPassword) {
t.Fatal(err)
}
Expand Down