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

     1  // paper command is meant to be easily auditable and can safely generate offline wallets
     2  package main
     3  
     4  import (
     5  	"encoding/hex"
     6  	"encoding/json"
     7  	"flag"
     8  	"fmt"
     9  	"log"
    10  	"os"
    11  	"strconv"
    12  
    13  	"gitlab.com/aquachain/aquachain/crypto"
    14  )
    15  
    16  const usage = `This 'paper' command generates aquachain wallets.
    17  
    18  Here are 3 ways of using:
    19  
    20  Generate 10 in json array:          paper -json 10
    21  Generate 1                          paper
    22  Generate 200                        paper 200
    23  
    24  `
    25  
    26  type paperWallet struct {
    27  	Private string `json:"private"`
    28  	Public  string `json:"public"`
    29  }
    30  
    31  func main() {
    32  	flag.Usage = func() {
    33  		fmt.Println(`                                    _           _
    34          __ _  __ _ _   _  __ _  ___| |__   __ _(_)_ __
    35         / _ '|/ _' | | | |/ _' |/ __| '_ \ / _' | | '_ \
    36        | (_| | (_| | |_| | (_| | (__| | | | (_| | | | | |
    37         \__,_|\__, |\__,_|\__,_|\___|_| |_|\__,_|_|_| |_|
    38                  |_|` + " https://gitlab.com/aquachain/aquachain\n\n")
    39  
    40  		fmt.Println(usage)
    41  	}
    42  	log.SetPrefix("")
    43  	log.SetFlags(0)
    44  	jsonFlag := flag.Bool("json", false, "output json")
    45  	flag.Parse()
    46  	n := flag.Args()
    47  	if len(n) != 1 {
    48  		fmt.Println("expecting zero or one argument\n", usage)
    49  		os.Exit(111)
    50  	}
    51  	count, err := strconv.Atoi(n[0])
    52  	if err != nil {
    53  		fmt.Println("expecting digits", usage)
    54  		os.Exit(111)
    55  	}
    56  	wallets := []paperWallet{}
    57  	for i := 0; i < count; i++ {
    58  		key, err := crypto.GenerateKey()
    59  		if err != nil {
    60  			log.Println("fatal:", err)
    61  			os.Exit(111)
    62  		}
    63  
    64  		addr := crypto.PubkeyToAddress(key.PublicKey)
    65  		wallet := paperWallet{
    66  			Private: hex.EncodeToString(crypto.FromECDSA(key)),
    67  			Public:  "0x" + hex.EncodeToString(addr[:]),
    68  		}
    69  
    70  		if *jsonFlag {
    71  			wallets = append(wallets, wallet)
    72  		} else {
    73  			fmt.Println(wallet.Private, wallet.Public)
    74  		}
    75  	}
    76  	if *jsonFlag {
    77  		b, _ := json.Marshal(wallets)
    78  		fmt.Println(string(b))
    79  	}
    80  
    81  }