Skip to content

Commit a4df9d7

Browse files
committed
accounts order by keyfile ctime
1 parent eb82ca4 commit a4df9d7

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

‎cmd/geth/js_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"runtime"
1010
"strconv"
1111
"testing"
12+
"time"
1213

1314
"github.com/ethereum/go-ethereum/accounts"
1415
"github.com/ethereum/go-ethereum/common"
@@ -20,8 +21,8 @@ import (
2021
"github.com/ethereum/go-ethereum/core/state"
2122
"github.com/ethereum/go-ethereum/crypto"
2223
"github.com/ethereum/go-ethereum/eth"
23-
"github.com/ethereum/go-ethereum/rpc/comms"
2424
"github.com/ethereum/go-ethereum/rpc/codec"
25+
"github.com/ethereum/go-ethereum/rpc/comms"
2526
)
2627

2728
const (
@@ -141,7 +142,6 @@ func TestAccounts(t *testing.T) {
141142

142143
checkEvalJSON(t, repl, `eth.accounts`, `["`+testAddress+`"]`)
143144
checkEvalJSON(t, repl, `eth.coinbase`, `null`)
144-
145145
val, err := repl.re.Run(`personal.newAccount("password")`)
146146
if err != nil {
147147
t.Errorf("expected no error, got %v", err)
@@ -151,7 +151,7 @@ func TestAccounts(t *testing.T) {
151151
t.Errorf("address not hex: %q", addr)
152152
}
153153

154-
// checkEvalJSON(t, repl, `eth.accounts`, `["`+testAddress+`", "`+addr+`"]`)
154+
checkEvalJSON(t, repl, `eth.accounts`, `["`+testAddress+`","`+addr+`"]`)
155155
}
156156

157157
func TestBlockChain(t *testing.T) {

‎crypto/key_store_plain.go‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ import (
2727
"encoding/hex"
2828
"encoding/json"
2929
"fmt"
30-
"github.com/ethereum/go-ethereum/common"
3130
"io"
3231
"io/ioutil"
3332
"os"
3433
"path/filepath"
34+
"sort"
35+
"syscall"
36+
"time"
37+
38+
"github.com/ethereum/go-ethereum/common"
3539
)
3640

3741
// TODO: rename to KeyStore when replacing existing KeyStore
@@ -118,12 +122,31 @@ func GetKeyAddresses(keysDirPath string) (addresses []common.Address, err error)
118122
if err != nil {
119123
return nil, err
120124
}
125+
var kfis keyFileInfos
121126
for _, fileInfo := range fileInfos {
122-
address, err := hex.DecodeString(fileInfo.Name())
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)
123134
if err != nil {
124135
continue
125136
}
126137
addresses = append(addresses, common.BytesToAddress(address))
127138
}
128139
return addresses, err
129140
}
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

Comments
 (0)