How to integrate Firebase with Web3Auth iOS SFA SDK
In this guide, we'll talk about how we can use Web3Auth Single Factor Auth with Firebase in your iOS application.
As an overview, the guide is quite simple, with functionality to log in, display user details, and perform blockchain interactions. The signing of the blockchain transactions is done through the Web3Auth embedded wallet. You can check out the infrastructure docs, "Web3Auth Wallet Management Infrastructure" for a high-level overview of the Web3Auth architecture and implementation. For those who want to skip straight to the code, you can find it on GitHub.
How to set up Web3Auth Dashboard
If you haven't already, sign up on the Web3Auth platform. It is free and gives you access to the Web3Auth's base plan. After the basic setup, explore other features and functionalities offered by the Web3Auth Dashboard. It includes custom verifiers, whitelabeling, analytics, and more. Head to Web3Auth's documentation page for detailed instructions on setting up the Web3Auth Dashboard.
How to set up Firebase for iOS
If you haven't already setup the Firebase for your iOS app, please setup the Firebase, as it's the prerequisites for the guide. Head to the Firebase's documentation for the details instructions.
How to set up Custom verifier
Once, you have set up the Web3Auth Dashboard, created a new project, and set up Firebase, it's time to create a Custom Verifier for your Firebase application. We already have detail instructions on how to create a Custom Verifier for Firebase, please head to our documentation.
Integrating Web3Auth SFA in iOS
Once, you have set up the Custom Verifier, it's time to integrate Web3Auth in your iOS application. For the implementation, we'll use the "single-factor-auth-swift". This SDK facilitates integration with Web3Auth. This way you can easily manage embedded wallet in your iOS application.
Installation
To install the single-factor-auth-swift package, you have two options. You can either use the Swift Package Manager, or you can install using Cocoapods.
- Swift Package Manager
- Cocoapods
-
In Xcode, with your app project open, navigate to File > Add Packages.
-
When prompted, add the single-factor-auth-swift iOS SDK repository:
https://github.com/Web3Auth/single-factor-auth-swift
From the
Dependency Rule
dropdown, selectExact Version
and enter9.0.2
as the version. -
When finished, Xcode will automatically begin resolving and downloading your dependencies in the background.
To install the Web3Auth SingleFactorAuth SDK using Cocoapods, follow the below steps:
- Open the Podfile, and add the SingleFactorAuth pod:
pod 'SingleFactorAuth', '~> 9.0.2'
- Once added, use
pod install
command to download the SingleFactorAuth dependency.
Initialization
After successfully installing the package, the next step is to initialize SingleFactorAuth
in your
iOS app. This sets up the necessary configurations using Web3Auth network and prepares the SDK.
Learn more about SingleFactorAuth Initialization.
The initialize
method is responsible for authorizing the existing sessions, and initializing the
SDK. If the existing session is not valid, it'll throw an error.
import SingleFactorAuth
let options = Web3AuthOptions(
clientId: "YOUR_WEB3AUTH_CLIENT_ID",
web3AuthNetwork: .SAPPHIRE_MAINNET,
// Sets a session for 2 days, takes the input
// in seconds.
sessionTime: 172800
)
do {
let singleFactoreAuth = try SingleFactorAuth(params: options)
try await singleFactoreAuth.initialize()
} catch {
// Handle error
}
Session Management
To check whether the user is authenticated, you can use the initialize
method. For a user already
authenticated, the result would be a non-nullable SFAKey
. You can navigate to different views
based on the result.
do {
let result = try await singleFactorAuth.initialize()
// Handle Success
} catch let error {
// Handle Error
}
Authentication
If the user is not authenticated, you should utilize the connect
method. For the guide, we will
add Email Password login using Firebase. The connect
method is pretty straightforward in
SingleFactorAuth and takes LoginParams
as input. After successfully logging in, the method will
return the SFAKey
.
Learn more about SingleFactorAuth LoginParams. To more about Firebase login methods, please checkout the Firebase documentation.
do {
// Replace email and password with user's email and password
let res = try await Auth.auth().signIn(withEmail: "ios@firebase.com", password: "iOS@Web3Auth")
let id_token = try await res.user.getIDToken()
// Replace with the verifier you created in Web3Auth Dashboard
// for your project.
let verifierName = "w3a-firebase-demo"
let sfaKey = try await singleFactorAuth.connect(
loginParams: .init(
verifier: verifierName,
verifierId: res.user.uid,
idToken: id_token
)
)
} catch {
// Handle error
}
Set up Blockchain Providers
Once we have successfully authenticated the user, the next step would be to fetch the user details, retrieve wallet address and prepare blockchain providers for interactions. For this guide, we are supporting only Ethereum ecosystem, but the general idea can be used for any blockchain ecosystem.
For interacting with ethereum chains, we'll use the web3.swift package. To install the single-factor-auth-swift package, you have two options. You can either use the Swift Package Manager, or you can install using Cocoapods.
- Swift Package Manager
- Cocoapods
-
In Xcode, with your app project open, navigate to File > Add Packages.
-
When prompted, add the web3.swift package repository:
https://github.com/argentlabs/web3.swift
From the
Dependency Rule
dropdown, selectExact Version
and enter1.6.1
as the version. -
When finished, Xcode will automatically begin resolving and downloading your dependencies in the background.
Add web3.swift
as a dependency to your Podfile
.
pod 'web3.swift', '~> 1.6.1'
After successfully installing package, it's time to create an extension for SFAKey
which conforms
to EthereumSingleKeyStorageProtocol
.
extension SFAKey: EthereumSingleKeyStorageProtocol {
public func storePrivateKey(key: Data) throws {}
public func loadPrivateKey() throws -> Data {
guard let privKeyData = Data.init(hex: self.getPrivateKey()) else {
return Data()
}
return privKeyData
}
}
Once, we have created the extension, we will create EthereumAccount
instance to retrive user's EOA
address, and sign the transactions.
let ethereumAccount = try! EthereumAccount(
keyStorage: sfaKey as EthereumSingleKeyStorageProtocol
)
let address = ethereumAccount.address
To retrive user's balance, and interacting with Blockcahin, you can follow our detailed guide on how to interact with EVM chain guides. To interact with Solana blockchain, you can checkout our Solana blockchain guide.
Conclusion
Voila, you have learned how to use Web3Auth SFA SDK with iOS application.
If you are interested in learning more about SFA SDK, please checkout our documentation for iOS SFA SDK. You can find the code used for the guide on our examples repo.