github.com/aquanetwork/aquachain@v1.7.8/cmd/aquachain/walletcmd.go (about)

     1  // +build !nousb
     2  
     3  package main
     4  
     5  import (
     6  	"encoding/hex"
     7  	"encoding/json"
     8  	"fmt"
     9  	"strconv"
    10  	"strings"
    11  
    12  	"gitlab.com/aquachain/aquachain/cmd/utils"
    13  	"gitlab.com/aquachain/aquachain/crypto"
    14  	cli "gopkg.in/urfave/cli.v1"
    15  )
    16  
    17  var (
    18  	walletCommand = cli.Command{
    19  		Name:     "wallet",
    20  		Usage:    `Launch MyAquaWallet, an offline wallet that connects to this aquachain program via JSON-RPC/HTTP`,
    21  		Category: "ACCOUNT COMMANDS",
    22  		Action:   launchmaw,
    23  		Description: `
    24    aquachain wallet
    25  
    26  will launch browser MAW`,
    27  	}
    28  	paperCommand = cli.Command{
    29  		Name:      "paper",
    30  		Usage:     `Generate paper wallet keypair`,
    31  		Flags:     []cli.Flag{utils.JsonFlag, utils.VanityFlag},
    32  		ArgsUsage: "[number of wallets]",
    33  		Category:  "ACCOUNT COMMANDS",
    34  		Action:    paper,
    35  		Description: `
    36  Generate a number of wallets.`,
    37  	}
    38  )
    39  
    40  func launchmaw(c *cli.Context) error {
    41  	fmt.Println("MAW has been removed to save space in favor of other options. Try https://github.com/MyCryptoHQ/MyCrypto/releases")
    42  	return nil
    43  }
    44  
    45  type paperWallet struct{ Private, Public string }
    46  
    47  func paper(c *cli.Context) error {
    48  
    49  	if c.NArg() > 1 {
    50  		return fmt.Errorf("too many arguments")
    51  	}
    52  	var (
    53  		count = 1
    54  		err   error
    55  	)
    56  	if c.NArg() == 1 {
    57  		count, err = strconv.Atoi(c.Args().First())
    58  		if err != nil {
    59  			return err
    60  		}
    61  	}
    62  	wallets := []paperWallet{}
    63  	vanity := c.String("vanity")
    64  	for i := 0; i < count; i++ {
    65  		var wallet paperWallet
    66  		for {
    67  			key, err := crypto.GenerateKey()
    68  			if err != nil {
    69  				return err
    70  			}
    71  
    72  			addr := crypto.PubkeyToAddress(key.PublicKey)
    73  			wallet = paperWallet{
    74  				Private: hex.EncodeToString(crypto.FromECDSA(key)),
    75  				Public:  "0x" + hex.EncodeToString(addr[:]),
    76  			}
    77  			if vanity == "" {
    78  				break
    79  			}
    80  			pubkey := hex.EncodeToString(addr[:])
    81  			if strings.HasPrefix(pubkey, vanity) {
    82  				break
    83  			}
    84  		}
    85  		if c.Bool("json") {
    86  			wallets = append(wallets, wallet)
    87  		} else {
    88  			fmt.Println(wallet.Private, wallet.Public)
    89  		}
    90  	}
    91  	if c.Bool("json") {
    92  		b, _ := json.Marshal(wallets)
    93  		fmt.Println(string(b))
    94  	}
    95  	return nil
    96  }