gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/cmd/attacher/walletcmd.go (about)

     1  // Copyright 2019 The aquachain Authors
     2  // This file is part of aquachain.
     3  //
     4  // aquachain is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // aquachain is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with aquachain. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/hex"
    21  	"encoding/json"
    22  	"fmt"
    23  	"strconv"
    24  	"strings"
    25  
    26  	cli "github.com/urfave/cli"
    27  	"gitlab.com/aquachain/aquachain/cmd/utils"
    28  	"gitlab.com/aquachain/aquachain/crypto"
    29  )
    30  
    31  var (
    32  	paperCommand = cli.Command{
    33  		Name:      "paper",
    34  		Usage:     `Generate paper wallet keypair`,
    35  		Flags:     []cli.Flag{utils.JsonFlag, utils.VanityFlag},
    36  		ArgsUsage: "[number of wallets]",
    37  		Category:  "ACCOUNT COMMANDS",
    38  		Action:    paper,
    39  		Description: `
    40  Generate a number of wallets.`,
    41  	}
    42  )
    43  
    44  type paperWallet struct{ Private, Public string }
    45  
    46  func paper(c *cli.Context) error {
    47  
    48  	if c.NArg() > 1 {
    49  		return fmt.Errorf("too many arguments")
    50  	}
    51  	var (
    52  		count = 1
    53  		err   error
    54  	)
    55  	if c.NArg() == 1 {
    56  		count, err = strconv.Atoi(c.Args().First())
    57  		if err != nil {
    58  			return err
    59  		}
    60  	}
    61  	wallets := []paperWallet{}
    62  	vanity := c.String("vanity")
    63  	for i := 0; i < count; i++ {
    64  		var wallet paperWallet
    65  		for {
    66  			key, err := crypto.GenerateKey()
    67  			if err != nil {
    68  				return err
    69  			}
    70  
    71  			addr := crypto.PubkeyToAddress(key.PublicKey)
    72  			wallet = paperWallet{
    73  				Private: hex.EncodeToString(crypto.FromECDSA(key)),
    74  				Public:  "0x" + hex.EncodeToString(addr[:]),
    75  			}
    76  			if vanity == "" {
    77  				break
    78  			}
    79  			pubkey := hex.EncodeToString(addr[:])
    80  			if strings.HasPrefix(pubkey, vanity) {
    81  				break
    82  			}
    83  		}
    84  		if c.Bool("json") {
    85  			wallets = append(wallets, wallet)
    86  		} else {
    87  			fmt.Println(wallet.Private, wallet.Public)
    88  		}
    89  	}
    90  	if c.Bool("json") {
    91  		b, _ := json.Marshal(wallets)
    92  		fmt.Println(string(b))
    93  	}
    94  	return nil
    95  }