コード例 #1
0
def add_channel(local_node, remote_peer, input_tx_id, input_tx_index):

    tx_in = TxIn(bytes.fromhex(input_tx_id), input_tx_index)
    local_amount = tx_in.value()
    remote_amount = 0

    # Construct the output: amount, scriptPubKey = 2-of-2 Bare Multisig  = Script([op_1, pubkey1, pubkey2, op_2, op_checkmultisig])
    scriptPubKey = Script([
        0x52,
        local_node.public_key.sec(),
        remote_peer.public_key.sec(), 0x52, 0xae
    ])
    tx_out = TxOut(amount=local_amount, script_pubkey=scriptPubKey)

    # Construct the transaction object
    funding_tx = Tx(1, [tx_in], [tx_out], 0, True)

    # Sign the input
    funding_tx.sign_input(0, local_node.private_key)

    #send channel request
    new_channel = Channel(remote_peer, local_amount, remote_amount, funding_tx)
    remote_peer.send(str.encode(new_channel.toJSON()))

    return new_channel
 def test_exercise_4(self):
     last_block_hex = '000000000d65610b5af03d73ed67704713c9b734d87cf4b970d39a0416dd80f9'
     last_block = bytes.fromhex(last_block_hex)
     secret = little_endian_to_int(
         hash256(b'Jimmy Song Programming Blockchain'))
     private_key = PrivateKey(secret=secret)
     addr = private_key.point.address(testnet=True)
     h160 = decode_base58(addr)
     target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv'
     self.assertEqual(addr, target_address)
     filter_size = 30
     filter_num_functions = 5
     filter_tweak = 90210  # FILL THIS IN
     target_h160 = decode_base58(target_address)
     target_script = p2pkh_script(target_h160)
     fee = 5000  # fee in satoshis
     node = SimpleNode('tbtc.programmingblockchain.com',
                       testnet=True,
                       logging=False)
     bf = BloomFilter(filter_size, filter_num_functions, filter_tweak)
     bf.add(h160)
     node.handshake()
     node.send(b'filterload', bf.filterload())
     getheaders_message = GetHeadersMessage(start_block=last_block)
     node.send(getheaders_message.command, getheaders_message.serialize())
     headers_envelope = node.wait_for_commands([HeadersMessage.command])
     stream = headers_envelope.stream()
     headers = HeadersMessage.parse(stream)
     get_data_message = GetDataMessage()
     for block in headers.blocks:
         self.assertTrue(block.check_pow())
         if last_block is not None:
             self.assertEqual(block.prev_block, last_block)
         last_block = block.hash()
         get_data_message.add_data(FILTERED_BLOCK_DATA_TYPE, last_block)
     node.send(get_data_message.command, get_data_message.serialize())
     prev_tx = None
     while prev_tx is None:
         envelope = node.wait_for_commands([b'merkleblock', b'tx'])
         stream = envelope.stream()
         if envelope.command == b'merkleblock':
             mb = MerkleBlock.parse(stream)
             self.assertTrue(mb.is_valid())
         else:
             prev = Tx.parse(stream, testnet=True)
             for i, tx_out in enumerate(prev.tx_outs):
                 if tx_out.script_pubkey.address(testnet=True) == addr:
                     prev_tx = prev.hash()
                     prev_index = i
                     prev_amount = tx_out.amount
                     break
     tx_in = TxIn(prev_tx, prev_index)
     output_amount = prev_amount - fee
     tx_out = TxOut(output_amount, target_script)
     tx_obj = Tx(1, [tx_in], [tx_out], 0, testnet=True)
     tx_obj.sign_input(0, private_key)
     self.assertEqual(
         tx_obj.serialize().hex(),
         '010000000194e631abb9e1079ec72a1616a3aa0111c614e65b96a6a4420e2cc6af9e6cc96e000000006a47304402203cc8c56abe1c0dd043afa9eb125dafbebdde2dd4cd7abf0fb1aae0667a22006e02203c95b74d0f0735bbf1b261d36e077515b6939fc088b9d7c1b7030a5e494596330121021cdd761c7eb1c90c0af0a5963e94bf0203176b4662778d32bd6d7ab5d8628b32ffffffff01f8829800000000001976a914ad346f8eb57dee9a37981716e498120ae80e44f788ac00000000'
     )
