Skip to content

Commit

Permalink
Added cmd line helps
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Mar 12, 2019
1 parent 806fc85 commit 429016a
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 17 deletions.
5 changes: 5 additions & 0 deletions cmd/benchclient/ordercmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ func (cl *BenchClient) CancelOrder(orderID string) (cancelOrderReply *cxrpc.Canc
OrderID: orderID,
}

// get privkey for signing
privkey, _ := koblitz.PrivKeyFromBytes(koblitz.S256(), cl.PrivKey[:])

// create e = hash(m)
sha3 := sha3.New256()
sha3.Write([]byte(cancelOrderArgs.OrderID))
Expand All @@ -127,6 +130,8 @@ func (cl *BenchClient) CancelOrder(orderID string) (cancelOrderReply *cxrpc.Canc
// Sign order
compactSig, err := koblitz.SignCompact(koblitz.S256(), privkey, e, false)

cancelOrderArgs.Signature = compactSig

// Actually use the RPC Client to call the method
if err = cl.Call("OpencxRPC.CancelOrder", cancelOrderArgs, cancelOrderReply); err != nil {
return
Expand Down
12 changes: 12 additions & 0 deletions cmd/ocx/accountcmds.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package main

import (
"fmt"

"github.com/mit-dci/lit/lnutil"
"github.com/mit-dci/opencx/cxrpc"
"github.com/mit-dci/opencx/logging"
)

var registerCommand = &Command{
Format: fmt.Sprintf("%s\n", lnutil.White("register")),
Description: fmt.Sprintf("%s\n%s\n",
"Register the public key associated with your private key as an identity on the exchange.",
"You will use your private key to sign commands that require authorization.",
),
ShortDescription: fmt.Sprintf("%s\n", "Register yourself on the exchange"),
}

// Register registers the user for an account with a username and password
func (cl *openCxClient) Register(args []string) (err error) {
if err = cl.UnlockKey(); err != nil {
Expand Down
23 changes: 15 additions & 8 deletions cmd/ocx/ocx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ type openCxClient struct {
}

type ocxConfig struct {
// Filename of config file where this stuff can be set as well
ConfigFile string

// stuff for files and directories
LogFilename string `long:"logFilename" description:"Filename for output log file"`
OcxHomeDir string `long:"dir" description:"Location of the root directory relative to home directory"`
LogFilename string `long:"logFilename" short:"l" description:"Filename for output log file"`
OcxHomeDir string `long:"dir" short:"d" description:"Location of the root directory relative to home directory"`

// stuff for ports
Rpchost string `long:"rpchost" description:"Hostname of OpenCX Server you'd like to connect to"`
Rpcport uint16 `long:"rpcport" description:"Port of the OpenCX Port you'd like to connect to"`
Rpchost string `long:"rpchost" short:"h" description:"Hostname of OpenCX Server you'd like to connect to"`
Rpcport uint16 `long:"rpcport" short:"p" description:"Port of the OpenCX Port you'd like to connect to"`

// filename for key
KeyFileName string `long:"keyfilename" short:"k" description:"Filename for private key within root opencx directory used to send transactions"`

// logging and debug parameters
LogLevel []bool `short:"v" description:"Set verbosity level to verbose (-v), very verbose (-vv) or very very verbose (-vvv)"`
Expand Down Expand Up @@ -60,13 +64,16 @@ func main() {
var client openCxClient

conf := &ocxConfig{
OcxHomeDir: defaultOcxHomeDirName,
Rpchost: defaultRpchost,
Rpcport: defaultRpcport,
OcxHomeDir: defaultOcxHomeDirName,
Rpchost: defaultRpchost,
Rpcport: defaultRpcport,
LogFilename: defaultLogFilename,
KeyFileName: defaultKeyFileName,
ConfigFile: defaultConfigFilename,
}

ocxSetup(conf)
client.KeyPath = filepath.Join(conf.OcxHomeDir, defaultKeyFileName)
client.KeyPath = filepath.Join(conf.OcxHomeDir, conf.KeyFileName)
client.RPCClient = new(benchclient.BenchClient)
if err = client.RPCClient.SetupBenchClient(conf.Rpchost, conf.Rpcport); err != nil {
logging.Fatalf("Error setting up OpenCX RPC Client: \n%s", err)
Expand Down
8 changes: 6 additions & 2 deletions cmd/ocx/ocxinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func ocxSetup(conf *ocxConfig) {

if _, err := parser.ParseArgs(os.Args); err != nil {
// catch all cli argument errors
logging.Fatal(err)
// logging.Fatal(err)
// this just prints it out twice
logging.Fatal("Error parsing args")
}

// create home directory
Expand Down Expand Up @@ -79,7 +81,9 @@ func ocxSetup(conf *ocxConfig) {
// Parse command line options again to ensure they take precedence.
_, err = parser.ParseArgs(os.Args) // returns invalid flags
if err != nil {
logging.Fatal(err)
// logging.Fatal(err)
// This just prints it out again.
return
}

logFilePath := filepath.Join(conf.OcxHomeDir, conf.LogFilename)
Expand Down
47 changes: 47 additions & 0 deletions cmd/ocx/shell.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
package main

import (
"github.com/fatih/color"
"github.com/mit-dci/lit/lnutil"

"fmt"
)

// Command holds information about commands so we can show all the stuff you can do
type Command struct {
Format string
Description string
ShortDescription string
}

var helpCommand = &Command{
Format: fmt.Sprintf("%s%s\n", lnutil.White("help"), lnutil.OptColor("command")),
Description: "Show information about a given command\n",
ShortDescription: "Show information about a given command\n",
}

func (cl *openCxClient) parseCommands(commands []string) error {
var args []string

Expand All @@ -15,6 +31,11 @@ func (cl *openCxClient) parseCommands(commands []string) error {
if len(commands) > 1 {
args = commands[1:]
}
// help gives you really terse help. Just a list of commands.
if cmd == "help" {
err := cl.Help(args)
return err
}
if cmd == "register" {
if len(args) != 0 {
return fmt.Errorf("Please do not specify any arguments. You do not need a username, you will be registered by public key")
Expand Down Expand Up @@ -116,3 +137,29 @@ func (cl *openCxClient) parseCommands(commands []string) error {
}
return nil
}

func printHelp(commands []*Command) {
for _, command := range commands {
fmt.Fprintf(color.Output, "%s\t%s", command.Format, command.ShortDescription)
}
}

func (cl *openCxClient) Help(textArgs []string) error {
if len(textArgs) == 0 {

fmt.Fprintf(color.Output, lnutil.Header("Commands:\n"))
listofCommands := []*Command{}
printHelp(listofCommands)
return nil
}

if textArgs[0] == "help" || textArgs[0] == "-h" {
fmt.Fprintf(color.Output, helpCommand.Format)
fmt.Fprintf(color.Output, helpCommand.Description)
return nil
}
res := make([]string, 0)
res = append(res, textArgs[0])
res = append(res, "-h")
return cl.parseCommands(res)
}
6 changes: 6 additions & 0 deletions db/ocxsql/accountqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/mit-dci/lit/crypto/koblitz"

"github.com/mit-dci/opencx/logging"
"github.com/mit-dci/opencx/match"
)

Expand Down Expand Up @@ -76,6 +77,8 @@ func (db *DB) InsertDepositAddresses(pubkey *koblitz.PublicKey, addressMap map[m
err = tx.Commit()
}()

logging.Infof("pubkey length (serialized, compressed): %d", len(pubkey.SerializeCompressed()))

// use deposit schema
if _, err = tx.Exec("USE " + db.depositSchema + ";"); err != nil {
return
Expand All @@ -85,8 +88,11 @@ func (db *DB) InsertDepositAddresses(pubkey *koblitz.PublicKey, addressMap map[m
for _, asset := range db.assetArray {
// if you found an address in the map
if addr, found := addressMap[asset]; found {

logging.Infof("Addr: %s, len: %d", addr, len(addr))
// insert into db
insertDepositAddrQuery := fmt.Sprintf("INSERT INTO %s VALUES ('%x', '%s') ON DUPLICATE KEY UPDATE address='%s';", asset, pubkey.SerializeCompressed(), addr, addr)
logging.Infof("%s", insertDepositAddrQuery)
if _, err = tx.Exec(insertDepositAddrQuery); err != nil {
return
}
Expand Down
13 changes: 7 additions & 6 deletions db/ocxsql/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,27 @@ func (db *DB) SetupClient(assets []match.Asset, pairs []*match.Pair) error {

// Initialize Balance tables
// hacky workaround to get behind the fact I made a dumb abstraction with InitializeTables
err = db.InitializeNewTables(db.balanceSchema, "pubkey TEXT, balance BIGINT(64)")
// 66 bytes because we use big bytes and they use small bytes for varbinary
err = db.InitializeNewTables(db.balanceSchema, "pubkey VARBINARY(66), balance BIGINT(64)")
if err != nil {
return fmt.Errorf("Could not initialize balance tables: \n%s", err)
}

// Initialize Deposit tables
err = db.InitializeTables(db.depositSchema, "pubkey VARBINARY(33), address VARCHAR(34), CONSTRAINT unique_pubkeys UNIQUE (pubkey, address)")
err = db.InitializeTables(db.depositSchema, "pubkey VARBINARY(66), address VARCHAR(34), CONSTRAINT unique_pubkeys UNIQUE (pubkey, address)")
if err != nil {
return fmt.Errorf("Could not initialize deposit tables: \n%s", err)
}

// Initialize pending_deposits table
err = db.InitializeNewTables(db.pendingDepositSchema, "pubkey VARBINARY(33), expectedConfirmHeight INT(32), depositHeight INT(32), amount BIGINT(64), txid TEXT")
err = db.InitializeNewTables(db.pendingDepositSchema, "pubkey VARBINARY(66), expectedConfirmHeight INT(32), depositHeight INT(32), amount BIGINT(64), txid TEXT")
if err != nil {
return fmt.Errorf("Could not initialize pending deposit tables: \n%s", err)
}

// Initialize order table
// You can have a price up to 30 digits total, and 10 decimal places.
err = db.InitializePairTables(db.orderSchema, "pubkey VARBINARY(33), orderID TEXT, side TEXT, price DOUBLE(30,2) UNSIGNED, amountHave BIGINT(64), amountWant BIGINT(64), time TIMESTAMP")
err = db.InitializePairTables(db.orderSchema, "pubkey VARBINARY(66), orderID TEXT, side TEXT, price DOUBLE(30,2) UNSIGNED, amountHave BIGINT(64), amountWant BIGINT(64), time TIMESTAMP")
if err != nil {
return fmt.Errorf("Could not initialize order tables: \n%s", err)
}
Expand All @@ -122,7 +123,7 @@ func (db *DB) InitializeTables(schemaName string, schemaSpec string) error {
return fmt.Errorf("Could not use %s schema: \n%s", schemaName, err)
}
for _, assetString := range db.assetArray {
tableQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s);", assetString, schemaSpec)
tableQuery := fmt.Sprintf("CREATE OR REPLACE TABLE %s (%s);", assetString, schemaSpec)
_, err = db.DBHandler.Exec(tableQuery)
if err != nil {
return fmt.Errorf("Could not create table %s: \n%s", assetString, err)
Expand All @@ -141,7 +142,7 @@ func (db *DB) InitializeNewTables(schemaName string, schemaSpec string) error {
return fmt.Errorf("Could not use %s schema: \n%s", schemaName, err)
}
for _, assetString := range db.assetArray {
tableQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s);", assetString, schemaSpec)
tableQuery := fmt.Sprintf("CREATE OR REPLACE TABLE %s (%s);", assetString, schemaSpec)
_, err = db.DBHandler.Exec(tableQuery)
if err != nil {
return fmt.Errorf("Could not create table %s: \n%s", assetString, err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dchest/blake256 v1.0.0 // indirect
github.com/deedlefake/crypto v0.0.0-20170910233742-2f50d39c528d // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/fatih/color v1.7.0
github.com/getlantern/deepcopy v0.0.0-20160317154340-7f45deb8130a // indirect
github.com/go-redis/redis v6.15.2+incompatible
github.com/go-sql-driver/mysql v1.4.1
Expand Down
3 changes: 3 additions & 0 deletions opencx.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type opencxConfig struct {
MinPeerPort uint16 `long:"minpeerport" description:"Port to start creating ports for peers at"`
Lithost string `long:"lithost" description:"Host for the lightning node on the exchange to run"`
Litport uint16 `long:"litport" description:"Port for the lightning node on the exchange to run"`

// filename for key
KeyFileName string `long:"keyfilename" short:"k" description:"Filename for private key within root opencx directory used to send transactions"`
}

var (
Expand Down

0 comments on commit 429016a

Please sign in to comment.