67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
|
package zcash2cwtch
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/base64"
|
||
|
"encoding/json"
|
||
|
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
|
||
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||
|
"io/ioutil"
|
||
|
"net"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
type ZcashResult struct {
|
||
|
Result interface{} `json:"result"`
|
||
|
}
|
||
|
|
||
|
type ZcashClient interface {
|
||
|
ListReceivedTransactionsByAddress(string) ([]ZcashTransaction, error)
|
||
|
}
|
||
|
|
||
|
type zcashOnionClient struct {
|
||
|
client http.Client
|
||
|
onion string
|
||
|
auth string
|
||
|
}
|
||
|
|
||
|
// NewClient creates a new Zcash rpc client over an onion address
|
||
|
func NewClient(onion string, username, password string, acn connectivity.ACN) ZcashClient {
|
||
|
zc := new(zcashOnionClient)
|
||
|
zc.onion = onion
|
||
|
zc.client = http.Client{
|
||
|
Transport: &http.Transport{
|
||
|
Dial: func(network, addr string) (net.Conn, error) {
|
||
|
conn, _, err := acn.Open(onion)
|
||
|
return conn, err
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
zc.auth = base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
|
||
|
return zc
|
||
|
}
|
||
|
|
||
|
func (zc *zcashOnionClient) ListReceivedTransactionsByAddress(address string) ([]ZcashTransaction, error) {
|
||
|
var jsonStr = []byte(`{"jsonrpc": "1.0", "id":"zcash2cwtch", "method": "z_listreceivedbyaddress", "params": ["` + address + `"]}`)
|
||
|
req, err := http.NewRequest("POST", "http://"+zc.onion+".onion:9878", bytes.NewBuffer(jsonStr))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
req.Header.Add("Authorization", "Basic "+zc.auth)
|
||
|
log.Debugf("Sending request to zcash server %v", req)
|
||
|
resp, err := zc.client.Do(req)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
body, err := ioutil.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
log.Debugf("Got response from zcash server %s", body)
|
||
|
transactions := []ZcashTransaction{}
|
||
|
result := &ZcashResult{Result: &transactions}
|
||
|
err = json.Unmarshal(body, &result)
|
||
|
return transactions, err
|
||
|
}
|