main.go
TEXT FILE3.5 KB · Public source code for developers, search crawlers and AI agents
1package main
2
3import (
4 "context"
5 "encoding/hex"
6 "fmt"
7 "math/big"
8 "os"
9 "strings"
10 "time"
11
12 "github.com/ethereum/go-ethereum"
13 "github.com/ethereum/go-ethereum/common"
14 "github.com/ethereum/go-ethereum/core/types"
15 "github.com/ethereum/go-ethereum/crypto"
16 "github.com/ethereum/go-ethereum/ethclient"
17)
18
19func check(err error) {
20 if err != nil {
21 panic(err)
22 }
23}
24func fail(message string) { panic(message) }
25
26func main() {
27 ctx, cancel := context.WithTimeout(context.Background(), 35*time.Second)
28 defer cancel()
29 args := os.Args[1:]
30 if len(args) < 2 {
31 fail("usage: KEY deploy BIN HASH RECIPIENT EXPIRY | KEY claim CONTRACT SECRET | KEY balance TOKEN OWNER")
32 }
33 rpc, err := ethclient.DialContext(ctx, "https://ethereum-sepolia-rpc.publicnode.com")
34 check(err)
35 defer rpc.Close()
36 chain, err := rpc.ChainID(ctx)
37 check(err)
38 if chain.Int64() != 11155111 {
39 fail("expected Sepolia")
40 }
41 raw, err := os.ReadFile(args[0])
42 check(err)
43 key, err := crypto.ToECDSA(raw)
44 clear(raw)
45 check(err)
46 owner := crypto.PubkeyToAddress(key.PublicKey)
47 msg := ethereum.CallMsg{From: owner}
48 switch args[1] {
49 case "deploy":
50 if len(args) != 6 {
51 fail("expected bytecode path, hash, recipient and expiry")
52 }
53 code, err := os.ReadFile(args[2])
54 check(err)
55 msg.Data, err = hex.DecodeString(strings.TrimSpace(string(code)))
56 check(err)
57 hash, err := hex.DecodeString(args[3])
58 check(err)
59 expiry, ok := new(big.Int).SetString(args[5], 10)
60 if !ok || len(hash) != 32 || !common.IsHexAddress(args[4]) || expiry.Sign() <= 0 {
61 fail("invalid constructor parameters")
62 }
63 msg.Data = append(msg.Data, hash...)
64 msg.Data = append(msg.Data, common.LeftPadBytes(common.HexToAddress(args[4]).Bytes(), 32)...)
65 msg.Data = append(msg.Data, common.LeftPadBytes(expiry.Bytes(), 32)...)
66 case "refund":
67 if len(args) != 3 || !common.IsHexAddress(args[2]) {
68 fail("expected refund contract")
69 }
70 address := common.HexToAddress(args[2])
71 msg.To = &address
72 msg.Data = crypto.Keccak256([]byte("refund()"))[:4]
73 case "claim", "balance":
74 if len(args) != 4 || !common.IsHexAddress(args[2]) {
75 fail("expected contract and argument")
76 }
77 address := common.HexToAddress(args[2])
78 msg.To = &address
79 if args[1] == "balance" {
80 if !common.IsHexAddress(args[3]) {
81 fail("expected owner address")
82 }
83 msg.Data = append(crypto.Keccak256([]byte("balanceOf(address)"))[:4], common.LeftPadBytes(common.HexToAddress(args[3]).Bytes(), 32)...)
84 result, err := rpc.CallContract(ctx, msg, nil)
85 check(err)
86 if len(result) != 32 {
87 fail("invalid balance output")
88 }
89 fmt.Println(new(big.Int).SetBytes(result))
90 return
91 }
92 secret, err := hex.DecodeString(args[3])
93 check(err)
94 if len(secret) != 32 {
95 fail("expected 32-byte secret")
96 }
97 msg.Data = append(crypto.Keccak256([]byte("claim(bytes32)"))[:4], secret...)
98 default:
99 fail("unknown command")
100 }
101 gas, err := rpc.EstimateGas(ctx, msg)
102 check(err)
103 price, err := rpc.SuggestGasPrice(ctx)
104 check(err)
105 if gas > 5_000_000 || new(big.Int).Mul(price, new(big.Int).SetUint64(gas+gas/10)).Cmp(big.NewInt(15_000_000_000_000_000)) > 0 {
106 fail(fmt.Sprintf("gas exceeds lab budget: gas=%d price=%s", gas, price))
107 }
108 nonce, err := rpc.PendingNonceAt(ctx, owner)
109 check(err)
110 tx := types.NewTx(&types.LegacyTx{Nonce: nonce, GasPrice: price, Gas: gas + gas/10, To: msg.To, Data: msg.Data})
111 signed, err := types.SignTx(tx, types.LatestSignerForChainID(chain), key)
112 check(err)
113 fmt.Fprintln(os.Stderr, signed.Hash().Hex())
114 check(rpc.SendTransaction(ctx, signed))
115 fmt.Print(signed.Hash().Hex())
116}Read-only source · Select a line number to link directly · Raw download above
