wallet-core icon indicating copy to clipboard operation
wallet-core copied to clipboard

How to Build and Sign a BTC Transaction Type 2 Using Wallet Core?

Open ronaldoguedess opened this issue 1 year ago • 1 comments

When I build and sign a BTC transaction, it defaults to type 1. Is it possible to build and sign a type 2 BTC transaction using Wallet Core? image

This is how I'm building the tx, how can I convert it to type 2? Btw, is there any advantage to using transaction type 2?

      //BUILD TX
            val scriptUTXO = BitcoinScript.lockScriptForAddress(addressFrom, CoinType.BITCOIN)
            val scriptHash = scriptUTXO.matchPayToWitnessPublicKeyHash()
            val redeemScriptHash = scriptHash.toHexString()
            val redeemScript = BitcoinScript.lockScriptForAddress(addressFrom, CoinType.BITCOIN).data()

            //INPUT
            val input = Bitcoin.SigningInput.newBuilder().apply {
                this.amount = amount       //1962973
                if (maxAmount) {
                    this.useMaxAmount = true //auto-sweep
                }
                this.hashType = BitcoinSigHashType.ALL.value()
                this.toAddress = addressToTss //Receiver ADDRESS (TO) MUST BE TSS
                this.changeAddress = changeAddressLeftOver.ifEmpty { addressFrom } //LEFT OVER
                this.byteFee = byteFee.toLong()
                this.coinType = CoinType.BITCOIN.value()
                this.fixedDustThreshold = 1000 // or 546 for P2PKH, P2WPKH transfers
                this.dustPolicyCase
                //this.disableDustFilter = true
                this.addPrivateKey(ByteString.copyFrom(hexPrivateKey.hexStringToByteArray()))  //HEX
            }
            input.putScripts(redeemScriptHash, ByteString.copyFrom(redeemScript))

ronaldoguedess avatar Jun 01 '24 14:06 ronaldoguedess

Hi @ronaldoguedess, at this moment we don't allow to specify version 1, but it will be possible in a few weeks when Bitcoin V2 refactoring comes: https://github.com/trustwallet/wallet-core/pull/3848

satoshiotomakan avatar Jun 03 '24 07:06 satoshiotomakan

Hi @ronaldoguedess, sorry for the late update. You can now use BitcoinV2 protocol (please take a look at the unit tests as examples) https://github.com/trustwallet/wallet-core/pull/3848

satoshiotomakan avatar Nov 26 '24 10:11 satoshiotomakan