コード例 #3
0
ファイル: 12.py プロジェクト: gionasoldati/programmingbitcoin
 def test_exercise_6(self):
     last_block_hex = '000000000d65610b5af03d73ed67704713c9b734d87cf4b970d39a0416dd80f9'
     secret = little_endian_to_int(
         hash256(b'Jimmy Song Programming Blockchain'))
     private_key = PrivateKey(secret=secret)
     addr = private_key.point.address(testnet=True)
     h160 = decode_base58(addr)
     target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv'
     self.assertEqual(addr, target_address)
     target_h160 = decode_base58(target_address)
     target_script = p2pkh_script(target_h160)
     fee = 5000
     node = SimpleNode('tbtc.programmingblockchain.com', testnet=True)
     bf = BloomFilter(30, 5, 90210)
     bf.add(h160)
     node.handshake()
     node.send(bf.filterload())
     start_block = bytes.fromhex(last_block_hex)
     getheaders = GetHeadersMessage(start_block=start_block)
     node.send(getheaders)
     headers = node.wait_for(HeadersMessage)
     last_block = None
     getdata = GetDataMessage()
     for b in headers.blocks:
         if not b.check_pow():
             raise RuntimeError('proof of work is invalid')
         if last_block is not None and b.prev_block != last_block:
             raise RuntimeError('chain broken')
         getdata.add_data(FILTERED_BLOCK_DATA_TYPE, b.hash())
         last_block = b.hash()
     node.send(getdata)
     prev_tx, prev_index, prev_tx_obj = None, None, None
     while prev_tx is None:
         message = node.wait_for(MerkleBlock, Tx)
         if message.command == b'merkleblock':
             if not message.is_valid():
                 raise RuntimeError('invalid merkle proof')
         else:
             message.testnet = True
             for i, tx_out in enumerate(message.tx_outs):
                 if tx_out.script_pubkey.address(testnet=True) == addr:
                     prev_tx = message.hash()
                     prev_index = i
                     prev_amount = tx_out.amount
                     self.assertEqual(
                         message.id(),
                         '6ec96c9eafc62c0e42a4a6965be614c61101aaa316162ac79e07e1b9ab31e694'
                     )
                     self.assertEqual(i, 0)
                     break
     tx_in = TxIn(prev_tx, prev_index)
     output_amount = prev_amount - fee
     tx_out = TxOut(output_amount, target_script)
     tx_obj = Tx(1, [tx_in], [tx_out], 0, testnet=True)
     tx_obj.sign_input(0, private_key)
     self.assertEqual(
         tx_obj.serialize().hex(),
         '010000000194e631abb9e1079ec72a1616a3aa0111c614e65b96a6a4420e2cc6af9e6cc96e000000006a47304402203cc8c56abe1c0dd043afa9eb125dafbebdde2dd4cd7abf0fb1aae0667a22006e02203c95b74d0f0735bbf1b261d36e077515b6939fc088b9d7c1b7030a5e494596330121021cdd761c7eb1c90c0af0a5963e94bf0203176b4662778d32bd6d7ab5d8628b32ffffffff01f8829800000000001976a914ad346f8eb57dee9a37981716e498120ae80e44f788ac00000000'
     )
