Skip to content

Instantly share code, notes, and snippets.

@lukem512
Last active May 26, 2018 07:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukem512/32a50d04c187a471940d to your computer and use it in GitHub Desktop.
Save lukem512/32a50d04c187a471940d to your computer and use it in GitHub Desktop.
OP_RETURN using Bitcore
// 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