Skip to content

Instantly share code, notes, and snippets.

@nodech
Created October 18, 2018 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nodech/84192013db9550156871323cc0bf2f37 to your computer and use it in GitHub Desktop.
Save nodech/84192013db9550156871323cc0bf2f37 to your computer and use it in GitHub Desktop.
Create transaction less than 100 bytes.
'use strict';
const assert = require('assert');
const bcash = require('..');
const {Stack, Script, Address} = bcash;
const {MTX, Outpoint, Input} = bcash;
const network = 'regtest';
// fill after receive receiving
const hashstr = '';
const index = 0;
/*
* First generate receive address
*/
const receiveScript = Script.fromString(
'OP_HASH160 0x14 0xda1745e9b549bd0bfa1a569971c77eba30cd5a4b OP_EQUAL'
);
const addr = Address.fromScripthash(receiveScript.hash160());
console.log('Address: ', addr.toString(network));
/*
* Spend script
*/
const spendScript = Script.fromString('81');
const spendOut = Script.fromNulldata(Buffer.alloc(0));
/*
* Verify stack
*/
const stack = new Stack();
receiveScript.compile();
spendScript.compile();
spendScript.execute(stack);
receiveScript.execute(stack);
assert.strictEqual(stack.length, 1);
assert.strictEqual(stack.getBool(0), true);
/*
* Assemble raw spending transaction if we have hashstr
*/
if (hashstr.length > 0) {
const mtx = new MTX();
// reverse hex as well
const outpoint = Outpoint.fromJSON({
hash: hashstr,
index: index
});
mtx.addInput(Input.fromOutpoint(outpoint));
const redeem = spendScript.clone();
redeem.pushData(receiveScript.toRaw());
redeem.compile();
mtx.inputs[0].script = redeem;
mtx.addOutput({
script: spendOut
});
console.log(mtx.toRaw().toString('hex'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment