From f45b9723f73e05ea80060870f6e41e737fb52a1a Mon Sep 17 00:00:00 2001 From: Philip Magyar Date: Wed, 20 Nov 2024 09:04:05 +0100 Subject: [PATCH 1/4] fix confidance/horizontal acc, add mac address --- findmy/keys.py | 8 +++++++- findmy/reports/reports.py | 14 +++++++++++--- findmy/util/parsers.py | 3 +++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/findmy/keys.py b/findmy/keys.py index e0febc1..1ebc81d 100644 --- a/findmy/keys.py +++ b/findmy/keys.py @@ -12,7 +12,7 @@ from typing import Generator, Generic, TypeVar, overload from cryptography.hazmat.primitives.asymmetric import ec from typing_extensions import override -from .util import crypto +from .util import crypto, parsers class KeyType(Enum): @@ -70,6 +70,12 @@ class HasPublicKey(HasHashedPublicKey, ABC): def adv_key_b64(self) -> str: """Return the advertised (public) key as a base64-encoded string.""" return base64.b64encode(self.adv_key_bytes).decode("ascii") + + @property + def mac_address(self) -> str: + """Get the mac address from the public key.""" + first_hex = self.adv_key_bytes[0] | 0b11000000 + return parsers.format_hex_byte(first_hex) + ":" + ":".join([parsers.format_hex_byte(x) for x in self.adv_key_bytes[1:6]]) @property @override diff --git a/findmy/reports/reports.py b/findmy/reports/reports.py index 3f17be3..0403839 100644 --- a/findmy/reports/reports.py +++ b/findmy/reports/reports.py @@ -121,6 +121,14 @@ class LocationReport(HasHashedPublicKey): """The `datetime` when this report was recorded by a device.""" timestamp_int = int.from_bytes(self._payload[0:4], "big") + (60 * 60 * 24 * 11323) return datetime.fromtimestamp(timestamp_int, tz=timezone.utc).astimezone() + + @property + def confidence(self) -> int: + """Confidence of the location of this report. Int between 1 and 3""" + # If the payload length is 88, the confidence is the 5th byte, otherwise it's the 6th byte + if (len(self._payload) == 88): + return self.payload[4] + return self.payload[5] @property def latitude(self) -> float: @@ -145,10 +153,10 @@ class LocationReport(HasHashedPublicKey): return struct.unpack(">i", lon_bytes)[0] / 10000000 @property - def confidence(self) -> int: - """Confidence of the location of this report.""" + def horizontal_accuracy(self) -> int: + """Horizontal accuracy of the location of this report.""" if not self.is_decrypted: - msg = "Confidence is unavailable while the report is encrypted." + msg = "Horizontal accuracy is unavailable while the report is encrypted." raise RuntimeError(msg) assert self._decrypted_data is not None diff --git a/findmy/util/parsers.py b/findmy/util/parsers.py index 2f06769..563db93 100644 --- a/findmy/util/parsers.py +++ b/findmy/util/parsers.py @@ -15,3 +15,6 @@ def decode_plist(data: bytes) -> Any: # noqa: ANN401 data = plist_header + data return plistlib.loads(data) + +def format_hex_byte(byte): + return f'{byte:02x}'.upper() \ No newline at end of file From a4b753287ec4adbc514fc8da553e2c039329ec88 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Wed, 20 Nov 2024 08:06:31 +0000 Subject: [PATCH 2/4] [pre-commit.ci lite] apply automatic fixes --- findmy/keys.py | 8 ++++++-- findmy/reports/reports.py | 4 ++-- findmy/util/parsers.py | 3 ++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/findmy/keys.py b/findmy/keys.py index 1ebc81d..1b53f33 100644 --- a/findmy/keys.py +++ b/findmy/keys.py @@ -70,12 +70,16 @@ class HasPublicKey(HasHashedPublicKey, ABC): def adv_key_b64(self) -> str: """Return the advertised (public) key as a base64-encoded string.""" return base64.b64encode(self.adv_key_bytes).decode("ascii") - + @property def mac_address(self) -> str: """Get the mac address from the public key.""" first_hex = self.adv_key_bytes[0] | 0b11000000 - return parsers.format_hex_byte(first_hex) + ":" + ":".join([parsers.format_hex_byte(x) for x in self.adv_key_bytes[1:6]]) + return ( + parsers.format_hex_byte(first_hex) + + ":" + + ":".join([parsers.format_hex_byte(x) for x in self.adv_key_bytes[1:6]]) + ) @property @override diff --git a/findmy/reports/reports.py b/findmy/reports/reports.py index 0403839..307db87 100644 --- a/findmy/reports/reports.py +++ b/findmy/reports/reports.py @@ -121,12 +121,12 @@ class LocationReport(HasHashedPublicKey): """The `datetime` when this report was recorded by a device.""" timestamp_int = int.from_bytes(self._payload[0:4], "big") + (60 * 60 * 24 * 11323) return datetime.fromtimestamp(timestamp_int, tz=timezone.utc).astimezone() - + @property def confidence(self) -> int: """Confidence of the location of this report. Int between 1 and 3""" # If the payload length is 88, the confidence is the 5th byte, otherwise it's the 6th byte - if (len(self._payload) == 88): + if len(self._payload) == 88: return self.payload[4] return self.payload[5] diff --git a/findmy/util/parsers.py b/findmy/util/parsers.py index 563db93..b1abb43 100644 --- a/findmy/util/parsers.py +++ b/findmy/util/parsers.py @@ -16,5 +16,6 @@ def decode_plist(data: bytes) -> Any: # noqa: ANN401 return plistlib.loads(data) + def format_hex_byte(byte): - return f'{byte:02x}'.upper() \ No newline at end of file + return f"{byte:02x}".upper() From 263cd5bd16a159a931f3833419e41dd7e6126e62 Mon Sep 17 00:00:00 2001 From: Philip Magyar Date: Wed, 20 Nov 2024 09:12:19 +0100 Subject: [PATCH 3/4] fix commit errors --- findmy/reports/reports.py | 2 +- findmy/util/parsers.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/findmy/reports/reports.py b/findmy/reports/reports.py index 307db87..14a9f2a 100644 --- a/findmy/reports/reports.py +++ b/findmy/reports/reports.py @@ -124,7 +124,7 @@ class LocationReport(HasHashedPublicKey): @property def confidence(self) -> int: - """Confidence of the location of this report. Int between 1 and 3""" + """Confidence of the location of this report. Int between 1 and 3.""" # If the payload length is 88, the confidence is the 5th byte, otherwise it's the 6th byte if len(self._payload) == 88: return self.payload[4] diff --git a/findmy/util/parsers.py b/findmy/util/parsers.py index b1abb43..fdd7ecb 100644 --- a/findmy/util/parsers.py +++ b/findmy/util/parsers.py @@ -17,5 +17,6 @@ def decode_plist(data: bytes) -> Any: # noqa: ANN401 return plistlib.loads(data) -def format_hex_byte(byte): +def format_hex_byte(byte: int) -> str: + """Format a byte as a two character hex string in uppercase.""" return f"{byte:02x}".upper() From 78d472ba0dfb588631ace514ced0a54a17713661 Mon Sep 17 00:00:00 2001 From: "Mike A." Date: Wed, 20 Nov 2024 21:15:37 +0100 Subject: [PATCH 4/4] Nitpicking --- findmy/keys.py | 8 ++------ findmy/reports/reports.py | 4 ++-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/findmy/keys.py b/findmy/keys.py index 1b53f33..89f9fc0 100644 --- a/findmy/keys.py +++ b/findmy/keys.py @@ -74,12 +74,8 @@ class HasPublicKey(HasHashedPublicKey, ABC): @property def mac_address(self) -> str: """Get the mac address from the public key.""" - first_hex = self.adv_key_bytes[0] | 0b11000000 - return ( - parsers.format_hex_byte(first_hex) - + ":" - + ":".join([parsers.format_hex_byte(x) for x in self.adv_key_bytes[1:6]]) - ) + first_byte = (self.adv_key_bytes[0] | 0b11000000).to_bytes(1) + return ":".join([parsers.format_hex_byte(x) for x in first_byte + self.adv_key_bytes[1:6]]) @property @override diff --git a/findmy/reports/reports.py b/findmy/reports/reports.py index 14a9f2a..931ad40 100644 --- a/findmy/reports/reports.py +++ b/findmy/reports/reports.py @@ -127,8 +127,8 @@ class LocationReport(HasHashedPublicKey): """Confidence of the location of this report. Int between 1 and 3.""" # If the payload length is 88, the confidence is the 5th byte, otherwise it's the 6th byte if len(self._payload) == 88: - return self.payload[4] - return self.payload[5] + return self._payload[4] + return self._payload[5] @property def latitude(self) -> float: