Skip to content

Commit

Permalink
Merge pull request #85 from Philip2809/main
Browse files Browse the repository at this point in the history
fix confidence/horizontal acc, add mac address
  • Loading branch information
malmeloo authored Nov 20, 2024
2 parents 12de1db + 78d472b commit 41a041e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
8 changes: 7 additions & 1 deletion findmy/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from cryptography.hazmat.primitives.asymmetric import ec
from typing_extensions import override

from .util import crypto
from .util import crypto, parsers


class KeyType(Enum):
Expand Down Expand Up @@ -71,6 +71,12 @@ 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_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
def hashed_adv_key_bytes(self) -> bytes:
Expand Down
14 changes: 11 additions & 3 deletions findmy/reports/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ def timestamp(self) -> datetime:
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:
"""Latitude of the location of this report."""
Expand All @@ -145,10 +153,10 @@ def longitude(self) -> float:
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

Expand Down
5 changes: 5 additions & 0 deletions findmy/util/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ def decode_plist(data: bytes) -> Any: # noqa: ANN401
data = plist_header + data

return plistlib.loads(data)


def format_hex_byte(byte: int) -> str:
"""Format a byte as a two character hex string in uppercase."""
return f"{byte:02x}".upper()

0 comments on commit 41a041e

Please sign in to comment.