Integrate Web3Auth with the Solana Blockchain in iOS
While using the Web3Auth iOS SDK, you can retrive the Ed25519 private key upon successful authentication. This private key can be used to derive user's public address and interact with Solana chain. We have highlighted a few methods here for getting you started quickly on that.
Installation
To interact with the Solana blockchain in iOS, you can use any Solana compatible SDK. Here, we're using SolanaSwift to demonstrate how to interact with Solana chain using Web3Auth.
- Swift Package Manager
- CocoaPods
To install SolanaSwift
through Swift Package Manager, follow the below steps:
-
Open your project in Xcode, navigate to File > Add Package Dependencies.
-
When prompted, add the SolanaSwift SDK repository:
https://github.com/p2p-org/solana-swift
From the
Dependency Rule
dropdown, selectExact Version
and enter5.0.0
as the version.
Once finished, Xcode will automatically begin resolving and downloading your dependencies in the background.
To install using CocoaPods, simply add the following line to your Podfile
pod 'SolanaSwift', '~> 5.0.0'
Once you have added SolanaSwift to Podfile, run the following command:
pod install
Initialize
To Initialize the JSONRPCAPIClient
we require rpc url. The JSONRPCAPIClient
instance will
provide a gateway & protocol to interact with Solana cluster while sending requests and receving
response. For this example, we are using rpc url for Devnet-beta. To interact with Testnet or
Mainnet, you can simply change the rpc url.
Initializing Solana SDK
In the below code block, we'll create the JSONRPCAPIClient
instance using the Devnet-beta rpc.
import SolanaSwift
let endpoint = APIEndPoint(
address: "https://api.devnet.solana.com",
network: .devnet
)
let solanaJSONRPCClient = JSONRPCAPIClient(endpoint: endpoint)
Initializing Web3Auth SDK
In the below code block, we'll initialize the Web3Auth SDK and check whether the user has any
Web3Auth session persisted or not. If the user is already authenticated, you can route them directly
to home view, otherwise you can route them to login view for authentication purpose. For checking,
if user is already authenticated, we can check whether Web3Auth.state
is nil
or not.
By default, the session is persisted for 1 day. You can modify it using sessionTime
parameter
during initialization.
Note: Note: The session can be persisted only for 30 days max
import Web3Auth
// Initialize Web3Auth SDK
let web3Auth = await Web3Auth(
W3AInitParams(
clientId: "BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ",
network: Network.sapphire_mainnet,
redirectUrl: "com.w3a.ios-solana-example"
)
)
// Check whether state is nil or not for user authentication status.
let isUserAuthenticated = web3Auth.state != nil
// Customize your logic to perform operations or navigation