PHP Bitwasp,签署比特币现金交易

时间:2018-09-07 08:01:28

标签: php bitcoin signing

我尝试签署测试比特币现金交易,然后广播。 对于bitwasp版本0.0.35.0,代码为:

   $utxoOwnerPrivateKey = 'MyPrIvAtEKey';//public key is "16Dbmp13CqdLVwjXrd6amF48t7L8gYSGBj", note - the real private key is another
   $utxo = '5e44cdab9cb4a4f1871f2137ab568bf9ef2760e52816971fbaf0198f19e28378';
   $utxoAmount = 598558;
   $reciverPublicKey = '1EjCxux1FcohsBNGzY9KdF59Dz7MYHQyPN';
   $fee = 1000;

   $addressCreator = new \Btccom\BitcoinCash\Address\AddressCreator();

   $networkObject = \Btccom\BitcoinCash\Network\NetworkFactory::bitcoinCash();
   $keyPairInput = \BitWasp\Bitcoin\Key\PrivateKeyFactory::fromWif($utxoOwnerPrivateKey, null, $networkObject);


   $outpoint = new \BitWasp\Bitcoin\Transaction\OutPoint(\BitWasp\Buffertools\Buffer::hex($utxo, 32), 0);


   $transaction = \BitWasp\Bitcoin\Transaction\TransactionFactory::build()
       ->spendOutPoint($outpoint)
       ->payToAddress($utxoAmount - $fee, $addressCreator->fromString($reciverPublicKey, $networkObject) )
       ->get();

   echo "Unsigned transaction: " . $transaction->getHex() . '<BR><BR>';

   $signScript = \BitWasp\Bitcoin\Script\ScriptFactory::scriptPubKey()->payToPubKeyHash($keyPairInput->getPublicKey()->getPubKeyHash());
   $txOut = new \BitWasp\Bitcoin\Transaction\TransactionOutput($utxoAmount  - $fee, $signScript);

   $signer = new \BitWasp\Bitcoin\Transaction\Factory\Signer($transaction);

   $signatureChecker = \Btccom\BitcoinCash\Transaction\Factory\Checker\CheckerCreator::fromEcAdapter( \BitWasp\Bitcoin\Bitcoin::getEcAdapter() ); // for version 0.0.35
   $signer->setCheckerCreator( $signatureChecker ); // for version 0.0.35
   $input = $signer->input(0, $txOut);
   $signatureType = \Btccom\BitcoinCash\Transaction\SignatureHash\SigHash::ALL | \Btccom\BitcoinCash\Transaction\SignatureHash\SigHash::BITCOINCASH;
   $input->sign($keyPairInput, $signatureType);
   $signed = $signer->get();


   echo "Witness serialized transaction: " . $signed->getHex() . '<BR><BR>';
   echo "Base serialized transaction: " . $signed->getBaseSerialization()->getHex() . '<BR><BR>';
   echo "Script validation result: " . ($input->verify() ? "yes\n" : "no\n"). '<BR><BR>';

   die();

在这种情况下,我得到结果:

Script validation result: no

尝试broadcast the BCH transaction时出现错误:

An error occured:
16: mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation). Code:-26

我认为,这意味着签名是错误的。如果我们删除标志$signatureType(保留默认设置),则Script validation result将是yes,但是广播将出现错误:

16: mandatory-script-verify-flag-failed (Signature must use SIGHASH_FORKID). Code:-26

我认为,这意味着-就像在比特币网络中签名的交易一样,必须由比特币现金规则签名。也许我错了。但是比特币交易签名很好。 Bitwasp没有手册,如何签署比特币现金交易,我对bitwasp v.0.0.34.2具有相同的代码(不带插件“ btccom / bitwasp-bitcoin-bch-addon”,使用$signer->redeemBitcoinCash(true);函数)但是它给出相同的结果。

有趣的是,在比特币现金签名者的代码中,它接受内部变量amount并将其包含在哈希中:

$hasher = new V1Hasher($this->transaction, $this->amount);

但是,对于比特币而言,bitwasp不会收取这笔钱,大概是从交易中收取了一笔钱。

请帮助我签署交易,bitwasp仅包含比特币示例,而不包含bitcoin-cash。没有第三方软件,很难找到有关php altcoins签名的任何信息。问候。

1 个答案:

答案 0 :(得分:1)

此代码可使用PHP bitwasp v.0.0.35库签署比特币现金交易!

convert -size 256x256 gradient: -depth 2 grad_d2.jpg

  Depth: 8-bit
  Colors: 10
  Histogram:
     10752: (  0,  0,  0) #000000 gray(0)
       256: (  1,  1,  1) #010101 gray(1)
       512: ( 84, 84, 84) #545454 gray(84)
     20992: ( 85, 85, 85) #555555 gray(85)
       256: ( 86, 86, 86) #565656 gray(86)
       256: (169,169,169) #A9A9A9 gray(169)
     21248: (170,170,170) #AAAAAA gray(170)
       256: (171,171,171) #ABABAB gray(171)
       256: (254,254,254) #FEFEFE gray(254)
     10752: (255,255,255) #FFFFFF gray(255)


convert -size 256x256 gradient: -depth 2 grad_d2.png

  Depth: 8/2-bit
  Colors: 4
  Histogram:
     11008: (  0,  0,  0) #000000 gray(0)
     21760: ( 85, 85, 85) #555555 gray(85)
     21760: (170,170,170) #AAAAAA gray(170)
     11008: (255,255,255) #FFFFFF gray(255)

我已经广播了此PHP生成的测试事务,请参见link to tx。问题可能出在

    $unspendedTx = '49343e0a4ef29b819f87df1371c6f8eafa1f235074a27fb6aa5f4ab4c48e5c16';
    $utxOutputIndex = 0;
    $utxoPrivateKey = 'MyPrIvAtEKey';
    $utxoAmount = 300000;
    $reciverPublicKey = '1E8XaWNsCWyVaZaWTLh8uBdAZjLQqwWmzM';
    $fee = 1000;

    $networkObject = \Btccom\BitcoinCash\Network\NetworkFactory::bitcoinCash();

    $outpoint = new \BitWasp\Bitcoin\Transaction\OutPoint(\BitWasp\Buffertools\Buffer::hex($unspendedTx, 32), $utxOutputIndex /* index of utxo in transaction, generated it */);
    $destination = \BitWasp\Bitcoin\Script\ScriptFactory::scriptPubKey()->payToPubKeyHash( (new \Btccom\BitcoinCash\Address\AddressCreator())->fromString($reciverPublicKey)->getHash() );

    $transaction = \BitWasp\Bitcoin\Transaction\TransactionFactory::build()
        ->spendOutPoint($outpoint)
        ->output($utxoAmount - $fee, $destination)
        ->get();
    echo "Unsigned transaction: " . $transaction->getHex() . '<BR><BR>';


    $keyPairInput = \BitWasp\Bitcoin\Key\PrivateKeyFactory::fromWif($utxoPrivateKey, null, $networkObject);
    $txOut = new \BitWasp\Bitcoin\Transaction\TransactionOutput($utxoAmount, \BitWasp\Bitcoin\Script\ScriptFactory::scriptPubKey()->payToPubKeyHash(  $keyPairInput->getPubKeyHash() ) );
    $signer = new \BitWasp\Bitcoin\Transaction\Factory\Signer($transaction);

    $signatureChecker = \Btccom\BitcoinCash\Transaction\Factory\Checker\CheckerCreator::fromEcAdapter( \BitWasp\Bitcoin\Bitcoin::getEcAdapter() ); // for version 0.0.35
    $signer->setCheckerCreator( $signatureChecker ); // for version 0.0.35

    $input = $signer->input(0, $txOut);
    $signatureType = \Btccom\BitcoinCash\Transaction\SignatureHash\SigHash::ALL | \Btccom\BitcoinCash\Transaction\SignatureHash\SigHash::BITCOINCASH;
    $input->sign($keyPairInput, $signatureType);
    $signed = $signer->get();


    echo "Transaction: " . $signed->getHex() . '<BR><BR>';
    die();

也许->payToAddress($utxoAmount - $fee, $addressCreator->fromString($reciverPublicKey, $networkObject) ) 有一个错误,因为“手动”脚本的创建带来了另一笔交易。第二:

payToAddress

我们必须免费创建tx输入(以前的输出)。

使用的库( composer.json ):

$txOut = new \BitWasp\Bitcoin\Transaction\TransactionOutput($utxoAmount  - $fee, $signScript);

希望,这将对PHP上的所有altcoin api创建者有所帮助。