<commit_before><commit_msg>first version of mnumonic wallet import<commit_after>// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package wallet

import (
	"fmt"
	"os"
	"strings"

	"github.com/FactomProject/factomd/common/factoid"
)

// ImportWalletFromMnemonic creates a new wallet with a provided Mnemonic seed
// defined in bip-0039.
func ImportWalletFromMnemonic(mnemonic, path string) (*Wallet, error) {
	// check the mnemonic length
	if l := len(strings.Fields(mnemonic)); l != 12 {
		return nil, fmt.Errorf("Incorrect mnemonic length. Expecitng 12 words, found %d", l)
	}

	// check if the file exists
	_, err := os.Stat(path)
	if err == nil {
		return nil, fmt.Errorf("%s: file already exists", path)
	}

	db, err := NewBoltDB(path)
	if err != nil {
		return nil, err
	}

	seed := new(DBSeed)
	seed.MnemonicSeed = mnemonic
	if err := db.InsertDBSeed(seed); err != nil {
		return nil, err
	}

	w := new(Wallet)
	w.transactions = make(map[string]*factoid.Transaction)
	w.WalletDatabaseOverlay = db

	return w, nil
}
{'fraction_non_alphanumeric': 0.11732851985559567, 'fraction_numerical': 0.010830324909747292, 'mean_word_length': 3.928888888888889, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7282013', 'n_tokens_mistral': 406, 'n_tokens_neox': 381, 'n_words': 122}