コード例 #4
0
ファイル: 7.py プロジェクト: BrownBurger/programmingbitcoin
 def test_exercise_5(self):
     prev_tx_1 = bytes.fromhex(
         '11d05ce707c1120248370d1cbf5561d22c4f83aeba0436792c82e0bd57fe2a2f')
     prev_index_1 = 1
     prev_tx_2 = bytes.fromhex(
         '51f61f77bd061b9a0da60d4bedaaf1b1fad0c11e65fdc744797ee22d20b03d15')
     prev_index_2 = 1
     target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv'
     target_amount = 0.0429
     secret = 8675309
     priv = PrivateKey(secret=secret)
     tx_ins = []
     tx_ins.append(TxIn(prev_tx_1, prev_index_1, Script([]), 0xffffffff))
     tx_ins.append(TxIn(prev_tx_2, prev_index_2, Script([]), 0xffffffff))
     tx_outs = []
     h160 = decode_base58(target_address)
     script_pubkey = p2pkh_script(h160)
     target_satoshis = int(target_amount * 100000000)
     tx_outs.append(TxOut(target_satoshis, script_pubkey))
     tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True)
     self.assertTrue(tx_obj.sign_input(0, priv))
     self.assertTrue(tx_obj.sign_input(1, priv))
     self.assertTrue(tx_obj.verify())
     want = '01000000022f2afe57bde0822c793604baae834f2cd26155bf1c0d37480212c107e75cd011010000006a47304402204cc5fe11b2b025f8fc9f6073b5e3942883bbba266b71751068badeb8f11f0364022070178363f5dea4149581a4b9b9dbad91ec1fd990e3fa14f9de3ccb421fa5b269012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b67ffffffff153db0202de27e7944c7fd651ec1d0fab1f1aaed4b0da60d9a1b06bd771ff651010000006b483045022100b7a938d4679aa7271f0d32d83b61a85eb0180cf1261d44feaad23dfd9799dafb02205ff2f366ddd9555f7146861a8298b7636be8b292090a224c5dc84268480d8be1012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b67ffffffff01d0754100000000001976a914ad346f8eb57dee9a37981716e498120ae80e44f788ac00000000'
     self.assertEqual(tx_obj.serialize().hex(), want)
コード例 #5
0
ファイル: 7.py プロジェクト: BrownBurger/programmingbitcoin
 def test_exercise_4(self):
     prev_tx = bytes.fromhex(
         '75a1c4bc671f55f626dda1074c7725991e6f68b8fcefcfca7b64405ca3b45f1c')
     prev_index = 1
     target_address = 'miKegze5FQNCnGw6PKyqUbYUeBa4x2hFeM'
     target_amount = 0.01
     change_address = 'mzx5YhAH9kNHtcN481u6WkjeHjYtVeKVh2'
     change_amount = 0.009
     secret = 8675309
     priv = PrivateKey(secret=secret)
     tx_ins = []
     tx_ins.append(TxIn(prev_tx, prev_index, Script([]), 0xffffffff))
     tx_outs = []
     h160 = decode_base58(target_address)
     script_pubkey = p2pkh_script(h160)
     target_satoshis = int(target_amount * 100000000)
     tx_outs.append(TxOut(target_satoshis, script_pubkey))
     h160 = decode_base58(change_address)
     script_pubkey = p2pkh_script(h160)
     change_satoshis = int(change_amount * 100000000)
     tx_outs.append(TxOut(change_satoshis, script_pubkey))
     tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True)
     self.assertTrue(tx_obj.sign_input(0, priv))
     self.assertTrue(tx_obj.verify())
     want = '01000000011c5fb4a35c40647bcacfeffcb8686f1e9925774c07a1dd26f6551f67bcc4a175010000006b483045022100a08ebb92422b3599a2d2fcdaa11f8f807a66ccf33e7f4a9ff0a3c51f1b1ec5dd02205ed21dfede5925362b8d9833e908646c54be7ac6664e31650159e8f69b6ca539012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b67ffffffff0240420f00000000001976a9141ec51b3654c1f1d0f4929d11a1f702937eaf50c888ac9fbb0d00000000001976a914d52ad7ca9b3d096a38e752c2018e6fbc40cdf26f88ac00000000'
     self.assertEqual(tx_obj.serialize().hex(), want)
コード例 #6
0
    def test_sign_input(self):
        private_key = PrivateKey(secret=8675309)
        tx_ins = []
        prev_tx = unhexlify(
            '0025bc3c0fa8b7eb55b9437fdbd016870d18e0df0ace7bc9864efc38414147c8')
        tx_ins.append(
            TxIn(
                prev_tx=prev_tx,
                prev_index=0,
                script_sig=b'',
                sequence=0xffffffff,
            ))
        tx_ins[0]._value = 110000000
        tx_ins[0]._script_pubkey = Script.parse(
            private_key.point.p2pkh_script())
        tx_outs = []
        h160 = Tx.get_address_data(
            'mzx5YhAH9kNHtcN481u6WkjeHjYtVeKVh2')['h160']
        tx_outs.append(
            TxOut(amount=int(0.99 * 100000000),
                  script_pubkey=p2pkh_script(h160)))
        h160 = Tx.get_address_data(
            'mnrVtF8DWjMu839VW3rBfgYaAfKk8983Xf')['h160']
        tx_outs.append(
            TxOut(amount=int(0.1 * 100000000),
                  script_pubkey=p2pkh_script(h160)))

        tx = Tx(
            version=1,
            tx_ins=tx_ins,
            tx_outs=tx_outs,
            locktime=0,
            testnet=True,
        )
        self.assertTrue(tx.sign_input(0, private_key, SIGHASH_ALL))
コード例 #7
0
 def test_exercise_3_1(self):
     prev_tx = bytes.fromhex(
         'eb581753a4dbd6befeaaaa28a6f4576698ba13a07c03da693a65bce11cf9887a')
     prev_index = 1
     target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv'
     target_amount = 0.04
     change_address = 'mzx5YhAH9kNHtcN481u6WkjeHjYtVeKVh2'
     fee = 50000
     secret = 8675309
     private_key = PrivateKey(secret=secret)
     tx_ins = []
     tx_ins.append(TxIn(prev_tx, prev_index))
     tx_outs = []
     h160 = decode_base58(target_address)
     script_pubkey = p2pkh_script(h160)
     target_satoshis = int(target_amount * 100000000)
     tx_outs.append(TxOut(target_satoshis, script_pubkey))
     h160 = decode_base58(change_address)
     script_pubkey = p2pkh_script(h160)
     prev_amount = tx_ins[0].value(testnet=True)
     change_satoshis = prev_amount - target_satoshis - fee
     tx_outs.append(TxOut(change_satoshis, script_pubkey))
     tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True)
     tx_obj.sign_input(0, private_key)
     if private_key.point.address(testnet=True) != change_address:
         raise RuntimeError(
             'Private Key does not correspond to Change Address, check priv_key and change_address'
         )
     if tx_ins[0].script_pubkey(
             testnet=True).instructions[2] != decode_base58(change_address):
         raise RuntimeError(
             'Output is not something you can spend with this private key. Check that the prev_tx and prev_index are correct'
         )
     if tx_obj.fee() > 0.05 * 100000000 or tx_obj.fee() <= 0:
         raise RuntimeError(
             'Check that the change amount is reasonable. Fee is {}'.format(
                 tx_obj.fee()))
     self.assertEqual(
         tx_obj.serialize().hex(),
         '01000000017a88f91ce1bc653a69da037ca013ba986657f4a628aaaafebed6dba4531758eb010000006a47304402204ce6e3877ed2e18d2165276cbdba241507ce72b44d8df640eb6cb4d415eaaea002207dffd162da35593d86188ce87a1cbc9d3a5b26391870f19bf1764ca05b315ad9012103935581e52c354cd2f484fe8ed83af7a3097005b2f9c60bff71d35bd795f54b67ffffffff0200093d00000000001976a914ad346f8eb57dee9a37981716e498120ae80e44f788ac7077e401000000001976a914d52ad7ca9b3d096a38e752c2018e6fbc40cdf26f88ac00000000'
     )
