Created
March 19, 2015 18:39
-
-
Save anonymous/f369b5e521ca2fef3101 to your computer and use it in GitHub Desktop.
redeem script is hex of string buffer and op_equal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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 redeam script | |
var redeemScript = new Script(); | |
redeemScript.add(new bitcore.deps.Buffer("323020307833383337353036643735333635333638333734633338343134373637333734353635333336343665204f505f455155414c","hex")); | |
console.log(redeemScript.toString()); | |
//create unlocking script | |
unlockingScript = new Script(); | |
unlockingScript.add(new bitcore.deps.Buffer("87Pmu6Sh7L8AGg7Ee3dn")); | |
unlockingScript.add(57); | |
unlockingScript.add(new bitcore.deps.Buffer("323020307833383337353036643735333635333638333734633338343134373637333734353635333336343665204f505f455155414c","hex")); | |
console.log(unlockingScript.toString()); | |
//create locking script using toScriptHashOut() | |
var lockingScript = redeemScript.toScriptHashOut(); | |
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