Skip to content

Commit

Permalink
Merge pull request ethereum#28 from ethersphere/feature/keys
Browse files Browse the repository at this point in the history
Feature/keys
  • Loading branch information
obscuren committed Jul 1, 2014
2 parents 550407b + ff5703f commit 29f613e
Show file tree
Hide file tree
Showing 35 changed files with 2,440 additions and 1,994 deletions.
16 changes: 9 additions & 7 deletions ethchain/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ethchain
import (
"bytes"
"fmt"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethtrie"
"github.com/ethereum/eth-go/ethutil"
"math/big"
"strconv"
Expand Down Expand Up @@ -102,18 +104,18 @@ func CreateBlock(root interface{},
}
block.SetUncles([]*Block{})

block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, root))
block.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, root))

return block
}

// Returns a hash of the block
func (block *Block) Hash() []byte {
return ethutil.Sha3Bin(block.Value().Encode())
return ethcrypto.Sha3Bin(block.Value().Encode())
}

func (block *Block) HashNoNonce() []byte {
return ethutil.Sha3Bin(ethutil.Encode([]interface{}{block.PrevHash,
return ethcrypto.Sha3Bin(ethutil.Encode([]interface{}{block.PrevHash,
block.UncleSha, block.Coinbase, block.state.trie.Root,
block.TxSha, block.Difficulty, block.Number, block.MinGasPrice,
block.GasLimit, block.GasUsed, block.Time, block.Extra}))
Expand Down Expand Up @@ -239,7 +241,7 @@ func (block *Block) SetUncles(uncles []*Block) {
block.Uncles = uncles

// Sha of the concatenated uncles
block.UncleSha = ethutil.Sha3Bin(ethutil.Encode(block.rlpUncles()))
block.UncleSha = ethcrypto.Sha3Bin(ethutil.Encode(block.rlpUncles()))
}

func (self *Block) SetReceipts(receipts []*Receipt, txs []*Transaction) {
Expand All @@ -250,7 +252,7 @@ func (self *Block) SetReceipts(receipts []*Receipt, txs []*Transaction) {
func (block *Block) setTransactions(txs []*Transaction) {
block.transactions = txs

trie := ethutil.NewTrie(ethutil.Config.Db, "")
trie := ethtrie.NewTrie(ethutil.Config.Db, "")
for i, tx := range txs {
trie.Update(strconv.Itoa(i), string(tx.RlpEncode()))
}
Expand Down Expand Up @@ -287,7 +289,7 @@ func (block *Block) RlpValueDecode(decoder *ethutil.Value) {
block.PrevHash = header.Get(0).Bytes()
block.UncleSha = header.Get(1).Bytes()
block.Coinbase = header.Get(2).Bytes()
block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, header.Get(3).Val))
block.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, header.Get(3).Val))
block.TxSha = header.Get(4).Bytes()
block.Difficulty = header.Get(5).BigInt()
block.Number = header.Get(6).BigInt()
Expand Down Expand Up @@ -329,7 +331,7 @@ func NewUncleBlockFromValue(header *ethutil.Value) *Block {
block.PrevHash = header.Get(0).Bytes()
block.UncleSha = header.Get(1).Bytes()
block.Coinbase = header.Get(2).Bytes()
block.state = NewState(ethutil.NewTrie(ethutil.Config.Db, header.Get(3).Val))
block.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, header.Get(3).Val))
block.TxSha = header.Get(4).Bytes()
block.Difficulty = header.Get(5).BigInt()
block.Number = header.Get(6).BigInt()
Expand Down
2 changes: 1 addition & 1 deletion ethchain/block_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func AddTestNetFunds(block *Block) {
"e6716f9544a56c530d868e4bfbacb172315bdead",
"1a26338f0d905e295fccb71fa9ea849ffa12aaf4",
} {
codedAddr := ethutil.FromHex(addr)
codedAddr := ethutil.Hex2Bytes(addr)
account := block.state.GetAccount(codedAddr)
account.Amount = ethutil.Big("1606938044258990275541962092341162602522202993782792835301376") //ethutil.BigPow(2, 200)
block.state.UpdateStateObject(account)
Expand Down
3 changes: 2 additions & 1 deletion ethchain/dagger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ethchain

import (
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethutil"
"github.com/obscuren/sha3"
Expand Down Expand Up @@ -41,7 +42,7 @@ func (pow *EasyPow) Search(block *Block, reactChan chan ethutil.React) []byte {
powlogger.Infoln("Hashing @", int64(hashes), "khash")
}

sha := ethutil.Sha3Bin(big.NewInt(r.Int63()).Bytes())
sha := ethcrypto.Sha3Bin(big.NewInt(r.Int63()).Bytes())
if pow.Verify(hash, diff, sha) {
return sha
}
Expand Down
7 changes: 4 additions & 3 deletions ethchain/genesis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ethchain

import (
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethutil"
"math/big"
)
Expand All @@ -11,13 +12,13 @@ import (

var ZeroHash256 = make([]byte, 32)
var ZeroHash160 = make([]byte, 20)
var EmptyShaList = ethutil.Sha3Bin(ethutil.Encode([]interface{}{}))
var EmptyShaList = ethcrypto.Sha3Bin(ethutil.Encode([]interface{}{}))

var GenesisHeader = []interface{}{
// Previous hash (none)
ZeroHash256,
// Sha of uncles
ethutil.Sha3Bin(ethutil.Encode([]interface{}{})),
ethcrypto.Sha3Bin(ethutil.Encode([]interface{}{})),
// Coinbase
ZeroHash160,
// Root state
Expand All @@ -39,7 +40,7 @@ var GenesisHeader = []interface{}{
// Extra
nil,
// Nonce
ethutil.Sha3Bin(big.NewInt(42).Bytes()),
ethcrypto.Sha3Bin(big.NewInt(42).Bytes()),
}

var Genesis = []interface{}{GenesisHeader, []interface{}{}, []interface{}{}}
10 changes: 6 additions & 4 deletions ethchain/state.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ethchain

import (
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethtrie"
"github.com/ethereum/eth-go/ethutil"
"math/big"
)
Expand All @@ -12,15 +14,15 @@ import (
// * Accounts
type State struct {
// The trie for this structure
trie *ethutil.Trie
trie *ethtrie.Trie

stateObjects map[string]*StateObject

manifest *Manifest
}

// Create a new state from a given trie
func NewState(trie *ethutil.Trie) *State {
func NewState(trie *ethtrie.Trie) *State {
return &State{trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest()}
}

Expand Down Expand Up @@ -73,7 +75,7 @@ func (s *State) Purge() int {
return s.trie.NewIterator().Purge()
}

func (s *State) EachStorage(cb ethutil.EachCallback) {
func (s *State) EachStorage(cb ethtrie.EachCallback) {
it := s.trie.NewIterator()
it.Each(cb)
}
Expand All @@ -91,7 +93,7 @@ func (self *State) UpdateStateObject(stateObject *StateObject) {
self.stateObjects[string(addr)] = stateObject
}

ethutil.Config.Db.Put(ethutil.Sha3Bin(stateObject.Script()), stateObject.Script())
ethutil.Config.Db.Put(ethcrypto.Sha3Bin(stateObject.Script()), stateObject.Script())

self.trie.Update(string(addr), string(stateObject.RlpEncode()))

Expand Down
4 changes: 3 additions & 1 deletion ethchain/state_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"container/list"
"fmt"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethwire"
Expand Down Expand Up @@ -39,6 +40,7 @@ type EthManager interface {
IsMining() bool
IsListening() bool
Peers() *list.List
KeyManager() *ethcrypto.KeyManager
}

type StateManager struct {
Expand Down Expand Up @@ -299,7 +301,7 @@ func (sm *StateManager) ValidateBlock(block *Block) error {

// Verify the nonce of the block. Return an error if it's not valid
if !sm.Pow.Verify(block.HashNoNonce(), block.Difficulty, block.Nonce) {
return ValidationError("Block's nonce is invalid (= %v)", ethutil.Hex(block.Nonce))
return ValidationError("Block's nonce is invalid (= %v)", ethutil.Bytes2Hex(block.Nonce))
}

return nil
Expand Down
13 changes: 8 additions & 5 deletions ethchain/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ethchain

import (
"fmt"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethtrie"
"github.com/ethereum/eth-go/ethutil"
"math/big"
"strings"
Expand Down Expand Up @@ -39,7 +41,7 @@ func MakeContract(tx *Transaction, state *State) *StateObject {

contract := state.NewStateObject(addr)
contract.initScript = tx.Data
contract.state = NewState(ethutil.NewTrie(ethutil.Config.Db, ""))
contract.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, ""))

return contract
}
Expand All @@ -49,14 +51,14 @@ func MakeContract(tx *Transaction, state *State) *StateObject {

func NewStateObject(addr []byte) *StateObject {
object := &StateObject{address: addr, Amount: new(big.Int), gasPool: new(big.Int)}
object.state = NewState(ethutil.NewTrie(ethutil.Config.Db, ""))
object.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, ""))

return object
}

func NewContract(address []byte, Amount *big.Int, root []byte) *StateObject {
contract := &StateObject{address: address, Amount: Amount, Nonce: 0}
contract.state = NewState(ethutil.NewTrie(ethutil.Config.Db, string(root)))
contract.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, string(root)))

return contract
}
Expand Down Expand Up @@ -249,15 +251,16 @@ func (c *StateObject) RlpEncode() []byte {
root = ""
}

return ethutil.Encode([]interface{}{c.Nonce, c.Amount, root, ethutil.Sha3Bin(c.script)})
return ethutil.Encode([]interface{}{c.Nonce, c.Amount, root, ethcrypto.Sha3Bin(c.script)})
}

func (c *StateObject) RlpDecode(data []byte) {
decoder := ethutil.NewValueFromBytes(data)

c.Nonce = decoder.Get(0).Uint()
c.Amount = decoder.Get(1).BigInt()
c.state = NewState(ethutil.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface()))
c.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface()))
c.state = NewState(ethtrie.NewTrie(ethutil.Config.Db, decoder.Get(2).Interface()))

c.ScriptHash = decoder.Get(3).Bytes()

Expand Down
3 changes: 2 additions & 1 deletion ethchain/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ethchain
import (
"bytes"
"fmt"
"github.com/ethereum/eth-go/ethtrie"
"github.com/ethereum/eth-go/ethutil"
"math/big"
)
Expand Down Expand Up @@ -268,7 +269,7 @@ func Call(vm *Vm, closure *Closure, data []byte) (ret []byte, err error, deepErr
var (
context = closure.object
trie = context.state.trie
trie2 = ethutil.NewTrie(ethutil.Config.Db, "")
trie2 = ethtrie.NewTrie(ethutil.Config.Db, "")
)

trie.NewIterator().Each(func(key string, v *ethutil.Value) {
Expand Down
7 changes: 4 additions & 3 deletions ethchain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ethchain
import (
"bytes"
"fmt"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethutil"
"github.com/obscuren/secp256k1-go"
"math/big"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (self *Transaction) TotalValue() *big.Int {
func (tx *Transaction) Hash() []byte {
data := []interface{}{tx.Nonce, tx.GasPrice, tx.Gas, tx.Recipient, tx.Value, tx.Data}

return ethutil.Sha3Bin(ethutil.NewValue(data).Encode())
return ethcrypto.Sha3Bin(ethutil.NewValue(data).Encode())
}

func (tx *Transaction) CreatesContract() bool {
Expand All @@ -75,7 +76,7 @@ func (tx *Transaction) IsContract() bool {
}

func (tx *Transaction) CreationAddress() []byte {
return ethutil.Sha3Bin(ethutil.NewValue([]interface{}{tx.Sender(), tx.Nonce}).Encode())[12:]
return ethcrypto.Sha3Bin(ethutil.NewValue([]interface{}{tx.Sender(), tx.Nonce}).Encode())[12:]
}

func (tx *Transaction) Signature(key []byte) []byte {
Expand Down Expand Up @@ -111,7 +112,7 @@ func (tx *Transaction) Sender() []byte {
return nil
}

return ethutil.Sha3Bin(pubkey[1:])[12:]
return ethcrypto.Sha3Bin(pubkey[1:])[12:]
}

func (tx *Transaction) Sign(privk []byte) error {
Expand Down
5 changes: 3 additions & 2 deletions ethchain/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ethchain

import (
"fmt"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethutil"
"math"
Expand Down Expand Up @@ -421,7 +422,7 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) (ret []byte, err erro
case SHA3:
require(2)
size, offset := stack.Popn()
data := ethutil.Sha3Bin(mem.Get(offset.Int64(), size.Int64()))
data := ethcrypto.Sha3Bin(mem.Get(offset.Int64(), size.Int64()))

stack.Push(ethutil.BigD(data))
// 0x30 range
Expand Down Expand Up @@ -617,7 +618,7 @@ func (vm *Vm) RunClosure(closure *Closure, hook DebugHook) (ret []byte, err erro
snapshot := vm.state.Copy()

// Generate a new address
addr := ethutil.CreateAddress(closure.caller.Address(), closure.caller.N())
addr := ethcrypto.CreateAddress(closure.caller.Address(), closure.caller.N())

vm.Printf(" (*) %x", addr).Endl()

Expand Down
35 changes: 35 additions & 0 deletions ethcrypto/crypto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ethcrypto

import (
"code.google.com/p/go.crypto/ripemd160"
"crypto/sha256"
"github.com/obscuren/sha3"
"math/big"
)

func Sha256Bin(data []byte) []byte {
hash := sha256.Sum256(data)

return hash[:]
}

func Ripemd160(data []byte) []byte {
ripemd := ripemd160.New()
ripemd.Write(data)

return ripemd.Sum(nil)
}

func Sha3Bin(data []byte) []byte {
d := sha3.NewKeccak256()
d.Write(data)

return d.Sum(nil)
}

// Creates an ethereum address given the bytes and the nonce
func CreateAddress(b []byte, nonce *big.Int) []byte {
addrBytes := append(b, nonce.Bytes()...)

return Sha3Bin(addrBytes)[12:]
}

0 comments on commit 29f613e

Please sign in to comment.