github.com/aquanetwork/aquachain@v1.7.8/aqua/accounts/keystore/keystore_plain.go (about)

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