TesseractSDK 0.1.1

TesseractSDK 0.1.1

Yehor Popovych 维护。



Tesseract dApps 平台 SDK for Swift

GitHub license Build Status GitHub release CocoaPods version Platform iOS

开始使用

要使用此库,需要在设备上安装兼容的钱包。

我们发布了自己的 Tesseract 钱包 作为参考钱包实现。在您的设备上安装它以检查提供的示例。

以太坊

安装

将以下内容添加到您的 Podfile

pod 'TesseractSDK/Ethereum'

然后运行 pod install

Hello Tesseract, hello Web3.

让我们尝试获取以太坊账户余额。

import Tesseract

// Check that we have wallet installed. You should handle this situation in your app.
guard Tesseract.Ethereum.isKeychainInstalled else {
    fatalError("Wallet is not installed!")
}

// Our HTTP RPC URL. Can be Infura
let rpcUrl = "https://mainnet.infura.io/v3/{API-KEY}"

// Creating Web3 instance. Try to reuse existing instance of Web3 in your app.
let web3 = Tesseract.Ethereum.Web3(rpcUrl: rpcUrl)

// Asking wallet for the Account
web3.eth.accounts() { response in
    // Check that we have response
    guard let accounts = response.result else {
        print("Error:", response.error!)
        return
    }
    // Asking network for balance
    web3.eth.getBalance(address: accounts[0], block: .latest) { response in
        switch response.status {
        case .success(let balance): print("Balance:", balance)
        case .failure(let err): print("Error:", err)
        }
    }
}

使用PromiseKit

安装PromiseKit扩展

将以下内容添加到您的 Podfile

pod 'TesseractSDK/Ethereum.PromiseKit'

然后运行 pod install

现在你可以像这样做Web3
import PromiseKit

// Asking wallet for Account
web3.eth.accounts()
    .then { accounts in
        // Obtaining balance
        web3.eth.getBalance(address: accounts[0], block: .latest)
    }.done { balance in
        print("Balance:", balance)
    }.catch { err in
        print("Error:", err)
    }

示例

新事务

// Creating Transaction
let tx = Ethereum.Transaction(
    from: account, // Account from previous examples
    to: try! Ethereum.Address(hex: "0x...", eip55: false),
    value: 1.eth
)

// Sending it. Tesseract 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:", err)
    }
}
PromiseKit
// Sending it. Tesseract will handle signing automatically.
web3.eth.sendTransaction(transaction: tx)
    .done { hash in
        print("TX Hash:", hash.hex())
    }.catch { err in
        print("Error:", err)
    }

ERC20智能合约

创建智能合约实例
// EOS ERC20 token
let contractAddress = try! Ethereum.Address(hex: "0x86Fa049857E0209aa7D9e616F7eb3b3B78ECfdb0", eip55: true)
// ERC20 contract object
let contract = web3.eth.Contract(type: GenericERC20Contract.self, address: contractAddress)
获取ERC20余额
contract.balanceOf(address: account) // Account from previous steps
    .call() // Performing Ethereum Call
    .done { outputs in
        print("Balance:", outputs["_balance"] as! BigUInt)
    }.catch { error in
        print("Error:", error)
    }
发送ERC20代币
// Our recipient
let recipient = try! Ethereum.Address(hex: "0x....", eip55: true)

contract
    .transfer(to: recipient, value: 100000) // Creating SC invocaion
    .send(from: account) // Sending it from our account
    .done { hash in
        print("TX Hash:", hash.hex())
    }.catch { err in
        print("Error:", err)
    }

自定义智能合约

Web3 可以解析您的 JSON 智能合约 ABI。

您可以通过名称从合约对象中对智能合约的方法进行订阅。

创建智能合约实例
// EOS ERC20 token
let contractAddress = try! Ethereum.Address(hex: "0x86Fa049857E0209aa7D9e616F7eb3b3B78ECfdb0", eip55: true)
// JSON ABI. Can be loaded from json file
let contractJsonABI = "<your contract ABI as a JSON string>".data(using: .utf8)!
// You can optionally pass an abiKey param if the actual abi is nested and not the top level element of the json
let contract = try web3.eth.Contract(json: contractJsonABI, abiKey: nil, address: contractAddress)
获取ERC20余额
contract["balanceOf"]!(account) // Account from previous steps
    .call()
    .done { outputs in
        print("Balance:", outputs["_balance"] as! BigUInt)
    }.catch { error in
        print("Error:", error)
    }
发送ERC20代币
// Our recipient
let recipient = try! Ethereum.Address(hex: "0x....", eip55: true)

// Creating ERC20 call object
let invocation = contract["transfer"]!(recipient, BigUInt(100000))

invocation
    .send(from: account) // Sending it from our account
    .done { hash in
        print("TX Hash:", hash.hex())
    }.catch { err in
        print("Error:", err)
    }

更多示例

更多示例请查看使用其中的Web3.swift库。

SDK 结构

此 SDK 具有模块化结构。所有模块都可以使用 CocoaPods 安装。

目前 SDK 拥有如下模块:

  • Tesseract.OpenWallet - 适用于 Swift 的 OpenWallet 引用客户端实现。SDK 主体部分
  • Tesseract.Ethereum - 元包,将安装所有 Ethereum 模块
  • Tesseract.Ethereum.PromiseKit - 元包,将安装所有支持 PromiseKit 的 Ethereum 模块

模块安装

可逐个安装模块。

例如,如果你只想安装 Web3,只需在你的 Podfile 中添加以下内容

pod 'TesseractSDK/Ethereum.Web3'

# Uncomment this line if you want to enable Web3 PromiseKit extensions
# pod 'TesseractSDK/Ethereum.Web3.PromiseKit'

然后运行 pod install

背后的理念

Tesseract dApps 平台源于一个简单的愿景——dApps 应当不在内部存储私钥。

基于这个愿景,我们创建了一个以移动端为先的平台。它允许应用开发者编写原生的 dApps,同时将所有密钥存储安全任务留给钱包开发者。

我们从一个开放的协议开始,该协议描述了钱包与 dApp 的通信。它被称为 Open Wallet

此 SDK 可以与实现了此协议的任何钱包交互。请让您的钱包实现它 :)

作者

许可协议

Tesseract.swift 在 Apache 2.0 许可协议下可用。更多信息请参阅 LICENSE 文件