@@ -26,7 +26,7 @@ This abstracts part of a user's interaction with an account she controls.
2626It's not an abstraction of core Ethereum accounts data type / logic -
2727for that see the core processing code of blocks / txs.
2828
29- Currently this is pretty much a passthrough to the KeyStore2 interface,
29+ Currently this is pretty much a passthrough to the KeyStore interface,
3030and accounts persistence is derived from stored keys' addresses
3131
3232*/
@@ -36,6 +36,7 @@ import (
3636 "crypto/ecdsa"
3737 crand "crypto/rand"
3838 "errors"
39+ "fmt"
3940 "os"
4041 "sync"
4142 "time"
@@ -49,17 +50,12 @@ var (
4950 ErrNoKeys = errors .New ("no keys in store" )
5051)
5152
52- const (
53- // Default unlock duration (in seconds) when an account is unlocked from the console
54- DefaultAccountUnlockDuration = 300
55- )
56-
5753type Account struct {
5854 Address common.Address
5955}
6056
6157type Manager struct {
62- keyStore crypto.KeyStore2
58+ keyStore crypto.KeyStore
6359 unlocked map [common.Address ]* unlocked
6460 mutex sync.RWMutex
6561}
@@ -69,7 +65,7 @@ type unlocked struct {
6965 abort chan struct {}
7066}
7167
72- func NewManager (keyStore crypto.KeyStore2 ) * Manager {
68+ func NewManager (keyStore crypto.KeyStore ) * Manager {
7369 return & Manager {
7470 keyStore : keyStore ,
7571 unlocked : make (map [common.Address ]* unlocked ),
@@ -86,19 +82,6 @@ func (am *Manager) HasAccount(addr common.Address) bool {
8682 return false
8783}
8884
89- func (am * Manager ) Primary () (addr common.Address , err error ) {
90- addrs , err := am .keyStore .GetKeyAddresses ()
91- if os .IsNotExist (err ) {
92- return common.Address {}, ErrNoKeys
93- } else if err != nil {
94- return common.Address {}, err
95- }
96- if len (addrs ) == 0 {
97- return common.Address {}, ErrNoKeys
98- }
99- return addrs [0 ], nil
100- }
101-
10285func (am * Manager ) DeleteAccount (address common.Address , auth string ) error {
10386 return am .keyStore .DeleteKey (address , auth )
10487}
@@ -114,28 +97,58 @@ func (am *Manager) Sign(a Account, toSign []byte) (signature []byte, err error)
11497 return signature , err
11598}
11699
117- // TimedUnlock unlocks the account with the given address.
118- // When timeout has passed, the account will be locked again.
100+ // unlock indefinitely
101+ func (am * Manager ) Unlock (addr common.Address , keyAuth string ) error {
102+ return am .TimedUnlock (addr , keyAuth , 0 )
103+ }
104+
105+ // Unlock unlocks the account with the given address. The account
106+ // stays unlocked for the duration of timeout
107+ // it timeout is 0 the account is unlocked for the entire session
119108func (am * Manager ) TimedUnlock (addr common.Address , keyAuth string , timeout time.Duration ) error {
120109 key , err := am .keyStore .GetKey (addr , keyAuth )
121110 if err != nil {
122111 return err
123112 }
124- u := am .addUnlocked (addr , key )
125- go am .dropLater (addr , u , timeout )
113+ var u * unlocked
114+ am .mutex .Lock ()
115+ defer am .mutex .Unlock ()
116+ var found bool
117+ u , found = am .unlocked [addr ]
118+ if found {
119+ // terminate dropLater for this key to avoid unexpected drops.
120+ if u .abort != nil {
121+ close (u .abort )
122+ }
123+ }
124+ if timeout > 0 {
125+ u = & unlocked {Key : key , abort : make (chan struct {})}
126+ go am .expire (addr , u , timeout )
127+ } else {
128+ u = & unlocked {Key : key }
129+ }
130+ am .unlocked [addr ] = u
126131 return nil
127132}
128133
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
134+ func (am * Manager ) expire (addr common.Address , u * unlocked , timeout time.Duration ) {
135+ t := time .NewTimer (timeout )
136+ defer t .Stop ()
137+ select {
138+ case <- u .abort :
139+ // just quit
140+ case <- t .C :
141+ am .mutex .Lock ()
142+ // only drop if it's still the same key instance that dropLater
143+ // was launched with. we can check that using pointer equality
144+ // because the map stores a new pointer every time the key is
145+ // unlocked.
146+ if am .unlocked [addr ] == u {
147+ zeroKey (u .PrivateKey )
148+ delete (am .unlocked , addr )
149+ }
150+ am .mutex .Unlock ()
136151 }
137- am .addUnlocked (addr , key )
138- return nil
139152}
140153
141154func (am * Manager ) NewAccount (auth string ) (Account , error ) {
@@ -146,6 +159,20 @@ func (am *Manager) NewAccount(auth string) (Account, error) {
146159 return Account {Address : key .Address }, nil
147160}
148161
162+ func (am * Manager ) AddressByIndex (index int ) (addr string , err error ) {
163+ var addrs []common.Address
164+ addrs , err = am .keyStore .GetKeyAddresses ()
165+ if err != nil {
166+ return
167+ }
168+ if index < 0 || index >= len (addrs ) {
169+ err = fmt .Errorf ("index out of range: %d (should be 0-%d)" , index , len (addrs )- 1 )
170+ } else {
171+ addr = addrs [index ].Hex ()
172+ }
173+ return
174+ }
175+
149176func (am * Manager ) Accounts () ([]Account , error ) {
150177 addresses , err := am .keyStore .GetKeyAddresses ()
151178 if os .IsNotExist (err ) {
@@ -162,43 +189,6 @@ func (am *Manager) Accounts() ([]Account, error) {
162189 return accounts , err
163190}
164191
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-
202192// zeroKey zeroes a private key in memory.
203193func zeroKey (k * ecdsa.PrivateKey ) {
204194 b := k .D .Bits ()
@@ -229,6 +219,19 @@ func (am *Manager) Import(path string, keyAuth string) (Account, error) {
229219 return Account {Address : key .Address }, nil
230220}
231221
222+ func (am * Manager ) Update (addr common.Address , authFrom , authTo string ) (err error ) {
223+ var key * crypto.Key
224+ key , err = am .keyStore .GetKey (addr , authFrom )
225+
226+ if err == nil {
227+ err = am .keyStore .StoreKey (key , authTo )
228+ if err == nil {
229+ am .keyStore .Cleanup (addr )
230+ }
231+ }
232+ return
233+ }
234+
232235func (am * Manager ) ImportPreSaleKey (keyJSON []byte , password string ) (acc Account , err error ) {
233236 var key * crypto.Key
234237 key , err = crypto .ImportPreSaleKey (am .keyStore , keyJSON , password )
0 commit comments