-
Notifications
You must be signed in to change notification settings - Fork 6
/
tx.go
197 lines (169 loc) · 4.86 KB
/
tx.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
package tx
import (
"bytes"
"encoding/hex"
"encoding/json"
"log"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
)
// Helpers allowing for easy transactions through the SDKs
////////////////
/// ETHEREUM ///
////////////////
type EthereumTx struct {
tx *types.Transaction
signer *types.EIP155Signer
hash []byte
}
func (tx *EthereumTx) Tx() *types.Transaction {
return tx.tx
}
func NewEthereumTxWithRlp(encodedRawTx []byte) (*EthereumTx, error) {
var tx types.Transaction
err := rlp.DecodeBytes(encodedRawTx, &tx)
if err != nil {
log.Println("error decoding rlp encoded raw tx")
return nil, err
}
signer := types.NewEIP155Signer(tx.ChainId())
ethTx := &EthereumTx{
tx: &tx,
signer: &signer,
}
return ethTx, nil
}
func NewEthereumTxWithHash(encodedRawTx []byte, hash []byte) (*EthereumTx, error) {
tx, err := NewEthereumTxWithRlp(encodedRawTx)
if err != nil {
return nil, err
}
tx.hash = hash
return tx, nil
}
// NewEthereumTxWithJson creates a new EthereumTx object based on a json encoded transaction
// Used to process a transaction coming from the client
func NewEthereumTxWithJson(jsonData string, chainIdAny any) (*EthereumTx, error) {
var params TransactionParams
err := json.Unmarshal([]byte(jsonData), ¶ms)
if err != nil {
log.Println("error unmarshaling ethereum tx json:", err)
return nil, err
}
chainId := ParseBigInt(chainIdAny)
nonce := ParseBigInt(params.Nonce).Uint64()
value := ParseBigInt(params.Value)
gasLimit := ParseBigInt(params.GasLimit).Uint64()
to := common.HexToAddress(params.To)
data := common.FromHex(params.Data)
var tx *types.Transaction
if params.MaxFeePerGas != nil {
maxFeePerGas := ParseBigInt(params.MaxFeePerGas)
maxPriorityFeePerGas := ParseBigInt(params.MaxPriorityFeePerGas)
tx = types.NewTx(&types.DynamicFeeTx{
ChainID: chainId, // replace with your ChainID
Nonce: nonce,
To: &to,
Value: value,
GasFeeCap: maxFeePerGas,
GasTipCap: maxPriorityFeePerGas,
Gas: gasLimit,
Data: data,
AccessList: params.AccessList,
})
} else {
gasPrice := ParseBigInt(params.GasPrice)
tx = types.NewTx(&types.LegacyTx{
Nonce: nonce,
GasPrice: gasPrice,
Gas: gasLimit,
To: &to,
Value: value,
Data: data,
})
}
signer := types.NewEIP155Signer(chainId)
return &EthereumTx{tx: tx, signer: &signer}, nil
}
// GenerateMessage generates the hash to be signed through the TSS signing process for the transaction
func (tx *EthereumTx) GenerateMessage() []byte {
message := tx.signer.Hash(tx.tx)
tx.hash = message.Bytes()
return tx.hash
}
// Sign adds the signature to tx and returns the rlp encoded raw transaction
// (to be processed by web3.js or other)
func (tx *EthereumTx) Sign(signature []byte) (string, error) {
var err error
// Add signature to tx
tx.tx, err = tx.tx.WithSignature(tx.signer, signature)
if err != nil {
log.Println("Error signing transaction:", err)
return "", err
}
// Compute and return rlp encoded rawTx
buf := new(bytes.Buffer)
err = tx.tx.EncodeRLP(buf)
if err != nil {
log.Println("Error encoding RLP:", err)
return "", err
}
rawTxBytes := buf.Bytes()
rawTxHex := hex.EncodeToString(rawTxBytes)
return rawTxHex, nil
}
type TransactionParams struct {
To string `json:"to"`
Nonce interface{} `json:"nonce"`
Value interface{} `json:"value"`
GasLimit interface{} `json:"gasLimit"`
GasPrice interface{} `json:"gasPrice"`
Data string `json:"data"`
AccessList types.AccessList `json:"accessList"`
MaxFeePerGas interface{} `json:"maxFeePerGas,omitempty"`
MaxPriorityFeePerGas interface{} `json:"maxPriorityFeePerGas,omitempty"`
}
// ParseBigInt takes "something" and transforms it into a bigInt if it can
// Allows for more generic json inputs from the SDKs
// e.g. the nonce could be represented as a hex encoded string, a dec encoded string or an int
func ParseBigInt(value interface{}) *big.Int {
switch v := value.(type) {
case int64:
return big.NewInt(v)
case int:
return big.NewInt(int64(v))
case *big.Int:
return v
case float64:
return big.NewInt(int64(v))
case string:
if len(v) > 2 && v[:2] == "0x" {
result, successful := new(big.Int).SetString(v[2:], 16)
if successful {
return result
} else {
return big.NewInt(0)
}
} else if strings.Contains(v, ".") {
result, successful := new(big.Float).SetString(v)
if successful {
ret, _ := result.Int(nil)
return ret
} else {
return big.NewInt(0)
}
} else {
result, successful := new(big.Int).SetString(v, 10)
if successful {
return result
} else {
return big.NewInt(0)
}
}
default:
return big.NewInt(0)
}
}