Web3 Full-Stack Development: Creating Multi-Chain Wallets and Sending Transactions

ยท

Introduction to Multi-Chain Wallet Development

Today, we dive into Web3 and app development using Flutter/Dart. This guide focuses on creating a multi-chain wallet supporting Bitcoin, Ethereum, and Tron networks, along with transaction functionalities.

Key Concepts Covered

Creating a Multi-Chain Wallet

HD Wallet Fundamentals

Hierarchical Deterministic (HD) wallets enable users to manage multiple blockchain assets with a single mnemonic phrase. This approach follows the BIP-44 standard, allowing derivation of wallets for different chains by specifying unique coin types:

Implementation Code

import 'package:bip39/bip39.dart' as bip39;
import 'package:flutter_bitcoin/flutter_bitcoin.dart';

final mnemonic = bip39.generateMnemonic(strength: 128);
final seed = bip39.mnemonicToSeed(mnemonic);
final hdWallet = HDWallet.fromSeed(seed);

// Derive wallet paths
btcWallet = hdWallet.derivePath("m/44'/0'/0'/0/0");
ethWallet = hdWallet.derivePath("m/44'/60'/0'/0/0");
tronWallet = hdWallet.derivePath("m/44'/195'/0'/0/0");

Address Generation Differences

Each blockchain employs distinct address derivation methods:

BlockchainAddress PrefixKey Derivation Method
Bitcoin1 or 3SHA256 + RIPEMD160 โ†’ Base58
Ethereum0xKeccak256 (last 20 bytes)
TronTSHA256 + RIPEMD160 โ†’ Base58Check

Transaction Signing Mechanisms

Ethereum Transactions

Future<String> signTransaction({
  required EthPrivateKey privateKey,
  required Transaction transaction,
}) async {
  final result = await web3Client.signTransaction(
    privateKey,
    transaction,
    chainId: 11155111, // Sepolia Testnet
  );
  return HEX.encode(result);
}

Bitcoin UTXO Model

Bitcoin transactions use unspent outputs (UTXOs) as inputs:

String createBitcoinTx(HDWallet btcWallet) {
  final txb = TransactionBuilder();
  txb.setVersion(1);
  txb.addInput('previous_tx_hash', output_index);
  txb.addOutput(recipient_address, amount);
  txb.sign(vin: 0, keyPair: ECPair.fromWIF(btcWallet.wif!));
  return txb.build().toHex();
}

๐Ÿ‘‰ Learn more about UTXO transactions

Building the Complete Application

Functional Components

  1. Mnemonic phrase input
  2. Address generation for all three chains
  3. Transaction signing interface
  4. Ethereum transaction broadcasting

Execution Command

flutter pub get
flutter run

Frequently Asked Questions

What's the advantage of HD wallets?

HD wallets allow managing unlimited addresses across multiple blockchains with a single recovery phrase while maintaining strict privacy standards.

How do gas fees differ between Ethereum and Bitcoin?

๐Ÿ‘‰ Compare gas fee structures across blockchains

Can I use the same address across different chains?

No. Each blockchain has unique address formats and derivation methods, though HD wallets make them manageable under one interface.

Next Steps

This guide provides the foundation for building secure multi-chain wallets in Flutter. For production applications, always implement additional security layers like biometric authentication and hardware wallet integration.