Skip to content

Commit

Permalink
IOS-3059 Code polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
tureck1y committed Mar 13, 2023
1 parent 007515e commit f7a2b25
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
4 changes: 2 additions & 2 deletions TangemSdk/TangemSdk/Common/Extensions/String+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
12 changes: 6 additions & 6 deletions TangemSdk/TangemSdk/Crypto/BIP39/BIP39.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down Expand Up @@ -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<String>()
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
}


Expand All @@ -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

Expand Down
5 changes: 0 additions & 5 deletions TangemSdk/TangemSdk/Crypto/CryptoUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion TangemSdk/TangemSdkTests/BIP39Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
]
}

0 comments on commit f7a2b25

Please sign in to comment.