2014-02-15 00:56:09 +02:00
|
|
|
package ethchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
|
|
|
"github.com/obscuren/secp256k1-go"
|
|
|
|
"math/big"
|
|
|
|
)
|
|
|
|
|
2014-02-19 12:35:17 +02: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-15 00:56:09 +02:00
|
|
|
type Transaction struct {
|
|
|
|
Nonce uint64
|
|
|
|
Recipient []byte
|
|
|
|
Value *big.Int
|
2014-03-21 01:04:31 +02:00
|
|
|
Gas *big.Int
|
|
|
|
Gasprice *big.Int
|
2014-04-09 19:28:16 +03:00
|
|
|
Data []byte
|
|
|
|
Init []byte
|
2014-02-15 00:56:09 +02:00
|
|
|
v byte
|
|
|
|
r, s []byte
|
2014-03-27 16:42:39 +02:00
|
|
|
|
|
|
|
// Indicates whether this tx is a contract creation transaction
|
|
|
|
contractCreation bool
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
2014-04-09 19:28:16 +03:00
|
|
|
func NewContractCreationTx(value, gasprice *big.Int, data []byte) *Transaction {
|
2014-03-27 16:42:39 +02:00
|
|
|
return &Transaction{Value: value, Gasprice: gasprice, Data: data, contractCreation: true}
|
2014-03-21 01:04:31 +02:00
|
|
|
}
|
|
|
|
|
2014-04-09 19:28:16 +03:00
|
|
|
func NewTransactionMessage(to []byte, value, gasprice, gas *big.Int, data []byte) *Transaction {
|
2014-03-21 01:04:31 +02:00
|
|
|
return &Transaction{Recipient: to, Value: value, Gasprice: gasprice, Gas: gas, Data: data}
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:22:18 +02:00
|
|
|
func NewTransactionFromBytes(data []byte) *Transaction {
|
2014-02-15 00:56:09 +02: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-04-09 19:28:16 +03:00
|
|
|
data := []interface{}{tx.Nonce, tx.Value, tx.Gasprice, tx.Gas, tx.Recipient, string(tx.Data)}
|
|
|
|
if tx.contractCreation {
|
|
|
|
data = append(data, string(tx.Init))
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
2014-04-09 19:28:16 +03:00
|
|
|
return ethutil.Sha3Bin(ethutil.NewValue(data).Encode())
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Transaction) IsContract() bool {
|
2014-03-28 00:17:14 +02:00
|
|
|
return tx.contractCreation
|
2014-02-15 00:56:09 +02: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
|
|
|
|
}
|
|
|
|
|
2014-04-09 19:28:16 +03:00
|
|
|
// [ NONCE, VALUE, GASPRICE, GAS, TO, DATA, V, R, S ]
|
|
|
|
// [ NONCE, VALUE, GASPRICE, GAS, 0, CODE, INIT, V, R, S ]
|
2014-02-15 00:56:09 +02:00
|
|
|
func (tx *Transaction) RlpData() interface{} {
|
2014-04-09 19:28:16 +03:00
|
|
|
data := []interface{}{tx.Nonce, tx.Value, tx.Gasprice, tx.Gas, tx.Recipient, tx.Data}
|
2014-03-27 20:49:47 +02:00
|
|
|
|
2014-04-09 19:28:16 +03:00
|
|
|
if tx.contractCreation {
|
|
|
|
data = append(data, tx.Init)
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
2014-04-09 19:28:16 +03:00
|
|
|
//d := ethutil.NewSliceValue(tx.Data).Slice()
|
2014-03-27 20:49:47 +02:00
|
|
|
|
2014-04-09 19:28:16 +03:00
|
|
|
return append(data, tx.v, tx.r, tx.s)
|
2014-02-15 00:56:09 +02: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-03-27 16:42:39 +02:00
|
|
|
tx.Value = decoder.Get(1).BigInt()
|
|
|
|
tx.Gasprice = decoder.Get(2).BigInt()
|
2014-04-09 19:28:16 +03:00
|
|
|
tx.Gas = decoder.Get(3).BigInt()
|
|
|
|
tx.Recipient = decoder.Get(4).Bytes()
|
|
|
|
tx.Data = decoder.Get(5).Bytes()
|
2014-03-27 16:42:39 +02:00
|
|
|
|
2014-04-09 19:28:16 +03:00
|
|
|
// If the list is of length 10 it's a contract creation tx
|
|
|
|
if decoder.Len() == 10 {
|
2014-03-27 16:42:39 +02:00
|
|
|
tx.contractCreation = true
|
2014-04-09 19:28:16 +03:00
|
|
|
tx.Init = decoder.Get(6).Bytes()
|
2014-03-27 16:42:39 +02:00
|
|
|
|
2014-04-09 19:28:16 +03:00
|
|
|
tx.v = byte(decoder.Get(7).Uint())
|
|
|
|
tx.r = decoder.Get(8).Bytes()
|
|
|
|
tx.s = decoder.Get(9).Bytes()
|
|
|
|
} else {
|
2014-03-27 16:42:39 +02:00
|
|
|
tx.v = byte(decoder.Get(6).Uint())
|
|
|
|
tx.r = decoder.Get(7).Bytes()
|
|
|
|
tx.s = decoder.Get(8).Bytes()
|
2014-02-15 00:56:09 +02:00
|
|
|
}
|
|
|
|
}
|