Integrate Embedded Wallets with the Ancient8 Blockchain in Flutter
While using the Embedded Wallets Mobile SDKs (formerly Web3Auth) in Flutter, the private key is available in the SDK state after authorization. Use it to derive the user's address and interact with the Ancient8 blockchain for signing and JSON-RPC calls.
The SDKs are now branded as MetaMask Embedded Wallet SDKs (formerly Web3Auth Plug and Play SDKs). Package names and APIs remain Web3Auth (for example, Web3Auth React SDK), and code snippets may reference web3auth identifiers.
Prerequisites
This doc assumes you have already setup your project in the Embedded Wallets Dashboard (formerly Web3Auth), and have integrated Embedded Wallets in your Flutter app. If you haven't done that yet, you can learn how to integrate Embedded Wallets in your Flutter app.
Installation
To interact with the Ethereum compatible blockchains in Flutter, you can use any EIP1193 compatible package. Here, we're using web3dart to demonstrate how to make blockchain calls using it with Embedded Wallets (formerly Web3Auth).
To install the web3dart package, you have two options. You can either manually add the package in the pubspec.yaml file, or you can use the flutter pub add command.
- Console
- Pubspec
Add web3dart using flutter pub add command.
flutter pub add web3dart
Add web3dart as a dependency to your pubspec.yaml.
dependencies:
web3dart: ^2.7.3
Initialize
We'll initialize the Web3Client with the RPC URL of the network you want to connect to. This
client allows us to interact with the blockchain. To get the public RPC URL, you can checkout
chainlist.org.
import 'package:web3dart/web3dart.dart';
// Please avoid using public RPC URL in production, use services
// like Infura, Quicknode, etc.
final client = Web3Client("YOUR_RPC_URL", Client());
Get Account
Once user has successfully logged in, you can retrieve the user's private key using the getPrivKey method from the Embedded Wallets Flutter SDK (formerly Web3Auth). We'll use this private key to generate the Credentials for the user.
This Credentials object has the user's key pair of private key and public key, and can be used to sign the transactions. Please note, that this assumes that the user has already logged in and the private key is available.
import 'package:web3dart/web3dart.dart';
final privateKey = await Web3AuthFlutter.getPrivKey();
final credentials = EthPrivateKey.fromHex(privateKey);
final address = credentials.address;
Get Balance
To retrieve the native balance of the user, you can use the Web3Client object we created earlier.
It has getBalance method which helps to fetch the user's native token balance.
The result we get from Web3Client is in wei, the smallest value. To convert the value to ether, you can divide it with 10^18, where 18 denotes the decimals for wei.
For the simplicity, we'll use a helper function from web3dart package which has the same implementation
// Use the client and address variable from above
final balanceResponse = await client.getBalance(address);
// Convert the balance to ether format, and round off to 4 decimal places.
final balance = balanceResponse.getValueInUnit(EtherUnit.ether).toStringAsFixed(4);