2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00: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 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00: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 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-01-25 01:07:20 +00:00
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// Package accounts implements encrypted storage of secp256k1 private keys.
|
2015-07-07 03:08:16 +00:00
|
|
|
//
|
2016-04-04 23:08:50 +00:00
|
|
|
// Keys are stored as encrypted JSON files according to the Web3 Secret Storage specification.
|
|
|
|
// See https://github.com/ethereum/wiki/wiki/Web3-Secret-Storage-Definition for more information.
|
2015-01-25 01:07:20 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
2015-03-07 23:35:23 +00:00
|
|
|
"crypto/ecdsa"
|
2015-01-25 01:07:20 +00:00
|
|
|
crand "crypto/rand"
|
2016-03-03 00:15:42 +00:00
|
|
|
"encoding/json"
|
2015-02-25 16:29:23 +00:00
|
|
|
"errors"
|
2015-07-03 03:56:20 +00:00
|
|
|
"fmt"
|
2016-03-03 00:15:42 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2015-02-25 16:29:23 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2015-02-26 10:16:01 +00:00
|
|
|
|
2015-04-02 19:14:25 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-01-25 01:07:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
)
|
|
|
|
|
2015-03-07 11:38:33 +00:00
|
|
|
var (
|
2016-03-03 00:15:42 +00:00
|
|
|
ErrLocked = errors.New("account is locked")
|
|
|
|
ErrNoMatch = errors.New("no key for given address or file")
|
2016-04-01 20:41:47 +00:00
|
|
|
ErrDecrypt = errors.New("could not decrypt key with given passphrase")
|
2015-03-07 11:38:33 +00:00
|
|
|
)
|
2015-02-25 16:29:23 +00:00
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// Account represents a stored key.
|
|
|
|
// When used as an argument, it selects a unique key file to act on.
|
2015-01-28 04:12:57 +00:00
|
|
|
type Account struct {
|
2016-04-04 23:08:50 +00:00
|
|
|
Address common.Address // Ethereum account address derived from the key
|
|
|
|
|
|
|
|
// File contains the key file name.
|
|
|
|
// When Acccount is used as an argument to select a key, File can be left blank to
|
|
|
|
// select just by address or set to the basename or absolute path of a file in the key
|
|
|
|
// directory. Accounts returned by Manager will always contain an absolute path.
|
|
|
|
File string
|
2015-01-25 01:07:20 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 14:07:19 +00:00
|
|
|
func (acc *Account) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"` + acc.Address.Hex() + `"`), nil
|
|
|
|
}
|
|
|
|
|
2016-03-03 00:15:42 +00:00
|
|
|
func (acc *Account) UnmarshalJSON(raw []byte) error {
|
|
|
|
return json.Unmarshal(raw, &acc.Address)
|
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// Manager manages a key storage directory on disk.
|
2015-03-08 00:52:49 +00:00
|
|
|
type Manager struct {
|
2016-03-03 00:15:42 +00:00
|
|
|
cache *addrCache
|
2016-03-02 12:57:15 +00:00
|
|
|
keyStore keyStore
|
2016-03-03 00:15:42 +00:00
|
|
|
mu sync.RWMutex
|
2015-04-02 19:14:25 +00:00
|
|
|
unlocked map[common.Address]*unlocked
|
2015-03-08 00:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type unlocked struct {
|
2016-03-02 12:57:15 +00:00
|
|
|
*Key
|
2015-03-08 01:45:02 +00:00
|
|
|
abort chan struct{}
|
2015-01-25 01:07:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// NewManager creates a manager for the given directory.
|
2016-03-02 12:57:15 +00:00
|
|
|
func NewManager(keydir string, scryptN, scryptP int) *Manager {
|
2016-03-03 00:15:42 +00:00
|
|
|
keydir, _ = filepath.Abs(keydir)
|
|
|
|
am := &Manager{keyStore: &keyStorePassphrase{keydir, scryptN, scryptP}}
|
|
|
|
am.init(keydir)
|
|
|
|
return am
|
2016-03-02 12:57:15 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// NewPlaintextManager creates a manager for the given directory.
|
|
|
|
// Deprecated: Use NewManager.
|
2016-03-02 12:57:15 +00:00
|
|
|
func NewPlaintextManager(keydir string) *Manager {
|
2016-03-03 00:15:42 +00: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 01:07:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// HasAddress reports whether a key with the given address is present.
|
2016-03-03 00:09:16 +00:00
|
|
|
func (am *Manager) HasAddress(addr common.Address) bool {
|
2016-03-03 00:15:42 +00:00
|
|
|
return am.cache.hasAddress(addr)
|
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// Accounts returns all key files present in the directory.
|
2016-03-03 00:15:42 +00:00
|
|
|
func (am *Manager) Accounts() []Account {
|
|
|
|
return am.cache.accounts()
|
2015-03-09 22:02:43 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// DeleteAccount deletes the key matched by account if the passphrase is correct.
|
|
|
|
// If a contains no filename, the address must match a unique key.
|
|
|
|
func (am *Manager) DeleteAccount(a Account, passphrase string) error {
|
2016-03-03 00:15:42 +00:00
|
|
|
// Decrypting the key isn't really necessary, but we do
|
|
|
|
// it anyway to check the password and zero out the key
|
|
|
|
// immediately afterwards.
|
2016-04-04 23:08:50 +00:00
|
|
|
a, key, err := am.getDecryptedKey(a, passphrase)
|
2016-03-03 00:15:42 +00:00
|
|
|
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 17:03:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// Sign signs hash with an unlocked private key matching the given address.
|
|
|
|
func (am *Manager) Sign(addr common.Address, hash []byte) (signature []byte, err error) {
|
2016-03-03 00:15:42 +00:00
|
|
|
am.mu.RLock()
|
|
|
|
defer am.mu.RUnlock()
|
2016-04-04 23:08:50 +00:00
|
|
|
unlockedKey, found := am.unlocked[addr]
|
2015-03-08 00:27:30 +00:00
|
|
|
if !found {
|
2015-02-25 16:29:23 +00:00
|
|
|
return nil, ErrLocked
|
|
|
|
}
|
2016-04-04 23:08:50 +00:00
|
|
|
return crypto.Sign(hash, unlockedKey.PrivateKey)
|
2015-02-25 16:29:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-20 15:42:14 +00:00
|
|
|
// Unlock unlocks the given account indefinitely.
|
2016-03-03 00:09:16 +00:00
|
|
|
func (am *Manager) Unlock(a Account, keyAuth string) error {
|
|
|
|
return am.TimedUnlock(a, keyAuth, 0)
|
2015-06-18 14:12:39 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// Lock removes the private key with the given address from memory.
|
2015-10-15 14:07:19 +00:00
|
|
|
func (am *Manager) Lock(addr common.Address) error {
|
2016-03-03 00:15:42 +00:00
|
|
|
am.mu.Lock()
|
2015-10-15 14:07:19 +00:00
|
|
|
if unl, found := am.unlocked[addr]; found {
|
2016-03-03 00:15:42 +00:00
|
|
|
am.mu.Unlock()
|
2015-12-16 09:58:01 +00:00
|
|
|
am.expire(addr, unl, time.Duration(0)*time.Nanosecond)
|
2015-10-15 14:07:19 +00:00
|
|
|
} else {
|
2016-03-03 00:15:42 +00:00
|
|
|
am.mu.Unlock()
|
2015-10-15 14:07:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// TimedUnlock unlocks the given account with. The account
|
2015-07-20 15:42:14 +00:00
|
|
|
// stays unlocked for the duration of timeout. A timeout of 0 unlocks the account
|
2016-04-04 23:08:50 +00:00
|
|
|
// until the program exits. The account must match a unique key.
|
2015-07-20 15:42:14 +00:00
|
|
|
//
|
|
|
|
// If the accout is already unlocked, TimedUnlock extends or shortens
|
|
|
|
// the active unlock timeout.
|
2016-03-03 00:09:16 +00:00
|
|
|
func (am *Manager) TimedUnlock(a Account, keyAuth string, timeout time.Duration) error {
|
2016-03-03 00:15:42 +00:00
|
|
|
_, key, err := am.getDecryptedKey(a, keyAuth)
|
2015-01-25 01:07:20 +00:00
|
|
|
if err != nil {
|
2015-03-09 23:09:39 +00:00
|
|
|
return err
|
2015-01-25 01:07:20 +00:00
|
|
|
}
|
2016-03-03 00:15:42 +00:00
|
|
|
|
|
|
|
am.mu.Lock()
|
|
|
|
defer am.mu.Unlock()
|
|
|
|
u, found := am.unlocked[a.Address]
|
2015-06-18 14:12:39 +00: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 00:09:16 +00:00
|
|
|
go am.expire(a.Address, u, timeout)
|
2015-06-18 14:12:39 +00:00
|
|
|
} else {
|
|
|
|
u = &unlocked{Key: key}
|
|
|
|
}
|
2016-03-03 00:09:16 +00:00
|
|
|
am.unlocked[a.Address] = u
|
2015-03-09 23:09:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-03 00:15:42 +00: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 14:12:39 +00: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 00:15:42 +00:00
|
|
|
am.mu.Lock()
|
2015-06-18 14:12:39 +00: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 00:15:42 +00:00
|
|
|
am.mu.Unlock()
|
2015-03-09 23:09:39 +00:00
|
|
|
}
|
2015-01-25 01:07:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// NewAccount generates a new key and stores it into the key directory,
|
|
|
|
// encrypting it with the passphrase.
|
|
|
|
func (am *Manager) NewAccount(passphrase string) (Account, error) {
|
|
|
|
_, account, err := storeNewKey(am.keyStore, crand.Reader, passphrase)
|
2015-01-25 01:07:20 +00:00
|
|
|
if err != nil {
|
2015-03-07 23:18:13 +00:00
|
|
|
return Account{}, err
|
2015-01-25 01:07:20 +00:00
|
|
|
}
|
2016-03-03 00:15:42 +00: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 01:07:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// AccountByIndex returns the ith account.
|
|
|
|
func (am *Manager) AccountByIndex(i int) (Account, error) {
|
2016-03-03 00:15:42 +00:00
|
|
|
accounts := am.Accounts()
|
2016-04-04 23:08:50 +00:00
|
|
|
if i < 0 || i >= len(accounts) {
|
|
|
|
return Account{}, fmt.Errorf("account index %d out of range [0, %d]", i, len(accounts)-1)
|
2015-03-08 00:27:30 +00:00
|
|
|
}
|
2016-04-04 23:08:50 +00:00
|
|
|
return accounts[i], nil
|
2015-02-25 16:29:23 +00:00
|
|
|
}
|
2015-03-23 13:00:06 +00:00
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// Export exports as a JSON key, encrypted with newPassphrase.
|
|
|
|
func (am *Manager) Export(a Account, passphrase, newPassphrase string) (keyJSON []byte, err error) {
|
|
|
|
_, key, err := am.getDecryptedKey(a, passphrase)
|
2015-03-23 13:00:06 +00:00
|
|
|
if err != nil {
|
2016-04-04 23:08:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var N, P int
|
|
|
|
if store, ok := am.keyStore.(*keyStorePassphrase); ok {
|
|
|
|
N, P = store.scryptN, store.scryptP
|
|
|
|
} else {
|
|
|
|
N, P = StandardScryptN, StandardScryptP
|
2015-03-23 13:00:06 +00:00
|
|
|
}
|
2016-04-04 23:08:50 +00:00
|
|
|
return EncryptKey(key, newPassphrase, N, P)
|
2015-03-23 13:00:06 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// Import stores the given encrypted JSON key into the key directory.
|
|
|
|
func (am *Manager) Import(keyJSON []byte, passphrase, newPassphrase string) (Account, error) {
|
|
|
|
key, err := DecryptKey(keyJSON, passphrase)
|
|
|
|
if key != nil && key.PrivateKey != nil {
|
|
|
|
defer zeroKey(key.PrivateKey)
|
|
|
|
}
|
2015-03-23 13:00:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return Account{}, err
|
|
|
|
}
|
2016-04-04 23:08:50 +00:00
|
|
|
return am.importKey(key, newPassphrase)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImportECDSA stores the given key into the key directory, encrypting it with the passphrase.
|
|
|
|
func (am *Manager) ImportECDSA(priv *ecdsa.PrivateKey, passphrase string) (Account, error) {
|
|
|
|
return am.importKey(newKeyFromECDSA(priv), passphrase)
|
2016-03-02 12:57:15 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
func (am *Manager) importKey(key *Key, passphrase string) (Account, error) {
|
2016-03-03 00:15:42 +00:00
|
|
|
a := Account{Address: key.Address, File: am.keyStore.JoinPath(keyFileName(key.Address))}
|
2016-04-04 23:08:50 +00:00
|
|
|
if err := am.keyStore.StoreKey(a.File, key, passphrase); err != nil {
|
2015-03-23 13:00:06 +00:00
|
|
|
return Account{}, err
|
|
|
|
}
|
2016-03-03 00:15:42 +00:00
|
|
|
am.cache.add(a)
|
|
|
|
return a, nil
|
2015-03-23 13:00:06 +00:00
|
|
|
}
|
2015-03-24 16:19:11 +00:00
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// Update changes the passphrase of an existing account.
|
|
|
|
func (am *Manager) Update(a Account, passphrase, newPassphrase string) error {
|
|
|
|
a, key, err := am.getDecryptedKey(a, passphrase)
|
2016-03-03 00:15:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-03 03:56:20 +00:00
|
|
|
}
|
2016-04-04 23:08:50 +00:00
|
|
|
return am.keyStore.StoreKey(a.File, key, newPassphrase)
|
2015-07-03 03:56:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 23:08:50 +00:00
|
|
|
// ImportPreSaleKey decrypts the given Ethereum presale wallet and stores
|
|
|
|
// a key file in the key directory. The key file is encrypted with the same passphrase.
|
|
|
|
func (am *Manager) ImportPreSaleKey(keyJSON []byte, passphrase string) (Account, error) {
|
|
|
|
a, _, err := importPreSaleKey(am.keyStore, keyJSON, passphrase)
|
2015-03-24 16:19:11 +00:00
|
|
|
if err != nil {
|
2016-03-03 00:15:42 +00: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 16:19:11 +00:00
|
|
|
}
|
|
|
|
}
|