2019-08-23 20:51:21 +00:00
|
|
|
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)
|
2019-09-14 01:13:29 +00:00
|
|
|
GetTransaction(string) (Transaction, error)
|
2019-08-23 20:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-09-14 01:13:29 +00:00
|
|
|
func (zc *zcashOnionClient) GetTransaction(id string) (Transaction, error) {
|
|
|
|
var jsonStr = []byte(`{"jsonrpc": "1.0", "id":"zcash2cwtch", "method": "gettransaction", "params": ["` + id + `"]}`)
|
|
|
|
req, err := http.NewRequest("POST", "http://"+zc.onion+".onion:9878", bytes.NewBuffer(jsonStr))
|
|
|
|
if err != nil {
|
|
|
|
return Transaction{}, 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 Transaction{}, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return Transaction{}, err
|
|
|
|
}
|
|
|
|
log.Debugf("Got response from zcash server %s", body)
|
|
|
|
var transaction Transaction
|
|
|
|
result := &ZcashResult{Result: &transaction}
|
|
|
|
err = json.Unmarshal(body, &result)
|
|
|
|
return transaction, err
|
|
|
|
}
|
|
|
|
|
2019-08-23 20:51:21 +00:00
|
|
|
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
|
|
|
|
}
|