From f4fa0d48cb10f925908062357be965c54370cba9 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 14 May 2014 13:54:40 +0200 Subject: Moved keyring to ethutil & removed old methods. Implements #20 --- ethutil/db.go | 2 +- ethutil/key.go | 19 ---------- ethutil/keypair.go | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 20 deletions(-) delete mode 100644 ethutil/key.go create mode 100644 ethutil/keypair.go (limited to 'ethutil') diff --git a/ethutil/db.go b/ethutil/db.go index abbf4a2b0..e02a80fca 100644 --- a/ethutil/db.go +++ b/ethutil/db.go @@ -4,7 +4,7 @@ package ethutil type Database interface { Put(key []byte, value []byte) Get(key []byte) ([]byte, error) - GetKeys() []*Key + //GetKeys() []*Key Delete(key []byte) error LastKnownTD() []byte Close() diff --git a/ethutil/key.go b/ethutil/key.go deleted file mode 100644 index ec195f213..000000000 --- a/ethutil/key.go +++ /dev/null @@ -1,19 +0,0 @@ -package ethutil - -type Key struct { - PrivateKey []byte - PublicKey []byte -} - -func NewKeyFromBytes(data []byte) *Key { - val := NewValueFromBytes(data) - return &Key{val.Get(0).Bytes(), val.Get(1).Bytes()} -} - -func (k *Key) Address() []byte { - return Sha3Bin(k.PublicKey[1:])[12:] -} - -func (k *Key) RlpEncode() []byte { - return EmptyValue().Append(k.PrivateKey).Append(k.PublicKey).Encode() -} diff --git a/ethutil/keypair.go b/ethutil/keypair.go new file mode 100644 index 000000000..cf5882e2c --- /dev/null +++ b/ethutil/keypair.go @@ -0,0 +1,109 @@ +package ethutil + +import ( + "github.com/obscuren/secp256k1-go" +) + +type KeyPair struct { + PrivateKey []byte + PublicKey []byte + + // The associated account + account *StateObject +} + +func NewKeyPairFromSec(seckey []byte) (*KeyPair, error) { + pubkey, err := secp256k1.GeneratePubKey(seckey) + if err != nil { + return nil, err + } + + return &KeyPair{PrivateKey: seckey, PublicKey: pubkey}, nil +} + +func NewKeyPairFromValue(val *Value) *KeyPair { + v, _ := NewKeyPairFromSec(val.Bytes()) + + return v +} + +func (k *KeyPair) Address() []byte { + return Sha3Bin(k.PublicKey[1:])[12:] +} + +func (k *KeyPair) RlpEncode() []byte { + return k.RlpValue().Encode() +} + +func (k *KeyPair) RlpValue() *Value { + return NewValue(k.PrivateKey) +} + +type KeyRing struct { + keys []*KeyPair +} + +func (k *KeyRing) Add(pair *KeyPair) { + k.keys = append(k.keys, pair) +} + +func (k *KeyRing) Get(i int) *KeyPair { + if len(k.keys) > i { + return k.keys[i] + } + + return nil +} + +func (k *KeyRing) Len() int { + return len(k.keys) +} + +func (k *KeyRing) NewKeyPair(sec []byte) (*KeyPair, error) { + keyPair, err := NewKeyPairFromSec(sec) + if err != nil { + return nil, err + } + + k.Add(keyPair) + Config.Db.Put([]byte("KeyRing"), k.RlpValue().Encode()) + + return keyPair, nil +} + +func (k *KeyRing) Reset() { + Config.Db.Put([]byte("KeyRing"), nil) + k.keys = nil +} + +func (k *KeyRing) RlpValue() *Value { + v := EmptyValue() + for _, keyPair := range k.keys { + v.Append(keyPair.RlpValue()) + } + + return v +} + +// The public "singleton" keyring +var keyRing *KeyRing + +func GetKeyRing() *KeyRing { + if keyRing == nil { + keyRing = &KeyRing{} + + data, _ := Config.Db.Get([]byte("KeyRing")) + it := NewValueFromBytes(data).NewIterator() + for it.Next() { + v := it.Value() + + key, err := NewKeyPairFromSec(v.Bytes()) + if err != nil { + panic(err) + } + keyRing.Add(key) + } + } + + return keyRing +} -- cgit v1.2.3