EthereumWeb3.swift
Ethereum Web3 Lirary for Swift with Open Wallet support
Goals
This library intergates Boilertalk/Web3.swift library with OpenWallet.swift and Wallet.swift.
It supports major Ethereum Web3 methods through HTTP.
Getting started
Installation
OpenWallet.swift (Client)
WithAdd the following to your Podfile:
pod 'Tesseract.OpenWallet/Ethereum'
pod 'Tesseract.EthereumWeb3'
# Uncomment this lines if you want to enable PromiseKit extensions
# pod 'Tesseract.OpenWallet/EthereumPromiseKit'
# pod 'Tesseract.EthereumWeb3/PromiseKit'
Then run pod install
.
Wallet.swift (Wallet)
WithAdd the following to your Podfile:
pod 'Tesseract.Wallet/Ethereum'
pod 'Tesseract.EthereumWeb3'
# Uncomment this lines if you want to enable PromiseKit extensions
# pod 'Tesseract.Wallet/EthereumPromiseKit'
# pod 'Tesseract.EthereumWeb3/PromiseKit'
Then run pod install
.
Supported methods
For the list of supported methods and API reference check Boilertalk/Web3.swift repository.
Examples
OpenWallet.swift (Client)
New transaction
import OpenWallet
import EthereumWeb3
// HTTP RPC URL
let rpcUrl = "https://mainnet.infura.io/v3/{API-KEY}"
// Initializing OpenWallet with Ethereum. Creating Web3 instance
// Store your OpenWallet instance somewhere(AppDelegate, Context). It should be reused.
// If you need only Web3, you can store it only(it will store OpenWallet inside itself).
let web3 = OpenWallet(networks: [.Ethereum]).ethereum.web3(rpcUrl: rpcUrl)
// Creating Transaction
let tx = EthereumTransaction(
from: try! EthereumAddress(hex: "0x...", eip55: false),
to: try! EthereumAddress(hex: "0x...", eip55: false),
value: 1.eth
)
// Sending it. OpenWallet will handle signing automatically.
web3.eth.sendTransaction(transaction: tx) { response in
switch response.status {
case .success(let hash): print("TX Hash:", hash.hex())
case .failure(let err): print("Error:", error)
}
}
Wallet.swift (Wallet)
New transaction
import Wallet
import EthereumWeb3
// Path to sqlite database with wallets
let dbPath = "path/to/database.sqlite"
// Wallet Storage
let storage = try! DatabaseWalletStorage(path: dbPath)
// Applying migrations
try! storage.bootstrap()
// Creating manager with Ethereum network support
let manager = try! Manager(networks: [EthereumNetwork()], storage: storage)
// Restoring wallet data from mnemonic
let walletData = try! manager.restoreWalletData(mnemonic: "aba caba ...", password: "12345678")
// Creating wallet from data
let wallet = try! manager.create(from: walletData)
// Unlocking wallet
try! wallet.unlock(password: "12345678")
// Adding first account
let account = wallet.addAccount()
// HTTP RPC URL
let rpcUrl = "https://mainnet.infura.io/v3/{API-KEY}"
// Creating Web3 for this Wallet
let web3 = wallet.ethereum.web3(rpcUrl: rpcUrl)
// Creating Transaction
let tx = EthereumTransaction(
from: try! account.eth_address().web3,
to: try! EthereumAddress(hex: "0x...", eip55: false),
value: 1.eth
)
// Wallet will sign this transaction automatically (with 'from' account)
web3.eth.sendTransaction(transaction: tx) { response in
switch response.status {
case .success(let hash): print("TX Hash:", hash.hex())
case .failure(let err): print("Error:", error)
}
}
Author
License
EthereumWeb3.swift
is available under the Apache 2.0 license. See the LICENSE file for more information.