Integrate Embedded Wallets with the BNB Blockchain in iOS/Swift Applications
While using the Web3Auth iOS SDK, you get the private key within the user scope after successful authorization. This private key can be used to retrieve the user's address, and interact with BNB to make any blockchain calls. We have highlighted a few here for getting you started quickly on that.
Chain details for BNB
- Mainnet
- Testnet
- Chain ID: 0x38
- RPC URL: You can use our bundled RPC service from Infura, or your own choice of RPC service for production.
- Display Name: BNB Smart Chain Mainnet
- Block Explorer Link: https://bscscan.com
- Ticker: BNB
- Ticker Name: Binance Coin
- Chain ID: 0x61
- Public RPC URL: https://data-seed-prebsc-1-s1.binance.org:8545
- Display Name: BNB Smart Chain Testnet
- Block Explorer Link: https://testnet.bscscan.com
- Ticker: BNB
- Ticker Name: Binance Coin
Prerequisites
- Project is set up in the Web3Auth dashboard
- Web3Auth is integrated in your iOS app
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 installcommand to download the dependency.
Initialize
Initialize the EthereumHttpClient with the RPC URL and chain id
of the network you want to connect to, to interact with the blockchain. To get the public RPC URL, and other details as chain id, see ChainList.
import web3
let client = EthereumHttpClient(
// Please avoid using public RPC URL in production, use services
// like Infura.
url: URL.init(string: rpcUrl)!,
// Replace with the chain id of the network you want to connect to
network: .custom(chainId)
)