Giter VIP home page Giter VIP logo

cobosdk-ios's Introduction

CoboSDK-iOS

CoboSDK帮助开发者通过Cobo钱包进行ethereum交易的签名和发送。 DApp开发者可以使用CoboSDK获取用户的ethereum账户地址,完成消息的签名和校验,发起交易、调用智能合约以及广播签名后的交易数据。从而省去了开发者自行实现用户私钥管理和钱包功能的工作。

*其他语言:English

接入CoboSDK

接入CoboSDK需要如下几个步骤:

添加依赖

  • Cocoapods

在你的Podfile中添加:

pod 'CoboSDK'

然后在终端中执行$ pod install

  • Carthage
TODO

注册URL Scheme

在Xcode中,选择工程设置项,选中TARGETS,打开Info标签页,点击URL Types中的+按钮添加新的URL Type。在URL Schemes 中填入应用的URL Scheme。URL Scheme应当尽可能唯一,建议使用<Bundle ID>.cobo

URL Types

注册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>

Info.plist

接收返回结果

实现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工程。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.