Move Bitcoin OP_RETURN to 2nd Output Instead of Third #3842
Answered
by
satoshiotomakan
ronaldoguedess
asked this question in
Q&A
-
Is it possible for me to move the OP_RETURN to the second output instead? In the linked transaction, it's currently in the last output, but I need to comply with a requirement that specifies it should be sent in the second output. Wrong: This is how the below code is sending..Right: This is how I need to send it... (OP_RETURN in the second output)//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.addPrivateKey(ByteString.copyFrom(hexPrivateKey.hexStringToByteArray())) //HEX
}
input.putScripts(redeemScriptHash, ByteString.copyFrom(redeemScript))
// OP_RETURN OUTPUT
val opReturnData = "D6037B019c86c912963CBAe893Db3f8DB1a471B7025F0b1a82749cb4E2278EC87F8BF6B618dC71a8bffE26BF9Fe82e05d011f9B2B4Fc6fC9DB6F4d62E400".toHexBytes()
input.outputOpReturn = ByteString.copyFrom(opReturnData)
//UTXO
for (buff in unspentUtxos.items) {
val txHash = buff.txHash.toHexBytes()
txHash.reverse()
val outPoint = Bitcoin.OutPoint.newBuilder().apply {
this.hash = ByteString.copyFrom(txHash)
this.index = buff.txOutput
if (transferRbf){
this.sequence = 0 //RBF ENABLED
}else{
this.sequence = Long.MAX_VALUE.toInt() //RBF DISABLED
}
}.build()
val utxo = Bitcoin.UnspentTransaction.newBuilder().apply {
this.amount = buff.value.toBigDecimal().toLong()
this.outPoint = outPoint
this.script = ByteString.copyFrom(scriptUTXO.data())
}.build()
input.addUtxo(utxo);
}
//PLAN
val plan = AnySigner.plan(input.build(), CoinType.BITCOIN, Bitcoin.TransactionPlan.parser())
Log.e("xxxxxxxxxxxxx", "Planned fee: ${plan.fee} amount: ${plan.amount} avail_amount: ${plan.availableAmount} change: ${plan.change}")
input.plan = plan
input.amount = plan.amount
//SIGN TX
val output = AnySigner.sign(input.build(), CoinType.BITCOIN, Bitcoin.SigningOutput.parser())
val encoded = output.encoded
val transactionSizeBytes = encoded.toByteArray().size
val signedTransaction = Numeric.toHexString(encoded.toByteArray()).substring(2)
assert(output.error == Common.SigningError.OK) |
Beta Was this translation helpful? Give feedback.
Answered by
satoshiotomakan
May 15, 2024
Replies: 2 comments 5 replies
-
Hi @ronaldoguedess, this is exactly the feature we recently added to WalletCore. |
Beta Was this translation helpful? Give feedback.
5 replies
-
Thank you Timothy P. KilgoreOn May 15, 2024, at 10:20, satoshiotomakan ***@***.***> wrote:
Should work with:
input.outputOpReturnIndex = Bitcoin.OutputIndex.newBuilder().apply {
this.index = 1
}
.build()
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should work with: