Bitcoin Forum

Economy => Service Discussion => Topic started by: DrSeuss on January 23, 2017, 12:19:32 PM



Title: How to correctly push transaction?
Post by: DrSeuss on January 23, 2017, 12:19:32 PM
What's up! ;)

I'm currently coding a Bitcoin-Transaction-System on my Website. (via bitcoinjs-lib)
Now I'm kinda stuck with pushing the transaction.
Can anyone tell me what exactly I have to push?
It's weird because I generate a Hex which works fine if I push it manually via http://tbtc.blockr.io/documentation/api
But as soon as I try to push via code the whole thing just dies.

Code:
Failed to push transaction. -> {"status":"error","data":"TX is missing","code":400,"message":"Please see API documentation on how to POST tx hex as json."}

Code:
$.ajax({
                            url: "http://btc.blockr.io/api/v1/tx/decode",
                            type: "post",
                            data: {
                            hex: transactionHex
                            },
                            success: function(response) {
                                decodedHex = response;
                                console.log(decodedHex);
                                document.getElementById('baseDiv').innerHTML += "<br /><br />Decoded Hex: " + transactionHex;
                            },
                            error: function(xhr) {
                                console.log("Failed to decode Hex. -> " + xhr);
                            }
                        });

                        $.ajax({
                            url: "http://btc.blockr.io/api/v1/tx/push",
                            type: "post",
                            data: {
                            hex: decodedHex
                            },
                            success: function(response) {
                                console.log("Transaction pushed. " + response);
                            },
                            error: function(xhr) {
                                console.log("Failed to push transaction. -> " + xhr.responseText);
                            }
                        });

Can anyone give me a few pointers what could be the problem? :)
Thanks so much! <3
Cheerz~


Title: Re: How to correctly push transaction?
Post by: DrSeuss on January 23, 2017, 01:46:57 PM
I just tried pushing it manually and actually  got an error, maybe this helps:

PUSH TRANSACTION ERROR: 16: MANDATORY-SCRIPT-VERIFY-FLAG-FAILED (SCRIPT FAILED AN OP_EQUALVERIFY OPERATION)



Title: Re: How to correctly push transaction?
Post by: DrSeuss on January 23, 2017, 02:37:03 PM
I just tried pushing it manually and actually  got an error, maybe this helps:

PUSH TRANSACTION ERROR: 16: MANDATORY-SCRIPT-VERIFY-FLAG-FAILED (SCRIPT FAILED AN OP_EQUALVERIFY OPERATION)



I found out that I had something mixed up, but sadly I still can't push

Failed to Push: {"status":"error","data":"TX is missing","code":400,"message":"Please see API documentation on how to POST tx hex as json."}

Does anyone know why this happens?
I can push manually but not via code, this is driving me insane. ;(


Title: Re: How to correctly push transaction?
Post by: DrSeuss on January 23, 2017, 03:00:24 PM
This is how I post my request to the API

Code:

   "hex":{ 
      "txid":"3fc25ec159a031c1c8b643a9ef083dbf4011f91b2eff6952b59fa239dccfba94",
      "size":226,
      "version":1,
      "locktime":0,
      "vin":[ 
         { 
            "txid":"97cf8b040250cee0b30ea61bd2dbbc2425a4b50231477bd8e459ada9e2a98ab2",
            "vout":1,
            "scriptSig":{ 
               "asm":"3045022100c7d4fcfa0b8880766e1ef89516236667be59c1a087702df263515a40f84f51b502205f84c36551bf8ad8a3a54e715241181b835720d56d6b5816b172409dd4bab1e3[ALL] 0250a251bd8365ee73a91fe09cb3477b400841c158a59874a8b041e7dff48412e5",
               "hex":"483045022100c7d4fcfa0b8880766e1ef89516236667be59c1a087702df263515a40f84f51b502205f84c36551bf8ad8a3a54e715241181b835720d56d6b5816b172409dd4bab1e301210250a251bd8365ee73a91fe09cb3477b400841c158a59874a8b041e7dff48412e5"
            },
            "sequence":4294967295
         }
      ],
      "vout":[ 
         { 
            "value":0.1,
            "n":0,
            "scriptPubKey":{ 
               "asm":"OP_DUP OP_HASH160 2ca0f264fcffd0ceb34b109e1403a11ecacd73b2 OP_EQUALVERIFY OP_CHECKSIG",
               "hex":"76a9142ca0f264fcffd0ceb34b109e1403a11ecacd73b288ac",
               "reqSigs":1,
               "type":"pubkeyhash",
               "addresses":[ 
                  "154yYDzR6NXTrGyaZnM6d8zpB9PiDBjGGG"
               ]
            }
         },
         { 
            "value":1.5994576,
            "n":1,
            "scriptPubKey":{ 
               "asm":"OP_DUP OP_HASH160 251fafe412cdd9d4f32aabb8ea8858a972030165 OP_EQUALVERIFY OP_CHECKSIG",
               "hex":"76a914251fafe412cdd9d4f32aabb8ea8858a97203016588ac",
               "reqSigs":1,
               "type":"pubkeyhash",
               "addresses":[ 
                  "14PHxkkBb3PxFGNPBiWTWKKiYSA7xWJgC7"
               ]
            }
         }
      ]
   }
}

