Skip to content

Commit

Permalink
acounts/keystore, cmd/faucet: fix faucet double import (ethereum#21172)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Jan 22, 2025
1 parent 269fc3e commit 1474e53
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 6 additions & 2 deletions accounts/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ var (
ErrLocked = accounts.NewAuthNeededError("password or unlock")
ErrNoMatch = errors.New("no key for given address or file")
ErrDecrypt = errors.New("could not decrypt key with given password")

// ErrAccountAlreadyExists is returned if an account attempted to import is
// already present in the keystore.
ErrAccountAlreadyExists = errors.New("account alreaady exists")
)

// KeyStoreType is the reflect type of a keystore backend.
Expand Down Expand Up @@ -453,7 +457,7 @@ func (ks *KeyStore) Import(keyJSON []byte, passphrase, newPassphrase string) (ac
ks.importMu.Lock()
defer ks.importMu.Unlock()
if ks.cache.hasAddress(key.Address) {
return accounts.Account{}, errors.New("account already exists")
return accounts.Account{}, ErrAccountAlreadyExists
}
return ks.importKey(key, newPassphrase)
}
Expand All @@ -464,7 +468,7 @@ func (ks *KeyStore) ImportECDSA(priv *ecdsa.PrivateKey, passphrase string) (acco
ks.importMu.Lock()
defer ks.importMu.Unlock()
if ks.cache.hasAddress(key.Address) {
return accounts.Account{}, errors.New("account already exists")
return accounts.Account{}, ErrAccountAlreadyExists
}
return ks.importKey(key, passphrase)
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/faucet/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func main() {
log.Crit("Failed to read account key contents", "file", *accJSONFlag, "err", err)
}
acc, err := ks.Import(blob, pass, pass)
if err != nil {
if err != nil && err != keystore.ErrAccountAlreadyExists {
log.Crit("Failed to import faucet signer account", "err", err)
}
ks.Unlock(acc, pass)
Expand Down Expand Up @@ -710,8 +710,11 @@ func authTwitter(url string) (string, string, common.Address, error) {
return "", "", common.Address{}, errors.New("invalid Twitter status URL")
}
// Twitter's API isn't really friendly with direct links. Still, we don't
// want to do ask read permissions from users, so just load the public posts and
// scrape it for the Ethereum address and profile URL.
// want to do ask read permissions from users, so just load the public posts
// and scrape it for the Ethereum address and profile URL. We need to load
// the mobile page though since the main page loads tweet contents via JS.
url = strings.Replace(url, "https://twitter.com/", "https://mobile.twitter.com/", 1)

res, err := http.Get(url)
if err != nil {
return "", "", common.Address{}, err
Expand Down

0 comments on commit 1474e53

Please sign in to comment.