Normalize the default subaccount in ICRC-1 deposit records
The same ledger account can arrive in two representations. A deposit database that keys directly on the incoming encoding can create two balances for one account.
Canonicalize the account tuple
Use the ledger canister ID, owner's principal bytes and a 32-byte subaccount as the database key. Expand an absent subaccount to 32 zero bytes. Keep a nonzero subaccount unchanged, even if it starts with zeros. Validate the length before normalization to catch malformed external records.
A = { owner = P; subaccount = null }
B = { owner = P; subaccount = 32 zero bytes }
normalized(A) = normalized(B)
Reconcile existing duplicates
If A and B already occupy separate rows, replay their distinct ledger transactions into one canonical row. Simply adding the cached totals may count the same deposit twice. Two ledgers using the same owner and subaccount still represent different asset balances.