I am getting error messages when trying to serialize or broadcast a transaction. Any help with this would be appreciated.
I get an error when I try serializing a transaction. I do not get the error message when I pass in true or the disableAll flag in as an argument for serialize()
. Non of the other flags prevent the error for serialize()
.
The error message is:
bitcore.ErrorTransactionUnableToVerifySignature:
Unable to verify signature: Unrecognized script kind, or not enough information to execute script.This usually happens when creating a transaction from a serialized transaction
I get three error messages if I pass in true or the disableAll flag as arguments for serialize()
and then pass the serialized transaction to broadcast()
. These are:
In the console:
bitcore.ErrorTransactionUnableToVerifySignature:
Unable to verify signature: Unrecognized script kind, or not enough information to execute script.This usually happens when creating a transaction from a serialized transaction
The returned err from broadcast()
:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
The post response from the insight server:
POST https://test-insight.bitpay.com/api/tx/send 400 Bad Request
Response: TX decode failed (code -22)
Both:
Script.Interpreter().verify()
and
transaction.verify()
are evaluating to true.
Here is the code that I have written:
<!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;
bitcore.Address = require('bitcore').Address;
var network = "testnet";
var lockingScript = "";
var unlockingScript = "";
var toAddress = "n1yigjdP1sKUm6n3ZvdAjA5xwY3MeyiMNZ";
var g_utxos = [{"address":"2MwEv1p64YJJowC8HzUtsBDyweKWgutmqv1","txid":"cb0573919f768f8bead54168b9ab49de3ef811d168e7d0fbe41008b9ce7eff40","vout":0,"ts":1426187331,"scriptPubKey":"a9142bd17fded06462cfad6402f4b86ccc06e5c543e587","amount":0.01,"confirmations":4,"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 redeam script
var redeamScript = new Script();
redeamScript.add(new bitcore.deps.Buffer("87Pmu6Sh7L8AGg7Ee3dn OP_EQUAL"));
//create locking script using toScriptHashOut()
var initialLockingScript = redeamScript.toScriptHashOut();
lockingScript = new Script();
lockingScript.add(new bitcore.deps.Buffer("OP_HASH160"));
var hashedRedeamScriptBuffer = initialLockingScript.chunks[1].buf;
lockingScript.add(new bitcore.deps.Buffer(hashedRedeamScriptBuffer));
lockingScript.add(new bitcore.deps.Buffer("OP_EQUAL"));
//create unlocking script
unlockingScript = new Script();
unlockingScript.add(new bitcore.deps.Buffer("87Pmu6Sh7L8AGg7Ee3dn"));
unlockingScript.add(new bitcore.deps.Buffer("87Pmu6Sh7L8AGg7Ee3dn OP_EQUAL"));
//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(flags);
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>