Skip to content

How to transfer BTC from a P2SH address? #895

Open

Description

@cqtop

Hello,
I want to transfer btc from p2sh address to another p2sh address.
If there is only one uxto, the following code will work normally,
But if there are multiple uxto, blockcypher node will return an error "error validating transaction: error running script for input 0 referencing 31caa7f45b886e7206c4eff08e34cd0a9713af33c003e786b8decbca50c9492a at 0: script was not verified successfully.."
Please help me, thank you very much

        $privKeyFactory = new PrivateKeyFactory();
        $privateKey = $privKeyFactory->fromWif($from_addr_wif);
        $prog = WitnessProgram::V0($privateKey->getPublicKey()->getPubKeyHash());
        $redeemScript = new P2shScript($prog->getScript());

        $spendFromAddress = $redeemScript->getAddress();
        $addressCreator = new AddressCreator();
        $sendToAddress = $addressCreator->fromString($to_addr);

        $transaction = TransactionFactory::build();
        foreach($utxo_hash_list as $v){
            $transaction->input($v['txid'], (int)$v['n']);
        }
        $real_number_sato = $real_number * pow(10,8);
        $transaction->payToAddress($real_number_sato, $sendToAddress);

        $number_satoshi = $number * pow(10,8); 
        $txOut = new TransactionOutput($number_satoshi, $redeemScript->getOutputScript());

        $signData = (new SignData())->p2sh($redeemScript);
        $signer = new Signer($transaction->get());

        $input = $signer->input(0, $txOut, $signData);
        $input->sign($privateKey);

        $signed = $signer->get();
        $raw_data = $signed->getHex();

        //then broadcast   https://api.blockcypher.com/

Activity

  1. reopened this on Aug 3, 2022
  2. tcolonel commented on Jan 22, 2024

    @tcolonel

    Hello @cqtop.

    Did you findout a solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions