2
So the below is the function I'm running. I'm sending in the key for this bitcoin address
16WBguy6KVyTGnF4KX7Vmdx8ztj4wENh4W
and the message
bkawk
to this function
// Require the packages that we need.
var bitcoin = require('bitcoinjs-lib');
var request = require('request');
var blockchain = require('blockchain.info');
var blockexplorer = require('blockchain.info/blockexplorer');
// Export the module and pass in the message and the WIF
exports.opReturn = function (message, WIF) {
// Setup the new promise
return new Promise(function(resolve, reject) {
// Set the network to the main net not test
var network = bitcoin.networks.mainnet;
// Generate the keypair
var keyPair = bitcoin.ECPair.fromWIF(WIF);
// Get the address from the key pair
var address = keyPair.getAddress();
// Ask blockchain.info for all the unspent transactions
var url = 'https://blockchain.info/id/unspent?active='+address;
request(url, function (error, response, body) {
// Check the request didn't error and we got a 200 ok back
if (!error && response.statusCode == 200) {
// Parse the results
var body = JSON.parse(body);
// Build a new transaction
var tx = new bitcoin.TransactionBuilder(network);
cont...
// Buffer the message into hex
var data = new Buffer(message);
// Prepare the message
var dataScript = bitcoin.script.nullDataOutput(data);
// I Suspect there is problems here!!
var unspent = body.unspent_outputs[0].tx_hash_big_endian;
// Add the unspent tx to the output
tx.addInput(unspent, 0);
// Add the unspent transaction to the first output
tx.addOutput(dataScript, 0);
// Add our address to the 1st output - Must I use another address or no?
tx.addOutput(address, 1000);
// Add our address to the 2nd output - Must I use another address here too?
tx.addOutput(address, 1000);
// Sign the transaction
tx.sign(0, keyPair);
// Convert to hex
var txRaw = tx.build().toHex();
var txId = txRaw.getId()
console.log(txId);
// Resolve the promise
resolve(txRaw);
// Log the hex
console.log(txRaw.toHex());
} else{
// if there was an error reject the promise
reject(error);
};
})
}); //-- END PROMISE
};// END FUNCTION
the above console.log is giving me this transaction hex
0100000001b4c1870d4e0c6928f40224f465ab7d2c3692fe29e4bc2cc5213776ff128de13f000000006b483045022100a954cdb05753fed2f8afc41619d66dde45e330b1aa75cd5a1d1a03b67392642702203bda25690dac67bed0117fd28ad30fc2cbc16aa778a3b37832aabba9939e344001210298e836ce7df5696bdce02742bb5d7a070b2e5b1ab3535001b71f3615c21c1b1dffffffff030000000000000000076a05626b61776bb80b0000000000001976a9143c5d9d2466274014c2db74dd4738116a193d142788acb80b0000000000001976a9143c5d9d2466274014c2db74dd4738116a193d142788ac00000000
which decoded looks like this
{
"lock_time":0,
"size":242,
"inputs":[
{
"prev_out":{
"index":0,
"hash":"3fe18d12ff763721c52cbce429fe92362c7dab65f42402f428690c4e0d87c1b4"
},
"script":"483045022100a954cdb05753fed2f8afc41619d66dde45e330b1aa75cd5a1d1a03b67392642702203bda25690dac67bed0117fd28ad30fc2cbc16aa778a3b37832aabba9939e344001210298e836ce7df5696bdce02742bb5d7a070b2e5b1ab3535001b71f3615c21c1b1d"
}
],
"version":1,
"vin_sz":1,
"hash":"926eeecb1fc28a34cb8ed14e1e6175ec9012af390b232e26d3c610c345abba47",
"vout_sz":3,
"out":[
{
"script_string":"OP_RETURN 626b61776b",
"value":0,
"script":"6a05626b61776b"
},
{
"script_string":"OP_DUP OP_HASH160 3c5d9d2466274014c2db74dd4738116a193d1427 OP_EQUALVERIFY OP_CHECKSIG",
"address":"16WBguy6KVyTGnF4KX7Vmdx8ztj4wENh4W",
"value":3000,
"script":"76a9143c5d9d2466274014c2db74dd4738116a193d142788ac"
},
{
"script_string":"OP_DUP OP_HASH160 3c5d9d2466274014c2db74dd4738116a193d1427 OP_EQUALVERIFY OP_CHECKSIG",
"address":"16WBguy6KVyTGnF4KX7Vmdx8ztj4wENh4W",
"value":3000,
"script":"76a9143c5d9d2466274014c2db74dd4738116a193d142788ac"
}
]
}
Pushing it to https://blockchain.info/pushtx returns...
None Standard Script Output OP_RETURN 626b61776b
And pushing here http://blockr.io/tx/push results in
There was an error pushing your transaction to network!
Did you sign your transaction? Is this double spend? Have you already sent this transaction?
but then when I check it here I don't see the transaction. Any Ideas?
https://blockchain.info/address/16WBguy6KVyTGnF4KX7Vmdx8ztj4wENh4W
Here is an example of a successful transaction made from Colu
You should probably edit the title to be the actual question, rather than the question's general topic. It would also be nice if the question itself was a little more specific, rather than just "here's my code, tell me what's wrong". Narrow your question down to a specific, identified problem, and you are more likely to get better answers. – Jestin – 2016-06-10T13:43:02.203
Thank you @jestin for the kind suggestion, the answer to the problem was unknown at the time of writing and as you can see I detailed the issue and where I thought the problems may be as best as I could. Please can you make a suggestions for the better title? – Will-In-China – 2016-06-10T13:52:13.513
Something like "Invalid transaction created with bitcoinjs-lib" might be better. I don't think you had any reason to believe the Promise was the problem, since you were obviously resolving the raw transaction. That word just made it confusing which part you were asking about. – Jestin – 2016-06-10T14:00:01.477