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 ¶
- Constants
- Variables
- func FromBigInt(entBin *big.Int) []string
- func FromBigIntFixed(data *big.Int, sizeBits int) ([]string, error)
- func FromBytes(data []byte) ([]string, error)
- func FromHex(data string) ([]string, error)
- func FromInt[T constraints.Signed](data T) []string
- func FromInt32WithObfuscation(data int32) []string
- func FromInt64WithObfuscation(data int64) []string
- func FromUint[T constraints.Unsigned](data T) []string
- func FromUint32WithObfuscation(data uint32) []string
- func FromUint64WithObfuscation(data uint64) []string
- func NextNumberDivisibleBy32(num int) int
- func ToBigInt(words []string) (*big.Int, error)
- func ToBytes(words []string) ([]byte, error)
- func ToHex(words []string) (string, error)
- func ToInt(words []string) (int64, error)
- func ToInt32WithDeobfuscation(words []string) (int32, error)
- func ToInt64WithDeobfuscation(words []string) (int64, error)
- func ToUint(words []string) (uint64, error)
- func ToUint32WithDeobfuscation(words []string) (uint32, error)
- func ToUint64WithDeobfuscation(words []string) (uint64, error)
Examples ¶
Constants ¶
const (
// ObfuscationPrime is the prime number used to obfuscate the data.
ObfuscationPrime = 2127482879
)
Variables ¶
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") )
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
NextNumberDivisibleBy32 returns the next number that is divisible by 32. If the number is 0, it returns 32.
func ToBytes ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.