コード例 #8
0
 def test_exercise_3_2(self):
     prev_tx_1 = bytes.fromhex(
         '89cbfe2eddaddf1eb11f5c4adf6adaa9bca4adc01b2a3d03f8dd36125c068af4')
     prev_index_1 = 0
     prev_tx_2 = bytes.fromhex(
         '19069e1304d95f70e03311d9d58ee821e0978e83ecfc47a30af7cd10fca55cf4')
     prev_index_2 = 0
     target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv'
     fee = 50000
     secret = 61740721216174072121
     private_key = PrivateKey(secret=secret)
     tx_ins = []
     tx_ins.append(TxIn(prev_tx_1, prev_index_1))
     tx_ins.append(TxIn(prev_tx_2, prev_index_2))
     tx_outs = []
     h160 = decode_base58(target_address)
     script_pubkey = p2pkh_script(h160)
     target_satoshis = tx_ins[0].value(True) + tx_ins[1].value(True) - fee
     tx_outs.append(TxOut(target_satoshis, script_pubkey))
     tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True)
     tx_obj.sign_input(0, private_key)
     tx_obj.sign_input(1, private_key)
     if tx_ins[0].script_pubkey(
             testnet=True).instructions[2] != decode_base58(
                 private_key.point.address(testnet=True)):
         raise RuntimeError(
             'Output is not something you can spend with this private key. Check that the prev_tx and prev_index are correct'
         )
     if tx_obj.fee() > 0.05 * 100000000 or tx_obj.fee() <= 0:
         raise RuntimeError(
             'Check that the change amount is reasonable. Fee is {}'.format(
                 tx_obj.fee()))
     self.assertEqual(
         tx_obj.serialize().hex(),
         '0100000002f48a065c1236ddf8033d2a1bc0ada4bca9da6adf4a5c1fb11edfaddd2efecb89000000006a47304402204b9ee431a2f5deaefb5282a34d7dcfdb47d55b1e3ce00cac4c6b6e6f0f0e8d58022062710e84786d2c6c89ddda5a149b45088b15230c6b825f0f21490f99bd74c81d012103f96f3a1efd31e1a8d7078118ee56bff7355d58907ce0f865f5f0b3dbe34e55befffffffff45ca5fc10cdf70aa347fcec838e97e021e88ed5d91133e0705fd904139e0619000000006a473044022073d7217b2d582e55978284c2628015a14e3490e835c76488eb29b63de15d17920220384e4b5282c911273efd4d98170e7092e10a729d142db17f4725c15364fa4ecc012103f96f3a1efd31e1a8d7078118ee56bff7355d58907ce0f865f5f0b3dbe34e55beffffffff01021f320a000000001976a914ad346f8eb57dee9a37981716e498120ae80e44f788ac00000000'
     )
コード例 #9
0
ファイル: wallet.py プロジェクト: justinmoon/old_wallets
    def send(self, adddress, amount, fee=500):
        # collect inputs
        unspent = self.unspent()
        tx_ins = []
        input_sum = 0
        for utxo in unspent:
            if input_sum >= amount + fee:
                break
            input_sum += utxo.amount
            tx_in = TxIn(utxo.tx_id, utxo.index)
            tx_ins.append(tx_in)

        # make sure we have enough
        assert input_sum > amount + fee, 'not enough satoshis'

        # send output
        send_h160 = decode_base58(address)
        send_script = p2pkh_script(send_h160)
        send_output = TxOut(amount=amount, script_pubkey=send_script)

        # change output
        change_amount = input_sum - amount - fee
        change_h160 = decode_base58(self.address())
        change_script = p2pkh_script(change_h160)
        change_output = TxOut(amount=amount, script_pubkey=change_script)

        # construct
        tx = Tx(1, tx_ins, [send_output, change_output], 0, True)

        # sign
        for i in range(len(tx_ins)):
            utxo = unspent[i]
            assert tx.sign_input(i, self.private_key, utxo.script_pubkey)
            print(f'signed {i}')
        print(tx)

        # broadcast
        import bit
        tx_hex = tx.serialize().hex()
        print(tx_hex)
        # raises a ConnectionError if it fails
        print(bit.network.NetworkAPI.broadcast_tx_testnet(tx_hex))
コード例 #10
0
ファイル: keypool.py プロジェクト: justinmoon/old_wallets
def spend(keypool, send_amount, fee=500):
    # collect inputs
    unspent = keypool.unspent()
    tx_ins = []
    input_sum = 0
    for private_key, utxo in unspent:
        if input_sum >= send_amount + fee:
            break
        input_sum += utxo.amount
        tx_in = TxIn(utxo.tx_id, utxo.index)
        tx_ins.append(tx_in)

    # make sure we have enough
    assert input_sum > send_amount + fee

    # outputs
    send_output = construct_tx_out(keypool.next_address(), send_amount)
    change_amount = input_sum - send_amount - fee
    change_output = construct_tx_out(keypool.next_address(), change_amount)

    # construct
    tx = Tx(1, tx_ins, [send_output, change_output], 0, True)

    # sign
    # FIXME
    for i in range(len(tx_ins)):
        private_key, utxo = unspent[i]
        assert tx.sign_input(i, private_key, utxo.script_pubkey)
        print(f'signed {i}')
    print(tx)

    # broadcast
    import bit
    tx_hex = tx.serialize().hex()
    print(tx_hex)
    # raises a ConnectionError if it fails
    print(bit.network.NetworkAPI.broadcast_tx_testnet(tx_hex))
