Esempio n. 1
0
def parse_TxIn(vds):
  d = {}
  d['prevout_hash'] = hash_encode(vds.read_bytes(32))
  d['prevout_n'] = vds.read_uint32()
  scriptSig = vds.read_bytes(vds.read_compact_size())
  d['sequence'] = vds.read_uint32()
  d['address'] = get_address_from_input_script(scriptSig)
  return d
Esempio n. 2
0
 def get_hash(self):
     vds = BCDataStream()
     vds.write_string(self.proposal_name)
     vds.write_string(self.proposal_url)
     vds.write_int32(self.start_block)
     vds.write_int32(self.end_block)
     vds.write_int64(self.payment_amount)
     vds.write_string(Transaction.pay_script(bitcoin.TYPE_ADDRESS, self.address).decode('hex'))
     return bitcoin.hash_encode(bitcoin.Hash(vds.input))
Esempio n. 3
0
def parse_TxIn(vds):
  d = {}
  d['prevout_hash'] = hash_encode(vds.read_bytes(32))
  d['prevout_n'] = vds.read_uint32()
  scriptSig = vds.read_bytes(vds.read_compact_size())
  d['sequence'] = vds.read_uint32()
  d['address'] = extract_public_key(scriptSig)
  #d['script'] = decode_script(scriptSig)
  return d
Esempio n. 4
0
    def serialize_for_sig(self, update_time=False):
        """Serialize the message for signing."""
        if update_time:
            self.sig_time = int(time.time())

        s = str(self.addr)
        s += str(self.sig_time)

        if self.protocol_version < 70201:
            # Decode the hex-encoded bytes for our keys.
            s += self.collateral_key.decode('hex')
            s += self.delegate_key.decode('hex')
        else:
            # Use the RIPEMD-160 hashes of our keys.
            s += bitcoin.hash_encode(bitcoin.hash_160(self.collateral_key.decode('hex')))
            s += bitcoin.hash_encode(bitcoin.hash_160(self.delegate_key.decode('hex')))

        s += str(self.protocol_version)
        return s
Esempio n. 5
0
def parse_TxIn(vds):
    d = {}
    d['prevout_hash'] = hash_encode(vds.read_bytes(32))
    d['prevout_n'] = vds.read_uint32()
    scriptSig = vds.read_bytes(vds.read_compact_size())
    d['sequence'] = vds.read_uint32()

    if scriptSig:
        pubkeys, signatures, address = get_address_from_input_script(scriptSig)
    else:
        pubkeys = []
        signatures = []
        address = None

    d['address'] = address
    d['signatures'] = signatures

    return d
Esempio n. 6
0
def parse_TxIn(vds):
    d = {}
    d['prevout_hash'] = hash_encode(vds.read_bytes(32))
    d['prevout_n'] = vds.read_uint32()
    scriptSig = vds.read_bytes(vds.read_compact_size())
    d['sequence'] = vds.read_uint32()

    if scriptSig:
        pubkeys, signatures, address = get_address_from_input_script(scriptSig)
    else:
        pubkeys = []
        signatures = []
        address = None
    
    d['address'] = address
    d['signatures'] = signatures

    return d
Esempio n. 7
0
 def tx_response(self, response):
     params, result = self.parse_response(response)
     if not params:
         return
     tx_hash, tx_height = params
     assert tx_hash == hash_encode(sha256(result.decode('hex')))
     tx = Transaction(result)
     try:
         tx.deserialize()
     except Exception:
         self.print_msg("cannot deserialize transaction, skipping", tx_hash)
         return
     self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
     self.requested_tx.remove((tx_hash, tx_height))
     self.print_error("received tx:", tx_hash, len(tx.raw))
     # callbacks
     self.network.trigger_callback('new_transaction', (tx,))
     if not self.requested_tx:
         self.network.trigger_callback('updated')
Esempio n. 8
0
 def tx_response(self, response):
     params, result = self.parse_response(response)
     if not params:
         return
     tx_hash, tx_height = params
     assert tx_hash == hash_encode(Hash(result.decode('hex')))
     tx = Transaction(result)
     try:
         tx.deserialize()
     except Exception:
         self.print_msg("cannot deserialize transaction, skipping", tx_hash)
         return
     self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
     self.requested_tx.remove((tx_hash, tx_height))
     self.print_error("received tx:", tx_hash, len(tx.raw))
     # callbacks
     self.network.trigger_callback('new_transaction', (tx, ))
     if not self.requested_tx:
         self.network.trigger_callback('updated')
Esempio n. 9
0
    def tx_response(self, response):
        params, result = self.parse_response(response)
        if not params:
            return
        tx_hash, tx_height = params
        assert tx_hash == hash_encode(Hash(result.decode('hex')))
        tx = Transaction(result)
        try:
            tx.deserialize()
        except Exception:
            self.print_msg("cannot deserialize transaction, skipping", tx_hash)
            return

        self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
        self.requested_tx.remove((tx_hash, tx_height))
        self.print_error("received tx:", tx_hash, len(tx.raw))
        if not self.requested_tx:
            self.network.trigger_callback('updated')
            # Updated gets called too many times from other places as
            # well; if we used that signal we get the notification
            # three times
            self.network.trigger_callback("new_transaction")
