Building a React Native Mobile App with Abstraxion (Xion.js)
Mobile apps are an integral part of modern life, and therefore integrating XION into mobile applications is a crucial step toward mass adoption. This guide will walk you through creating a React Native app using Expo and the Abstraxion authentication system, based on the Abstraxion Expo Demo.
Prerequisites
Before you begin, ensure you have the following installed:
You’ll be prompted to name your app. Once the setup is complete, navigate into the project directory:
cdabstraxion-app
Install Required Dependencies
Install the necessary dependencies for Abstraxion and other required libraries:
These libraries enable authentication, cryptographic operations, and secure storage.
Update Project Configuration
Modify app.json
Update the app.json file to configure your app’s package name (for Android apps) or bundle identifier (for IOS apps).
For Android:
Add the following under the "android" section changing the value to your unique identifier:
For iOS:
Add the following under the "ios" section:
Note: Ensure that the package and bundleIdentifier values are unique to your app. These must be distinct and not shared with other apps to successfully submit your application to the Google Play Store or Apple App Store.
Update the Main Tab (index.tsx)
Replace the contents of the app/(tabs)/index.tsx file with the following code to integrate Abstraxion authentication and transaction signing:
Here are the key changes in the updated file:
Integration with Abstraxion package
The new file now imports useAbstraxionAccount and useAbstraxionSigningClient from @burnt-labs/abstraxion-react-native, enabling authentication and transaction signing functionalities.
State Management with useState
Introduces state variables (signArbResponse, txHash, loadingInstantiate) to manage transaction status and responses.
New Authentication Logic
Includes authentication management (login, logout, isConnected, isConnecting) using useAbstraxionAccount().
Transaction Signing & Contract Instantiation
Adds a function (handleInstantiate) to instantiate a smart contract using MsgInstantiateContract.
Remove explore tab
We'll not be using the Explore tab and so let's go ahead and remove it from the project. Delete the following file "app/(tabs)/explore.tsx".
Update the Layout (_layout.tsx)
Replace the contents of app/_layout.tsx with the following code to set up the Abstraxion provider:
Here’s a breakdown of the key modifications:
1. New Imports
import "react-native-get-random-values";
This import ensures that cryptographic random values can be properly generated.
import { AbstraxionProvider } from "@burnt-labs/abstraxion-react-native";
This adds support for the Abstraxion library.
import { Buffer } from "buffer"; and import crypto from "react-native-quick-crypto";
Commonly used for handling binary data.
2. Global Object Modifications
global.crypto = crypto;
Assigns react-native-quick-crypto as the global crypto library, ensuring cryptographic functions work correctly.
global.Buffer = Buffer;
Makes the Buffer object globally available.
3. New treasuryConfig Object
Contains configuration for interacting with the XION blockchain, including:
Treasury address: "xion1r0tt64mdld2svywzeaf4pa7ezsg6agkyajk48ea398njywdl28rs3jhvry" that allows for gasless transaction. You would update this contract with your own treasity address when building your own custom app.
RPC & REST URLs: Connecting to XION network
Callback URL: "abstraxion-expo-demo://" for handling deep linking.
4. Introduction of AbstraxionProvider
Wraps the application inside <AbstraxionProvider config={treasuryConfig}>, providing the app with Abstraxion functionalities.
Add Metro Configuration
Create a metro.config.js file in the root of your project to configure Metro:
Run the Project
To build and run the app on your emulator or device, use the following commands for your respective environment:
For Android:
For iOS:
Video Demo
For a visual walkthrough of the app, check out this video demo