Newer
Older
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
import 'dart:convert';
import 'dart:math';
import 'dart:typed_data';
import 'package:durt/durt.dart';
Random createRandom() {
try {
return Random.secure();
} catch (e) {
return Random();
}
}
Uint8List generateUintSeed() {
final Random random = createRandom();
return Uint8List.fromList(List<int>.generate(32, (_) => random.nextInt(256)));
}
String seedToString(Uint8List seed) {
final Uint8List seedsBytes = Uint8List.fromList(seed);
final String encoded = json.encode(seedsBytes.toList());
return encoded;
}
CesiumWallet generateCesiumWallet(Uint8List seed) {
return CesiumWallet.fromSeed(seed);
}
Uint8List seedFromString(String sString) {
final List<dynamic> list = json.decode(sString) as List<dynamic>;
final Uint8List bytes =
Uint8List.fromList(list.map((dynamic e) => e as int).toList());
return bytes;
}
String generateSalt(int length) {
final Random random = createRandom();
const String charset =
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
return List<String>.generate(
length, (int index) => charset[random.nextInt(charset.length)]).join();
}
String parseHost(String endpointUnParsed) {
final List<String> parts = endpointUnParsed.split(' ');
// FIXME (vjrj): figure out if exists a way to detect http or https
const String protocol = 'https';
final String lastPart = parts.removeLast();
String path =
RegExp(r'^\/[a-zA-Z0-9\-\/]+$').hasMatch(lastPart) ? lastPart : '';
final String nextToLast = parts[parts.length - 1];
final String port = lastPart == ''
? (RegExp(r'^\/[0-9]$').hasMatch(lastPart) ? lastPart : '443')
: RegExp(r'^\/[0-9]$').hasMatch(nextToLast)
? nextToLast
: '443';
final List<String> hostSplited = parts[1].split('/');
// Process hosts like monnaie-libre.ortie.org/bma/
final String host = hostSplited[0];
path = path.isEmpty
? ((hostSplited.length > 1 && hostSplited[1].isNotEmpty
? hostSplited[1]
: '')
.isNotEmpty
? hostSplited.length > 1 && hostSplited[1].isNotEmpty
? '/${hostSplited[1]}'
: ''
: path)
: path;
//final bool hasIp =
// RegExp(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}').hasMatch(parts[2]);
// final String port = hasIp ? parts[3] : parts[2];
//final int pathIndex = hasIp ? 4 : 3;
//final String path = parts.length > pathIndex ? parts[pathIndex] : '';
final String endpoint = '$protocol://$host:$port$path'.trim();
return endpoint;
}