Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: static files loading #12

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const (
KDFScrypt KDFFunction = "scrypt"
KDFPBKDF2 KDFFunction = "pbkdf2"

DefaultWordListPath = "../word_lists"

DerivationPathBN254 = "m/254/60/0/0"
)

Expand Down Expand Up @@ -495,7 +493,7 @@ func NewKeyPair(
language mnemonic.Language,
) (*KeyPair, error) {
// Get the mnemonic
pkMnemonic, err := mnemonic.GetMnemonic(language, DefaultWordListPath, nil)
pkMnemonic, err := mnemonic.GetMnemonic(language, nil)
if err != nil {
return nil, err
}
Expand Down
51 changes: 10 additions & 41 deletions mnemonic/mnemonic.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package mnemonic

import (
"bufio"
"crypto/rand"
"crypto/sha256"
"errors"
"fmt"
"math/big"
"os"
"path/filepath"
"slices"
"strings"

Expand Down Expand Up @@ -39,37 +36,12 @@ const (
//
// Parameters:
// - language (string): The language of the wordlist.
// - path (string): The path to the directory containing the wordlist file.
//
// Returns:
// - []string: A slice of words from the wordlist.
// - error: An error object if the wordlist file cannot be read.
func getWordList(language Language, path string) ([]string, error) {
cleanLanguageFileName := filepath.Clean(fmt.Sprintf("%s.txt", language))

// Get the wordlist path
path = wordlists.GetWordListFolderPath()

// Append the path to the wordlist folder and the language file
filePath := filepath.Join(path, cleanLanguageFileName)

cleanFilePath := filepath.Clean(filePath)
file, err := os.Open(cleanFilePath)
if err != nil {
return nil, fmt.Errorf("failed to open wordlist file: %v", err)
}
defer file.Close()

var wordList []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
word := strings.TrimSpace(scanner.Text())
wordList = append(wordList, word)
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading wordlist: %v", err)
}
return wordList, nil
func getWordList(language Language) ([]string, error) {
return wordlists.GetWordList(string(language))
}

// indexToWord returns the corresponding word for the given index in the word list.
Expand Down Expand Up @@ -143,12 +115,11 @@ func GetSeed(mnemonic, password string) ([]byte, error) {
//
// Parameters:
// - mnemonic (string): The mnemonic phrase.
// - wordsPath (string): The path to the directory containing the wordlist files.
//
// Returns:
// - []Language: A slice of detected languages.
// - error: An error object if the language determination fails.
func determineMnemonicLanguage(mnemonic, wordsPath string) ([]Language, error) {
func determineMnemonicLanguage(mnemonic string) ([]Language, error) {
languages := []Language{
English,
Italian,
Expand All @@ -162,7 +133,7 @@ func determineMnemonicLanguage(mnemonic, wordsPath string) ([]Language, error) {
wordLanguageMap := make(map[string]Language)

for _, lang := range languages {
wordList, err := getWordList(lang, wordsPath)
wordList, err := getWordList(lang)
if err != nil {
return nil, fmt.Errorf("failed to get wordlist for language %s: %v", lang, err)
}
Expand Down Expand Up @@ -268,22 +239,21 @@ func reconstructFromWordIndices(wordList []string, wordIndices []int) string {
//
// Parameters:
// - mnemonic (string): The abbreviated mnemonic phrase.
// - wordsPath (string): The path to the directory containing the wordlist files.
//
// Returns:
// - string: The reconstructed mnemonic.
// - error: An error object if the reconstruction fails.
func ReconstructMnemonic(mnemonic, wordsPath string) (string, error) {
func ReconstructMnemonic(mnemonic string) (string, error) {
// Determine the language of the mnemonic
languages, err := determineMnemonicLanguage(mnemonic, wordsPath)
languages, err := determineMnemonicLanguage(mnemonic)
if err != nil {
return "", err
}

var reconstructedMnemonic string
for _, language := range languages {
// Get the abbreviated word list and the full word list for the language
wordList, err := getWordList(language, wordsPath)
wordList, err := getWordList(language)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -326,7 +296,7 @@ func ReconstructMnemonic(mnemonic, wordsPath string) (string, error) {
entropyBytes := entropy.FillBytes(make([]byte, checksumLength*4))

// Get the full word list for the language
fullWordList, err := getWordList(language, wordsPath)
fullWordList, err := getWordList(language)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -354,13 +324,12 @@ func ReconstructMnemonic(mnemonic, wordsPath string) (string, error) {
//
// Parameters:
// - language (string): The language of the wordlist.
// - wordsPath (string): The path to the directory containing the wordlist files.
// - entropy ([]byte): The entropy bytes. If nil, random entropy will be generated.
//
// Returns:
// - string: The generated mnemonic phrase.
// - error: An error object if the mnemonic generation fails.
func GetMnemonic(language Language, wordsPath string, entropy []byte) (string, error) {
func GetMnemonic(language Language, entropy []byte) (string, error) {
// Generate random entropy if not provided
if entropy == nil {
entropy = make([]byte, 32) // 256 bits
Expand All @@ -382,7 +351,7 @@ func GetMnemonic(language Language, wordsPath string, entropy []byte) (string, e
entropyBits.Or(entropyBits, checksum)

// Load the word list for the specified language
wordList, err := getWordList(language, wordsPath)
wordList, err := getWordList(language)
if err != nil {
return "", fmt.Errorf("failed to load word list: %v", err)
}
Expand Down
9 changes: 4 additions & 5 deletions mnemonic/mnemonic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
)

var (
wordListsPath = filepath.Join(os.Getenv("PWD"), "..", "word_lists")
testVectorMnemonicFilePath = filepath.Join(os.Getenv("PWD"), "..", "tests_vectors", "mnemonic.json")
testVectorsMnemonic map[Language][][4]string
)
Expand Down Expand Up @@ -46,7 +45,7 @@ func TestBip39(t *testing.T) {
}

// Test the mnemonic generation from entropy
mnemonic, err := GetMnemonic(language, wordListsPath, testEntropy)
mnemonic, err := GetMnemonic(language, testEntropy)
if err != nil {
t.Fatalf("Failed to generate mnemonic: %v", err)
}
Expand All @@ -73,7 +72,7 @@ func TestReconstructMnemonic(t *testing.T) {
mnemonic := testMnemonic[1] // Extract the mnemonic from the test vector

t.Run(fmt.Sprintf("Testing mnemonic: %s", mnemonic), func(t *testing.T) {
reconstructedMnemonic, err := ReconstructMnemonic(mnemonic, wordListsPath)
reconstructedMnemonic, err := ReconstructMnemonic(mnemonic)
if err != nil {
t.Fatalf("Failed to reconstruct mnemonic: %v", err)
}
Expand Down Expand Up @@ -102,7 +101,7 @@ func TestReconstructAbbreviatedMnemonic(t *testing.T) {
for _, languageTestVectors := range testVectorsMnemonic {
for _, testMnemonic := range languageTestVectors {
abbreviatedMnemonic := abbreviateMnemonic(testMnemonic[1])
result, err := ReconstructMnemonic(abbreviatedMnemonic, wordListsPath)
result, err := ReconstructMnemonic(abbreviatedMnemonic)
assert.NoError(t, err)
if result == "" {
t.Errorf("Failed to reconstruct mnemonic: %s", abbreviatedMnemonic)
Expand All @@ -116,7 +115,7 @@ func TestGetWord(t *testing.T) {
language := English

// Get the word list for the specified language
wordList, err := getWordList(language, wordListsPath)
wordList, err := getWordList(language)
if err != nil {
t.Fatalf("Failed to get word list for language %s: %v", language, err)
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 12 additions & 9 deletions wordlists/wordlists.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package wordlists

import (
"embed"
"fmt"
"path/filepath"
"runtime"
"strings"
)

func GetWordListFolderPath() string {
_, filename, _, ok := runtime.Caller(0)
if !ok {
fmt.Println("Unable to get current file path")
return ""
}
//go:embed all:files/*
var staticFiles embed.FS

return filepath.Dir(filename)
func GetWordList(language string) ([]string, error) {
data, err := staticFiles.ReadFile("files/" + fmt.Sprintf("%s.txt", language))
if err != nil {
return nil, err
}
content := string(data)
wordList := strings.Split(content, "\n")
return wordList, nil
}
Loading