Compare commits

...

7 Commits

5 changed files with 105 additions and 20 deletions

29
api.go
View File

@ -13,6 +13,8 @@ const (
GetBlock Method = "getblock"
GetTransaction Method = "gettransaction"
ZGetTotalBalance = "z_gettotalbalance"
ZListAddresses = "z_listaddresses"
ZListReceivedByAddress = "z_listreceivedbyaddress"
ZSendMany = "z_sendmany"
ZValidateAddress = "z_validateaddress"
@ -58,6 +60,23 @@ func NewZValidateAddress(address string) []byte {
return data
}
// NewZListAddresses constructs a properly formatted z_listaddresses request
func NewZListAddresses() []byte {
request := setupZcashRequest()
request.Method = ZListAddresses
data, _ := json.Marshal(request)
return data
}
// NewZGetTotalBalance constructs a properly formatted z_gettotalbalance request
func NewZGetTotalBalance(minConfirmations int) []byte {
request := setupZcashRequest()
request.Method = ZGetTotalBalance
request.Params = append(request.Params, minConfirmations)
data, _ := json.Marshal(request)
return data
}
// NewZListReceivedByAddress constructs a properly formatted z_listreceivedbyaddress request
func NewZListReceivedByAddress(fromAddress string) []byte {
request := setupZcashRequest()
@ -107,6 +126,9 @@ type ZcashTransaction struct {
Memo Hex `json:"memo"`
Change bool `json:"change"`
OutIndex int `json:"outindex"`
// The following parameters are not provided by the zcash node
Address string `json:"address"`
}
// ZcashResult represents the result of a zcash operation. Result can be any one of a number of defined types.
@ -123,3 +145,10 @@ type ZValidateAddressResponse struct {
Diversifier Hex `json:"diversifier"`
DiversifiedTransmissionKey Hex `json:"diversifiedtransmissionkey"`
}
// ZGetTotalBalanceResponse encapsulates the result returned from a z_gettotalbalance request
type ZGetTotalBalanceResponse struct {
Transparent string `json:"transparent"`
Private string `json:"private"`
Total string `json:"total"`
}

4
go.mod
View File

@ -4,10 +4,6 @@ go 1.12
require (
git.openprivacy.ca/openprivacy/libricochet-go v1.0.6
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dghubble/go-twitter v0.0.0-20190719072343-39e5462e111f
github.com/dghubble/oauth1 v0.6.0
github.com/golang/protobuf v1.3.2 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.4.0 // indirect
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586 // indirect

14
go.sum
View File

@ -2,26 +2,12 @@ git.openprivacy.ca/openprivacy/libricochet-go v1.0.6 h1:5o4K2qn3otEE1InC5v5CzU0y
git.openprivacy.ca/openprivacy/libricochet-go v1.0.6/go.mod h1:yMSG1gBaP4f1U+RMZXN85d29D39OK5s8aTpyVRoH5FY=
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 h1:w1UutsfOrms1J05zt7ISrnJIXKzwaspym5BTKGx93EI=
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412/go.mod h1:WPjqKcmVOxf0XSf3YxCJs6N6AOSrOx3obionmG7T0y0=
github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY=
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cretz/bine v0.1.0 h1:1/fvhLE+fk0bPzjdO5Ci+0ComYxEMuB1JhM4X5skT3g=
github.com/cretz/bine v0.1.0/go.mod h1:6PF6fWAvYtwjRGkAuDEJeWNOv3a2hUouSP/yRYXmvHw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dghubble/go-twitter v0.0.0-20190719072343-39e5462e111f h1:M2wB039zeS1/LZtN/3A7tWyfctiOBL4ty5PURBmDdWU=
github.com/dghubble/go-twitter v0.0.0-20190719072343-39e5462e111f/go.mod h1:xfg4uS5LEzOj8PgZV7SQYRHbG7jPUnelEiaAVJxmhJE=
github.com/dghubble/oauth1 v0.6.0 h1:m1yC01Ohc/eF38jwZ8JUjL1a+XHHXtGQgK+MxQbmSx0=
github.com/dghubble/oauth1 v0.6.0/go.mod h1:8pFdfPkv/jr8mkChVbNVuJ0suiHe278BtWI4Tk1ujxk=
github.com/dghubble/sling v1.3.0 h1:pZHjCJq4zJvc6qVQ5wN1jo5oNZlNE0+8T/h0XeXBUKU=
github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

View File

@ -1,9 +1,9 @@
package zcash2cwtch
import (
"cwtch.im/zcash2cwtch"
"encoding/json"
"git.openprivacy.ca/openprivacy/libricochet-go/log"
"git.openprivacy.ca/openprivacy/zcashrpc"
"io/ioutil"
"testing"
)
@ -13,6 +13,52 @@ type ZcashConfig struct {
Password string `json:"password"`
}
func TestGetTotalBalance(t *testing.T) {
log.SetLevel(log.LevelDebug)
configFile, _ := ioutil.ReadFile("config.json")
config := ZcashConfig{}
_ = json.Unmarshal(configFile, &config)
zc := zcashrpc.NewLocalClient(config.Username, config.Password)
result, err := zc.GetTotalBalance(1)
t.Logf("Result: %v %v", result, err)
if err != nil {
t.Errorf("Failed to get total balance: %v %v", result, err)
}
}
func TestListTransactions(t *testing.T) {
log.SetLevel(log.LevelDebug)
configFile, _ := ioutil.ReadFile("config.json")
config := ZcashConfig{}
_ = json.Unmarshal(configFile, &config)
zc := zcashrpc.NewLocalClient(config.Username, config.Password)
result, err := zc.ListReceivedTransactionsByAddress("zs1pjv7eneq9jshw0eyywpruv2cetl74sh84ymdnyv4c4vg8vl5k2qmlv0n7ye77g49lhqkg75v52f")
t.Logf("Result: %v %v", result, err)
if len(result) == 0 {
t.Errorf("Failed to list transactions")
} else {
for _, transaction := range result {
t.Logf("Transaction: %v", transaction)
}
}
}
func TestListAddresses(t *testing.T) {
log.SetLevel(log.LevelDebug)
configFile, _ := ioutil.ReadFile("config.json")
config := ZcashConfig{}
_ = json.Unmarshal(configFile, &config)
zc := zcashrpc.NewLocalClient(config.Username, config.Password)
result, err := zc.ListAddresses()
t.Logf("Result: %v %v", result, err)
if len(result) == 0 {
t.Errorf("Failed to list zaddresses")
}
}
func TestNewZValidateAddress(t *testing.T) {
log.SetLevel(log.LevelDebug)
configFile, _ := ioutil.ReadFile("config.json")

View File

@ -9,12 +9,15 @@ import (
"io/ioutil"
"net"
"net/http"
"strconv"
)
// ZcashClient defines an interface for any zcash client to present.
type ZcashClient interface {
ListReceivedTransactionsByAddress(string) ([]ZcashTransaction, error)
GetTotalBalance(minConfirmations int) (float64, error)
GetTransaction(string) (Transaction, error)
ListAddresses() ([]string, error)
ListReceivedTransactionsByAddress(string) ([]ZcashTransaction, error)
GetBlock(int) (Block, error)
SendOne(string, string, string, float64) ([]byte, error)
ValidateAddress(address string) (ZValidateAddressResponse, error)
@ -58,6 +61,17 @@ func NewOnionClient(onion string, username, password string, acn connectivity.AC
return zc
}
// Return the total value of funds stored in the nodes wallet. Set the minimum number of confirmations a private or transparent transaction must have in order to be included in the balance. Use 0 to count unconfirmed transactions.
func (zc *zcashClient) GetTotalBalance(minConfirmations int) (float64, error) {
body, err := zc.sendRequest(NewZGetTotalBalance(minConfirmations))
var totalBalance ZGetTotalBalanceResponse
if err == nil {
result := &ZcashResult{Result: &totalBalance}
err = json.Unmarshal(body, &result)
}
return strconv.ParseFloat(totalBalance.Private, 64)
}
// GetTransaction returns a specific transaction given an id
func (zc *zcashClient) GetBlock(id int) (Block, error) {
body, err := zc.sendRequest(NewGetBlock(id))
@ -80,6 +94,17 @@ func (zc *zcashClient) GetTransaction(id string) (Transaction, error) {
return transaction, err
}
// Returns a list of all the zaddrs in this nodes wallet for which you have a spending key.
func (zc *zcashClient) ListAddresses() ([]string, error) {
body, err := zc.sendRequest(NewZListAddresses())
var addresses []string
if err == nil {
result := &ZcashResult{Result: &addresses}
err = json.Unmarshal(body, &result)
}
return addresses, err
}
// ListReceivedTransactionsByAddress returns all the transactions received by a given zcash address
func (zc *zcashClient) ListReceivedTransactionsByAddress(address string) ([]ZcashTransaction, error) {
body, err := zc.sendRequest(NewZListReceivedByAddress(address))
@ -88,6 +113,9 @@ func (zc *zcashClient) ListReceivedTransactionsByAddress(address string) ([]Zcas
result := &ZcashResult{Result: &transactions}
err = json.Unmarshal(body, &result)
}
for i := range transactions {
transactions[i].Address = address
}
return transactions, err
}