Author

Topic: How to create and sign a true rawtransaction ? Send rawtransaction hex code err. (Read 1175 times)

newbie
Activity: 4
Merit: 2
Maybe you are spending more than the input provides?

What exactly is the error you are getting?

No, The spending is less than the input provides. so i add the from address to outputs to give change.

send generated raw transaction hex code on https://blockexplorer.com/tx/send will be successfully.

but at https://chain.so/, https://blockcypher.com, http://coinprism.com/ will be unsuccessfully! WTF..

I just sent an signed rawtransaction on https://blockexplorer.com/tx/send, it's successfully. on http://coinprism.com/ got an error and the error message is "Error".. -_-!

Very strange phenomenon, some times, it's success. some times it's unsuccessfully.  The library have any errors ?

The sample signed rawtransaction hex code:
Code:
0100000001d4b8d474abae03f6b0e565a0b6bc5091d31534a222582176ce71d56bcb691373020000008b4830450221008d1c2450cc4e0ad3dd83e6e2b5932f13fc836d1f27356da41c802fd84e3c800802207b1f14170bbb3b2e14f550181209b0686aa5caf63b90dff73227e55d0764bc570141048a8f8b00f3b73fa5cc6c1d4db0d9bccdd1a7f482e7b4402bc573cdd3b31962f11faaeb77df0b04b5569e24ce0fea2e3e193f5665b9d0437b5b2b69176375f7fdffffffff03379a0000000000001976a9149d108663348b241aa709a3e4c5587d9e98ab669f88acc5040000000000001976a9141c2218349eec9b483ce7ca9d2f5269fd040f885a88aca4b50000000000001976a91416e2f94ff158c985cd3c37f6a648190ba3977cf188ac00000000

coinprism.com and another platform is failed.
blockexplorer.com successfully.

coinprism.com debug url: http://docs.coinprism.apiary.io/#reference/transaction-signing-and-broadcasting/push-a-signed-raw-transaction-to-the-network/post?console=1
staff
Activity: 3458
Merit: 6793
Just writing some code
Maybe you are spending more than the input provides?

What exactly is the error you are getting?
newbie
Activity: 4
Merit: 2
I used open source bitcoin-php library to create a rawtransaction hex code and sign it. When send it to bitcoin network, get an error

Quote
An error occured: Transaction rejected by network (code -26). Reason: 16: mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element)

Library: https://github.com/Bit-Wasp/bitcoin-php

Code:

Code:
$private_key = PrivateKeyFactory::fromWif('address1 wif private key');


try {
    $transaction = TransactionFactory::build()
    ->input('unspent hash of address1','2')// unspent hash value : 147900, vout : 2
    ->payToAddress('39479',AddressFactory::fromString('address2'))
    ->payToAddress('97200',AddressFactory::fromString('address1'))// Give change
    ->payToAddress('1221',AddressFactory::fromString('address3'))
    ->get();

    $hex = $transaction->getHex();

    $myTx = TransactionFactory::fromHex($hex);


    $hex = TransactionFactory::sign($transaction)
      -> sign (0, $private_key, $myTx->getOutputs()->get(2)->getScript())
      -> get() -> getHex();

    echo ($hex);
} catch (Exception $e) {
    echo $e->getMessage().PHP_EOL;
}

Issue: https://github.com/Bit-Wasp/bitcoin-php/issues/166

The author have another library bitcoin-lib-php , Still have the same error
You need to get the output from the previous transaction. What you have is that you are getting the output script for signing from one of your outputs in the transaction. It actually has to come from the transaction which the input references, and it needs to be exactly the same output.


Use another library https://github.com/Bit-Wasp/bitcoin-lib-php

Sample code:

