transaction.go 4.7 KB
Newer Older
1
package types
O
obscuren 已提交
2 3

import (
O
obscuren 已提交
4
	"bytes"
5
	"crypto/ecdsa"
O
obscuren 已提交
6
	"fmt"
7 8
	"math/big"

O
obscuren 已提交
9
	"github.com/ethereum/go-ethereum/crypto"
O
obscuren 已提交
10
	"github.com/ethereum/go-ethereum/crypto/secp256k1"
11
	"github.com/ethereum/go-ethereum/ethutil"
O
obscuren 已提交
12
	"github.com/ethereum/go-ethereum/rlp"
O
obscuren 已提交
13 14
)

15
func IsContractAddr(addr []byte) bool {
16
	return len(addr) == 0
17 18
}

O
obscuren 已提交
19
type Transaction struct {
O
obscuren 已提交
20
	AccountNonce uint64
O
obscuren 已提交
21 22
	Price        *big.Int
	GasLimit     *big.Int
O
obscuren 已提交
23 24 25 26 27
	Recipient    []byte
	Amount       *big.Int
	Payload      []byte
	V            uint64
	R, S         []byte
O
obscuren 已提交
28 29
}

O
obscuren 已提交
30 31
func NewContractCreationTx(Amount, gasAmount, price *big.Int, data []byte) *Transaction {
	return NewTransactionMessage(nil, Amount, gasAmount, price, data)
32 33
}

O
obscuren 已提交
34
func NewTransactionMessage(to []byte, Amount, gasAmount, price *big.Int, data []byte) *Transaction {
O
obscuren 已提交
35
	return &Transaction{Recipient: to, Amount: Amount, Price: price, GasLimit: gasAmount, Payload: data}
36 37
}

O
obscuren 已提交
38
func NewTransactionFromBytes(data []byte) *Transaction {
O
obscuren 已提交
39 40 41 42 43 44
	tx := &Transaction{}
	tx.RlpDecode(data)

	return tx
}

O
obscuren 已提交
45
func NewTransactionFromAmount(val *ethutil.Value) *Transaction {
O
obscuren 已提交
46 47 48 49 50 51 52
	tx := &Transaction{}
	tx.RlpValueDecode(val)

	return tx
}

func (tx *Transaction) Hash() []byte {
O
obscuren 已提交
53
	data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload}
54

O
obscuren 已提交
55
	return crypto.Sha3(ethutil.Encode(data))
O
obscuren 已提交
56 57
}

58
func (self *Transaction) Data() []byte {
O
obscuren 已提交
59
	return self.Payload
60 61 62
}

func (self *Transaction) Gas() *big.Int {
O
obscuren 已提交
63
	return self.GasLimit
64 65 66
}

func (self *Transaction) GasPrice() *big.Int {
O
obscuren 已提交
67
	return self.Price
68 69 70
}

func (self *Transaction) Value() *big.Int {
O
obscuren 已提交
71
	return self.Amount
72 73 74
}

func (self *Transaction) Nonce() uint64 {
O
obscuren 已提交
75
	return self.AccountNonce
76 77
}

O
obscuren 已提交
78 79
func (self *Transaction) SetNonce(AccountNonce uint64) {
	self.AccountNonce = AccountNonce
80 81 82
}

func (self *Transaction) From() []byte {
83
	return self.sender()
84 85 86
}

func (self *Transaction) To() []byte {
O
obscuren 已提交
87
	return self.Recipient
88 89
}

O
obscuren 已提交
90
func (tx *Transaction) Curve() (v byte, r []byte, s []byte) {
O
obscuren 已提交
91 92 93
	v = byte(tx.V)
	r = ethutil.LeftPadBytes(tx.R, 32)
	s = ethutil.LeftPadBytes(tx.S, 32)
O
obscuren 已提交
94 95 96 97

	return
}

O
obscuren 已提交
98 99 100 101 102 103 104 105 106 107 108
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()

O
obscuren 已提交
109
	v, r, s := tx.Curve()
O
obscuren 已提交
110

111
	sig := append(r, s...)
O
obscuren 已提交
112
	sig = append(sig, v-27)
O
obscuren 已提交
113

114 115
	//pubkey := crypto.Ecrecover(append(hash, sig...))
	pubkey, _ := secp256k1.RecoverPubkey(hash, sig)
