Skip to content

Generating Mnemonics for HPB

BlockGeek edited this page Apr 24, 2019 · 3 revisions

1 BIP32, BIP39, BIP44

  • BIP32: Hierarchical Deterministic wallet (short for "HD Wallet") can be defined as a system which can generate a tree structure from a single seed to store multiple sets of keypairs (private and public keys). The advantage is that it can be easily backed up and transferred to other compatible devices (for the seed is all that’s needed across devices). Besides hierarchical permission control can be achieved with such a design.
  • BIP39: The seed is represented by a single word that is easy to remember and write, usually composed of 12 letters or digits. This is called mnemonic code (phrase). For example:

rose rocket invest real refuse margin festival danger anger border idle brown

  • BIP44: A BIP32-based system which gives special meaning to the layers in the tree structure. A single seed can support multiple currencies and multiple accounts. The multiple layers involved can be defined as follows:

m / purpose' / coin_type' / account' / change / address_index

In the above digram, the fixed value for purpose’ stands at 44’, whereas it typically adopts BIP44. coin_type' is used to refer to different currencies, i.e. Bitcoin is represented by 0’, and Ethereum is represented by 60’.

2 Generating Mnemonics

HPB Wallet now uses BIP39, which reduces the 64-character private key down to just 12 characters, making it much easier to memorize.

Open the BIP39.swift file and generate a random mnemonic by adopting the following method:

static public func generateMnemonics(bitsOfEntropy: Int, language: BIP39Language = BIP39Language.english) throws -> String? {
        guard bitsOfEntropy >= 128 && bitsOfEntropy <= 256 && bitsOfEntropy % 32 == 0 else {return nil}
        guard let entropy = Data.randomBytes(length: bitsOfEntropy/8) else {throw AbstractKeystoreError.noEntropyError}
        return BIP39.generateMnemonicsFromEntropy(entropy: entropy, language: language)
        
    }     

One can acquire the seed with the help of the mnemonic and then get the private key through sha256

 let seed = BIP39.seedFromMmemonics(mnemonic, language: BIP39Language.english)
 let privateKey = seed.sha256()

2.1 Backing up Mnemonics

Since he who has the mnemonic has the power to control the account, the user needs to be reminded to back his mnemonic up once it is created. For decentralized apps, users can store the encrypted version locally but, when the back-up is completed, must delete the mnemonic from the local hard drive.

In the next chapter, we will explain how to import the Wallet.

Clone this wiki locally