Documentation
¶
Index ¶
- Variables
- func IsMnemonicValid(mnemonic string) bool
- func NewSeed(mnemonic string, opts ...NewSeedOption) []byte
- func NormalizeMnemonic(mnemonic string) string
- func SplitMnemonic(mnemonic string) (words []string, delimiter string)
- func WithEntropyBits(entropyBits int) func(*GenerateMnemonicOptions)
- func WithLanguage(language Language) func(*NewMnemonicOptions)
- func WithLanguages(languages []Language) func(*DetectLanguageOptions)
- func WithPassphrase(passphrase string) func(*NewSeedOptions)
- type DetectLanguageOption
- type DetectLanguageOptions
- type GenerateMnemonicOption
- type GenerateMnemonicOptions
- type Language
- type Mnemonic
- func (m *Mnemonic) ArbitraryEntropyToMnemonic(entropy []byte) (string, error)
- func (m *Mnemonic) ArbitraryMnemonicToEntropy(mnemonic string) ([]byte, error)
- func (m *Mnemonic) EntropyFromMnemonic(mnemonic string) ([]byte, error)
- func (m *Mnemonic) EntropyToMnemonic(entropy []byte) (string, error)
- func (m *Mnemonic) GenerateMnemonic(opts ...GenerateMnemonicOption) (string, error)
- type NewMnemonicOption
- type NewMnemonicOptions
- type NewSeedOption
- type NewSeedOptions
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidEntropy = errors.New("invalid entropy") ErrInvalidMnemonic = errors.New("invalid mnemonic") ErrInvalidNumberWords = errors.New("invalid number of words") ErrInvalidNumberEntropy = errors.New("invalid number of entropy") ErrChecksumIncorrect = errors.New("checksum incorrect") ErrEntropyTooLarge = errors.New("entropy too large: maximum 1024 bytes (8192 bits)") )
Functions ¶
func IsMnemonicValid ¶
IsMnemonicValid attempts to verify that the provided mnemonic is valid. Validity is determined by both the number of words being appropriate, and that all the words in the mnemonic are present in the word list.
func NewSeed ¶
func NewSeed(mnemonic string, opts ...NewSeedOption) []byte
NewSeed creates a hashed seed output given a provided string and password. No checking is performed to validate that the string provided is a valid mnemonic. default passphrase is empty string. if you want to set passphrase, use WithPassphrase() option.
func NormalizeMnemonic ¶
NormalizeMnemonic normalizes the mnemonic.
Example:
mnemonic := "おさえる けむり けしごむ うせつ もちろん とさか いはつ ざっか たりる こさめ いわい にいがた こてい ちんもく がぞう " mnemonic = NormalizeMnemonic(mnemonic) fmt.Println(mnemonic) // おさえる けむり けしごむ うせつ もちろん とさか いはつ ざっか たりる こさめ いわい にいがた こてい ちんもく がぞう
Example:
mnemonic := "carbon elder drip best unlock pool athlete fortune mixture exist bachelor quick faculty obey cliff" mnemonic = NormalizeMnemonic(mnemonic) fmt.Println(mnemonic) // carbon elder drip best unlock pool athlete fortune mixture exist bachelor quick faculty obey cliff
func SplitMnemonic ¶
SplitMnemonic splits a mnemonic into words and delimiter. If the delimiter is a Japanese space, then the language must be Japanese.
Example:
words, delimiter := SplitMnemonic("おさえる けむり けしごむ うせつ もちろん とさか いはつ ざっか たりる こさめ いわい にいがた こてい ちんもく がぞう")
fmt.Println(words) // ["おさえる", "けむり", "けしごむ", "うせつ", "もちろん", "とさか", "いはつ", "ざっか", "たりる", "こさめ", "いわい", "にいがた", "こてい", "ちんもく", "がぞう"]
fmt.Println(delimiter) // " "
Example:
words, delimiter := SplitMnemonic("carbon elder drip best unlock pool athlete fortune mixture exist bachelor quick faculty obey cliff")
fmt.Println(words) // ["carbon", "elder", "drip", "best", "unlock", "pool", "athlete", "fortune", "mixture", "exist", "bachelor", "quick", "faculty", "obey", "cliff"]
fmt.Println(delimiter) // " "
func WithEntropyBits ¶
func WithEntropyBits(entropyBits int) func(*GenerateMnemonicOptions)
WithEntropyBits sets the bits of the entropy.
func WithLanguage ¶
func WithLanguage(language Language) func(*NewMnemonicOptions)
WithLanguage sets the language of the mnemonic.
func WithLanguages ¶
func WithLanguages(languages []Language) func(*DetectLanguageOptions)
WithLanguages sets the languages to detect. If not set, all languages are possible.
func WithPassphrase ¶
func WithPassphrase(passphrase string) func(*NewSeedOptions)
WithPassphrase sets the passphrase used to generate the seed.
Types ¶
type DetectLanguageOption ¶
type DetectLanguageOption func(*DetectLanguageOptions)
DetectLanguageOption a function that modifies DetectLanguageOptions
type DetectLanguageOptions ¶
type DetectLanguageOptions struct {
// contains filtered or unexported fields
}
DetectLanguageOptions options for DetectLanguage function
type GenerateMnemonicOption ¶
type GenerateMnemonicOption func(*GenerateMnemonicOptions)
GenerateMnemonicOption a function that modifies GenerateMnemonicOptions
type GenerateMnemonicOptions ¶
type GenerateMnemonicOptions struct {
// contains filtered or unexported fields
}
GenerateMnemonicOptions options for GenerateMnemonic function
type Language ¶
type Language byte
func DetectLanguage ¶
func DetectLanguage(mnemonic string, opts ...DetectLanguageOption) (languages []Language, ok bool)
DetectLanguage detect and return the languages of the mnemonic. Default all languages are possible. In some cases, multiple languages might be matched simultaneously, such as Simplified Chinese and Traditional Chinese. If you only want to perform detection within a specified list of languages. use WithLanguages() option.
type Mnemonic ¶
type Mnemonic struct {
// contains filtered or unexported fields
}
Mnemonic
func NewMnemonic ¶
func NewMnemonic(opts ...NewMnemonicOption) (*Mnemonic, error)
NewMnemonic creates a new Mnemonic instance.
The default language is English. If you want to set the language, use WithLanguage() option.
func (*Mnemonic) ArbitraryEntropyToMnemonic ¶
ArbitraryEntropyToMnemonic converts arbitrary length entropy to a mnemonic. This is NOT strict BIP39 compliant. Use this for custom applications that need to encode payloads smaller than 128 bits or of arbitrary length.
If the entropy is not a multiple of 32 bits (4 bytes), it will be padded with zero bytes to the next 32-bit boundary. The minimum size after padding is 4 bytes (32 bits).
Maximum entropy size is 1024 bytes (8192 bits) due to SHA-256 checksum limitations. Beyond this size, ErrEntropyTooLarge is returned.
The resulting mnemonic will have a number of words based on the padded entropy size: - 4 bytes (32 bits) + 1 checksum bit = 3 words - 8 bytes (64 bits) + 2 checksum bits = 6 words - 12 bytes (96 bits) + 3 checksum bits = 9 words - 16 bytes (128 bits) + 4 checksum bits = 12 words (standard BIP39) - 1024 bytes (8192 bits) + 256 checksum bits = 768 words (maximum)
func (*Mnemonic) ArbitraryMnemonicToEntropy ¶
ArbitraryMnemonicToEntropy converts a mnemonic created with ArbitraryEntropyToMnemonic back to entropy bytes. This is the reverse operation of ArbitraryEntropyToMnemonic.
This is NOT strict BIP39 compliant. The returned entropy will be padded to a 32-bit boundary (matching what ArbitraryEntropyToMnemonic does).
Supported word counts: 3, 6, 9, 12, 15, 18, 21, 24 words - 3 words = 4 bytes (32 bits) - 6 words = 8 bytes (64 bits) - 9 words = 12 bytes (96 bits) - 12 words = 16 bytes (128 bits) (standard BIP39) - etc.
func (*Mnemonic) EntropyFromMnemonic ¶
EntropyFromMnemonic converts a mnemonic to entropy.
func (*Mnemonic) EntropyToMnemonic ¶
EntropyToMnemonic converts entropy to a mnemonic.
The entropy must be in [16, 20, 24, 28, 32] bytes. Corresponding to [128, 160, 192, 224, 256] bits.
func (*Mnemonic) GenerateMnemonic ¶
func (m *Mnemonic) GenerateMnemonic(opts ...GenerateMnemonicOption) (string, error)
GenerateMnemonic generates a new mnemonic.
The default entropy bits is 128. If you want to set the entropy bits, use WithEntropyBits() option. The entropy bits must be in [128, 160, 192, 224, 256]. Corresponding to [16, 20, 24, 28, 32] bytes.
type NewMnemonicOption ¶
type NewMnemonicOption func(*NewMnemonicOptions)
NewMnemonicOption a function that modifies NewMnemonicOptions
type NewMnemonicOptions ¶
type NewMnemonicOptions struct {
// contains filtered or unexported fields
}
NewMnemonicOptions options for NewMnemonic function
type NewSeedOption ¶
type NewSeedOption func(*NewSeedOptions)
NewSeedOption a function that modifies NewSeedOptions
type NewSeedOptions ¶
type NewSeedOptions struct {
// contains filtered or unexported fields
}
NewSeedOptions options for NewSeed function