-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
eth_legacy_155_transactions.go
303 lines (248 loc) · 9.14 KB
/
eth_legacy_155_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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package ethtypes
import (
"fmt"
"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/build/buildconstants"
"github.com/filecoin-project/lotus/chain/types"
)
var _ EthTransaction = (*EthLegacy155TxArgs)(nil)
// EthLegacy155TxArgs is a legacy Ethereum transaction that uses the EIP-155 chain replay protection mechanism
// by incorporating the chainId in the signature.
// See how the `V` value in the signature is derived from the chainId at
// https://github.com/ethereum/go-ethereum/blob/86a1f0c39494c8f5caddf6bd9fbddd4bdfa944fd/core/types/transaction_signing.go#L424
// For EthLegacy155TxArgs, the digest that is used to create a signed transaction includes the `ChainID` but the serialised RLP transaction
// does not include the `ChainID` as an explicit field. Instead, the `ChainID` is included in the V value of the signature as mentioned above.
type EthLegacy155TxArgs struct {
legacyTx *EthLegacyHomesteadTxArgs
}
func NewEthLegacy155TxArgs(tx *EthLegacyHomesteadTxArgs) *EthLegacy155TxArgs {
return &EthLegacy155TxArgs{legacyTx: tx}
}
func (tx *EthLegacy155TxArgs) GetLegacyTx() *EthLegacyHomesteadTxArgs {
return tx.legacyTx
}
func (tx *EthLegacy155TxArgs) 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.legacyTx.GasPrice)
ethTx := EthTx{
ChainID: buildconstants.Eip155ChainId,
Type: EthLegacyTxType,
Nonce: EthUint64(tx.legacyTx.Nonce),
Hash: hash,
To: tx.legacyTx.To,
Value: EthBigInt(tx.legacyTx.Value),
Input: tx.legacyTx.Input,
Gas: EthUint64(tx.legacyTx.GasLimit),
GasPrice: &gasPrice,
From: from,
R: EthBigInt(tx.legacyTx.R),
S: EthBigInt(tx.legacyTx.S),
V: EthBigInt(tx.legacyTx.V),
}
return ethTx, nil
}
func (tx *EthLegacy155TxArgs) ToUnsignedFilecoinMessage(from address.Address) (*types.Message, error) {
if err := validateEIP155ChainId(tx.legacyTx.V); err != nil {
return nil, fmt.Errorf("failed to validate EIP155 chain id: %w", err)
}
return tx.legacyTx.ToUnsignedFilecoinMessage(from)
}
func (tx *EthLegacy155TxArgs) ToRlpUnsignedMsg() ([]byte, error) {
return toRlpUnsignedMsg(tx)
}
func (tx *EthLegacy155TxArgs) TxHash() (EthHash, error) {
encoded, err := tx.ToRawTxBytesSigned()
if err != nil {
return EthHash{}, fmt.Errorf("failed to encode rlp signed msg: %w", err)
}
return EthHashFromTxBytes(encoded), nil
}
func (tx *EthLegacy155TxArgs) ToRawTxBytesSigned() ([]byte, error) {
packed1, err := tx.packTxFields()
if err != nil {
return nil, err
}
packed1 = packed1[:len(packed1)-3] // remove chainId, r and s as they are only used for signature verification
packed2, err := packSigFields(tx.legacyTx.V, tx.legacyTx.R, tx.legacyTx.S)
if err != nil {
return nil, err
}
encoded, err := EncodeRLP(append(packed1, packed2...))
if err != nil {
return nil, fmt.Errorf("failed to encode rlp signed msg: %w", err)
}
return encoded, nil
}
func (tx *EthLegacy155TxArgs) ToRlpSignedMsg() ([]byte, error) {
return toRlpSignedMsg(tx, tx.legacyTx.V, tx.legacyTx.R, tx.legacyTx.S)
}
func (tx *EthLegacy155TxArgs) Signature() (*typescrypto.Signature, error) {
if err := validateEIP155ChainId(tx.legacyTx.V); err != nil {
return nil, fmt.Errorf("failed to validate EIP155 chain id: %w", err)
}
r := tx.legacyTx.R.Int.Bytes()
s := tx.legacyTx.S.Int.Bytes()
v := tx.legacyTx.V.Int.Bytes()
sig := append([]byte{}, padLeadingZeros(r, 32)...)
sig = append(sig, padLeadingZeros(s, 32)...)
sig = append(sig, v...)
// pre-pend a one byte marker so nodes know that this is a legacy transaction
sig = append([]byte{EthLegacy155TxSignaturePrefix}, sig...)
if len(sig) != EthLegacy155TxSignatureLen0 && len(sig) != EthLegacy155TxSignatureLen1 {
return nil, fmt.Errorf("signature is not %d OR %d bytes; it is %d bytes", EthLegacy155TxSignatureLen0, EthLegacy155TxSignatureLen1,
len(sig))
}
return &typescrypto.Signature{
Type: typescrypto.SigTypeDelegated, Data: sig,
}, nil
}
func (tx *EthLegacy155TxArgs) Sender() (address.Address, error) {
if err := validateEIP155ChainId(tx.legacyTx.V); err != nil {
return address.Address{}, fmt.Errorf("failed to validate EIP155 chain id: %w", err)
}
return sender(tx)
}
func (tx *EthLegacy155TxArgs) Type() int {
return EthLegacyTxType
}
var big8 = big.NewInt(8)
func (tx *EthLegacy155TxArgs) ToVerifiableSignature(sig []byte) ([]byte, error) {
if len(sig) != EthLegacy155TxSignatureLen0 && len(sig) != EthLegacy155TxSignatureLen1 {
return nil, fmt.Errorf("signature should be %d or %d bytes long but got %d bytes",
EthLegacy155TxSignatureLen0, EthLegacy155TxSignatureLen1, len(sig))
}
if sig[0] != EthLegacy155TxSignaturePrefix {
return nil, fmt.Errorf("expected signature prefix 0x%x, but got 0x%x", EthLegacy155TxSignaturePrefix, 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
vValue := big.NewFromGo(big.NewInt(0).SetBytes(sig[64:]))
if err := validateEIP155ChainId(vValue); err != nil {
return nil, fmt.Errorf("failed to validate EIP155 chain id: %w", err)
}
// See https://github.com/ethereum/go-ethereum/blob/86a1f0c39494c8f5caddf6bd9fbddd4bdfa944fd/core/types/transaction_signing.go#L424
chainIdMul := big.Mul(big.NewIntUnsigned(buildconstants.Eip155ChainId), big.NewInt(2))
vValue = big.Sub(vValue, chainIdMul)
vValue = big.Sub(vValue, big8)
// 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[0:65], nil
}
func (tx *EthLegacy155TxArgs) InitialiseSignature(sig typescrypto.Signature) error {
if sig.Type != typescrypto.SigTypeDelegated {
return fmt.Errorf("RecoverSignature only supports Delegated signature")
}
if len(sig.Data) != EthLegacy155TxSignatureLen0 && len(sig.Data) != EthLegacy155TxSignatureLen1 {
return fmt.Errorf("signature should be %d or %d bytes long, but got %d bytes", EthLegacy155TxSignatureLen0,
EthLegacy155TxSignatureLen1, len(sig.Data))
}
if sig.Data[0] != EthLegacy155TxSignaturePrefix {
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(sig.Data[65:])
if err != nil {
return fmt.Errorf("cannot parse v into EthBigInt: %w", err)
}
if err := validateEIP155ChainId(v_); err != nil {
return fmt.Errorf("failed to validate EIP155 chain id: %w", err)
}
tx.legacyTx.R = r_
tx.legacyTx.S = s_
tx.legacyTx.V = v_
return nil
}
func (tx *EthLegacy155TxArgs) packTxFields() ([]interface{}, error) {
nonce, err := formatInt(tx.legacyTx.Nonce)
if err != nil {
return nil, err
}
// format gas price
gasPrice, err := formatBigInt(tx.legacyTx.GasPrice)
if err != nil {
return nil, err
}
gasLimit, err := formatInt(tx.legacyTx.GasLimit)
if err != nil {
return nil, err
}
value, err := formatBigInt(tx.legacyTx.Value)
if err != nil {
return nil, err
}
chainIdBigInt := big.NewIntUnsigned(buildconstants.Eip155ChainId)
chainId, err := formatBigInt(chainIdBigInt)
if err != nil {
return nil, err
}
r, err := formatInt(0)
if err != nil {
return nil, err
}
s, err := formatInt(0)
if err != nil {
return nil, err
}
res := []interface{}{
nonce,
gasPrice,
gasLimit,
formatEthAddr(tx.legacyTx.To),
value,
tx.legacyTx.Input,
chainId,
r, s,
}
return res, nil
}
func validateEIP155ChainId(v big.Int) error {
chainId := deriveEIP155ChainId(v)
if !chainId.Equals(big.NewIntUnsigned(buildconstants.Eip155ChainId)) {
return fmt.Errorf("invalid chain id, expected %d, got %s", buildconstants.Eip155ChainId, chainId.String())
}
return nil
}
// deriveEIP155ChainId derives the chain id from the given v parameter
func deriveEIP155ChainId(v big.Int) big.Int {
if big.BitLen(v) <= 64 {
vUint64 := v.Uint64()
if vUint64 == 27 || vUint64 == 28 {
return big.NewInt(0)
}
return big.NewIntUnsigned((vUint64 - 35) / 2)
}
v = big.Sub(v, big.NewInt(35))
return big.Div(v, big.NewInt(2))
}
func calcEIP155TxSignatureLen(chain uint64, v int) int {
chainId := big.NewIntUnsigned(chain)
vVal := big.Add(big.Mul(chainId, big.NewInt(2)), big.NewInt(int64(v)))
vLen := len(vVal.Int.Bytes())
// EthLegacyHomesteadTxSignatureLen includes the 1 byte legacy tx marker prefix and also 1 byte for the V value.
// So we subtract 1 to not double count the length of the v value
return EthLegacyHomesteadTxSignatureLen + vLen - 1
}