2016-04-14 11:18:24 -05:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05: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 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05: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 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2014-12-31 08:39:33 -06:00
|
|
|
|
2016-03-02 06:57:15 -06:00
|
|
|
package accounts
|
2014-12-31 08:39:33 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-05-12 07:24:11 -05:00
|
|
|
"path/filepath"
|
2015-06-21 16:17:17 -05:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2014-12-31 08:39:33 -06:00
|
|
|
)
|
|
|
|
|
2015-01-07 09:06:26 -06:00
|
|
|
type keyStorePlain struct {
|
2014-12-31 08:39:33 -06:00
|
|
|
keysDirPath string
|
|
|
|
}
|
|
|
|
|
2016-03-02 18:15:42 -06:00
|
|
|
func (ks keyStorePlain) GetKey(addr common.Address, filename, auth string) (*Key, error) {
|
|
|
|
fd, err := os.Open(filename)
|
2014-12-31 08:39:33 -06:00
|
|
|
if err != nil {
|
2016-02-16 15:28:11 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-02 18:15:42 -06:00
|
|
|
defer fd.Close()
|
2016-02-16 15:28:11 -06:00
|
|
|
key := new(Key)
|
2016-03-02 18:15:42 -06:00
|
|
|
if err := json.NewDecoder(fd).Decode(key); err != nil {
|
2016-02-16 15:28:11 -06:00
|
|
|
return nil, err
|
2014-12-31 08:39:33 -06:00
|
|
|
}
|
2016-03-02 18:15:42 -06:00
|
|
|
if key.Address != addr {
|
|
|
|
return nil, fmt.Errorf("key content mismatch: have address %x, want %x", key.Address, addr)
|
2015-07-02 16:58:00 -05:00
|
|
|
}
|
2016-03-02 18:15:42 -06:00
|
|
|
return key, nil
|
2014-12-31 08:39:33 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 18:15:42 -06:00
|
|
|
func (ks keyStorePlain) StoreKey(filename string, key *Key, auth string) error {
|
|
|
|
content, err := json.Marshal(key)
|
2014-12-31 08:39:33 -06:00
|
|
|
if err != nil {
|
2015-01-07 09:06:26 -06:00
|
|
|
return err
|
2014-12-31 08:39:33 -06:00
|
|
|
}
|
2016-03-02 18:15:42 -06:00
|
|
|
return writeKeyFile(filename, content)
|
2014-12-31 08:39:33 -06:00
|
|
|
}
|
2015-01-24 19:07:20 -06:00
|
|
|
|
2016-03-02 18:15:42 -06:00
|
|
|
func (ks keyStorePlain) JoinPath(filename string) string {
|
|
|
|
if filepath.IsAbs(filename) {
|
|
|
|
return filename
|
2015-07-02 16:58:00 -05:00
|
|
|
} else {
|
2016-03-02 18:15:42 -06:00
|
|
|
return filepath.Join(ks.keysDirPath, filename)
|
2015-01-24 19:07:20 -06:00
|
|
|
}
|
|
|
|
}
|