This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wallet-handler.go
92 lines (70 loc) · 2.35 KB
/
wallet-handler.go
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
package plugin
import (
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/leonardochaia/vendopunkto/errors"
)
// WalletPluginHandler is the Wallet Plugin HTTP API implementation
// which runs on the plugin server and will be called
// by vendopunkto
// It exposes the wallet interface provided by the
// plugin developer into HTTP endpoints
type WalletPluginHandler struct {
wallet WalletPlugin
serverPlugin ServerPlugin
}
type CoinWalletAddressResponse struct {
Address string `json:"address"`
}
type CoinWalletAddressParams struct {
InvoiceID string `json:"invoiceId"`
}
func NewWalletHandler(plugin WalletPlugin, serverPlugin ServerPlugin) *chi.Mux {
router := chi.NewRouter()
handler := &WalletPluginHandler{
wallet: plugin,
serverPlugin: serverPlugin,
}
router.Post(GenerateAddressWalletEndpoint, errors.WrapHandler(handler.generateAddress))
router.Post(GetIncomingTransfersWalletEndpoint, errors.WrapHandler(handler.getIncomingTransfers))
router.Get(WalletInfoEndpoint, errors.WrapHandler(handler.getWalletInfo))
return router
}
func (handler *WalletPluginHandler) generateAddress(w http.ResponseWriter, r *http.Request) error {
const op errors.Op = "plugin.wallet.generateAddress"
var params = new(CoinWalletAddressParams)
if err := render.DecodeJSON(r.Body, ¶ms); err != nil {
return errors.E(op, errors.Parameters, err)
}
res, err := handler.wallet.GenerateNewAddress(params.InvoiceID)
if err != nil {
return errors.E(op, errors.Internal, err)
}
render.JSON(w, r, &CoinWalletAddressResponse{
Address: res,
})
return nil
}
func (handler *WalletPluginHandler) getIncomingTransfers(w http.ResponseWriter, r *http.Request) error {
const op errors.Op = "plugin.wallet.getIncomingTransfers"
var params = new(WalletPluginIncomingTransferParams)
if err := render.DecodeJSON(r.Body, ¶ms); err != nil {
return errors.E(op, errors.Parameters, err)
}
res, err := handler.wallet.GetIncomingTransfers(*params)
if err != nil {
return errors.E(op, errors.Internal, err)
}
render.JSON(w, r, res)
return nil
}
func (handler *WalletPluginHandler) getWalletInfo(w http.ResponseWriter, r *http.Request) error {
const op errors.Op = "plugin.wallet.getWalletInfo"
res, err := handler.wallet.GetWalletInfo()
if err != nil {
return errors.E(op, errors.Internal, err)
}
render.JSON(w, r, res)
return nil
}