Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Error when serializing and broadcasting transaction
<!DOCTYPE html>
<html>
<head></head>
<body>
<script src="bitcore/bitcore.min.js"></script>
<script src="bitcore-explorers/bitcore-explorers.min.js"></script>
<script>
var bitcore = require('bitcore');
bitcore.Transaction = require('bitcore').Transaction;
bitcore.Script = require('bitcore').Script;
bitcore.Buffer = require('bitcore').Buffer;
var network = "testnet";
var lockingScript = "";
var unlockingScript = "";
var toAddress = "n1yigjdP1sKUm6n3ZvdAjA5xwY3MeyiMNZ";
var g_utxos = [{"address":"2N8qVQ2FVMiVDTFoAHbw1fdjqgidqxSLhhj","txid":"b33f9ffea78871d8a3f6666544b280b0513d65f364f23141d73eb08379aad4c5","vout":0,"ts":1426687759,"scriptPubKey":"a914ab04e479838731dd1b3d75986d50adbed1629c0587","amount":0.01,"confirmationsFromCache":false}];
var amount = 990000;
var transaction = "";
var serializedTransaction = "";
//get scripts
getScripts();
transaction = createTransaction();
serializedTransaction = serialize(transaction);
broadcast(serializedTransaction);
//create script using Bitcore
function getScripts(){
var Buffer = require('bitcore').Buffer;
var Script = require('bitcore').Script;
//create unlocking script
unlockingScript = new Script();
unlockingScript.add(new bitcore.deps.Buffer("87Pmu6Sh7L8AGg7Ee3dn"));
console.log(unlockingScript.toString());
//create locking script using toScriptHashOut()
//var lockingScript = unlockingScript.toScriptHashOut();
var lockingScript = new Script();
lockingScript.add("OP_HASH160");
//lockingScript.add(new bitcore.deps.Buffer("87Pmu6Sh7L8AGg7Ee3dn"));
//lockingScript.add("OP_HASH160");
lockingScript.add(new bitcore.deps.Buffer("262683285a1ea2cc3ebf0081f078a89a8702a6eb", "hex"));
//lockingScript.add(new bitcore.deps.Buffer("262683285a1ea2cc3ebf0081f078a89a8702a6eb"));
lockingScript.add("OP_EQUAL");
console.log(lockingScript.toString());
//verify unlocking and locking script
var verified = bitcore.Script.Interpreter().verify(unlockingScript, lockingScript);
}
//create transactions using Bitcore
function createTransaction(){
var prevTxId = new bitcore.deps.Buffer(g_utxos[0].txid);
var outputIndex = g_utxos[0].vout
var sequenceNumber = 4294967295;
var param = {};
param.prevTxId = prevTxId;
param.outputIndex = outputIndex;
param.sequenceNumber = sequenceNumber;
param.script = unlockingScript;
var input = new bitcore.Transaction.Input(param);
var transaction = new bitcore.Transaction();
transaction = transaction.fee(0.0001*100000000);
transaction = transaction.to(toAddress, amount);
transaction = transaction.addInput(input, unlockingScript, 1000000);
var verify = transaction.verify();
return transaction;
}
//serialize transaction using Bitcore
function serialize(transaction){
var flags = {};
flags.disableAll = true;
var serializedTransaction = transaction.serialize();
return serializedTransaction;
}
//broadcast transaction using Bitcore
function broadcast(transaction){
var Insight = require('bitcore-explorers').Insight;
var insight = new Insight("https://test-insight.bitpay.com", "testnet");
insight.broadcast(transaction, function(err, returnedTxId){
if (err) {
console.log(err);
} else {
console.log(returnedTxId);
}
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment