// Copyright 2016 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package main

import (
	"context"
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"strings"

	"github.com/urfave/cli/v2"

	"github.com/ethereum/go-ethereum/accounts"
	"github.com/ethereum/go-ethereum/accounts/bls"
	"github.com/ethereum/go-ethereum/accounts/keystore"
	"github.com/ethereum/go-ethereum/cmd/utils"
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/ethereum/go-ethereum/crypto/bls/blst"
	blsCommon "github.com/ethereum/go-ethereum/crypto/bls/common"
	"github.com/ethereum/go-ethereum/log"
)

var (
	walletCommand = &cli.Command{
		Name:      "wallet",
		Usage:     "Manage Ethereum presale wallets",
		ArgsUsage: "",
		Category:  "ACCOUNT COMMANDS",
		Description: `
    ronin wallet import /path/to/my/presale.wallet

will prompt for your password and imports your ether presale account.
It can be used non-interactively with the --password option taking a
passwordfile as argument containing the wallet password in plaintext.`,
		Subcommands: []*cli.Command{
			{

				Name:      "import",
				Usage:     "Import Ethereum presale wallet",
				ArgsUsage: "<keyFile>",
				Action:    importWallet,
				Category:  "ACCOUNT COMMANDS",
				Flags: []cli.Flag{
					utils.DataDirFlag,
					utils.KeyStoreDirFlag,
					utils.PasswordFileFlag,
					utils.LightKDFFlag,
				},
				Description: `
	ronin wallet [options] /path/to/my/presale.wallet

will prompt for your password and imports your ether presale account.
It can be used non-interactively with the --password option taking a
passwordfile as argument containing the wallet password in plaintext.`,
			},
		},
	}

	accountCommand = &cli.Command{
		Name:     "account",
		Usage:    "Manage accounts",
		Category: "ACCOUNT COMMANDS",
		Description: `

Manage accounts, list all existing accounts, import a private key into a new
account, create a new account or update an existing account.

It supports interactive mode, when you are prompted for password as well as
non-interactive mode where passwords are supplied via a given password file.
Non-interactive mode is only meant for scripted use on test networks or known
safe environments.

Make sure you remember the password you gave when creating a new account (with
either new or import). Without it you are not able to unlock your account.

Note that exporting your key in unencrypted format is NOT supported.

Keys are stored under <DATADIR>/keystore.
It is safe to transfer the entire directory or the individual keys therein
between ethereum nodes by simply copying.

Make sure you backup your keys regularly.`,
		Subcommands: []*cli.Command{
			{
				Name:   "list",
				Usage:  "Print summary of existing accounts",
				Action: accountList,
				Flags: []cli.Flag{
					utils.DataDirFlag,
					utils.KeyStoreDirFlag,
				},
				Description: `
Print a short summary of all accounts`,
			},
			{
				Name:   "new",
				Usage:  "Create a new account",
				Action: accountCreate,
				Flags: []cli.Flag{
					utils.DataDirFlag,
					utils.KeyStoreDirFlag,
					utils.PasswordFileFlag,
					utils.LightKDFFlag,
				},
				Description: `
    ronin account new

Creates a new account and prints the address.

The account is saved in encrypted format, you are prompted for a password.

You must remember this password to unlock your account in the future.

For non-interactive use the password can be specified with the --password flag:

Note, this is meant to be used for testing only, it is a bad idea to save your
password to file or expose in any other way.
`,
			},
			{
				Name:      "update",
				Usage:     "Update an existing account",
				Action:    accountUpdate,
				ArgsUsage: "<address>",
				Flags: []cli.Flag{
					utils.DataDirFlag,
					utils.KeyStoreDirFlag,
					utils.LightKDFFlag,
				},
				Description: `
    ronin account update <address>

Update an existing account.

The account is saved in the newest version in encrypted format, you are prompted
for a password to unlock the account and another to save the updated file.

This same command can therefore be used to migrate an account of a deprecated
format to the newest format or change the password for an account.

For non-interactive use the password can be specified with the --password flag:

    ronin account update [options] <address>

Since only one password can be given, only format update can be performed,
changing your password is only possible interactively.
`,
			},
			{
				Name:   "import",
				Usage:  "Import a private key into a new account",
				Action: accountImport,
				Flags: []cli.Flag{
					utils.DataDirFlag,
					utils.KeyStoreDirFlag,
					utils.PasswordFileFlag,
					utils.LightKDFFlag,
				},
				ArgsUsage: "<keyFile>",
				Description: `
    ronin account import <keyfile>

Imports an unencrypted private key from <keyfile> and creates a new account.
Prints the address.

The keyfile is assumed to contain an unencrypted private key in hexadecimal format.

The account is saved in encrypted format, you are prompted for a password.

You must remember this password to unlock your account in the future.

For non-interactive use the password can be specified with the -password flag:

    ronin account import [options] <keyfile>

Note:
As you can directly copy your encrypted accounts to another ethereum instance,
this import mechanism is not needed when you transfer an account between
nodes.
`,
			},
			{
				Name:   "check",
				Usage:  "Check if the account corresponding to private key exists",
				Action: accountCheck,
				Flags: []cli.Flag{
					utils.DataDirFlag,
					utils.KeyStoreDirFlag,
				},
				ArgsUsage: "<keyFile>",
				Description: `
    ronin account check <keyfile>

Check if the account corresponding to the private key exists in keystore.

The keyfile is assumed to contain an unencrypted private key in hexadecimal format.
`,
			},
			{
				Name:   "listbls",
				Usage:  "Print information of BLS account",
				Action: blsAccountList,
				Flags: []cli.Flag{
					utils.BlsWalletPath,
					utils.BlsPasswordPath,
					&cli.BoolFlag{
						Name:  "secret",
						Usage: "include the secret key in the output",
					},
				},
				Description: `ronin account listbls [--secret]`,
			},
			{
				Name:   "importbls",
				Usage:  "Import the BLS secret key",
				Action: blsAccountImport,
				Flags: []cli.Flag{
					utils.BlsWalletPath,
					utils.BlsPasswordPath,
				},
				ArgsUsage:   "<keyFile>",
				Description: `ronin account importbls <keyFile>`,
			},
			{
				Name:   "checkbls",
				Usage:  "Check if the BLS account corresponding to secret key exists",
				Action: blsAccountCheck,
				Flags: []cli.Flag{
					utils.BlsWalletPath,
					utils.BlsPasswordPath,
				},
				ArgsUsage:   "<keyFile>",
				Description: `ronin account checkbls <keyFile>`,
			},
			{
				Name:   "generatebls",
				Usage:  "Generate BLS secret key",
				Action: blsAccountGenerate,
				Flags: []cli.Flag{
					utils.BlsWalletPath,
					utils.BlsPasswordPath,
					&cli.BoolFlag{
						Name:  "secret",
						Usage: "include the secret key in the output",
					},
				},
				Description: `ronin account generatebls [--secret]`,
			},
			{
				Name:   "generate-bls-proof",
				Usage:  "Generate BLS proof of possession",
				Action: blsProofGenerate,
				Flags: []cli.Flag{
					utils.BlsWalletPath,
					utils.BlsPasswordPath,
				},
				ArgsUsage: "[keyFile]",
				Description: `
    ronin account generate-bls-proof [keyFile] [--finality.blswalletpath walletpath] [--finality.blspasswordpath passwordpath]

Generate proof from keyfile or stored encrypted wallet

The keyfile is assumed to contain an unencrypted private key in hexadecimal format.
You must input either keyfile or a pair of walletpath and passwordpath.
`,
			},
		},
	}
)

