Integrate Web3Auth with the Arbitrum Blockchain in iOS/Swift Applications
While using the Web3Auth iOS SDK, you get the private key within the user scope. This private key
can interact with Arbitrum to make any blockchain calls, like getting the
user's account
, fetching balance
, sign transaction
, send transaction
, read
from and
write
to the smart contract, etc. We have highlighted a few here to get you started quickly on
that.
Installation
To interact with the Ethereum compatible blockchains in Swift, you can use any
EIP1193
compatible package. Here, we're using
web3swift to demonstrate how to make blockchain calls
using it with Web3Auth.
To install the web3swift
package, you have two options. You can either use Swift Package Manager
or 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/argentlabs/web3.swift
-
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 'web3.swift'
-
Once added, use
pod install
command to download the dependency.
Chain Details for Arbitrum
- Mainnet
- Testnet
- Chain ID: 0xA4B1
- Public RPC URL: https://rpc.ankr.com/arbitrum (Avoid using public rpcTarget in production, use services like Infura, Quicknode etc)
- Display Name: Arbitrum Mainnet
- Block Explorer Link: https://arbiscan.io
- Ticker: AETH
- Ticker Name: AETH
- Chain ID: 0x66EEE
- Public RPC URL: https://rpc.ankr.com/arbitrum_sepolia (Avoid using public rpcTarget in production, use services like Infura, Quicknode etc)
- Display Name: Arbitrum Sepolia Testnet
- Block Explorer Link: https://sepolia.arbiscan.io/
- Ticker: AETH
- Ticker Name: AETH
Initialize
To interact with the blockchain, initialize the EthereumHttpClient
with the RPC URL, and chain id
of the network you want to connect to. To get the public RPC URL, and other details as chain id, you
can checkout ChainList.
import web3
let client = EthereumHttpClient(
// Please avoid using public RPC URL in production, use services
// like Infura, Quicknode, etc.
url: URL.init(string: rpcUrl)!,
// Replace with the chain id of the network you want to connect to
network: .custom(chainId)
)
Get User Info
You get the user information after a successful login returned from the login
method. The userInfo
object contains the user information, whereas
the privKey
object contains the private key that can be used to make blockchain calls.
let result = try await Web3Auth(.init(
clientId: clientId,
network: network)).login(
W3ALoginParams(loginProvider: provider)
)
await MainActor.run(body: {
user = result
loggedIn = true
})
let userInfo = user.userInfo
let privKey = user.privKey
Get Account
To retrieve the user's Ethereum address, you can use the EthereumAccount
class. This account can
also be used to sign transactions and messages.
The package doesn't provides any direct way to consume the the private key and create an account.
Hence, we'll create an extension for the Web3AuthState
extending the
EthereumSingleKeyStorageProtocol
to retrieve the private key and create an account.
import web3
import Web3Auth
extension Web3AuthState: EthereumSingleKeyStorageProtocol {
public func storePrivateKey(key: Data) throws {
}
public func loadPrivateKey() throws -> Data {
guard let data = self.privKey?.web3.hexData else {
throw PlaygroundError.decodingError
}
return data
}
}
Once we have created the extension, we can use the Web3AuthState to create an EthereumAccount. Please note, that this assumes that the user has already logged in and the state is available.
import web3
// Use your existing Web3Auth instance
let web3authState = web3Auth.state!
let account = try EthereumAccount(keyStorage: web3authState)
let address = account.address