Last active
May 26, 2018 07:07
-
-
Save lukem512/32a50d04c187a471940d to your computer and use it in GitHub Desktop.
OP_RETURN using Bitcore
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
// Extend the TransactionBuilder class to include OP_RETURN scripts. | |
// The two parameters are the amount to use for the output and the data | |
// to push onto the blockchain. | |
// This is done inside bitcore/lib/TransactionBuilder.js | |
TransactionBuilder.prototype.setUnspendableOutput = function(amount, msg) { | |
var valueOutSat = bignum(0); | |
var txobj = {}; | |
txobj.version = 1; | |
txobj.lock_time = this.lockTime || 0; | |
txobj.ins = []; | |
txobj.outs = []; | |
var amountSat = util.parseValue(amount); | |
var value = util.bigIntToValue(amountSat); | |
var data = msg || 0; | |
var humanScript = "OP_RETURN " + data; | |
var script = Script.fromHumanReadable(humanScript); | |
var txout = { | |
v: value, | |
s: script.getBuffer(), | |
}; | |
txobj.outs.push(txout); | |
valueOutSat = valueOutSat.add(amountSat); | |
this.valueOutSat = valueOutSat; | |
this._setFeeAndRemainder(txobj); | |
this.tx = new Transaction(txobj); | |
return this; | |
}; | |
// Usage is as follows: | |
function CreateOpReturnTx() { | |
var opts = { | |
// options | |
}; | |
var utxos = [ | |
// unspent tx outputs | |
]; | |
var priv = [ | |
// private keys for utxos | |
]; | |
try { | |
// Create the TX | |
var TX = new Builder(opts) | |
.setUnspent(utxos) | |
.setUnspendableOutput(amount, 0x1234); | |
.sign(priv) | |
.build(); | |
} | |
catch (err) { | |
console.err(err.message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment