prev_index = i
                print('found: {}:{}'.format(prev_tx.hex(), prev_index))
# create the TxIn
tx_in = TxIn(prev_tx, prev_index)
# calculate the output amount (previous amount minus the fee)
output_amount = prev_amount - fee
# create a new TxOut to the target script with the output amount
tx_outs = TxOut(output_amount, target_script)
# create a new transaction with the one input and one output
tx_obj = Tx(1, [tx_in], [tx_out], 0, testnet=True)
# sign the only input of the transaction
tx_obj.sign_input(0, private_key)
# serialize and hex to see what it looks like
print(tx_obj.serialize().hex())
# send this signed transaction on the network
node.send(tx_obj)
# wait a sec so this message goes through with time.sleep(1)
time.sleep(1)
# now ask for this transaction from the other node
# create a GetDataMessage
getdata = GetDataMessage()
# ask for our transaction by adding it to the message
getdata.add_data(TX_DATA_TYPE, tx_obj.hash())
# send the message
node.send(getdata)
# now wait for a Tx response
received_tx = node.wait_for(Tx)
# if the received tx has the same id as our tx, we are done!
if received_tx.id() == tx_obj.id():
    print('success!')
示例#2
0
def save_genesis_block():

    script_sig = Script(
        [bytes.fromhex("ffff001d"),
         bytes.fromhex("04"), GENESIS_THE_TIMES])

    script_pubkey = Script([GENESIS_SEC_PUBKEY, 0xac])

    tx_in = TxIn(prev_tx=b"\x00" * 32,
                 prev_index=0xffffffff,
                 script_sig=script_sig,
                 sequence=0xffffffff)

    tx_out = TxOut(amount=GENESIS_BLOCK_REWARD, script_pubkey=script_pubkey)

    tx = Tx(version=1, tx_ins=[tx_in], tx_outs=[tx_out], locktime=0)

    block = Block_Full(version=1,
                       prev_block=b"\x00" * 32,
                       merkle_root=GENESIS_BLOCK_MERKLE_ROOT,
                       timestamp=GENESIS_BLOCK_TIMESTAMP,
                       bits=GENESIS_BLOCK_BITS,
                       nonce=GENESIS_BLOCK_NONCE,
                       txs=[tx])

    tx = block.txs[0]
    script_sig_cmds = tx.tx_ins[0].script_sig.cmds
    script_pubkey_cmds = tx.tx_outs[0].script_pubkey.cmds
    GENESIS_SCRIPT_SIG_DB = Script_db(cmds=script_sig_cmds)
    GENESIS_SCRIPT_PUBKEY_DB = Script_db(cmds=script_pubkey_cmds)
    tx_in = tx.tx_ins[0]
    GENESIS_TX_IN_DB = TxIn_db(prev_tx=tx_in.prev_tx,
                               prev_index=tx_in.prev_index,
                               script_sig=GENESIS_SCRIPT_SIG_DB,
                               sequence=tx_in.sequence)

    tx_out = tx.tx_outs[0]
    GENESIS_TX_OUT_DB = TxOut_db(amount=tx_out.amount,
                                 script_pubkey=GENESIS_SCRIPT_PUBKEY_DB)

    tx_size = len(tx.serialize())

    GENESIS_TX_DB = Tx_db(segwit=False,
                          tx_hash=tx.hash(),
                          version=tx.version,
                          tx_ins=[GENESIS_TX_IN_DB],
                          tx_outs=[GENESIS_TX_OUT_DB],
                          locktime=tx.locktime,
                          size=tx_size)

    block_size = block.size()
    try:
        GENESIS_BLOCK_DB = Block_db(block_hash=GENESIS_BLOCK_HASH,
                                    block_height=0,
                                    block_reward=GENESIS_BLOCK_REWARD,
                                    version=1,
                                    prev_block=b"\x00" * 32,
                                    merkle_root=GENESIS_BLOCK_MERKLE_ROOT,
                                    timestamp=GENESIS_BLOCK_TIMESTAMP,
                                    bits=GENESIS_BLOCK_BITS,
                                    nonce=GENESIS_BLOCK_NONCE,
                                    txs=[GENESIS_TX_DB],
                                    size=block_size,
                                    mined_btc=GENESIS_BLOCK_REWARD,
                                    total_size=block_size).save()
    except:
        pass