But the JSON that get's parsed by decoding the hex on the website itself looks different, why that? :o
Please note that the api want's me to give the tx json they key "hex"

Code:

   "Version":"1",
   "LockTime":"0",
   "Vin":[ 
      { 
         "TxId":"97cf8b040250cee0b30ea61bd2dbbc2425a4b50231477bd8e459ada9e2a98ab2",
         "Vout":"1",
         "ScriptSig":{ 
            "Asm":"3045022100c7d4fcfa0b8880766e1ef89516236667be59c1a087702df263515a40f84f51b502205f84c36551bf8ad8a3a54e715241181b835720d56d6b5816b172409dd4bab1e3[ALL] 0250a251bd8365ee73a91fe09cb3477b400841c158a59874a8b041e7dff48412e5",
            "Hex":"483045022100c7d4fcfa0b8880766e1ef89516236667be59c1a087702df263515a40f84f51b502205f84c36551bf8ad8a3a54e715241181b835720d56d6b5816b172409dd4bab1e301210250a251bd8365ee73a91fe09cb3477b400841c158a59874a8b041e7dff48412e5"
         },
         "CoinBase":null,
         "TxInWitness":null,
         "Sequence":"4294967295"
      }
   ],
   "Vout":[ 
      { 
         "Value":0.1,
         "N":0,
         "ScriptPubKey":{ 
            "Asm":"OP_DUP OP_HASH160 2ca0f264fcffd0ceb34b109e1403a11ecacd73b2 OP_EQUALVERIFY OP_CHECKSIG",
            "Hex":"76a9142ca0f264fcffd0ceb34b109e1403a11ecacd73b288ac",
            "ReqSigs":1,
            "Type":"pubkeyhash",
            "Addresses":[ 
               "mjavqH5PuPxidPTCHMKUT4D938zR7kat3R"
            ]
         }
      },
      { 
         "Value":1.5994576,
         "N":1,
         "ScriptPubKey":{ 
            "Asm":"OP_DUP OP_HASH160 251fafe412cdd9d4f32aabb8ea8858a972030165 OP_EQUALVERIFY OP_CHECKSIG",
            "Hex":"76a914251fafe412cdd9d4f32aabb8ea8858a97203016588ac",
            "ReqSigs":1,
            "Type":"pubkeyhash",
            "Addresses":[ 
               "miuFFoqAQ4qD2NqzuHUqLEY3QRkptMcHMu"
            ]
         }
      }
   ],
   "TxId":"3fc25ec159a031c1c8b643a9ef083dbf4011f91b2eff6952b59fa239dccfba94"
}


Title: Re: How to correctly push transaction?
Post by: Coding Enthusiast on January 23, 2017, 03:20:07 PM
I don't understand the programming language you are using but I think your problem is that you are passing a wrong string as your hex parameter when you want to push.
When you call the url .../decode and take the response and put it in the decodedHex variable, then you are pushing decodedHex (which is a JSON) to .../push url.
If I understood your code correctly, that is a wrong approach.

In the second part you have to push a Raw transaction JSON like this {"hex":"TX_HASH"} not the whole JSON.
Take your transactionHex variable from the first part (line 5) and use that to push.


