diff --git a/Source/TonSwift/Mnemonic/Mnemonic.swift b/Source/TonSwift/Mnemonic/Mnemonic.swift index 57b55b4..1e0a3c2 100644 --- a/Source/TonSwift/Mnemonic/Mnemonic.swift +++ b/Source/TonSwift/Mnemonic/Mnemonic.swift @@ -117,6 +117,22 @@ public enum Mnemonic { return seed[0] == 0 } + + public static func isMultiAccountSeed(mnemonicArray: [String]) -> Bool { + let entropy = hmacSha512(phrase: "TON Keychain", password: mnemonicArray.joined(separator: " ")) + + // There is a collision propability with TON mnemonics + if (isBasicSeed(entropy: mnemonicToEntropy(mnemonicArray: mnemonicArray, password: ""))) { + return false + } + + let salt = "TON Keychain Version" + let saltData = Data(salt.utf8) + let seed = pbkdf2Sha512(phrase: entropy, salt: saltData, iterations: 1, keyLength: 64) + + return seed[0] == 0 + } + public static func isPasswordSeed(entropy: Data) -> Bool { let salt = "TON fast seed version" diff --git a/Tests/TonSwiftTests/Mnemonic/MnemonicTest.swift b/Tests/TonSwiftTests/Mnemonic/MnemonicTest.swift index b8fd27a..d397488 100644 --- a/Tests/TonSwiftTests/Mnemonic/MnemonicTest.swift +++ b/Tests/TonSwiftTests/Mnemonic/MnemonicTest.swift @@ -19,5 +19,19 @@ final class MnemonicTest: XCTestCase { XCTAssertEqual(keyPair.publicKey.hexString, "34eb4b67d64f74d989ce2bc2e3dfddb7ed4cb0eec92f29fbecd05b1eabab0254") XCTAssertEqual(keyPair.privateKey.hexString, "c893fc0b676782a5c157ad8fddb389f75caba6eea1c198d8075a8a43afce70a934eb4b67d64f74d989ce2bc2e3dfddb7ed4cb0eec92f29fbecd05b1eabab0254") } + + func testIsMultiAccountSeedMnemonic() throws { + let collisionMnemonic = "cluster notice abandon frost gospel boring element situate click mix vague replace imitate garment useful crater resource dose tenant theme foam ancient phrase slight".components(separatedBy: " ") + + XCTAssertFalse(Mnemonic.isMultiAccountSeed(mnemonicArray: collisionMnemonic)) + + let multiAccountMnemonic = "execute peanut please demise thumb mango argue cloud reopen upset also dentist panic elite roast security pyramid extra boil execute lazy pledge notice check".components(separatedBy: " ") + + XCTAssertTrue(Mnemonic.isMultiAccountSeed(mnemonicArray: multiAccountMnemonic)) + + let tonMnemonic = "item supply cover volcano satisfy window custom cupboard license dance record tissue gadget rural health blossom useless useless hungry brush grief stock reflect morning".components(separatedBy: " ") + + XCTAssertFalse(Mnemonic.isMultiAccountSeed(mnemonicArray: tonMnemonic)) + } }