-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
eth_legacy_homestead_transactions.go
228 lines (190 loc) · 6.18 KB
/
eth_legacy_homestead_transactions.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package ethtypes
import (
"fmt"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/big"
typescrypto "github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/lotus/chain/types"
)
var _ EthTransaction = (*EthLegacyHomesteadTxArgs)(nil)
type EthLegacyHomesteadTxArgs struct {
Nonce int `json:"nonce"`
GasPrice big.Int `json:"gasPrice"`
GasLimit int `json:"gasLimit"`
To *EthAddress `json:"to"`
Value big.Int `json:"value"`
Input []byte `json:"input"`
V big.Int `json:"v"`
R big.Int `json:"r"`
S big.Int `json:"s"`
}
func (tx *EthLegacyHomesteadTxArgs) ToEthTx(smsg *types.SignedMessage) (EthTx, error) {
from, err := EthAddressFromFilecoinAddress(smsg.Message.From)
if err != nil {
return EthTx{}, fmt.Errorf("sender was not an eth account")
}
hash, err := tx.TxHash()
if err != nil {
return EthTx{}, fmt.Errorf("failed to get tx hash: %w", err)
}
gasPrice := EthBigInt(tx.GasPrice)
ethTx := EthTx{
ChainID: EthLegacyHomesteadTxChainID,
Type: EthLegacyTxType,
Nonce: EthUint64(tx.Nonce),
Hash: hash,
To: tx.To,
Value: EthBigInt(tx.Value),
Input: tx.Input,
Gas: EthUint64(tx.GasLimit),
GasPrice: &gasPrice,
From: from,
R: EthBigInt(tx.R),
S: EthBigInt(tx.S),
V: EthBigInt(tx.V),
}
return ethTx, nil
}
func (tx *EthLegacyHomesteadTxArgs) ToUnsignedFilecoinMessage(from address.Address) (*types.Message, error) {
mi, err := getFilecoinMethodInfo(tx.To, tx.Input)
if err != nil {
return nil, xerrors.Errorf("failed to get method info: %w", err)
}
return &types.Message{
Version: 0,
To: mi.to,
From: from,
Nonce: uint64(tx.Nonce),
Value: tx.Value,
GasLimit: int64(tx.GasLimit),
GasFeeCap: tx.GasPrice,
GasPremium: tx.GasPrice,
Method: mi.method,
Params: mi.params,
}, nil
}
func (tx *EthLegacyHomesteadTxArgs) ToVerifiableSignature(sig []byte) ([]byte, error) {
if len(sig) != EthLegacyHomesteadTxSignatureLen {
return nil, fmt.Errorf("signature should be %d bytes long (1 byte metadata, %d bytes sig data), but got %d bytes",
EthLegacyHomesteadTxSignatureLen, EthLegacyHomesteadTxSignatureLen-1, len(sig))
}
if sig[0] != EthLegacyHomesteadTxSignaturePrefix {
return nil, fmt.Errorf("expected signature prefix 0x%x, but got 0x%x", EthLegacyHomesteadTxSignaturePrefix, sig[0])
}
// Remove the prefix byte as it's only used for legacy transaction identification
sig = sig[1:]
// Extract the 'v' value from the signature, which is the last byte in Ethereum signatures
vValue := big.NewFromGo(big.NewInt(0).SetBytes(sig[64:]))
// Adjust 'v' value for compatibility with new transactions: 27 -> 0, 28 -> 1
if vValue.Equals(big.NewInt(27)) {
sig[64] = 0
} else if vValue.Equals(big.NewInt(28)) {
sig[64] = 1
} else {
return nil, fmt.Errorf("invalid 'v' value: expected 27 or 28, got %d", vValue.Int64())
}
return sig, nil
}
func (tx *EthLegacyHomesteadTxArgs) ToRlpUnsignedMsg() ([]byte, error) {
return toRlpUnsignedMsg(tx)
}
func (tx *EthLegacyHomesteadTxArgs) TxHash() (EthHash, error) {
rlp, err := tx.ToRlpSignedMsg()
if err != nil {
return EthHash{}, err
}
return EthHashFromTxBytes(rlp), nil
}
func (tx *EthLegacyHomesteadTxArgs) ToRlpSignedMsg() ([]byte, error) {
return toRlpSignedMsg(tx, tx.V, tx.R, tx.S)
}
func (tx *EthLegacyHomesteadTxArgs) Signature() (*typescrypto.Signature, error) {
// throw an error if the v value is not 27 or 28
if !tx.V.Equals(big.NewInt(27)) && !tx.V.Equals(big.NewInt(28)) {
return nil, fmt.Errorf("legacy homestead transactions only support 27 or 28 for v")
}
r := tx.R.Int.Bytes()
s := tx.S.Int.Bytes()
v := tx.V.Int.Bytes()
sig := append([]byte{}, padLeadingZeros(r, 32)...)
sig = append(sig, padLeadingZeros(s, 32)...)
if len(v) == 0 {
sig = append(sig, 0)
} else {
sig = append(sig, v[0])
}
// pre-pend a one byte marker so nodes know that this is a legacy transaction
sig = append([]byte{EthLegacyHomesteadTxSignaturePrefix}, sig...)
if len(sig) != EthLegacyHomesteadTxSignatureLen {
return nil, fmt.Errorf("signature is not %d bytes", EthLegacyHomesteadTxSignatureLen)
}
return &typescrypto.Signature{
Type: typescrypto.SigTypeDelegated, Data: sig,
}, nil
}
func (tx *EthLegacyHomesteadTxArgs) Sender() (address.Address, error) {
return sender(tx)
}
func (tx *EthLegacyHomesteadTxArgs) Type() int {
return EthLegacyTxType
}
func (tx *EthLegacyHomesteadTxArgs) InitialiseSignature(sig typescrypto.Signature) error {
if sig.Type != typescrypto.SigTypeDelegated {
return fmt.Errorf("RecoverSignature only supports Delegated signature")
}
if len(sig.Data) != EthLegacyHomesteadTxSignatureLen {
return fmt.Errorf("signature should be %d bytes long, but got %d bytes", EthLegacyHomesteadTxSignatureLen, len(sig.Data))
}
if sig.Data[0] != EthLegacyHomesteadTxSignaturePrefix {
return fmt.Errorf("expected signature prefix 0x01, but got 0x%x", sig.Data[0])
}
// ignore the first byte of the signature as it's only used for legacy transaction identification
r_, err := parseBigInt(sig.Data[1:33])
if err != nil {
return fmt.Errorf("cannot parse r into EthBigInt: %w", err)
}
s_, err := parseBigInt(sig.Data[33:65])
if err != nil {
return fmt.Errorf("cannot parse s into EthBigInt: %w", err)
}
v_, err := parseBigInt([]byte{sig.Data[65]})
if err != nil {
return fmt.Errorf("cannot parse v into EthBigInt: %w", err)
}
if !v_.Equals(big.NewInt(27)) && !v_.Equals(big.NewInt(28)) {
return fmt.Errorf("legacy homestead transactions only support 27 or 28 for v")
}
tx.R = r_
tx.S = s_
tx.V = v_
return nil
}
func (tx *EthLegacyHomesteadTxArgs) packTxFields() ([]interface{}, error) {
nonce, err := formatInt(tx.Nonce)
if err != nil {
return nil, err
}
// format gas price
gasPrice, err := formatBigInt(tx.GasPrice)
if err != nil {
return nil, err
}
gasLimit, err := formatInt(tx.GasLimit)
if err != nil {
return nil, err
}
value, err := formatBigInt(tx.Value)
if err != nil {
return nil, err
}
res := []interface{}{
nonce,
gasPrice,
gasLimit,
formatEthAddr(tx.To),
value,
tx.Input,
}
return res, nil
}