forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved keyring to ethutil & removed old methods. Implements ethereum#20
- Loading branch information
Showing
9 changed files
with
122 additions
and
118 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters