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