EthereumWeb3.swift
支持开放钱包的 Swift Ethereum Web3 库
目标
此库集成 Boilertalk/Web3.swift 库与 OpenWallet.swift 以及 Wallet.swift。
它通过 HTTP 支持主要的 Ethereum Web3 方法。
入门
安装
OpenWallet.swift(客户端)
使用将以下内容添加到您的 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'
然后运行 pod install
。
Wallet.swift(钱包)
使用将以下内容添加到您的 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'
然后运行 pod install
。
支持的方法
有关支持的方法和API参考,请查看 Boilertalk/Web3.swift 存储库。
示例
OpenWallet.swift(客户端)
新建交易
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 (钱包)
新交易
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)
}
}
作者
许可
EthereumWeb3.swift
在 Apache 2.0 许可下可用。有关更多信息,请参阅 LICENSE 文件。