Skip to content

Commit 2e589ba

Browse files
holimanqianhh
authored andcommitted
cmd/geth: remove unlock commandline flag (#30737)
This is one further step towards removing account management from `geth`. This PR deprecates the flag `unlock`, and makes the flag moot: unlock via geth is no longer possible.
1 parent 26ad97d commit 2e589ba

File tree

5 files changed

+58
-30
lines changed

5 files changed

+58
-30
lines changed

accounts/manager.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@ import (
2929
// the manager will buffer in its channel.
3030
const managerSubBufferSize = 50
3131

32-
// Config contains the settings of the global account manager.
33-
//
34-
// TODO(rjl493456442, karalabe, holiman): Get rid of this when account management
35-
// is removed in favor of Clef.
32+
// Config is a legacy struct which is not used
3633
type Config struct {
37-
InsecureUnlockAllowed bool // Whether account unlocking in insecure environment is allowed
34+
InsecureUnlockAllowed bool // Unused legacy-parameter
3835
}
3936

4037
// newBackendEvent lets the manager know it should
@@ -47,7 +44,6 @@ type newBackendEvent struct {
4744
// Manager is an overarching account manager that can communicate with various
4845
// backends for signing transactions.
4946
type Manager struct {
50-
config *Config // Global account manager configurations
5147
backends map[reflect.Type][]Backend // Index of backends currently registered
5248
updaters []event.Subscription // Wallet update subscriptions for all backends
5349
updates chan WalletEvent // Subscription sink for backend wallet changes
@@ -78,7 +74,6 @@ func NewManager(config *Config, backends ...Backend) *Manager {
7874
}
7975
// Assemble the account manager and return
8076
am := &Manager{
81-
config: config,
8277
backends: make(map[reflect.Type][]Backend),
8378
updaters: subs,
8479
updates: updates,
@@ -106,11 +101,6 @@ func (am *Manager) Close() error {
106101
return <-errc
107102
}
108103

109-
// Config returns the configuration of account manager.
110-
func (am *Manager) Config() *Config {
111-
return am.config
112-
}
113-
114104
// AddBackend starts the tracking of an additional backend for wallet updates.
115105
// cmd/geth assumes once this func returns the backends have been already integrated.
116106
func (am *Manager) AddBackend(backend Backend) {

cmd/geth/accountcmd.go

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
package main
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"os"
23+
"strings"
2224

2325
"github.com/ethereum/go-ethereum/accounts"
2426
"github.com/ethereum/go-ethereum/accounts/keystore"
2527
"github.com/ethereum/go-ethereum/cmd/utils"
28+
"github.com/ethereum/go-ethereum/common"
2629
"github.com/ethereum/go-ethereum/crypto"
2730
"github.com/ethereum/go-ethereum/log"
2831
"github.com/urfave/cli/v2"
@@ -191,7 +194,7 @@ nodes.
191194
// makeAccountManager creates an account manager with backends
192195
func makeAccountManager(ctx *cli.Context) *accounts.Manager {
193196
cfg := loadBaseConfig(ctx)
194-
am := accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: cfg.Node.InsecureUnlockAllowed})
197+
am := accounts.NewManager(nil)
195198
keydir, isEphemeral, err := cfg.Node.GetKeyStoreDir()
196199
if err != nil {
197200
utils.Fatalf("Failed to get the keystore directory: %v", err)
@@ -275,6 +278,24 @@ func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrErr
275278
return *match
276279
}
277280

281+
// readPasswordFromFile reads the first line of the given file, trims line endings,
282+
// and returns the password and whether the reading was successful.
283+
func readPasswordFromFile(path string) (string, bool) {
284+
if path == "" {
285+
return "", false
286+
}
287+
text, err := os.ReadFile(path)
288+
if err != nil {
289+
utils.Fatalf("Failed to read password file: %v", err)
290+
}
291+
lines := strings.Split(string(text), "\n")
292+
if len(lines) == 0 {
293+
return "", false
294+
}
295+
// Sanitise DOS line endings.
296+
return strings.TrimRight(lines[0], "\r"), true
297+
}
298+
278299
// accountCreate creates a new account into the keystore defined by the CLI flags.
279300
func accountCreate(ctx *cli.Context) error {
280301
cfg := loadBaseConfig(ctx)
@@ -292,8 +313,10 @@ func accountCreate(ctx *cli.Context) error {
292313
scryptP = keystore.LightScryptP
293314
}
294315

295-
password := utils.GetPassPhraseWithList("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
296-
316+
password, ok := readPasswordFromFile(ctx.Path(utils.PasswordFileFlag.Name))
317+
if !ok {
318+
password = utils.GetPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true)
319+
}
297320
account, err := keystore.StoreKey(keydir, password, scryptN, scryptP)
298321

299322
if err != nil {
@@ -323,10 +346,23 @@ func accountUpdate(ctx *cli.Context) error {
323346
ks := backends[0].(*keystore.KeyStore)
324347

325348
for _, addr := range ctx.Args().Slice() {
326-
account, oldPassword := unlockAccount(ks, addr, 0, nil)
327-
newPassword := utils.GetPassPhraseWithList("Please give a new password. Do not forget this password.", true, 0, nil)
328-
if err := ks.Update(account, oldPassword, newPassword); err != nil {
329-
utils.Fatalf("Could not update the account: %v", err)
349+
if !common.IsHexAddress(addr) {
350+
return errors.New("address must be specified in hexadecimal form")
351+
}
352+
account := accounts.Account{Address: common.HexToAddress(addr)}
353+
newPassword := utils.GetPassPhrase("Please give a NEW password. Do not forget this password.", true)
354+
updateFn := func(attempt int) error {
355+
prompt := fmt.Sprintf("Please provide the OLD password for account %s | Attempt %d/%d", addr, attempt+1, 3)
356+
password := utils.GetPassPhrase(prompt, false)
357+
return ks.Update(account, password, newPassword)
358+
}
359+
// let user attempt unlock thrice.
360+
err := updateFn(0)
361+
for attempts := 1; attempts < 3 && errors.Is(err, keystore.ErrDecrypt); attempts++ {
362+
err = updateFn(attempts)
363+
}
364+
if err != nil {
365+
return fmt.Errorf("could not update account: %w", err)
330366
}
331367
}
332368
return nil
@@ -347,10 +383,12 @@ func importWallet(ctx *cli.Context) error {
347383
if len(backends) == 0 {
348384
utils.Fatalf("Keystore is not available")
349385
}
386+
password, ok := readPasswordFromFile(ctx.Path(utils.PasswordFileFlag.Name))
387+
if !ok {
388+
password = utils.GetPassPhrase("", false)
389+
}
350390
ks := backends[0].(*keystore.KeyStore)
351-
passphrase := utils.GetPassPhraseWithList("", false, 0, utils.MakePasswordList(ctx))
352-
353-
acct, err := ks.ImportPreSaleKey(keyJSON, passphrase)
391+
acct, err := ks.ImportPreSaleKey(keyJSON, password)
354392
if err != nil {
355393
utils.Fatalf("%v", err)
356394
}
@@ -373,9 +411,11 @@ func accountImport(ctx *cli.Context) error {
373411
utils.Fatalf("Keystore is not available")
374412
}
375413
ks := backends[0].(*keystore.KeyStore)
376-
passphrase := utils.GetPassPhraseWithList("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
377-
378-
acct, err := ks.ImportECDSA(key, passphrase)
414+
password, ok := readPasswordFromFile(ctx.Path(utils.PasswordFileFlag.Name))
415+
if !ok {
416+
password = utils.GetPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true)
417+
}
418+
acct, err := ks.ImportECDSA(key, password)
379419
if err != nil {
380420
utils.Fatalf("Could not create the account: %v", err)
381421
}

internal/ethapi/api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ func allBlobTxs(addr common.Address, config *params.ChainConfig) []txData {
416416
func newTestAccountManager(t *testing.T) (*accounts.Manager, accounts.Account) {
417417
var (
418418
dir = t.TempDir()
419-
am = accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: true})
419+
am = accounts.NewManager(nil)
420420
b = keystore.NewKeyStore(dir, 2, 1)
421421
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
422422
)

node/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func New(conf *Config) (*Node, error) {
133133
node.keyDirTemp = isEphem
134134
// Creates an empty AccountManager with no backends. Callers (e.g. cmd/geth)
135135
// are required to add the backends later on.
136-
node.accman = accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: conf.InsecureUnlockAllowed})
136+
node.accman = accounts.NewManager(nil)
137137

138138
// Create an empty antimev keystore with only file path.
139139
antimevKeyDir, err := conf.GetAntiMEVKeyStorePath()

signer/core/api.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,7 @@ func StartClefAccountManager(ksLocation string, nousb, lightKDF bool, scpath str
184184
}
185185
}
186186
}
187-
188-
// Clef doesn't allow insecure http account unlock.
189-
return accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: false}, backends...)
187+
return accounts.NewManager(nil, backends...)
190188
}
191189

192190
// MetadataFromContext extracts Metadata from a given context.Context

0 commit comments

Comments
 (0)