Giter VIP home page Giter VIP logo

go-bip39's Introduction

The bip39 library for Go

PkgGoDev Latest release MIT License Contributors

Build check Go Report Card Coverage Status

Example

package main

import (
  "fmt"
  "github.com/tyler-smith/go-bip39"
  "github.com/tyler-smith/go-bip32"
)

func main(){
  // Generate a mnemonic for memorization or user-friendly seeds
  entropy, _ := bip39.NewEntropy(256)
  mnemonic, _ := bip39.NewMnemonic(entropy)

  // Generate a Bip32 HD wallet for the mnemonic and a user supplied password
  seed := bip39.NewSeed(mnemonic, "Secret Passphrase")

  masterKey, _ := bip32.NewMasterKey(seed)
  publicKey := masterKey.PublicKey()

  // Display mnemonic and keys
  fmt.Println("Mnemonic: ", mnemonic)
  fmt.Println("Master private key: ", masterKey)
  fmt.Println("Master public key: ", publicKey)
}

go-bip39's People

Contributors

ashfame avatar bartekn avatar carryforward avatar cornfeedhobo avatar dieswaytoofast avatar drhayt avatar glendc avatar guotie avatar manan19 avatar meiyoung avatar thepiachu avatar tiero avatar tyler-smith avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-bip39's Issues

🐜[Bug Report] Error when using Gomobile: func `GetWordIndex` returns wrong type

Problem

When preparing the package to call from swift, the gomobile bind command returns an error.

Here are the logs:

myterminal $ go-bip39 % go get golang.org/x/mobile/cmd/gobind                       
go: added golang.org/x/mobile v0.0.0-20231006135142-2b44d11868fe
myterminal $ go-bip39 % gomobile bind -target ios -o ../frameworks/bip39.xcframework
gomobile: /Users/myterminal/go-workspace/bin/gobind -lang=go,objc -outdir=/var/folders/hp/rh26j25gz_7vm0000gp/T/gomobile-work-825612255/ios -tags=ios github.com/tyler-smith/go-bip39 failed: exit status 1
functions and methods must return either zero or one values, and optionally an error
second result value must be of type error: func github.com/tyler-smith/go-bip39.GetWordIndex(word string) (int, bool)
second result value must be of type error: func github.com/tyler-smith/go-bip39.GetWordIndex(word string) (int, bool)
second result value must be of type error: func github.com/tyler-smith/go-bip39.GetWordIndex(word string) (int, bool)

Not sure how to modify the issue @tyler-smith to create a workaround.

Is there a workaround? Some of the logs suggested that the crypto library had a corrupted go.sum.

Add support for Japanese word separators

As per the BIP39 spec, Japanese mnemonics must be generated with a special separator. This will probably require wordlists to be structs with a separator property instead of simple []slices

Issue new release as 1.0.1 (for go modules)

By default, go modules is picking up the latest release (1.0.0 tag) which is older. Would it be possible to do a 1.0.1 release, so go mod picks up the latest?

Alternatively go module users will need to go get github.com/tyler-smith/go-bip39@master.

Thanks!

MnemonicToByteArray does not validate NewMnemonic

After generating a mnemonic from NewMnemonic and feeding it to MnemonicToByteArray, the result sometimes fails. For example, passing the seed of 00ae885f8a13b01b698bd3f973d1ec9b generates "absent inmate blood before deposit asset plate kidney west ozone kite cushion", which returns an error of Mnemonic checksum error. Check words are in correct order. (decoded byte 1).

Possible optimisations

Hi, I used your implementation as a reference but managed to get some optimisations in.
Bellow you can see a benchmark between yours (BIP39) and mine (MWords).

BenchmarkBIP39-4    	  200000	      8332 ns/op	    1343 B/op	      59 allocs/op
BenchmarkMWords-4   	  500000	      3856 ns/op	     816 B/op	       7 allocs/op

If you'd like to have a look over the implementation you can find it here:
https://github.com/cpl/cryptor/tree/master/crypt/mwords

The main changes are in this function:
https://github.com/cpl/cryptor/blob/master/crypt/mwords/mwords.go#L61

Add test vectors for all langauges

AFAIK only English and Japanese exist, so that leaves only Japanese todo. But it would be great if we get get or create vectors for each language we support. Anybody speak Spanish, Italian, Chinese, or Korean?

MnemonicToByteArray fails on test vectors 0, 4, 8.

I have not spent much time trying to figure out why, but MnemonicToByteArray fails to return some vectors back into bytes.

This is not really necessary as the process of converting back into bytes is not really required for bip0039.

Thanks,
Rick

Add functionality to generate final checksum words for a partial mnemonic?

Hi Tyler, thanks for your work on this library!

Would you consider it in-scope to include a function to generate a set of valid final checksum words given a partial (N-1) mnemonic? This would be useful for people who are wanting to generate their own seeds using dice or words-out-of-a-hat etc.

If so, would you accept a PR for this? Perhaps an API like:

func ChecksumWords(partialWords []string) ([]string, error)

?

There are python versions of this floating around e.g. https://github.com/avsync/bip39chk, https://github.com/veebch/Bip39-Dice/, but I'm not aware of a Go version.

