-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.go
226 lines (199 loc) · 5.93 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
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
package main
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"math/big"
"os"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
gethkzg4844 "github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/holiman/uint256"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Commands = []cli.Command{
{
Name: "tx",
Usage: "send a blob transaction",
Action: TxApp,
Flags: TxFlags,
},
{
Name: "download",
Usage: "download blobs from the beacon net",
Action: DownloadApp,
Flags: DownloadFlags,
},
{
Name: "proof",
Usage: "generate kzg proof for any input point by using jth blob polynomial",
Action: ProofApp,
Flags: ProofFlags,
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatalf("App failed: %v", err)
}
}
func TxApp(cliCtx *cli.Context) error {
addr := cliCtx.String(TxRPCURLFlag.Name)
to := common.HexToAddress(cliCtx.String(TxToFlag.Name))
prv := cliCtx.String(TxPrivateKeyFlag.Name)
file := cliCtx.String(TxBlobFileFlag.Name)
nonce := cliCtx.Int64(TxNonceFlag.Name)
value := cliCtx.String(TxValueFlag.Name)
gasLimit := cliCtx.Uint64(TxGasLimitFlag.Name)
gasPrice := cliCtx.String(TxGasPriceFlag.Name)
priorityGasPrice := cliCtx.String(TxPriorityGasPrice.Name)
maxFeePerBlobGas := cliCtx.String(TxMaxFeePerBlobGas.Name)
chainID := cliCtx.String(TxChainID.Name)
calldata := cliCtx.String(TxCalldata.Name)
value256, err := uint256.FromHex(value)
if err != nil {
return fmt.Errorf("invalid value param: %v", err)
}
data, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("error reading blob file: %v", err)
}
chainId, _ := new(big.Int).SetString(chainID, 0)
ctx := context.Background()
client, err := ethclient.DialContext(ctx, addr)
if err != nil {
log.Fatalf("Failed to connect to the Ethereum client: %v", err)
}
key, err := crypto.HexToECDSA(prv)
if err != nil {
return fmt.Errorf("%w: invalid private key", err)
}
if nonce == -1 {
pendingNonce, err := client.PendingNonceAt(ctx, crypto.PubkeyToAddress(key.PublicKey))
if err != nil {
log.Fatalf("Error getting nonce: %v", err)
}
nonce = int64(pendingNonce)
}
var gasPrice256 *uint256.Int
if gasPrice == "" {
val, err := client.SuggestGasPrice(ctx)
if err != nil {
log.Fatalf("Error getting suggested gas price: %v", err)
}
var nok bool
gasPrice256, nok = uint256.FromBig(val)
if nok {
log.Fatalf("gas price is too high! got %v", val.String())
}
} else {
gasPrice256, err = DecodeUint256String(gasPrice)
if err != nil {
return fmt.Errorf("%w: invalid gas price", err)
}
}
priorityGasPrice256 := gasPrice256
if priorityGasPrice != "" {
priorityGasPrice256, err = DecodeUint256String(priorityGasPrice)
if err != nil {
return fmt.Errorf("%w: invalid priority gas price", err)
}
}
maxFeePerBlobGas256, err := DecodeUint256String(maxFeePerBlobGas)
if err != nil {
return fmt.Errorf("%w: invalid max_fee_per_blob_gas", err)
}
blobs, commitments, proofs, versionedHashes, err := EncodeBlobs(data)
if err != nil {
log.Fatalf("failed to compute commitments: %v", err)
}
calldataBytes, err := common.ParseHexOrString(calldata)
if err != nil {
log.Fatalf("failed to parse calldata: %v", err)
}
tx := types.NewTx(&types.BlobTx{
ChainID: uint256.MustFromBig(chainId),
Nonce: uint64(nonce),
GasTipCap: priorityGasPrice256,
GasFeeCap: gasPrice256,
Gas: gasLimit,
To: to,
Value: value256,
Data: calldataBytes,
BlobFeeCap: maxFeePerBlobGas256,
BlobHashes: versionedHashes,
Sidecar: &types.BlobTxSidecar{Blobs: blobs, Commitments: commitments, Proofs: proofs},
})
signedTx, _ := types.SignTx(tx, types.NewCancunSigner(chainId), key)
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Fatalf("failed to send transaction: %v", err)
} else {
log.Printf("successfully sent transaction. txhash=%v", signedTx.Hash())
}
//var receipt *types.Receipt
for {
_, err = client.TransactionReceipt(context.Background(), tx.Hash())
if err == ethereum.NotFound {
time.Sleep(1 * time.Second)
} else if err != nil {
if _, ok := err.(*json.UnmarshalTypeError); ok {
// TODO: ignore other errors for now. Some clients are treating the blobGasUsed as big.Int rather than uint64
break
}
} else {
break
}
}
log.Printf("Transaction included. nonce=%d hash=%v", nonce, tx.Hash())
//log.Printf("Transaction included. nonce=%d hash=%v, block=%d", nonce, tx.Hash(), receipt.BlockNumber.Int64())
return nil
}
func ProofApp(cliCtx *cli.Context) error {
file := cliCtx.String(ProofBlobFileFlag.Name)
blobIndex := cliCtx.Uint64(ProofBlobIndexFlag.Name)
inputPoint := cliCtx.String(ProofInputPointFlag.Name)
data, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("error reading blob file: %v", err)
}
blobs, commitments, _, versionedHashes, err := EncodeBlobs(data)
if err != nil {
log.Fatalf("failed to compute commitments: %v", err)
}
if blobIndex >= uint64(len(blobs)) {
return fmt.Errorf("error reading %d blob", blobIndex)
}
if len(inputPoint) != 64 {
return fmt.Errorf("wrong input point, len is %d", len(inputPoint))
}
var x gethkzg4844.Point
ip, _ := hex.DecodeString(inputPoint)
copy(x[:], ip)
proof, claimedValue, err := gethkzg4844.ComputeProof(gethkzg4844.Blob(blobs[blobIndex]), x)
if err != nil {
log.Fatalf("failed to compute proofs: %v", err)
}
pointEvalInput := bytes.Join(
[][]byte{
versionedHashes[blobIndex][:],
x[:],
claimedValue[:],
commitments[blobIndex][:],
proof[:],
},
[]byte{},
)
log.Printf(
"\nversionedHash %x \n"+"x %x \n"+"y %x \n"+"commitment %x \n"+"proof %x \n"+"pointEvalInput %x",
versionedHashes[blobIndex][:], x[:], claimedValue[:], commitments[blobIndex][:], proof[:], pointEvalInput[:])
return nil
}