Skip to content

Commit 6498df7

Browse files
committed
accounts: ensure TimedUnlock does not override indefinite unlock timeout
1 parent 46df50b commit 6498df7

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

accounts/account_manager.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,15 @@ func (am *Manager) Lock(addr common.Address) error {
164164
return nil
165165
}
166166

167-
// TimedUnlock unlocks the given account with. The account
167+
// TimedUnlock unlocks the given account with the passphrase. The account
168168
// stays unlocked for the duration of timeout. A timeout of 0 unlocks the account
169-
// until the program exits. The account must match a unique key.
169+
// until the program exits. The account must match a unique key file.
170170
//
171-
// If the accout is already unlocked, TimedUnlock extends or shortens
172-
// the active unlock timeout.
173-
func (am *Manager) TimedUnlock(a Account, keyAuth string, timeout time.Duration) error {
174-
_, key, err := am.getDecryptedKey(a, keyAuth)
171+
// If the account address is already unlocked for a duration, TimedUnlock extends or
172+
// shortens the active unlock timeout. If the address was previously unlocked
173+
// indefinitely the timeout is not altered.
174+
func (am *Manager) TimedUnlock(a Account, passphrase string, timeout time.Duration) error {
175+
a, key, err := am.getDecryptedKey(a, passphrase)
175176
if err != nil {
176177
return err
177178
}
@@ -180,8 +181,13 @@ func (am *Manager) TimedUnlock(a Account, keyAuth string, timeout time.Duration)
180181
defer am.mu.Unlock()
181182
u, found := am.unlocked[a.Address]
182183
if found {
183-
// terminate dropLater for this key to avoid unexpected drops.
184-
if u.abort != nil {
184+
if u.abort == nil {
185+
// The address was unlocked indefinitely, so unlocking
186+
// it with a timeout would be confusing.
187+
zeroKey(key.PrivateKey)
188+
return nil
189+
} else {
190+
// Terminate the expire goroutine and replace it below.
185191
close(u.abort)
186192
}
187193
}

accounts/accounts_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ func TestOverrideUnlock(t *testing.T) {
120120
pass := "foo"
121121
a1, err := am.NewAccount(pass)
122122

123-
// Unlock indefinitely
124-
if err = am.Unlock(a1, pass); err != nil {
123+
// Unlock indefinitely.
124+
if err = am.TimedUnlock(a1, pass, 5*time.Minute); err != nil {
125125
t.Fatal(err)
126126
}
127127

0 commit comments

Comments
 (0)