Code:
function createTransaction(Array $inputs,Array $privs,Array $outputs) {
        try {

            foreach ($outputs as $address=>$amount) {
                if (!is_int($amount)) {
                    $amount = is_numeric($amount) ? (int)$amount: (int)BitcoinLib::toSatoshi($amount);
                    $outputs[$address] = $amount;
                }
            }

            $wallet = array();
            RawTransaction::private_keys_to_wallet($wallet, $privs, '00');

            $raw_transaction = RawTransaction::create($inputs, $outputs);

            $sign = RawTransaction::sign($wallet, $raw_transaction, json_encode($inputs));


            return $sign['hex'];
        } catch(Exception $e) {
            self::$error = $e->getMessage();
            return false;
        }
}
Code:
$hex = createTransaction([
    [
        'txid'=>'address1 unspent hash', // use public http api https://chain.so/ to get unspet transaction hashs of an address
        'vout'=>2,
        'scriptPubKey'=>'address1 pubkey',
    ]
],['address1_wif_private_key],[
    'address2'=>'amount',
    'address3' =>'amount',
    'address1' => 'amount', //give change
]);

// error
Code:
$hex = createTransaction([
    [
        'txid'=>'address1 unspent hash',
        'vout'=>2,
        'scriptPubKey'=>'address1 pubkey',
    ]
],['address1_wif_private_key],[
    'address2'=>'amount',
    'address1' => 'amount', //give change
]);

// Success!!! why !?
staff
Activity: 3458
Merit: 6793
Just writing some code
I used open source bitcoin-php library to create a rawtransaction hex code and sign it. When send it to bitcoin network, get an error

Quote
An error occured: Transaction rejected by network (code -26). Reason: 16: mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element)

Library: https://github.com/Bit-Wasp/bitcoin-php

Code:

Code:
$private_key = PrivateKeyFactory::fromWif('address1 wif private key');


try {
    $transaction = TransactionFactory::build()
    ->input('unspent hash of address1','2')// unspent hash value : 147900, vout : 2
    ->payToAddress('39479',AddressFactory::fromString('address2'))
    ->payToAddress('97200',AddressFactory::fromString('address1'))// Give change
    ->payToAddress('1221',AddressFactory::fromString('address3'))
    ->get();

    $hex = $transaction->getHex();

    $myTx = TransactionFactory::fromHex($hex);


    $hex = TransactionFactory::sign($transaction)
      -> sign (0, $private_key, $myTx->getOutputs()->get(2)->getScript())
      -> get() -> getHex();

    echo ($hex);
} catch (Exception $e) {
    echo $e->getMessage().PHP_EOL;
}

Issue: https://github.com/Bit-Wasp/bitcoin-php/issues/166

The author have another library bitcoin-lib-php , Still have the same error
You need to get the output from the previous transaction. What you have is that you are getting the output script for signing from one of your outputs in the transaction. It actually has to come from the transaction which the input references, and it needs to be exactly the same output.
newbie
Activity: 4
Merit: 2
I used open source bitcoin-php library to create a rawtransaction hex code and sign it. When send it to bitcoin network, get an error

Quote
An error occured: Transaction rejected by network (code -26). Reason: 16: mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element)

Library: https://github.com/Bit-Wasp/bitcoin-php

Code:

Code:
$private_key = PrivateKeyFactory::fromWif('address1 wif private key');


try {
    $transaction = TransactionFactory::build()
    ->input('unspent hash of address1','2')// unspent hash value : 147900, vout : 2
    ->payToAddress('39479',AddressFactory::fromString('address2'))
    ->payToAddress('97200',AddressFactory::fromString('address1'))// Give change
    ->payToAddress('1221',AddressFactory::fromString('address3'))
    ->get();

    $hex = $transaction->getHex();

    $myTx = TransactionFactory::fromHex($hex);


    $hex = TransactionFactory::sign($transaction)
      -> sign (0, $private_key, $myTx->getOutputs()->get(2)->getScript())
      -> get() -> getHex();

    echo ($hex);
} catch (Exception $e) {
    echo $e->getMessage().PHP_EOL;
}

Issue: https://github.com/Bit-Wasp/bitcoin-php/issues/166

The author have another library bitcoin-lib-php , Still have the same error
Jump to: