github.com/piotrnar/gocoin@v0.0.0-20240512203912-faa0448c5e96/wallet/config.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"strconv"
     9  	"strings"
    10  )
    11  
    12  var (
    13  	keycnt       uint   = 250
    14  	testnet      bool   = false
    15  	waltype      uint   = 3
    16  	uncompressed bool   = false
    17  	fee          string = "0.001"
    18  	apply2bal    bool   = true
    19  	secret_seed  []byte
    20  	litecoin     bool = false
    21  	txfilename   string
    22  	stdin        bool
    23  	hdpath       string = "m/0'"
    24  	bip39wrds    int    = 0
    25  	minsig       bool
    26  	usescrypt    uint
    27  	hdsubs       uint   = 1
    28  	atype        string = "p2kh"
    29  
    30  	segwit_mode, bech32_mode, taproot_mode, pubkeys_mode bool
    31  )
    32  
    33  func check_atype() {
    34  	switch atype {
    35  	case "p2kh":
    36  		segwit_mode, bech32_mode, taproot_mode, pubkeys_mode = false, false, false, false
    37  	case "segwit":
    38  		segwit_mode, bech32_mode, taproot_mode, pubkeys_mode = true, false, false, false
    39  	case "bech32":
    40  		segwit_mode, bech32_mode, taproot_mode, pubkeys_mode = true, true, false, false
    41  	case "tap":
    42  		segwit_mode, bech32_mode, taproot_mode, pubkeys_mode = true, true, true, false
    43  	case "pks":
    44  		segwit_mode, bech32_mode, taproot_mode, pubkeys_mode = false, false, false, true
    45  	default:
    46  		println("ERROR: Invalid value of atype:", atype)
    47  		os.Exit(1)
    48  	}
    49  }
    50  
    51  func parse_config() {
    52  	cfgfn := ""
    53  
    54  	// pre-parse command line: look for -cfg <fname> or -h
    55  	for i := 1; i < len(os.Args); i++ {
    56  		if os.Args[i] == "-cfg" || os.Args[i] == "--cfg" {
    57  			if i+1 >= len(os.Args) {
    58  				println("Missing the file name for", os.Args[i], "argument")
    59  				os.Exit(1)
    60  			}
    61  			cfgfn = os.Args[i+1]
    62  			break
    63  		}
    64  		if strings.HasPrefix(os.Args[i], "-cfg=") || strings.HasPrefix(os.Args[i], "--cfg=") {
    65  			ss := strings.SplitN(os.Args[i], "=", 2)
    66  			cfgfn = ss[1]
    67  		}
    68  	}
    69  
    70  	if cfgfn == "" {
    71  		cfgfn = os.Getenv("GOCOIN_WALLET_CONFIG")
    72  		if cfgfn == "" {
    73  			cfgfn = *cfg_fn
    74  		}
    75  	}
    76  	d, e := ioutil.ReadFile(cfgfn)
    77  	if e != nil {
    78  		fmt.Println(cfgfn, "not found - proceeding with the default config values.")
    79  	} else {
    80  		fmt.Println("Using config file", cfgfn)
    81  		lines := strings.Split(string(d), "\n")
    82  		for i := range lines {
    83  			line := strings.Trim(lines[i], " \n\r\t")
    84  			if len(line) == 0 || line[0] == '#' {
    85  				continue
    86  			}
    87  
    88  			ll := strings.SplitN(line, "=", 2)
    89  			if len(ll) != 2 {
    90  				println(i, "wallet.cfg: syntax error in line", ll)
    91  				continue
    92  			}
    93  
    94  			switch strings.ToLower(ll[0]) {
    95  			case "testnet":
    96  				v, e := strconv.ParseBool(ll[1])
    97  				if e == nil {
    98  					testnet = v
    99  				} else {
   100  					println(i, "wallet.cfg: value error for", ll[0], ":", e.Error())
   101  					os.Exit(1)
   102  				}
   103  
   104  			case "type":
   105  				v, e := strconv.ParseUint(ll[1], 10, 32)
   106  				if e == nil {
   107  					if v >= 1 && v <= 4 {
   108  						waltype = uint(v)
   109  					} else {
   110  						println(i, "wallet.cfg: incorrect wallet type", v)
   111  						os.Exit(1)
   112  					}
   113  				} else {
   114  					println(i, "wallet.cfg: value error for", ll[0], ":", e.Error())
   115  					os.Exit(1)
   116  				}
   117  
   118  			case "hdpath":
   119  				hdpath = strings.Trim(ll[1], "\"")
   120  
   121  			case "bip39":
   122  				v, e := strconv.ParseInt(ll[1], 10, 32)
   123  				if e == nil {
   124  					if v == -1 || v >= 12 && v <= 24 && (v%3) == 0 {
   125  						bip39wrds = int(v)
   126  					} else {
   127  						println(i, "wallet.cfg: incorrect bip39 value", v)
   128  						os.Exit(1)
   129  					}
   130  				} else {
   131  					println(i, "wallet.cfg: value error for", ll[0], ":", e.Error())
   132  					os.Exit(1)
   133  				}
   134  
   135  			case "hdsubs":
   136  				v, e := strconv.ParseUint(ll[1], 10, 32)
   137  				if e == nil {
   138  					if v >= 1 {
   139  						hdsubs = uint(v)
   140  					} else {
   141  						println(i, "wallet.cfg: incorrect hdsubs value", v)
   142  						os.Exit(1)
   143  					}
   144  				} else {
   145  					println(i, "wallet.cfg: value error for", ll[0], ":", e.Error())
   146  					os.Exit(1)
   147  				}
   148  
   149  			case "keycnt":
   150  				v, e := strconv.ParseUint(ll[1], 10, 32)
   151  				if e == nil {
   152  					if v >= 1 {
   153  						keycnt = uint(v)
   154  					} else {
   155  						println(i, "wallet.cfg: incorrect key count", v)
   156  						os.Exit(1)
   157  					}
   158  				} else {
   159  					println(i, "wallet.cfg: value error for", ll[0], ":", e.Error())
   160  					os.Exit(1)
   161  				}
   162  
   163  			case "atype":
   164  				atype = strings.Trim(ll[1], "\" ")
   165  
   166  			case "uncompressed":
   167  				v, e := strconv.ParseBool(ll[1])
   168  				if e == nil {
   169  					uncompressed = v
   170  				} else {
   171  					println(i, "wallet.cfg: value error for", ll[0], ":", e.Error())
   172  					os.Exit(1)
   173  				}
   174  
   175  			// case "secrand": <-- deprecated
   176  
   177  			case "fee":
   178  				fee = ll[1]
   179  
   180  			case "apply2bal":
   181  				v, e := strconv.ParseBool(ll[1])
   182  				if e == nil {
   183  					apply2bal = v
   184  				} else {
   185  					println(i, "wallet.cfg: value error for", ll[0], ":", e.Error())
   186  					os.Exit(1)
   187  				}
   188  
   189  			case "secret":
   190  				PassSeedFilename = ll[1]
   191  
   192  			case "others":
   193  				RawKeysFilename = ll[1]
   194  
   195  			case "seed":
   196  				if !*nosseed {
   197  					secret_seed = []byte(strings.Trim(ll[1], " \t\n\r"))
   198  				}
   199  
   200  			case "litecoin":
   201  				v, e := strconv.ParseBool(ll[1])
   202  				if e == nil {
   203  					litecoin = v
   204  				} else {
   205  					println(i, "wallet.cfg: value error for", ll[0], ":", e.Error())
   206  					os.Exit(1)
   207  				}
   208  
   209  			case "minsig":
   210  				v, e := strconv.ParseBool(ll[1])
   211  				if e == nil {
   212  					minsig = v
   213  				} else {
   214  					println(i, "wallet.cfg: value error for", ll[0], ":", e.Error())
   215  					os.Exit(1)
   216  				}
   217  
   218  			case "scrypt":
   219  				v, e := strconv.ParseUint(ll[1], 10, 32)
   220  				if e == nil {
   221  					if v >= 1 {
   222  						usescrypt = uint(v)
   223  					} else {
   224  						println(i, "wallet.cfg: incorrect scrypt value", v)
   225  						os.Exit(1)
   226  					}
   227  				} else {
   228  					println(i, "wallet.cfg: value error for", ll[0], ":", e.Error())
   229  					os.Exit(1)
   230  				}
   231  			}
   232  		}
   233  	}
   234  
   235  	flag.UintVar(&keycnt, "n", keycnt, "Set the number of determinstic keys to be calculated by the wallet")
   236  	flag.BoolVar(&testnet, "t", testnet, "Testnet mode")
   237  	flag.UintVar(&waltype, "type", waltype, "Type of a deterministic wallet to be used (1 to 4)")
   238  	flag.StringVar(&hdpath, "hdpath", hdpath, "Derivation Path to the first key in HD wallet (type=4)")
   239  	flag.UintVar(&hdsubs, "hdsubs", hdsubs, "Create HD Wallet with so many sub-accounts (use 2 for common walets)")
   240  	flag.IntVar(&bip39wrds, "bip39", bip39wrds, "Create HD Wallet in BIP39 mode using 12, 15, 18, 21 or 24 words")
   241  	flag.BoolVar(&uncompressed, "u", uncompressed, "Deprecated in this version")
   242  	flag.StringVar(&fee, "fee", fee, "Specify transaction fee to be used")
   243  	flag.BoolVar(&apply2bal, "a", apply2bal, "Apply changes to the balance folder (does not work with -raw)")
   244  	flag.BoolVar(&litecoin, "ltc", litecoin, "Litecoin mode")
   245  	flag.StringVar(&txfilename, "txfn", "", "Use this filename for output transaction (otherwise use a random name)")
   246  	flag.BoolVar(&stdin, "stdin", stdin, "Read password from stdin")
   247  	flag.BoolVar(&minsig, "minsig", minsig, "Make sure R and S inside ECDSA signatures are only 32 bytes long")
   248  	flag.UintVar(&usescrypt, "scrypt", usescrypt, "Use extra scrypt function to convert password into private keys (default 0 = disabled)")
   249  	flag.StringVar(&atype, "atype", atype, "When listing, use this type of deposit addresses")
   250  	if uncompressed {
   251  		fmt.Println("WARNING: Using uncompressed keys")
   252  	}
   253  }