Commit f4fa0d48 authored by obscuren's avatar obscuren

Moved keyring to ethutil & removed old methods. Implements #20

parent 0512113b
...@@ -54,11 +54,13 @@ func (db *LDBDatabase) LastKnownTD() []byte { ...@@ -54,11 +54,13 @@ func (db *LDBDatabase) LastKnownTD() []byte {
return data return data
} }
/*
func (db *LDBDatabase) GetKeys() []*ethutil.Key { func (db *LDBDatabase) GetKeys() []*ethutil.Key {
data, _ := db.Get([]byte("KeyRing")) data, _ := db.Get([]byte("KeyRing"))
return []*ethutil.Key{ethutil.NewKeyFromBytes(data)} return []*ethutil.Key{ethutil.NewKeyFromBytes(data)}
} }
*/
func (db *LDBDatabase) Close() { func (db *LDBDatabase) Close() {
// Close the leveldb database // Close the leveldb database
......
...@@ -26,11 +26,13 @@ func (db *MemDatabase) Get(key []byte) ([]byte, error) { ...@@ -26,11 +26,13 @@ func (db *MemDatabase) Get(key []byte) ([]byte, error) {
return db.db[string(key)], nil return db.db[string(key)], nil
} }
/*
func (db *MemDatabase) GetKeys() []*ethutil.Key { func (db *MemDatabase) GetKeys() []*ethutil.Key {
data, _ := db.Get([]byte("KeyRing")) data, _ := db.Get([]byte("KeyRing"))
return []*ethutil.Key{ethutil.NewKeyFromBytes(data)} return []*ethutil.Key{ethutil.NewKeyFromBytes(data)}
} }
*/
func (db *MemDatabase) Delete(key []byte) error { func (db *MemDatabase) Delete(key []byte) error {
delete(db.db, string(key)) delete(db.db, string(key))
......
...@@ -39,10 +39,7 @@ func (lib *PEthereum) GetBlock(hexHash string) *PBlock { ...@@ -39,10 +39,7 @@ func (lib *PEthereum) GetBlock(hexHash string) *PBlock {
} }
func (lib *PEthereum) GetKey() *PKey { func (lib *PEthereum) GetKey() *PKey {
keyPair, err := ethchain.NewKeyPairFromSec(ethutil.Config.Db.GetKeys()[0].PrivateKey) keyPair := ethutil.GetKeyRing().Get(0)
if err != nil {
return nil
}
return NewPKey(keyPair) return NewPKey(keyPair)
} }
...@@ -90,7 +87,7 @@ func (lib *PEthereum) IsContract(address string) bool { ...@@ -90,7 +87,7 @@ func (lib *PEthereum) IsContract(address string) bool {
} }
func (lib *PEthereum) SecretToAddress(key string) string { func (lib *PEthereum) SecretToAddress(key string) string {
pair, err := ethchain.NewKeyPairFromSec(ethutil.FromHex(key)) pair, err := ethutil.NewKeyPairFromSec(ethutil.FromHex(key))
if err != nil { if err != nil {
return "" return ""
} }
...@@ -115,12 +112,12 @@ func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, in ...@@ -115,12 +112,12 @@ func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, in
hash = ethutil.FromHex(recipient) hash = ethutil.FromHex(recipient)
} }
var keyPair *ethchain.KeyPair var keyPair *ethutil.KeyPair
var err error var err error
if key[0:2] == "0x" { if key[0:2] == "0x" {
keyPair, err = ethchain.NewKeyPairFromSec([]byte(ethutil.FromHex(key[0:2]))) keyPair, err = ethutil.NewKeyPairFromSec([]byte(ethutil.FromHex(key[0:2])))
} else { } else {
keyPair, err = ethchain.NewKeyPairFromSec([]byte(ethutil.FromHex(key))) keyPair, err = ethutil.NewKeyPairFromSec([]byte(ethutil.FromHex(key)))
} }
if err != nil { if err != nil {
......
...@@ -39,7 +39,7 @@ type PKey struct { ...@@ -39,7 +39,7 @@ type PKey struct {
PublicKey string `json:"publicKey"` PublicKey string `json:"publicKey"`
} }
func NewPKey(key *ethchain.KeyPair) *PKey { func NewPKey(key *ethutil.KeyPair) *PKey {
return &PKey{ethutil.Hex(key.Address()), ethutil.Hex(key.PrivateKey), ethutil.Hex(key.PublicKey)} return &PKey{ethutil.Hex(key.Address()), ethutil.Hex(key.PrivateKey), ethutil.Hex(key.PublicKey)}
} }
......
...@@ -4,7 +4,7 @@ package ethutil ...@@ -4,7 +4,7 @@ package ethutil
type Database interface { type Database interface {
Put(key []byte, value []byte) Put(key []byte, value []byte)
Get(key []byte) ([]byte, error) Get(key []byte) ([]byte, error)
GetKeys() []*Key //GetKeys() []*Key
Delete(key []byte) error Delete(key []byte) error
LastKnownTD() []byte LastKnownTD() []byte
Close() Close()
......
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()
}
package ethchain package ethutil
import ( import (
"github.com/ethereum/eth-go/ethutil"
"github.com/obscuren/secp256k1-go" "github.com/obscuren/secp256k1-go"
"math/big"
) )
type KeyPair struct { type KeyPair struct {
...@@ -12,7 +10,6 @@ type KeyPair struct { ...@@ -12,7 +10,6 @@ type KeyPair struct {
// The associated account // The associated account
account *StateObject account *StateObject
state *State
} }
func NewKeyPairFromSec(seckey []byte) (*KeyPair, error) { func NewKeyPairFromSec(seckey []byte) (*KeyPair, error) {
...@@ -24,62 +21,87 @@ func NewKeyPairFromSec(seckey []byte) (*KeyPair, error) { ...@@ -24,62 +21,87 @@ func NewKeyPairFromSec(seckey []byte) (*KeyPair, error) {
return &KeyPair{PrivateKey: seckey, PublicKey: pubkey}, nil return &KeyPair{PrivateKey: seckey, PublicKey: pubkey}, nil
} }
func NewKeyPairFromValue(val *ethutil.Value) *KeyPair { func NewKeyPairFromValue(val *Value) *KeyPair {
keyPair := &KeyPair{PrivateKey: val.Get(0).Bytes(), PublicKey: val.Get(1).Bytes()} v, _ := NewKeyPairFromSec(val.Bytes())
return keyPair return v
} }
func (k *KeyPair) Address() []byte { func (k *KeyPair) Address() []byte {
return ethutil.Sha3Bin(k.PublicKey[1:])[12:] return Sha3Bin(k.PublicKey[1:])[12:]
} }
func (k *KeyPair) Account() *StateObject { func (k *KeyPair) RlpEncode() []byte {
if k.account == nil { return k.RlpValue().Encode()
k.account = k.state.GetAccount(k.Address()) }
}
return k.account func (k *KeyPair) RlpValue() *Value {
return NewValue(k.PrivateKey)
} }
// Create transaction, creates a new and signed transaction, ready for processing type KeyRing struct {
func (k *KeyPair) CreateTx(receiver []byte, value *big.Int, data []string) *Transaction { keys []*KeyPair
/* TODO }
tx := NewTransaction(receiver, value, data)
tx.Nonce = k.account.Nonce
// Sign the transaction with the private key in this key chain func (k *KeyRing) Add(pair *KeyPair) {
tx.Sign(k.PrivateKey) k.keys = append(k.keys, pair)
}
func (k *KeyRing) Get(i int) *KeyPair {
if len(k.keys) > i {
return k.keys[i]
}
return tx
*/
return nil return nil
} }
func (k *KeyPair) RlpEncode() []byte { func (k *KeyRing) Len() int {
return ethutil.EmptyValue().Append(k.PrivateKey).Append(k.PublicKey).Encode() return len(k.keys)
} }
type KeyRing struct { func (k *KeyRing) NewKeyPair(sec []byte) (*KeyPair, error) {
keys []*KeyPair 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) Add(pair *KeyPair) { func (k *KeyRing) Reset() {
k.keys = append(k.keys, pair) 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 // The public "singleton" keyring
var keyRing *KeyRing var keyRing *KeyRing
func GetKeyRing(state *State) *KeyRing { func GetKeyRing() *KeyRing {
if keyRing == nil { if keyRing == nil {
keyRing = &KeyRing{} keyRing = &KeyRing{}
data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) data, _ := Config.Db.Get([]byte("KeyRing"))
it := ethutil.NewValueFromBytes(data).NewIterator() it := NewValueFromBytes(data).NewIterator()
for it.Next() { for it.Next() {
v := it.Value() v := it.Value()
keyRing.Add(NewKeyPairFromValue(v))
key, err := NewKeyPairFromSec(v.Bytes())
if err != nil {
panic(err)
}
keyRing.Add(key)
} }
} }
......
...@@ -581,8 +581,8 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) { ...@@ -581,8 +581,8 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
p.port = uint16(c.Get(4).Uint()) p.port = uint16(c.Get(4).Uint())
// Self connect detection // Self connect detection
key := ethutil.Config.Db.GetKeys()[0] keyPair := ethutil.GetKeyRing().Get(0)
if bytes.Compare(key.PublicKey, p.pubkey) == 0 { if bytes.Compare(keyPair.PublicKey, p.pubkey) == 0 {
p.Stop() p.Stop()
return return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment