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-
5752type 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
119120func (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
141166func (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.
203191func zeroKey (k * ecdsa.PrivateKey ) {
204192 b := k .D .Bits ()
0 commit comments