import "../types/structs.scrypt"; import "stdUtils.scrypt"; library _opcat_labs_scrypt_ts_opcat_4_0_0__rs__TxUtils { static const int ZERO_SATS = 0; static function buildOutput(bytes scriptHash, int satoshis) : bytes { return _opcat_labs_scrypt_ts_opcat_4_0_0__rs__TxUtils.buildDataOutput(scriptHash, satoshis, sha256(b'')); } static function buildDataOutput(bytes scriptHash, int satoshis, bytes dataHash) : bytes { int scriptHashLen = len(scriptHash); int dataHashLen = len(dataHash); require(scriptHashLen == 32); require(dataHashLen == 32); require(satoshis > 0); return _opcat_labs_scrypt_ts_opcat_4_0_0__rs__TxUtils.satoshisToByteString(satoshis) + scriptHash + dataHash; } static function buildChangeOutput(_opcat_labs_scrypt_ts_opcat_4_0_0__rs__TxOut change) : bytes { return change.satoshis > 0 ? _opcat_labs_scrypt_ts_opcat_4_0_0__rs__TxUtils.buildDataOutput(change.scriptHash, change.satoshis, change.dataHash) : b''; } static function mergeInput(_opcat_labs_scrypt_ts_opcat_4_0_0__rs__TxIn txInput) : bytes { require(len(txInput.prevTxHash) == 32); require(len(txInput.scriptHash) == 32); return (txInput.prevTxHash + _opcat_labs_scrypt_ts_opcat_4_0_0__rs__StdUtils.uint32ToByteString(txInput.prevOutputIndex) + txInput.scriptHash + _opcat_labs_scrypt_ts_opcat_4_0_0__rs__StdUtils.uint32ToByteString(txInput.sequence)); } static function buildOpReturnOutput(bytes data) : bytes { bytes script = b'6a' + pack(len(data)) + data; return _opcat_labs_scrypt_ts_opcat_4_0_0__rs__TxUtils.satoshisToByteString(0) + sha256(script) + sha256(b''); } static function buildP2PKHScript(Ripemd160 addr) : bytes { return (OpCode.OP_DUP + OpCode.OP_HASH160 + pack(20) + addr + OpCode.OP_EQUALVERIFY + OpCode.OP_CHECKSIG); } static function buildP2PKHOutput(int amount, Ripemd160 addr) : bytes { return _opcat_labs_scrypt_ts_opcat_4_0_0__rs__TxUtils.buildDataOutput(sha256(_opcat_labs_scrypt_ts_opcat_4_0_0__rs__TxUtils.buildP2PKHScript(addr)), amount, sha256(b'')); } static function buildOpreturnScript(bytes data) : bytes { return OpCode.OP_FALSE + OpCode.OP_RETURN + _opcat_labs_scrypt_ts_opcat_4_0_0__rs__StdUtils.pushData(data); } static function satoshisToByteString(int n) : bytes { return _opcat_labs_scrypt_ts_opcat_4_0_0__rs__StdUtils.uint64ToByteString(n); } static function byteStringToSatoshis(bytes bs) : int { require(len(bs) == 8); return _opcat_labs_scrypt_ts_opcat_4_0_0__rs__StdUtils.fromLEUnsigned(bs); } }