How to create crypto wallet mobile dApp with Flutter

How to create crypto wallet mobile dApp with Flutter

Written by Danubius IT Solutions

This post will show you how to create a bare minimum Mobile Crypto Wallet application with Flutter. This is not a step-by-step tutorial, it just highlights the critical solutions and keywords.

What is a wallet

Crypto wallets store your private key(s) for your address(es). Your address is the key to the door of the blockchain. An address represents an account on the network. With a wallet, you can easily access your account, and send, receive and spend tokens like Bitcoin or Ether.

A bare minimum crypto wallet

Crypto wallet with Flutter

A bare minimum crypto wallet application can create and store a new wallet and address, send and receive tokens. To support the process of address exchange between the participants, we implement a QR generator screen and a QR scanner screen. The QR code contains the recipient’s address. For easy network change, we add a dropdown list with network names.

Project setup

We have used the following dependencies:

  • web3dart: Dart implementation for web3. It is a bit tricky because it has 2 different repositories and one is deprecated. This is the current latest: xclud/web3dart.

  • bip39: Bip39 specification implementation for Dart. More about bip39 later…

  • shared_preferences: To store the generated wallet. This is not secure, don’t use it in production!

  • mobile_scanner: For QR code reading.

  • qr_flutter: For QR code generation.

Our sample application using the following versions:

  cupertino_icons: ^1.0.2

  web3dart: ^2.4.1

  bip39: ^1.0.6

  shared_preferences: ^2.0.15

  mobile_scanner: ^2.0.0

  qr_flutter: ^4.0.0

Using Ganache

Ganache is a one-click blockchain for development purposes. This cool tool is a member of the Truffle suite. You can download it from the Truffle Suite page. Ganache is fully simulating a blockchain network. If you installed and started, you got a default workspace with 10 pre-built addresses and passphrases for testing. You can create your own workspace and you can customize the account pre-seed, gas fees, block time, etc. To access to Ganache server from a device or emulator, you need to configure the server to listen on all interfaces. Click on the cog icon on the top right side and select the server tab. Change the hostname to 0.0.0.0 - All Interfaces.

Ganache

Recovery Phrase and BIP-39

Wallets are using asymmetric encryption. Hence they have a private and a public key. As the name is mentioned, the private key is confidential. You should never share it. To move your wallet address to another wallet app, you need to provide the keys. Usually, the private key is generated from a security phrase (or seed phrase). This seed is a list of 12 or 24 words, generated randomly. The recovery share should keep in a secure way by the end user. If she/he lost it, he lost the wallet as well.

BIP-39 comes from Bitcoin Improvement Proposal: 39. This is a standard and contains 2048 easy-to-remember words.

To generate a BIP-39 compliant word list, use the bip39.generateMnemonic() function.

Seed phrase

Create private key and generate wallet

To create a private key with web3dart we can use the EthPrivateKey class. Before creating a key from hex we need to convert the recovery phrase to hexadecimal representation. There is a helper method in bip39 lib, the mnemoicToSeedHex(mnemonic)function. The second important thing is protecting the wallet with a password. And the last one is a random number for the cryptography algorithm to generate a secure wallet. This is a sample implementation:

    var mnemonic = bip39.generateMnemonic();

    var passwd = "supers3cr3tp4word";

    var fromHex = mnemonicToSeedHex(mnemonic);

    EthPrivateKey privateKey = EthPrivateKey.fromHex(fromHex);

    Wallet wallet =  Wallet.createNew(privateKey, passwd, Random.secure());

Load Wallet and connect to network using Infura

To load the wallet from a JSON file, you can use Wallet.fromJson(jsonDecode(walletJson), walletPassword) function. Infura is an Infrastructure as a Service solution and toolset for easy development. We create a Web3 client with an HTTP provider and connect to the Rinkbey testnet. You can register and create your free API key on infura.io

Rinkeby test network is deprecated from 5th October. You can use Goerli testnet.

  var apiUrl = "https://rinkeby.infura.io/v3/<YOUR_API_KEY>";

  var httpClient = Client();

  var ethClient = Web3Client(apiUrl, httpClient);

After creating the client you can get the credentials and get the balance of the address:

var credentials = EthPrivateKey.fromInt(wallet!.privateKey.privateKeyInt);

ethClient

    .getBalance(credentials.address)

    .then((balance) => {

          setState(() {

            _address = credentials.address.toString();

            _balance = balance.getInWei.toDouble() * 0.000000000000000001;

          })

        })

    .onError((error, stackTrace) => {

          ScaffoldMessenger.of(context).showSnackBar(

            const SnackBar(

              backgroundColor: Colors.red,

              content: Text(

                'Unable to connect to network!',

                style: TextStyle(color: Colors.white),

              ),

            ),

          ),

        });

Wallet home

Send tokens to address

From this point sending tokens is very straightforward. Just create a transaction, and define the max gas fee and the amount to send. You can use predefined units with EtherUnit class. In our example, we are using Wei.

Transaction transaction = Transaction(

  to: EthereumAddress.fromHex(to, enforceEip55: true),

  maxGas: 100000,

  value: EtherAmount.fromUnitAndValue(EtherUnit.wei, amount),

);

Then you can send the transaction to the chain with the Web3 client. If the transaction has been sent, you will receive the transaction hash. You can check your transaction and mining state on the Ganache dashboard.

_ethClient?.sendTransaction(_credentials!, transaction)

  .then((value) {

    ScaffoldMessenger.of(context).showSnackBar(

      const SnackBar(

        content: Text('Transaction Sent'),

        backgroundColor: Colors.green,

      ),

    );

    setState(() {

      transactionSignature = value;

    });

  }).onError((error, stackTrace) {

    print(error);

    print(stackTrace);

});

That’s all. The UI is up to you.

Interested in IT solutions tailored to your business? Contact us for a free consultation, where we'll collaboratively explore your needs and our methodologies.

Read our tech blog

AI for C-level managers: hype or not?

AI for C-level managers: hype or not?

C-level managers play a crucial role in identifying and allocating resources to the most valuable opportunities, regardless of whether they involve AI. It’s not about the hype, it’s about the revenue. Some things never change – the fundamental principles of finance and economics remain the same: companies must generate returns above their cost of capital. Where is the AI in it?

5 minutes
Prompt Engineering – Is it Fake or a Vital Skill for the AI-Powered Future?

Prompt Engineering – Is it Fake or a Vital Skill for the AI-Powered Future?

In our continued journey to innovate within the AI-driven customer support landscape, we would like to draw attention to a term that's often misunderstood outside tech circles: prompt engineering. Prompt engineering emerges as a cornerstone of AI engineering, vital for refining the capabilities of large language...

5 minutes
What Can We Achieve with Artificial Intelligence in Customer Service?

What Can We Achieve with Artificial Intelligence in Customer Service?

Artificial intelligence (AI) has emerged as a transformative force and our team here, at Danubius embarked on an ambitious journey to explore the real impact of AI's practical applications in enhancing customer support. The adventure began a little over half a year ago with an internal project that aimed to not...

6 minutes

Listen to our podcast below

Scrum Mastery Unveiled: Part 1

Scrum Mastery Unveiled: Part 1

Scrum Masters are more than just facilitators; they are often a key part of a development team, ensuring efficiency, timely delivery, and much more. We discussed their challenges with two of our experts, Zsofia Samodai and...

55 minutes
Hello, Danubius!

Hello, Danubius!

The two founders of Danubius IT Solutions, Peter Balogh and Peter Halasz, sat down to discuss the company's past, present, and future. Our discussions covered the creation and development of an organizational culture, as well as scaling the company from two coworkers to 100.

31 minutes