Esempio n. 1
0
def create_and_sign_routed_peer_message(routed_msg_body, target_node,
                                        my_key_pair_nacl):
    routed_msg = RoutedMessage()
    routed_msg.target = PeerIdOrHash()
    routed_msg.target.enum = 'PeerId'
    routed_msg.target.PeerId = PublicKey()
    routed_msg.target.PeerId.keyType = 0
    routed_msg.target.PeerId.data = base58.b58decode(
        target_node.node_key.pk[len(ED_PREFIX):])
    routed_msg.author = PublicKey()
    routed_msg.author.keyType = 0
    routed_msg.author.data = bytes(my_key_pair_nacl.verify_key)
    routed_msg.ttl = 100
    routed_msg.body = routed_msg_body
    routed_msg.signature = Signature()
    routed_msg.signature.keyType = 0

    routed_msg_arr = bytes(
        bytearray([0, 0]) + routed_msg.target.PeerId.data + bytearray([0]) +
        routed_msg.author.data +
        BinarySerializer(schema).serialize(routed_msg.body))
    routed_msg_hash = hashlib.sha256(routed_msg_arr).digest()
    routed_msg.signature.data = my_key_pair_nacl.sign(
        routed_msg_hash).signature

    peer_message = PeerMessage()
    peer_message.enum = 'Routed'
    peer_message.Routed = routed_msg

    return peer_message
Esempio n. 2
0
def create_sync_data(accounts=[], edges=[]):
    sync_data = SyncData()
    sync_data.accounts = accounts
    sync_data.edges = edges

    peer_message = PeerMessage()
    peer_message.enum = 'Sync'
    peer_message.Sync = sync_data
    return peer_message
Esempio n. 3
0
def create_handshake(my_key_pair_nacl,
                     their_pk_serialized,
                     listen_port,
                     version=0):
    """
    Create handshake message but with placeholders in:
        - version
        - genesis_id.chain_id
        - genesis_id.hash
        - edge_info.signature
    """
    handshake = Handshake()
    handshake.version = version
    handshake.oldest_supported_version = version
    handshake.peer_id = PublicKey()
    handshake.target_peer_id = PublicKey()
    handshake.listen_port = listen_port
    handshake.chain_info = PeerChainInfoV2()
    handshake.edge_info = EdgeInfo()

    handshake.peer_id.keyType = 0
    handshake.peer_id.data = bytes(my_key_pair_nacl.verify_key)

    handshake.target_peer_id.keyType = 0
    handshake.target_peer_id.data = base58.b58decode(
        their_pk_serialized[len(ED_PREFIX):])

    handshake.chain_info.genesis_id = GenesisId()
    handshake.chain_info.height = 0
    handshake.chain_info.tracked_shards = []
    handshake.chain_info.archival = False

    handshake.chain_info.genesis_id.chain_id = 'moo'
    handshake.chain_info.genesis_id.hash = bytes([0] * 32)

    handshake.edge_info.nonce = 1
    handshake.edge_info.signature = Signature()

    handshake.edge_info.signature.keyType = 0
    handshake.edge_info.signature.data = bytes([0] * 64)

    peer_message = PeerMessage()
    peer_message.enum = 'Handshake'
    peer_message.Handshake = handshake

    return peer_message
Esempio n. 4
0
def create_peer_request():
    peer_message = PeerMessage()
    peer_message.enum = 'PeersRequest'
    peer_message.PeersRequest = ()
    return peer_message