github.com/klaytn/klaytn@v1.12.1/accounts/keystore/keystore_plain.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2015 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from accounts/keystore/keystore_plain.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package keystore 22 23 import ( 24 "encoding/json" 25 "fmt" 26 "os" 27 "path/filepath" 28 29 "github.com/klaytn/klaytn/common" 30 ) 31 32 type keyStorePlain struct { 33 keysDirPath string 34 } 35 36 func (ks keyStorePlain) GetKey(addr common.Address, filename, auth string) (Key, error) { 37 fd, err := os.Open(filename) 38 if err != nil { 39 return nil, err 40 } 41 defer fd.Close() 42 var key Key 43 keyv4 := new(KeyV4) 44 if err := json.NewDecoder(fd).Decode(keyv4); err != nil { 45 keyv3 := new(KeyV3) 46 if err := json.NewDecoder(fd).Decode(keyv3); err != nil { 47 return nil, err 48 } 49 key = keyv3 50 } else { 51 key = keyv4 52 } 53 if key.GetAddress() != addr { 54 return nil, fmt.Errorf("key content mismatch: have address %x, want %x", key.GetAddress(), addr) 55 } 56 return key, nil 57 } 58 59 func (ks keyStorePlain) StoreKey(filename string, key Key, auth string) error { 60 content, err := json.Marshal(key) 61 if err != nil { 62 return err 63 } 64 return writeKeyFile(filename, content) 65 } 66 67 func (ks keyStorePlain) JoinPath(filename string) string { 68 if filepath.IsAbs(filename) { 69 return filename 70 } 71 return filepath.Join(ks.keysDirPath, filename) 72 }