Coder Social home page Coder Social logo

Comments (3)

HelpDesk-Ai avatar HelpDesk-Ai commented on June 10, 2024

Hello,If you use a Bitcoin address starting with 3 and transfer to an address starting with 1, how should you sign and broadcast? No need for multi-signature functionality.

How can I be of help

from bitcoinj.

RookieExaminer avatar RookieExaminer commented on June 10, 2024

Use the Bitcoin address 3MtvkwxXtjK99GTvRKCMrfcPfb1CKd4FR5 to transfer to 1EN8KQzVYaZXWx8cZtWjxJUCQgfanNRnuU, and finally get the signature 0200000001f8bd6b068efceba07871dd6618baca979bd791a19f44ab7e7afc17590ed0ba6301000000610047304402205ab2526d9b4cd8062ae0e33d8809adc423e7e099f89c8f8638120291a3e5592a02200340607fb2bc371d377ffde32076f771ac82dc18b8d6a2f3850162fb2b304bbd0117a914dda33200ed8fd8ae42b3b415b82450f11a1061f587ffffffff02e8030000000000001976a91492987a8ca45b556f3e7f99b5cf4060accede7b3088acb9ff06000000000017a914dda33200ed8fd8ae42b3b415b82450f11a1061f58700000000

Use this signature to broadcast and report an error {"result":null,"error":{"code":-26,"message":"mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false /empty top stack element)"},"id":null}

0200000001f8bd6b068efceba07871dd6618baca979bd791a19f44ab7e7afc17590ed0ba6301000000610047304402205ab2526d9b4cd8062ae0e33d8809adc423e7e099f89c8f8638120291a3e5592a02200340607fb2bc371d377ffde32076f771ac82dc18b8d6a2f3850162fb2b304bbd0117a914dda33200ed8fd8ae42b3b415b82450f11a1061f587ffffffff02e8030000000000001976a91492987a8ca45b556f3e7f99b5cf4060accede7b3088acb9ff06000000000017a914dda33200ed8fd8ae42b3b415b82450f11a1061f58700000000

decoderawtransaction:

 {
"result": {
    "txid": "c99da465be930f40a327c6cb130dbfafb7387da5dc29272072e7269779ecb668",
    "hash": "c99da465be930f40a327c6cb130dbfafb7387da5dc29272072e7269779ecb668",
    "version": 2,
    "size": 214,
    "vsize": 214,
    "weight": 856,
    "locktime": 0,
    "vin": [
        {
            "txid": "63bad00e5917fc7a7eab449fa191d79b97caba1866dd7178a0ebfc8e066bbdf8",
            "vout": 1,
            "scriptSig": {
                "asm": "0 304402205ab2526d9b4cd8062ae0e33d8809adc423e7e099f89c8f8638120291a3e5592a02200340607fb2bc371d377ffde32076f771ac82dc18b8d6a2f3850162fb2b304bbd[ALL] a914dda33200ed8fd8ae42b3b415b82450f11a1061f587",
                "hex": "0047304402205ab2526d9b4cd8062ae0e33d8809adc423e7e099f89c8f8638120291a3e5592a02200340607fb2bc371d377ffde32076f771ac82dc18b8d6a2f3850162fb2b304bbd0117a914dda33200ed8fd8ae42b3b415b82450f11a1061f587"
            },
            "sequence": 4294967295
        }
    ],
    "vout": [
        {
            "value": 0.00001,
            "n": 0,
            "scriptPubKey": {
                "asm": "OP_DUP OP_HASH160 92987a8ca45b556f3e7f99b5cf4060accede7b30 OP_EQUALVERIFY OP_CHECKSIG",
                "desc": "addr(1EN8KQzVYaZXWx8cZtWjxJUCQgfanNRnuU)#jsvs9hkm",
                "hex": "76a91492987a8ca45b556f3e7f99b5cf4060accede7b3088ac",
                "address": "1EN8KQzVYaZXWx8cZtWjxJUCQgfanNRnuU",
                "type": "pubkeyhash"
            }
        },
        {
            "value": 0.00458681,
            "n": 1,
            "scriptPubKey": {
                "asm": "OP_HASH160 dda33200ed8fd8ae42b3b415b82450f11a1061f5 OP_EQUAL",
                "desc": "addr(3MtvkwxXtjK99GTvRKCMrfcPfb1CKd4FR5)#pj9atq34",
                "hex": "a914dda33200ed8fd8ae42b3b415b82450f11a1061f587",
                "address": "3MtvkwxXtjK99GTvRKCMrfcPfb1CKd4FR5",
                "type": "scripthash"
            }
        }
    ]
},
"error": null,
"id": null

}

