@@ -31,18 +31,15 @@ import (
3131 "io/ioutil"
3232 "os"
3333 "path/filepath"
34- "sort"
35- "syscall"
3634 "time"
3735
3836 "github.com/ethereum/go-ethereum/common"
3937)
4038
41- // TODO: rename to KeyStore when replacing existing KeyStore
42- type KeyStore2 interface {
39+ type KeyStore interface {
4340 // create new key using io.Reader entropy source and optionally using auth string
4441 GenerateNewKey (io.Reader , string ) (* Key , error )
45- GetKey (common.Address , string ) (* Key , error ) // key from addr and auth string
42+ GetKey (common.Address , string ) (* Key , error ) // get key from addr and auth string
4643 GetKeyAddresses () ([]common.Address , error ) // get all addresses
4744 StoreKey (* Key , string ) error // store key optionally using auth string
4845 DeleteKey (common.Address , string ) error // delete key by addr and auth string
@@ -52,15 +49,15 @@ type keyStorePlain struct {
5249 keysDirPath string
5350}
5451
55- func NewKeyStorePlain (path string ) KeyStore2 {
52+ func NewKeyStorePlain (path string ) KeyStore {
5653 return & keyStorePlain {path }
5754}
5855
5956func (ks keyStorePlain ) GenerateNewKey (rand io.Reader , auth string ) (key * Key , err error ) {
6057 return GenerateNewKeyDefault (ks , rand , auth )
6158}
6259
63- func GenerateNewKeyDefault (ks KeyStore2 , rand io.Reader , auth string ) (key * Key , err error ) {
60+ func GenerateNewKeyDefault (ks KeyStore , rand io.Reader , auth string ) (key * Key , err error ) {
6461 defer func () {
6562 if r := recover (); r != nil {
6663 err = fmt .Errorf ("GenerateNewKey error: %v" , r )
@@ -72,81 +69,111 @@ func GenerateNewKeyDefault(ks KeyStore2, rand io.Reader, auth string) (key *Key,
7269}
7370
7471func (ks keyStorePlain ) GetKey (keyAddr common.Address , auth string ) (key * Key , err error ) {
75- fileContent , err := GetKeyFile (ks .keysDirPath , keyAddr )
72+ key = new (Key )
73+ err = getKey (ks .keysDirPath , keyAddr , key )
74+ return
75+ }
76+
77+ func getKey (keysDirPath string , keyAddr common.Address , content interface {}) (err error ) {
78+ fileContent , err := getKeyFile (keysDirPath , keyAddr )
7679 if err != nil {
77- return nil , err
80+ return
7881 }
79-
80- key = new (Key )
81- err = json .Unmarshal (fileContent , key )
82- return key , err
82+ return json .Unmarshal (fileContent , content )
8383}
8484
8585func (ks keyStorePlain ) GetKeyAddresses () (addresses []common.Address , err error ) {
86- return GetKeyAddresses (ks .keysDirPath )
86+ return getKeyAddresses (ks .keysDirPath )
8787}
8888
8989func (ks keyStorePlain ) StoreKey (key * Key , auth string ) (err error ) {
9090 keyJSON , err := json .Marshal (key )
9191 if err != nil {
92- return err
92+ return
9393 }
94- err = WriteKeyFile (key .Address , ks .keysDirPath , keyJSON )
95- return err
94+ err = writeKeyFile (key .Address , ks .keysDirPath , keyJSON )
95+ return
9696}
9797
9898func (ks keyStorePlain ) DeleteKey (keyAddr common.Address , auth string ) (err error ) {
99- keyDirPath := filepath .Join (ks .keysDirPath , keyAddr .Hex ())
100- err = os .RemoveAll (keyDirPath )
101- return err
99+ return deleteKey (ks .keysDirPath , keyAddr )
100+ }
101+
102+ func deleteKey (keysDirPath string , keyAddr common.Address ) (err error ) {
103+ var keyFilePath string
104+ keyFilePath , err = getKeyFilePath (keysDirPath , keyAddr )
105+ if err == nil {
106+ err = os .Remove (keyFilePath )
107+ }
108+ return
109+ }
110+
111+ func getKeyFilePath (keysDirPath string , keyAddr common.Address ) (keyFilePath string , err error ) {
112+ addrHex := hex .EncodeToString (keyAddr [:])
113+ matches , err := filepath .Glob (filepath .Join (keysDirPath , fmt .Sprintf ("*--%s" , addrHex )))
114+ if len (matches ) > 0 {
115+ if err == nil {
116+ keyFilePath = matches [len (matches )- 1 ]
117+ }
118+ return
119+ }
120+ keyFilePath = filepath .Join (keysDirPath , addrHex , addrHex )
121+ _ , err = os .Stat (keyFilePath )
122+ return
102123}
103124
104- func GetKeyFile (keysDirPath string , keyAddr common.Address ) (fileContent []byte , err error ) {
105- fileName := hex .EncodeToString (keyAddr [:])
106- return ioutil .ReadFile (filepath .Join (keysDirPath , fileName , fileName ))
125+ func getKeyFile (keysDirPath string , keyAddr common.Address ) (fileContent []byte , err error ) {
126+ var keyFilePath string
127+ keyFilePath , err = getKeyFilePath (keysDirPath , keyAddr )
128+ if err == nil {
129+ fileContent , err = ioutil .ReadFile (keyFilePath )
130+ }
131+ return
107132}
108133
109- func WriteKeyFile (addr common.Address , keysDirPath string , content []byte ) (err error ) {
110- addrHex := hex .EncodeToString (addr [:])
111- keyDirPath := filepath .Join (keysDirPath , addrHex )
112- keyFilePath := filepath .Join (keyDirPath , addrHex )
113- err = os .MkdirAll (keyDirPath , 0700 ) // read, write and dir search for user
134+ func writeKeyFile (addr common.Address , keysDirPath string , content []byte ) (err error ) {
135+ filename := keyFileName (addr )
136+ // read, write and dir search for user
137+ err = os .MkdirAll (keysDirPath , 0700 )
114138 if err != nil {
115139 return err
116140 }
117- return ioutil .WriteFile (keyFilePath , content , 0600 ) // read, write for user
141+ // read, write for user
142+ return ioutil .WriteFile (filepath .Join (keysDirPath , filename ), content , 0600 )
118143}
119144
120- func GetKeyAddresses (keysDirPath string ) (addresses []common.Address , err error ) {
145+ // keyFilePath implements the naming convention for keyfiles:
146+ // UTC--<created_at UTC ISO8601>-<address hex>
147+ func keyFileName (keyAddr common.Address ) string {
148+ ts := time .Now ().UTC ()
149+ return fmt .Sprintf ("UTC--%s--%s" , toISO8601 (ts ), hex .EncodeToString (keyAddr [:]))
150+ }
151+
152+ func toISO8601 (t time.Time ) string {
153+ var tz string
154+ name , offset := t .Zone ()
155+ if name == "UTC" {
156+ tz = "Z"
157+ } else {
158+ tz = fmt .Sprintf ("%03d00" , offset / 3600 )
159+ }
160+ return fmt .Sprintf ("%04d-%02d-%02dT%02d:%02d:%02d.%09d%s" , t .Year (), t .Month (), t .Day (), t .Hour (), t .Minute (), t .Second (), t .Nanosecond (), tz )
161+ }
162+
163+ func getKeyAddresses (keysDirPath string ) (addresses []common.Address , err error ) {
121164 fileInfos , err := ioutil .ReadDir (keysDirPath )
122165 if err != nil {
123166 return nil , err
124167 }
125- var kfis keyFileInfos
126168 for _ , fileInfo := range fileInfos {
127- stat := fileInfo .Sys ().(* syscall.Stat_t )
128- ctime := time .Unix (int64 (stat .Ctimespec .Sec ), int64 (stat .Ctimespec .Nsec ))
129- kfis = append (kfis , keyFileInfo {fileInfo .Name (), ctime })
130- }
131- sort .Sort (kfis )
132- for _ , kfi := range kfis {
133- address , err := hex .DecodeString (kfi .name )
134- if err != nil {
135- continue
169+ filename := fileInfo .Name ()
170+ if len (filename ) >= 40 {
171+ addr := filename [len (filename )- 40 : len (filename )]
172+ address , err := hex .DecodeString (addr )
173+ if err == nil {
174+ addresses = append (addresses , common .BytesToAddress (address ))
175+ }
136176 }
137- addresses = append (addresses , common .BytesToAddress (address ))
138177 }
139178 return addresses , err
140179}
141-
142- type keyFileInfo struct {
143- name string
144- ctime time.Time
145- }
146- type keyFileInfos []keyFileInfo
147-
148- func (a keyFileInfos ) Len () int { return len (a ) }
149- func (a keyFileInfos ) Swap (i , j int ) { a [i ], a [j ] = a [j ], a [i ] }
150- func (a keyFileInfos ) Less (i , j int ) bool {
151- return a [i ].ctime .Before (a [j ].ctime )
152- }
0 commit comments