2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-01-25 03:07:20 +02:00
|
|
|
|
2015-07-07 06:08:16 +03:00
|
|
|
// Package implements a private key management facility.
|
|
|
|
//
|
|
|
|
// This abstracts part of a user's interaction with an account she controls.
|
2015-01-25 03:07:20 +02:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
2015-03-08 01:35:23 +02:00
|
|
|
"crypto/ecdsa"
|
2015-01-25 03:07:20 +02:00
|
|
|
crand "crypto/rand"
|
2016-03-03 02:15:42 +02:00
|
|
|
"encoding/json"
|
2015-02-25 18:29:23 +02:00
|
|
|
"errors"
|
2015-07-03 06:56:20 +03:00
|
|
|
"fmt"
|
2016-03-03 02:15:42 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2015-02-25 18:29:23 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
2015-02-26 12:16:01 +02:00
|
|
|
|
2015-04-02 22:14:25 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-01-25 03:07:20 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
)
|
|
|
|
|
2015-03-07 13:38:33 +02:00
|
|
|
var (
|
2016-03-03 02:15:42 +02:00
|
|
|
ErrLocked = errors.New("account is locked")
|
|
|
|
ErrNoMatch = errors.New("no key for given address or file")
|
2016-04-01 23:41:47 +03:00
|
|
|
ErrDecrypt = errors.New("could not decrypt key with given passphrase")
|
2015-03-07 13:38:33 +02:00
|
|
|
)
|
2015-02-25 18:29:23 +02:00
|
|
|
|
2015-01-28 06:12:57 +02:00
|
|
|
type Account struct {
|
2015-04-02 22:14:25 +03:00
|
|
|
Address common.Address
|
2016-03-03 02:15:42 +02:00
|
|
|
File string
|
2015-01-25 03:07:20 +02:00
|
|
|
}
|
|
|
|
|
2015-10-15 17:07:19 +03:00
|
|
|
func (acc *Account) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"` + acc.Address.Hex() + `"`), nil
|
|
|
|
}
|
|
|
|
|
2016-03-03 02:15:42 +02:00
|
|
|
func (acc *Account) UnmarshalJSON(raw []byte) error {
|
|
|
|
return json.Unmarshal(raw, &acc.Address)
|
|
|
|
}
|
|
|
|
|
2015-03-08 02:52:49 +02:00
|
|
|
type Manager struct {
|
2016-03-03 02:15:42 +02:00
|
|
|
cache *addrCache
|
2016-03-02 14:57:15 +02:00
|
|
|
keyStore keyStore
|
2016-03-03 02:15:42 +02:00
|
|
|
mu sync.RWMutex
|
2015-04-02 22:14:25 +03:00
|
|
|
unlocked map[common.Address]*unlocked
|
2015-03-08 02:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type unlocked struct {
|
2016-03-02 14:57:15 +02:00
|
|
|
*Key
|
2015-03-08 03:45:02 +02:00
|
|
|
abort chan struct{}
|
2015-01-25 03:07:20 +02:00
|
|
|
}
|
|
|
|
|
2016-03-02 14:57:15 +02:00
|
|
|
func NewManager(keydir string, scryptN, scryptP int) *Manager {
|
2016-03-03 02:15:42 +02:00
|
|
|
keydir, _ = filepath.Abs(keydir)
|
|
|
|
am := &Manager{keyStore: &keyStorePassphrase{keydir, scryptN, scryptP}}
|
|
|
|
am.init(keydir)
|
|
|
|
return am
|
2016-03-02 14:57:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPlaintextManager(keydir string) *Manager {
|
2016-03-03 02:15:42 +02:00
|
|
|
keydir, _ = filepath.Abs(keydir)
|
|
|
|
am := &Manager{keyStore: &keyStorePlain{keydir}}
|
|
|
|
am.init(keydir)
|
|
|
|
return am
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *Manager) init(keydir string) {
|
|
|
|
am.unlocked = make(map[common.Address]*unlocked)
|
|
|
|
am.cache = newAddrCache(keydir)
|
|
|
|
// TODO: In order for this finalizer to work, there must be no references
|
|
|
|
// to am. addrCache doesn't keep a reference but unlocked keys do,
|
|
|
|
// so the finalizer will not trigger until all timed unlocks have expired.
|
|
|
|
runtime.SetFinalizer(am, func(m *Manager) {
|
|
|
|
m.cache.close()
|
|
|
|
})
|
2015-01-25 03:07:20 +02:00
|
|
|
}
|
|
|
|
|
2016-03-03 02:09:16 +02:00
|
|
|
func (am *Manager) HasAddress(addr common.Address) bool {
|
2016-03-03 02:15:42 +02:00
|
|
|
return am.cache.hasAddress(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *Manager) Accounts() []Account {
|
|
|
|
return am.cache.accounts()
|
2015-03-10 00:02:43 +02:00
|
|
|
}
|
|
|
|
|
2016-03-03 02:09:16 +02:00
|
|
|
func (am *Manager) DeleteAccount(a Account, auth string) error {
|
2016-03-03 02:15:42 +02:00
|
|
|
// Decrypting the key isn't really necessary, but we do
|
|
|
|
// it anyway to check the password and zero out the key
|
|
|
|
// immediately afterwards.
|
|
|
|
a, key, err := am.getDecryptedKey(a, auth)
|
|
|
|
if key != nil {
|
|
|
|
zeroKey(key.PrivateKey)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// The order is crucial here. The key is dropped from the
|
|
|
|
// cache after the file is gone so that a reload happening in
|
|
|
|
// between won't insert it into the cache again.
|
|
|
|
err = os.Remove(a.File)
|
|
|
|
if err == nil {
|
|
|
|
am.cache.delete(a)
|
|
|
|
}
|
|
|
|
return err
|
2015-02-24 19:03:10 +02:00
|
|
|
}
|
|
|
|
|
2015-03-08 02:52:49 +02:00
|
|
|
func (am *Manager) Sign(a Account, toSign []byte) (signature []byte, err error) {
|
2016-03-03 02:15:42 +02:00
|
|
|
am.mu.RLock()
|
|
|
|
defer am.mu.RUnlock()
|
2015-04-02 22:14:25 +03:00
|
|
|
unlockedKey, found := am.unlocked[a.Address]
|
2015-03-08 02:27:30 +02:00
|
|
|
if !found {
|
2015-02-25 18:29:23 +02:00
|
|
|
return nil, ErrLocked
|
|
|
|
}
|
|
|
|
signature, err = crypto.Sign(toSign, unlockedKey.PrivateKey)
|
|
|
|
return signature, err
|
|
|
|
}
|
|
|
|
|
2015-07-20 18:42:14 +03:00
|
|
|
// Unlock unlocks the given account indefinitely.
|
2016-03-03 02:09:16 +02:00
|
|
|
func (am *Manager) Unlock(a Account, keyAuth string) error {
|
|
|
|
return am.TimedUnlock(a, keyAuth, 0)
|
2015-06-18 17:12:39 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 17:07:19 +03:00
|
|
|
func (am *Manager) Lock(addr common.Address) error {
|
2016-03-03 02:15:42 +02:00
|
|
|
am.mu.Lock()
|
2015-10-15 17:07:19 +03:00
|
|
|
if unl, found := am.unlocked[addr]; found {
|
2016-03-03 02:15:42 +02:00
|
|
|
am.mu.Unlock()
|
2015-12-16 11:58:01 +02:00
|
|
|
am.expire(addr, unl, time.Duration(0)*time.Nanosecond)
|
2015-10-15 17:07:19 +03:00
|
|
|
} else {
|
2016-03-03 02:15:42 +02:00
|
|
|
am.mu.Unlock()
|
2015-10-15 17:07:19 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-20 18:42:14 +03:00
|
|
|
// TimedUnlock unlocks the account with the given address. The account
|
|
|
|
// stays unlocked for the duration of timeout. A timeout of 0 unlocks the account
|
|
|
|
// until the program exits.
|
|
|
|
//
|
|
|
|
// If the accout is already unlocked, TimedUnlock extends or shortens
|
|
|
|
// the active unlock timeout.
|
2016-03-03 02:09:16 +02:00
|
|
|
func (am *Manager) TimedUnlock(a Account, keyAuth string, timeout time.Duration) error {
|
2016-03-03 02:15:42 +02:00
|
|
|
_, key, err := am.getDecryptedKey(a, keyAuth)
|
2015-01-25 03:07:20 +02:00
|
|
|
if err != nil {
|
2015-03-10 01:09:39 +02:00
|
|
|
return err
|
2015-01-25 03:07:20 +02:00
|
|
|
}
|
2016-03-03 02:15:42 +02:00
|
|
|
|
|
|
|
am.mu.Lock()
|
|
|
|
defer am.mu.Unlock()
|
|
|
|
u, found := am.unlocked[a.Address]
|
2015-06-18 17:12:39 +03:00
|
|
|
if found {
|
|
|
|
// terminate dropLater for this key to avoid unexpected drops.
|
|
|
|
if u.abort != nil {
|
|
|
|
close(u.abort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if timeout > 0 {
|
|
|
|
u = &unlocked{Key: key, abort: make(chan struct{})}
|
2016-03-03 02:09:16 +02:00
|
|
|
go am.expire(a.Address, u, timeout)
|
2015-06-18 17:12:39 +03:00
|
|
|
} else {
|
|
|
|
u = &unlocked{Key: key}
|
|
|
|
}
|
2016-03-03 02:09:16 +02:00
|
|
|
am.unlocked[a.Address] = u
|
2015-03-10 01:09:39 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-03 02:15:42 +02:00
|
|
|
func (am *Manager) getDecryptedKey(a Account, auth string) (Account, *Key, error) {
|
|
|
|
am.cache.maybeReload()
|
|
|
|
am.cache.mu.Lock()
|
|
|
|
a, err := am.cache.find(a)
|
|
|
|
am.cache.mu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return a, nil, err
|
|
|
|
}
|
|
|
|
key, err := am.keyStore.GetKey(a.Address, a.File, auth)
|
|
|
|
return a, key, err
|
|
|
|
}
|
|
|
|
|
2015-06-18 17:12:39 +03:00
|
|
|
func (am *Manager) expire(addr common.Address, u *unlocked, timeout time.Duration) {
|
|
|
|
t := time.NewTimer(timeout)
|
|
|
|
defer t.Stop()
|
|
|
|
select {
|
|
|
|
case <-u.abort:
|
|
|
|
// just quit
|
|
|
|
case <-t.C:
|
2016-03-03 02:15:42 +02:00
|
|
|
am.mu.Lock()
|
2015-06-18 17:12:39 +03:00
|
|
|
// only drop if it's still the same key instance that dropLater
|
|
|
|
// was launched with. we can check that using pointer equality
|
|
|
|
// because the map stores a new pointer every time the key is
|
|
|
|
// unlocked.
|
|
|
|
if am.unlocked[addr] == u {
|
|
|
|
zeroKey(u.PrivateKey)
|
|
|
|
delete(am.unlocked, addr)
|
|
|
|
}
|
2016-03-03 02:15:42 +02:00
|
|
|
am.mu.Unlock()
|
2015-03-10 01:09:39 +02:00
|
|
|
}
|
2015-01-25 03:07:20 +02:00
|
|
|
}
|
|
|
|
|
2015-03-08 02:52:49 +02:00
|
|
|
func (am *Manager) NewAccount(auth string) (Account, error) {
|
2016-03-03 02:15:42 +02:00
|
|
|
_, account, err := storeNewKey(am.keyStore, crand.Reader, auth)
|
2015-01-25 03:07:20 +02:00
|
|
|
if err != nil {
|
2015-03-08 01:18:13 +02:00
|
|
|
return Account{}, err
|
2015-01-25 03:07:20 +02:00
|
|
|
}
|
2016-03-03 02:15:42 +02:00
|
|
|
// Add the account to the cache immediately rather
|
|
|
|
// than waiting for file system notifications to pick it up.
|
|
|
|
am.cache.add(account)
|
|
|
|
return account, nil
|
2015-01-25 03:07:20 +02:00
|
|
|
}
|
|
|
|
|
2016-03-03 02:09:16 +02:00
|
|
|
func (am *Manager) AccountByIndex(index int) (Account, error) {
|
2016-03-03 02:15:42 +02:00
|
|
|
accounts := am.Accounts()
|
|
|
|
if index < 0 || index >= len(accounts) {
|
|
|
|
return Account{}, fmt.Errorf("account index %d out of range [0, %d]", index, len(accounts)-1)
|
2015-03-08 02:27:30 +02:00
|
|
|
}
|
2016-03-03 02:15:42 +02:00
|
|
|
return accounts[index], nil
|
2015-02-25 18:29:23 +02:00
|
|
|
}
|
2015-03-23 15:00:06 +02:00
|
|
|
|
2015-03-26 20:55:39 +02:00
|
|
|
// USE WITH CAUTION = this will save an unencrypted private key on disk
|
|
|
|
// no cli or js interface
|
2016-03-03 02:09:16 +02:00
|
|
|
func (am *Manager) Export(path string, a Account, keyAuth string) error {
|
2016-03-03 02:15:42 +02:00
|
|
|
_, key, err := am.getDecryptedKey(a, keyAuth)
|
2015-03-23 15:00:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return crypto.SaveECDSA(path, key.PrivateKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *Manager) Import(path string, keyAuth string) (Account, error) {
|
2016-03-02 14:57:15 +02:00
|
|
|
priv, err := crypto.LoadECDSA(path)
|
2015-03-23 15:00:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return Account{}, err
|
|
|
|
}
|
2016-03-02 14:57:15 +02:00
|
|
|
return am.ImportECDSA(priv, keyAuth)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *Manager) ImportECDSA(priv *ecdsa.PrivateKey, keyAuth string) (Account, error) {
|
|
|
|
key := newKeyFromECDSA(priv)
|
2016-03-03 02:15:42 +02:00
|
|
|
a := Account{Address: key.Address, File: am.keyStore.JoinPath(keyFileName(key.Address))}
|
|
|
|
if err := am.keyStore.StoreKey(a.File, key, keyAuth); err != nil {
|
2015-03-23 15:00:06 +02:00
|
|
|
return Account{}, err
|
|
|
|
}
|
2016-03-03 02:15:42 +02:00
|
|
|
am.cache.add(a)
|
|
|
|
return a, nil
|
2015-03-23 15:00:06 +02:00
|
|
|
}
|
2015-03-24 18:19:11 +02:00
|
|
|
|
2016-03-03 02:15:42 +02:00
|
|
|
func (am *Manager) Update(a Account, authFrom, authTo string) error {
|
|
|
|
a, key, err := am.getDecryptedKey(a, authFrom)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-03 06:56:20 +03:00
|
|
|
}
|
2016-03-03 02:15:42 +02:00
|
|
|
return am.keyStore.StoreKey(a.File, key, authTo)
|
2015-07-03 06:56:20 +03:00
|
|
|
}
|
|
|
|
|
2016-03-03 02:15:42 +02:00
|
|
|
func (am *Manager) ImportPreSaleKey(keyJSON []byte, password string) (Account, error) {
|
|
|
|
a, _, err := importPreSaleKey(am.keyStore, keyJSON, password)
|
2015-03-24 18:19:11 +02:00
|
|
|
if err != nil {
|
2016-03-03 02:15:42 +02:00
|
|
|
return a, err
|
|
|
|
}
|
|
|
|
am.cache.add(a)
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// zeroKey zeroes a private key in memory.
|
|
|
|
func zeroKey(k *ecdsa.PrivateKey) {
|
|
|
|
b := k.D.Bits()
|
|
|
|
for i := range b {
|
|
|
|
b[i] = 0
|
2015-03-24 18:19:11 +02:00
|
|
|
}
|
|
|
|
}
|