func accountList(ctx *cli.Context) error {
	stack, _ := makeConfigNode(ctx)
	var index int
	for _, wallet := range stack.AccountManager().Wallets() {
		for _, account := range wallet.Accounts() {
			fmt.Printf("Account #%d: {%x} %s\n", index, account.Address, &account.URL)
			index++
		}
	}
	return nil
}

// tries unlocking the specified account a few times.
func unlockAccount(ks *keystore.KeyStore, address string, i int, passwords []string) (accounts.Account, string) {
	account, err := utils.MakeAddress(ks, address)
	if err != nil {
		utils.Fatalf("Could not list accounts: %v", err)
	}
	for trials := 0; trials < 3; trials++ {
		prompt := fmt.Sprintf("Unlocking account %s | Attempt %d/%d", address, trials+1, 3)
		password := utils.GetPassPhraseWithList(prompt, false, i, passwords)
		err = ks.Unlock(account, password)
		if err == nil {
			log.Info("Unlocked account", "address", account.Address.Hex())
			return account, password
		}
		if err, ok := err.(*keystore.AmbiguousAddrError); ok {
			log.Info("Unlocked account", "address", account.Address.Hex())
			return ambiguousAddrRecovery(ks, err, password), password
		}
		if err != keystore.ErrDecrypt {
			// No need to prompt again if the error is not decryption-related.
			break
		}
	}
	// All trials expended to unlock account, bail out
	utils.Fatalf("Failed to unlock account %s (%v)", address, err)

	return accounts.Account{}, ""
}

func ambiguousAddrRecovery(ks *keystore.KeyStore, err *keystore.AmbiguousAddrError, auth string) accounts.Account {
	fmt.Printf("Multiple key files exist for address %x:\n", err.Addr)
	for _, a := range err.Matches {
		fmt.Println("  ", a.URL)
	}
	fmt.Println("Testing your password against all of them...")
	var match *accounts.Account
	for _, a := range err.Matches {
		if err := ks.Unlock(a, auth); err == nil {
			match = &a
			break
		}
	}
	if match == nil {
		utils.Fatalf("None of the listed files could be unlocked.")
	}
	fmt.Printf("Your password unlocked %s\n", match.URL)
	fmt.Println("In order to avoid this warning, you need to remove the following duplicate key files:")
	for _, a := range err.Matches {
		if a != *match {
			fmt.Println("  ", a.URL)
		}
	}
	return *match
}

// accountCreate creates a new account into the keystore defined by the CLI flags.
func accountCreate(ctx *cli.Context) error {
	cfg := gethConfig{Node: defaultNodeConfig()}
	// Load config file.
	if file := ctx.String(configFileFlag.Name); file != "" {
		if err := loadConfig(file, &cfg); err != nil {
			utils.Fatalf("%v", err)
		}
	}
	utils.SetNodeConfig(ctx, &cfg.Node)
	keydir, err := cfg.Node.KeyDirConfig()
	if err != nil {
		utils.Fatalf("Failed to read configuration: %v", err)
	}
	scryptN := keystore.StandardScryptN
	scryptP := keystore.StandardScryptP
	if cfg.Node.UseLightweightKDF {
		scryptN = keystore.LightScryptN
		scryptP = keystore.LightScryptP
	}

	password := utils.GetPassPhraseWithList("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))

	account, err := keystore.StoreKey(keydir, password, scryptN, scryptP)

	if err != nil {
		utils.Fatalf("Failed to create account: %v", err)
	}
	fmt.Printf("\nYour new key was generated\n\n")
	fmt.Printf("Public address of the key:   %s\n", account.Address.Hex())
	fmt.Printf("Path of the secret key file: %s\n\n", account.URL.Path)
	fmt.Printf("- You can share your public address with anyone. Others need it to interact with you.\n")
	fmt.Printf("- You must NEVER share the secret key with anyone! The key controls access to your funds!\n")
	fmt.Printf("- You must BACKUP your key file! Without the key, it's impossible to access account funds!\n")
	fmt.Printf("- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!\n\n")
	return nil
}

// accountUpdate transitions an account from a previous format to the current
// one, also providing the possibility to change the pass-phrase.
func accountUpdate(ctx *cli.Context) error {
	if ctx.Args().Len() == 0 {
		utils.Fatalf("No accounts specified to update")
	}
	stack, _ := makeConfigNode(ctx)
	ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)

	for _, addr := range ctx.Args().Slice() {
		account, oldPassword := unlockAccount(ks, addr, 0, nil)
		newPassword := utils.GetPassPhraseWithList("Please give a new password. Do not forget this password.", true, 0, nil)
		if err := ks.Update(account, oldPassword, newPassword); err != nil {
			utils.Fatalf("Could not update the account: %v", err)
		}
	}
	return nil
}

func importWallet(ctx *cli.Context) error {
	if ctx.Args().Len() != 1 {
		utils.Fatalf("keyfile must be given as the only argument")
	}
	keyfile := ctx.Args().First()
	if len(keyfile) == 0 {
		utils.Fatalf("keyfile must be given as argument")
	}
	keyJSON, err := ioutil.ReadFile(keyfile)
	if err != nil {
		utils.Fatalf("Could not read wallet file: %v", err)
	}

	stack, _ := makeConfigNode(ctx)
	passphrase := utils.GetPassPhraseWithList("", false, 0, utils.MakePasswordList(ctx))

	ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
	acct, err := ks.ImportPreSaleKey(keyJSON, passphrase)
	if err != nil {
		utils.Fatalf("%v", err)
	}
	fmt.Printf("Address: {%x}\n", acct.Address)
	return nil
}

func accountImport(ctx *cli.Context) error {
	if ctx.Args().Len() != 1 {
		utils.Fatalf("keyfile must be given as the only argument")
	}
	keyfile := ctx.Args().First()
	if len(keyfile) == 0 {
		utils.Fatalf("keyfile must be given as argument")
	}
	key, err := crypto.LoadECDSA(keyfile)
	if err != nil {
		utils.Fatalf("Failed to load the private key: %v", err)
	}
	stack, _ := makeConfigNode(ctx)
	passphrase := utils.GetPassPhraseWithList("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))

	ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)
	acct, err := ks.ImportECDSA(key, passphrase)
	if err != nil {
		utils.Fatalf("Could not create the account: %v", err)
	}
	fmt.Printf("Address: {%x}\n", acct.Address)
	return nil
}

func accountCheck(ctx *cli.Context) error {
	if ctx.Args().Len() != 1 {
		utils.Fatalf("keyfile must be given as the only argument")
	}
	keyfile := ctx.Args().First()
	if len(keyfile) == 0 {
		utils.Fatalf("keyfile must be given as argument")
	}
	key, err := crypto.LoadECDSA(keyfile)
	if err != nil {
		utils.Fatalf("Failed to load the private key: %v", err)
	}
	address := crypto.PubkeyToAddress(key.PublicKey)

	stack, _ := makeConfigNode(ctx)
	for _, wallet := range stack.AccountManager().Wallets() {
		for _, account := range wallet.Accounts() {
			if account.Address == address {
				fmt.Printf("Found account %x\n", address)
				return nil
			}
		}
	}
	utils.Fatalf("Account %x not found", address)
	return nil
}

func loadKeyManager(ctx *cli.Context) (*bls.KeyManager, []blsCommon.PublicKey, error) {
	blsPasswordPath := ctx.String(utils.BlsPasswordPath.Name)
	blsWalletPath := ctx.String(utils.BlsWalletPath.Name)

	wallet, err := bls.New(blsWalletPath, blsPasswordPath)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to create wallet, err %s", err)
	}

	km, err := bls.NewKeyManager(context.Background(), wallet)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to initialized key manager, err %s", err)
	}

	rawPublicKeys, err := km.FetchValidatingPublicKeys(context.Background())
	if err != nil {
		return nil, nil, fmt.Errorf("failed to fetch BLS public key, err %s", err)
	}

	var publicKeys []blsCommon.PublicKey
	for _, rawPublicKey := range rawPublicKeys {
		publicKey, err := blst.PublicKeyFromBytes(rawPublicKey[:])
		if err != nil {
			return nil, nil, fmt.Errorf("failed to decode BLS public key, err %s", err)
		}

		publicKeys = append(publicKeys, publicKey)
	}

	return km, publicKeys, nil
}

func loadBlsSecretKey(ctx *cli.Context) (blsCommon.SecretKey, error) {
	if ctx.Args().Len() != 1 {
		return nil, fmt.Errorf("keyfile must be given as the only argument")
	}
	keyfile := ctx.Args().First()
	if len(keyfile) == 0 {
		return nil, fmt.Errorf("keyfile must be given as argument")
	}

	secretKeyHex, err := ioutil.ReadFile(keyfile)
	if err != nil {
		return nil, fmt.Errorf("failed to read secret key, err %s", err)
	}
	secretKeyHexString := strings.TrimSuffix(string(secretKeyHex), "\n")

	key, err := hex.DecodeString(secretKeyHexString)
	if err != nil {
		return nil, fmt.Errorf("failed to decode secret key, err %s", err)
	}

	secretKey, err := blst.SecretKeyFromBytes(key)
	if err != nil {
		return nil, fmt.Errorf("failed to decode secret key, err %s", err)
	}

	return secretKey, nil

}

func blsAccountList(ctx *cli.Context) error {
	km, publicKeys, err := loadKeyManager(ctx)
	if err != nil {
		utils.Fatalf("Failed to fetch BLS public key, err %s", err)
	}

	if ctx.Bool("secret") {
		rawSecretKeys, err := km.FetchValidatingSecretKeys(context.Background())
		if err != nil {
			utils.Fatalf("Failed to fetch BLS secret key, err %s", err)
		}

		for i, rawsecretKey := range rawSecretKeys {
			secretKey, err := blst.SecretKeyFromBytes(rawsecretKey[:])
			if err != nil {
				utils.Fatalf("Failed to decode BLS secret key, err %s", err)
			}

			fmt.Printf("BLS public key #%d: {%x}\n", i, secretKey.PublicKey().Marshal())
			fmt.Printf("BLS secret key #%d: {%x}\n", i, secretKey.Marshal())
			fmt.Println()
		}

	} else {
		for i, publicKey := range publicKeys {
			fmt.Printf("BLS public key #%d: {%x}\n", i, publicKey.Marshal())
		}
	}

	return nil
}

