Skip to content

Commit

Permalink
Moved keyring to ethutil & removed old methods. Implements ethereum#20
Browse files Browse the repository at this point in the history
  • Loading branch information
obscuren committed May 14, 2014
1 parent 0512113 commit f4fa0d4
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 118 deletions.
87 changes: 0 additions & 87 deletions ethchain/keypair.go

This file was deleted.

2 changes: 2 additions & 0 deletions ethdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ func (db *LDBDatabase) LastKnownTD() []byte {
return data
}

/*
func (db *LDBDatabase) GetKeys() []*ethutil.Key {
data, _ := db.Get([]byte("KeyRing"))
return []*ethutil.Key{ethutil.NewKeyFromBytes(data)}
}
*/

func (db *LDBDatabase) Close() {
// Close the leveldb database
Expand Down
2 changes: 2 additions & 0 deletions ethdb/memory_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ func (db *MemDatabase) Get(key []byte) ([]byte, error) {
return db.db[string(key)], nil
}

/*
func (db *MemDatabase) GetKeys() []*ethutil.Key {
data, _ := db.Get([]byte("KeyRing"))
return []*ethutil.Key{ethutil.NewKeyFromBytes(data)}
}
*/

func (db *MemDatabase) Delete(key []byte) error {
delete(db.db, string(key))
Expand Down
13 changes: 5 additions & 8 deletions ethpub/pub.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ func (lib *PEthereum) GetBlock(hexHash string) *PBlock {
}

func (lib *PEthereum) GetKey() *PKey {
keyPair, err := ethchain.NewKeyPairFromSec(ethutil.Config.Db.GetKeys()[0].PrivateKey)
if err != nil {
return nil
}
keyPair := ethutil.GetKeyRing().Get(0)

return NewPKey(keyPair)
}
Expand Down Expand Up @@ -90,7 +87,7 @@ func (lib *PEthereum) IsContract(address string) bool {
}

func (lib *PEthereum) SecretToAddress(key string) string {
pair, err := ethchain.NewKeyPairFromSec(ethutil.FromHex(key))
pair, err := ethutil.NewKeyPairFromSec(ethutil.FromHex(key))
if err != nil {
return ""
}
Expand All @@ -115,12 +112,12 @@ func (lib *PEthereum) createTx(key, recipient, valueStr, gasStr, gasPriceStr, in
hash = ethutil.FromHex(recipient)
}

var keyPair *ethchain.KeyPair
var keyPair *ethutil.KeyPair
var err error
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 {
keyPair, err = ethchain.NewKeyPairFromSec([]byte(ethutil.FromHex(key)))
keyPair, err = ethutil.NewKeyPairFromSec([]byte(ethutil.FromHex(key)))
}

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion ethpub/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type PKey struct {
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)}
}

Expand Down
2 changes: 1 addition & 1 deletion ethutil/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 0 additions & 19 deletions ethutil/key.go

This file was deleted.

109 changes: 109 additions & 0 deletions ethutil/keypair.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,8 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
p.port = uint16(c.Get(4).Uint())

// Self connect detection
key := ethutil.Config.Db.GetKeys()[0]
if bytes.Compare(key.PublicKey, p.pubkey) == 0 {
keyPair := ethutil.GetKeyRing().Get(0)
if bytes.Compare(keyPair.PublicKey, p.pubkey) == 0 {
p.Stop()

return
Expand Down

0 comments on commit f4fa0d4

Please sign in to comment.