Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to transfer BTC from a P2SH address? #895

Open
cqtop opened this issue Mar 22, 2022 · 1 comment
Open

How to transfer BTC from a P2SH address? #895

cqtop opened this issue Mar 22, 2022 · 1 comment

Comments

@cqtop
Copy link

cqtop commented Mar 22, 2022

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
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants