Browse Source

Use go-bindata to embed wordlist in a binary

pull/1782/head
Ethan Frey 8 years ago
parent
commit
56200e167a
3 changed files with 334 additions and 22 deletions
  1. +6
    -2
      Makefile
  2. +20
    -20
      keys/wordcodec.go
  3. +308
    -0
      keys/wordlist/wordlist.go

+ 6
- 2
Makefile View File

@ -1,7 +1,8 @@
.PHONEY: all docs test install get_vendor_deps ensure_tools codegen
.PHONEY: all docs test install get_vendor_deps ensure_tools codegen wordlist
GOTOOLS = \
github.com/Masterminds/glide
github.com/Masterminds/glide \
github.com/jteeuwen/go-bindata/go-bindata
REPO:=github.com/tendermint/go-crypto
docs:
@ -35,6 +36,9 @@ get_vendor_deps: ensure_tools
ensure_tools:
go get $(GOTOOLS)
wordlist:
go-bindata -ignore ".*\.go" -o keys/wordlist/wordlist.go -pkg "wordlist" keys/wordlist/...
prepgen: install
go install ./vendor/github.com/btcsuite/btcutil/base58
go install ./vendor/github.com/stretchr/testify/assert


+ 20
- 20
keys/wordcodec.go View File

@ -1,12 +1,12 @@
package keys
import (
"io/ioutil"
"math/big"
"os"
"strings"
"github.com/pkg/errors"
"github.com/tendermint/go-crypto/keys/wordlist"
)
const BankSize = 2048
@ -50,30 +50,30 @@ func LoadCodec(bank string) (codec *WordCodec, err error) {
// loadBank opens a wordlist file and returns all words inside
func loadBank(bank string) ([]string, error) {
filename := "wordlist/" + bank + ".txt"
words, err := getData(filename)
filename := "keys/wordlist/" + bank + ".txt"
words, err := wordlist.Asset(filename)
if err != nil {
return nil, err
}
wordsAll := strings.Split(strings.TrimSpace(words), "\n")
wordsAll := strings.Split(strings.TrimSpace(string(words)), "\n")
return wordsAll, nil
}
// TODO: read from go-bind assets
func getData(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {
return "", errors.WithStack(err)
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return "", errors.WithStack(err)
}
return string(data), nil
}
// // TODO: read from go-bind assets
// func getData(filename string) (string, error) {
// f, err := os.Open(filename)
// if err != nil {
// return "", errors.WithStack(err)
// }
// defer f.Close()
// data, err := ioutil.ReadAll(f)
// if err != nil {
// return "", errors.WithStack(err)
// }
// return string(data), nil
// }
// given this many bytes, we will produce this many words
func wordlenFromBytes(numBytes int) int {


+ 308
- 0
keys/wordlist/wordlist.go
File diff suppressed because it is too large
View File


Loading…
Cancel
Save