From f7a2b25573cc3babddcad9bd38874009d0437322 Mon Sep 17 00:00:00 2001 From: Alexander Osokin Date: Mon, 13 Mar 2023 15:57:23 +0500 Subject: [PATCH] IOS-3059 Code polishing --- TangemSdk/TangemSdk/Common/Extensions/String+.swift | 4 ++-- TangemSdk/TangemSdk/Crypto/BIP39/BIP39.swift | 12 ++++++------ TangemSdk/TangemSdk/Crypto/CryptoUtils.swift | 5 ----- TangemSdk/TangemSdkTests/BIP39Tests.swift | 2 +- .../Jsons/BIP39/mnemonic_invalid_test_vectors.json | 3 ++- 5 files changed, 11 insertions(+), 15 deletions(-) diff --git a/TangemSdk/TangemSdk/Common/Extensions/String+.swift b/TangemSdk/TangemSdk/Common/Extensions/String+.swift index 35fdf999d..4fc5c3075 100644 --- a/TangemSdk/TangemSdk/Common/Extensions/String+.swift +++ b/TangemSdk/TangemSdk/Common/Extensions/String+.swift @@ -59,10 +59,10 @@ public extension String { return trimmingCharacters(in: .whitespacesAndNewlines) } - internal func zeroPadding(toLength newLength: Int) -> String { + internal func leadingZeroPadding(toLength newLength: Int) -> String { guard count < newLength else { return self } - let prefix = Array(repeating: "0", count: newLength - count).joined() + let prefix = String(repeating: "0", count: newLength - count) return prefix + self } } diff --git a/TangemSdk/TangemSdk/Crypto/BIP39/BIP39.swift b/TangemSdk/TangemSdk/Crypto/BIP39/BIP39.swift index 430c46646..98bb58a2d 100644 --- a/TangemSdk/TangemSdk/Crypto/BIP39/BIP39.swift +++ b/TangemSdk/TangemSdk/Crypto/BIP39/BIP39.swift @@ -43,7 +43,7 @@ struct BIP39 { /// Generate a mnemonic from data. Useful for testing purposes. /// - Parameters: - /// - data: The entropy data in hex format + /// - entropyData: The entropy data in hex format /// - wordlist: The wordlist to use. /// - Returns: The generated mnemonic func generateMnemonic(from entropyData: Data, wordlist: Wordlist) throws -> [String] { @@ -91,7 +91,7 @@ struct BIP39 { } // Validate wordlist by the first word - let wordlist = try getWordlist(by: mnemonicComponents[0]).1 + let wordlistDictionary = try getWordlist(by: mnemonicComponents[0]).dictionary // Validate all the words var invalidWords = Set() @@ -100,12 +100,12 @@ struct BIP39 { var concatenatedBits = "" for word in mnemonicComponents { - guard let wordIndex = wordlist.firstIndex(of: word) else { + guard let wordIndex = wordlistDictionary.firstIndex(of: word) else { invalidWords.insert(word) continue } - let indexBits = String(wordIndex, radix: 2).zeroPadding(toLength: 11) + let indexBits = String(wordIndex, radix: 2).leadingZeroPadding(toLength: 11) concatenatedBits.append(contentsOf: indexBits) } @@ -164,7 +164,7 @@ struct BIP39 { /// - Parameter mnemonicComponents: Menemonic components to use /// - Returns: The Wordlist, selected by the first word func parseWordlist(from mnemonicComponents: [String]) throws -> Wordlist { - return try getWordlist(by: mnemonicComponents[0]).0 + return try getWordlist(by: mnemonicComponents[0]).wordlist } @@ -185,7 +185,7 @@ struct BIP39 { return data } - private func getWordlist(by word: String) throws -> (Wordlist, [String]) { + private func getWordlist(by word: String) throws -> (wordlist: Wordlist, dictionary: [String]) { for list in Wordlist.allCases { let words = list.words diff --git a/TangemSdk/TangemSdk/Crypto/CryptoUtils.swift b/TangemSdk/TangemSdk/Crypto/CryptoUtils.swift index 486b847d3..ee0bf752d 100644 --- a/TangemSdk/TangemSdk/Crypto/CryptoUtils.swift +++ b/TangemSdk/TangemSdk/Crypto/CryptoUtils.swift @@ -94,11 +94,6 @@ public enum CryptoUtils { throw TangemSdkError.unsupportedCurve } } - - func createPublicKey(privateKey: Data) throws -> Data { - let key = try P256.Signing.PrivateKey(rawRepresentation: privateKey) - return key.publicKey.rawRepresentation - } /** * Helper function to verify that the data was signed with a private key that corresponds diff --git a/TangemSdk/TangemSdkTests/BIP39Tests.swift b/TangemSdk/TangemSdkTests/BIP39Tests.swift index 40caf0845..3a4c1a1e2 100644 --- a/TangemSdk/TangemSdkTests/BIP39Tests.swift +++ b/TangemSdk/TangemSdkTests/BIP39Tests.swift @@ -16,7 +16,7 @@ class BIP39Tests: XCTestCase { let langs = Wordlist.allCases for lang in langs { - XCTAssertTrue(lang.words.count > 0) + XCTAssertTrue(lang.words.count == 2048) } } diff --git a/TangemSdk/TangemSdkTests/Jsons/BIP39/mnemonic_invalid_test_vectors.json b/TangemSdk/TangemSdkTests/Jsons/BIP39/mnemonic_invalid_test_vectors.json index dc35f3565..68a2eeef2 100644 --- a/TangemSdk/TangemSdkTests/Jsons/BIP39/mnemonic_invalid_test_vectors.json +++ b/TangemSdk/TangemSdkTests/Jsons/BIP39/mnemonic_invalid_test_vectors.json @@ -7,7 +7,8 @@ "legal winner thank year wave sausage worth useful legal winner thank yellow yellow", "legal winner thank year δΊ‚ sausage worth useful legal winner thank yellow", "", - "pear peasant pelican pen pear peasant pelican pen pear peasant pelican pen pear peasant pelican pen" + "pear peasant pelican pen pear peasant pelican pen pear peasant pelican pen pear peasant pelican pen", + "Legal winner thank year wave sausige worth useful legal winner thank yellow" ] ] }