diff options
author | zelig <viktor.tron@gmail.com> | 2014-06-29 23:02:23 +0800 |
---|---|---|
committer | zelig <viktor.tron@gmail.com> | 2014-06-29 23:02:23 +0800 |
commit | 772e7e8c8df066a64726b23bde84b6025c5af318 (patch) | |
tree | 8ee0e9b19d6ce2747481a8890473db99c66b87cb /ethcrypto/key_store.go | |
parent | e1ea41ee9cbe387221874fa6732b11d262a4ff12 (diff) | |
download | go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.tar go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.tar.gz go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.tar.bz2 go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.tar.lz go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.tar.xz go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.tar.zst go-tangerine-772e7e8c8df066a64726b23bde84b6025c5af318.zip |
Key Manager
- keypair, keyring:
-- consistent naming of methods
-- error propagation
-- no panic
- keyManager: persist, import, export, initialize and (re)set keyring
- no global public "singleton" keyring, instead interface via keyManager
- keys test
- KeyStore interface, DB and File store implementations
Diffstat (limited to 'ethcrypto/key_store.go')
-rw-r--r-- | ethcrypto/key_store.go | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/ethcrypto/key_store.go b/ethcrypto/key_store.go new file mode 100644 index 000000000..c8c506fda --- /dev/null +++ b/ethcrypto/key_store.go @@ -0,0 +1,112 @@ +package ethcrypto + +import ( + "fmt" + "github.com/ethereum/eth-go/ethutil" + "io/ioutil" + "os" + "path" + "strings" +) + +type KeyStore interface { + Load(string) (*KeyRing, error) + Save(string, *KeyRing) error +} + +type DBKeyStore struct { + db ethutil.Database +} + +const dbKeyPrefix = "KeyRing" + +func (k *DBKeyStore) dbKey(session string) []byte { + return []byte(fmt.Sprintf("%s%s", dbKeyPrefix, session)) +} + +func (k *DBKeyStore) Save(session string, keyRing *KeyRing) error { + k.db.Put(k.dbKey(session), keyRing.RlpEncode()) + return nil +} + +func (k *DBKeyStore) Load(session string) (*KeyRing, error) { + data, err := k.db.Get(k.dbKey(session)) + if err != nil { + return nil, err + } + var keyRing *KeyRing + keyRing, err = NewKeyRingFromBytes(data) + if err != nil { + return nil, err + } + // if empty keyRing is found we return nil, no error + if keyRing.Len() == 0 { + return nil, nil + } + return keyRing, nil +} + +type FileKeyStore struct { + basedir string +} + +func (k *FileKeyStore) Save(session string, keyRing *KeyRing) error { + var content []byte + var err error + var privateKeys []string + var publicKeys []string + var mnemonics []string + var addresses []string + keyRing.Each(func(keyPair *KeyPair) { + privateKeys = append(privateKeys, ethutil.Bytes2Hex(keyPair.PrivateKey)) + publicKeys = append(publicKeys, ethutil.Bytes2Hex(keyPair.PublicKey)) + addresses = append(addresses, ethutil.Bytes2Hex(keyPair.Address())) + mnemonics = append(mnemonics, strings.Join(MnemonicEncode(ethutil.Bytes2Hex(keyPair.PrivateKey)), " ")) + }) + + basename := session + if session == "" { + basename = "default" + } + + path := path.Join(k.basedir, basename) + content = []byte(strings.Join(privateKeys, "\n")) + err = ioutil.WriteFile(path+".prv", content, 0600) + if err != nil { + return err + } + + content = []byte(strings.Join(publicKeys, "\n")) + err = ioutil.WriteFile(path+".pub", content, 0644) + if err != nil { + return err + } + + content = []byte(strings.Join(addresses, "\n")) + err = ioutil.WriteFile(path+".addr", content, 0644) + if err != nil { + return err + } + + content = []byte(strings.Join(mnemonics, "\n")) + err = ioutil.WriteFile(path+".mne", content, 0600) + if err != nil { + return err + } + + return nil +} + +func (k *FileKeyStore) Load(session string) (*KeyRing, error) { + basename := session + if session == "" { + basename = "default" + } + secfile := path.Join(k.basedir, basename+".prv") + _, err := os.Stat(secfile) + // if file is not found then we return nil, no error + if err != nil { + return nil, nil + } + return NewKeyRingFromFile(secfile) +} |