Integrate Embedded Wallets with the Solana Blockchain in iOS
While using the Embedded Wallets iOS SDK (formerly Web3Auth), you can retrieve the Ed25519 private key upon successful authentication. This private key can be used to derive the user's public address and interact with the Solana chain. We’ve highlighted a few methods here to get you started quickly.
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.
Chain details for Solana
- Mainnet
- Testnet
- Devnet
- Chain Namespace: SOLANA
- Chain ID: 0x1
- Public RPC URL: https://api.mainnet-beta.solana.com (avoid public RPC in production; prefer managed services)
- Display Name: Solana Mainnet
- Block Explorer Link: https://explorer.solana.com
- Ticker: SOL
- Ticker Name: Solana
- Logo: https://images.toruswallet.io/solana.svg
- Chain Namespace: SOLANA
- Chain ID: 0x2
- Public RPC URL: https://api.testnet.solana.com (avoid public RPC in production; prefer managed services)
- Display Name: Solana Testnet
- Block Explorer Link: https://explorer.solana.com
- Ticker: SOL
- Ticker Name: Solana
- Logo: https://images.toruswallet.io/solana.svg
- Chain Namespace: SOLANA
- Chain ID: 0x3
- Public RPC URL: https://api.devnet.solana.com (avoid public RPC in production; prefer managed services)
- Display Name: Solana Devnet
- Block Explorer Link: https://explorer.solana.com?cluster=devnet
- Ticker: SOL
- Ticker Name: Solana
- Logo: https://images.toruswallet.io/solana.svg
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-swiftFrom the
Dependency Ruledropdown, selectExact Versionand enter5.0.0as 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 the 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 the 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