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

Generating 12-word mnemonic from private key #79

Closed
barrasso opened this issue Oct 30, 2018 · 1 comment
Closed

Generating 12-word mnemonic from private key #79

barrasso opened this issue Oct 30, 2018 · 1 comment
Labels
question Further information is requested

Comments

@barrasso
Copy link
Contributor

barrasso commented Oct 30, 2018

Hey all, I would like to get a mnemonic representation of a private key. Right now, I am getting 24 words by doing the following:

    private func generateKey() -> String {
        let userDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        var ks: EthereumKeystoreV3?
        ks = try! EthereumKeystoreV3(password: "PASSWORD")
        let keydata = try! JSONEncoder().encode(ks!.keystoreParams)
        FileManager.default.createFile(atPath: userDir + "/keystore"+"/key.json", contents: keydata, attributes: nil)
        var mnemonic: String = ""
        do {
            if let pk = try ks?.UNSAFE_getPrivateKeyData(password: "PASSWORD", account: (ks?.addresses?.first)!) {
                print("pk to hex: \(pk.toHexString())")
                let entropy = Data(hex: pk.toHexString())
                mnemonic = Mnemonic.create(entropy: entropy)
                print("pk hex to menmonic: \(mnemonic)")
            }
        } catch {print("Error with getting private key: \(error)")}
        return mnemonic
    }

I am using the Mnemonic helper class found here: https://github.com/essentiaone/HDWallet/blob/develop/HDWalletKit/Mnemonic/Mnemonic.swift

This is the Mnemonic.create function:

    public static func create(entropy: Data, language: WordList = .english) -> String {
        let entropybits = String(entropy.flatMap { ("00000000" + String($0, radix: 2)).suffix(8) })
        let hashBits = String(entropy.sha256().flatMap { ("00000000" + String($0, radix: 2)).suffix(8) })
        let checkSum = String(hashBits.prefix((entropy.count * 8) / 32))
        
        let words = language.words
        let concatenatedBits = entropybits + checkSum
        
        var mnemonic: [String] = []
        for index in 0..<(concatenatedBits.count / 11) {
            let startIndex = concatenatedBits.index(concatenatedBits.startIndex, offsetBy: index * 11)
            let endIndex = concatenatedBits.index(startIndex, offsetBy: 11)
            let wordIndex = Int(strtoul(String(concatenatedBits[startIndex..<endIndex]), nil, 2))
            mnemonic.append(String(words[wordIndex]))
        }
        
        return mnemonic.joined(separator: " ")
    }

A couple questions:

  • Would the user still be able to derive their private key from this mnemonic?
  • Also, instead of generating a 24-word mnemonic, is it possible to generate a 12-word mnemonic?
@barrasso
Copy link
Contributor Author

I was able to generate a 12-word mnemonic by changing the following:

        for index in 0..<(concatenatedBits.count / 11) {
            ...

Changed to:

        for index in 0..<(concatenatedBits.count / 22) {
            ...

concatenatedBits.count is equal to 264. By dividing by 22, the for loop index now runs from 0 to 11.

@skywinder skywinder added the question Further information is requested label Nov 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants