예제 #1
0
 def filterload(self, flag=1):
     payload = encode_varint(self.size)
     payload += self.filter_bytes()
     payload += int_to_little_endian(self.function_count, 4)
     payload += int_to_little_endian(self.tweak, 4)
     payload += int_to_little_endian(flag, 1)
     return GenericMessage(b'filterload', payload)
 def serialize(self):
     '''Returns the 80 byte block header'''
     # version - 4 bytes, little endian
     result = int_to_little_endian(self.version, 4)
     # prev_block - 32 bytes, little endian
     result += self.prev_block[::-1]
     # merkle_root - 32 bytes, little endian
     result += self.merkle_root[::-1]
     # timestamp - 4 bytes, little endian
     result += int_to_little_endian(self.timestamp, 4)
     # bits - 4 bytes
     result += self.bits
     # nonce - 4 bytes
     result += self.nonce
     return result
예제 #3
0
 def serialize(self):
     result = self.magic
     result += self.command + b'\x00' * (12 - len(self.command))
     result += int_to_little_endian(len(self.payload), 4)
     result += hash256(self.payload)[:4]
     result += self.payload
     return result
 def __init__(self,
              version=70015,
              services=0,
              timestamp=None,
              receiver_services=0,
              receiver_ip=b'\x00\x00\x00\x00',
              receiver_port=8333,
              sender_services=0,
              sender_ip=b'\x00\x00\x00\x00',
              sender_port=8333,
              nonce=None,
              user_agent=b'/programmingbitcoin:0.1/',
              latest_block=0,
              relay=False):
     self.version = version  # 4 bytes, little-endian, 70015
     self.services = services  # 8 bytes, little-endian
     if timestamp is None:  # 8 bytes, little-endian
         self.timestamp = int(time.time())
     else:
         self.timestamp = timestamp
     self.receiver_services = receiver_services  # 8 bytes, little-endian
     self.receiver_ip = receiver_ip  # 16 bytes
     self.receiver_port = receiver_port  # 2 bytes
     self.sender_services = sender_services  # 8 bytes, little-endian
     self.sender_ip = sender_ip  # 16 bytes
     self.sender_port = sender_port  # 2 bytes
     if nonce is None:  # 8 bytes
         self.nonce = int_to_little_endian(randint(0, 2**64), 8)
     else:
         self.nonce = nonce
     self.user_agent = user_agent
     self.latest_block = latest_block  # current block height
     self.relay = relay  # for bloom filter
예제 #5
0
def alice_swap_tx():
    prev_tx, prev_index, prev_amount, prev_output_script = None, None, None, None
    url_addr_history = '{}/addrs/{}/full'.format(
        TxFetcher.get_url(testnet=True), alice_addr_btc)
    response_addr_history = requests.get(url_addr_history)
    response_addr_history_binary = response_addr_history.content
    addr_history_json = json.loads(response_addr_history_binary)
    # since a tx has multiple outputs to cover the pay_amount, we only consider the output that has sufficient coins
    if addr_history_json['balance'] < pay_amount + fee:
        print('The balance of address %s is not enough to cover the payment',
              alice_addr_btc)
    for tx_info in addr_history_json['txs']:
        if prev_tx is not None:
            break
        for i, output in enumerate(tx_info['outputs']):
            if output['value'] >= pay_amount + fee and output['addresses'][
                    0] == alice_addr_btc:
                prev_tx = tx_info['hash']
                prev_index = i
                prev_amount = output['value']
                break

    hash_secret = hash_of_secret()
    tx_in = TxIn(bytes.fromhex(prev_tx), prev_index)

    change_amount = prev_amount - pay_amount - fee
    change_h160 = decode_base58(alice_addr_btc)
    change_script = p2pkh_script(change_h160)
    change_output = TxOut(change_amount, change_script)

    target_address = bob_addr_btc
    target_h160 = decode_base58(target_address)
    target_output_script = atomic_swap_output_script(
        int_to_little_endian(len_alice_secret, 1), hash_secret, change_h160,
        target_h160, int_to_little_endian(alice_locktime, 4))
    target_output = TxOut(pay_amount, target_output_script)

    tx_out = [change_output, target_output]

    # tx_obj = Tx(1, [tx_in], tx_out, int(time.time()), testnet=True) # set nLocktime to present time
    tx_obj = Tx(1, [tx_in], tx_out, 0,
                testnet=True)  # set nLocktime to present time
    tx_obj.sign_input(0, alice_private_key)

    print('Alice swap tx (BTC) created successfully!, TX ID: ', tx_obj.id())
    return tx_obj
 def raw_serialize(self):
     # initialize what we'll send back
     result = b''
     # go through each cmd
     for cmd in self.cmds:
         # if the cmd is an integer, it's an opcode
         if type(cmd) == int:
             # turn the cmd into a single byte integer using int_to_little_endian
             result += int_to_little_endian(cmd, 1)
         else:
             # otherwise, this is an element
             # get the length in bytes
             length = len(cmd)
             # for large lengths, we have to use a pushdata opcode
             if length < 75:
                 # turn the length into a single byte integer
                 result += int_to_little_endian(length, 1)
             elif length > 75 and length < 0x100:
                 # 76 is pushdata1
                 result += int_to_little_endian(76, 1)
                 result += int_to_little_endian(length, 1)
             elif length >= 0x100 and length <= 520:
                 # 77 is pushdata2
                 result += int_to_little_endian(77, 1)
                 result += int_to_little_endian(length, 2)
             else:
                 raise ValueError('too long an cmd')
             result += cmd
     return result
 def serialize(self):
     '''Serialize this message to send over the network'''
     # version is 4 bytes little endian
     result = int_to_little_endian(self.version, 4)
     # services is 8 bytes little endian
     result += int_to_little_endian(self.services, 8)
     # timestamp is 8 bytes little endian
     result += int_to_little_endian(self.timestamp, 8)
     # receiver services is 8 bytes little endian
     result += int_to_little_endian(self.receiver_services, 8)
     # IPV4 is 10 00 bytes and 2 ff bytes then receiver ip
     result += b'\x00' * 10 + b'\xff\xff' + self.receiver_ip
     # receiver port is 2 bytes, big endian
     result += self.receiver_port.to_bytes(2, 'big')
     # sender services is 8 bytes little endian
     result += int_to_little_endian(self.sender_services, 8)
     # IPV4 is 10 00 bytes and 2 ff bytes then sender ip
     result += b'\x00' * 10 + b'\xff\xff' + self.sender_ip
     # sender port is 2 bytes, big endian
     result += self.sender_port.to_bytes(2, 'big')
     # nonce should be 8 bytes
     result += self.nonce
     # useragent is a variable string, so varint first
     result += encode_varint(len(self.user_agent))
     result += self.user_agent
     # latest block is 4 bytes little endian
     result += int_to_little_endian(self.latest_block, 4)
     # relay is 00 if false, 01 if true
     if self.relay:
         result += b'\x01'
     else:
         result += b'\x00'
     return result
 def serialize(self):
     result = encode_varint(len(self.data))
     for data_type, identifier in self.data:
         result += int_to_little_endian(data_type, 4)
         result += identifier[::-1]
     return result
 def serialize(self):
     result = int_to_little_endian(self.version, 4)
     result += encode_varint(self.num_hashes)
     result += self.start_block[::-1]
     result += self.end_block[::-1]
     return result