fix(device-key): address PR review — SE orphaning, atomic writes, error discrimination
- CRITICAL-1: SE fast-path now checks both SE_PUB_ACCOUNT and SE_APP_LABEL_ACCOUNT
to ensure state consistency. Errors are now properly discriminated:
ItemNotFound (no key yet) falls through to generation, transient OS errors
(errSecInteractionNotAllowed, etc.) are propagated as KeychainError to prevent
key orphaning on retries.
- CRITICAL-2: Non-atomic SE Keychain writes now roll back SE_PUB_ACCOUNT if
application_label() or the SE_APP_LABEL_ACCOUNT write fails, preventing
permanent inconsistency where fast-path finds public key but sign() fails.
- HIGH-1: search.search() OS errors now map to KeychainError instead of KeyNotFound.
KeyNotFound is reserved for the case where search succeeds but finds no key,
matching the contract that KeyNotFound signals 'call get_or_create first'.
- HIGH-2: Simulator sign() now uses is_not_found() to distinguish ItemNotFound
from transient OS errors, preventing false KeyNotFound returns.
- HIGH-3: Device key errors in create_account now map to KeychainError instead
of Unknown, using the correct error variant already defined.
- Added is_not_found() helper to keychain.rs to centralize errSecItemNotFound
detection (-25300). Use this to distinguish missing items from OS errors.
- Enforced --test-threads=1 via [env] RUST_TEST_THREADS in .cargo/config.toml
to prevent Keychain races in tests (previously advisory comment only).
- Suggestion fixes: Updated to_vec() comment (line 65), removed Phase 2 header,
removed compiler note about ambiguous type, updated normalize_s() comment.
- Test improvements: Added assertions for SigningFailed and InvalidSignature
variants to device_key_error_serializes_as_code test. Added cryptographic
verification test (sign_output_verifies_against_public_key) that confirms
signatures actually verify against the public key from get_or_create().
- Renamed test to device_key_contract_satisfies_relay_format to clarify that
it tests the device_key module contract (format + idempotency) that
create_account depends on, not create_account itself.
- Added libiconv library path to aarch64-apple-darwin rustflags to resolve
linking errors in host proc-macro builds (phf_macros, darling, etc.).