github.com/aquanetwork/aquachain@v1.7.8/cmd/aquachain/walletcmd_lite.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  Disabled in this version of Aquachain`,
    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  	return fmt.Errorf("MyAquaWallet is not included in this version of Aquachain")
    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  }