Bit-Wasp / bitcoin-php

Bitcoin implementation in PHP
The Unlicense
1.05k stars 419 forks source link

Error validating transaction: witness script detected in tx without witness data. #807

Open maksus opened 5 years ago

maksus commented 5 years ago

Hello! Cannot figure it out for 37 sleepless hours. May be misunderstood the signer logic or Blockchain specific.. Tried to make the code as simple as possible.

     ` $privKeyFactory = new PrivateKeyFactory();
        $privateKey = $privKeyFactory->fromWif("p2pkh:PRIVATE_KEY");

         $program = ScriptFactory::scriptPubKey()->payToPubKeyHash($privateKey->getPubKeyHash());
         $txOut = new TransactionOutput(100000, $program);

  $addressCreator = new AddressCreator();
  $transaction = TransactionFactory::build()
->input('76a44ad2ed45c37a03fb5ca77f6a842152d85a46fbe6d371324742011031968a', 0)
->payToAddress(90000, $addressCreator->fromString('181ahqXXRpEyQ7Qsav7p7DrWF7t19LTSpv'))
->get();

   $signer = new Signer($transaction);
   $signer -> sign (0, $privateKey, $txOut);
   $signed = $signer->get();

` RESULT:

01000000018a9631100142473271d3e6fb465ad85221846a7fa75cfb037ac345edd24aa476000000008b48304502210090422d73bb845c8767d32f775655748515e88779ce8ad38a0084d83e21613955022008eb2dd80bc345be83854aa63104d29c7248576ab2bd133fe60ec45424fb3132014104846c8b900fd4783a0a81aea7f32619d120628d3b3ea44083832bcc772f275da6963a8872fcf633c13a296099f2d43ad1598bf48b89c94577fd2f44395d92a2c9ffffffff01905f0100000000001976a9144ce503e67f53bc558c935919e33761eeb7f1defb88ac00000000

Trying to push this gives me:

Error validating transaction: witness script detected in tx without witness data.

PS The library is super-cool and extremely useful.. Thanks!

afk11 commented 5 years ago

Cannot figure it out for 37 sleepless hours.

Oh wow! You should have posted an issue sooner :)

The 0'th output of the input tx 76a44ad2ed45c37a03fb5ca77f6a842152d85a46fbe6d371324742011031968a is a P2WPKH (segwit) output. The code you posted shows you telling the signer that the script is P2PKH (legacy)

Error validating transaction: witness script detected in tx without witness data.

This is explainable - the network expects spends of that output to come with witness data, which isn't provided in your transaction.

I see you were reusing an address for those inputs - is that the 'test address'? Because my guess is maybe you meant to spend output 1 (the same address), instead of output 0 (some bech32 address).

remove:->input('76a44ad2ed45c37a03fb5ca77f6a842152d85a46fbe6d371324742011031968a', 0)
substitute:->input('76a44ad2ed45c37a03fb5ca77f6a842152d85a46fbe6d371324742011031968a', 1)

PS The library is super-cool and extremely useful.. Thanks!

Thanks :)

maksus commented 5 years ago

Yes - this is TEST ADDRESS. You saved me from going crazy about this problem... So this number in

"input('hash', 'NUMBER')"

means the id of the UTXO?

Thanks once again. PS didn't notice in the description the way I can express my sincere gratitude in crypto... )

afk11 commented 5 years ago

So this number in "input('hash', 'NUMBER')" means the id of the UTXO?

Yes - it's the id of the output of transaction identified by 'hash'. And when you're signing, you provide the full transaction output (value, and scriptPubKey) for $hash, $id

Sure, I got this one! bc1qp8ys9s3tvrjdv40m26tg0r2rlwtg6yeadqu56z

Cheers!

Lutnerd commented 2 years ago

Good this transaction is giving me this ,Error validating transaction: witness script detected in tx without witness data.