O
obscuren 已提交
116 117 118 119

	return pubkey
}

120
func (tx *Transaction) sender() []byte {
O
obscuren 已提交
121 122 123 124
	pubkey := tx.PublicKey()

	// Validate the returned key.
	// Return nil if public key isn't in full format
E
Ethan Buchman 已提交
125
	if len(pubkey) == 0 || pubkey[0] != 4 {
O
obscuren 已提交
126 127 128
		return nil
	}

O
obscuren 已提交
129
	return crypto.Sha3(pubkey[1:])[12:]
O
obscuren 已提交
130 131 132 133 134 135
}

func (tx *Transaction) Sign(privk []byte) error {

	sig := tx.Signature(privk)

O
obscuren 已提交
136 137 138
	tx.R = sig[:32]
	tx.S = sig[32:64]
	tx.V = uint64(sig[64] + 27)
O
obscuren 已提交
139 140 141 142

	return nil
}

143 144 145 146
func (tx *Transaction) SignECDSA(key *ecdsa.PrivateKey) error {
	return tx.Sign(crypto.FromECDSA(key))
}

O
obscuren 已提交
147
func (tx *Transaction) RlpData() interface{} {
O
obscuren 已提交
148
	data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload}
149

O
obscuren 已提交
150
	return append(data, tx.V, new(big.Int).SetBytes(tx.R).Bytes(), new(big.Int).SetBytes(tx.S).Bytes())
O
obscuren 已提交
151 152 153
}

func (tx *Transaction) RlpEncode() []byte {
O
obscuren 已提交
154
	return ethutil.Encode(tx)
O
obscuren 已提交
155 156 157
}

func (tx *Transaction) RlpDecode(data []byte) {
O
obscuren 已提交
158
	rlp.Decode(bytes.NewReader(data), tx)
O
obscuren 已提交
159 160 161
}

func (tx *Transaction) RlpValueDecode(decoder *ethutil.Value) {
O
obscuren 已提交
162 163
	tx.AccountNonce = decoder.Get(0).Uint()
	tx.Price = decoder.Get(1).BigInt()
O
obscuren 已提交
164
	tx.GasLimit = decoder.Get(2).BigInt()
O
obscuren 已提交
165 166 167 168 169 170
	tx.Recipient = decoder.Get(3).Bytes()
	tx.Amount = decoder.Get(4).BigInt()
	tx.Payload = decoder.Get(5).Bytes()
	tx.V = decoder.Get(6).Uint()
	tx.R = decoder.Get(7).Bytes()
	tx.S = decoder.Get(8).Bytes()
O
obscuren 已提交
171 172 173 174 175 176 177
}

func (tx *Transaction) String() string {
	return fmt.Sprintf(`
	TX(%x)
	Contract: %v
	From:     %x
O
obscuren 已提交
178
	To:       %x
O
obscuren 已提交
179 180 181 182
	Nonce:    %v
	GasPrice: %v
	GasLimit  %v
	Value:    %v
O
obscuren 已提交
183 184 185 186
	Data:     0x%x
	V:        0x%x
	R:        0x%x
	S:        0x%x
187
	Hex:      %x
O
obscuren 已提交
188
`,
O
obscuren 已提交
189
		tx.Hash(),
O
obscuren 已提交
190
		len(tx.Recipient) == 0,
191
		tx.From(),
O
obscuren 已提交
192
		tx.To(),
O
obscuren 已提交
193 194
		tx.AccountNonce,
		tx.Price,
O
obscuren 已提交
195
		tx.GasLimit,
O
obscuren 已提交
196 197 198 199 200
		tx.Amount,
		tx.Payload,
		tx.V,
		tx.R,
		tx.S,
201 202
		ethutil.Encode(tx),
	)
O
obscuren 已提交
203 204
}

O
obscuren 已提交
205 206 207
// Transaction slice type for basic sorting
type Transactions []*Transaction

O
obscuren 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220
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]) }
O
obscuren 已提交
221 222 223 224

type TxByNonce struct{ Transactions }

func (s TxByNonce) Less(i, j int) bool {
O
obscuren 已提交
225
	return s.Transactions[i].AccountNonce < s.Transactions[j].AccountNonce
O
obscuren 已提交
226
}