go-ethereum/chain/transaction.go

220 lines
4.9 KiB
Go
Raw Normal View History

2014-10-31 04:59:17 -05:00
package chain
2014-02-14 16:56:09 -06:00
import (
"fmt"
"math/big"
2014-10-31 06:37:43 -05:00
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethutil"
2014-10-31 08:43:14 -05:00
"github.com/ethereum/go-ethereum/state"
2014-02-14 16:56:09 -06:00
"github.com/obscuren/secp256k1-go"
)
2014-02-19 04:35:17 -06:00
var ContractAddr = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
2014-06-19 06:42:47 -05:00
func IsContractAddr(addr []byte) bool {
return len(addr) == 0
//return bytes.Compare(addr, ContractAddr) == 0
2014-06-19 06:42:47 -05:00
}
2014-02-14 16:56:09 -06:00
type Transaction struct {
Nonce uint64
Recipient []byte
Value *big.Int
Gas *big.Int
2014-04-18 06:41:07 -05:00
GasPrice *big.Int
Data []byte
2014-02-14 16:56:09 -06:00
v byte
r, s []byte
2014-03-27 09:42:39 -05:00
// Indicates whether this tx is a contract creation transaction
contractCreation bool
2014-02-14 16:56:09 -06:00
}
2014-05-22 11:24:04 -05:00
func NewContractCreationTx(value, gas, gasPrice *big.Int, script []byte) *Transaction {
return &Transaction{Recipient: nil, Value: value, Gas: gas, GasPrice: gasPrice, Data: script, contractCreation: true}
}
func NewTransactionMessage(to []byte, value, gas, gasPrice *big.Int, data []byte) *Transaction {
return &Transaction{Recipient: to, Value: value, GasPrice: gasPrice, Gas: gas, Data: data, contractCreation: IsContractAddr(to)}
}
2014-02-25 04:22:18 -06:00
func NewTransactionFromBytes(data []byte) *Transaction {
2014-02-14 16:56:09 -06:00
tx := &Transaction{}
tx.RlpDecode(data)
return tx
}
func NewTransactionFromValue(val *ethutil.Value) *Transaction {
tx := &Transaction{}
tx.RlpValueDecode(val)
return tx
}
2014-06-13 05:45:11 -05:00
func (self *Transaction) GasValue() *big.Int {
return new(big.Int).Mul(self.Gas, self.GasPrice)
}
func (self *Transaction) TotalValue() *big.Int {
v := self.GasValue()
return v.Add(v, self.Value)
}
2014-02-14 16:56:09 -06:00
func (tx *Transaction) Hash() []byte {
data := []interface{}{tx.Nonce, tx.GasPrice, tx.Gas, tx.Recipient, tx.Value, tx.Data}
2014-10-31 06:37:43 -05:00
return crypto.Sha3(ethutil.NewValue(data).Encode())
2014-02-14 16:56:09 -06:00
}
func (tx *Transaction) CreatesContract() bool {
return tx.contractCreation
2014-02-14 16:56:09 -06:00
}
/* Deprecated */
func (tx *Transaction) IsContract() bool {
return tx.CreatesContract()
}
2014-10-31 08:43:14 -05:00
func (tx *Transaction) CreationAddress(state *state.State) []byte {
2014-10-02 10:03:15 -05:00
// Generate a new address
return crypto.Sha3(ethutil.NewValue([]interface{}{tx.Sender(), tx.Nonce}).Encode())[12:]
2014-05-02 07:08:54 -05:00
}
2014-02-14 16:56:09 -06:00
func (tx *Transaction) Signature(key []byte) []byte {
hash := tx.Hash()
sig, _ := secp256k1.Sign(hash, key)
return sig
}
func (tx *Transaction) PublicKey() []byte {
hash := tx.Hash()
2014-07-01 17:05:48 -05:00
// TODO
r := ethutil.LeftPadBytes(tx.r, 32)
s := ethutil.LeftPadBytes(tx.s, 32)
2014-02-14 16:56:09 -06:00
sig := append(r, s...)
2014-02-14 16:56:09 -06:00
sig = append(sig, tx.v-27)
2014-10-31 06:37:43 -05:00
pubkey := crypto.Ecrecover(append(hash, sig...))
//pubkey, _ := secp256k1.RecoverPubkey(hash, sig)
2014-02-14 16:56:09 -06:00
return pubkey
}
func (tx *Transaction) Sender() []byte {
pubkey := tx.PublicKey()
// Validate the returned key.
// Return nil if public key isn't in full format
if len(pubkey) != 0 && pubkey[0] != 4 {
2014-02-14 16:56:09 -06:00
return nil
}
2014-10-31 06:37:43 -05:00
return crypto.Sha3(pubkey[1:])[12:]
2014-02-14 16:56:09 -06:00
}
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) RlpData() interface{} {
2014-05-20 06:06:47 -05:00
data := []interface{}{tx.Nonce, tx.GasPrice, tx.Gas, tx.Recipient, tx.Value, tx.Data}
// TODO Remove prefixing zero's
return append(data, tx.v, new(big.Int).SetBytes(tx.r).Bytes(), new(big.Int).SetBytes(tx.s).Bytes())
2014-02-14 16:56:09 -06:00
}
func (tx *Transaction) RlpValue() *ethutil.Value {
return ethutil.NewValue(tx.RlpData())
}
func (tx *Transaction) RlpEncode() []byte {
return tx.RlpValue().Encode()
}
func (tx *Transaction) RlpDecode(data []byte) {
tx.RlpValueDecode(ethutil.NewValueFromBytes(data))
}
func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) {
tx.Nonce = decoder.Get(0).Uint()
2014-05-20 06:06:47 -05:00
tx.GasPrice = decoder.Get(1).BigInt()
tx.Gas = decoder.Get(2).BigInt()
tx.Recipient = decoder.Get(3).Bytes()
tx.Value = decoder.Get(4).BigInt()
tx.Data = decoder.Get(5).Bytes()
tx.v = byte(decoder.Get(6).Uint())
2014-06-20 13:12:08 -05:00
tx.r = decoder.Get(7).Bytes()
tx.s = decoder.Get(8).Bytes()
2014-06-19 06:42:47 -05:00
if IsContractAddr(tx.Recipient) {
tx.contractCreation = true
}
}
func (tx *Transaction) String() string {
return fmt.Sprintf(`
TX(%x)
Contract: %v
From: %x
2014-05-22 10:56:33 -05:00
To: %x
Nonce: %v
GasPrice: %v
Gas: %v
Value: %v
Data: 0x%x
V: 0x%x
R: 0x%x
S: 0x%x
`,
tx.Hash(),
len(tx.Recipient) == 0,
tx.Sender(),
2014-05-22 10:56:33 -05:00
tx.Recipient,
tx.Nonce,
tx.GasPrice,
tx.Gas,
tx.Value,
tx.Data,
tx.v,
tx.r,
tx.s)
}
// Transaction slice type for basic sorting
type Transactions []*Transaction
func (self Transactions) RlpData() interface{} {
// Marshal the transactions of this block
enc := make([]interface{}, len(self))
for i, tx := range self {
// Cast it to a string (safe)
enc[i] = tx.RlpData()
}
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 ethutil.Rlp(s[i]) }
type TxByNonce struct{ Transactions }
func (s TxByNonce) Less(i, j int) bool {
return s.Transactions[i].Nonce < s.Transactions[j].Nonce
}