Define a protocol function for decoding object parameters outside of network

This commit is contained in:
Lee Miller 2023-10-12 03:35:44 +03:00
parent a39fc2cff5
commit 9747f65884
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
3 changed files with 30 additions and 1 deletions

View File

@ -436,6 +436,17 @@ def assembleErrorMessage(fatal=0, banTime=0, inventoryVector='', errorText=''):
# Packet decoding
def decodeObjectParameters(data):
"""Decode the parameters of a raw object needed to put it in inventory"""
# BMProto.decode_payload_content("QQIvv")
expiresTime = unpack('>Q', data[8:16])[0]
objectType = unpack('>I', data[16:20])[0]
parserPos = 20 + decodeVarint(data[20:30])[1]
toStreamNumber = decodeVarint(data[parserPos:parserPos + 10])[0]
return objectType, toStreamNumber, expiresTime
def decryptAndCheckPubkeyPayload(data, address):
"""
Version 4 pubkeys are encrypted. This function is run when we

View File

@ -54,3 +54,12 @@ sample_subscription_addresses = [
'BM-2cWQLCBGorT9pUGkYSuGGVr9LzE4mRnQaq',
'BM-GtovgYdgs7qXPkoYaRgrLFuFKz1SFpsw']
sample_subscription_name = 'test sub'
sample_object_expires = 1712271487
# from minode import structure
# obj = structure.Object(
# b'\x00' * 8, sample_object_expires, 42, 1, 2, b'HELLO')
# .. do pow and obj.to_bytes()
sample_object_data = unhexlify(
'00000000001be7fc00000000660f307f0000002a010248454c4c4f')

View File

@ -5,7 +5,8 @@ from struct import pack
from pybitmessage import addresses, protocol
from .samples import sample_addr_data
from .samples import (
sample_addr_data, sample_object_data, sample_object_expires)
from .test_protocol import TestSocketInet
@ -49,6 +50,14 @@ class TestSerialize(TestSocketInet):
self.assertEqual(
protocol.CreatePacket(b'ping')[:len(head)], head)
def test_decode_obj_parameters(self):
"""Check parameters decoded from a sample object"""
objectType, toStreamNumber, expiresTime = \
protocol.decodeObjectParameters(sample_object_data)
self.assertEqual(objectType, 42)
self.assertEqual(toStreamNumber, 2)
self.assertEqual(expiresTime, sample_object_expires)
def test_encodehost(self):
"""Check the result of protocol.encodeHost()"""
self.assertEqual(