Guide
What is TokenUp Wallet SDK
The TokenUp Wallet SDK provides a streamlined gateway for users to adopt Web3 effortlessly. It eliminates the complexities of wallet management and user relationships, abstracts away low-level blockchain communication, and enables rapid wallet integration – empowering developers to build with ease.
Prerequisites
- Use Xcode 15.4 or later
- Target iOS 15.0 or later
Importing TokenUp Wallet SDK
- Add
TokenUpWalletSdk.frameworkto your Xcode project and set it toEmbed & Sign
Initialization
Before building the wallet, call the initSDK method of WalletSDKManager.manager to initialize the SDK. This operation only needs to be performed once, preferably at app launch.
The following example demonstrates how to call the initSDK method in AppDelegate:
import TokenUp Wallet SDK
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
WalletSDKManager.manager.initSDK()
return true
}
}
Login
- Call the
loginmethod ofWalletSDKManager.managerto log in, which allows you to store your wallet-related information in iCloud. Thecacheparameter indicates whether to cache the current login state.
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
login()
}
func login() {
WalletSDKManager.manager.login(cache: false) { result in
switch result {
case .success:
//Handle post-login logic
case .failure(let error):
//Handle failure
}
}
}
}
- Call the
checkIsLogininterface ofWalletSDKManager.managerto verify the login status
func checkIsLogin
- Call the
logoutinterface ofWalletSDKManager.managerto log out
func logout(callback: @escaping () -> Void)
Account
- Call the
getAllAccountinterface ofWalletSDKManager.managerto retrieve all wallet account information you have created.syncdefaults to false, meaning it fetches all account information from the local cache. If you need to synchronize account information from iCloud, set it to true
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
getAllAccountList()
}
func getAllAccountList() {
WalletSDKManager.manager.getAllAccount(sync: false) { result in
switch result {
case .success:
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
- Call the
getCurrentAccountinterface ofWalletSDKManager.managerto retrieve the current account information
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
getCurrentAccount()
}
func getCurrentAccount() {
WalletSDKManager.manager.getCurrentAccount { result in
switch result {
case .success:
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
- Call the
addAccountinterface ofWalletSDKManager.managerto add an account.passwordis set to empty, andsupportedChainTypeswill default to creating a wallet on the HC, Tron, Ethereum, Solana, TON, and TONTESTNET networks. You can also modify this based on your needs. Themnemonicparameter is used when you need to import a wallet using a mnemonic phrase. TheprivateKeyparameter is used when you need to import a wallet using a private key
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
addAccount()
}
func addAccount() {
WalletSDKManager.manager.addAccount(password: "") { result in
switch result {
case .success:
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
- Call the
delAccountinterface ofWalletSDKManager.managerto delete an account.accountIdis the id of the AccouAccountModelntModel object you want to delete, andpasswordis set to empty
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
delAccount()
}
func delAccount() {
WalletSDKManager.manager.delAccount((accountId: "targetAccountId", password: "")) { result in
switch result {
case .success:
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
- Call the
switchAccountinterface ofWalletSDKManager.managerto switch accounts.accountIdis the id of the targetAccountModelobject you want to switch to
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
switchAccount()
}
func delAccount() {
WalletSDKManager.manager.switchAccount((accountId: "targetAccountId")) { result in
switch result {
case .success:
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
- Call the
getMnemonicinterface ofWalletSDKManager.managerto retrieve the mnemonic phrase information.chainTypeshould be obtained from the current network,accountIdis theidof the target account'sAccountModelobject, and thepasswordparameter is set to empty by default.
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
getMnemonic()
}
func getMnemonic() {
WalletSDKManager.manager.getMnemonic(chainType: "chainType", accountId: "targetAccountId", password: "") { result in
switch result {
case .success:
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
- Call the
getPrivateKeyinterface ofWalletSDKManager.managerto retrieve the private key information.accountIdis theidof the target account'sAccountModelobject, and thepasswordparameter is set to empty by default.
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
getMnemonic()
}
func getMnemonic() {
WalletSDKManager.manager.getMnemonic(chainType: "chainType", accountId: "targetAccountId", password: "") { result in
switch result {
case .success:
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
- Call the
changePasswordinterface ofWalletSDKManager.managerto change your password.passwordis your current password, andnewPasswordis the new password you create.
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
changePassword()
}
func changePassword() {
WalletSDKManager.manager.changePassword(password: "Current password", newPassword: "New password") { result in
switch result {
case .success:
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
- Call the
checkPasswordinterface ofWalletSDKManager.managerto verify if your current password is correct.currentPasswordis your current password.
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
checkPassword()
}
func checkPassword() {
WalletSDKManager.manager.checkPassword(password: "Current password") { result in
switch result {
case .success:
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
Network
- Call the
getCurrentNetworkinterface ofWalletSDKManager.managerto retrieve the current network information.
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
getCurrentNetwork()
}
func getCurrentNetwork() {
WalletSDKManager.manager.getCurrentNetwork(chainType: "ChainType") { result in
switch result {
case .success(let currentNetwork):
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
- Call the
getAllNetworkinterface ofWalletSDKManager.managerto retrieve the list of network information.
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
getAllNetwork()
}
func getAllNetwork() {
WalletSDKManager.manager.getAllNetwork { result in
switch result {
case .success(let networkList):
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
- Call the
switchNetworkinterface ofWalletSDKManager.managerto switch networks.chainTypeis the chainType of your current network, andnetworkis the targetNetworkModelobject you want to switch to.
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
switchNetwork()
}
func switchNetwork() {
WalletSDKManager.manager.switchNetwork(chainType:"Current network chainType", network:"targetNetwork") { result in
switch result {
case .success(let tokenList):
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
- Call the
getSupportedChainsinterface ofWalletSDKManager.managerto retrieve the list of currently supported networks.
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
getSupportedChains()
}
func getSupportedChains() {
WalletSDKManager.manager.getSupportedChains { result in
switch result {
case .success(let supportList):
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
Transaction
- Call the
getTokenListinterface ofWalletSDKManager.managerto retrieve tokenList information.chainTypeis the chainType of your current network, andchainidis the chainid of your current network.
import WalletSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
getTokenList()
}
func getTokenList() {
WalletSDKManager.manager.getTokenList(chainType:"Current ntwork chainType", "chainid":"Current ntwork chainid") { result in
switch result {
case .success(let tokenList):
//Handle success
case .failure(let error):
//Handle failure
}
}
}
}
- Call the
estimateGasinterface ofWalletSDKManager.managerto retrieve the estimated gas fee.
// from: Current wallet address
// to: Transfer address
// amount: Transfer amount
// contract: Token contract address (for native tokens, pass the symbol)
// isEIP1559: Only required for EVM
// suggestGas1559Url: Only required for EVM
func estimateGas(from: String, to: String, amount: String, contract: String, isEIP1559: Bool, suggestGas1559Url: String, callback: @escaping (Result<EstimateGasModel?, JsBridgeError>) -> Void)
- Call the
getTotalBalanceinterface ofWalletSDKManager.managerto retrieve the total balance.addressis the target wallet address.
func getTotalBalance(address: String, callback: @escaping (Result<BalanceModel?, JsBridgeError>) -> Void)
- Call the
estimateGasinterface ofWalletSDKManager.managerto retrieve the balance information of a specified token.addressis the target wallet address, andcontractis the target token contract address.
func getSpecificTokenBalance(address: String, contract: String, callback: @escaping (Result<BalanceModel?, JsBridgeError>) -> Void)
- Call the
transferinterface ofWalletSDKManager.managerto handle transfer transactions.
// from: Source address for the transfer
// to: Destination address for the transfer
// amount: Transfer amount
// contract: Token contract address for the transfer (for native tokens, pass the symbol)
// password: Password
// chainId: Only required for EVM, the chainid of the current network,
// maxPriorityFeePerGas: Only required for EVM
// maxFeePerGas: Only required for EVM
// gasLimit: Only required for EVM
// gasPrice: Only required for EVM
func transfer(
from: String,
to: String,
amount: String,
contract: String,
password: String,
maxPriorityFeePerGas: String,
maxFeePerGas: String,
gasLimit: String,
gasPrice: String,
callback: @escaping (Result<String?, JsBridgeError>) -> Void)
- Call the
checkAddressinterface ofWalletSDKManager.managerto verify if a wallet address is valid in transfer transactions.chainTypeis the chainType of your current network, andaddressis the target wallet address.
func checkAddress(chainType: String, address: String, callback: @escaping (Result<CheckAddressModel?, JsBridgeError>) -> Void)
- Call the
getFiatcurrencyExchangeRateinterface ofWalletSDKManager.managerto retrieve the exchange rate information of tokens.
func getFiatcurrencyExchangeRate(callback: @escaping (Result<Any?, JsBridgeError>) -> Void)
- Call the
getCheckTokenPriceinterface ofWalletSDKManager.managerto retrieve the transaction price of tokens.
// params : key is chainid, value is the token contract address (for native tokens, pass the symbol)
func getCheckTokenPrice(params:[[String : String]]?, callback: @escaping (Result<Any?, JsBridgeError>) -> Void)
Dapp Events
Before integrating Dapp events, please inject the js first. The following example demonstrates how to inject js in a webView:
class DappWebViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
addUserScript()
}
func addUserScript() {
let configuration = WKWebViewConfiguration()
configuration.applicationNameForUserAgent = "Version/8.0.2 Safari/600.2.5"
if let injectJsFilePath = Bundle.main.path(forResource: "injectJs.iife", ofType: "js") {
do {
let injectJsContent = try String(contentsOfFile: injectJsFilePath, encoding: .utf8)
let injectScript = WKUserScript(source: injectJsContent, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let contentController = WKUserContentController()
contentController.addUserScript(injectScript)
configuration.userContentController = contentController
} catch {
print("Failed to load JavaScript file: \(error)")
}
}
let webView = WKWebView(frame: UIScreen.main.bounds, configuration: configuration)
view.addSubview(webView)
}
}
Event interaction, the following is an example using WKWebViewJavascriptBridge:
func registerHandlers() {
bridge?.register(handlerName: "hInjectJsSend") { parameters, callback in
//Event name and related parameters need to be parsed from parameters
}
}
HC
Connection
- Retrieve the list of relevant accounts for the target network that the current Dapp needs to connect to, and select the account to connect.
Sign Message
- Retrieve the target network and account connected by the current Dapp and perform signing.
Sign Transaction
- Call the
hcParseTransactionmethod ofWalletSDKManager.managerto retrieve the signature.
func hcParseTransaction(transactions:[String], rpc: String, completion: @escaping (Result<HCParseTransaction?, JsBridgeError>) -> Void)
- Call the
hcEstimateGasByRawTxmethod ofWalletSDKManager.managerto retrieve Gas information.
func hcEstimateGasByRawTx(transactions:[String], rpc: String, completion: @escaping (Result<HCEstimateGasByRawTx?, JsBridgeError>) -> Void)
- Call the
hcSignRawTransactionmethod ofWalletSDKManager.managerto sign the transaction.
func hcSignRawTransaction(transaction:String, accountId: String, chainType: String, password:String, completion: @escaping (Result<HCSignRawTransaction?, JsBridgeError>) -> Void)
- Call the
hcSendSignedTransactionmethod ofWalletSDKManager.managerto sign and send the transaction.
func hcSendSignedTransaction(transaction:String, rpc: String, completion: @escaping (Result<HCSendSignedTransaction?, JsBridgeError>) -> Void)
- Callback the above results to js.
EVM
Connection
- Retrieve the list of relevant accounts for the target network that the current Dapp needs to connect to, and select the account to connect.
Sign Message
- Retrieve the target network and account connected by the current Dapp and perform signing.
Sign Transaction & Sign and Send Transaction
- Call the
ethParseTransactionmethod ofWalletSDKManager.managerto retrieve related events.
func ethParseTransaction(to: String, value: String, data: String, completion: @escaping (Result<ETHParseTransaction?, JsBridgeError>) -> Void)
- Call the
ethEstimateGasmethod ofWalletSDKManager.managerto retrieve Gas information.
func ethEstimateGas(from: String, to: String, rpc: String, value: String, data: String, isEIP1559: Int, chainId: String, completion: @escaping (Result<EstimateGasModel?, JsBridgeError>) -> Void)
- Call the
getTokenMetadatamethod ofWalletSDKManager.managerto retrieve symbol-related information.
func getTokenMetadata(chainType: String, contract:String, callback: @escaping (Result<TokenMetaData?, JsBridgeError>) -> Void)
- Call the
ethSignTransactionmethod ofWalletSDKManager.managerto sign the transaction.
func ethSignTransaction(accountId: String, chainType: String, from: String, to: String, rpc: String, password: String, chainId:String, amount: String, data: String, contract:String, nonce: String, maxPriorityFeePerGas: String, maxFeePerGas:String, gasLimit:String, gasPrice:String, completion: @escaping (Result<ETHSignTransaction?, JsBridgeError>) -> Void)
- Call the
ethSendSignedTransactionmethod ofWalletSDKManager.managerto sign and send the transaction.
func ethSendSignedTransaction(signedTransaction:String, rpc: String, completion: @escaping (Result<ETHSendSignedTransaction?, JsBridgeError>) -> Void)
- Callback the above results to js.
Tron
Connection
- Retrieve the list of relevant accounts for the target network that the current Dapp needs to connect to, and select the account to connect.
Sign Message
- Retrieve the target network and account connected by the current Dapp and perform signing.
Sign Transaction
- Call the
tronParseTransactionmethod ofWalletSDKManager.managerto retrieve signature information.
func tronParseTransaction(input:String, rpc: String, completion: @escaping (Result<TRONParseTransaction?, JsBridgeError>) -> Void)
- Call the
tronEstimateGasByRawTxmethod ofWalletSDKManager.managerto retrieve Gas information.
func tronEstimateGasByRawTx(transaction:String, rpc: String, from: String, input: String, completion: @escaping (Result<TRONEstimateGasByRawTx?, JsBridgeError>) -> Void
- Call the
tronSignRawTransactionmethod ofWalletSDKManager.managerto sign the transaction.
func tronSignRawTransaction(transaction:String, accountId: String, chainType: String, password:String, completion: @escaping (Result<TRONSignRawTransaction?, JsBridgeError>) -> Void)
- Callback the above results to js.
Solana
Connection
- Retrieve the list of relevant accounts for the target network that the current Dapp needs to connect to, and select the account to connect.
Sign Message
- Retrieve the target network and account connected by the current Dapp and perform signing.
Sign Transaction & Sign and Send Transaction
- Call the
solParseTransactionmethod ofWalletSDKManager.managerto retrieve signature information.
func solParseTransaction(transactions:[String], rpc: String, signerAddress:String, completion: @escaping (Result<SOLParseTransaction?, JsBridgeError>) -> Void)
- Call the
solEstimateGasByRawTxmethod ofWalletSDKManager.managerto retrieve Gas information.
func solEstimateGasByRawTx(transactions:[String], rpc: String, completion: @escaping (Result<SOLEstimateGasByRawTx?, JsBridgeError>) -> Void)
- Call the
solSignRawTransactionmethod ofWalletSDKManager.managerto sign the transaction.
func solSignRawTransaction(transaction:String, accountId: String, chainType: String, password:String, completion: @escaping (Result<SOLSignRawTransaction?, JsBridgeError>) -> Void)
- Call the
solSendSignedTransactionmethod ofWalletSDKManager.managerto sign and send the transaction.
func solSendSignedTransaction(transaction:String, rpc: String, completion: @escaping (Result<SOLSendSignedTransaction?, JsBridgeError>) -> Void)
- Callback the above results to js.