def createGenesisCoinbase(signblockpubkey): genesis_coinbase = CTransaction() coinbaseinput = CTxIn(outpoint=COutPoint(0, 0), nSequence=0xffffffff) coinbaseinput.scriptSig=CScript([hex_str_to_bytes(signblockpubkey)]) genesis_coinbase.vin.append(coinbaseinput) coinbaseoutput = CTxOut() coinbaseoutput.nValue = 50 * COIN coinbaseoutput.scriptPubKey = CScript([OP_DUP, OP_HASH160, hex_str_to_bytes(signblockpubkey),OP_EQUALVERIFY, OP_CHECKSIG]) genesis_coinbase.vout.append(coinbaseoutput) genesis_coinbase.calc_sha256() return genesis_coinbase
def create_var_transaction(spendfrom, custom_script, size_bytes, fee_sats): # Fund and sign a transaction to a given output, padding it to exactly # size = size_bytes and providing the given fee. # spendfrom should be a CTransaction with first output to OP_TRUE. customout = CTxOut(0, custom_script) # set output amount to required dust customout.nValue = (len(customout.serialize()) + 148) * 3 ctx = CTransaction() ctx.vin.append(CTxIn(COutPoint(spendfrom.sha256, 0), b'')) ctx.vout.append( CTxOut(spendfrom.vout[0].nValue - customout.nValue - fee_sats, bytes([OP_TRUE]))) ctx.vout.append(customout) # two padding outputs ctx.vout.append(CTxOut(0, b'')) ctx.vout.append(CTxOut(0, b'')) size_without_padding = len(ctx.serialize()) padding_needed = size_bytes - size_without_padding assert padding_needed >= 0, (size_bytes, size_without_padding) # Now insert padding. We need to be careful due to the flexible # var_int size. if padding_needed > 0xFFFF + 4: # We can fit all padding in one script. # 4 extra bytes will be used for var_int ctx.vout[2].scriptPubKey = bytes([OP_RETURN]) * (padding_needed - 4) padding_needed = 0 elif padding_needed >= 0xFD + 2: # We can pad most (probably all) using a script that is between # 253 and 65535 in length. Probably fit everything in one script. # 2 extra bytes will be used for var_int scriptsize = min(padding_needed - 2, 0xFFFF) ctx.vout[2].scriptPubKey = bytes([OP_RETURN]) * scriptsize padding_needed -= scriptsize + 2 elif padding_needed >= 0xFD: # 0xFD or 0xFD+1. We can't pad with just one script, so just # take off some. ctx.vout[2].scriptPubKey = bytes([OP_RETURN]) * 50 padding_needed -= 50 # extra padding on another output is needed if original padding_needed # was <= 0xfd+1, or was 0xffff+3 or 0xffff+4 . assert padding_needed < 0xfd ctx.vout[3].scriptPubKey = bytes([OP_RETURN]) * padding_needed ctx.calc_sha256() assert_equal(len(ctx.serialize()), size_bytes) return ctx
def create_transaction(spendfrom, custom_script, amount=None): # Fund and sign a transaction to a given output. # spendfrom should be a CTransaction with first output to OP_TRUE. # custom output will go on position 1, after position 0 which will be # OP_TRUE (so it can be reused). customout = CTxOut(0, bytes(custom_script)) # set output amount to required dust if not given customout.nValue = amount or (len(customout.serialize()) + 148) * 3 ctx = CTransaction() ctx.vin.append(CTxIn(COutPoint(spendfrom.sha256, 0), b'')) ctx.vout.append(CTxOut(0, bytes([OP_TRUE]))) ctx.vout.append(customout) pad_tx(ctx) fee = len(ctx.serialize()) ctx.vout[0].nValue = spendfrom.vout[0].nValue - customout.nValue - fee ctx.rehash() return ctx