github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/accounts/keystore/keystore_plain.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package keystore 13 14 import ( 15 "encoding/json" 16 "fmt" 17 "os" 18 "path/filepath" 19 20 "github.com/Sberex/go-sberex/common" 21 ) 22 23 type keyStorePlain struct { 24 keysDirPath string 25 } 26 27 func (ks keyStorePlain) GetKey(addr common.Address, filename, auth string) (*Key, error) { 28 fd, err := os.Open(filename) 29 if err != nil { 30 return nil, err 31 } 32 defer fd.Close() 33 key := new(Key) 34 if err := json.NewDecoder(fd).Decode(key); err != nil { 35 return nil, err 36 } 37 if key.Address != addr { 38 return nil, fmt.Errorf("key content mismatch: have address %x, want %x", key.Address, addr) 39 } 40 return key, nil 41 } 42 43 func (ks keyStorePlain) StoreKey(filename string, key *Key, auth string) error { 44 content, err := json.Marshal(key) 45 if err != nil { 46 return err 47 } 48 return writeKeyFile(filename, content) 49 } 50 51 func (ks keyStorePlain) JoinPath(filename string) string { 52 if filepath.IsAbs(filename) { 53 return filename 54 } else { 55 return filepath.Join(ks.keysDirPath, filename) 56 } 57 }