TomoWalletCore 0.0.6

TomoWalletCore 0.0.6

candangios 维护。



 
依赖项
BigInt>= 0
CryptoSwift>= 0
TrezorCrypto~> 0.0.8
Moya>= 0
PromiseKit>= 0
SwiftProtobuf>= 0
KeychainSwift>= 0
 

  • 作者
  • CanDang

TomoWalletCore

TomoWallet-coreTomoWallet 的重要组成部分。它包含帮助您更轻松、简单地与 TomoChain 交互的功能。在下一个版本中,我们将提供内置屏幕以创建钱包、通过私钥/恢复短语导入钱包、备份钱包、发送资金和确认交易的界面。

TomoWallet-core 支持 iOS,配备 CocoaPods 和 macOS。

功能

  • 创建钱包。
  • 管理钱包。
  • 导入钱包。
  • 导出钱包(助记词、私钥)
  • 制作交易。
  • 签名并发送交易。
  • 获取余额。
  • 获取代币信息。

安装

CocoaPods 中的 TomoWalletCore 可通过 CocoaPods 使用。要安装,

请将以下行添加到您的 Podfile 中

pod 'TomoWalletCore'

下步:从项目根目录打开命令行终端,输入以下命令:pod install

安装完成后,您可以在 .swift 文件中导入 TomoWalletCore。

import TomoWalletCore

依赖项

如何使用

与TomoWalletCore交互TomoChain,您可以连接到 主网测试网。所有可用方法的基类是WalletCore。

let walletCore = WalletCore(network: .Mainnet)
WalletCore提供的方法
  • CreateWallet
  • ImportWallet
TomoWallet提供的方法
  • 获取Tomo余额。
  • 获取代币余额。
  • 获取代币信息。
  • 交易。
  • 发送或签名交易。

创建您的钱包

参数:无 返回值tomoWallet

let walletCore = WalletCore(network: .Mainnet)
walletCore.createWallet { (result) in
    switch result{
    case .success(let wallet):
        //success
        // do something.
        print(wallet.getAddress())
    case .failure(let error):
        print(error)
    }
}

使用私钥导入钱包

参数:"745044ccdb778fb6d2d999c561f4329deb57ee3628672d7a2954a53e20b167e" 返回值tomoWallet

walletCore.importWallet(hexPrivateKey: "Input your private key here") { (result) in
    switch result{
    case .success(let wallet):
        //success
        //do somwthing.
        print(wallet.getAddress())
    case .failure(let error):
        print(error)
    }
}

使用助记词导入钱包

参数:"普通胸 giant 愚蠢 van 否认 twin 音乐 曲线 提供阳伞 地点" 返回值tomoWallet

walletCore.importWallet(recoveryPhase: "Input your mnemonic here") { (result) in
    switch result{
    case .success(let wallet):
        //success
        // tomoWallet
        print(wallet.getAddress())
    case .failure(let error):
        print(error)
    }
}

通过地址导入钱包(只读)

参数: “0x36d0701257ab74000588e6bdaff014583e03775b “ 返回: tomoWallet

walletCore.importAddressOnly(address: "Input your address here") { (result) in
    switch result{
    case .success(let wallet):
        //success
        // do something
        print(wallet.getAddress())
    case .failure(let error):
        print(error)
    }
}

注意:如果以只读方式导入地址,则无法使用此钱包创建或签署交易。

管理钱包

TomoWalletCore支持多钱包,您可以创建一个或多个钱包。

TomoWallet有3种类型

  • HDWallet: 是通过createWallet创建或通过recoveryPhase恢复的导入钱包
  • 私钥:是通过私钥导入的钱包
  • 只读地址:是通过只读地址导入的钱包
switch wallet.walletType(){
case .AddressOnly:
    print("AddressOnly")
case .Privatekey:
    print("PrivateKey")
case .HDWallet:
    print("HDWallet")
}

列出所有钱包

列出您已创建和导入的所有钱包 参数: 无 返回 : [tomoWallet]

let wallets = walletCore.getAllWallets()
print(wallets.count)

获取已创建或导入的钱包

参数: 地址 返回: tomoWallet

walletCore.getWallet(address: "Input your Address") { (result) in
    switch result{
    case .success(let wallet):
        // success
        // do some thing
        print(wallet)
    case .failure(let error):
        print( error)
    }
}

获取余额

首先,必须导入PromiseKit框架。Promises简化了异步编程。https://github.com/mxcl/PromiseKit

import PromiseKit
 // get tomo babance
  firstly {
            wallet.getTomoBabance()
        }.done { (balance) in
            // String balance tomo
            // do something
            print(balance)
        }.catch { (let error) in
            print("error")
        }

获取TokenInfo 参数: 合约地址 返回 : tokenObject

   firstly {
            wallet.getTokenInfo(contract: " input contract address here")
        }.done { (token) in
            // return tokenobject
            print(token)
        }.catch { (let error) in
            print(error)
        }

获取代币余额

参数:tokenObject

返回值:babance

firstly {
            wallet.getTokenBalance(token: tokenObject)
        }.done { (tokenBalance) in
            print(tokenBalance)
        }.catch { (let error) in
            print(error)
        }

执行发送Tomo交易

参数:

  • 地址
  • Amount Tomo

返回值 : signTransaction

 firstly{
            tomoWallet.makeTomoTransaction(toAddress: "0x9f6b8fDD3733B099A91B6D70CDC7963ebBbd2684 ", amount: "1.0")
        }.done { (signTransaction) in
            print(signTransaction)
        }.catch { (error) in
            print(error)
        }

发送Tomo交易

参数:

  • 接收地址
  • Tomo数量

返回值 : sentTransaction

 firstly{
            wallet.sendTomo(toAddress: "0x9f6b8fDD3733B099A91B6D70CDC7963ebBbd2684", amount: "1.0")
        }.done { (sentTransaction) in
            print(sentTransaction)
        }.catch { (error) in
            print( error)
        }

执行发送代币交易

参数:

  • TokenTRC
  • 地址
  • Amount Tomo

返回值 : signTransaction

 firstly{
            tomoWallet.makeTokenTransaction(token: <tokenTRCObject> ,toAddress: "0x9f6b8fDD3733B099A91B6D70CDC7963ebBbd2684 ", amount: "1.0")
        }.done { (signTransaction) in
            print(signTransaction)
        }.catch { (error) in
            print(error)
        }  

发送代币交易

参数:

  • 合约地址
  • 地址
  • Tomo数量

返回值 : sentTransaction

 firstly{
            wallet.sendToken(contract: "0x3005111b37f476c56480672cebacc0031c95d32f ", toAddress: "0x9f6b8fDD3733B099A91B6D70CDC7963ebBbd2684", amount: "2.0")
        }.done { (sentTransaction) in
            print(sentTransaction)
        }.catch { (error) in
            print( error)
        }

许可证

MIT