This introduces a new utility named checkdevpremine which can be used to test transactions to determine whether or not they have inputs that trace back to the original dev premine coins.
143 lines
4.9 KiB
Go
143 lines
4.9 KiB
Go
// Copyright (c) 2016 The Decred developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/decred/dcrutil"
|
|
|
|
flags "github.com/btcsuite/go-flags"
|
|
)
|
|
|
|
var (
|
|
dcrdHomeDir = dcrutil.AppDataDir("dcrd", false)
|
|
appHomeDir = dcrutil.AppDataDir("checkdevpremine", false)
|
|
defaultConfigFile = filepath.Join(appHomeDir, "checkdevpremine.conf")
|
|
defaultRPCServer = "localhost"
|
|
defaultRPCCertFile = filepath.Join(dcrdHomeDir, "rpc.cert")
|
|
)
|
|
|
|
// config defines the configuration options for dcrctl.
|
|
//
|
|
// See loadConfig for details on the configuration load process.
|
|
type config struct {
|
|
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"`
|
|
RPCUser string `short:"u" long:"rpcuser" description:"RPC username"`
|
|
RPCPassword string `short:"P" long:"rpcpass" default-mask:"-" description:"RPC password"`
|
|
RPCServer string `short:"s" long:"rpcserver" description:"RPC server to connect to"`
|
|
RPCCert string `short:"c" long:"rpccert" description:"RPC server certificate chain for validation"`
|
|
NoTLS bool `long:"notls" description:"Disable TLS"`
|
|
TLSSkipVerify bool `long:"skipverify" description:"Do not verify tls certificates (not recommended!)"`
|
|
Quiet bool `long:"quiet" description:"Do not print any found outpoints which can be useful if only relying on the return code"`
|
|
}
|
|
|
|
// normalizeAddress returns addr with the default port appended if there is not
|
|
// already a port specified.
|
|
func normalizeAddress(addr string) string {
|
|
_, _, err := net.SplitHostPort(addr)
|
|
if err != nil {
|
|
return net.JoinHostPort(addr, "9109")
|
|
}
|
|
return addr
|
|
}
|
|
|
|
// cleanAndExpandPath expands environement variables and leading ~ in the
|
|
// passed path, cleans the result, and returns it.
|
|
func cleanAndExpandPath(path string) string {
|
|
// Expand initial ~ to OS specific home directory.
|
|
if strings.HasPrefix(path, "~") {
|
|
homeDir := filepath.Dir(appHomeDir)
|
|
path = strings.Replace(path, "~", homeDir, 1)
|
|
}
|
|
|
|
// NOTE: The os.ExpandEnv doesn't work with Windows-style %VARIABLE%,
|
|
// but they variables can still be expanded via POSIX-style $VARIABLE.
|
|
return filepath.Clean(os.ExpandEnv(path))
|
|
}
|
|
|
|
// loadConfig initializes and parses the config using a config file and command
|
|
// line options.
|
|
//
|
|
// The configuration proceeds as follows:
|
|
// 1) Start with a default config with sane settings
|
|
// 2) Pre-parse the command line to check for an alternative config file
|
|
// 3) Load configuration file overwriting defaults with any specified options
|
|
// 4) Parse CLI options and overwrite/add any specified options
|
|
//
|
|
// The above results in functioning properly without any config settings
|
|
// while still allowing the user to override settings with config files and
|
|
// command line options. Command line options always take precedence.
|
|
func loadConfig() (*config, []string, error) {
|
|
// Default config.
|
|
cfg := config{
|
|
ConfigFile: defaultConfigFile,
|
|
RPCServer: defaultRPCServer,
|
|
RPCCert: defaultRPCCertFile,
|
|
}
|
|
|
|
// Create the home directory if it doesn't already exist.
|
|
err := os.MkdirAll(dcrdHomeDir, 0700)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
os.Exit(-1)
|
|
}
|
|
|
|
// Pre-parse the command line options to see if an alternative config
|
|
// file, the version flag, or the list commands flag was specified. Any
|
|
// errors aside from the help message error can be ignored here since
|
|
// they will be caught by the final parse below.
|
|
preCfg := cfg
|
|
preParser := flags.NewParser(&preCfg, flags.HelpFlag)
|
|
_, err = preParser.Parse()
|
|
if err != nil {
|
|
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
fmt.Fprintln(os.Stderr, "")
|
|
fmt.Fprintln(os.Stderr, "The special parameter `-` "+
|
|
"indicates that a parameter should be read "+
|
|
"from the\nnext unread line from standard "+
|
|
"input.")
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
|
|
// Load additional config from file.
|
|
appName := filepath.Base(os.Args[0])
|
|
appName = strings.TrimSuffix(appName, filepath.Ext(appName))
|
|
usageMessage := fmt.Sprintf("Use %s -h to show options", appName)
|
|
parser := flags.NewParser(&cfg, flags.Default)
|
|
err = flags.NewIniParser(parser).ParseFile(preCfg.ConfigFile)
|
|
if err != nil {
|
|
if _, ok := err.(*os.PathError); !ok {
|
|
fmt.Fprintf(os.Stderr, "Error parsing config file: %v\n",
|
|
err)
|
|
fmt.Fprintln(os.Stderr, usageMessage)
|
|
return nil, nil, err
|
|
}
|
|
}
|
|
|
|
// Parse command line options again to ensure they take precedence.
|
|
remainingArgs, err := parser.Parse()
|
|
if err != nil {
|
|
if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
|
|
fmt.Fprintln(os.Stderr, usageMessage)
|
|
}
|
|
return nil, nil, err
|
|
}
|
|
|
|
// Handle environment variable expansion in the RPC certificate path.
|
|
cfg.RPCCert = cleanAndExpandPath(cfg.RPCCert)
|
|
|
|
// Add default port to RPC server if needed.
|
|
cfg.RPCServer = normalizeAddress(cfg.RPCServer)
|
|
|
|
return &cfg, remainingArgs, nil
|
|
}
|