trustwallet / wallet-core

Cross-platform, cross-blockchain wallet library.
https://developer.trustwallet.com/wallet-core
Apache License 2.0
2.86k stars 1.6k forks source link

How to get the utxotxid of BTC transfer? #2534

Closed developerjet closed 2 years ago

developerjet commented 2 years ago

How to get the utxotxid of BTC transfer?If I can't get the utxotxid I'm fighting for, the signature will fail.

  guard let coin = coinModel else { return }
        guard let myWallet = STWalletInfoManager.shared.currentWallet else { return }
        guard let prvKeyData = Data(hexString: myWallet.privateKey) else { return }

        let fromAddress = coin.address
        let targetAddress = toAddress
        let amount = sendAmount.multiply(amountRate).preciseDecimal(p: 18, isRoundingDown: false)

        //let utxoTxId = Hash.sha256(data: Data(hex: blockRaw.hash160))
        let utxoTxId = Data(hex: "....")
        let script = BitcoinScript.lockScriptForAddress(address: fromAddress, coin: .bitcoin).data

        let utxos = [
            BitcoinUnspentTransaction.with {
                $0.outPoint.hash = Data(utxoTxId.reversed())
                $0.outPoint.index = 0
                $0.outPoint.sequence = UINT32_MAX
                $0.script = script
                $0.amount = Int64(coin.amount)! * 100000000 //余额
            }
        ]

        let input = BitcoinSigningInput.with {
            //$0.hashType = BitcoinScript.hashTypeForCoin(coinType: .bitcoin)
            $0.hashType = BitcoinSigHashType.all.rawValue | BitcoinSigHashType.fork.rawValue
            $0.amount = Int64(amount.intValue)
            $0.byteFee = 1
            $0.toAddress = toAddress
            $0.changeAddress = fromAddress
            $0.privateKey = [prvKeyData]
            $0.utxo = utxos
        }

        let output : BitcoinSigningOutput = AnySigner.sign(input: input, coin: .bitcoin)
        let hexString = output.encoded.hexString
hewigovens commented 2 years ago

Take a look at https://developer.trustwallet.com/wallet-core/integration-guide/wallet-core-usage#bitcoin-transaction-signing, it mentions blockbook api https://github.com/trezor/blockbook/blob/master/docs/api.md#get-utxo

developerjet commented 2 years ago

Take a look at https://developer.trustwallet.com/wallet-core/integration-guide/wallet-core-usage#bitcoin-transaction-signing, it mentions blockbook api https://github.com/trezor/blockbook/blob/master/docs/api.md#get-utxo

OK, thank you.

developerjet commented 2 years ago

Take a look at https://developer.trustwallet.com/wallet-core/integration-guide/wallet-core-usage#bitcoin-transaction-signing, it mentions blockbook api https://github.com/trezor/blockbook/blob/master/docs/api.md#get-utxo

OK, thank you.

Hello, how to generate the wallet address of BTC test network.