Blockchain infrastructure · 5 MIN READ

Raw transactions offline: sign, encode, decode and recover the sender before anything is broadcast

Sign a typed transaction with a test key, round-trip its raw bytes, recover the sender and use the precomputed hash to make retries safe.

eth_sendRawTransaction takes one opaque byte string. Everything that matters about a transfer, including its chain, nonce, fees, destination, the signer and its future on-chain hash, is fixed when those bytes are produced, before any node sees them. Producing, checking and storing the bytes offline turns a network call with an unknown outcome into a lookup you can repeat.

blockchain · A MAP OF THE IDEA

  1. Build typed intent
  2. Sign with chain signer
  3. Persist raw bytes + hash
  4. Send, then look up hash

Useful when

  • The hash is known before broadcast, so retries can be idempotent.
  • Decoding and recovering the sender verifies bytes from other systems offline.

Watch out

  • Remote signers with random nonces produce new hashes for the same fields.
  • A fresh nonce on retry is a second payment, not a retry.

Implementation & operation cost: A durable record per signed transaction (intent, nonce, raw bytes, hashes) and a receipt lookup before every resend.

01

One transaction, two hashes and a byte string

go-ethereum distinguishes three artifacts. The signing hash, returned by Signer.Hash, is keccak256 over the type byte and the RLP list of fields without a signature; that is what the private key signs. The raw transaction, returned by MarshalBinary, is the EIP-2718 envelope: one type byte (0x02 for EIP-1559) followed by the RLP list that now includes V, R and S. The transaction hash, Transaction.Hash, is keccak256 of exactly those raw bytes. Because the hash is a pure function of the signed bytes, it is known the moment signing finishes, before broadcast, and it does not depend on which node accepts it. The signing-boundaries lesson built an unsigned intent; here it goes through the whole offline lifecycle.

02

Sign with a fixed test key and round-trip the bytes

The example derives a throwaway key from 32 bytes of 0x01, a well-known test key that must never hold funds, and signs an EIP-1559 transfer for local chain 1337. It then plays the receiving side: decode the raw bytes into a fresh Transaction, recover the sender and compare hashes. Two final checks show what stays stable across signing and what changes.

go
package main

import (
	"bytes"
	"fmt"
	"math/big"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/crypto"
)

func main() {
	// TEST KEY ONLY: 32 bytes of 0x01. Never fund it or reuse it outside a local chain.
	key, err := crypto.ToECDSA(bytes.Repeat([]byte{1}, 32))
	if err != nil {
		panic(err)
	}
	from := crypto.PubkeyToAddress(key.PublicKey)
	signer := types.LatestSignerForChainID(big.NewInt(1337))

	to := common.HexToAddress("0x00000000000000000000000000000000000000aa")
	intent := &types.DynamicFeeTx{
		ChainID:   big.NewInt(1337),
		Nonce:     7,
		GasTipCap: big.NewInt(2_000_000_000),
		GasFeeCap: big.NewInt(62_000_000_000),
		Gas:       21_000,
		To:        &to,
		Value:     big.NewInt(1_000_000_000_000_000),
	}
	signed, err := types.SignTx(types.NewTx(intent), signer, key)
	if err != nil {
		panic(err)
	}
	raw, err := signed.MarshalBinary() // the bytes eth_sendRawTransaction carries
	if err != nil {
		panic(err)
	}
	fmt.Printf("sender %s\n", from.Hex())
	fmt.Printf("raw: %d bytes, first byte 0x%02x (type %d)\n", len(raw), raw[0], signed.Type())
	fmt.Println("tx hash  ", signed.Hash().Hex())
	fmt.Println("sign hash", signer.Hash(signed).Hex())
	fmt.Println("hash == keccak256(raw):", signed.Hash() == crypto.Keccak256Hash(raw))

	// The receiving side: decode untrusted bytes, then recover who signed them.
	var decoded types.Transaction
	if err := decoded.UnmarshalBinary(raw); err != nil {
		panic(err)
	}
	got, err := types.Sender(signer, &decoded)
	fmt.Println("recovered sender matches:", got == from, err)
	fmt.Println("decoded hash matches:", decoded.Hash() == signed.Hash())

	_, err = types.Sender(types.LatestSignerForChainID(big.NewInt(1)), &decoded)
	fmt.Println("verify as chain 1:", err)

	again, _ := types.SignTx(types.NewTx(intent), signer, key)
	fmt.Println("re-signed same fields, same hash:", again.Hash() == signed.Hash())

	bump := *intent // shallow copy is enough: we replace the big.Int pointers
	bump.GasTipCap = big.NewInt(2_200_000_000)
	bump.GasFeeCap = big.NewInt(68_200_000_000)
	replacement, _ := types.SignTx(types.NewTx(&bump), signer, key)
	fmt.Println("replacement: same nonce", replacement.Nonce() == signed.Nonce(),
		"new hash", replacement.Hash() != signed.Hash())
}
Read the code

