如何在Swift 4中将比特币从一个钱包发送到另一个钱包

时间:2018-10-13 13:42:58

标签: ios swift swift4 bitcoin

我正在开发自定义比特币钱包,我能够通过使用Yenom / BitcoinKit生成公钥/私钥。我需要将BTC发送到我要使用BitcoinKit的另一个钱包。但是我无法发送Btc。我该怎么做呢?我们可以使用BitcoinKit以外的其他库将Btc从一个钱包发送到另一个电子钱包吗?

  

所以我如何在Swift中将BTC从一个钱包转移到另一个钱包   建议。

1 个答案:

答案 0 :(得分:0)

请检查以下链接

https://github.com/yenom/BitcoinKit/blob/master/Examples/Wallet/Wallet/SendViewController.swift

有一个名为sendToSomeAddress

的函数
private func sendToSomeAddress(_ amount: Int64) {
        let toAddress: Address = try! AddressFactory.create("bchtest:qpytf7xczxf2mxa3gd6s30rthpts0tmtgyw8ud2sy3")
        let changeAddress: Address = try! AppController.shared.wallet!.changeAddress()

        var utxos: [UnspentTransaction] = []
        for p in payments {
            let value = p.amount
            let lockScript = Script.buildPublicKeyHashOut(pubKeyHash: p.to.data)
            let txHash = Data(p.txid.reversed())
            let txIndex = UInt32(p.index)
            print(p.txid.hex, txIndex, lockScript.hex, value)

            let unspentOutput = TransactionOutput(value: value, lockingScript: lockScript)
            let unspentOutpoint = TransactionOutPoint(hash: txHash, index: txIndex)
            let utxo = UnspentTransaction(output: unspentOutput, outpoint: unspentOutpoint)
            utxos.append(utxo)
        }

        let unsignedTx = createUnsignedTx(toAddress: toAddress, amount: amount, changeAddress: changeAddress, utxos: utxos)
        let signedTx = signTx(unsignedTx: unsignedTx, keys: usedKeys())

        peerGroup?.sendTransaction(transaction: signedTx)
    }

我希望这会有所帮助

相关问题