コード例 #11
0
change_satoshis = int(change_amount * 100000000)
# create a new tx output for target with amount and script_pubkey
tx_outs.append(TxOut(
    amount=change_satoshis,
    script_pubkey=script_pubkey,
))

# create the transaction
tx_obj = Tx(version=1,
            tx_ins=tx_ins,
            tx_outs=tx_outs,
            locktime=0,
            testnet=True)

# now sign the 0th input with the private key using SIGHASH_ALL using sign_input
tx_obj.sign_input(0, priv1, SIGHASH_ALL)

# SANITY CHECK: change address corresponds to private key
if priv1.point.address(testnet=True) != change_address:
    raise RuntimeError(
        'Private Key does not correspond to Change Address, check priv_key and change_address'
    )

# SANITY CHECK: output's script_pubkey is the same one as your address
if tx_ins[0].script_pubkey(
        testnet=True).elements[2] != decode_base58(change_address):
    raise RuntimeError(
        'Output is not something you can spend with this private key. Check that the prev_tx and prev_index are correct'
    )

# SANITY CHECK: fee is reasonable
コード例 #12
0
    'b685879f3939fa9531899b729394ed85edab8b6850b72a9e378ea31af96a304a')
index_tx_previa = 1

tx_inputs = []
tx_inputs.append(TxIn(tx_previa, index_tx_previa))

#OUPUTS
tx_outputs = []
destinatario = 'mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB'
monto = .00008
change_adress = ADDRESS
cambio = .00001
#SEND OUTPUT
h160 = decode_base58(destinatario)
script_pubkey = p2pkh_script(h160)
monto_satoshis = int(monto * 100_000_000)
tx_outputs.append(TxOut(monto_satoshis, script_pubkey))

#CHANGE OUPUT
h160 = decode_base58(change_adress)
script_pubkey = p2pkh_script(h160)
cambio_satoshis = int(cambio * 100_000_000)
tx_outputs.append(TxOut(cambio_satoshis, script_pubkey))

#TX BUILD
TX_BUILD = Tx(1, tx_inputs, tx_outputs, 0, testnet=True)

#SIGN
print(TX_BUILD.sign_input(0, PrivKey))
print(TX_BUILD.serialize().hex())
コード例 #13
0
ファイル: run_file2.py プロジェクト: TKChattoraj/btc_app
output_address = pay_private_key.point.address(testnet=True)
# out1_address resulting from the above pay_secret
# out1_address = 'mgbxvp8L3o5zbw8P81VWVv42gUj9eZNjAN'

change_secret = 9883161471906162552674337436001522581212416354973578695250248738884382136882
#secret = random.getrandbits(256)
change_private_key = PrivateKey(change_secret)
change_address = change_private_key.point.address(testnet=True)

paying_amount = prev_tx_obj.tx_outs[0].amount
paid_amount = math.floor(paying_amount * .1)
fee = math.floor(paid_amount * .1)
change_amount = paying_amount - paid_amount - fee

paid_script = p2pkh_script(decode_base58(output_address))
change_script = p2pkh_script(decode_base58(change_address))

tx_out_paid = TxOut(paid_amount, paid_script)

tx_out_change = TxOut(change_amount, change_script)

tx = Tx(1, [txi], [tx_out_paid, tx_out_change], 0, True)

print(tx)

print(tx.sign_input(input_index=0, private_key=input_private_key))
print(tx)
print("tx_in script...")
print(tx.tx_ins[0].script_sig)
print(tx.serialize().hex())
コード例 #14
0
prev_tx = bytes.fromhex(
    "63fdb36b4ca0140ad343687ad8947a9d3adf9820383aefd9ae4d4c3521dde7dc")
prev_index = 1
target_address = "mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv"
target_amount = 0.01

change_address = "myxBsPLTwYQXJXbeausiiTHBXodDcX27mR"
change_amount = 0.009

