Skip to content

Commit

Permalink
add noconf option to check, also refactor rpc lib a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Mar 30, 2017
1 parent f04a357 commit 63a5c35
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
4 changes: 2 additions & 2 deletions auth.go
Expand Up @@ -16,8 +16,8 @@ func init() {
fmt.Println("Error reading zcash config: ", err)
}

rpc.DefaultUser = u
rpc.DefaultPass = p
rpc.DefaultClient.User = u
rpc.DefaultClient.Pass = p
}

func readAuthCreds() (string, string, error) {
Expand Down
20 changes: 15 additions & 5 deletions main.go
Expand Up @@ -55,12 +55,18 @@ func parseTypedData(d []byte) (uint64, []byte, error) {
}

// getReceivedForAddr returns all received messages for a given address
func getReceivedForAddr(addr string) ([]*Message, error) {
func getReceivedForAddr(addr string, noconf bool) ([]*Message, error) {
req := &rpc.Request{
Method: "z_listreceivedbyaddress",
Params: []string{addr},
}

params := []interface{}{addr}
if noconf {
params = append(params, 0)
}

req.Params = params

var out struct {
Result []*TxDesc
}
Expand Down Expand Up @@ -130,15 +136,15 @@ func getReceivedForAddr(addr string) ([]*Message, error) {
}

// CheckMessages returns all messages that the local zcash daemon has received
func CheckMessages() ([]*Message, error) {
func CheckMessages(noconf bool) ([]*Message, error) {
addrs, err := getMyAddresses()
if err != nil {
return nil, err
}

var allmsgs []*Message
for _, myaddr := range addrs {
msgs, err := getReceivedForAddr(myaddr)
msgs, err := getReceivedForAddr(myaddr, noconf)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -291,10 +297,14 @@ var CheckCmd = cli.Command{
Name: "verbose",
Usage: "enable verbose log output",
},
cli.BoolFlag{
Name: "noconf",
Usage: "show messages with no transaction confirmations",
},
},
Action: func(c *cli.Context) error {
Verbose = c.Bool("verbose")
msgs, err := CheckMessages()
msgs, err := CheckMessages(c.Bool("noconf"))
if err != nil {
return err
}
Expand Down
22 changes: 19 additions & 3 deletions rpc/rpc.go
Expand Up @@ -23,29 +23,45 @@ func (e Error) Error() string {

type Response struct {
Result interface{} `json:"result"`
Error Error `json:"error"`
Error *Error `json:"error"`
}

type Request struct {
Method string `json:"method"`
Params interface{} `json:"params"`
}

type Client struct {
Host string
User string
Pass string
}

var DefaultClient = &Client{
Host: "http://localhost:8232",
}

func Do(obj *Request, out interface{}) error {
return DefaultClient.Do(obj, out)
}

func (c *Client) Do(obj *Request, out interface{}) error {
data, err := json.Marshal(obj)
if err != nil {
return err
}

body := bytes.NewReader(data)

req, err := http.NewRequest("POST", "http://localhost:8232/", body)
req, err := http.NewRequest("POST", c.Host, body)
if err != nil {
return err
}

// auth auth baby
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(DefaultUser+":"+DefaultPass)))
if c.User != "" {
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(c.User+":"+c.Pass)))
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down

0 comments on commit 63a5c35

Please sign in to comment.