-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
376 lines (351 loc) · 9.72 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/joho/godotenv"
"github.com/trisacrypto/testnet/pkg"
openvasp "github.com/trisacrypto/testnet/pkg/openvasp/web-service-gin"
"github.com/urfave/cli"
)
func main() {
// Load the dotenv file if it exists
godotenv.Load()
app := cli.NewApp()
app.Name = "openvasp"
app.Version = pkg.Version()
app.Usage = "a tool used to run and test a Gin server implementing the TRP protocol"
app.Flags = []cli.Flag{}
app.Commands = []cli.Command{
{
Name: "serve",
Usage: "run the OpenVASP gin server",
Category: "server",
Action: serve,
Flags: []cli.Flag{
cli.StringFlag{
Name: "a, address",
Usage: "the address and port to bind the server on",
Value: "localhost:4435",
EnvVar: "OPENVASP_BIND_ADDR",
},
cli.StringFlag{
Name: "c, callback",
Usage: "the URL that the server will listen on for callbacks from the counterparty",
Value: "http://localhost:4435",
EnvVar: "OPENVASP_CALLBACK_URL",
},
cli.StringFlag{
Name: "d, dsn",
Usage: "the dsn of the postgres database to connect to",
Value: "",
EnvVar: "OPENVASP_DATABASE_DSN",
},
},
},
{
Name: "register",
Usage: "register a new contact with the OpenVASP server",
Category: "client",
Action: register,
Flags: []cli.Flag{
cli.StringFlag{
Name: "a, address",
Usage: "address of the gin server",
Value: "openvasp.test-net.io",
},
cli.StringFlag{
Name: "n, name",
Usage: "name of the OpenVASP customer",
Value: "Tildred Milcot",
},
cli.StringFlag{
Name: "t, asset",
Usage: "asset type (for example bitcoin) of the OpenVASP customer",
Value: "BTC",
},
cli.StringFlag{
Name: "w, walletaddress",
Usage: "Wallet address of the OpenVASP customer",
Value: "926ca69a-6c22-42e6-9105-11ab5de1237b",
},
},
},
{
Name: "listusers",
Usage: "list the registered OpenVASP contacts",
Category: "client",
Action: listUsers,
Flags: []cli.Flag{
cli.StringFlag{
Name: "a, address",
Usage: "address of the gin server",
Value: "localhost:4435",
},
},
},
{
Name: "gettraveladdress",
Usage: "list a specific OpenVASP contact",
Category: "client",
Action: getTravelAddress,
Flags: []cli.Flag{
cli.StringFlag{
Name: "a, address",
Usage: "address of the gin server",
Value: "localhost:4435",
},
cli.StringFlag{
Name: "i, id",
Usage: "The customer id of the registered user to lookup",
Required: true,
},
},
},
{
Name: "transfer",
Usage: "initiate a TRP tranfer",
Category: "client",
Action: transfer,
Flags: []cli.Flag{
cli.StringFlag{
Name: "a, address",
Usage: "address of the gin server",
Value: "openvasp.test-net.io/originatortransfer",
},
cli.StringFlag{
Name: "l, lnurl",
Usage: "lnurl encoding the address of the gin server",
Value: "lnurl1dp68gup69uhkcmmrv9kxsmmnwsargdpnx5hhgunpdeekvetj9uunydnrvymrjcfdxe3nyv3dxsex2d3d8ycnqdfdxyckzc34v3jnzv3nxa3r7arpvu7hgunpwejkcun4d3jkjmn3w45hy7gkp969c",
},
cli.BoolFlag{
Name: "b, beneficiary",
Usage: "path to the IVMS101 payload",
},
cli.StringFlag{
Name: "t, asset",
Usage: "asset type for transfer, i.e. Bitcoin, Etheriem, etc.",
Value: "BTC",
},
cli.IntFlag{
Name: "m, amount",
Usage: "amount of the asset type to be transfered",
Value: 100,
},
cli.StringFlag{
Name: "c, callback",
Usage: "callback for the beneficiary to reply to",
Value: "foo",
},
},
},
{
Name: "listtransfers",
Usage: "list the registered OpenVASP contacts",
Category: "client",
Action: listTransfers,
Flags: []cli.Flag{
cli.StringFlag{
Name: "a, address",
Usage: "address of the gin server",
Value: "openvasp.test-net.io",
},
},
},
{
Name: "gettransfer",
Usage: "list a transfer by transfer id",
Category: "client",
Action: getTransfer,
Flags: []cli.Flag{
cli.StringFlag{
Name: "a, address",
Usage: "address of the gin server",
Value: "openvasp.test-net.io",
},
cli.StringFlag{
Name: "i, id",
Usage: "transfer id of the transfer to lookup",
Required: true,
},
},
},
{
Name: "confirm",
Usage: "confirms a TRP transfer after resolution",
Category: "client",
Action: confirm,
Flags: []cli.Flag{
cli.StringFlag{
Name: "e, endpoint",
Usage: "address of the gin server",
Value: "localhost:4435",
},
cli.BoolFlag{
Name: "r, reject",
Usage: "whether or not the tranfer should be rejected",
},
cli.StringFlag{
Name: "c, callback",
Usage: "amount of the asset type to be transfered",
Value: "foo",
},
},
},
}
app.Run(os.Args)
}
// Serve the OpenVASP gin server
func serve(c *cli.Context) (err error) {
if err = openvasp.Serve(c.String("address"), c.String("callback"), c.String("dsn")); err != nil {
return cli.NewExitError(err, 1)
}
return nil
}
// sends a POST request to the register endpoint
func register(c *cli.Context) (err error) {
url := fmt.Sprintf("http://%s/register", c.String("address"))
body := fmt.Sprintf(`{"name": "%s", "assettype": %s, "walletaddress": "%s"}`,
c.String("name"),
c.String("asset"),
c.String("walletaddress"))
var response string
if response, err = postRequest(body, url); err != nil {
return cli.NewExitError(err, 1)
}
fmt.Println(response)
return nil
}
// sends a GET request to the listusers endpoint
func listUsers(c *cli.Context) (err error) {
var response string
url := fmt.Sprintf("http://%s/listusers", c.String("address"))
if response, err = getRequest(url); err != nil {
return cli.NewExitError(err, 1)
}
fmt.Println(response)
return nil
}
// sends a GET request to the gettraveladdress endpoint
func getTravelAddress(c *cli.Context) (err error) {
var response string
url := fmt.Sprintf("http://%s/gettraveladdress/%s",
c.String("address"),
c.String("id"))
if response, err = getRequest(url); err != nil {
return cli.NewExitError(err, 1)
}
fmt.Println(response)
return nil
}
// sends a POST request to the transfer endpoint
func transfer(c *cli.Context) (err error) {
var path string
path = "pkg/openvasp/testdata/originator.json"
if c.Bool("beneficiary") {
path = "pkg/openvasp/testdata/beneficiary.json"
}
var file *os.File
if file, err = os.Open(path); err != nil {
return cli.NewExitError(err, 1)
}
defer file.Close()
responseBody := &bytes.Buffer{}
if _, err = io.Copy(responseBody, file); err != nil {
return cli.NewExitError(err, 1)
}
ivms101 := responseBody.String()
ivms101 = strings.ReplaceAll(ivms101, `"`, `*`)
ivms101 = strings.ReplaceAll(ivms101, "\n", "+")
ivms101 = fmt.Sprintf(`"%s"`, ivms101)
url := fmt.Sprintf("https://%s/%s", c.String("address"), c.String("lnurl"))
var response string
body := fmt.Sprintf(`{"asset": {"slip0044": "%s"}, "amount": %d, "callback": "%s", "IVMS101": %s}`,
c.String("asset"),
c.Int("amount"),
c.String("callback"),
ivms101)
if response, err = postRequest(body, url); err != nil {
return cli.NewExitError(err, 1)
}
fmt.Printf("\n%s\n\n", response)
return nil
}
// sends a GET request to the listusers endpoint
func listTransfers(c *cli.Context) (err error) {
var response string
url := fmt.Sprintf("http://%s/listtransfers", c.String("address"))
if response, err = getRequest(url); err != nil {
return cli.NewExitError(err, 1)
}
fmt.Println(response)
return nil
}
// sends a GET request to the transfer endpoint
func getTransfer(c *cli.Context) (err error) {
var response string
url := fmt.Sprintf("http://%s/gettransfer/%s",
c.String("address"),
c.String("id"))
if response, err = getRequest(url); err != nil {
return cli.NewExitError(err, 1)
}
fmt.Println(response)
return nil
}
func confirm(c *cli.Context) (err error) {
var body string
if !c.Bool("reject") {
body = fmt.Sprintf(`{"approved": {"address": "payment address", "callback": "%s"}}`, c.String("callback"))
} else {
body = fmt.Sprintf(`{"rejected": "transfer canceled", "callback": "%s"}`, c.String("callback"))
}
var response string
if response, err = postRequest(body, c.String("endpoint")); err != nil {
return cli.NewExitError(err, 1)
}
fmt.Println(response)
return nil
}
// sends a POST request containing the provided body to the provided
// URL and returns the response
func postRequest(body string, url string) (_ string, err error) {
var request *http.Request
byteBody := []byte(body)
if request, err = http.NewRequest(http.MethodPost, url, bytes.NewReader(byteBody)); err != nil {
return "", err
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("api-version", "3.0.0")
request.Header.Set("request-identifier", "ebxe")
var response *http.Response
if response, err = http.DefaultClient.Do(request); err != nil {
return "", cli.NewExitError(err, 1)
}
fmt.Println(response)
responseBody := &bytes.Buffer{}
if _, err = io.Copy(responseBody, response.Body); err != nil {
return "", cli.NewExitError(err, 1)
}
return responseBody.String(), nil
}
// sends a GET request to the provided URL and returns
// the response
func getRequest(url string) (_ string, err error) {
var request *http.Request
if request, err = http.NewRequest(http.MethodGet, url, nil); err != nil {
return "", err
}
var response *http.Response
if response, err = http.DefaultClient.Do(request); err != nil {
return "", cli.NewExitError(err, 1)
}
responseBody := &bytes.Buffer{}
if _, err = io.Copy(responseBody, response.Body); err != nil {
return "", cli.NewExitError(err, 1)
}
return responseBody.String(), nil
}