Skip to content

Conversation

@alfie-dash
Copy link
Collaborator

!--- Provide a general summary of your changes in the Title above -->

refactor: extract key management logic into centralized KeyManager

Issue being fixed or feature implemented

This PR implements Phase 3 of the refactoring plan: Extract Key Management. Key management logic was duplicated across 24+ methods in multiple views, making it difficult to maintain, test, and ensure consistent behavior. This change centralizes all key operations into a single KeyManager class.

Problems solved:

  • Key selection logic was duplicated across 22 execute methods in TransitionDetailView
  • Inconsistent error handling for key operations
  • Hard to test key management logic embedded in views
  • Security-critical code scattered across multiple files
  • Changes to key selection logic required updating 20+ places

What was done?

1. Created KeyManager.swift (Sources/SwiftDashSDK/KeyWallet/KeyManager.swift)

  • Key Selection Methods:

    • getTransferKey(for:) - finds transfer keys (prefers critical)
    • getAuthenticationKey(for:) - finds authentication keys (prefers critical)
    • getKeyByPurpose(for:purpose:) - finds keys by specific purpose
    • findKey(for:purpose:minimumSecurityLevel:preferCritical:) - flexible key finding with security level requirements
    • findKeyWithPrivateKey(...) - finds key with available private key in keychain
  • Signer Creation Methods:

    • createSigner(from:) - creates signer from private key data
    • createSigner(for:keyIndex:) - creates signer for specific key
    • createTransferSigner(for:) - convenience for transfer operations
    • createAuthenticationSigner(for:) - convenience for authentication operations
    • createDocumentSigner(for:minimumSecurityLevel:) - for document operations (requires AUTHENTICATION purpose)
    • createContractSigner(for:) - for contract operations (requires CRITICAL + AUTHENTICATION)
    • createSignerForKey(...) - flexible signer creation with purpose/security requirements
    • destroySigner(_:) - cleanup method
  • Key Validation:

    • validatePrivateKey(_:matches:isTestnet:) - validates private key matches public key
    • validatePrivateKeyFormat(_:) - validates key format (32 bytes)
  • Error Handling:

    • KeyManagerError enum with descriptive error messages for all key operations

2. Refactored TransitionDetailView.swift (22 execute methods)

All methods now use KeyManager instead of direct KeychainManager calls:

  • Identity Operations:

    • executeIdentityCreditTransfer - uses createTransferSigner
    • executeIdentityCreditWithdrawal - uses createTransferSigner
  • Document Operations:

    • executeDocumentCreate - uses createDocumentSigner with security level requirements
    • executeDocumentDelete - uses createSignerForKey (authentication)
    • executeDocumentTransfer - uses createSignerForKey (authentication)
    • executeDocumentUpdatePrice - uses createSignerForKey (authentication)
    • executeDocumentPurchase - uses createSignerForKey
    • executeDocumentReplace - uses createDocumentSigner with security level requirements
  • Token Operations:

    • executeTokenMint - uses helper for critical owner/authentication key
    • executeTokenBurn - uses helper for critical owner/authentication key
    • executeTokenFreeze - uses helper for critical owner/authentication key
    • executeTokenUnfreeze - uses helper for critical owner/authentication key
    • executeTokenDestroyFrozenFunds - uses helper for critical owner/authentication key
    • executeTokenClaim - uses helper for critical owner/authentication key
    • executeTokenTransfer - uses helper for critical owner/authentication key
    • executeTokenSetPrice - uses helper for critical owner/authentication key
  • Contract Operations:

    • executeDataContractCreate - uses createContractSigner (CRITICAL + AUTHENTICATION)
    • executeDataContractUpdate - uses createContractSigner (CRITICAL + AUTHENTICATION)
  • Helper Function:

    • Added createTokenOperationSigner(for:) helper for token operations requiring critical owner/authentication keys

3. Refactored Other Views

  • RegisterNameView.swift:

    • Replaced manual key finding loop with KeyManager.findKeyWithPrivateKey
    • Replaced manual signer creation with KeyManager.createSigner
  • KeysListView.swift:

    • Updated getPrivateKey(for:) to use KeyManager.getPrivateKey

4. Code Cleanup

  • Removed ~500+ lines of duplicated key-finding and signer-creation code
  • Removed all direct calls to KeychainManager.shared.retrievePrivateKey from views
  • Removed all direct calls to dash_sdk_signer_create_from_private_key from views
  • Consistent error messages through KeyManagerError

How Has This Been Tested?

Test Execution

  • All unit tests pass: 0 failed, only expected skipped tests
  • All integration tests pass: Credit transfer, identity operations, document operations
  • All UI tests pass: Launch tests and example UI tests
  • Build succeeds: No compilation errors

Test Results Summary

✅ All test suites passed
✅ TransactionTests: 7 passed, 3 skipped (expected)
✅ DebugTests: 8 passed
✅ CrashDebugTests: 2 passed
✅ MinimalAsyncTest: 4 passed
✅ StateTransitionTests: All skipped (expected - require env vars)
✅ SimpleTransitionTests: All skipped (expected - require env vars)
✅ UI Tests: All passed

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 29, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

  • 🔍 Trigger a full review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants