<commit_before>// Copyright (c) 2014-2015 The btcsuite developers
// Copyright (c) 2015-2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package main

import (
	"bufio"
	"context"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"decred.org/dcrwallet/errors"
	"decred.org/dcrwallet/internal/loader"
	"decred.org/dcrwallet/internal/prompt"
	"decred.org/dcrwallet/wallet"
	_ "decred.org/dcrwallet/wallet/drivers/bdb"
	"decred.org/dcrwallet/walletseed"
	"github.com/decred/dcrd/chaincfg/v3"
	"github.com/decred/dcrd/dcrec"
	"github.com/decred/dcrd/dcrutil/v3"
	"github.com/decred/dcrd/hdkeychain/v3"
	"github.com/decred/dcrd/wire"
)

// networkDir returns the directory name of a network directory to hold wallet
// files.
func networkDir(dataDir string, chainParams *chaincfg.Params) string {
	netname := chainParams.Name
	// Be cautious of v2+ testnets being named only "testnet".
	switch chainParams.Net {
	case 0x48e7a065: // testnet2
		netname = "testnet2"
	case wire.TestNet3:
		netname = "testnet3"
	}
	return filepath.Join(dataDir, netname)
}

// createWallet prompts the user for information needed to generate a new wallet
// and generates the wallet accordingly.  The new wallet will reside at the
// provided path. The bool passed back gives whether or not the wallet was
// restored from seed, while the []byte passed is the private password required
// to do the initial sync.
func createWallet(ctx context.Context, cfg *config) error {
	dbDir := networkDir(cfg.AppDataDir.Value, activeNet.Params)
	stakeOptions := &loader.StakeOptions{
		VotingEnabled: cfg.EnableVoting,
		VotingAddress: cfg.TBOpts.votingAddress,
	}
	loader := loader.NewLoader(activeNet.Params, dbDir, stakeOptions,
		cfg.GapLimit, cfg.AllowHighFees, cfg.RelayFee.Amount,
		cfg.AccountGapLimit, cfg.DisableCoinTypeUpgrades)

	var privPass, pubPass, seed []byte
	var imported bool
	var err error
	c := make(chan struct{}, 1)
	go func() {
		defer func() { c <- struct{}{} }()
		r := bufio.NewReader(os.Stdin)

		// Start by prompting for the private passphrase.  This function
		// prompts whether any configured private passphrase should be
		// used.
		privPass, err = prompt.PrivatePass(r, []byte(cfg.Pass))
		if err != nil {
			return
		}

		// Ascertain the public passphrase.  This will either be a value
		// specified by the user or the default hard-coded public passphrase if
		// the user does not want the additional public data encryption.
		// This function also prompts whether the configured public data
		// passphrase should be used.
		pubPass, err = prompt.PublicPass(r, privPass,
			[]byte(wallet.InsecurePubPassphrase), []byte(cfg.WalletPass))
		if err != nil {
			return
		}

		// Ascertain the wallet generation seed.  This will either be an
		// automatically generated value the user has already confirmed or a
		// value the user has entered which has already been validated.
		// There is no config flag to set the seed.
		seed, imported, err = prompt.Seed(r)
	}()
	select {
	case <-ctx.Done():
		return ctx.Err()
	case <-c:
		if err != nil {
			return err
		}
	}

	fmt.Println("Creating the wallet...")
	w, err := loader.CreateNewWallet(ctx, pubPass, privPass, seed)
	if err != nil {
		return err
	}

	if !imported {
		err := w.UpgradeToSLIP0044CoinType(ctx)
		if err != nil {
			return err
		}
	}

	// Display a mining address when creating a simnet wallet.
	if cfg.SimNet {
		xpub, err := w.AccountXpub(ctx, 0)<commit_msg>Unconditionally use SLIP0044 cointype on simnet<commit_after>// Copyright (c) 2014-2015 The btcsuite developers
// Copyright (c) 2015-2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package main

import (
	"bufio"
	"context"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"decred.org/dcrwallet/errors"
	"decred.org/dcrwallet/internal/loader"
	"decred.org/dcrwallet/internal/prompt"
	"decred.org/dcrwallet/wallet"
	_ "decred.org/dcrwallet/wallet/drivers/bdb"
	"decred.org/dcrwallet/walletseed"
	"github.com/decred/dcrd/chaincfg/v3"
	"github.com/decred/dcrd/dcrec"
	"github.com/decred/dcrd/dcrutil/v3"
	"github.com/decred/dcrd/hdkeychain/v3"
	"github.com/decred/dcrd/wire"
)

// networkDir returns the directory name of a network directory to hold wallet
// files.
func networkDir(dataDir string, chainParams *chaincfg.Params) string {
	netname := chainParams.Name
	// Be cautious of v2+ testnets being named only "testnet".
	switch chainParams.Net {
	case 0x48e7a065: // testnet2
		netname = "testnet2"
	case wire.TestNet3:
		netname = "testnet3"
	}
	return filepath.Join(dataDir, netname)
}

// createWallet prompts the user for information needed to generate a new wallet
// and generates the wallet accordingly.  The new wallet will reside at the
// provided path. The bool passed back gives whether or not the wallet was
// restored from seed, while the []byte passed is the private password required
// to do the initial sync.
func createWallet(ctx context.Context, cfg *config) error {
	dbDir := networkDir(cfg.AppDataDir.Value, activeNet.Params)
	stakeOptions := &loader.StakeOptions{
		VotingEnabled: cfg.EnableVoting,
		VotingAddress: cfg.TBOpts.votingAddress,
	}
	loader := loader.NewLoader(activeNet.Params, dbDir, stakeOptions,
		cfg.GapLimit, cfg.AllowHighFees, cfg.RelayFee.Amount,
		cfg.AccountGapLimit, cfg.DisableCoinTypeUpgrades)

	var privPass, pubPass, seed []byte
	var imported bool
	var err error
	c := make(chan struct{}, 1)
	go func() {
		defer func() { c <- struct{}{} }()
		r := bufio.NewReader(os.Stdin)

		// Start by prompting for the private passphrase.  This function
		// prompts whether any configured private passphrase should be
		// used.
		privPass, err = prompt.PrivatePass(r, []byte(cfg.Pass))
		if err != nil {
			return
		}

		// Ascertain the public passphrase.  This will either be a value
		// specified by the user or the default hard-coded public passphrase if
		// the user does not want the additional public data encryption.
		// This function also prompts whether the configured public data
		// passphrase should be used.
		pubPass, err = prompt.PublicPass(r, privPass,
			[]byte(wallet.InsecurePubPassphrase), []byte(cfg.WalletPass))
		if err != nil {
			return
		}

		// Ascertain the wallet generation seed.  This will either be an
		// automatically generated value the user has already confirmed or a
		// value the user has entered which has already been validated.
		// There is no config flag to set the seed.
		seed, imported, err = prompt.Seed(r)
	}()
	select {
	case <-ctx.Done():
		return ctx.Err()
	case <-c:
		if err != nil {
			return err
		}
	}

	fmt.Println("Creating the wallet...")
	w, err := loader.CreateNewWallet(ctx, pubPass, privPass, seed)
	if err != nil {
		return err
	}

	// Upgrade to the SLIP0044 cointype if this is a new (rather than
	// user-provided) seed, and also unconditionally on simnet (to prevent
	// the mining address printed below from ever becoming invalid if a
	// cointype upgrade occurred later).
	if !imported || cfg.SimNet {
		err := w.UpgradeToSLIP0044CoinType(ctx)
		if err != nil {
			return err
		}
	}

	// Display a mining address when creating a simnet wallet.
	if cfg.SimNet {
		xpub, err := w.AccountXpub(ctx, 0)
{'fraction_non_alphanumeric': 0.10570882272603115, 'fraction_numerical': 0.011199125921879268, 'mean_word_length': 3.685220729366603, 'pattern_counts': {'":': 0, '<': 9, '<?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': '712677', 'n_tokens_mistral': 2579, 'n_tokens_neox': 2406, 'n_words': 783}