mnemonic

package module
v0.0.0-...-343f179 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Mnemonic

GitHub license GoDoc Go Report Card

Overview

The Mnemonic package provides a flexible and customizable way to encode data into a mnemonic word list. It implements the BIP-0039 specification using big.Int, allowing for the use of arbitrary entropy sizes.

Features

  • BIP-0039 Specification: The implementation adheres to the BIP-0039 specification, providing compatibility with various cryptographic systems.

  • Arbitrary Entropy Sizes: This package allows users to work with arbitrary entropy sizes, providing flexibility in encoding different types of data.

  • Number Obfuscation: For enhanced usability with IDs, the package offers optional number obfuscation. This feature uses Modular Multiplicative Inverse to convert the provided number into a seemingly random number before generating the mnemonic word list using https://github.com/c2h5oh/hide

Getting Started

Installation

To use this package in your Go project, run the following command:

go get github.com/DIMO-Network/mnemonic
Example Usage

All Examples can be found in the go docs or examples_test.go

package main

import (
	"fmt"
	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Example usage with a number
	id := int32(1)
	words := mnemonic.FromInt(id)
	obfuscatedWords := mnemonic.FromInt32WithObfuscation(id)
	fmt.Println(words)
	fmt.Println(obfuscatedWords)
	// Output: [abandon abandon about]
	// [learn island zoo]

	// Example usage with a word list
	obfuscatedWords := []string{"learn", "island", "zoo"}
	deobfuscatedUint, err := mnemonic.ToUint32WithDeobfuscation(obfuscatedWords)
	if err != nil {
		panic(err)
	}

	fmt.Println(deobfuscatedUint)
	// Output: 1
}

Documentation

Overview

Package mnemonic encodes data into menemonic word list. encoding is implements the BIP-0039 specification using big.Int. This implementations allows for the use of arbitrary data sizes. This package also provides optional number obfuscation. Obfuscation uses Modular multiplicative inverse to convert the provided number to a seemingly random number before creating the mnemonic word list.

Index

Examples

Constants

View Source
const (
	// ObfuscationPrime is the prime number used to obfuscate the data.
	ObfuscationPrime = 2127482879
)

Variables

View Source
var (

	// ErrInvalidBitSize is returned when the number of bits is not a multiple of 32.
	ErrInvalidBitSize = errors.New("invalid bit size")

	// ErrInvalidWord is returned when the word is not in the word list.
	ErrInvalidWord = errors.New("invalid words")

	// ErrInvalidChecksum is returned when the checksum is invalid.
	ErrInvalidChecksum = errors.New("invalid checksum")
)
View Source
var (

	// Hider used for optional data obfuscattion.
	Hider = hide.Hide{}

	// ErrInvalidData is returned when the data is invalid.
	ErrInvalidData = errors.New("invalid data")
)

Functions

func FromBigInt

func FromBigInt(entBin *big.Int) []string

FromBigInt converts a big int to a list of mnemonic words. The big int is padded to the next highest number divisible by 32.

func FromBigIntFixed

func FromBigIntFixed(data *big.Int, sizeBits int) ([]string, error)

FromBigIntFixed converts a big int to a list of mnemonic words. The size is the number of bits to be used and must be a nonzero multiple of 32. If the provided data is smaller than the desired bits, it will be padded with 0s. If the provided data is larger than the desired bits the rightmost bits will be truncated.

func FromBytes

func FromBytes(data []byte) ([]string, error)

FromBytes converts a byte slice to a list of mnemonic words. The length in bits of the byte slice must be a multiple of 32.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate a mnemonic from a byte slice
	bytes := []byte{'z', 35, 67, 0xff, 0x89, 0, 0xcd, 0xef}
	words, err := mnemonic.FromBytes(bytes)
	if err != nil {
		panic(err)
	}

	fmt.Println(words)
}
Output:

[kick borrow zoo bamboo art wasp]

func FromHex

func FromHex(data string) ([]string, error)

FromHex converts a hex string to a list of mnemonic words. The length in bits of the hex string must be a multiple of 32.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate a mnemonic from a hex string
	hex := "023456789abcdef0"
	words, err := mnemonic.FromHex(hex)
	if err != nil {
		panic(err)
	}

	fmt.Println(words)
}
Output:

[acquire pencil owner cube social journey]

func FromInt

func FromInt[T constraints.Signed](data T) []string

FromInt converts a signed integer to a list of mnemonic words.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate a mnemonic from an integer
	words := mnemonic.FromInt(1234567890)

	fmt.Println(words)
}
Output:

[end quote region]

func FromInt32WithObfuscation

func FromInt32WithObfuscation(data int32) []string

FromInt32WithObfuscation behaves the same as FromInt, but the provided data is obfuscated first.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate a mnemonic from an obfuscated integer
	obfuscatedInt := int32(1)
	words := mnemonic.FromInt(obfuscatedInt)
	obfuscatedWords := mnemonic.FromInt32WithObfuscation(obfuscatedInt)
	fmt.Println(words)
	fmt.Println(obfuscatedWords)
}
Output:

[abandon abandon about]
[learn island zoo]

func FromInt64WithObfuscation

func FromInt64WithObfuscation(data int64) []string

FromInt64WithObfuscation behaves the same as FromInt, but the provided data is obfuscated first.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate a mnemonic from an obfuscated integer
	obfuscatedInt := int64(1)
	words := mnemonic.FromInt(obfuscatedInt)
	obfuscatedWords := mnemonic.FromInt64WithObfuscation(obfuscatedInt)

	fmt.Println(words)
	fmt.Println(obfuscatedWords)
}
Output:

[abandon abandon about]
[learn island zoo]

func FromUint

func FromUint[T constraints.Unsigned](data T) []string

