Пример #1
0
    def get_key(self, compressed=None):
        """Get the hex-encoded key.

        :param compressed: False if you want a standard 65 Byte key (the most
            standard option). True if you want the compressed 33 Byte form.
            Defaults to None, which in turn uses the self.compressed attribute.
        :type compressed: bool

        PublicKeys consist of an ID byte, the x, and the y coordinates
        on the elliptic curve.

        In the case of uncompressed keys, the ID byte is 04.
        Compressed keys use the SEC1 format:
            If Y is odd: id_byte = 03
            else: id_byte = 02

        Note that I pieced this algorithm together from the pycoin source.

        This is documented in http://www.secg.org/collateral/sec1_final.pdf
        but, honestly, it's pretty confusing.

        I guess this is a pretty big warning that I'm not *positive* this
        will do the right thing in all cases. The tests pass, and this does
        exactly what pycoin does, but I'm not positive pycoin works either!
        """
        if compressed is None:
            compressed = self.compressed
        if compressed:
            parity = 2 + (self.y & 1)  # 0x02 even, 0x03 odd
            return ensure_bytes(
                long_to_hex(parity, 2) + long_to_hex(self.x, 64))
        else:
            return ensure_bytes(b'04' + long_to_hex(self.x, 64) +
                                long_to_hex(self.y, 64))
Пример #2
0
 def _random_key_in_bucket_range(self, bucket_index):
     """
     Returns a random key in the specified bucket's range.
     """
     keyValue = random.randrange(self._buckets[bucket_index].range_min,
                                 self._buckets[bucket_index].range_max)
     return long_to_hex(keyValue)
Пример #3
0
 def __init__(self, network_id, ip_address, port, version, last_seen=0):
     """
     Initialise the peer node with a unique id within the network, IP
     address, port, the p4p2p version the contact is running and a
     timestamp indicating when the last connection was made with the
     contact (defaults to 0). The network id, if passed in as a numeric
     value, will be converted into a hexadecimal string.
     """
     if isinstance(network_id, long) or isinstance(network_id, int):
         self.network_id = long_to_hex(network_id)
     else:
         self.network_id = network_id
     self.ip_address = ip_address
     self.port = port
     self.version = version
     self.last_seen = last_seen
     # failed_RPCs keeps track of the number of failed RPCs to this peer.
     # If this number reaches a threshold then it is evicted from a
     # bucket and replaced with another node that is more reliable.
     self.failed_RPCs = 0