Skip to content

Commit 7d9059b

Browse files
committed
Bug fix: run unicode normalisation on input strings before generating cryptographic seed
1 parent 757c197 commit 7d9059b

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

Sources/KukaiCryptoSwift/Mnemonic/Mnemonic.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,15 @@ public struct Mnemonic: Equatable, Codable {
9292
* - returns: A _result_ with the seed's bytes, or an `Error`.
9393
*/
9494
public func seed(passphrase: String = "") throws -> Data {
95-
guard var passwordData = self.phrase.data(using: .utf8)?.map(Int8.init) else {
95+
guard var passwordData = self.phrase.decomposedStringWithCompatibilityMapping.data(using: .utf8)?.map(Int8.init) else {
9696
throw MnemonicError.seedPhraseInvalid("Can't convert to data")
9797
}
9898

99-
let salt = ("mnemonic" + passphrase)
99+
let salt = ("mnemonic" + passphrase.decomposedStringWithCompatibilityMapping)
100100
let passwordCount = self.phrase.count
101101

102102
var saltData = salt.bytes
103-
let saltCount = salt.count
104-
103+
let saltCount = saltData.count
105104
var data = [UInt8](repeating: 0, count: 64)
106105

107106
let status = CCKeyDerivationPBKDF( CCPBKDFAlgorithm(kCCPBKDF2),

Tests/KukaiCryptoSwiftTests/KeyPairTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,20 @@ final class KeyPairTests: XCTestCase {
231231
XCTAssert(KeyPair.isSecretKeyEncrypted("spsk29hF9oJ6koNnnJMs1rXz4ynBs8hL8FyubTNPCu2tCVP5beGDbw") == false)
232232
XCTAssert(KeyPair.isSecretKeyEncrypted("spesk1S5bMTCyH9z4mHSpnbn6DBY831DD6Rxgq7ANfEKkngoHSwy6B5odh942TKL6DtLbfTkpTHfSTAQu2d72Qd6") == true)
233233
}
234+
235+
func testUnicodeCompatibility() {
236+
guard let mnemonic = try? Mnemonic(seedPhrase: "kit trigger pledge excess payment sentence dutch mandate start sense seed venture") else {
237+
XCTFail("invlaid mnemonic")
238+
return
239+
}
240+
241+
let passphrase = "mail@email.compa55word"
242+
let passphraseWithSpecialChar = "mail@email.compa55wordé"
243+
244+
let keyPair1 = KeyPair.regular(fromMnemonic: mnemonic, passphrase: passphrase)
245+
let keyPair2 = KeyPair.regular(fromMnemonic: mnemonic, passphrase: passphraseWithSpecialChar)
246+
247+
XCTAssert(keyPair1?.publicKey.publicKeyHash == "tz1LBTLMmLMkhaG4Gw3juPaQTTgp95CbLfaf", keyPair1?.publicKey.publicKeyHash ?? "-")
248+
XCTAssert(keyPair2?.publicKey.publicKeyHash == "tz1WF2KjXMu1mFewnqH91bXUNjcYKyGRURee", keyPair2?.publicKey.publicKeyHash ?? "-")
249+
}
234250
}

0 commit comments

Comments
 (0)