I don't know, exactly what went wrong

from bitcoinj.

RookieExaminer avatar RookieExaminer commented on June 10, 2024
       val params = MainNetParams.get()
        val context = Context(params)
        Context.propagate(context)
        val transaction = TransactionBit(params)
        transaction.setVersion(2)
        val fromAddress = Address.fromString(params, fromAddress)
        val toAddress: Address = Address.fromString(params, toAddress)
        val privateKey =
            ECKey.fromPrivate(Hex.decode(walletCoinBean!!.privateKeyAsHex))
        transaction.addOutput(inputAmount, toAddress)
        val utxoList: List<BtcUtxoBean.UnspentOutput>? = btcGetUtxoList()
        var gatheredAmount = Coin.ZERO
        val newUtxoList = mutableListOf<BtcUtxoBean.UnspentOutput>()
        for (i in utxoList!!.indices) {
            if (requiredAmount.isLessThan(Coin.valueOf(utxoList[i].value))) {
                newUtxoList.add(utxoList[i])
                break
            }
        }
        if (newUtxoList.size == 0) {
            for (i in utxoList!!.indices) {
                if (requiredAmount.isLessThan(gatheredAmount)) {
                    break
                }
                gatheredAmount = gatheredAmount.add(Coin.valueOf(utxoList[i].value))
                val outPoint = TransactionOutPoint(
                    params,
                    utxoList[i].tx_output_n!!.toLong(),
                    Sha256Hash.wrap(utxoList[i].tx_hash_big_endian)
                )
                val transactionInput = TransactionInput(
                    params, transaction, HEX.decode(utxoList[i].script!!),
                    outPoint, Coin.valueOf(utxoList[i].value)
                )
                transaction.addInput(transactionInput)
            }
        } else {
            for (i in newUtxoList!!.indices) {
                if (requiredAmount.isLessThan(gatheredAmount)) {
                    break
                }
                gatheredAmount = gatheredAmount.add(Coin.valueOf(newUtxoList[i].value))
                val outPoint = TransactionOutPoint(
                    params,
                    newUtxoList[i].tx_output_n!!.toLong(),
                    Sha256Hash.wrap(newUtxoList[i].tx_hash_big_endian)
                )
                val redeemScript = ScriptBuilder.createOutputScript(toAddress)
                val transactionInput = TransactionInput(
                    params, null, redeemScript.program,
                    outPoint, null
                )
                transaction.addInput(transactionInput)
            }
        }
        if (gatheredAmount.isGreaterThan(requiredAmount)) {
            val redeemScript = ScriptBuilder.createOutputScript(fromAddress)
            transaction.addOutput(gatheredAmount.minus(requiredAmount), redeemScript)
        }
        LogUtils.d(transaction.inputs.size)
        signInputsOfTransaction(fromAddress, transaction, privateKey, toAddress, params)
        transaction.verify()
        transaction.getConfidence().setSource(TransactionConfidence.Source.SELF)
        transaction.setPurpose(TransactionBit.Purpose.USER_PAYMENT)
        val valueToSend: String = HEX.encode(transaction.bitcoinSerialize())

sign

private fun signInputsOfTransaction(
    fromAddress: Address,
    transaction: TransactionBit,
    privateKey: ECKey,
    toAddress: Address,
    params: MainNetParams
) {
    for (i in transaction.inputs.indices) {
        val redeemScript = ScriptBuilder.createP2SHOutputScript(fromAddress.hash)
        LogUtils.d(HEX.encode(fromAddress.hash))
        val hash: Sha256Hash =
            transaction.hashForSignature(
                i,
                redeemScript,
                TransactionBit.SigHash.ALL,
                false
            )
        val signature: ECKey.ECDSASignature = privateKey.sign(hash)
        val txSignature = TransactionSignature(
            signature,
            TransactionBit.SigHash.ALL,
            false
        )
             val scriptSig = ScriptBuilder.createP2SHMultiSigInputScript(
                 mutableListOf(txSignature),
                 redeemScript
             )
        transaction.getInput(i.toLong()).scriptSig = scriptSig
    }

}

from bitcoinj.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.