1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
// Package xmrpayclnt is a client library for the MoneroPay v2 HTTP API.
//
// All amounts are in piconero.
package xmrpayclnt
import (
"bytes"
"context"
"encoding/json"
"log"
"net/http"
"net/url"
"os"
"git.samanthony.xyz/jttp"
"gitlab.com/moneropay/moneropay/v2/pkg/model"
)
const (
balancePath = "/balance"
healthPath = "/health"
receivePath = "/receive"
)
var lg = log.New(os.Stderr, "xmrpayclnt: ", log.LstdFlags)
type Client struct {
c *jttp.Client
endpoint *url.URL
}
// New creates a client that will connect to the moneropay server at
// endpoint.
func New(endpoint *url.URL) *Client {
return &Client{jttp.NewClient(&http.Client{}), endpoint}
}
// Balance returns the entire wallet balance.
func (c *Client) Balance(ctx context.Context) (model.BalanceResponse, error) {
url := c.endpoint.JoinPath(balancePath)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
if err != nil {
return model.BalanceResponse{}, err
}
var bal model.BalanceResponse
err = c.c.Do(req, http.StatusOK, &bal)
return bal, err
}
// Health returns true if all required services are up.
func (c *Client) Health(ctx context.Context) (bool, error) {
url := c.endpoint.JoinPath(healthPath)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
if err != nil {
return false, err
}
var health model.HealthResponse
err = c.c.Do(req, http.StatusOK, &health)
return err == nil, err
}
// NewTx creates a subaddress for incoming transfers.
func (c *Client) NewTx(ctx context.Context, amount uint64, description string) (model.ReceivePostResponse, error) {
return c.postReceive(ctx, amount, description, "")
}
// NewTxWithCallback creates a subaddress and a callback to monitor it.
func (c *Client) NewTxWithCallback(ctx context.Context, amount uint64, description string, ch *CallbackHandler) (model.ReceivePostResponse, Callback, error) {
cb := ch.listen()
if resp, err := c.postReceive(ctx, amount, description, cb.url.String()); err == nil {
return resp, cb, nil
} else {
cb.Close()
return resp, Callback{}, err
}
}
// GetTxStatus retrieves information about a transfer on a subaddress.
func (c *Client) GetTxStatus(ctx context.Context, address string) (model.ReceiveGetResponse, error) {
var resp model.ReceiveGetResponse
url := c.endpoint.JoinPath(receivePath, address)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
if err != nil {
return resp, err
}
err = c.c.Do(req, http.StatusOK, &resp)
return resp, err
}
func (c *Client) postReceive(ctx context.Context, amount uint64, description string, callbackUrl string) (model.ReceivePostResponse, error) {
var resp model.ReceivePostResponse
body := new(bytes.Buffer)
enc := json.NewEncoder(body)
err := enc.Encode(model.ReceivePostRequest{amount, callbackUrl, description})
if err != nil {
return resp, err
}
url := c.endpoint.JoinPath(receivePath)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url.String(), body)
if err != nil {
return resp, err
}
err = c.c.Do(req, http.StatusOK, &resp)
return resp, err
}
|