# -*- coding: utf-8 -*-
import re
import zipfile
from pathlib import Path

APK = Path(__file__).resolve().parent.parent / "apks" / "maribank_sg_base.apk"
with zipfile.ZipFile(str(APK)) as zf:
    data = b"".join(zf.read(n) for n in zf.namelist() if n.endswith(".dex"))

for needle in [b"getDfp", b"DeviceFingerprintResp", b"deviceFingerprint", b"/dfp/", b"dfp/v1"]:
    print("\n===", needle.decode(), "===")
    for m in re.finditer(re.escape(needle) + rb"[\x00-\xff]{0,80}", data):
        chunk = data[m.start() : m.start() + 120]
        s = re.sub(rb"[^\x20-\x7e]+", b"|", chunk).decode("ascii", "ignore")
        print(s[:140])
        break

# classes with fingerprint in name
fps = sorted(set(m.group().decode()[1:-1].replace("/", ".")
                 for m in re.finditer(rb"Lcom/shopee/bke/[^;\x00]{0,120}[Ff]ingerprint[^;\x00]{0,40};", data)))
print("\n=== fingerprint classes ===")
for c in fps[:30]:
    print(c)

# RegisterViewModel methods - search string RegisterViewModel in dex
for m in re.finditer(rb"Lcom/shopee/bke/biz/user/viewmodel/RegisterViewModel[^;\x00]*;", data):
    print("\nRegisterViewModel:", m.group().decode())