Esempio n. 10
0
    def tx_response(self, response):
        params, result = self.parse_response(response)
        if not params:
            return
        tx_hash, tx_height = params
        assert tx_hash == hash_encode(Hash(result.decode('hex')))
        tx = Transaction(result)
        try:
            tx.deserialize()
        except Exception:
            self.print_msg("cannot deserialize transaction, skipping", tx_hash)
            return

        self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
        self.requested_tx.remove((tx_hash, tx_height))
        self.print_error("received tx:", tx_hash, len(tx.raw))
        if not self.requested_tx:
            self.network.trigger_callback('updated')
            # Updated gets called too many times from other places as
            # well; if we used that signal we get the notification
            # three times
            self.network.trigger_callback("new_transaction")
Esempio n. 11
0
 def deserialize(cls, vds):
     vin = parse_input(vds)
     block_hash = hash_encode(vds.read_bytes(32))
     sig_time = vds.read_int64()
     sig = vds.read_bytes(vds.read_compact_size())
     return cls(vin=vin, block_hash=block_hash, sig_time=sig_time, sig=sig)
Esempio n. 12
0
 def get_hash(self):
     vds = BCDataStream()
     serialize_input(vds, self.vin)
     vds.write_string(self.collateral_key.decode('hex'))
     vds.write_int64(self.sig_time)
     return hash_encode(bitcoin.Hash(vds.input))
Esempio n. 13
0
    def run_interface(self):
        #print_error("synchronizer: connected to", self.network.get_parameters())

        requested_tx = []
        missing_tx = []
        requested_histories = {}

        # request any missing transactions
        for history in self.wallet.history.values():
            if history == ['*']: continue
            for tx_hash, tx_height in history:
                if self.wallet.transactions.get(tx_hash) is None and (tx_hash, tx_height) not in missing_tx:
                    missing_tx.append((tx_hash, tx_height))

        if missing_tx:
            print_error("missing tx", missing_tx)

        # subscriptions
        self.subscribe_to_addresses(self.wallet.addresses(True))

        while self.is_running():

            # 1. create new addresses
            self.wallet.synchronize()

            # request missing addresses
            new_addresses = []
            while True:
                try:
                    addr = self.address_queue.get(block=False)
                except Queue.Empty:
                    break
                new_addresses.append(addr)
            if new_addresses:
                self.subscribe_to_addresses(new_addresses)

            # request missing transactions
            for tx_hash, tx_height in missing_tx:
                if (tx_hash, tx_height) not in requested_tx:
                    self.network.send([('blockchain.transaction.get', [tx_hash, tx_height])], self.queue.put)
                    requested_tx.append((tx_hash, tx_height))
            missing_tx = []

            # detect if situation has changed
            if self.network.is_up_to_date() and self.queue.empty():
                if not self.wallet.is_up_to_date():
                    self.wallet.set_up_to_date(True)
                    self.was_updated = True
            else:
                if self.wallet.is_up_to_date():
                    self.wallet.set_up_to_date(False)
                    self.was_updated = True

            if self.was_updated:
                self.network.trigger_callback('updated')
                self.was_updated = False

            # 2. get a response
            try:
                r = self.queue.get(timeout=0.1)
            except Queue.Empty:
                continue

            # 3. process response
            method = r['method']
            params = r['params']
            result = r.get('result')
            error = r.get('error')
            if error:
                print_error("error", r)
                continue

            if method == 'blockchain.address.subscribe':
                addr = params[0]
                if self.wallet.get_status(self.wallet.get_history(addr)) != result:
                    if requested_histories.get(addr) is None:
                        self.network.send([('blockchain.address.get_history', [addr])], self.queue.put)
                        requested_histories[addr] = result

            elif method == 'blockchain.address.get_history':
                addr = params[0]
                print_error("receiving history", addr, result)
                if result == ['*']:
                    assert requested_histories.pop(addr) == '*'
                    self.wallet.receive_history_callback(addr, result)
                else:
                    hist = []
                    # check that txids are unique
                    txids = []
                    for item in result:
                        tx_hash = item['tx_hash']
                        if tx_hash not in txids:
                            txids.append(tx_hash)
                            hist.append((tx_hash, item['height']))

                    if len(hist) != len(result):
                        raise Exception("error: server sent history with non-unique txid", result)

                    # check that the status corresponds to what was announced
                    rs = requested_histories.pop(addr)
                    if self.wallet.get_status(hist) != rs:
                        raise Exception("error: status mismatch: %s" % addr)
                
                    # store received history
                    self.wallet.receive_history_callback(addr, hist)

                    # request transactions that we don't have 
                    for tx_hash, tx_height in hist:
                        if self.wallet.transactions.get(tx_hash) is None:
                            if (tx_hash, tx_height) not in requested_tx and (tx_hash, tx_height) not in missing_tx:
                                missing_tx.append((tx_hash, tx_height))

            elif method == 'blockchain.transaction.get':
                tx_hash = params[0]
                tx_height = params[1]
                assert tx_hash == bitcoin.hash_encode(bitcoin.Hash(result.decode('hex')))
                tx = Transaction.deserialize(result)
                self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
                self.was_updated = True
                requested_tx.remove((tx_hash, tx_height))
                print_error("received tx:", tx_hash, len(tx.raw))

            else:
                print_error("Error: Unknown message:" + method + ", " + repr(params) + ", " + repr(result))

            if self.was_updated and not requested_tx:
                self.network.trigger_callback('updated')
                # Updated gets called too many times from other places as well.
                # If we use that signal we get the notification three times.
                self.network.trigger_callback("new_transaction") 
                self.was_updated = False