passphrase = b'pickmeupimscared1419'
secret = little_endian_to_int(hash256(passphrase))
priv = PrivateKey(secret)

tx_ins = []
tx_ins.append(TxIn(prev_tx, prev_index))

tx_outs = []
h160 = decode_base58(target_address)
script_pubkey = p2pkh_script(h160)
target_satoshis = int(target_amount * 100_000_000)
tx_outs.append(TxOut(target_satoshis, script_pubkey))

h160 = decode_base58(change_address)
script_pubkey = p2pkh_script(h160)
change_satoshis = int(change_amount * 100_000_000)
tx_outs.append(TxOut(change_satoshis, script_pubkey))

tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True)
print(tx_obj.sign_input(0, priv))
print(tx_obj.serialize().hex())
from ecc import PrivateKey
from helper import hash256, little_endian_to_int, decode_base58, SIGHASH_ALL
from script import p2pkh_script, Script
from tx import TxIn, TxOut, Tx

secret = little_endian_to_int(hash256(b'my first secret key'))
private_key = PrivateKey(secret)
print(private_key.point.address(testnet=True))

prev_tx = bytes.fromhex(
    '6e7657754799fe7a906b213aa57b6920ad2d660f349fcb52dd384568aaddc2ef')
prev_index = 1
tx_in = TxIn(prev_tx, prev_index)

tx_outs = []
change_amount = int(0.004 * 10000000)
change_h160 = decode_base58('mxUJg9m1dsqpHnbhV15mwJ3GjwPBVMHVmu')
change_script = p2pkh_script(change_h160)
change_output = TxOut(change_amount, change_script)

target_amount = int(0.007 * 10000000)
target_h160 = decode_base58('mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv')
target_script = p2pkh_script(target_h160)
target_output = TxOut(target_amount, target_script)

tx_obj = Tx(1, [tx_in], [change_output, target_output], 0, True)
print(tx_obj.sign_input(0, private_key))
print(tx_obj.serialize().hex())
コード例 #16
0
            # if our output has the same address as our address we found it
            if tx_out.script_pubkey.address(testnet=True) == addr:
                # we found our utxo. set prev_tx, prev_index, and transaction
                prev_tx = message.hash()
                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)
コード例 #17
0
tx_ins.append(TxIn(prev_tx_1, prev_index_1))
tx_ins.append(TxIn(prev_tx_2, prev_index_2))

# #out1_secret = random.getrandbits(256)
# #print("output secret:")
# #print(out1_secret)
# #out1_private_key = PrivateKey(out1_secret)
# #out1_address = out1_private_key.point.address(testnet=True)
# #print("output address:")

target_address = 'mqJ8CrnEEUbeMFQ628e99P3RAATbarrQwK'
target_amount = 499000

h160 = decode_base58(target_address)
script_pubkey = p2pkh_script(h160)
target_satoshis = int(target_amount)

tx_outs = []
tx_outs.append(TxOut(target_satoshis, script_pubkey))
tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True)
print(tx_obj.sign_input(0, priv1))

print(tx_obj.sign_input(1, priv2))

print(tx_obj.serialize().hex())

# Two Input Transaction  private keys 6775... and 1939...:
# 0100000002681fc41cbecc4d8d0b50d59531d7ddb7477d381569bbc671289eb39fbf37753b000000006b483045022100a75d4be72872c697fe7ea9154661c256f93329bcb95084746a0117fede161a9e02206a4773d1cc205fa5dda8a22fe43320ec33c3dbe79645379373536df5ee662b57012102370658d5af6a0cbcc3351f831b7159153c674fa0ff736e01509ceba5c5d14b9affffffffc14312c2749052498087e4e38848b9b95b730ca8584f1283dfedaf1a124fdb07000000006b483045022100a87ee9ae8b767ff11efd3abd5c756f1db577b0fb87ac4ba4974f70264bd76df502200a102d6d61cccf8b05348bd92e27931f20480d2559d02fbc635b3664ea311010012102d36c1120a73be7ab367a9b553db92cc2d66be861da5179b6e7072d8f2e2124c1ffffffff01389d0700000000001976a9146b441f868ae16c367afe74c136fb2da72377992488ac00000000

