BitcoinSwift 1.0.0

BitcoinSwift 1.0.0

Marcos-cmyk 维护。



  • 作者
  • Marcos-cmyk

BitcoinSwift

BitcoinSwift 是一个用于与 BTC 网络交互的 iOS 工具包。

language CocoaPods CocoaPods

有关更多具体信息,请参阅 示例

CocoaPods 安装

将此内容添加到您的 Podfile 并运行 pod install 以进行安装

pod 'BitcoinSwift', '~> 1.0.0'

Swift 包管理器

Swift 包管理器是一个用于自动分发 Swift 代码的工具,它集成到 Swift 编译器中。

一旦您设置了 Swift 包,添加 TronWeb 作为依赖项就如同将它添加到 Package.swift 的依赖项值一样简单。

dependencies: [
    .package(url: "https://github.com/Marcos-cmyk/Bitcoin.git", .upToNextMajor(from: "1.0.0"))
]

CocoaPods 中的示例用法

import BitcoinSwift   

Swift 包管理器中的示例用法

import BitcoinSwift   
设置 Bitcoin
let bitcoin = Bitcoin_V1()
if bitcoin.isSuccess {
            //To do...
} else {
    bitcoin.setup(showLog: true) { [weak self] _ in
        guard let self = self else { return }
        //To do...
    }
}
创建账户
bitcoin.createAccount() { [weak self] state, address, privateKey, mnemonic,error in
    guard let self = self else { return }
    self.createAccountBtn.isEnabled = true
    tipLabel.text = "create finished."
    if state {
        let text =
            "address: " + address + "\n\n" +
            "mnemonic: " + mnemonic + "\n\n" +
            "privateKey: " + privateKey
        accountDetailTextView.text = text
    } else {
        accountDetailTextView.text = error
    }
}
从助记符导入账户
guard let mnemonic = mnemonicTextView.text else{return}
bitcoin.importAccountFromMnemonic (mnemonic: mnemonic){ [weak self] state, address, privateKey, mnemonic, error in
    guard let self = self else { return }
    self.importAccountFromMnemonicBtn.isEnabled = true
    tipLabel.text = "import finished."
    if state {
        let text =
            "address: " + address + "\n\n" +
            "privateKey: " + privateKey + "\n\n" +
            "mnemonic: " + mnemonic
        walletDetailTextView.text = text
    } else {
        walletDetailTextView.text = error
    }
}
从私钥导入账户
guard let privateKey = privateKeyTextView.text else{return}
bitcoin.importAccountFromPrivateKey(privateKey: privateKey){ [weak self] state, address, privateKey,error in
    guard let self = self else { return }
    self.importAccountFromPrivateKeyBtn.isEnabled = true
    tipLabel.text = "import finished."
    if state {
        let text =
            "address: " + address + "\n\n" +
            "privateKey: " + privateKey
        walletDetailTextView.text = text
    } else {
        walletDetailTextView.text = error
    }
}
获取余额
bitcoin.getBTCBalance(address: address) { [weak self] state, balance,error in
    guard let self = self else { return }
    self.getBalanceBtn.isEnabled = true
    if state {
        let title = "BTC Balance: "
        self.balanceLabel.text = title + balance
    } else {
        self.balanceLabel.text = error
    }
}
估计 BTC 转账费用
// If you have n input addresses and m output addresses, then your inputCount is n, and your outputCount is m.
let inputCount = 1
let outputCount = 2
bitcoin.estimateBtcTransferFee(inputsCount: inputCount,outputsCount: outputCount){ [weak self] state, high,medium,low,error in
    guard let self = self else { return }
    print("Estimate fee finised.")
    if (state) {
        let highFormatted = String(format: "%.2f", high)
        let mediumFormatted = String(format: "%.2f", medium)
        let lowFormatted = String(format: "%.2f", low)
        self.estimatedFeetLabel.text = "Send BTC hava three estimated fee. \n high:\(highFormatted) Satoshis. \n medium:\(mediumFormatted) Satoshis. \n low:\(lowFormatted) Satoshis"
    } else {
        self.estimatedFeetLabel.text = error
    }
}
BTC 转账
// Support a Bitcoin address sending to multiple Bitcoin addresses simultaneously.
guard let privateKey = privateKeyTextView.text,
      let reviceAddress1 = reviceAddressField.text,
      let amount1 = amountTextField.text else { return }
var outputs = [[String: String]]()
let output1: [String: String] = ["address":reviceAddress1,"amount":amount1]
let output2: [String: String] = ["address":"address2","amount":"0.00001"]
outputs.append(output1)
outputs.append(output2)
let fee = 3000.0
bitcoin.transfer(privateKey: privateKey, outputs: outputs, fee: fee){ [weak self] state, hash,error in
    guard let self = self else { return }
    print("state = \(state)")
    print("hash = \(hash)")
    if (state) {
        self.hashLabel.text = hash
    } else {
        self.hashLabel.text = error
    }
}

有关更多具体信息,请参阅 示例

许可证

TronWeb 采用 MIT 许可发布。有关详细信息,请参阅 LICENSE