core/types: use common.{Hash,Address} in for transactions

release/1.0.1
Felix Lange 10 years ago
parent 13ade2ed60
commit 16df850af2
  1. 5
      core/types/block.go
  2. 89
      core/types/transaction.go

@ -1,7 +1,6 @@
package types
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"
@ -156,9 +155,9 @@ func (self *Block) Transactions() Transactions {
return self.transactions
}
func (self *Block) Transaction(hash []byte) *Transaction {
func (self *Block) Transaction(hash common.Hash) *Transaction {
for _, transaction := range self.transactions {
if bytes.Equal(hash, transaction.Hash()) {
if transaction.Hash() == hash {
return transaction
}
}

@ -2,13 +2,13 @@ package types
import (
"bytes"
"crypto/ecdsa"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/rlp"
)
@ -20,7 +20,7 @@ type Transaction struct {
AccountNonce uint64
Price *big.Int
GasLimit *big.Int
Recipient []byte
Recipient common.Address
Amount *big.Int
Payload []byte
V byte
@ -28,31 +28,24 @@ type Transaction struct {
}
func NewContractCreationTx(Amount, gasAmount, price *big.Int, data []byte) *Transaction {
return NewTransactionMessage(nil, Amount, gasAmount, price, data)
return NewTransactionMessage(common.Address{}, Amount, gasAmount, price, data)
}
func NewTransactionMessage(to []byte, Amount, gasAmount, price *big.Int, data []byte) *Transaction {
func NewTransactionMessage(to common.Address, Amount, gasAmount, price *big.Int, data []byte) *Transaction {
return &Transaction{Recipient: to, Amount: Amount, Price: price, GasLimit: gasAmount, Payload: data}
}
func NewTransactionFromBytes(data []byte) *Transaction {
tx := &Transaction{}
tx.RlpDecode(data)
tx := new(Transaction)
rlp.Decode(bytes.NewReader(data), tx)
return tx
}
func NewTransactionFromAmount(val *common.Value) *Transaction {
tx := &Transaction{}
tx.RlpValueDecode(val)
return tx
}
func (tx *Transaction) Hash() []byte {
data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload}
return crypto.Sha3(common.Encode(data))
func (tx *Transaction) Hash() (a common.Hash) {
h := sha3.NewKeccak256()
rlp.Encode(h, []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload})
h.Sum(a[:])
return a
}
func (self *Transaction) Data() []byte {
@ -79,11 +72,11 @@ func (self *Transaction) SetNonce(AccountNonce uint64) {
self.AccountNonce = AccountNonce
}
func (self *Transaction) From() []byte {
func (self *Transaction) From() common.Address {
return self.sender()
}
func (self *Transaction) To() []byte {
func (self *Transaction) To() common.Address {
return self.Recipient
}
@ -97,48 +90,31 @@ func (tx *Transaction) Curve() (v byte, r []byte, s []byte) {
func (tx *Transaction) Signature(key []byte) []byte {
hash := tx.Hash()
sig, _ := secp256k1.Sign(hash, key)
sig, _ := secp256k1.Sign(hash[:], key)
return sig
}
func (tx *Transaction) PublicKey() []byte {
hash := tx.Hash()
v, r, s := tx.Curve()
sig := append(r, s...)
sig = append(sig, v-27)
//pubkey := crypto.Ecrecover(append(hash, sig...))
pubkey, _ := secp256k1.RecoverPubkey(hash, sig)
pubkey, _ := secp256k1.RecoverPubkey(hash[:], sig)
return pubkey
}
func (tx *Transaction) sender() []byte {
func (tx *Transaction) sender() (a common.Address) {
pubkey := tx.PublicKey()
// Validate the returned key.
// Return nil if public key isn't in full format
if len(pubkey) == 0 || pubkey[0] != 4 {
return nil
return a
}
return crypto.Sha3(pubkey[1:])[12:]
}
// TODO: deprecate after new accounts & key stores are integrated
func (tx *Transaction) Sign(privk []byte) error {
sig := tx.Signature(privk)
tx.R = sig[:32]
tx.S = sig[32:64]
tx.V = sig[64] + 27
return nil
copy(a[:], crypto.Sha3(pubkey[1:]))
return a
}
func (tx *Transaction) SetSignatureValues(sig []byte) error {
@ -148,36 +124,17 @@ func (tx *Transaction) SetSignatureValues(sig []byte) error {
return nil
}
func (tx *Transaction) SignECDSA(key *ecdsa.PrivateKey) error {
return tx.Sign(crypto.FromECDSA(key))
}
// TODO: remove
func (tx *Transaction) RlpData() interface{} {
data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload}
return append(data, tx.V, new(big.Int).SetBytes(tx.R).Bytes(), new(big.Int).SetBytes(tx.S).Bytes())
}
// TODO: remove
func (tx *Transaction) RlpEncode() []byte {
return common.Encode(tx)
}
func (tx *Transaction) RlpDecode(data []byte) {
rlp.Decode(bytes.NewReader(data), tx)
}
func (tx *Transaction) RlpValueDecode(decoder *common.Value) {
tx.AccountNonce = decoder.Get(0).Uint()
tx.Price = decoder.Get(1).BigInt()
tx.GasLimit = decoder.Get(2).BigInt()
tx.Recipient = decoder.Get(3).Bytes()
tx.Amount = decoder.Get(4).BigInt()
tx.Payload = decoder.Get(5).Bytes()
tx.V = decoder.Get(6).Byte()
tx.R = decoder.Get(7).Bytes()
tx.S = decoder.Get(8).Bytes()
}
func (tx *Transaction) String() string {
return fmt.Sprintf(`
TX(%x)
@ -213,6 +170,7 @@ func (tx *Transaction) String() string {
// Transaction slice type for basic sorting
type Transactions []*Transaction
// TODO: remove
func (self Transactions) RlpData() interface{} {
// Marshal the transactions of this block
enc := make([]interface{}, len(self))
@ -223,6 +181,7 @@ func (self Transactions) RlpData() interface{} {
return enc
}
func (s Transactions) Len() int { return len(s) }
func (s Transactions) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s Transactions) GetRlp(i int) []byte { return common.Rlp(s[i]) }

Loading…
Cancel
Save