Skip to content

Instantly share code, notes, and snippets.

@mdehoog
Last active June 13, 2022 18:05
Show Gist options
  • Save mdehoog/a95becd596ea07880abaa6c285da79cc to your computer and use it in GitHub Desktop.
Save mdehoog/a95becd596ea07880abaa6c285da79cc to your computer and use it in GitHub Desktop.
Send blob transaction
package main
import (
"context"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/holiman/uint256"
"github.com/protolambda/ztyp/view"
"log"
"math/big"
"os"
"path/filepath"
)
func main() {
chainId := big.NewInt(1)
signer := types.NewDankSigner(chainId)
home, err := os.UserHomeDir()
if err != nil {
log.Fatalf("Failed to retrieve home dir: %v", err)
}
ctx := context.Background()
client, err := ethclient.DialContext(ctx, filepath.Join(home, "ethereum/taunus/geth.ipc"))
if err != nil {
log.Fatalf("Failed to connect to the Ethereum client: %v", err)
}
matches, err := filepath.Glob(filepath.Join(home, "ethereum/taunus/keystore/*a94f5374fce5edbc8e2a8697c15331677e6ebf0b"))
if err != nil {
log.Fatalf("Error finding keystore file: %v", err)
}
if len(matches) == 0 {
panic("Keystore file not found")
}
dat, err := os.ReadFile(matches[0])
if err != nil {
log.Fatalf("Error loading file: %v", err)
}
key, err := keystore.DecryptKey(dat, "")
if err != nil {
log.Fatalf("Error decrypting key: %v", err)
}
nonce, err := client.PendingNonceAt(ctx, key.Address)
if err != nil {
log.Fatalf("Error getting nonce: %v", err)
}
log.Printf("Nonce: %d", nonce)
to := common.HexToAddress("ffb38a7a99e3e2335be83fc74b7faa19d5531243")
//txData := types.LegacyTx{
// Nonce: nonce,
// Gas: 210000,
// GasPrice: big.NewInt(5000000000),
// To: &to,
// Value: big.NewInt(12345678),
//}
// just fake blob data
blobs := []types.Blob{{[32]byte{0x12, 0x34}, [32]byte{0x56}, [32]byte{0x78}}}
var commitments []types.KZGCommitment
var hashes []common.Hash
for _, b := range blobs {
c, ok := b.ComputeCommitment()
if !ok {
panic("Could not compute commitment")
}
commitments = append(commitments, c)
hashes = append(hashes, c.ComputeVersionedHash())
}
txData := types.SignedBlobTx{
Message: types.BlobTxMessage{
ChainID: view.Uint256View(*uint256.NewInt(chainId.Uint64())),
Nonce: view.Uint64View(nonce),
Gas: 210000,
GasFeeCap: view.Uint256View(*uint256.NewInt(5000000000)),
GasTipCap: view.Uint256View(*uint256.NewInt(5000000000)),
Value: view.Uint256View(*uint256.NewInt(12345678)),
To: types.AddressOptionalSSZ{Address: (*types.AddressSSZ)(&to)},
BlobVersionedHashes: hashes,
},
}
wrapData := types.BlobTxWrapData{
BlobKzgs: commitments,
Blobs: blobs,
}
tx := types.NewTx(&txData, types.WithTxWrapData(&wrapData))
tx, err = types.SignTx(tx, signer, key.PrivateKey)
if err != nil {
log.Fatalf("Error signing tx: %v", err)
}
err = client.SendTransaction(ctx, tx)
if err != nil {
log.Fatalf("Error sending tx: %v", err)
}
log.Printf("Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment