Skip to content

Commit 1d72aaa

Browse files
committed
simplify account unlocking
1 parent 9f6016e commit 1d72aaa

2 files changed

Lines changed: 85 additions & 56 deletions

File tree

‎accounts/account_manager.go‎

Lines changed: 43 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ var (
4949
ErrNoKeys = errors.New("no keys in store")
5050
)
5151

52-
const (
53-
// Default unlock duration (in seconds) when an account is unlocked from the console
54-
DefaultAccountUnlockDuration = 300
55-
)
56-
5752
type Account struct {
5853
Address common.Address
5954
}
@@ -114,28 +109,58 @@ func (am *Manager) Sign(a Account, toSign []byte) (signature []byte, err error)
114109
return signature, err
115110
}
116111

117-
// TimedUnlock unlocks the account with the given address.
118-
// When timeout has passed, the account will be locked again.
112+
// unlock indefinitely
113+
func (am *Manager) Unlock(addr common.Address, keyAuth string) error {
114+
return am.TimedUnlock(addr, keyAuth, 0)
115+
}
116+
117+
// Unlock unlocks the account with the given address. The account
118+
// stays unlocked for the duration of timeout
119+
// it timeout is 0 the account is unlocked for the entire session
119120
func (am *Manager) TimedUnlock(addr common.Address, keyAuth string, timeout time.Duration) error {
120121
key, err := am.keyStore.GetKey(addr, keyAuth)
121122
if err != nil {
122123
return err
123124
}
124-
u := am.addUnlocked(addr, key)
125-
go am.dropLater(addr, u, timeout)
125+
var u *unlocked
126+
am.mutex.Lock()
127+
defer am.mutex.Unlock()
128+
var found bool
129+
u, found = am.unlocked[addr]
130+
if found {
131+
// terminate dropLater for this key to avoid unexpected drops.
132+
if u.abort != nil {
133+
close(u.abort)
134+
}
135+
}
136+
if timeout > 0 {
137+
u = &unlocked{Key: key, abort: make(chan struct{})}
138+
go am.expire(addr, u, timeout)
139+
} else {
140+
u = &unlocked{Key: key}
141+
}
142+
am.unlocked[addr] = u
126143
return nil
127144
}
128145

129-
// Unlock unlocks the account with the given address. The account
130-
// stays unlocked until the program exits or until a TimedUnlock
131-
// timeout (started after the call to Unlock) expires.
132-
func (am *Manager) Unlock(addr common.Address, keyAuth string) error {
133-
key, err := am.keyStore.GetKey(addr, keyAuth)
134-
if err != nil {
135-
return err
146+
func (am *Manager) expire(addr common.Address, u *unlocked, timeout time.Duration) {
147+
t := time.NewTimer(timeout)
148+
defer t.Stop()
149+
select {
150+
case <-u.abort:
151+
// just quit
152+
case <-t.C:
153+
am.mutex.Lock()
154+
// only drop if it's still the same key instance that dropLater
155+
// was launched with. we can check that using pointer equality
156+
// because the map stores a new pointer every time the key is
157+
// unlocked.
158+
if am.unlocked[addr] == u {
159+
zeroKey(u.PrivateKey)
160+
delete(am.unlocked, addr)
161+
}
162+
am.mutex.Unlock()
136163
}
137-
am.addUnlocked(addr, key)
138-
return nil
139164
}
140165

141166
func (am *Manager) NewAccount(auth string) (Account, error) {
@@ -162,43 +187,6 @@ func (am *Manager) Accounts() ([]Account, error) {
162187
return accounts, err
163188
}
164189

165-
func (am *Manager) addUnlocked(addr common.Address, key *crypto.Key) *unlocked {
166-
u := &unlocked{Key: key, abort: make(chan struct{})}
167-
am.mutex.Lock()
168-
prev, found := am.unlocked[addr]
169-
if found {
170-
// terminate dropLater for this key to avoid unexpected drops.
171-
close(prev.abort)
172-
// the key is zeroed here instead of in dropLater because
173-
// there might not actually be a dropLater running for this
174-
// key, i.e. when Unlock was used.
175-
zeroKey(prev.PrivateKey)
176-
}
177-
am.unlocked[addr] = u
178-
am.mutex.Unlock()
179-
return u
180-
}
181-
182-
func (am *Manager) dropLater(addr common.Address, u *unlocked, timeout time.Duration) {
183-
t := time.NewTimer(timeout)
184-
defer t.Stop()
185-
select {
186-
case <-u.abort:
187-
// just quit
188-
case <-t.C:
189-
am.mutex.Lock()
190-
// only drop if it's still the same key instance that dropLater
191-
// was launched with. we can check that using pointer equality
192-
// because the map stores a new pointer every time the key is
193-
// unlocked.
194-
if am.unlocked[addr] == u {
195-
zeroKey(u.PrivateKey)
196-
delete(am.unlocked, addr)
197-
}
198-
am.mutex.Unlock()
199-
}
200-
}
201-
202190
// zeroKey zeroes a private key in memory.
203191
func zeroKey(k *ecdsa.PrivateKey) {
204192
b := k.D.Bits()

‎accounts/accounts_test.go‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestSign(t *testing.T) {
1818
pass := "" // not used but required by API
1919
a1, err := am.NewAccount(pass)
2020
toSign := randentropy.GetEntropyCSPRNG(32)
21-
am.Unlock(a1.Address, "")
21+
am.Unlock(a1.Address, "", 0)
2222

2323
_, err = am.Sign(a1, toSign)
2424
if err != nil {
@@ -58,6 +58,47 @@ func TestTimedUnlock(t *testing.T) {
5858
if err != ErrLocked {
5959
t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
6060
}
61+
62+
}
63+
64+
func TestOverrideUnlock(t *testing.T) {
65+
dir, ks := tmpKeyStore(t, crypto.NewKeyStorePassphrase)
66+
defer os.RemoveAll(dir)
67+
68+
am := NewManager(ks)
69+
pass := "foo"
70+
a1, err := am.NewAccount(pass)
71+
toSign := randentropy.GetEntropyCSPRNG(32)
72+
73+
// Unlock indefinitely
74+
if err = am.Unlock(a1.Address, pass); err != nil {
75+
t.Fatal(err)
76+
}
77+
78+
// Signing without passphrase works because account is temp unlocked
79+
_, err = am.Sign(a1, toSign)
80+
if err != nil {
81+
t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
82+
}
83+
84+
// reset unlock to a shorter period, invalidates the previous unlock
85+
if err = am.TimedUnlock(a1.Address, pass, 100*time.Millisecond); err != nil {
86+
t.Fatal(err)
87+
}
88+
89+
// Signing without passphrase still works because account is temp unlocked
90+
_, err = am.Sign(a1, toSign)
91+
if err != nil {
92+
t.Fatal("Signing shouldn't return an error after unlocking, got ", err)
93+
}
94+
95+
// Signing fails again after automatic locking
96+
time.Sleep(150 * time.Millisecond)
97+
_, err = am.Sign(a1, toSign)
98+
if err != ErrLocked {
99+
t.Fatal("Signing should've failed with ErrLocked timeout expired, got ", err)
100+
}
101+
61102
}
62103

63104
func tmpKeyStore(t *testing.T, new func(string) crypto.KeyStore2) (string, crypto.KeyStore2) {

0 commit comments

Comments
 (0)