aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain/transaction.go
blob: 3b07c81d4504482f4a3bae56e4187b9f1951ac44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package ethchain

import (
    "bytes"
    "github.com/ethereum/eth-go/ethutil"
    "github.com/obscuren/secp256k1-go"
    "math/big"
)

var ContractAddr = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

type Transaction struct {
    Nonce     uint64
    Recipient []byte
    Value     *big.Int
    Gas       *big.Int
    Gasprice  *big.Int
    Data      []string
    v         byte
    r, s      []byte
}

func NewTransaction(to []byte, value *big.Int, data []string) *Transaction {
    tx := Transaction{Recipient: to, Value: value, Nonce: 0, Data: data}

    return &tx
}

func NewContractCreationTx(value, gasprice *big.Int, data []string) *Transaction {
    return &Transaction{Value: value, Gasprice: gasprice, Data: data}
}

func NewContractMessageTx(to []byte, value, gasprice, gas *big.Int, data []string) *Transaction {
    return &Transaction{Recipient: to, Value: value, Gasprice: gasprice, Gas: gas, Data: data}
}

func NewTx(to []byte, value *big.Int, data []string) *Transaction {
    return &Transaction{Recipient: to, Value: value, Gasprice: big.NewInt(0), Gas: big.NewInt(0), Nonce: 0, Data: data}
}

// XXX Deprecated
func NewTransactionFromData(data []byte) *Transaction {
    return NewTransactionFromBytes(data)
}

func NewTransactionFromBytes(data []byte) *Transaction {
    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 {
    data := make([]interface{}, len(tx.Data))
    for i, val := range tx.Data {
        data[i] = val
    }

    preEnc := []interface{}{
        tx.Nonce,
        tx.Recipient,
        tx.Value,
        data,
    }

    return ethutil.Sha3Bin(ethutil.Encode(preEnc))
}

func (tx *Transaction) IsContract() bool {
    return bytes.Compare(tx.Recipient, ContractAddr) == 0
}

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{} {
    // Prepare the transaction for serialization
    return []interface{}{
        tx.Nonce,
        tx.Recipient,
        tx.Value,
        ethutil.NewSliceValue(tx.Data).Slice(),
        tx.v,
        tx.r,
        tx.s,
    }
}

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()
    tx.Recipient = decoder.Get(1).Bytes()
    tx.Value = decoder.Get(2).BigInt()

    d := decoder.Get(3)
    tx.Data = make([]string, d.Len())
    for i := 0; i < d.Len(); i++ {
        tx.Data[i] = d.Get(i).Str()
    }

    // TODO something going wrong here
    tx.v = byte(decoder.Get(4).Uint())
    tx.r = decoder.Get(5).Bytes()
    tx.s = decoder.Get(6).Bytes()
}