refactor(swift-sdk): extract key management logic into centralized KeyManager #3033
+3,949
−4,031
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
!--- 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
KeyManagerclass.Problems solved:
TransitionDetailViewWhat 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 purposefindKey(for:purpose:minimumSecurityLevel:preferCritical:)- flexible key finding with security level requirementsfindKeyWithPrivateKey(...)- finds key with available private key in keychainSigner Creation Methods:
createSigner(from:)- creates signer from private key datacreateSigner(for:keyIndex:)- creates signer for specific keycreateTransferSigner(for:)- convenience for transfer operationscreateAuthenticationSigner(for:)- convenience for authentication operationscreateDocumentSigner(for:minimumSecurityLevel:)- for document operations (requires AUTHENTICATION purpose)createContractSigner(for:)- for contract operations (requires CRITICAL + AUTHENTICATION)createSignerForKey(...)- flexible signer creation with purpose/security requirementsdestroySigner(_:)- cleanup methodKey Validation:
validatePrivateKey(_:matches:isTestnet:)- validates private key matches public keyvalidatePrivateKeyFormat(_:)- validates key format (32 bytes)Error Handling:
KeyManagerErrorenum with descriptive error messages for all key operations2. Refactored
TransitionDetailView.swift(22 execute methods)All methods now use
KeyManagerinstead of directKeychainManagercalls:Identity Operations:
executeIdentityCreditTransfer- usescreateTransferSignerexecuteIdentityCreditWithdrawal- usescreateTransferSignerDocument Operations:
executeDocumentCreate- usescreateDocumentSignerwith security level requirementsexecuteDocumentDelete- usescreateSignerForKey(authentication)executeDocumentTransfer- usescreateSignerForKey(authentication)executeDocumentUpdatePrice- usescreateSignerForKey(authentication)executeDocumentPurchase- usescreateSignerForKeyexecuteDocumentReplace- usescreateDocumentSignerwith security level requirementsToken Operations:
executeTokenMint- uses helper for critical owner/authentication keyexecuteTokenBurn- uses helper for critical owner/authentication keyexecuteTokenFreeze- uses helper for critical owner/authentication keyexecuteTokenUnfreeze- uses helper for critical owner/authentication keyexecuteTokenDestroyFrozenFunds- uses helper for critical owner/authentication keyexecuteTokenClaim- uses helper for critical owner/authentication keyexecuteTokenTransfer- uses helper for critical owner/authentication keyexecuteTokenSetPrice- uses helper for critical owner/authentication keyContract Operations:
executeDataContractCreate- usescreateContractSigner(CRITICAL + AUTHENTICATION)executeDataContractUpdate- usescreateContractSigner(CRITICAL + AUTHENTICATION)Helper Function:
createTokenOperationSigner(for:)helper for token operations requiring critical owner/authentication keys3. Refactored Other Views
RegisterNameView.swift:KeyManager.findKeyWithPrivateKeyKeyManager.createSignerKeysListView.swift:getPrivateKey(for:)to useKeyManager.getPrivateKey4. Code Cleanup
KeychainManager.shared.retrievePrivateKeyfrom viewsdash_sdk_signer_create_from_private_keyfrom viewsKeyManagerErrorHow Has This Been Tested?
Test Execution
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