CoboSDK-iOS
CoboSDK 帮助开发者通过Cobo 钱包进行以太坊交易的签名和发送。DApp 开发者可以使用 CoboSDK 获取用户的以太坊账户地址,完成消息的签名和验证,发起交易、调用智能合约以及广播签名后的交易数据。从而省去了开发者自行实现用户私钥管理和钱包功能的工作。
接入 CoboSDK
接入 CoboSDK 需要以下几个步骤:
添加依赖
- Cocoapods
在您的 Podfile
中添加:
pod 'CoboSDK', :git => 'https://github.com/cobowallet/CoboSDK-iOS.git'
然后在终端中执行 $ pod install
- Carthage
TODO
注册 URL Scheme
在 Xcode 中,选择项目设置选项,选中 TARGETS
,打开 Info
标签页,点击 URL Types
中的 +
按钮添加新的 URL Type。在 URL Schemes
中填写应用的 URL Scheme。URL Scheme 应尽可能唯一,建议使用 <Bundle ID>.cobo
。
将 URL Scheme 注册到 CoboSDK 中,这里的 URL Scheme 应与 URL Type
中添加的 URL Scheme 相同。
CoboSDK.shared.setup(callbackScheme: <replace with your url scheme>)
LSApplicationQueriesSchemes
添加 在应用的 Info.plist
文件中,添加如下内容:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>cobo-wallet</string>
</array>
接收返回结果
通过实现AppDelegate
中的func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
方法来接收返回的结果数据:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return CoboSDK.shared.application(app, open: url, options: options)
}
使用SDK
签名消息
let message = "Hello Cobo!"
CoboSDK.shared.signMessage(message: message) { result in
switch result {
case .success(let value):
guard let address = value.address, let signature = value.signature else { break }
print(address)
let match = CoboSDK.shared.verifyMessage(address: address, signature: signature, message: message)
print("\(match)")
case .failure(let error):
print(error.localizedDescription)
}
}
发送交易
let gasPrice = BigUInt(stringLiteral: "1000000000") // 1 Gwei
let gasLimit = BigUInt(21000)
let value = BigUInt(stringLiteral: "1000000000000000000") // 1 ETH
let from = <replace with address in sign message result>
let to = <replace with address to transfer to>
let tx = EthereumTransaction(gasPrice: gasPrice, gasLimit: gasLimit, to: to, value: value, data: Data())
CoboSDK.shared.sendTransaction(transaction: tx, from: from) { result in
switch result {
case .success(let value):
guard let hash = value.hash, let tx = value.rawTransaction else { break }
print("hash: \(hash)")
case .failure(let error):
print(error.localizedDescription)
}
}
更多功能请参阅Example
工程。