github.com/igggame/nebulas-go@v2.1.0+incompatible/cmd/neb/accountcmd.go (about) 1 // Copyright (C) 2017 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // the go-nebulas library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 19 package main 20 21 import ( 22 "fmt" 23 "io/ioutil" 24 25 "github.com/nebulasio/go-nebulas/cmd/console" 26 "github.com/nebulasio/go-nebulas/core" 27 "github.com/urfave/cli" 28 ) 29 30 var ( 31 accountCommand = cli.Command{ 32 Name: "account", 33 Usage: "Manage accounts", 34 Category: "ACCOUNT COMMANDS", 35 Description: ` 36 Manage accounts, list all existing accounts, import a private key into a new 37 account, create a new account or update an existing account.`, 38 39 Subcommands: []cli.Command{ 40 { 41 Name: "new", 42 Usage: "Create a new account", 43 Action: MergeFlags(accountCreate), 44 ArgsUsage: "[passphrase]", 45 Description: ` 46 neb account new 47 48 Creates a new account and prints the address. If passphrase not input, prompt input and confirm.`, 49 }, 50 { 51 Name: "list", 52 Usage: "Print summary of existing addresses", 53 Action: MergeFlags(accountList), 54 Description: ` 55 Print a short summary of all accounts`, 56 }, 57 { 58 Name: "update", 59 Usage: "Update an existing account", 60 Action: MergeFlags(accountUpdate), 61 ArgsUsage: "<address>", 62 Description: ` 63 neb account update <address> 64 65 Update an existing account.`, 66 }, 67 { 68 Name: "import", 69 Usage: "Import a private key into a new account", 70 Action: MergeFlags(accountImport), 71 ArgsUsage: "<keyFile>", 72 Description: ` 73 neb account import <keyfile> 74 75 Imports an encrypted private key from <keyfile> and creates a new account.`, 76 }, 77 }, 78 } 79 ) 80 81 // accountList list account 82 func accountList(ctx *cli.Context) error { 83 neb, err := makeNeb(ctx) 84 if err != nil { 85 return err 86 } 87 88 for index, addr := range neb.AccountManager().Accounts() { 89 fmt.Printf("Account #%d: %s\n", index, addr.String()) 90 index++ 91 } 92 return nil 93 } 94 95 // accountCreate creates a new account into the keystore 96 func accountCreate(ctx *cli.Context) error { 97 neb, err := makeNeb(ctx) 98 if err != nil { 99 return err 100 } 101 102 passphrase := ctx.Args().First() 103 104 if len(passphrase) == 0 { 105 passphrase = getPassPhrase("Your new account is locked with a passphrase. Please give a passphrase. Do not forget this passphrase.", true) 106 } 107 108 addr, err := neb.AccountManager().NewAccount([]byte(passphrase)) 109 fmt.Printf("Address: %s\n", addr.String()) 110 return err 111 } 112 113 // accountUpdate update 114 func accountUpdate(ctx *cli.Context) error { 115 if len(ctx.Args()) == 0 { 116 FatalF("No accounts specified to update") 117 } 118 119 neb, err := makeNeb(ctx) 120 if err != nil { 121 return err 122 } 123 124 for _, address := range ctx.Args() { 125 addr, err := core.AddressParse(address) 126 if err != nil { 127 FatalF("address parse failed:%s,%s", address, err) 128 } 129 oldPassphrase := getPassPhrase("Please input current passhprase", false) 130 newPassword := getPassPhrase("Please give a new password. Do not forget this password.", true) 131 132 err = neb.AccountManager().Update(addr, []byte(oldPassphrase), []byte(newPassword)) 133 if err != nil { 134 FatalF("account update failed:%s,%s", address, err) 135 } 136 fmt.Printf("Updated address: %s\n", addr.String()) 137 } 138 return nil 139 } 140 141 // accountImport import keyfile 142 func accountImport(ctx *cli.Context) error { 143 keyfile := ctx.Args().First() 144 if len(keyfile) == 0 { 145 FatalF("keyfile must be given as argument") 146 } 147 keyJSON, err := ioutil.ReadFile(keyfile) 148 if err != nil { 149 FatalF("file read failed:%s", err) 150 } 151 152 neb, err := makeNeb(ctx) 153 if err != nil { 154 return err 155 } 156 157 passphrase := getPassPhrase("", false) 158 addr, err := neb.AccountManager().Import([]byte(keyJSON), []byte(passphrase)) 159 if err != nil { 160 FatalF("key import failed:%s", err) 161 } 162 fmt.Printf("Import address: %s\n", addr.String()) 163 return nil 164 } 165 166 // getPassPhrase get passphrase from consle 167 func getPassPhrase(prompt string, confirmation bool) string { 168 if prompt != "" { 169 fmt.Println(prompt) 170 } 171 passphrase, err := console.Stdin.PromptPassphrase("Passphrase: ") 172 if err != nil { 173 FatalF("Failed to read passphrase: %v", err) 174 } 175 if confirmation { 176 confirm, err := console.Stdin.PromptPassphrase("Repeat passphrase: ") 177 if err != nil { 178 FatalF("Failed to read passphrase confirmation: %v", err) 179 } 180 if passphrase != confirm { 181 FatalF("Passphrases do not match") 182 } 183 } 184 return passphrase 185 }