github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/accounts/keystore/keystore_plain.go (about)

     1  package keystore
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/neatlab/neatio/utilities/common"
    10  )
    11  
    12  type keyStorePlain struct {
    13  	keysDirPath string
    14  }
    15  
    16  func (ks keyStorePlain) GetKey(addr common.Address, filename, auth string) (*Key, error) {
    17  	fd, err := os.Open(filename)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	defer fd.Close()
    22  	key := new(Key)
    23  	if err := json.NewDecoder(fd).Decode(key); err != nil {
    24  		return nil, err
    25  	}
    26  	if key.Address != addr {
    27  		return nil, fmt.Errorf("key content mismatch: have address %x, want %x", key.Address, addr)
    28  	}
    29  	return key, nil
    30  }
    31  
    32  func (ks keyStorePlain) StoreKey(filename string, key *Key, auth string) error {
    33  	content, err := json.Marshal(key)
    34  	if err != nil {
    35  		return err
    36  	}
    37  	return writeKeyFile(filename, content)
    38  }
    39  
    40  func (ks keyStorePlain) JoinPath(filename string) string {
    41  	if filepath.IsAbs(filename) {
    42  		return filename
    43  	} else {
    44  		return filepath.Join(ks.keysDirPath, filename)
    45  	}
    46  }