Bit-Wasp / bitcoin-php

Bitcoin implementation in PHP
The Unlicense
1.04k stars 416 forks source link

How to transfer BTC from a P2SH address? #895

Open cqtop opened 2 years ago

cqtop commented 2 years ago

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/
tcolonel commented 7 months ago

Hello @cqtop.

Did you findout a solution?