diff options
author | obscuren <geffobscura@gmail.com> | 2014-03-07 18:26:35 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-03-07 18:26:35 +0800 |
commit | 685ea3e9a944cc982bf5afc5b3e29c935a6e4c0b (patch) | |
tree | c823467ba272b1348ef0cf324e1ec3053845c60e /ethchain | |
parent | ea873304cad4b6a9f02a6686df44da8364304905 (diff) | |
download | dexon-685ea3e9a944cc982bf5afc5b3e29c935a6e4c0b.tar dexon-685ea3e9a944cc982bf5afc5b3e29c935a6e4c0b.tar.gz dexon-685ea3e9a944cc982bf5afc5b3e29c935a6e4c0b.tar.bz2 dexon-685ea3e9a944cc982bf5afc5b3e29c935a6e4c0b.tar.lz dexon-685ea3e9a944cc982bf5afc5b3e29c935a6e4c0b.tar.xz dexon-685ea3e9a944cc982bf5afc5b3e29c935a6e4c0b.tar.zst dexon-685ea3e9a944cc982bf5afc5b3e29c935a6e4c0b.zip |
Wip keychains
Diffstat (limited to 'ethchain')
-rw-r--r-- | ethchain/keypair.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/ethchain/keypair.go b/ethchain/keypair.go new file mode 100644 index 000000000..9fdc95972 --- /dev/null +++ b/ethchain/keypair.go @@ -0,0 +1,74 @@ +package ethchain + +import ( + "github.com/ethereum/eth-go/ethutil" + "math/big" +) + +type KeyPair struct { + PrivateKey []byte + PublicKey []byte + + // The associated account + account *Account + state *State +} + +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() *Account { + 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 { + 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 +} + +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 +} |