From 9747f65884cf8745a9d277a682b75e0be40d747a Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Thu, 12 Oct 2023 03:35:44 +0300 Subject: [PATCH] Define a protocol function for decoding object parameters outside of network --- src/protocol.py | 11 +++++++++++ src/tests/samples.py | 9 +++++++++ src/tests/test_packets.py | 11 ++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/protocol.py b/src/protocol.py index f5aa2750..722ce9c1 100644 --- a/src/protocol.py +++ b/src/protocol.py @@ -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 diff --git a/src/tests/samples.py b/src/tests/samples.py index aa4bac9e..93e5ed61 100644 --- a/src/tests/samples.py +++ b/src/tests/samples.py @@ -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') diff --git a/src/tests/test_packets.py b/src/tests/test_packets.py index f030912a..9dfb1d23 100644 --- a/src/tests/test_packets.py +++ b/src/tests/test_packets.py @@ -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(