FromUint converts an unsigned integer to a list of mnemonic words.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate a mnemonic from an unsigned integer
	unsignedInt := uint(1234567890)
	words := mnemonic.FromUint(unsignedInt)

	fmt.Println(words)
}
Output:

[end quote region]

func FromUint32WithObfuscation

func FromUint32WithObfuscation(data uint32) []string

FromUint32WithObfuscation behaves the same as FromUint, but the provided data is obfuscated first.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate a mnemonic from an obfuscated unsigned integer
	obfuscatedUint := uint32(1)
	words := mnemonic.FromUint(obfuscatedUint)
	obfuscatedWords := mnemonic.FromUint32WithObfuscation(obfuscatedUint)

	fmt.Println(words)
	fmt.Println(obfuscatedWords)
}
Output:

[abandon abandon about]
[learn island zoo]

func FromUint64WithObfuscation

func FromUint64WithObfuscation(data uint64) []string

FromUint64WithObfuscation behaves the same as FromUint, but the provided data is obfuscated first.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate a mnemonic from an obfuscated unsigned integer
	obfuscatedUint := uint64(1)
	words := mnemonic.FromUint(obfuscatedUint)
	obfuscatedWords := mnemonic.FromUint64WithObfuscation(obfuscatedUint)

	fmt.Println(words)
	fmt.Println(obfuscatedWords)
}
Output:

[abandon abandon about]
[learn island zoo]

func NextNumberDivisibleBy32

func NextNumberDivisibleBy32(num int) int

NextNumberDivisibleBy32 returns the next number that is divisible by 32. If the number is 0, it returns 32.

func ToBigInt

func ToBigInt(words []string) (*big.Int, error)

ToBigInt converts a list of mnemonic words to a big int.

func ToBytes

func ToBytes(words []string) ([]byte, error)

ToBytes converts a list of mnemonic words to a byte slice.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate a byte slice from a mnemonic
	words := []string{"kick", "borrow", "zoo", "bamboo", "art", "wasp"}
	bytes, err := mnemonic.ToBytes(words)
	if err != nil {
		panic(err)
	}

	fmt.Println(bytes)
}
Output:

[122 35 67 255 137 0 205 239]

func ToHex

func ToHex(words []string) (string, error)

ToHex converts a list of mnemonic words to a hex string.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate a hex string from a mnemonic
	words := []string{"acquire", "pencil", "owner", "cube", "social", "journey"}
	hex, err := mnemonic.ToHex(words)
	if err != nil {
		panic(err)
	}

	fmt.Println(hex)
}
Output:

023456789abcdef0

func ToInt

func ToInt(words []string) (int64, error)

ToInt converts a list of mnemonic words to an int64.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate an integer from a mnemonic
	words := []string{"end", "quote", "region"}
	number, err := mnemonic.ToInt(words)
	if err != nil {
		panic(err)
	}

	fmt.Println(number)
}
Output:

1234567890

func ToInt32WithDeobfuscation

func ToInt32WithDeobfuscation(words []string) (int32, error)

ToInt32WithDeobfuscation behaves the same as ToInt, but the result is deobfuscated.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate an integer from an obfuscated mnemonic
	obfuscatedWords := []string{"learn", "island", "zoo"}
	deobfuscatedInt, err := mnemonic.ToInt32WithDeobfuscation(obfuscatedWords)
	if err != nil {
		panic(err)
	}

	fmt.Println(deobfuscatedInt)
}
Output:

1

func ToInt64WithDeobfuscation

func ToInt64WithDeobfuscation(words []string) (int64, error)

ToInt64WithDeobfuscation behaves the same as ToInt, but the result is deobfuscated.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate an integer from an obfuscated mnemonic
	obfuscatedWords := []string{"learn", "island", "zoo"}
	deobfuscatedInt, err := mnemonic.ToInt64WithDeobfuscation(obfuscatedWords)
	if err != nil {
		panic(err)
	}

	fmt.Println(deobfuscatedInt)
}
Output:

1

func ToUint

func ToUint(words []string) (uint64, error)

ToUint converts a list of mnemonic words to a uint64.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate an unsigned integer from a mnemonic
	words := []string{"end", "quote", "region"}
	unsignedInt, err := mnemonic.ToUint(words)
	if err != nil {
		panic(err)
	}

	_, _ = fmt.Printf("Value: %d Type: %T\n", unsignedInt, unsignedInt)
}
Output:

Value: 1234567890 Type: uint64

func ToUint32WithDeobfuscation

func ToUint32WithDeobfuscation(words []string) (uint32, error)

ToUint32WithDeobfuscation behaves the same as ToUint, but the result is deobfuscated.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate an unsigned integer from an obfuscated mnemonic
	obfuscatedWords := []string{"learn", "island", "zoo"}
	deobfuscatedUint, err := mnemonic.ToUint32WithDeobfuscation(obfuscatedWords)
	if err != nil {
		panic(err)
	}

	fmt.Println(deobfuscatedUint)
}
Output:

1

func ToUint64WithDeobfuscation

func ToUint64WithDeobfuscation(words []string) (uint64, error)

ToUint64WithDeobfuscation behaves the same as ToUint, but the result is deobfuscated.

Example
package main

import (
	"fmt"

	"github.com/DIMO-Network/mnemonic"
)

func main() {
	// Generate an unsigned integer from an obfuscated mnemonic
	obfuscatedWords := []string{"learn", "island", "zoo"}
	deobfuscatedUint, err := mnemonic.ToUint64WithDeobfuscation(obfuscatedWords)
	if err != nil {
		panic(err)
	}

	fmt.Println(deobfuscatedUint)
}
Output:

1

Types

This section is empty.

Jump to

Keyboard shortcuts

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