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

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