github.com/ava-labs/subnet-evm@v0.6.4/accounts/keystore/plain.go (about)

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