-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMatch.java
More file actions
138 lines (133 loc) · 4.65 KB
/
Match.java
File metadata and controls
138 lines (133 loc) · 4.65 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
/*
* Copyright (C) 2014 Tim Bray <tbray@textuality.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.textuality.keybase.lib;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
public class Match {
private final JSONObject mComponents;
public Match(JSONObject json) throws KeybaseException {
try {
mComponents = JWalk.getObject(json, "components");
} catch (JSONException e) {
throw KeybaseException.keybaseScrewup(e);
}
}
public boolean hasKey() throws KeybaseException {
try {
return (JWalk.optObject(mComponents, "key_fingerprint") != null);
} catch (JSONException e) {
throw KeybaseException.keybaseScrewup(e);
}
}
public String getKeyID() throws KeybaseException {
String fingerprint = getFingerprint();
if (fingerprint.length() > 16) {
fingerprint = fingerprint.substring(fingerprint.length() - 16);
}
return fingerprint.replace(" ", "");
}
public String getUsername() throws KeybaseException{
try {
return JWalk.getString(mComponents, "username", "val");
} catch (JSONException e) {
throw KeybaseException.keybaseScrewup(e);
}
}
public String getFullName() throws KeybaseException {
try {
return JWalk.getString(mComponents, "full_name", "val");
} catch (JSONException e) {
return null;
}
}
public String getFingerprint() throws KeybaseException {
try {
return JWalk.getString(mComponents, "key_fingerprint", "val");
} catch (JSONException e) {
throw KeybaseException.keybaseScrewup(e);
}
}
public int getAlgorithmId() throws KeybaseException {
try {
return JWalk.getInt(mComponents, "key_fingerprint", "algo");
} catch (JSONException e) {
throw KeybaseException.keybaseScrewup(e);
}
}
public int getBitStrength() throws KeybaseException {
try {
return JWalk.getInt(mComponents, "key_fingerprint", "nbits");
} catch (JSONException e) {
throw KeybaseException.keybaseScrewup(e);
}
}
public List<String> getProofLabels() {
ArrayList<String> labels = new ArrayList<String>();
try {
labels.add("Twitter: @" + JWalk.getString(mComponents, "twitter", "val"));
} catch (JSONException e) {
// s'OK
}
try {
labels.add("GitHub: " + JWalk.getString(mComponents, "github", "val"));
} catch (JSONException e) {
// s'OK
}
try {
labels.add("Reddit: " + JWalk.getString(mComponents, "reddit", "val"));
} catch (JSONException e) {
// s'OK
}
try {
labels.add("Hacker News: " + JWalk.getString(mComponents, "hackernews", "val"));
} catch (JSONException e) {
// s'OK
}
try {
labels.add("Coinbase: " + JWalk.getString(mComponents, "coinbase", "val"));
} catch (JSONException e) {
// s'OK
}
try {
JSONArray sites = JWalk.getArray(mComponents, "websites");
Hashtable<String, Integer> uniqueNames = new Hashtable<String, Integer>();
int i;
for (i = 0; i < sites.length(); i++) {
uniqueNames.put(JWalk.getString(sites.getJSONObject(i), "val"), 1);
}
Set<String> names = uniqueNames.keySet();
StringBuilder label = new StringBuilder("Web: ");
i = 0;
for (String name : names) {
label.append(name);
if (i < names.size() - 1) {
label.append(", ");
}
i++;
}
labels.add(label.toString());
} catch (JSONException e) {
// s'OK
}
return labels;
}
}