Documentation ¶
Overview ¶
Package mnemo is an implementation of the Bip-39 mnemonic code generation scheme. A mnemonic code is a set of words that are used to generate seeds to create HD wallets.
Index ¶
- func Checksum(entropy []byte) (byte, uint8, error)
- func Entropy(length EntropyLength) ([]byte, error)
- func New(length MnemonicLength, dict Dictionary) (string, error)
- func NewFromEntropy(entropy []byte, dict Dictionary) (string, error)
- func Seed(rawMenmonic, passphrase string) []byte
- type Dictionary
- type EntropyLength
- type MnemonicLength
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Checksum ¶
Checksum returns the first N bits of the sha256 hash of the entropy.
The entropy must be of 128, 160, 192, 224 or 256 bits.
128 bits entropy generates a 4 bits checksum
160 bits entropy generates a 5 bits checksum
192 bits entropy generates a 6 bits checksum
224 bits entropy generates a 7 bits checksum
256 bits entropy generates a 8 bits checksum
Example ¶
package main import ( "encoding/hex" "fmt" "github.com/gnkz/mnemo" ) func main() { entropy, _ := hex.DecodeString("9e885d952ad362caeb4efe34a8e91bd2") cs, cslen, _ := mnemo.Checksum(entropy) fmt.Println(cs) fmt.Println(cslen) }
Output: 1 4
func Entropy ¶
func Entropy(length EntropyLength) ([]byte, error)
Entropy returns an entropy of length bits. If the length is different from 128, 160, 192, 224 or 256 it will return an error
Example ¶
package main import ( "fmt" "github.com/gnkz/mnemo" ) func main() { en, _ := mnemo.Entropy(mnemo.Entropy128) fmt.Println(len(en)) }
Output: 16
func New ¶
func New(length MnemonicLength, dict Dictionary) (string, error)
New generates a random mnemonic of a determined length using words from a Dictionary
Example ¶
package main import ( "fmt" "strings" "github.com/gnkz/mnemo" "github.com/gnkz/mnemo/en" ) func main() { dict := en.New() mnemonic, _ := mnemo.New(mnemo.Words12, dict) words := strings.Split(mnemonic, dict.Separator()) fmt.Println(len(words)) }
Output: 12
func NewFromEntropy ¶
func NewFromEntropy(entropy []byte, dict Dictionary) (string, error)
NewFromEntropy creates a new mnemonic using a valid entropy and a Dictionary
Example ¶
package main import ( "encoding/hex" "fmt" "github.com/gnkz/mnemo" "github.com/gnkz/mnemo/en" ) func main() { entropy, _ := hex.DecodeString("9e885d952ad362caeb4efe34a8e91bd2") mnemonic, _ := mnemo.NewFromEntropy(entropy, en.New()) fmt.Println(mnemonic) }
Output: ozone drill grab fiber curtain grace pudding thank cruise elder eight picnic
func Seed ¶
Seed creates a master seed from a rawMnemonic and a passphrase
Example ¶
package main import ( "fmt" "github.com/gnkz/mnemo" ) func main() { mnemonic := "gravity machine north sort system female filter attitude volume fold club stay feature office ecology stable narrow fog" passphrase := "TREZOR" seed := mnemo.Seed(mnemonic, passphrase) fmt.Printf("%x", seed) }
Output: 628c3827a8823298ee685db84f55caa34b5cc195a778e52d45f59bcf75aba68e4d7590e101dc414bc1bbd5737666fbbef35d1f1903953b66624f910feef245ac
Types ¶
type Dictionary ¶
type Dictionary interface { // Returns a word at certain position. If the index does not exists this should return an error Word(index int) (string, error) // Returns a string used as separator for each word in the mnemonic Separator() string }
A Dictionary represents a list of words and a separator used to create a mnemonic
type EntropyLength ¶
type EntropyLength int
EntropyLength defines a set of defined entropy length in bits
const ( // Entropy128 represents a 128 bits length entropy Entropy128 EntropyLength = 32 * (4 + iota) // Entropy160 represents a 160 bits length entropy Entropy160 // Entropy192 represents a 192 bits length entropy Entropy192 // Entropy224 represents a 224 bits length entropy Entropy224 // Entropy256 represents a 256 bits length entropy Entropy256 )
type MnemonicLength ¶
type MnemonicLength int
MnemonicLength represents the number of words to include in the mnemonic
const ( // Words12 represents a 12 words mnemonic Words12 MnemonicLength = 12 + (3 * iota) // Words15 represents a 15 words mnemonic Words15 // Words18 represents a 18 words mnemonic Words18 // Words21 represents a 21 words mnemonic Words21 // Words24 represents a 24 words mnemonic Words24 )