core/types: use common.{Hash,Address} in for transactions
This commit is contained in:
parent
13ade2ed60
commit
16df850af2
|
@ -1,7 +1,6 @@
|
||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
@ -156,9 +155,9 @@ func (self *Block) Transactions() Transactions {
|
||||||
return self.transactions
|
return self.transactions
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Block) Transaction(hash []byte) *Transaction {
|
func (self *Block) Transaction(hash common.Hash) *Transaction {
|
||||||
for _, transaction := range self.transactions {
|
for _, transaction := range self.transactions {
|
||||||
if bytes.Equal(hash, transaction.Hash()) {
|
if transaction.Hash() == hash {
|
||||||
return transaction
|
return transaction
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,13 @@ package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/ecdsa"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/crypto/secp256k1"
|
"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"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ type Transaction struct {
|
||||||
AccountNonce uint64
|
AccountNonce uint64
|
||||||
Price *big.Int
|
Price *big.Int
|
||||||
GasLimit *big.Int
|
GasLimit *big.Int
|
||||||
Recipient []byte
|
Recipient common.Address
|
||||||
Amount *big.Int
|
Amount *big.Int
|
||||||
Payload []byte
|
Payload []byte
|
||||||
V byte
|
V byte
|
||||||
|
@ -28,31 +28,24 @@ type Transaction struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContractCreationTx(Amount, gasAmount, price *big.Int, data []byte) *Transaction {
|
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}
|
return &Transaction{Recipient: to, Amount: Amount, Price: price, GasLimit: gasAmount, Payload: data}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTransactionFromBytes(data []byte) *Transaction {
|
func NewTransactionFromBytes(data []byte) *Transaction {
|
||||||
tx := &Transaction{}
|
tx := new(Transaction)
|
||||||
tx.RlpDecode(data)
|
rlp.Decode(bytes.NewReader(data), tx)
|
||||||
|
|
||||||
return tx
|
return tx
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTransactionFromAmount(val *common.Value) *Transaction {
|
func (tx *Transaction) Hash() (a common.Hash) {
|
||||||
tx := &Transaction{}
|
h := sha3.NewKeccak256()
|
||||||
tx.RlpValueDecode(val)
|
rlp.Encode(h, []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload})
|
||||||
|
h.Sum(a[:])
|
||||||
return tx
|
return a
|
||||||
}
|
|
||||||
|
|
||||||
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 (self *Transaction) Data() []byte {
|
func (self *Transaction) Data() []byte {
|
||||||
|
@ -79,11 +72,11 @@ func (self *Transaction) SetNonce(AccountNonce uint64) {
|
||||||
self.AccountNonce = AccountNonce
|
self.AccountNonce = AccountNonce
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Transaction) From() []byte {
|
func (self *Transaction) From() common.Address {
|
||||||
return self.sender()
|
return self.sender()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Transaction) To() []byte {
|
func (self *Transaction) To() common.Address {
|
||||||
return self.Recipient
|
return self.Recipient
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,48 +90,31 @@ func (tx *Transaction) Curve() (v byte, r []byte, s []byte) {
|
||||||
|
|
||||||
func (tx *Transaction) Signature(key []byte) []byte {
|
func (tx *Transaction) Signature(key []byte) []byte {
|
||||||
hash := tx.Hash()
|
hash := tx.Hash()
|
||||||
|
sig, _ := secp256k1.Sign(hash[:], key)
|
||||||
sig, _ := secp256k1.Sign(hash, key)
|
|
||||||
|
|
||||||
return sig
|
return sig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *Transaction) PublicKey() []byte {
|
func (tx *Transaction) PublicKey() []byte {
|
||||||
hash := tx.Hash()
|
hash := tx.Hash()
|
||||||
|
|
||||||
v, r, s := tx.Curve()
|
v, r, s := tx.Curve()
|
||||||
|
|
||||||
sig := append(r, s...)
|
sig := append(r, s...)
|
||||||
sig = append(sig, v-27)
|
sig = append(sig, v-27)
|
||||||
|
|
||||||
//pubkey := crypto.Ecrecover(append(hash, sig...))
|
//pubkey := crypto.Ecrecover(append(hash, sig...))
|
||||||
pubkey, _ := secp256k1.RecoverPubkey(hash, sig)
|
pubkey, _ := secp256k1.RecoverPubkey(hash[:], sig)
|
||||||
|
|
||||||
return pubkey
|
return pubkey
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *Transaction) sender() []byte {
|
func (tx *Transaction) sender() (a common.Address) {
|
||||||
pubkey := tx.PublicKey()
|
pubkey := tx.PublicKey()
|
||||||
|
|
||||||
// Validate the returned key.
|
// Validate the returned key.
|
||||||
// Return nil if public key isn't in full format
|
// Return nil if public key isn't in full format
|
||||||
if len(pubkey) == 0 || pubkey[0] != 4 {
|
if len(pubkey) == 0 || pubkey[0] != 4 {
|
||||||
return nil
|
return a
|
||||||
}
|
}
|
||||||
|
copy(a[:], crypto.Sha3(pubkey[1:]))
|
||||||
return crypto.Sha3(pubkey[1:])[12:]
|
return a
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *Transaction) SetSignatureValues(sig []byte) error {
|
func (tx *Transaction) SetSignatureValues(sig []byte) error {
|
||||||
|
@ -148,36 +124,17 @@ func (tx *Transaction) SetSignatureValues(sig []byte) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *Transaction) SignECDSA(key *ecdsa.PrivateKey) error {
|
// TODO: remove
|
||||||
return tx.Sign(crypto.FromECDSA(key))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tx *Transaction) RlpData() interface{} {
|
func (tx *Transaction) RlpData() interface{} {
|
||||||
data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload}
|
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())
|
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 {
|
func (tx *Transaction) RlpEncode() []byte {
|
||||||
return common.Encode(tx)
|
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 {
|
func (tx *Transaction) String() string {
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
TX(%x)
|
TX(%x)
|
||||||
|
@ -213,6 +170,7 @@ func (tx *Transaction) String() string {
|
||||||
// Transaction slice type for basic sorting
|
// Transaction slice type for basic sorting
|
||||||
type Transactions []*Transaction
|
type Transactions []*Transaction
|
||||||
|
|
||||||
|
// TODO: remove
|
||||||
func (self Transactions) RlpData() interface{} {
|
func (self Transactions) RlpData() interface{} {
|
||||||
// Marshal the transactions of this block
|
// Marshal the transactions of this block
|
||||||
enc := make([]interface{}, len(self))
|
enc := make([]interface{}, len(self))
|
||||||
|
@ -223,6 +181,7 @@ func (self Transactions) RlpData() interface{} {
|
||||||
|
|
||||||
return enc
|
return enc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Transactions) Len() int { return len(s) }
|
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) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||||
func (s Transactions) GetRlp(i int) []byte { return common.Rlp(s[i]) }
|
func (s Transactions) GetRlp(i int) []byte { return common.Rlp(s[i]) }
|
||||||
|
|
Loading…
Reference in New Issue