# One Input Transaction private key 6775
# 0100000001681fc41cbecc4d8d0b50d59531d7ddb7477d381569bbc671289eb39fbf37753b000000006b483045022100dd9299f3ea9bee021eaa82c14b8bc76f57135accf4160163eda8636c861d5c67022039cd7bda38d25d9989395ed5f3b36edb8fee9d19e5d3f16551aa7b229776afb8012102370658d5af6a0cbcc3351f831b7159153c674fa0ff736e01509ceba5c5d14b9affffffff01a8cc0300000000001976a9146b441f868ae16c367afe74c136fb2da72377992488ac00000000
コード例 #18
0
 def test_exercise_6(self):
     last_block_hex = '00000000000538d5c2246336644f9a4956551afb44ba47278759ec55ea912e19'
     secret = little_endian_to_int(
         hash256(b'Jimmy Song Programming Blockchain'))
     private_key = PrivateKey(secret=secret)
     addr = private_key.point.address(testnet=True)
     h160 = decode_base58(addr)
     target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv'
     self.assertEqual(addr, target_address)
     target_h160 = decode_base58(target_address)
     target_script = p2pkh_script(target_h160)
     fee = 5000
     node = SimpleNode('tbtc.programmingblockchain.com', testnet=True)
     bf = BloomFilter(30, 5, 90210)
     bf.add(h160)
     node.handshake()
     node.send(b'filterload', bf.filterload())
     start_block = bytes.fromhex(last_block_hex)
     getheaders_message = GetHeadersMessage(start_block=start_block)
     node.send(getheaders_message.command, getheaders_message.serialize())
     headers_envelope = node.wait_for_commands({HeadersMessage.command})
     stream = headers_envelope.stream()
     headers = HeadersMessage.parse(stream)
     last_block = None
     get_data_message = GetDataMessage()
     for b in headers.blocks:
         if not b.check_pow():
             raise RuntimeError('proof of work is invalid')
         if last_block is not None and b.prev_block != last_block:
             raise RuntimeError('chain broken')
         get_data_message.add_data(FILTERED_BLOCK_DATA_TYPE, b.hash())
         last_block = b.hash()
     node.send(get_data_message.command, get_data_message.serialize())
     prev_tx, prev_index, prev_tx_obj = None, None, None
     while prev_tx is None:
         envelope = node.wait_for_commands({b'merkleblock', b'tx'})
         stream = envelope.stream()
         if envelope.command == b'merkleblock':
             mb = MerkleBlock.parse(stream)
             if not mb.is_valid():
                 raise RuntimeError('invalid merkle proof')
         else:
             prev_tx_obj = Tx.parse(stream, testnet=True)
             for i, tx_out in enumerate(prev_tx_obj.tx_outs):
                 if tx_out.script_pubkey.address(testnet=True) == addr:
                     prev_tx = prev_tx_obj.hash()
                     prev_index = i
                     self.assertEqual(
                         prev_tx_obj.id(),
                         'e3930e1e566ca9b75d53b0eb9acb7607f547e1182d1d22bd4b661cfe18dcddf1'
                     )
                     self.assertEqual(i, 0)
     tx_in = TxIn(prev_tx, prev_index, Script([]), 0xffffff)
     TxFetcher.cache[prev_tx] = prev_tx_obj
     tx_ins = [tx_in]
     total = prev_tx_obj.tx_outs[prev_index].amount
     tx_outs = [TxOut(total - fee, target_script)]
     tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True)
     tx_obj.sign_input(0, private_key)
     self.assertEqual(
         tx_obj.serialize().hex(),
         '0100000001f1dddc18fe1c664bbd221d2d18e147f50776cb9aebb0535db7a96c561e0e93e3000000006a473044022046a49962540a89e83da0636455b6c81c11c2844b7f3cd414c02e1a13741f4d15022006eed4eeda994d2bfebb9f1a494bfa3c8bab96e7e4c82623f4a29736dfe309e70121021cdd761c7eb1c90c0af0a5963e94bf0203176b4662778d32bd6d7ab5d8628b32ffffff0001a1629ef5000000001976a914ad346f8eb57dee9a37981716e498120ae80e44f788ac00000000'
     )