Skip to content

Instantly share code, notes, and snippets.

@benhenryhunter
Created July 8, 2022 22:05
Show Gist options
  • Save benhenryhunter/fea593bd445fa76aff25ff5f442ab0db to your computer and use it in GitHub Desktop.
Save benhenryhunter/fea593bd445fa76aff25ff5f442ab0db to your computer and use it in GitHub Desktop.
Signing transaction for Flashbots MegaBundles
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"fmt"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/rlp"
)
type UnsignedMegabundle struct {
Txs []hexutil.Bytes
BlockNumber uint64
MinTimestamp uint64
MaxTimestamp uint64
RevertingTxHashes []common.Hash
}
func main() {
umb := UnsignedMegabundle{
Txs: []hexutil.Bytes{
common.FromHex("0xf86b0184b1c5ad7283061a809474ae21044283ae165c8816832cd3a3ac3bff85ca87038d7ea4c68000802aa041de6b5c5df12bf5eee96da3940cb7e68b9e7d6410d5e777073110c2368ee332a02bdfcc746e7ee020cf7a2db0085541256d767a5a790bc4859f6fad68164d9804"),
}, // TXs you're submitting
BlockNumber: 12553628, // decimal block number, not hex
MinTimestamp: 0,
MaxTimestamp: 0,
RevertingTxHashes: []common.Hash{},
}
b, _ := rlp.EncodeToBytes(umb)
msg := accounts.TextHash(b)
// pub := common.Hex2Bytes("040b4b509bdb077eb274e543c44aa1c27cb3792ae8efcdd7c12c8ca851bdc9edd42bcb125705f2b2e40ae842834def93381bf3cddeceb46e0cacd241b9c7b6753b")
// secKey := common.Hex2Bytes("66dda74a91924968cec8fcda88a125162b9181b25e7ecf1b1d33900668d77809")
pub, secKey := generateKeyPair()
fmt.Printf("pub: %x\n", string(pub)) // save this to reuse easier
fmt.Printf("priv: %x\n", string(secKey)) // save this to reuse easier
sig, _ := secp256k1.Sign(msg, secKey)
sig[64] += 27
fmt.Printf("sig: %x\n", string(sig)) // use this for the relaySignature
}
func generateKeyPair() (pubkey, privkey []byte) {
key, err := ecdsa.GenerateKey(secp256k1.S256(), rand.Reader)
if err != nil {
panic(err)
}
pubkey = elliptic.Marshal(secp256k1.S256(), key.X, key.Y)
privkey = make([]byte, 32)
blob := key.D.Bytes()
copy(privkey[32-len(blob):], blob)
return pubkey, privkey
}
@benhenryhunter
Copy link
Author

This allows you to create a signature for the eth_sendMegaBundle RPC endpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment