From 4a2d45a8bcd5393aa8c2901e0d3a0f9d1d2a40e3 Mon Sep 17 00:00:00 2001 From: keefertaylor Date: Fri, 26 Apr 2019 17:35:52 +0100 Subject: [PATCH 1/3] Validate that mnemonics are deterministic before creating seed strings --- MnemonicKit/Mnemonic.swift | 5 +++-- Tests/MnemonicKitTests.swift | 36 +++++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/MnemonicKit/Mnemonic.swift b/MnemonicKit/Mnemonic.swift index 67e1377..1d415a2 100644 --- a/MnemonicKit/Mnemonic.swift +++ b/MnemonicKit/Mnemonic.swift @@ -63,7 +63,8 @@ public enum Mnemonic { passphrase: String = "", language _: MnemonicLanguageType = .english ) -> String? { - guard let normalizedData = self.normalized(string: mnemonic), + guard self.validate(mnemonic: mnemonic), + let normalizedData = self.normalized(string: mnemonic), let saltData = normalized(string: "mnemonic" + passphrase) else { return nil } @@ -95,7 +96,7 @@ public enum Mnemonic { guard SecRandomCopyBytes(kSecRandomDefault, count, UnsafeMutablePointer(mutating: bytes)) != -1 else { return nil } - let data = Data(bytes: bytes) + let data = Data(bytes) let hexString = data.toHexString() return mnemonicString(from: hexString, language: language) diff --git a/Tests/MnemonicKitTests.swift b/Tests/MnemonicKitTests.swift index 8edee51..ea6d787 100644 --- a/Tests/MnemonicKitTests.swift +++ b/Tests/MnemonicKitTests.swift @@ -49,7 +49,7 @@ class MnemonicTests: XCTestCase { } } - private static func dictionaryFromTestInputFile() -> [String: Any]? { + static func dictionaryFromTestInputFile() -> [String: Any]? { let testBundle = Bundle(for: self) guard let url = testBundle.url(forResource: "vectors", withExtension: "json") else { return nil @@ -69,19 +69,19 @@ class MnemonicTests: XCTestCase { } /// Test mnemonic generation in english. - public func testGenerateMnemonic() { + func testGenerateMnemonic() { let mnemonic = Mnemonic.generateMnemonic(strength: 32) XCTAssertNotNil(mnemonic) } /// Prove that functions work in chinese as well. - public func testGenerateMnemonicChinese() { + func testGenerateMnemonicChinese() { let chineseMnemonic = Mnemonic.generateMnemonic(strength: 32, language: .chinese) XCTAssertNotNil(chineseMnemonic) } /// Test input strengths for mnemonic generation. - public func testMnemonicGenerationStrength() { + func testMnemonicGenerationStrength() { let mnemonic32 = Mnemonic.generateMnemonic(strength: 32) let mnemonic64 = Mnemonic.generateMnemonic(strength: 32) XCTAssertNotNil(mnemonic32) @@ -92,7 +92,7 @@ class MnemonicTests: XCTestCase { } /// Test valid chinese and english mnemonics are determined to be valid. - public func testValidEnglishAndChineseMnemonics() { + func testValidEnglishAndChineseMnemonics() { let englishMnemonic = "pear peasant pelican pen pear peasant pelican pen pear peasant pelican pen pear peasant pelican pen" let chineseMnemonic = "路 级 少 图 路 级 少 图 路 级 少 图 路 级 少 图" @@ -102,7 +102,7 @@ class MnemonicTests: XCTestCase { } /// Test invalid chinese and english mnemonics are determined to be invalid. - public func testInvalidEnglishAndChineseMnemonics() { + func testInvalidEnglishAndChineseMnemonics() { let englishMnemonic = "slacktivist snacktivity snuggie" let chineseMnemonic = "亂 語" @@ -111,32 +111,46 @@ class MnemonicTests: XCTestCase { } /// Test the empty string is determined to be an invalid mnemonic. - public func testEmptyStringValidation() { + func testEmptyStringValidation() { XCTAssertFalse(Mnemonic.validate(mnemonic: "")) } /// Test that strings in an unknown language are determined to be invalid. - public func testUnknownLanguageValidation() { + func testUnknownLanguageValidation() { let spanishMnemonic = "pera campesina pelican pen pera campesina pelican pen pera campesina pelican pen pera campesina pelican pen" XCTAssertFalse(Mnemonic.validate(mnemonic: spanishMnemonic)) } /// Test that strings of mixed case are determined to be valid. - public func testMixedCaseValidation() { + func testMixedCaseValidation() { let mixedCaseMnemonic = "pear PEASANT PeLiCaN pen" XCTAssertTrue(Mnemonic.validate(mnemonic: mixedCaseMnemonic)) } /// Test mixed language mnemonics. - public func testMixedLanguageMnemonicValidation() { + func testMixedLanguageMnemonicValidation() { let mixedLanguageMnemonic = "pear peasant pelican pen 路 级 少 图" XCTAssertFalse(Mnemonic.validate(mnemonic: mixedLanguageMnemonic)) } /// Test that strings padded with whitespace are determined to be valid. - public func testWhitespacePaddedValidation() { + func testWhitespacePaddedValidation() { let whitespacePaddedMnemonic = " pear peasant pelican pen\t\t\n" XCTAssertTrue(Mnemonic.validate(mnemonic: whitespacePaddedMnemonic)) } + + /// Test an valid mnemonic generates a seed string. + func testDeterministicSeedStringValidMnemonic() { + let invalidMnemonic = + "pear peasant pelican pen pear peasant pelican pen pear peasant pelican pen pear peasant pelican pen" + XCTAssertNotNil(Mnemonic.deterministicSeedString(from: invalidMnemonic)) + } + + /// Test an invalid mnemonic does not generate a seed string. + func testDeterministicSeedStringInvalidMnemonic() { + let invalidMnemonic = + "mnemonickit mnemonickit mnemonickit mnemonickit mnemonickit mnemonickit mnemonickit mnemonickit mnemonickit" + XCTAssertNil(Mnemonic.deterministicSeedString(from: invalidMnemonic)) + } } From f6b797ecfbfa7a307c45894eec0d7ac76f7569e5 Mon Sep 17 00:00:00 2001 From: keefertaylor Date: Fri, 26 Apr 2019 17:36:25 +0100 Subject: [PATCH 2/3] Bump version --- MnemonicKit.podspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MnemonicKit.podspec b/MnemonicKit.podspec index 877d4ae..a6120b2 100644 --- a/MnemonicKit.podspec +++ b/MnemonicKit.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "MnemonicKit" - s.version = "1.3.5" + s.version = "1.3.6" s.summary = "MnemonicKit provides a Swift implementation of BIP39" s.description = <<-DESC MnemonicKit provides a Swift implementation of BIP39. @@ -11,7 +11,7 @@ Pod::Spec.new do |s| s.homepage = "https://github.com/keefertaylor/MnemonicKit" s.license = { :type => "MIT", :file => "LICENSE" } s.author = { "Keefer Taylor" => "keefer@keefertaylor.com" } - s.source = { :git => "https://github.com/keefertaylor/MnemonicKit.git", :tag => "1.3.5" } + s.source = { :git => "https://github.com/keefertaylor/MnemonicKit.git", :tag => "1.3.6" } s.source_files = "MnemonicKit/**/*.swift", s.swift_version = "4.2" s.ios.deployment_target = "8.0" From 9b2df31d0ab72f9bd5c3b62095c377c68bbd0943 Mon Sep 17 00:00:00 2001 From: keefertaylor Date: Fri, 26 Apr 2019 17:47:14 +0100 Subject: [PATCH 3/3] Wire up build for higher timeouts --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7a64beb..6e4ef99 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,9 +9,9 @@ before_install: - gem install slather - brew update - brew outdated carthage || brew upgrade carthage -- carthage bootstrap --platform iOS --cache-builds +- travis_wait 120 carthage bootstrap --platform iOS --cache-builds before_deploy: -- carthage build --no-skip-current --platform iOS --cache-builds +- travis_wait 120 carthage build --no-skip-current --platform iOS --cache-builds - carthage archive $FRAMEWORK_NAME after_deploy: - pod trunk push --skip-import-validation --skip-tests --allow-warnings