2017-04-14 03:29:00 -05:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
2016-03-02 18:15:42 -06:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-01-24 03:49:20 -06:00
|
|
|
package keystore
|
2016-03-02 18:15:42 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-01-24 03:49:20 -06:00
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
2016-03-02 18:15:42 -06:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-02-22 06:10:07 -06:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-10-09 05:40:50 -05:00
|
|
|
"gopkg.in/fatih/set.v0"
|
2016-03-02 18:15:42 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Minimum amount of time between cache reloads. This limit applies if the platform does
|
|
|
|
// not support change notifications. It also applies if the keystore directory does not
|
|
|
|
// exist yet, the code will attempt to create a watcher at most this often.
|
|
|
|
const minReloadInterval = 2 * time.Second
|
|
|
|
|
2017-02-07 04:47:34 -06:00
|
|
|
type accountsByURL []accounts.Account
|
2016-03-02 18:15:42 -06:00
|
|
|
|
2017-02-07 04:47:34 -06:00
|
|
|
func (s accountsByURL) Len() int { return len(s) }
|
2017-02-08 07:53:02 -06:00
|
|
|
func (s accountsByURL) Less(i, j int) bool { return s[i].URL.Cmp(s[j].URL) < 0 }
|
2017-02-07 04:47:34 -06:00
|
|
|
func (s accountsByURL) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
2016-03-02 18:15:42 -06:00
|
|
|
|
|
|
|
// AmbiguousAddrError is returned when attempting to unlock
|
|
|
|
// an address for which more than one file exists.
|
|
|
|
type AmbiguousAddrError struct {
|
|
|
|
Addr common.Address
|
2017-01-24 03:49:20 -06:00
|
|
|
Matches []accounts.Account
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (err *AmbiguousAddrError) Error() string {
|
|
|
|
files := ""
|
|
|
|
for i, a := range err.Matches {
|
2017-02-08 07:53:02 -06:00
|
|
|
files += a.URL.Path
|
2016-03-02 18:15:42 -06:00
|
|
|
if i < len(err.Matches)-1 {
|
|
|
|
files += ", "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("multiple keys match address (%s)", files)
|
|
|
|
}
|
|
|
|
|
2017-02-07 04:47:34 -06:00
|
|
|
// accountCache is a live index of all accounts in the keystore.
|
|
|
|
type accountCache struct {
|
2016-03-02 18:15:42 -06:00
|
|
|
keydir string
|
|
|
|
watcher *watcher
|
|
|
|
mu sync.Mutex
|
2017-02-07 04:47:34 -06:00
|
|
|
all accountsByURL
|
2017-01-24 03:49:20 -06:00
|
|
|
byAddr map[common.Address][]accounts.Account
|
2016-03-02 18:15:42 -06:00
|
|
|
throttle *time.Timer
|
2017-02-07 04:47:34 -06:00
|
|
|
notify chan struct{}
|
2017-10-09 05:40:50 -05:00
|
|
|
fileC fileCache
|
|
|
|
}
|
|
|
|
|
|
|
|
// fileCache is a cache of files seen during scan of keystore
|
|
|
|
type fileCache struct {
|
|
|
|
all *set.SetNonTS // list of all files
|
|
|
|
mtime time.Time // latest mtime seen
|
|
|
|
mu sync.RWMutex
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
|
|
|
|
2017-02-07 04:47:34 -06:00
|
|
|
func newAccountCache(keydir string) (*accountCache, chan struct{}) {
|
|
|
|
ac := &accountCache{
|
2016-03-02 18:15:42 -06:00
|
|
|
keydir: keydir,
|
2017-01-24 03:49:20 -06:00
|
|
|
byAddr: make(map[common.Address][]accounts.Account),
|
2017-02-07 04:47:34 -06:00
|
|
|
notify: make(chan struct{}, 1),
|
2017-10-09 05:40:50 -05:00
|
|
|
fileC: fileCache{all: set.NewNonTS()},
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
|
|
|
ac.watcher = newWatcher(ac)
|
2017-02-07 04:47:34 -06:00
|
|
|
return ac, ac.notify
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
|
|
|
|
2017-02-07 04:47:34 -06:00
|
|
|
func (ac *accountCache) accounts() []accounts.Account {
|
2016-03-02 18:15:42 -06:00
|
|
|
ac.maybeReload()
|
|
|
|
ac.mu.Lock()
|
|
|
|
defer ac.mu.Unlock()
|
2017-01-24 03:49:20 -06:00
|
|
|
cpy := make([]accounts.Account, len(ac.all))
|
2016-03-02 18:15:42 -06:00
|
|
|
copy(cpy, ac.all)
|
|
|
|
return cpy
|
|
|
|
}
|
|
|
|
|
2017-02-07 04:47:34 -06:00
|
|
|
func (ac *accountCache) hasAddress(addr common.Address) bool {
|
2016-03-02 18:15:42 -06:00
|
|
|
ac.maybeReload()
|
|
|
|
ac.mu.Lock()
|
|
|
|
defer ac.mu.Unlock()
|
|
|
|
return len(ac.byAddr[addr]) > 0
|
|
|
|
}
|
|
|
|
|
2017-02-07 04:47:34 -06:00
|
|
|
func (ac *accountCache) add(newAccount accounts.Account) {
|
2016-03-02 18:15:42 -06:00
|
|
|
ac.mu.Lock()
|
|
|
|
defer ac.mu.Unlock()
|
|
|
|
|
2017-02-08 07:53:02 -06:00
|
|
|
i := sort.Search(len(ac.all), func(i int) bool { return ac.all[i].URL.Cmp(newAccount.URL) >= 0 })
|
2016-03-02 18:15:42 -06:00
|
|
|
if i < len(ac.all) && ac.all[i] == newAccount {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// newAccount is not in the cache.
|
2017-01-24 03:49:20 -06:00
|
|
|
ac.all = append(ac.all, accounts.Account{})
|
2016-03-02 18:15:42 -06:00
|
|
|
copy(ac.all[i+1:], ac.all[i:])
|
|
|
|
ac.all[i] = newAccount
|
|
|
|
ac.byAddr[newAccount.Address] = append(ac.byAddr[newAccount.Address], newAccount)
|
|
|
|
}
|
|
|
|
|
|
|
|
// note: removed needs to be unique here (i.e. both File and Address must be set).
|
2017-02-07 04:47:34 -06:00
|
|
|
func (ac *accountCache) delete(removed accounts.Account) {
|
2016-03-02 18:15:42 -06:00
|
|
|
ac.mu.Lock()
|
|
|
|
defer ac.mu.Unlock()
|
2017-02-07 04:47:34 -06:00
|
|
|
|
2016-03-02 18:15:42 -06:00
|
|
|
ac.all = removeAccount(ac.all, removed)
|
|
|
|
if ba := removeAccount(ac.byAddr[removed.Address], removed); len(ba) == 0 {
|
|
|
|
delete(ac.byAddr, removed.Address)
|
|
|
|
} else {
|
|
|
|
ac.byAddr[removed.Address] = ba
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 05:40:50 -05:00
|
|
|
// deleteByFile removes an account referenced by the given path.
|
|
|
|
func (ac *accountCache) deleteByFile(path string) {
|
|
|
|
ac.mu.Lock()
|
|
|
|
defer ac.mu.Unlock()
|
|
|
|
i := sort.Search(len(ac.all), func(i int) bool { return ac.all[i].URL.Path >= path })
|
|
|
|
|
|
|
|
if i < len(ac.all) && ac.all[i].URL.Path == path {
|
|
|
|
removed := ac.all[i]
|
|
|
|
ac.all = append(ac.all[:i], ac.all[i+1:]...)
|
|
|
|
if ba := removeAccount(ac.byAddr[removed.Address], removed); len(ba) == 0 {
|
|
|
|
delete(ac.byAddr, removed.Address)
|
|
|
|
} else {
|
|
|
|
ac.byAddr[removed.Address] = ba
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 03:49:20 -06:00
|
|
|
func removeAccount(slice []accounts.Account, elem accounts.Account) []accounts.Account {
|
2016-03-02 18:15:42 -06:00
|
|
|
for i := range slice {
|
|
|
|
if slice[i] == elem {
|
|
|
|
return append(slice[:i], slice[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
|
|
|
// find returns the cached account for address if there is a unique match.
|
2017-01-24 03:49:20 -06:00
|
|
|
// The exact matching rules are explained by the documentation of accounts.Account.
|
2016-03-02 18:15:42 -06:00
|
|
|
// Callers must hold ac.mu.
|
2017-02-07 04:47:34 -06:00
|
|
|
func (ac *accountCache) find(a accounts.Account) (accounts.Account, error) {
|
2016-03-02 18:15:42 -06:00
|
|
|
// Limit search to address candidates if possible.
|
|
|
|
matches := ac.all
|
|
|
|
if (a.Address != common.Address{}) {
|
|
|
|
matches = ac.byAddr[a.Address]
|
|
|
|
}
|
2017-02-08 07:53:02 -06:00
|
|
|
if a.URL.Path != "" {
|
2016-03-02 18:15:42 -06:00
|
|
|
// If only the basename is specified, complete the path.
|
2017-02-08 07:53:02 -06:00
|
|
|
if !strings.ContainsRune(a.URL.Path, filepath.Separator) {
|
|
|
|
a.URL.Path = filepath.Join(ac.keydir, a.URL.Path)
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
|
|
|
for i := range matches {
|
2017-01-24 03:49:20 -06:00
|
|
|
if matches[i].URL == a.URL {
|
2016-03-02 18:15:42 -06:00
|
|
|
return matches[i], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (a.Address == common.Address{}) {
|
2017-01-24 03:49:20 -06:00
|
|
|
return accounts.Account{}, ErrNoMatch
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
switch len(matches) {
|
|
|
|
case 1:
|
|
|
|
return matches[0], nil
|
|
|
|
case 0:
|
2017-01-24 03:49:20 -06:00
|
|
|
return accounts.Account{}, ErrNoMatch
|
2016-03-02 18:15:42 -06:00
|
|
|
default:
|
2017-01-24 03:49:20 -06:00
|
|
|
err := &AmbiguousAddrError{Addr: a.Address, Matches: make([]accounts.Account, len(matches))}
|
2016-03-02 18:15:42 -06:00
|
|
|
copy(err.Matches, matches)
|
2017-10-09 05:40:50 -05:00
|
|
|
sort.Sort(accountsByURL(err.Matches))
|
2017-01-24 03:49:20 -06:00
|
|
|
return accounts.Account{}, err
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-07 04:47:34 -06:00
|
|
|
func (ac *accountCache) maybeReload() {
|
2016-03-02 18:15:42 -06:00
|
|
|
ac.mu.Lock()
|
2017-02-07 04:47:34 -06:00
|
|
|
|
2016-03-02 18:15:42 -06:00
|
|
|
if ac.watcher.running {
|
2017-10-09 05:40:50 -05:00
|
|
|
ac.mu.Unlock()
|
2016-03-02 18:15:42 -06:00
|
|
|
return // A watcher is running and will keep the cache up-to-date.
|
|
|
|
}
|
|
|
|
if ac.throttle == nil {
|
|
|
|
ac.throttle = time.NewTimer(0)
|
|
|
|
} else {
|
|
|
|
select {
|
|
|
|
case <-ac.throttle.C:
|
|
|
|
default:
|
2017-10-09 05:40:50 -05:00
|
|
|
ac.mu.Unlock()
|
2016-03-02 18:15:42 -06:00
|
|
|
return // The cache was reloaded recently.
|
|
|
|
}
|
|
|
|
}
|
2017-10-09 05:40:50 -05:00
|
|
|
// No watcher running, start it.
|
2016-03-02 18:15:42 -06:00
|
|
|
ac.watcher.start()
|
|
|
|
ac.throttle.Reset(minReloadInterval)
|
2017-10-09 05:40:50 -05:00
|
|
|
ac.mu.Unlock()
|
|
|
|
ac.scanAccounts()
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
|
|
|
|
2017-02-07 04:47:34 -06:00
|
|
|
func (ac *accountCache) close() {
|
2016-03-02 18:15:42 -06:00
|
|
|
ac.mu.Lock()
|
|
|
|
ac.watcher.close()
|
|
|
|
if ac.throttle != nil {
|
|
|
|
ac.throttle.Stop()
|
|
|
|
}
|
2017-02-07 04:47:34 -06:00
|
|
|
if ac.notify != nil {
|
|
|
|
close(ac.notify)
|
|
|
|
ac.notify = nil
|
|
|
|
}
|
2016-03-02 18:15:42 -06:00
|
|
|
ac.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-10-09 05:40:50 -05:00
|
|
|
// scanFiles performs a new scan on the given directory, compares against the already
|
|
|
|
// cached filenames, and returns file sets: new, missing , modified
|
|
|
|
func (fc *fileCache) scanFiles(keyDir string) (set.Interface, set.Interface, set.Interface, error) {
|
|
|
|
t0 := time.Now()
|
|
|
|
files, err := ioutil.ReadDir(keyDir)
|
|
|
|
t1 := time.Now()
|
2017-02-22 06:10:07 -06:00
|
|
|
if err != nil {
|
2017-10-09 05:40:50 -05:00
|
|
|
return nil, nil, nil, err
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
2017-10-09 05:40:50 -05:00
|
|
|
fc.mu.RLock()
|
|
|
|
prevMtime := fc.mtime
|
|
|
|
fc.mu.RUnlock()
|
|
|
|
|
|
|
|
filesNow := set.NewNonTS()
|
|
|
|
moddedFiles := set.NewNonTS()
|
|
|
|
var newMtime time.Time
|
|
|
|
for _, fi := range files {
|
|
|
|
modTime := fi.ModTime()
|
|
|
|
path := filepath.Join(keyDir, fi.Name())
|
|
|
|
if skipKeyFile(fi) {
|
|
|
|
log.Trace("Ignoring file on account scan", "path", path)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
filesNow.Add(path)
|
|
|
|
if modTime.After(prevMtime) {
|
|
|
|
moddedFiles.Add(path)
|
|
|
|
}
|
|
|
|
if modTime.After(newMtime) {
|
|
|
|
newMtime = modTime
|
|
|
|
}
|
2017-02-07 04:47:34 -06:00
|
|
|
}
|
2017-10-09 05:40:50 -05:00
|
|
|
t2 := time.Now()
|
|
|
|
|
|
|
|
fc.mu.Lock()
|
|
|
|
// Missing = previous - current
|
|
|
|
missing := set.Difference(fc.all, filesNow)
|
|
|
|
// New = current - previous
|
|
|
|
newFiles := set.Difference(filesNow, fc.all)
|
|
|
|
// Modified = modified - new
|
|
|
|
modified := set.Difference(moddedFiles, newFiles)
|
|
|
|
fc.all = filesNow
|
|
|
|
fc.mtime = newMtime
|
|
|
|
fc.mu.Unlock()
|
|
|
|
t3 := time.Now()
|
|
|
|
log.Debug("FS scan times", "list", t1.Sub(t0), "set", t2.Sub(t1), "diff", t3.Sub(t2))
|
|
|
|
return newFiles, missing, modified, nil
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
|
|
|
|
2017-10-09 05:40:50 -05:00
|
|
|
// scanAccounts checks if any changes have occurred on the filesystem, and
|
|
|
|
// updates the account cache accordingly
|
|
|
|
func (ac *accountCache) scanAccounts() error {
|
|
|
|
newFiles, missingFiles, modified, err := ac.fileC.scanFiles(ac.keydir)
|
|
|
|
t1 := time.Now()
|
2016-03-02 18:15:42 -06:00
|
|
|
if err != nil {
|
2017-10-09 05:40:50 -05:00
|
|
|
log.Debug("Failed to reload keystore contents", "err", err)
|
|
|
|
return err
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
|
|
|
var (
|
|
|
|
buf = new(bufio.Reader)
|
|
|
|
keyJSON struct {
|
2016-11-27 18:30:54 -06:00
|
|
|
Address string `json:"address"`
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
|
|
|
)
|
2017-10-09 05:40:50 -05:00
|
|
|
readAccount := func(path string) *accounts.Account {
|
2016-03-02 18:15:42 -06:00
|
|
|
fd, err := os.Open(path)
|
|
|
|
if err != nil {
|
2017-10-09 05:40:50 -05:00
|
|
|
log.Trace("Failed to open keystore file", "path", path, "err", err)
|
|
|
|
return nil
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
2017-10-09 05:40:50 -05:00
|
|
|
defer fd.Close()
|
2016-03-02 18:15:42 -06:00
|
|
|
buf.Reset(fd)
|
|
|
|
// Parse the address.
|
2016-11-27 18:30:54 -06:00
|
|
|
keyJSON.Address = ""
|
2016-03-02 18:15:42 -06:00
|
|
|
err = json.NewDecoder(buf).Decode(&keyJSON)
|
2016-11-27 18:30:54 -06:00
|
|
|
addr := common.HexToAddress(keyJSON.Address)
|
2016-03-02 18:15:42 -06:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
2017-10-09 05:40:50 -05:00
|
|
|
log.Debug("Failed to decode keystore key", "path", path, "err", err)
|
2016-11-27 18:30:54 -06:00
|
|
|
case (addr == common.Address{}):
|
2017-10-09 05:40:50 -05:00
|
|
|
log.Debug("Failed to decode keystore key", "path", path, "err", "missing or zero address")
|
2016-03-02 18:15:42 -06:00
|
|
|
default:
|
2017-10-09 05:40:50 -05:00
|
|
|
return &accounts.Account{Address: addr, URL: accounts.URL{Scheme: KeyStoreScheme, Path: path}}
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
2017-10-09 05:40:50 -05:00
|
|
|
return nil
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
2017-10-09 05:40:50 -05:00
|
|
|
|
|
|
|
for _, p := range newFiles.List() {
|
|
|
|
path, _ := p.(string)
|
|
|
|
a := readAccount(path)
|
|
|
|
if a != nil {
|
|
|
|
ac.add(*a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, p := range missingFiles.List() {
|
|
|
|
path, _ := p.(string)
|
|
|
|
ac.deleteByFile(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range modified.List() {
|
|
|
|
path, _ := p.(string)
|
|
|
|
a := readAccount(path)
|
|
|
|
ac.deleteByFile(path)
|
|
|
|
if a != nil {
|
|
|
|
ac.add(*a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t2 := time.Now()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ac.notify <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
log.Trace("Handled keystore changes", "time", t2.Sub(t1))
|
|
|
|
|
|
|
|
return nil
|
2016-03-02 18:15:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func skipKeyFile(fi os.FileInfo) bool {
|
|
|
|
// Skip editor backups and UNIX-style hidden files.
|
|
|
|
if strings.HasSuffix(fi.Name(), "~") || strings.HasPrefix(fi.Name(), ".") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Skip misc special files, directories (yes, symlinks too).
|
|
|
|
if fi.IsDir() || fi.Mode()&os.ModeType != 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|