mnemo

package module
v0.0.0-...-36f5957 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 2, 2019 License: MIT Imports: 7 Imported by: 0

README

mnemo

A bip-39 implementation in Go.

Documentation

You can get the documentation here.

Usage

package main

import(
	"fmt"
	
	"github.com/gnkz/mnemo"
	"github.com/gnkz/mnemo/en"
)

func main() {
    // Generate a 128 bit entropy
	ent, err := mnemo.Entropy(mnemo.Entropy128)
	if err != nil {
		panic(err)	
	}

    // Generate the mnemonic using the entropy and the english set of words
	mnemonic, err := mnemo.NewFromEntropy(ent, en.New())
	if err != nil {
		panic(err)
	}

	fmt.Println(mnemonic)

    // Generate a master seed using the mnemonic and a passphrase
	seed := mnemo.Seed(mnemonic, "sup3rs3cr3t")

	fmt.Printf("%x\n", seed) 
}

License

Copyright 2019 gnkz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Checksum

func Checksum(entropy []byte) (byte, uint8, error)

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

func Seed(rawMenmonic, passphrase string) []byte

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
)

Directories

Path Synopsis
Package mock_mnemo is a generated GoMock package.
Package mock_mnemo is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL