Skip to content

Commit

Permalink
move rpc code into its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Dec 10, 2016
1 parent 7fb2a7b commit f04a357
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
9 changes: 4 additions & 5 deletions auth.go
Expand Up @@ -6,19 +6,18 @@ import (
"os"
"path/filepath"
"strings"
)

var username string
var password string
rpc "github.com/whyrusleeping/zmsg/rpc"
)

func init() {
u, p, err := readAuthCreds()
if err != nil {
fmt.Println("Error reading zcash config: ", err)
}

username = u
password = p
rpc.DefaultUser = u
rpc.DefaultPass = p
}

func readAuthCreds() (string, string, error) {
Expand Down
23 changes: 12 additions & 11 deletions main.go
Expand Up @@ -10,6 +10,7 @@ import (
"time"

cli "github.com/urfave/cli"
rpc "github.com/whyrusleeping/zmsg/rpc"
)

var Verbose = false
Expand All @@ -22,14 +23,14 @@ type Message struct {
}

func getMyAddresses() ([]string, error) {
req := &Request{
req := &rpc.Request{
Method: "z_listaddresses",
}

var out struct {
Result []string
}
err := request(req, &out)
err := rpc.Do(req, &out)
if err != nil {
return nil, err
}
Expand All @@ -55,7 +56,7 @@ func parseTypedData(d []byte) (uint64, []byte, error) {

// getReceivedForAddr returns all received messages for a given address
func getReceivedForAddr(addr string) ([]*Message, error) {
req := &Request{
req := &rpc.Request{
Method: "z_listreceivedbyaddress",
Params: []string{addr},
}
Expand All @@ -64,7 +65,7 @@ func getReceivedForAddr(addr string) ([]*Message, error) {
Result []*TxDesc
}

err := request(req, &out)
err := rpc.Do(req, &out)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -168,7 +169,7 @@ func SendMessage(from, to, msg string, msgval float64) (string, error) {
fmt.Printf("sending message from %s\n", from)
}

req := &Request{
req := &rpc.Request{
Method: "z_sendmany",
Params: []interface{}{
from, // first parameter is address to send from (where the ZEC comes from)
Expand All @@ -185,7 +186,7 @@ func SendMessage(from, to, msg string, msgval float64) (string, error) {
var out struct {
Result string
}
err := request(req, &out)
err := rpc.Do(req, &out)
if err != nil {
return "", err
}
Expand All @@ -203,22 +204,22 @@ type opStatus struct {
Id string
Status string
CreationTime uint64 `json:"creation_time"`
Error Error
Error rpc.Error
Result struct {
Txid string
}
}

func checkOperationStatus(opid string) (*opStatus, error) {
req := &Request{
req := &rpc.Request{
Method: "z_getoperationstatus",
Params: []interface{}{[]string{opid}},
}

var out struct {
Result []*opStatus
}
err := request(req, &out)
err := rpc.Do(req, &out)
if err != nil {
return nil, err
}
Expand All @@ -232,7 +233,7 @@ type Transaction struct {
}

func getTransaction(txid string) (*Transaction, error) {
req := &Request{
req := &rpc.Request{
Method: "gettransaction",
Params: []string{txid},
}
Expand All @@ -241,7 +242,7 @@ func getTransaction(txid string) (*Transaction, error) {
Result Transaction
}

err := request(req, &out)
err := rpc.Do(req, &out)
if err != nil {
return nil, err
}
Expand Down
9 changes: 6 additions & 3 deletions rpc.go → rpc/rpc.go
@@ -1,4 +1,4 @@
package main
package rpc

import (
"bytes"
Expand All @@ -9,6 +9,9 @@ import (
"strings"
)

var DefaultUser string
var DefaultPass string

type Error struct {
Code int `json:"code"`
Message string `json:"message"`
Expand All @@ -28,7 +31,7 @@ type Request struct {
Params interface{} `json:"params"`
}

func request(obj *Request, out interface{}) error {
func Do(obj *Request, out interface{}) error {
data, err := json.Marshal(obj)
if err != nil {
return err
Expand All @@ -42,7 +45,7 @@ func request(obj *Request, out interface{}) error {
}

// auth auth baby
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(DefaultUser+":"+DefaultPass)))

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

0 comments on commit f04a357

Please sign in to comment.