Skip to content

Refactor Web3Wrapper and optimize string/array operations#2

Open
Kushmanmb wants to merge 24 commits intoFoboz:masterfrom
kushmanmb-org:master
Open

Refactor Web3Wrapper and optimize string/array operations#2
Kushmanmb wants to merge 24 commits intoFoboz:masterfrom
kushmanmb-org:master

Conversation

@Kushmanmb
Copy link

@Kushmanmb Kushmanmb commented Dec 29, 2025

Summary

Improves code quality, readability, and performance across several core components of the MEWconnect iOS app. This PR focuses on refactoring repeated code patterns, optimizing string/array operations, and modernizing project configuration files.

🎯 Motivation

  • Reduce code duplication in Web3Wrapper cryptographic operations
  • Improve performance of string processing operations (from O(n²) to O(n))
  • Modernize Git configuration for better iOS/Swift development workflow
  • Eliminate unsafe force unwrapping where possible

📝 Changes

🔧 Refactoring (Web3Wrapper. swift)

Problem: Keydata decryption logic was duplicated across 3 methods
Solution: Extracted into reusable helper methods

  • obtainDecryptedKeydata(): Centralized repeated 2-line decryption pattern
  • createBIP32Keystore(): Static helper for BIP32 keystore creation
  • Removed force unwrapping (!) operators for safer optional handling
  • Cleaner method signatures: Eliminated unnecessary optional chaining

Impact:

  • 🔒 More maintainable cryptographic code
  • 🐛 Reduced risk of nil-pointer crashes
  • 📖 Improved code readability

⚡ Performance Optimizations

Character+ASCIIValue. swift

Before: Created intermediate String object
After: Direct unicode scalar access

// Before: O(n) + String allocation
let s = String(self).unicodeScalars
return s[s.startIndex]. value

// After: O(1) + zero allocations
return self.unicodeScalars. first!.value

Kushmanmb and others added 10 commits December 24, 2025 09:13
- Extract hex prefix stripping duplication in NSString+HexToDecimal.swift to use existing stripHexPrefix() method
- Extract keydata decryption pattern into obtainDecryptedKeydata() helper method (used in 3 places)
- Extract BIP32Keystore creation pattern into createBIP32Keystore() helper method (used in 2 places)

Co-authored-by: Kushmanmb <193178375+Kushmanmb@users.noreply.github.com>
The condition `bip32Keystore != nil` is redundant since `try?` already returns `nil` on failure

Co-authored-by: Kushmanmb <193178375+Kushmanmb@users.noreply.github.com>
- Fix O(n²) string indexing in String+RandSeed (use enumerated() instead of repeated index())
- Optimize Array+UInt32 rand() to avoid O(n) remove/append pattern
- Remove unnecessary String allocation in Character+ASCIIValue
- Pre-allocate array capacities in UIColor+RandSeed and Web3Wrapper
- Add performance comments explaining optimizations

Co-authored-by: Kushmanmb <193178375+Kushmanmb@users.noreply.github.com>
- Fix incorrect O(1) claim in Array+UInt32 comment (still O(n) but avoids reallocation)
- Extract magic number 3 to named constant colorCount in UIColor+RandSeed

Co-authored-by: Kushmanmb <193178375+Kushmanmb@users.noreply.github.com>
- Use force unwrap in Character.asciiValue (every Character has at least one unicode scalar)
- Remove empty array guard in Array.rand() to preserve original crash behavior on misuse

Co-authored-by: Kushmanmb <193178375+Kushmanmb@users.noreply.github.com>
Copy link
Author

@Kushmanmb Kushmanmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

``

Copy link
Author

@Kushmanmb Kushmanmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kmmb

Refactor duplicated code patterns in Web3Wrapper and NSString extensions
Copy link
Author

@Kushmanmb Kushmanmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

@Kushmanmb Kushmanmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot AI and others added 5 commits January 7, 2026 20:15
Co-authored-by: Kushmanmb <193178375+Kushmanmb@users.noreply.github.com>
Co-authored-by: Kushmanmb <193178375+Kushmanmb@users.noreply.github.com>
Add .gitattributes and modernize .gitignore for iOS development
@Kushmanmb Kushmanmb changed the title Kushmanmb Refactor Web3Wrapper and optimize string/array operations Jan 16, 2026
Copy link
Author

@Kushmanmb Kushmanmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work

Copilot AI and others added 4 commits February 4, 2026 15:32
Co-authored-by: Kushmanmb <193178375+Kushmanmb@users.noreply.github.com>
…eToJSON

Co-authored-by: Kushmanmb <193178375+Kushmanmb@users.noreply.github.com>
Co-authored-by: Kushmanmb <193178375+Kushmanmb@users.noreply.github.com>
Refactor duplicated code patterns in Web3Wrapper and CheckboxButton
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