Title: Re: How to correctly push transaction?
Post by: DrSeuss on January 23, 2017, 03:24:23 PM
Code:
POST http://btc.blockr.io/api/v1/tx/push 400 (Bad Request)
send @ jquery.min.js:4
ajax @ jquery.min.js:4
success @ (index):159
i @ jquery.min.js:2
fireWith @ jquery.min.js:2
z @ jquery.min.js:4
(anonymous) @ jquery.min.js:4
(index):169 Failed to Push: {"status":"error","data":"TX is missing","code":400,"message":"Please see API documentation on how to POST tx hex as json."}
(index):170 Transaction Hash: 010000000194bacfdc39a29fb55269ff2e1bf91140bf3d08efa943b6c8c131a059c15ec23f01000000694630430220691210387a5a5897afedf546e42a482265b2be96ba36f5e2624168f4d94df8d7021f587c9b670089d81f0aee897364aa6e3320f0a9235e5f70924bbaee287f0f6d01210250a251bd8365ee73a91fe09cb3477b400841c158a59874a8b041e7dff48412e5ffffffff0280969800000000001976a9142ca0f264fcffd0ceb34b109e1403a11ecacd73b288ac00b7ef08000000001976a914251fafe412cdd9d4f32aabb8ea8858a97203016588ac00000000
(index):171 This is pushed: {"hex":"010000000194bacfdc39a29fb55269ff2e1bf91140bf3d08efa943b6c8c131a059c15ec23f01000000694630430220691210387a5a5897afedf546e42a482265b2be96ba36f5e2624168f4d94df8d7021f587c9b670089d81f0aee897364aa6e3320f0a9235e5f70924bbaee287f0f6d01210250a251bd8365ee73a91fe09cb3477b400841c158a59874a8b041e7dff48412e5ffffffff0280969800000000001976a9142ca0f264fcffd0ceb34b109e1403a11ecacd73b288ac00b7ef08000000001976a914251fafe412cdd9d4f32aabb8ea8858a97203016588ac00000000"}

This is what I get when I try to do it with the transactionHex :/


Title: Re: How to correctly push transaction?
Post by: Coding Enthusiast on January 23, 2017, 03:43:08 PM
I fixed a mistake in my comment, you have to push what you have in the last line not a string, and not the whole JSON you have in above comment.
The JSON is a one parameter object with this format: {"hex":"TX_HASH"}
Replace TX_HASH with the transaction hash (the line before last, the string starting with 010000000194)


Title: Re: How to correctly push transaction?
Post by: DrSeuss on January 23, 2017, 03:48:50 PM
Code:
var transactionHex = tx.build().toHex()

                        // Request current BTC Price for $ & €
                        var decodedHex;

                        $.ajax({
                            url: "http://btc.blockr.io/api/v1/tx/decode",
                            type: "post",
                            data: {
                            hex: transactionHex
                            },
                            success: function(response) {
                                decodedHex = response;
                                console.log(decodedHex);
                                pushHex    = JSON.stringify(decodedHex.data.tx);
                                pushHex2    = JSON.parse(pushHex);
                                document.getElementById('baseDiv').innerHTML += "<br /><br />Decoded Hex: " + JSON.stringify(response);

                                $.ajax({
                                    url: "https://testnet-api.smartbit.com.au/v1/blockchain/pushtx",
                                    type: "post",
                                    data2: {
                                    hex: transactionHex
                                    },
                                    success: function(response) {
                                        console.log("Transaction pushed. " + response);
                                    },
                                    error: function(xhr) {
                                        console.log("Failed to Push: " + xhr.responseText);
                                        console.log("Transaction Hash: " + transactionHex);
                                        console.log("This is pushed: " + JSON.stringify(this.data2));
                                    }
                                });
                            },
                            error: function(xhr) {
                                console.log("Failed to decode Hex. -> " + xhr);
                            }
                        });

I don't really understand what you mean but as you see I'm using the transactionHash as a parameter. :o
Code:
{"hex":"010000000194bacfdc39a29fb55269ff2e1bf91140bf3d08efa943b6c8c131a059c15ec23f01000000694630430220691210387a5a5897afedf546e42a482265b2be96ba36f5e2624168f4d94df8d7021f587c9b670089d81f0aee897364aa6e3320f0a9235e5f70924bbaee287f0f6d01210250a251bd8365ee73a91fe09cb3477b400841c158a59874a8b041e7dff48412e5ffffffff0280969800000000001976a9142ca0f264fcffd0ceb34b109e1403a11ecacd73b288ac00b7ef08000000001976a914251fafe412cdd9d4f32aabb8ea8858a97203016588ac00000000"}
is what's beeing pushed, with hex beeing the key and the transactionHash as parameter, what is my mistake?
I feel so stupid right now  ::)


Title: Re: How to correctly push transaction?
Post by: Coding Enthusiast on January 23, 2017, 04:04:46 PM
The problem is that I don't know anything about this language you are using.

Try adding a header indicating your contentType and set it to application/json

This is what I do in C#
Code:
using (HttpClient client = new HttpClient())
      {
          string hex = "010000000194bacf......";
          string Json = "{\"hex\":\"" + hex + "\"}";
          string url = "http://btc.blockr.io/api/v1/tx/push";
          HttpContent content = new StringContent(Json);
          content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

          var respond = await client.PostAsync(url, content);

          var result = await respond.Content.ReadAsStringAsync();
      }
A quick and dirty work.


Title: Re: How to correctly push transaction?
Post by: DrSeuss on January 23, 2017, 04:14:57 PM
I'm using Javascript. :)

Code:
$.ajax({
                                    url: "http://btc.blockr.io/api/v1/tx/push",
                                    type: "post",
                                    contentType: "application/json; charset=utf-8",
                                    dataType: "json",
                                    data2: {
                                    hex: transactionHex
                                    },
                                    success: function(response) {
                                        console.log("Transaction pushed. " + response);
                                    },
                                    error: function(xhr) {
                                        console.log("Failed to Push: " + xhr.responseText);
                                        console.log("Transaction Hash: " + transactionHex);
                                        console.log("This is pushed: " + JSON.stringify(this.data2));
                                    }
                                });

I now declared that I will send a json but..

Code:
This is pushed: "{\"hex\":\"010000000194bacfdc39a29fb55269ff2e1bf91140bf3d08efa943b6c8c131a059c15ec23f01000000694630430220691210387a5a5897afedf546e42a482265b2be96ba36f5e2624168f4d94df8d7021f587c9b670089d81f0aee897364aa6e3320f0a9235e5f70924bbaee287f0f6d01210250a251bd8365ee73a91fe09cb3477b400841c158a59874a8b041e7dff48412e5ffffffff0280969800000000001976a9142ca0f264fcffd0ceb34b109e1403a11ecacd73b288ac00b7ef08000000001976a914251fafe412cdd9d4f32aabb8ea8858a97203016588ac00000000\"}"

it fails :'(


Title: Re: How to correctly push transaction?
Post by: Coding Enthusiast on January 23, 2017, 04:26:54 PM
I am sorry that I can not help you more than this. But you can take a look at these links maybe they can be of some help:
https://github.com/search?l=JavaScript&q=blockr.io&type=Repositories&utf8=%E2%9C%93
https://github.com/search?l=JavaScript&q=smartbit&ref=searchresults&type=Repositories&utf8=%E2%9C%93
These are wrappers on blockr.io and smartbit.com Api written in JavaScript.

Also you can ask your questions on stackoverflow.com or reddit.com JavaScript boards.


Title: Re: How to correctly push transaction?
Post by: DrSeuss on January 23, 2017, 05:26:20 PM
Thanks anyway man :)

Sadly I don't find any help online, the Repo's don't include info either. :/
So if anyone has more ideas, I'd be glad. :)

This is what I have
Code:
// Sign the transaction with keyPair of Sender
                        for (var i in transHashesNeeded){
                            tx.sign(parseInt(i), keyPair);
                        }

                        var transactionHex = tx.build().toHex()

                        // Request current BTC Price for $ & €
                        var decodedHex;

                        $.ajax({
                            url: "http://btc.blockr.io/api/v1/tx/decode",
                            type: "post",
                            data: {
                            hex: transactionHex
                            },
                            success: function(response) {
                                decodedHex = response;

                                document.getElementById('baseDiv').innerHTML += "<br /><br />Decoded Hex: " + JSON.stringify(response);

                                $.ajax({
                                    url: "http://btc.blockr.io/api/v1/tx/push",
                                    type: "post",
                                    contentType: "application/json; charset=utf-8",
                                    dataType: "json",
                                    data: JSON.stringify({   
                                        hex: transactionHex
                                    }),
                                    success: function(response) {
                                        console.log("Transaction pushed. " + response);
                                    },
                                    error: function(xhr) {
                                        console.log(this);
                                        console.log("Failed to Push: " + xhr.responseText);
                                        console.log("Transaction Hash: " + transactionHex);
                                        console.log("This is pushed: " + this.data);
                                    }
                                });
                            },
                            error: function(xhr) {
                                console.log("Failed to decode Hex. -> " + xhr);
                            }
                        });
                    }             
                });

And this is the console output

Code:
Objectaccepts: Objectasync: truecontentType: "application/json; charset=utf-8"contents: Objectconverters: ObjectcrossDomain: truedata: "{"hex":"0100000001329e495a5e7dbb619ca84b3f1bc9ee0cca279f538b7ea0513a33be5ed46fe370000000006a473044022075b6372ed5febf7724613eddf7207fb5ed7b60b8c3f8d2cb77e76248c1f2daf102206edef165631bdf8455a5b486c0f7189ed80719c5ec726dccdecf3564fd59ae7901210250a251bd8365ee73a91fe09cb3477b400841c158a59874a8b041e7dff48412e5ffffffff0280969800000000001976a9142ca0f264fcffd0ceb34b109e1403a11ecacd73b288ac17db6d07000000001976a914251fafe412cdd9d4f32aabb8ea8858a97203016588ac00000000"}"dataType: "json"dataTypes: Array[2]error: (xhr)flatOptions: Objectglobal: truehasContent: trueisLocal: falsejsonp: "callback"jsonpCallback: ()processData: trueresponseFields: Objectsuccess: (response)type: "POST"url: "http://btc.blockr.io/api/v1/tx/push"xhr: ()__proto__: Object
(index):170 Failed to Push: {"status":"fail","data":"Could not push your transaction!","code":500,"message":"Did you sign your transaction?"}
(index):171 Transaction Hash: 0100000001329e495a5e7dbb619ca84b3f1bc9ee0cca279f538b7ea0513a33be5ed46fe370000000006a473044022075b6372ed5febf7724613eddf7207fb5ed7b60b8c3f8d2cb77e76248c1f2daf102206edef165631bdf8455a5b486c0f7189ed80719c5ec726dccdecf3564fd59ae7901210250a251bd8365ee73a91fe09cb3477b400841c158a59874a8b041e7dff48412e5ffffffff0280969800000000001976a9142ca0f264fcffd0ceb34b109e1403a11ecacd73b288ac17db6d07000000001976a914251fafe412cdd9d4f32aabb8ea8858a97203016588ac00000000
(index):172 This is pushed: {"hex":"0100000001329e495a5e7dbb619ca84b3f1bc9ee0cca279f538b7ea0513a33be5ed46fe370000000006a473044022075b6372ed5febf7724613eddf7207fb5ed7b60b8c3f8d2cb77e76248c1f2daf102206edef165631bdf8455a5b486c0f7189ed80719c5ec726dccdecf3564fd59ae7901210250a251bd8365ee73a91fe09cb3477b400841c158a59874a8b041e7dff48412e5ffffffff0280969800000000001976a9142ca0f264fcffd0ceb34b109e1403a11ecacd73b288ac17db6d07000000001976a914251fafe412cdd9d4f32aabb8ea8858a97203016588ac00000000"}

If you simply take the transactionHex and push it manually everything works perfectly fine.


Title: Re: How to correctly push transaction?
Post by: moonpie45 on January 24, 2017, 04:53:41 AM
I would suggest that you run a full node (with pruning), and have your website send transactions to the network via your node.

In addition to your problem with blockr.io's API, with your current setup, you are risking that you will be unable to push any transactions when any part of the chain between you --> blockr.io --> blockr.io's node --> the rest of the network is broken.

You can setup your node so that it will delete the portion of the blockchain that goes back more than x MB (the default setting is 300 MB, but you can reduce this), and I believe you can change your node's settings so that it will only listen for new blocks (and not unconfirmed transactions), while still being able to relay new transactions that you wish to broadcast.