Mnemonic whitespace separator deviates from reference implementation

The use of strings.Fields in splitMnemonicWords allow more than one whitespace character between words.

https://golang.org/pkg/strings/#Fields

Fields splits the string s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace

The bip39 spec is not explicit about how words are to be separated.

The reference implementation uses a single space for the separator, after normalizing unicode to ascii:

https://github.com/trezor/python-mnemonic/blob/7d23efe34f3dd537996f31dae34f663207dac232/mnemonic/mnemonic.py#L153

Remove reliance on a package-level wordlist

Currently there are package level variables holding the wordlist to use. This is not ideal because it makes using the library with multiple languages difficult.

I've started an attempt at a v2 of this lib that creates an Encoder type that encapsulates the wordlist. Encoders for each built-in language are provided. Package-root level functions that proxy to the English Encoder are provided.

https://github.com/tyler-smith/go-bip39/tree/TS_v2

Please give feedback/thoughts on this approach.

Command line usage

I would like to be able to run this as a stand-alone mnemonic password generator from a terminal like:

$ ./bip39 --entropy=256 --lang=en --mnemoniconly

And grab the password from stdout for use in another program. Would something like this be feasible or within the scope of this project?

Expand README example with reverse validation

Hi! I just wanted to suggest to expand the main example with a validation test starting from a set of known keys:

package main

import (
	"fmt"

	"github.com/tyler-smith/go-bip32"
	"github.com/tyler-smith/go-bip39"
)

func main() {
	// Generate a mnemonic for memorization or user-friendly seeds
	entropy, _ := bip39.NewEntropy(256)
	mnemonic, _ := bip39.NewMnemonic(entropy)

	// Generate a Bip32 HD wallet for the mnemonic and a user supplied password
	seed := bip39.NewSeed(mnemonic, "Secret Passphrase")

	masterKey, _ := bip32.NewMasterKey(seed)
	publicKey := masterKey.PublicKey()

	// Display mnemonic and keys
	fmt.Println("Mnemonic: ", mnemonic)
	fmt.Println("Master private key: ", masterKey)
	fmt.Println("Master public key: ", publicKey)

	// Validate test
	testMnemonic := "drip hover remember layer torch father remind voyage fatigue skull mango business local lawsuit balance omit surprise silent peasant pull shrug promote holiday armor"
	testMasterKey := "xprv9s21ZrQH143K4ZyKb8QHCDbbWZ2qUkBRtQZ3y9bnGnmYzxumiJCJYvsLXfiBtkTdwJ9G6g213z8dxCFotenv4TyUNy3rr5nqvABZkzyBxm7"
	testPublicKey := "xpub661MyMwAqRbcH43nh9wHZMYL4asKtCuHFdUemY1Pq8JXsmEvFqWZ6jBpNxxZr2d2qErR5vKzU2vVecv7fLkNfma9yVmLe9JZrtCCz72MtWg"

	revSeed := bip39.NewSeed(testMnemonic, "Secret Passphrase")

	revMasterKey, _ := bip32.NewMasterKey(revSeed)
	revPublicKey := revMasterKey.PublicKey()

	validMnemonic := bip39.IsMnemonicValid(testMnemonic)

	validMaster := false
	if revMasterKey.String() == testMasterKey {
		validMaster = true
	}

	validPublic := false
	if revPublicKey.String() == testPublicKey {
		validPublic = true
	}

	fmt.Println("Valid mnemonic: ", validMnemonic)
	fmt.Println("Valid master private key: ", validMaster)
	fmt.Println("Valid master public key: ", validPublic)
}

French accents are not matching accents from common keyboard layout

For example the word "siècle" 0x736965cc80636c65 will not match the word "siècle" 0x7369c3a8636c65.

One is coming from the word list and the other from utf8 text from any website.

I do not know which is the "correct" solution:

  • Either both are good and should match the typed word.
  • Only one is good and must respect the specification.
  • Maybe it's more subtle and shall match depending on the dialect, for example maybe those might be accents from canadian keyboard layout, etc...

I'll probably find a workaround of my own to input this correctly. This might be worth looking into.

IsMnemonicValid does not validate checksum

IsMnemonicValid returns true for invalid mnemonic sequences that do not pass the checsum test. Take any valid mnemonic and change one of the words. IsMnemonicValid must return false for that input.

go-bip39 $ go test .
--- FAIL: TestIsMnemonicValid (0.00s)
    bip39_test.go:450: Expected false, got true
FAIL
FAIL    _/home/piotr/go-bip39   0.232s

go-bip39 $ git diff
diff --git a/bip39_test.go b/bip39_test.go
index 230c831..63d19e0 100644
--- a/bip39_test.go
+++ b/bip39_test.go
@@ -408,6 +408,7 @@ func testVectors() []vector {
 
 func badMnemonicSentences() []vector {
        return []vector{
+               {mnemonic: "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon letter"},
                {mnemonic: "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon"},
                {mnemonic: "legal winner thank year wave sausage worth useful legal winner thank yellow yellow"},
                {mnemonic: "letter advice cage absurd amount doctor acoustic avoid letter advice caged above"},

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.