Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to detect multi-account mnemonic #20

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Source/TonSwift/Mnemonic/Mnemonic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions Tests/TonSwiftTests/Mnemonic/MnemonicTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Loading