2014-02-14 14:56:09 -08:00
|
|
|
package ethchain
|
|
|
|
|
|
|
|
import (
|
2014-05-22 08:35:26 -07:00
|
|
|
"fmt"
|
2014-02-14 14:56:09 -08:00
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
|
|
|
"github.com/obscuren/secp256k1-go"
|
|
|
|
"math/big"
|
|
|
|
)
|
|
|
|
|
2014-02-19 02:35:17 -08: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-02-14 14:56:09 -08:00
|
|
|
type Transaction struct {
|
|
|
|
Nonce uint64
|
|
|
|
Recipient []byte
|
|
|
|
Value *big.Int
|
2014-03-20 16:04:31 -07:00
|
|
|
Gas *big.Int
|
2014-04-18 04:41:07 -07:00
|
|
|
GasPrice *big.Int
|
2014-04-09 09:28:16 -07:00
|
|
|
Data []byte
|
2014-02-14 14:56:09 -08:00
|
|
|
v byte
|
|
|
|
r, s []byte
|
2014-03-27 07:42:39 -07:00
|
|
|
|
|
|
|
// Indicates whether this tx is a contract creation transaction
|
|
|
|
contractCreation bool
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
|
2014-05-22 09:24:04 -07:00
|
|
|
func NewContractCreationTx(value, gas, gasPrice *big.Int, script []byte) *Transaction {
|
|
|
|
return &Transaction{Value: value, Gas: gas, GasPrice: gasPrice, Data: script, contractCreation: true}
|
2014-03-20 16:04:31 -07:00
|
|
|
}
|
|
|
|
|
2014-04-23 02:51:04 -07:00
|
|
|
func NewTransactionMessage(to []byte, value, gas, gasPrice *big.Int, data []byte) *Transaction {
|
|
|
|
return &Transaction{Recipient: to, Value: value, GasPrice: gasPrice, Gas: gas, Data: data}
|
2014-03-20 16:04:31 -07:00
|
|
|
}
|
|
|
|
|
2014-02-25 02:22:18 -08:00
|
|
|
func NewTransactionFromBytes(data []byte) *Transaction {
|
2014-02-14 14:56:09 -08:00
|
|
|
tx := &Transaction{}
|
|
|
|
tx.RlpDecode(data)
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTransactionFromValue(val *ethutil.Value) *Transaction {
|
|
|
|
tx := &Transaction{}
|
|
|
|
tx.RlpValueDecode(val)
|
|
|
|
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Transaction) Hash() []byte {
|
2014-05-22 08:35:26 -07:00
|
|
|
data := []interface{}{tx.Nonce, tx.GasPrice, tx.Gas, tx.Recipient, tx.Value, tx.Data}
|
2014-04-23 02:51:04 -07:00
|
|
|
|
2014-05-22 08:35:26 -07:00
|
|
|
/*
|
|
|
|
if tx.contractCreation {
|
|
|
|
data = append(data, tx.Init)
|
|
|
|
}
|
|
|
|
*/
|
2014-02-14 14:56:09 -08:00
|
|
|
|
2014-04-09 09:28:16 -07:00
|
|
|
return ethutil.Sha3Bin(ethutil.NewValue(data).Encode())
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
|
2014-05-25 06:13:54 -07:00
|
|
|
func (tx *Transaction) CreatesContract() bool {
|
2014-03-27 15:17:14 -07:00
|
|
|
return tx.contractCreation
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
|
|
|
|
2014-05-25 06:13:54 -07:00
|
|
|
/* Depricated */
|
|
|
|
func (tx *Transaction) IsContract() bool {
|
|
|
|
return tx.CreatesContract()
|
|
|
|
}
|
|
|
|
|
2014-05-02 05:08:54 -07:00
|
|
|
func (tx *Transaction) CreationAddress() []byte {
|
2014-05-20 02:19:07 -07:00
|
|
|
return ethutil.Sha3Bin(ethutil.NewValue([]interface{}{tx.Sender(), tx.Nonce}).Encode())[12:]
|
2014-05-02 05:08:54 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 14:56:09 -08: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()
|
|
|
|
|
|
|
|
// If we don't make a copy we will overwrite the existing underlying array
|
|
|
|
dst := make([]byte, len(tx.r))
|
|
|
|
copy(dst, tx.r)
|
|
|
|
|
|
|
|
sig := append(dst, tx.s...)
|
|
|
|
sig = append(sig, tx.v-27)
|
|
|
|
|
|
|
|
pubkey, _ := secp256k1.RecoverPubkey(hash, sig)
|
|
|
|
|
|
|
|
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 pubkey[0] != 4 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ethutil.Sha3Bin(pubkey[1:])[12:]
|
|
|
|
}
|
|
|
|
|
|
|
|
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 04:06:47 -07:00
|
|
|
data := []interface{}{tx.Nonce, tx.GasPrice, tx.Gas, tx.Recipient, tx.Value, tx.Data}
|
2014-03-27 11:49:47 -07:00
|
|
|
|
2014-04-09 09:28:16 -07:00
|
|
|
return append(data, tx.v, tx.r, tx.s)
|
2014-02-14 14:56:09 -08: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 04:06:47 -07: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()
|
2014-04-09 09:28:16 -07:00
|
|
|
tx.Data = decoder.Get(5).Bytes()
|
2014-05-22 08:35:26 -07:00
|
|
|
tx.v = byte(decoder.Get(6).Uint())
|
|
|
|
tx.r = decoder.Get(7).Bytes()
|
|
|
|
tx.s = decoder.Get(8).Bytes()
|
2014-05-25 06:13:54 -07:00
|
|
|
if len(tx.Recipient) == 0 {
|
|
|
|
tx.contractCreation = true
|
|
|
|
}
|
2014-05-22 08:35:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Transaction) String() string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
TX(%x)
|
|
|
|
Contract: %v
|
|
|
|
From: %x
|
2014-05-22 08:56:33 -07:00
|
|
|
To: %x
|
2014-05-22 08:35:26 -07:00
|
|
|
Nonce: %v
|
|
|
|
GasPrice: %v
|
|
|
|
Gas: %v
|
|
|
|
Value: %v
|
|
|
|
Data: 0x%x
|
|
|
|
V: 0x%x
|
|
|
|
R: 0x%x
|
|
|
|
S: 0x%x
|
|
|
|
`,
|
|
|
|
tx.Hash(),
|
2014-05-25 06:13:54 -07:00
|
|
|
len(tx.Recipient) == 0,
|
2014-05-22 08:35:26 -07:00
|
|
|
tx.Sender(),
|
2014-05-22 08:56:33 -07:00
|
|
|
tx.Recipient,
|
2014-05-22 08:35:26 -07:00
|
|
|
tx.Nonce,
|
|
|
|
tx.GasPrice,
|
|
|
|
tx.Gas,
|
|
|
|
tx.Value,
|
|
|
|
tx.Data,
|
|
|
|
tx.v,
|
|
|
|
tx.r,
|
|
|
|
tx.s)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Receipt struct {
|
|
|
|
Tx *Transaction
|
|
|
|
PostState []byte
|
|
|
|
CumulativeGasUsed *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRecieptFromValue(val *ethutil.Value) *Receipt {
|
|
|
|
r := &Receipt{}
|
|
|
|
r.RlpValueDecode(val)
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Receipt) RlpValueDecode(decoder *ethutil.Value) {
|
|
|
|
self.Tx = NewTransactionFromValue(decoder.Get(0))
|
|
|
|
self.PostState = decoder.Get(1).Bytes()
|
|
|
|
self.CumulativeGasUsed = decoder.Get(2).BigInt()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Receipt) RlpData() interface{} {
|
|
|
|
return []interface{}{self.Tx.RlpData(), self.PostState, self.CumulativeGasUsed}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Receipt) String() string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
R
|
|
|
|
Tx:[ %v]
|
|
|
|
PostState: 0x%x
|
|
|
|
CumulativeGasUsed: %v
|
|
|
|
`,
|
|
|
|
self.Tx,
|
|
|
|
self.PostState,
|
|
|
|
self.CumulativeGasUsed)
|
2014-02-14 14:56:09 -08:00
|
|
|
}
|
2014-05-28 03:06:09 -07:00
|
|
|
|
|
|
|
// Transaction slice type for basic sorting
|
|
|
|
type Transactions []*Transaction
|
|
|
|
|
|
|
|
func (s Transactions) Len() int { return len(s) }
|
|
|
|
func (s Transactions) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
|
|
|
|
type TxByNonce struct{ Transactions }
|
|
|
|
|
|
|
|
func (s TxByNonce) Less(i, j int) bool {
|
|
|
|
return s.Transactions[i].Nonce < s.Transactions[j].Nonce
|
|
|
|
}
|