aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-05-14 19:54:40 +0800
committerobscuren <geffobscura@gmail.com>2014-05-14 19:54:40 +0800
commitf4fa0d48cb10f925908062357be965c54370cba9 (patch)
tree4b274e1a6a25fb6a468d52a2e1e67775b42d0a0f
parent0512113bdd5cc55ae35abd442b668ab5ed7a116b (diff)
downloaddexon-f4fa0d48cb10f925908062357be965c54370cba9.tar
dexon-f4fa0d48cb10f925908062357be965c54370cba9.tar.gz
dexon-f4fa0d48cb10f925908062357be965c54370cba9.tar.bz2
dexon-f4fa0d48cb10f925908062357be965c54370cba9.tar.lz
dexon-f4fa0d48cb10f925908062357be965c54370cba9.tar.xz
dexon-f4fa0d48cb10f925908062357be965c54370cba9.tar.zst
dexon-f4fa0d48cb10f925908062357be965c54370cba9.zip
Moved keyring to ethutil & removed old methods. Implements #20
-rw-r--r--ethchain/keypair.go87
-rw-r--r--ethdb/database.go2
-rw-r--r--ethdb/memory_database.go2
-rw-r--r--ethpub/pub.go13
-rw-r--r--ethpub/types.go2
-rw-r--r--ethutil/db.go2
-rw-r--r--ethutil/key.go19
-rw-r--r--ethutil/keypair.go109
-rw-r--r--peer.go4
9 files changed, 122 insertions, 118 deletions
diff --git a/ethchain/keypair.go b/ethchain/keypair.go
deleted file mode 100644
index 0f23bacdf..000000000
--- a/ethchain/keypair.go
+++ /dev/null
@@ -1,87 +0,0 @@
-package ethchain
-
-import (
- "github.com/ethereum/eth-go/ethutil"
- "github.com/obscuren/secp256k1-go"
- "math/big"
-)
-
-type KeyPair struct {
- PrivateKey []byte
- PublicKey []byte
-
- // The associated account
- account *StateObject
- state *State
-}
-
-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 *ethutil.Value) *KeyPair {
- keyPair := &KeyPair{PrivateKey: val.Get(0).Bytes(), PublicKey: val.Get(1).Bytes()}
-
- return keyPair
-}
-
-func (k *KeyPair) Address() []byte {
- return ethutil.Sha3Bin(k.PublicKey[1:])[12:]
-}
-
-func (k *KeyPair) Account() *StateObject {
- if k.account == nil {
- k.account = k.state.GetAccount(k.Address())
- }
-
- return k.account
-}
-
-// Create transaction, creates a new and signed transaction, ready for processing
-func (k *KeyPair) CreateTx(receiver []byte, value *big.Int, data []string) *Transaction {
- /* TODO
- tx := NewTransaction(receiver, value, data)
- tx.Nonce = k.account.Nonce
-
- // Sign the transaction with the private key in this key chain
- tx.Sign(k.PrivateKey)
-
- return tx
- */
- return nil
-}
-
-func (k *KeyPair) RlpEncode() []byte {
- return ethutil.EmptyValue().Append(k.PrivateKey).Append(k.PublicKey).Encode()
-}
-
-type KeyRing struct {
- keys []*KeyPair
-}
-
-func (k *KeyRing) Add(pair *KeyPair) {
- k.keys = append(k.keys, pair)
-}
-
-// The public "singleton" keyring
-var keyRing *KeyRing
-
-func GetKeyRing(state *State) *KeyRing {
- if keyRing == nil {
- keyRing = &KeyRing{}
-
- data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
- it := ethutil.NewValueFromBytes(data).NewIterator()
- for it.Next() {
- v := it.Value()
- keyRing.Add(NewKeyPairFromValue(v))
- }
- }
-
- return keyRing
-}
diff --git a/ethdb/database.go b/ethdb/database.go
index 3dbff36de..09e9d8c7d 100644
--- a/ethdb/database.go
+++ b/ethdb/database.go
@@ -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
diff --git a/ethdb/memory_database.go b/ethdb/memory_database.go
index b0fa64ed7..1e9d2899a 100644
--- a/ethdb/memory_database.go
+++ b/ethdb/memory_database.go
@@ -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))
diff --git a/ethpub/pub.go b/ethpub/pub.go
index 4d1536368..daacb9d78 100644
--- a/ethpub/pub.go
+++ b/ethpub/pub.go
@@ -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)
}
@@ -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 ""
}
@@ -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 {
diff --git a/ethpub/types.go b/ethpub/types.go
index 7f25e48a6..c902afc56 100644
--- a/ethpub/types.go
+++ b/ethpub/types.go
@@ -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)}
}
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
+}
diff --git a/peer.go b/peer.go
index 70759f246..433ce161f 100644
--- a/peer.go
+++ b/peer.go
@@ -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