Skip to content

Commit

Permalink
Merge pull request #17 from keefertaylor/validate_deterministic_string
Browse files Browse the repository at this point in the history
Validate mnemonics when generating a deterministic string
  • Loading branch information
keefertaylor authored Apr 26, 2019
2 parents 117c483 + 9b2df31 commit 99f50be
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions MnemonicKit.podspec
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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" => "[email protected]" }
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"
Expand Down
5 changes: 3 additions & 2 deletions MnemonicKit/Mnemonic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -95,7 +96,7 @@ public enum Mnemonic {
guard SecRandomCopyBytes(kSecRandomDefault, count, UnsafeMutablePointer<UInt8>(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)
Expand Down
36 changes: 25 additions & 11 deletions Tests/MnemonicKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 = "路 级 少 图 路 级 少 图 路 级 少 图 路 级 少 图"
Expand All @@ -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 = "亂 語"

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

0 comments on commit 99f50be

Please sign in to comment.