func blsAccountImport(ctx *cli.Context) error {
	secretKey, err := loadBlsSecretKey(ctx)
	if err != nil {
		utils.Fatalf("Failed to load BLS secret key, err %s", err)
	}

	publicKey := secretKey.PublicKey()

	km, publicKeys, err := loadKeyManager(ctx)
	if err != nil {
		utils.Fatalf("Failed to load BLS public key, err %s", err)
	}

	for _, pkey := range publicKeys {
		if pkey.Equals(publicKey) {
			utils.Fatalf("Account already existed, public key: {%x}", publicKey.Marshal())
		}
	}

	err = km.ImportKeypairs(
		context.Background(),
		[][]byte{secretKey.Marshal()},
		[][]byte{publicKey.Marshal()},
	)
	if err != nil {
		utils.Fatalf("Failed to import secret key, err %s", err)
	}

	return nil
}

func blsAccountCheck(ctx *cli.Context) error {
	secretKey, err := loadBlsSecretKey(ctx)
	if err != nil {
		utils.Fatalf("Failed to load BLS secret key, err %s", err)
	}
	publicKey := secretKey.PublicKey()

	_, publicKeys, err := loadKeyManager(ctx)
	if err != nil {
		utils.Fatalf("Failed to load BLS public key, err %s", err)
	}

	for _, pkey := range publicKeys {
		if pkey.Equals(publicKey) {
			fmt.Printf("Found BLS account %x\n", publicKey.Marshal())
			return nil
		}
	}

	utils.Fatalf("BLS account %x not found", publicKey.Marshal())
	return nil
}

func blsAccountGenerate(ctx *cli.Context) error {
	secretKey, err := blst.RandKey()
	if err != nil {
		utils.Fatalf("Failed to generate secret key, err %s", err)
	}

	km, _, err := loadKeyManager(ctx)
	if err != nil {
		utils.Fatalf("Failed to load BLS public key, err %s", err)
	}

	err = km.ImportKeypairs(
		context.Background(),
		[][]byte{secretKey.Marshal()},
		[][]byte{secretKey.PublicKey().Marshal()},
	)
	if err != nil {
		utils.Fatalf("Failed to import generated BLS key, err %s", err)
	}

	fmt.Println("Successfully generated BLS key")
	fmt.Printf("Public key: {%x}\n", secretKey.PublicKey().Marshal())

	if ctx.Bool("secret") {
		fmt.Printf("Secret key: {%x}\n", secretKey.Marshal())
	}

	return nil
}

func blsProofGenerate(ctx *cli.Context) error {
	var (
		keyManager *bls.KeyManager
		secretKeys []blsCommon.SecretKey
	)

	secretKey, err := loadBlsSecretKey(ctx)
	if err != nil {
		keyManager, _, err = loadKeyManager(ctx)
		if err != nil {
			utils.Fatalf("Either keyfile or path to wallet must be provided, err: %s", err)
		}
		rawSecretKeys, err := keyManager.FetchValidatingSecretKeys(context.Background())
		if err != nil {
			utils.Fatalf("Failed to fetch BLS secret key, err %s", err)
		}
		for _, rawSecretKey := range rawSecretKeys {
			secretKey, err := blst.SecretKeyFromBytes(rawSecretKey[:])
			if err != nil {
				utils.Fatalf("Failed to decode BLS secret key, err %s", err)
			}
			secretKeys = append(secretKeys, secretKey)
		}
	} else {
		secretKeys = append(secretKeys, secretKey)
	}

	for i, secretKey := range secretKeys {
		rawPublicKey := secretKey.PublicKey().Marshal()
		proof := secretKey.SignProof(rawPublicKey)
		fmt.Printf("BLS public key #%d: {%x}\n", i, rawPublicKey)
		fmt.Printf("BLS proof #%d: {%x}\n", i, proof.Marshal())
	}

	return nil
}
