func main() { // get rpc address from env rpcAddr := os.Getenv("RPC_ADDR") if rpcAddr == "" { rpcAddr = "127.0.0.1:6430" } // get wallet dir from env wltDir := os.Getenv("WALLET_DIR") if wltDir == "" { home := util.UserHome() wltDir = home + "/.skycoin/wallets" } // get wallet name from env wltName := os.Getenv("WALLET_NAME") if wltName == "" { wltName = "skycoin_cli.wlt" } else { if !strings.HasSuffix(wltName, ".wlt") { fmt.Println("value of 'WALLET_NAME' env is not correct, must has .wlt extenstion") return } } // init the skycli skycli.Init(skycli.RPCAddr(rpcAddr), skycli.WalletDir(wltDir), skycli.DefaultWltName(wltName)) cli.SubcommandHelpTemplate = commandHelpTemplate cli.CommandHelpTemplate = commandHelpTemplate cli.HelpFlag = cli.BoolFlag{ Name: "help,h", Usage: "show help, can also be used to show subcommand help", } app := cli.NewApp() app.Usage = "the skycoin command line interface" app.Version = "0.1" app.Commands = skycli.Commands() app.EnableBashCompletion = true app.OnUsageError = func(context *cli.Context, err error, isSubcommand bool) error { fmt.Fprintf(context.App.Writer, "Error: %v\n\n", err) cli.ShowAppHelp(context) return nil } app.CommandNotFound = func(ctx *cli.Context, command string) { tmp := fmt.Sprintf("{{.HelpName}}: '%s' is not a {{.HelpName}} command. See '{{.HelpName}} --help'.\n", command) cli.HelpPrinter(app.Writer, tmp, app) } if err := app.Run(os.Args); err != nil { fmt.Println(err) } }
// initDataDir init the data dir of skycoin exchange. func initDataDir(dir string) string { if dir == "" { logger.Error("data directory is nil") } home := util.UserHome() if home == "" { logger.Warning("Failed to get home directory") dir = filepath.Join("./", dir) } else { dir = filepath.Join(home, dir) } if err := os.MkdirAll(dir, os.FileMode(0700)); err != nil { logger.Error("Failed to create directory %s: %v", dir, err) } return dir }
func main() { var cfg client.Config home := util.UserHome() var servPubkey string flag.StringVar(&cfg.ServAddr, "s", "localhost:8080", "server address") flag.IntVar(&cfg.Port, "p", 6060, "rpc port") flag.StringVar(&cfg.GuiDir, "gui-dir", "./src/web-app/static", "webapp static dir") flag.StringVar(&cfg.WalletDir, "wlt-dir", filepath.Join(home, ".exchange-client/wallet"), "wallet dir") flag.StringVar(&cfg.AccountDir, "account-dir", filepath.Join(home, ".exchange-client/account"), "account dir") flag.StringVar(&servPubkey, "server-pubkey", "02942e46684114b35fe15218dfdc6e0d74af0446a397b8fcbf8b46fb389f756eb8", "server pubkey") flag.Parse() cfg.GuiDir = util.ResolveResourceDirectory(cfg.GuiDir) // init sknet server pubkey sknet.SetPubkey(servPubkey) // init logger. initLogging(logging.DEBUG, true) quit := make(chan int) go catchInterrupt(quit) // Watch for SIGUSR1 go catchDebug() c := client.New(cfg) c.BindCoins(&bitcoin.Bitcoin{}, skycoin.New(cfg.ServAddr), mzcoin.New(cfg.ServAddr)) c.Run() <-quit logger.Info("Goodbye") }
// Init initialize the cli's configuration func Init(ops ...Option) { for _, op := range ops { op(&cfg) } if cfg.RPCAddress == "" { cfg.RPCAddress = "127.0.0.1:6422" } if cfg.WalletDir == "" { home := util.UserHome() cfg.WalletDir = home + "/." + os.Args[0] + "/wallets" } if cfg.DefaultWalletName == "" { cfg.DefaultWalletName = fmt.Sprintf("%s_cli.wlt", os.Args[0]) } commands = append(commands, addPrivateKeyCMD(), blocksCMD(), broadcastTxCMD(), checkBalanceCMD(), createRawTxCMD(), generateAddrsCMD(), generateWalletCMD(), lastBlocksCMD(), listAddressesCMD(), listWalletsCMD(), sendCMD(), statusCMD(), transactionCMD(), versionCMD(), walletDirCMD(), walletHisCMD()) }
package account import ( "errors" "fmt" "os" "path/filepath" "sync" logging "github.com/op/go-logging" "github.com/skycoin/skycoin/src/util" ) var ( acntDir = filepath.Join(util.UserHome(), ".skycoin-exchange/account") acntName = "account.data" logger = logging.MustGetLogger("exchange.account") ) type Accounter interface { GetID() string // return the account id. GetBalance(ct string) uint64 // return the account's Balance. AddDepositAddress(ct string, addr string) // add the deposit address to the account. DecreaseBalance(ct string, amt uint64) error IncreaseBalance(ct string, amt uint64) error SetBalance(cp string, amt uint64) error } // ExchangeAccount maintains the account state type ExchangeAccount struct { ID string // account id
type Walleter interface { GetID() string // get wallet id. SetID(id string) // set wallet id. SetSeed(seed string) // init the wallet seed. GetType() string // get the wallet coin type. NewAddresses(num int) ([]coin.AddressEntry, error) // generate new addresses. GetAddresses() []string // get all addresses in the wallet. GetKeypair(addr string) (string, string, error) // get pub/sec key pair of specific address Save(w io.Writer) error // save the wallet. Load(r io.Reader) error // load wallet from reader. Copy() Walleter // copy of self, for thread safe. } // wltDir default wallet dir, wallet file name sturct: $type_$seed.wlt. // example: bitcoin_seed.wlt, skycoin_seed.wlt. var wltDir = filepath.Join(util.UserHome(), ".exchange-client/wallet") // Ext wallet file extension name var Ext = "wlt" // Creator wallet creator. type Creator func() Walleter var gWalletCreators = make(map[string]Creator) func init() { // the default wallet creator are registered here, using the following RegisterCreator function // to extend new wallet type. gWalletCreators[bitcoin.Type] = NewBtcWltCreator() gWalletCreators[skycoin.Type] = NewSkyWltCreator() gWalletCreators[mzcoin.Type] = NewSkyWltCreator()