github.com/amazechain/amc@v0.1.3/cmd/utils/utils.go (about) 1 // Copyright 2023 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser 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 // The AmazeChain library 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 Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package utils 18 19 import ( 20 "fmt" 21 "github.com/amazechain/amc/common/types" 22 "github.com/amazechain/amc/log" 23 "io" 24 "os" 25 "runtime" 26 "strconv" 27 28 "github.com/amazechain/amc/accounts" 29 "github.com/amazechain/amc/accounts/keystore" 30 "github.com/amazechain/amc/internal/avm/common" 31 ) 32 33 // MakeAddress converts an account specified directly as a hex encoded string or 34 // a key index in the key store to an internal account representation. 35 func MakeAddress(ks *keystore.KeyStore, account string) (accounts.Account, error) { 36 // If the specified account is a valid address, return it 37 if common.IsHexAddress(account) { 38 return accounts.Account{Address: types.Address(common.HexToAddress(account))}, nil 39 } 40 // Otherwise try to interpret the account as a keystore index 41 index, err := strconv.Atoi(account) 42 if err != nil || index < 0 { 43 return accounts.Account{}, fmt.Errorf("invalid account address or index %q", account) 44 } 45 log.Warn("-------------------------------------------------------------------") 46 log.Warn("Referring to accounts by order in the keystore folder is dangerous!") 47 log.Warn("This functionality is deprecated and will be removed in the future!") 48 log.Warn("Please use explicit addresses! (can search via `geth account list`)") 49 log.Warn("-------------------------------------------------------------------") 50 51 accs := ks.Accounts() 52 if len(accs) <= index { 53 return accounts.Account{}, fmt.Errorf("index %d higher than number of accounts %d", index, len(accs)) 54 } 55 return accs[index], nil 56 } 57 58 // Fatalf formats a message to standard error and exits the program. 59 // The message is also printed to standard output if standard error 60 // is redirected to a different file. 61 func Fatalf(format string, args ...interface{}) { 62 w := io.MultiWriter(os.Stdout, os.Stderr) 63 if runtime.GOOS == "windows" { 64 // The SameFile check below doesn't work on Windows. 65 // stdout is unlikely to get redirected though, so just print there. 66 w = os.Stdout 67 } else { 68 outf, _ := os.Stdout.Stat() 69 errf, _ := os.Stderr.Stat() 70 if outf != nil && errf != nil && os.SameFile(outf, errf) { 71 w = os.Stderr 72 } 73 } 74 fmt.Fprintf(w, "Fatal: "+format+"\n", args...) 75 os.Exit(1) 76 }