Blockchain: Ethereum Offline Transaction Signatures Explained

·

In our previous discussions, we covered Ethereum wallet creation and unlocking. This chapter focuses on transaction processing—specifically offline signing for ETH and ERC-20 token transfers.

Transaction Flow Overview

  1. Offline Signing: Wallet locally signs transaction data
  2. Broadcasting: Signed transaction sent to Ethereum node via JSON-RPC
  3. Key Difference: ETH and ERC-20 tokens require distinct signing approaches

ETH Transfer Signing

Code Implementation

public String signedEthTransactionData(String to, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, BigDecimal amount, HLWallet wallet, String password) throws Exception {
    BigDecimal amountInWei = Convert.toWei(amount.toString(), Convert.Unit.ETHER);
    RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to, amountInWei.toBigInteger());
    return signData(rawTransaction,wallet,password);
}

private String signData(RawTransaction rawTransaction, HLWallet wallet, String password) throws Exception {
    Credentials credentials = Credentials.create(LWallet.decrypt(password, wallet.walletFile));
    byte[] signMessage = TransactionEncoder.signMessage(rawTransaction, ChainId.MAINNET, credentials);
    return Numeric.toHexString(signMessage);
}

Understanding Nonce

Critical for preventing replay attacks:

👉 Master Ethereum transaction security with these best practices.

ERC-20 Token Transfer Signing

Requires smart contract interaction with manually constructed data:

public String signedContractTransactionData(String contractAddress, String to, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, BigDecimal amount, BigDecimal decimal, HLWallet wallet, String password) throws Exception {
    BigDecimal realValue = amount.multiply(decimal);
    String data = Params.Abi.transfer + Numeric.toHexStringNoPrefixZeroPadded(Numeric.toBigInt(to), 64) + Numeric.toHexStringNoPrefixZeroPadded(realValue.toBigInteger(), 64);
    RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, contractAddress, data);
    return signData(rawTransaction, wallet, password);
}

Data Structure Components

  1. MethodID: 0xa9059cbb (SHA-3 of transfer(address,uint256))
  2. Recipient Address: 64-character padded hex
  3. Amount: Value in wei (64-character padded hex)

For simplified implementation using web3j:

public String signContractTransaction(String contractAddress, String to, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, BigDecimal amount, BigDecimal decimal, HLWallet wallet, String password) throws IOException, CipherException {
    BigDecimal realValue = amount.multiply(decimal);
    Function function = new Function("transfer", Arrays.asList(new Address(to), new Uint256(realValue.toBigInteger())), Collections.emptyList());
    String data = FunctionEncoder.encode(function);
    RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, contractAddress, data);
    return signData(rawTransaction, wallet, password);
}

Broadcasting Transactions

Use Ethereum's JSON-RPC method eth_sendrawtransaction to broadcast signed transactions.

👉 View real-world ETH transfer example showing:

FAQ Section

Q: Why can't I find my transaction after calling sendRawTransaction?
A: The returned txHash only confirms submission. Blockchain confirmation delays are normal during network congestion. Implement status tracking in your app's UI.

Q: What causes 'invalid sender' errors?
A: This indicates chainId mismatch. Always specify the correct network:

private String signData(byte chainId, RawTransaction rawTransaction, HDWallet wallet, String password) throws Exception {
    Credentials credentials = Credentials.create(Wallet.decrypt(password, wallet.getWalletFile()));
    byte[] signMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
    return Numeric.toHexString(signMessage);
}

Q: How do I handle decimal precision in ERC-20 transfers?
A: Always multiply by token's decimal places before conversion to wei-equivalent value.

Q: Can I speed up stuck transactions?
A: Yes, by resending with higher gasPrice and the same nonce.

Core Keywords


This version:
1. Removes all promotional/Chinese content
2. Organizes with clear Markdown headings
3. Integrates SEO keywords naturally
4. Includes required anchor links