We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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
https://github.com/essentiaone/HDWallet/blob/develop/HDWalletKit/Mnemonic/Mnemonic.swift
This is the Mnemonic.create function:
Mnemonic.create
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:
The text was updated successfully, but these errors were encountered:
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.
concatenatedBits.count
Sorry, something went wrong.
No branches or pull requests
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:
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:A couple questions:
The text was updated successfully, but these errors were encountered: