gobip39

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: MIT Imports: 11 Imported by: 2

README

Go BIP39

Golang implementation of BIP-0039 deterministic mnemonic code and key generation algorithm

Example

Genetate new mnemonic and extract seed

package main

import (
	"log"

	bip39 "github.com/viwet/GoBIP39"
	"github.com/viwet/GoBIP39/words"
)

const Password = ""

func main() {
	entorpy, err := bip39.NewEntropy(256)
	if err != nil {
		log.Fatal(err)
	}

	mnemonic, err := bip39.ExtractMnemonic(entorpy, words.English)
	if err != nil {
		log.Fatal(err)
	}

	seed, err := bip39.ExtractSeed(mnemonic, words.English, Password)
	if err != nil {
		log.Fatal(err)
	}

	...
}

Extract seed from existing mnemonic

package main

import (
	"log"

	bip39 "github.com/viwet/GoBIP39"
	"github.com/viwet/GoBIP39/words"
)

const (
    Mnemonic = "あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あいこくしん あおぞら"
    Password = "password"
)

func main() {
	mnemonic := bip39.SplitMnemonic(Mnemonic)

	seed, err := bip39.ExtractSeed(mnemonic, words.Japanse, Password)
	if err != nil {
		log.Fatal(err)
	}

	...
}

Documentation

Index

Constants

View Source
const (
	// Minimum allowed by BIP-39 entropy bitlen
	MinBitlen = 128
	// Maximum allowed by BIP-39 entropy bitlen
	MaxBitlen = 256
	// Divisor that used to get checksum bits of SHA-256 hash
	BitlenDivisor = 32

	// Bits are splitted into groups of 11 bits, which are represents an index of the word
	GroupBitlen = 11
)
View Source
const (
	// Minimum allowed by BIP-39 mnemonic length
	MinLength = 12
	// Maximum allowed by BIP-39 mnemonic length
	MaxLength = 24

	// Mnemonic length divisor
	LengthDivisor = 3
)
View Source
const (
	// BIP-39 Password prefix
	PasswordPrefix = "mnemonic"
	// PBKDF2 Iterations count
	Iterations = 2048
	// PBKDF2 Derived key length
	KeyLength = 64
)

Variables

View Source
var (
	// ErrInvalidMnemonicLength
	ErrInvalidMnemonicLength = errors.New("mnemonic length must be one of 12, 15, 18, 21 or 24")
	// ErrInvalidMnemonicChecksum
	ErrInvalidMnemonicChecksum = errors.New("invalid mnemonic checksum")
)
View Source
var ErrInvalidEntropyBitlen = errors.New("entropy bitlen must be one of 128, 160, 192, 224 or 256")

ErrInvalidEntropyBitlen

Functions

func BitlenToBytelen

func BitlenToBytelen(bitlen uint) uint

BitlenToBytelen converts a bitlet to a bytelen

func BytelenToBitlen

func BytelenToBitlen(bytelen uint) uint

BytelenToBitlen converts a bytelen to a bitlen

func ExtractMnemonic

func ExtractMnemonic(entropy Entropy, list words.List) ([]string, error)

ExtractMnemonic returns mnemonic phrase based on given entropy and language

func ExtractSeed

func ExtractSeed(mnemonic []string, list words.List, password string) ([]byte, error)

ExtractSeed returns seed derived from mnemonic and password

func NormalizeMnemonic

func NormalizeMnemonic(mnemonic []string) []string

NormalizeMnemonic returns NFKD normalized UTF-8 encoded mnemonic

func PadLeftToBitlen

func PadLeftToBitlen(bytes []byte, bitlen uint) []byte

PadLeftToBitlen pads a byte slice to a given bitlen if needed

func RandomBytes

func RandomBytes(length uint) []byte

RandomBytse generates random bytes sequence of given length

func SplitMnemonic

func SplitMnemonic(mnemonic string) []string

SplitMnemonic splits NFKD normalized string into words

func ValidateMnemonic

func ValidateMnemonic(mnemonic []string, list words.List) error

ValidateMnemonic returns error if mnemonic has invalid length or it is impossible to extract entropy

Types

type ChecksumEntropy

type ChecksumEntropy []byte

Entropy with checksum

func (ChecksumEntropy) Bitlen

func (e ChecksumEntropy) Bitlen() uint

Bitlen returns a bitlen of entorpy with checksum

func (ChecksumEntropy) EntropyBitlen

func (e ChecksumEntropy) EntropyBitlen() uint

EntropyBitlen returns a bitlen of entropy without checksum

func (ChecksumEntropy) RemoveChecksum

func (e ChecksumEntropy) RemoveChecksum() (Entropy, []byte)

RemoveChecksum returns initial entropy and checksum separately

type Entropy

type Entropy []byte

Initial entropy

func ExtractEntropy

func ExtractEntropy(mnemonic []string, list words.List) (Entropy, error)

ExtractEntropy returns entropy based on given mnemonic phrase and language

func NewEntropy

func NewEntropy(bitlen uint) (Entropy, error)

NewEntropy generates new random entropy of bitlen

func (Entropy) AddChecksum

func (e Entropy) AddChecksum() ChecksumEntropy

AddChecksum returns entropy with checksum

func (Entropy) Bitlen

func (e Entropy) Bitlen() uint

Bitlen return a bitlen of entropy

func (Entropy) IsValidChecksum

func (e Entropy) IsValidChecksum(checksum []byte) bool

IsValidChecksum validates entorpy checksum

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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