-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsafe.go
212 lines (186 loc) · 6.62 KB
/
safe.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
package bridge
import (
"bytes"
"context"
"encoding/json"
"fmt"
"math/big"
"net/http"
"github.com/G7DAO/protocol/bindings/GnosisSafe"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
)
// OperationType represents the type of operation for a Safe transaction
type OperationType uint8
const (
Call OperationType = 0
DelegateCall OperationType = 1
)
// String returns the string representation of the OperationType
func (o OperationType) String() string {
switch o {
case Call:
return "Call"
case DelegateCall:
return "DelegateCall"
default:
return "Unknown"
}
}
// SafeTransactionData represents the data for a Safe transaction
type SafeTransactionData struct {
To string `json:"to"`
Value string `json:"value"`
Data string `json:"data"`
Operation OperationType `json:"operation"`
SafeTxGas uint64 `json:"safeTxGas"`
BaseGas uint64 `json:"baseGas"`
GasPrice string `json:"gasPrice"`
GasToken string `json:"gasToken"`
RefundReceiver string `json:"refundReceiver"`
Nonce *big.Int `json:"nonce"`
SafeTxHash string `json:"safeTxHash"`
Sender string `json:"sender"`
Signature string `json:"signature"`
Origin string `json:"origin"`
}
const (
NativeTokenAddress = "0x0000000000000000000000000000000000000000"
)
func CreateSafeProposal(client *ethclient.Client, key *keystore.Key, safeAddress common.Address, to common.Address, data []byte, value *big.Int, safeApi string, safeOperation OperationType, safeNonce *big.Int) error {
chainID, err := client.ChainID(context.Background())
if err != nil {
return fmt.Errorf("failed to get chain ID: %v", err)
}
// Create a new instance of the GnosisSafe contract
safeInstance, err := GnosisSafe.NewGnosisSafe(safeAddress, client)
if err != nil {
return fmt.Errorf("failed to create GnosisSafe instance: %v", err)
}
nonce := big.NewInt(0)
if safeNonce == nil {
// Fetch the current nonce from the Safe contract
fetchNonce, err := safeInstance.Nonce(&bind.CallOpts{})
if err != nil {
return fmt.Errorf("failed to fetch nonce from Safe contract: %v", err)
}
nonce = fetchNonce
} else {
nonce = safeNonce
}
safeTransactionData := SafeTransactionData{
To: to.Hex(),
Value: value.String(),
Data: common.Bytes2Hex(data),
Operation: safeOperation,
SafeTxGas: 0,
BaseGas: 0,
GasPrice: "0",
GasToken: NativeTokenAddress,
RefundReceiver: NativeTokenAddress,
Nonce: nonce,
}
// Calculate SafeTxHash
safeTxHash, err := CalculateSafeTxHash(safeAddress, safeTransactionData, chainID)
if err != nil {
return fmt.Errorf("failed to calculate SafeTxHash: %v", err)
}
// Sign the SafeTxHash
signature, err := crypto.Sign(safeTxHash.Bytes(), key.PrivateKey)
if err != nil {
return fmt.Errorf("failed to sign SafeTxHash: %v", err)
}
// Adjust V value for Ethereum's replay protection
signature[64] += 27
// Convert signature to hex
senderSignature := "0x" + common.Bytes2Hex(signature)
// Prepare the request body
requestBody := map[string]interface{}{
"to": safeTransactionData.To,
"value": safeTransactionData.Value,
"data": "0x" + safeTransactionData.Data,
"operation": int(safeTransactionData.Operation),
"safeTxGas": fmt.Sprintf("%d", safeTransactionData.SafeTxGas),
"baseGas": fmt.Sprintf("%d", safeTransactionData.BaseGas),
"gasPrice": safeTransactionData.GasPrice,
"gasToken": safeTransactionData.GasToken,
"refundReceiver": safeTransactionData.RefundReceiver,
"nonce": fmt.Sprintf("%d", safeTransactionData.Nonce),
"safeTxHash": safeTxHash.Hex(),
"sender": key.Address.Hex(),
"signature": senderSignature,
"origin": fmt.Sprintf("{\"url\":\"%s\",\"name\":\"TokenSender Deployment\"}", safeApi),
}
// Marshal the request body to JSON
jsonBody, err := json.Marshal(requestBody)
if err != nil {
return fmt.Errorf("failed to marshal request body: %v", err)
}
// Send the request to the Safe Transaction Service
req, err := http.NewRequest("POST", safeApi, bytes.NewBuffer(jsonBody))
if err != nil {
return fmt.Errorf("failed to create request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
fmt.Println("Safe proposal created successfully")
return nil
}
func CalculateSafeTxHash(safeAddress common.Address, txData SafeTransactionData, chainID *big.Int) (common.Hash, error) {
domainSeparator := apitypes.TypedDataDomain{
ChainId: (*math.HexOrDecimal256)(chainID),
VerifyingContract: safeAddress.Hex(),
}
typedData := apitypes.TypedData{
Types: apitypes.Types{
"EIP712Domain": []apitypes.Type{
{Name: "chainId", Type: "uint256"},
{Name: "verifyingContract", Type: "address"},
},
"SafeTx": []apitypes.Type{
{Name: "to", Type: "address"},
{Name: "value", Type: "uint256"},
{Name: "data", Type: "bytes"},
{Name: "operation", Type: "uint8"},
{Name: "safeTxGas", Type: "uint256"},
{Name: "baseGas", Type: "uint256"},
{Name: "gasPrice", Type: "uint256"},
{Name: "gasToken", Type: "address"},
{Name: "refundReceiver", Type: "address"},
{Name: "nonce", Type: "uint256"},
},
},
Domain: domainSeparator,
PrimaryType: "SafeTx",
Message: apitypes.TypedDataMessage{
"to": txData.To,
"value": txData.Value,
"data": "0x" + txData.Data,
"operation": fmt.Sprintf("%d", txData.Operation),
"safeTxGas": fmt.Sprintf("%d", txData.SafeTxGas),
"baseGas": fmt.Sprintf("%d", txData.BaseGas),
"gasPrice": txData.GasPrice,
"gasToken": txData.GasToken,
"refundReceiver": txData.RefundReceiver,
"nonce": fmt.Sprintf("%d", txData.Nonce),
},
}
typedDataHash, _, err := apitypes.TypedDataAndHash(typedData)
if err != nil {
return common.Hash{}, fmt.Errorf("failed to hash typed data: %v", err)
}
return common.BytesToHash(typedDataHash), nil
}