-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathpaynym_is_api.dart
More file actions
607 lines (572 loc) · 15.2 KB
/
paynym_is_api.dart
File metadata and controls
607 lines (572 loc) · 15.2 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
import 'dart:convert';
import 'package:tuple/tuple.dart';
import '../app_config.dart';
import '../models/paynym/created_paynym.dart';
import '../models/paynym/paynym_account.dart';
import '../models/paynym/paynym_claim.dart';
import '../models/paynym/paynym_follow.dart';
import '../models/paynym/paynym_response.dart';
import '../models/paynym/paynym_unfollow.dart';
import '../networking/http.dart';
import '../services/tor_service.dart';
import 'prefs.dart';
// todo: better error message parsing (from response itself?)
class PaynymIsApi {
static const String baseURL = "https://paynym.rs";
static const String version = "/v1";
HTTP client = HTTP();
Future<Tuple2<Map<String, dynamic>, int>> _post(
String endpoint,
Map<String, dynamic> body, [
Map<String, String> additionalHeaders = const {},
]) async {
final String url =
"$baseURL/api$version${endpoint.startsWith("/") ? endpoint : "/$endpoint"}";
final uri = Uri.parse(url);
// Calculate the body length.
final int contentLength = utf8.encode(jsonEncode(body)).length;
final headers = {
'Content-Type': 'application/json; charset=UTF-8',
'content-length': contentLength
.toString(), // Set the Content-Length header.
}..addAll(additionalHeaders);
final response = await client.post(
url: uri,
headers: headers,
body: jsonEncode(body),
proxyInfo: !AppConfig.hasFeature(AppFeature.tor)
? null
: Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null,
);
// debugPrint("Paynym request uri: $uri");
// debugPrint("Paynym request body: $body");
// debugPrint("Paynym request headers: $headers");
// debugPrint("Paynym response code: ${response.code}");
// debugPrint("Paynym response body: ${response.body}");
Map<String, dynamic> parsedBody;
try {
final bodyStr = response.body.trim();
parsedBody = bodyStr.isEmpty
? {}
: jsonDecode(bodyStr) as Map<String, dynamic>;
} catch (_) {
parsedBody = {};
}
return Tuple2(parsedBody, response.code);
}
// ### `/api/v1/create`
//
// Create a new PayNym entry in the database.
//
//
//
// **Request**
//
// ```json
// POST /api/v1/create
// content-type: application/json
//
// {
// "code":"PM8T..."
// }
//
// ```
//
// | Value | Key |
// | ----- | -------------------- |
// | code | A valid payment code |
//
//
//
// **Response** (201)
//
// ```json
// {
// "claimed": false,
// "nymID": "v9pJm...",
// "nymName": "snowysea",
// "segwit": true,
// "token": "IlBNOF...",
// }
// ```
//
// | Code | Meaning |
// | ---- | --------------------------- |
// | 201 | PayNym created successfully |
// | 200 | PayNym already exists |
// | 400 | Bad request |
//
//
//
// ------
Future<PaynymResponse<CreatedPaynym>> create(String code) async {
final result = await _post("/create", {"code": code});
String message;
CreatedPaynym? value;
switch (result.item2) {
case 201:
message = "PayNym created successfully";
value = CreatedPaynym.fromMap(result.item1);
break;
case 200:
message = "PayNym already exists";
value = CreatedPaynym.fromMap(result.item1);
break;
case 400:
message = "Bad request";
break;
default:
message = result.item1["message"] as String? ?? "Unknown error";
}
return PaynymResponse(value, result.item2, message);
}
// ### `/api/v1/token`
//
// Update the verification token in the database. A token is valid for 24 hours and only for a single authenticated call. The payment code must be in the database or the request will return `404`
//
//
//
// **Request**
//
// ```json
// POST /api/v1/token/
// content-type: application/json
//
// {"code":"PM8T..."}
// ```
//
// | Value | Key |
// | ----- | -------------------- |
// | code | A valid payment code |
//
//
//
// **Response** (200)
//
// ```json
// {
// "token": "DP7S3w..."
// }
// ```
//
// | Code | Meaning |
// | ---- | ------------------------------ |
// | 200 | Token was successfully updated |
// | 404 | Payment code was not found |
// | 400 | Bad request |
//
//
//
// ------
Future<PaynymResponse<String>> token(String code) async {
final result = await _post("/token", {"code": code});
String message;
String? value;
switch (result.item2) {
case 200:
message = "Token was successfully updated";
value = result.item1["token"] as String;
break;
case 404:
message = "Payment code was not found";
break;
case 400:
message = "Bad request";
break;
default:
message = result.item1["message"] as String? ?? "Unknown error";
}
return PaynymResponse(value, result.item2, message);
}
// ### `/api/v1/nym`
//
// Returns all known information about a PayNym account including any other payment codes associated with this Nym.
//
//
//
// **Request**
//
// ```json
// POST /api/v1/nym/
// content-type: application/json
//
// {"nym":"PM8T..."}
// ```
//
// | Value | Key |
// | ----- | ---------------------------------------- |
// | nym | A valid payment `code`, `nymID`, or `nymName` |
//
//
//
// **Response** (200)
//
// ```json
// {
// "codes": [
// {
// "claimed": true,
// "segwit": true,
// "code": "PM8T..."
// }
// ],
// "followers": [
// {
// "nymId": "5iEpU..."
// }
// ],
// "following": [],
// "nymID": "wXGgdC...",
// "nymName": "littlevoice"
// }
// ```
//
// If the `compact=true` parameter is added to the URL, follower and following will not returned. This can achieve faster requests.
//
// | Code | Meaning |
// | ---- | ---------------------- |
// | 200 | Nym found and returned |
// | 404 | Nym not found |
// | 400 | Bad request |
Future<PaynymResponse<PaynymAccount>> nym(
String code, [
bool compact = false,
]) async {
final Map<String, dynamic> requestBody = {"nym": code};
if (compact) {
requestBody["compact"] = true;
}
String message;
PaynymAccount? value;
int statusCode;
try {
final result = await _post("/nym", requestBody);
statusCode = result.item2;
switch (result.item2) {
case 200:
message = "Nym found and returned";
value = PaynymAccount.fromMap(result.item1);
break;
case 404:
message = "Nym not found";
break;
case 400:
message = "Bad request";
break;
default:
message = result.item1["message"] as String? ?? "Unknown error";
}
} catch (e) {
value = null;
message = e.toString();
statusCode = -1;
}
return PaynymResponse(value, statusCode, message);
}
// ## Authenticated Requests
//
//
//
// ### Making authenticated requests
//
// 1. Set an `auth-token` header containing the `token`
// 2. Sign the `token` with the private key of the notification address of the primary payment code
// 3. Add the `signature` to the body of the request.
// 4. A token can only be used once per authenticated request. A new `token` will be returned in the response of a successful authenticated request
//
// ### `/api/v1/claim`
//
// Claim ownership of a payment code added to a newly created PayNym identity.
//
//
//
// **Request**
//
// ```json
// POST /api/v1/claim
// content-type: application/json
// auth-token: IlBNOFRKWmt...
//
//
// {"signature":"..."}
// ```
//
// | Value | Key |
// | --------- | ---------------------------------------- |
// | signature | The `token` signed by the BIP47 notification address |
//
//
//
// **Response** (200)
//
// ```json
// {
// "claimed" : "PM8T...",
// "token" : "IlBNOFRKSmt..."
// }
// ```
//
// | Code | Meaning |
// | ---- | --------------------------------- |
// | 200 | Payment code successfully claimed |
// | 400 | Bad request |
//
// ------
Future<PaynymResponse<PaynymClaim>> claim(
String token,
String signature,
) async {
final result = await _post(
"/claim",
{"signature": signature},
{"auth-token": token},
);
String message;
PaynymClaim? value;
switch (result.item2) {
case 200:
message = "Payment code successfully claimed";
value = PaynymClaim.fromMap(result.item1);
break;
case 400:
message = "Bad request";
break;
default:
message = result.item1["message"] as String? ?? "Unknown error";
}
return PaynymResponse(value, result.item2, message);
}
// ### `/api/v1/follow`
//
// Follow another PayNym account.
//
//
//
// **Request**
//
// ```json
// POST /api/v1/follow/
// content-type: application/json
// auth-token: IlBNOFRKWmt...
//
// {
// "target": "wXGgdC...",
// "signature":"..."
// }
// ```
//
// | Key | Value |
// | --------- | ---------------------------------------- |
// | target | The payment code to follow |
// | signature | The `token` signed by the BIP47 notification address |
//
// **Response** (200)
//
// ```json
// {
// "follower": "5iEpU...",
// "following": "wXGgdC...",
// "token" : "IlBNOFRKSmt..."
// }
// ```
//
// | Code | Meaning |
// | ---- | ---------------------------------------- |
// | 200 | Added to followers |
// | 404 | Payment code not found |
// | 400 | Bad request |
// | 401 | Unauthorized token or signature or Unclaimed payment code |
//
// ------
Future<PaynymResponse<PaynymFollow>> follow(
String token,
String signature,
String target,
) async {
final result = await _post(
"/follow",
{"target": target, "signature": signature},
{"auth-token": token},
);
String message;
PaynymFollow? value;
switch (result.item2) {
case 200:
message = "Added to followers";
value = PaynymFollow.fromMap(result.item1);
break;
case 404:
message = "Payment code not found";
break;
case 400:
message = "Bad request";
break;
case 401:
message = "Unauthorized token or signature or Unclaimed payment code";
break;
default:
message = result.item1["message"] as String? ?? "Unknown error";
}
return PaynymResponse(value, result.item2, message);
}
// ### `/api/v1/unfollow`
//
// Unfollow another PayNym account.
//
//
//
// **Request**
//
// ```json
// POST /api/v1/unfollow/
// content-type: application/json
// auth-token: IlBNOFRKWmt...
//
// {
// "target": "wXGgdC...",
// "signature":"..."
// }
// ```
//
// | Key | Value |
// | --------- | ---------------------------------------- |
// | target | The payment code to unfollow |
// | signature | The `token` signed by the BIP47 notification address |
//
// **Response** (200)
//
// ```json
// {
// "follower": "5iEpU...",
// "unfollowing": "wXGgdC...",
// "token" : "IlBNOFRKSmt..."
// }
// ```
//
// | Code | Meaning |
// | ---- | ---------------------------------------- |
// | 200 | Unfollowed successfully |
// | 404 | Payment code not found |
// | 400 | Bad request |
// | 401 | Unauthorized token or signature or Unclaimed payment code |
//
// ------
Future<PaynymResponse<PaynymUnfollow>> unfollow(
String token,
String signature,
String target,
) async {
final result = await _post(
"/unfollow",
{"target": target, "signature": signature},
{"auth-token": token},
);
String message;
PaynymUnfollow? value;
switch (result.item2) {
case 200:
message = "Unfollowed successfully";
value = PaynymUnfollow.fromMap(result.item1);
break;
case 404:
message = "Payment code not found";
break;
case 400:
message = "Bad request";
break;
case 401:
message = "Unauthorized token or signature or Unclaimed payment code";
break;
default:
message = result.item1["message"] as String? ?? "Unknown error";
}
return PaynymResponse(value, result.item2, message);
}
// ### `/api/v1/nym/add`
//
// Add a new payment code to an existing Nym
//
//
//
// **Request**
//
// ```json
// POST /api/v1/nym/add
// content-type: application/json
// auth-token: IlBNOFRKWmt...
//
// {
// "nym": "wXGgdC...",
// "code":"PM8T...",
// "signature":"..."
// }
// ```
//
// | Key | Value |
// | --------- | ------------------------------------------------------------ |
// | nym | A valid payment `code`, `nymID`, or `nymName` |
// | code | A valid payment code |
// | signature | The `token` signed by the BIP47 notification address of the primary payment code. |
//
// **Response** (200)
//
// ```json
// {
// "code":"PM8T...",
// "segwit": true,
// "token" : "IlBNOFRKSmt..."
// }
// ```
//
// | Code | Meaning |
// | ---- | --------------------------------------------------------- |
// | 200 | Nym updated successfully |
// | 404 | Nym not found |
// | 400 | Bad request |
// | 401 | Unauthorized token or signature or Unclaimed payment code |
//
// ------
Future<PaynymResponse<bool>> add(
String token,
String signature,
String nym,
String code,
) async {
final result = await _post(
"/nym/add",
{"nym": nym, "code": code, "signature": signature},
{"auth-token": token},
);
String message;
bool value = false;
switch (result.item2) {
case 200:
message = "Code added successfully";
value = true;
break;
case 400:
message = "Bad request";
break;
case 401:
message = "Unauthorized token or signature or Unclaimed payment code";
break;
case 404:
message = "Nym not found";
break;
default:
message = result.item1["message"] as String? ?? "Unknown error";
}
return PaynymResponse(value, result.item2, message);
}
}