LatestSignerForChainID(1337) returns the newest signer in this release, the Prague signer, which accepts every typed transaction and binds signatures to chain 1337. SignTx computes signer.Hash, signs it with crypto.Sign and attaches V, R and S. MarshalBinary yields 119 bytes starting with 0x02, and keccak256 of those bytes equals Hash(). UnmarshalBinary rebuilds the transaction from untrusted bytes, and types.Sender recovers the address; the same bytes checked with a chain-1 signer fail with ErrInvalidChainId. Re-signing identical fields reproduces the hash because go-ethereum signs with deterministic RFC 6979 nonces. Raising both fee caps by 10 percent for the same nonce produces a new hash.

03

Persist the bytes and the hash before you send

The precomputed hash is what makes an unknown send outcome manageable. Store the intent, nonce, raw bytes and hash durably before the first network call, then treat sending as an attempt to publish those exact bytes. After a timeout, ask for a receipt by hash before deciding anything. If none exists yet, resending the stored bytes cannot create a second transfer, because the nonce allows at most one transaction from that account at that position to be included.

go
// Native environment only: a real node and a durable store.
if err := store.SaveSigned(ctx, intentID, signed.Nonce(), signed.Hash(), raw); err != nil {
	return err // nothing was sent, so repeating this step is safe
}
sendErr := client.SendTransaction(ctx, signed) // MarshalBinary + eth_sendRawTransaction
receipt, err := client.TransactionReceipt(ctx, signed.Hash())
switch {
case err == nil:
	return store.MarkIncluded(ctx, intentID, receipt.Status, receipt.BlockHash)
case errors.Is(err, ethereum.NotFound):
	// Pending, dropped or never received: resend the same raw bytes later.
	return store.MarkSubmitted(ctx, intentID, sendErr)
default:
	return err // provider trouble: retry the lookup, never the signing
}
Read the code

This fragment needs a node and a database, so it runs in your own native environment; store is a placeholder for your persistence layer. SendTransaction is MarshalBinary plus eth_sendRawTransaction, so the bytes on the wire are the stored ones. The receipt lookup uses the precomputed hash whether or not the send returned an error, and ethclient reports ethereum.NotFound while no receipt exists. On a resend, geth answers already known for a transaction it holds and nonce too low once that nonce is used; the second means some transaction took the slot, so look up the receipt by hash to learn whether it was yours.

04

Resend, re-sign or replace: three different retries

Three retry strategies look alike and behave very differently. Resending the stored raw bytes is idempotent: same hash, same nonce. Re-signing the same fields is safe only because the result occupies the same nonce. With go-ethereum's deterministic signer it even reproduces the hash, but a remote signer, HSM or KMS that uses random ECDSA nonces returns a different valid signature and therefore a different hash for the same intent, so the record must accept several hashes per nonce. Building a transaction with a fresh nonce is a second payment and belongs to a new business intent, never to a retry; the nonces lesson covers who owns that decision. A fee-bumped replacement deliberately produces a new hash for the same nonce. Whichever version is included wins and the others become permanently invalid, so reconcile by account and nonce as well as by hash.

05

Decode untrusted bytes like a verifier

Services that receive raw transactions from elsewhere, such as a relayer, a co-signer or an approval queue, should decode and check them instead of forwarding bytes blindly. After UnmarshalBinary, compare Type, ChainId, To, Value, Data, Nonce, Gas and both fee caps with the approved intent, recover the sender with a signer for the configured chain and compare it with the expected account. Two details from go-ethereum 1.17 matter in review. Sender rejects a chain mismatch, but SignTx stamps the signer's chain ID onto a typed transaction whose ChainID is zero instead of failing, so the signer, not the intent, picks the chain unless your policy checks ChainID first. And LatestSignerForChainID follows the newest fork of each release, so pin the module version and rerun these round-trip tests on every upgrade.

TAKE THIS WITH YOU

Sign offline, persist the raw bytes and their hash before broadcast, and retry by resending those exact bytes; a fresh nonce is a second payment, not a retry.

Check your understanding

A service signs a transaction and its eth_sendRawTransaction call times out. When could it first have known the hash to look up?

Go to the source

Progress stays in this browser. No account needed.