-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
200 lines (174 loc) · 5.05 KB
/
main.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
package main
import (
"context"
"fmt"
"math/big"
"os"
"time"
"github.com/base-org/op-enclave/bindings"
"github.com/base-org/op-enclave/op-withdrawer/withdrawals"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum-optimism/optimism/op-service/ctxinterrupt"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/ethclient/gethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/urfave/cli/v2"
)
var (
L1URLFlag = &cli.StringFlag{
Name: "l1-url",
Usage: "URL of an L1 RPC host",
EnvVars: []string{"L1_URL"},
Required: true,
}
L2URLFlag = &cli.StringFlag{
Name: "l2-url",
Usage: "URL of an L2 RPC host",
EnvVars: []string{"L2_URL"},
Required: true,
}
PortalAddressFlag = &cli.StringFlag{
Name: "portal-address",
Usage: "Optimism Portal address",
EnvVars: []string{"PORTAL_ADDRESS"},
Required: true,
}
PrivateKeyFlag = &cli.StringFlag{
Name: "private-key",
Usage: "Private key to sign the transaction",
EnvVars: []string{"PRIVATE_KEY"},
Required: true,
}
)
func main() {
oplog.SetupDefaults()
app := cli.NewApp()
app.Name = "withdrawer"
app.Usage = "Withdraws funds from L2 to L1"
app.Action = func(c *cli.Context) error {
return cli.ShowAppHelp(c)
}
app.Commands = []*cli.Command{
{
Name: "depositHash",
Usage: "Calculate L2 deposit hash from L1 deposit tx",
Action: DepositHash,
Flags: []cli.Flag{
L1URLFlag,
PortalAddressFlag,
},
},
{
Name: "proveWithdrawal",
Usage: "Prove and finalize an L2 -> L1 withdrawal",
Action: Main,
Flags: []cli.Flag{
L1URLFlag,
L2URLFlag,
PortalAddressFlag,
PrivateKeyFlag,
},
},
}
ctx := ctxinterrupt.WithSignalWaiterMain(context.Background())
err := app.RunContext(ctx, os.Args)
if err != nil {
log.Crit("Application failed", "message", err)
}
}
func Main(cliCtx *cli.Context) error {
l1URL := cliCtx.String(L1URLFlag.Name)
l2URL := cliCtx.String(L2URLFlag.Name)
portalAddress := common.HexToAddress(cliCtx.String(PortalAddressFlag.Name))
privateKey, err := crypto.ToECDSA(common.FromHex(cliCtx.String(PrivateKeyFlag.Name)))
if err != nil {
return err
}
withdrawalTxHash := common.HexToHash(cliCtx.Args().First())
if (withdrawalTxHash == common.Hash{}) {
return fmt.Errorf("invalid withdrawal transaction hash")
}
ctx := context.Background()
l1, err := ethclient.DialContext(ctx, l1URL)
if err != nil {
return err
}
l2, err := ethclient.DialContext(ctx, l2URL)
if err != nil {
return err
}
l2g := gethclient.New(l2.Client())
chainID, err := l1.ChainID(ctx)
if err != nil {
return err
}
opts, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID)
if err != nil {
return err
}
portal, err := bindings.NewPortal(portalAddress, l1)
if err != nil {
return err
}
receipt, err := withdrawals.WaitForReceipt(ctx, l2, withdrawalTxHash, 1*time.Second)
if err != nil {
return err
}
receipt, err = ProveWithdrawal(ctx, l1, l2, l2g, opts, portal, withdrawalTxHash, receipt.BlockNumber)
if err != nil {
return err
}
fmt.Printf("Withdrawal proved and finalized: %s\n", receipt.TxHash)
return nil
}
func ProveWithdrawal(ctx context.Context, l1, l2 *ethclient.Client, l2g *gethclient.Client, opts *bind.TransactOpts, portal *bindings.Portal, withdrawalTxHash common.Hash, withdrawalTxBlock *big.Int) (*types.Receipt, error) {
pollInterval := 1 * time.Second
outputOracleAddress, err := portal.L2Oracle(&bind.CallOpts{})
if err != nil {
return nil, err
}
outputOracle, err := bindings.NewOutputOracle(outputOracleAddress, l1)
if err != nil {
return nil, err
}
fmt.Printf("Waiting for TEE proof of block %d... ", withdrawalTxBlock)
l2OutputBlock, err := withdrawals.WaitForOutputBlock(ctx, outputOracle, withdrawalTxBlock, pollInterval)
fmt.Println("done")
tx, err := withdrawals.ProveAndFinalizeWithdrawal(ctx, l2g, l2, opts, outputOracle, portal, withdrawalTxHash, l2OutputBlock)
if err != nil {
return nil, err
}
return withdrawals.WaitForReceipt(ctx, l1, tx.Hash(), pollInterval)
}
func DepositHash(cliCtx *cli.Context) error {
l1URL := cliCtx.String(L1URLFlag.Name)
portalAddr := common.HexToAddress(cliCtx.String(PortalAddressFlag.Name))
ctx := context.Background()
l1, err := ethclient.DialContext(ctx, l1URL)
if err != nil {
return err
}
depositTxHash := common.HexToHash(cliCtx.Args().First())
if (depositTxHash == common.Hash{}) {
return fmt.Errorf("invalid deposit transaction hash")
}
receipt, err := l1.TransactionReceipt(ctx, depositTxHash)
if err != nil {
return err
}
deposits, err := derive.UserDeposits([]*types.Receipt{receipt}, portalAddr)
if err != nil {
return err
}
if len(deposits) != 1 {
return fmt.Errorf("expected 1 deposit, got %d", len(deposits))
}
hash := types.NewTx(deposits[0]).Hash()
fmt.Println(hash.Hex())
return nil
}