Skip to content

Commit

Permalink
Merge pull request #20 from tonkeeper/feature/is-multi-account-mnemon…
Browse files Browse the repository at this point in the history
…ic-method

Add method to detect multi-account mnemonic
  • Loading branch information
voloshinskii authored Oct 8, 2024
2 parents aae2414 + 9424efb commit 5f58cb5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
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))
}
}

0 comments on commit 5f58cb5

Please sign in to comment.