stated-protocol-parser 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,684 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.parseBoycott = exports.buildBoycott = exports.parseObservation = exports.buildObservation = exports.parseBounty = exports.buildBounty = exports.parseRating = exports.buildRating = exports.parsePDFSigning = exports.buildPDFSigningContent = exports.parseResponseContent = exports.buildResponseContent = exports.parseDisputeContent = exports.buildDisputeContentContent = exports.parseDisputeAuthenticity = exports.buildDisputeAuthenticityContent = exports.parseVote = exports.buildVoteContent = exports.parsePersonVerification = exports.buildPersonVerificationContent = exports.parseOrganisationVerification = exports.buildOrganisationVerificationContent = exports.parsePoll = exports.buildPollContent = exports.parseQuotation = exports.buildQuotationContent = exports.parseStatement = exports.buildStatement = void 0;
18
+ /* eslint-disable no-useless-concat */
19
+ const constants_1 = require("./constants");
20
+ const v3_1 = require("./v3");
21
+ const utils_1 = require("./utils");
22
+ const fallBackVersion = 3;
23
+ const version = 4;
24
+ __exportStar(require("./types"), exports);
25
+ __exportStar(require("./constants"), exports);
26
+ __exportStar(require("./utils"), exports);
27
+ __exportStar(require("./v3"), exports);
28
+ const buildStatement = ({ domain, author, time, tags, content, representative, supersededStatement }) => {
29
+ if (content.match(/\nPublishing domain: /))
30
+ throw (new Error("Statement must not contain 'Publishing domain: ', as this marks the beginning of a new statement."));
31
+ if (content.match(/\n\n/))
32
+ throw (new Error("Statement must not contain two line breaks in a row, as this is used for separating statements."));
33
+ if (typeof time !== 'object' || !time.toUTCString)
34
+ throw (new Error("Time must be a Date object."));
35
+ if (!domain)
36
+ throw (new Error("Publishing domain missing."));
37
+ const statement = "Publishing domain: " + domain + "\n" +
38
+ "Author: " + (author || "") + "\n" +
39
+ (representative && representative?.length > 0 ? "Authorized signing representative: " + (representative || "") + "\n" : '') +
40
+ "Time: " + time.toUTCString() + "\n" +
41
+ (tags && tags.length > 0 ? "Tags: " + tags.join(', ') + "\n" : '') +
42
+ (supersededStatement && supersededStatement?.length > 0 ? "Superseded statement: " + (supersededStatement || "") + "\n" : '') +
43
+ "Format version: " + version + "\n" +
44
+ "Statement content: " + content + (content.match(/\n$/) ? '' : "\n");
45
+ if (statement.length > 3000)
46
+ throw (new Error("Statement must not be longer than 3,000 characters."));
47
+ return statement;
48
+ };
49
+ exports.buildStatement = buildStatement;
50
+ const parseStatement = ({ statement: s, allowNoVersion = false }) => {
51
+ if (s.length > 3000)
52
+ throw (new Error("Statement must not be longer than 3,000 characters."));
53
+ if (s.match(/\n\n/))
54
+ throw new Error("Statements cannot contain two line breaks in a row, as this is used for separating statements.");
55
+ const statementRegex = new RegExp(''
56
+ + /^Publishing domain: (?<domain>[^\n]+?)\n/.source
57
+ + /Author: (?<author>[^\n]+?)\n/.source
58
+ + /(?:Authorized signing representative: (?<representative>[^\n]*?)\n)?/.source
59
+ + /Time: (?<time>[^\n]+?)\n/.source
60
+ + /(?:Tags: (?<tags>[^\n]*?)\n)?/.source
61
+ + /(?:Superseded statement: (?<supersededStatement>[^\n]*?)\n)?/.source
62
+ + /(?:Format version: (?<formatVersion>[^\n]*?)\n)?/.source
63
+ + /Statement content: (?:(?<typedContent>\n\tType: (?<type>[^\n]+?)\n[\s\S]+?\n$)|(?<content>[\s\S]+?\n$))/.source);
64
+ const match = s.match(statementRegex);
65
+ if (!match)
66
+ throw new Error("Invalid statement format:" + s);
67
+ const m = {
68
+ domain: match[1], author: match[2], representative: match[3], timeStr: match[4], tagsStr: match[5],
69
+ supersededStatement: match[6], formatVersion: match[7], content: match[8] || match[10],
70
+ type: match[9] ? match[9].toLowerCase().replace(' ', '_') : undefined
71
+ };
72
+ if (!(m['timeStr'].match(constants_1.UTCFormat)))
73
+ throw new Error("Invalid statement format: time must be in UTC");
74
+ if (!m['domain'])
75
+ throw new Error("Invalid statement format: domain is required");
76
+ if (!m['author'])
77
+ throw new Error("Invalid statement format: author is required");
78
+ if (!m['content'])
79
+ throw new Error("Invalid statement format: statement content is required");
80
+ if (!allowNoVersion && !m['formatVersion'])
81
+ throw new Error("Invalid statement format: format version is required");
82
+ const tags = m['tagsStr']?.split(', ');
83
+ const time = new Date(m['timeStr']);
84
+ return {
85
+ domain: m['domain'],
86
+ author: m['author'],
87
+ representative: m['representative'],
88
+ time,
89
+ tags: (tags && tags.length > 0) ? tags : undefined,
90
+ supersededStatement: m['supersededStatement'],
91
+ formatVersion: m['formatVersion'] || ('' + fallBackVersion),
92
+ content: m['content'],
93
+ type: m['type']?.toLowerCase().replace(' ', '_'),
94
+ };
95
+ };
96
+ exports.parseStatement = parseStatement;
97
+ const buildQuotationContent = ({ originalAuthor, authorVerification, originalTime, source, quotation, paraphrasedStatement, picture, confidence }) => {
98
+ if (quotation && quotation.match(/\n/))
99
+ throw (new Error("Quotation must not contain line breaks."));
100
+ if (!paraphrasedStatement && !quotation)
101
+ throw (new Error("Quotation must contain either a quotation or a paraphrased statement."));
102
+ const content = "\n" +
103
+ "\t" + "Type: Quotation" + "\n" +
104
+ "\t" + "Original author: " + originalAuthor + "\n" +
105
+ "\t" + "Author verification: " + authorVerification + "\n" +
106
+ (originalTime && originalTime?.length > 0 ? "\t" + "Original publication time: " + originalTime + "\n" : "") +
107
+ (source && source?.length > 0 ? "\t" + "Source: " + (source || "") + "\n" : '') +
108
+ (picture && picture.length > 0 ? "\t" + "Picture proof: " + (picture || "") + "\n" : '') +
109
+ (confidence && confidence?.length > 0 ? "\t" + "Confidence: " + (confidence || "") + "\n" : '') +
110
+ (quotation && quotation?.length > 0 ? "\t" + "Quotation: " + (quotation || "") + "\n" : '') +
111
+ (paraphrasedStatement && paraphrasedStatement?.length > 0 ? "\t" + "Paraphrased statement: " +
112
+ (paraphrasedStatement || "").replace(/\n\t([^\t])/, '\n\t\t($1)') + "\n" : '') +
113
+ "";
114
+ return content;
115
+ };
116
+ exports.buildQuotationContent = buildQuotationContent;
117
+ const parseQuotation = (s) => {
118
+ const voteRegex = new RegExp(''
119
+ + /^\n\tType: Quotation\n/.source
120
+ + /\tOriginal author: (?<originalAuthor>[^\n]+?)\n/.source
121
+ + /\tAuthor verification: (?<authorVerification>[^\n]+?)\n/.source
122
+ + /(?:\tOriginal publication time: (?<originalTime>[^\n]+?)\n)?/.source
123
+ + /(?:\tSource: (?<source>[^\n]+?)\n)?/.source
124
+ + /(?:\tPicture proof: (?<picture>[^\n]+?)\n)?/.source
125
+ + /(?:\tConfidence: (?<confidence>[^\n]+?)\n)?/.source
126
+ + /(?:\tQuotation: (?<quotation>[^\n]+?)\n)?/.source
127
+ + /(?:\tParaphrased statement: (?:(?<paraphrasedTypedStatement>\n\t\tType: (?<type>[^\n]+?)\n[\s\S]+?)|(?<paraphrasedStatement>[\s\S]+?)))/.source
128
+ + /$/.source);
129
+ let match = s.match(voteRegex);
130
+ if (!match)
131
+ throw new Error("Invalid quotation format: " + s);
132
+ let m = {};
133
+ m = {
134
+ originalAuthor: match[1], authorVerification: match[2], originalTime: match[3], source: match[4],
135
+ picture: match[5], confidence: match[6], quotation: match[7], paraphrasedStatement: match[8] || match[10],
136
+ type: match[9] ? match[9].toLowerCase().replace(' ', '_') : undefined
137
+ };
138
+ return {
139
+ originalAuthor: m['originalAuthor'],
140
+ authorVerification: m['authorVerification'],
141
+ originalTime: m['originalTime'],
142
+ source: m['source'],
143
+ picture: m['picture'],
144
+ confidence: m['confidence'],
145
+ quotation: m['quotation'],
146
+ paraphrasedStatement: (m['paraphrasedStatement']?.replace(/\n\t\t/g, "\n\t")),
147
+ type: m['type']?.toLowerCase().replace(' ', '_'),
148
+ };
149
+ };
150
+ exports.parseQuotation = parseQuotation;
151
+ const buildPollContent = ({ country, city, legalEntity, domainScope, judges, deadline, poll, scopeDescription, scopeQueryLink, options, allowArbitraryVote, requiredProperty: propertyScope, requiredPropertyObserver: propertyScopeObserver }) => {
152
+ if (!poll)
153
+ throw (new Error("Poll must contain a poll question."));
154
+ const scopeContent = (scopeDescription ? "\t\t" + "Description: " + scopeDescription + "\n" : "") +
155
+ (country ? "\t\t" + "Country scope: " + country + "\n" : "") +
156
+ (city ? "\t\t" + "City scope: " + city + "\n" : "") +
157
+ (legalEntity ? "\t\t" + "Legal form scope: " + legalEntity + "\n" : "") +
158
+ (domainScope && domainScope?.length > 0 ? "\t\t" + "Domain scope: " + domainScope.join(', ') + "\n" : "") +
159
+ (propertyScope ? "\t\t" + "All entities with the following property: " + propertyScope + "\n" : "") +
160
+ (propertyScopeObserver ? "\t\t" + "As observed by: " + propertyScopeObserver + "\n" : "") +
161
+ (scopeQueryLink ? "\t\t" + "Link to query defining who can vote: " + scopeQueryLink + "\n" : "");
162
+ if (scopeContent.length > 0 && !scopeDescription)
163
+ throw (new Error("Poll must contain a description of who can vote."));
164
+ const content = "\n" +
165
+ "\t" + "Type: Poll" + "\n" +
166
+ (judges ? "\t" + "The poll outcome is finalized when the following nodes agree: " + judges + "\n" : "") +
167
+ (deadline ? "\t" + "Voting deadline: " + deadline.toUTCString() + "\n" : "") +
168
+ "\t" + "Poll: " + poll + "\n" +
169
+ (options.length > 0 && options[0] ? "\t" + "Option 1: " + options[0] + "\n" : "") +
170
+ (options.length > 1 && options[1] ? "\t" + "Option 2: " + options[1] + "\n" : "") +
171
+ (options.length > 2 && options[2] ? "\t" + "Option 3: " + options[2] + "\n" : "") +
172
+ (options.length > 3 && options[3] ? "\t" + "Option 4: " + options[3] + "\n" : "") +
173
+ (options.length > 4 && options[4] ? "\t" + "Option 5: " + options[4] + "\n" : "") +
174
+ ((allowArbitraryVote === true || allowArbitraryVote === false) ? ("\t" + "Allow free text votes: " + (allowArbitraryVote ? 'Yes' : 'No') + "\n") : "") +
175
+ (scopeContent ? "\t" + "Who can vote: \n" + scopeContent : "") +
176
+ "";
177
+ return content;
178
+ };
179
+ exports.buildPollContent = buildPollContent;
180
+ const parsePoll = (s, version) => {
181
+ if (version && version === '3')
182
+ return (0, v3_1.parsePollV3)(s);
183
+ if (version && version !== '4')
184
+ throw new Error("Invalid version " + version);
185
+ const pollRegex = new RegExp(''
186
+ + /^\n\tType: Poll\n/.source
187
+ + /(?:\tThe poll outcome is finalized when the following nodes agree: (?<judges>[^\n]+?)\n)?/.source
188
+ + /(?:\tVoting deadline: (?<deadline>[^\n]+?)\n)?/.source
189
+ + /\tPoll: (?<poll>[^\n]+?)\n/.source
190
+ + /(?:\tOption 1: (?<option1>[^\n]+?)\n)?/.source
191
+ + /(?:\tOption 2: (?<option2>[^\n]+?)\n)?/.source
192
+ + /(?:\tOption 3: (?<option3>[^\n]+?)\n)?/.source
193
+ + /(?:\tOption 4: (?<option4>[^\n]+?)\n)?/.source
194
+ + /(?:\tOption 5: (?<option5>[^\n]+?)\n)?/.source
195
+ + /(?:\tAllow free text votes: (?<allowArbitraryVote>Yes|No)\n)?/.source
196
+ + /(?:\tWho can vote: (?<whoCanVote>\n[\s\S]+?\n))?/.source
197
+ + /$/.source);
198
+ let m = s.match(pollRegex);
199
+ if (!m)
200
+ throw new Error("Invalid poll format: " + s);
201
+ m = {
202
+ judges: m[1], deadline: m[2], poll: m[3],
203
+ option1: m[4], option2: m[5], option3: m[6], option4: m[7], option5: m[8],
204
+ allowArbitraryVote: m[9],
205
+ whoCanVote: m[10]
206
+ };
207
+ const whoCanVoteParsed = {};
208
+ if (m.whoCanVote) {
209
+ const whoCanVoteRegex = new RegExp(''
210
+ + /^\n\t\tDescription: (?<scopeDescription>[^\n]+?)\n/.source
211
+ + /(?:\t\tCountry scope: (?<countryScope>[^\n]+?)\n)?/.source
212
+ + /(?:\t\tCity scope: (?<cityScope>[^\n]+?)\n)?/.source
213
+ + /(?:\t\tLegal form scope: (?<legalEntity>[^\n]+?)\n)?/.source
214
+ + /(?:\t\tDomain scope: (?<domainScope>[^\n]+?)\n)?/.source
215
+ + /(?:\t\tAll entities with the following property: (?<propertyScope>[^\n]+?)\n)?/.source
216
+ + /(?:\t\tAs observed by: (?<propertyScopeObserver>[^\n]+?)\n)?/.source
217
+ + /(?:\t\tLink to query defining who can vote: (?<scopeQueryLink>[^\n]+?)\n)?/.source
218
+ + /$/.source);
219
+ let m2 = m.whoCanVote.match(whoCanVoteRegex);
220
+ if (!m2)
221
+ throw new Error("Invalid who can vote section: " + m.whoCanVote);
222
+ whoCanVoteParsed['scopeDescription'] = m2[1];
223
+ whoCanVoteParsed['country'] = m2[2];
224
+ whoCanVoteParsed['city'] = m2[3];
225
+ whoCanVoteParsed['legalEntity'] = m2[4];
226
+ whoCanVoteParsed['domainScopeStr'] = m2[5];
227
+ whoCanVoteParsed['requiredProperty'] = m2[6];
228
+ whoCanVoteParsed['requiredPropertyObserver'] = m2[7];
229
+ whoCanVoteParsed['scopeQueryLink'] = m2[8];
230
+ }
231
+ const options = [m.option1, m.option2, m.option3, m.option4, m.option5].filter(o => o);
232
+ const domainScope = whoCanVoteParsed.domainScopeStr?.split(', ');
233
+ const allowArbitraryVote = (m['allowArbitraryVote'] === 'Yes' ? true :
234
+ (m['allowArbitraryVote'] === 'No' ? false : undefined));
235
+ const deadlineStr = m.deadline;
236
+ if (deadlineStr && !deadlineStr.match(constants_1.UTCFormat))
237
+ throw new Error("Invalid poll, deadline must be in UTC: " + deadlineStr);
238
+ return {
239
+ judges: m['judges'],
240
+ deadline: deadlineStr ? new Date(deadlineStr) : undefined,
241
+ poll: m['poll'],
242
+ options,
243
+ allowArbitraryVote,
244
+ country: whoCanVoteParsed['country'],
245
+ scopeDescription: whoCanVoteParsed['scopeDescription'],
246
+ requiredProperty: whoCanVoteParsed['requiredProperty'],
247
+ requiredPropertyObserver: whoCanVoteParsed['requiredPropertyObserver'],
248
+ scopeQueryLink: whoCanVoteParsed['scopeQueryLink'],
249
+ city: whoCanVoteParsed['city'],
250
+ legalEntity: whoCanVoteParsed['legalEntity'],
251
+ domainScope: (domainScope && domainScope.length > 0) ? domainScope : undefined,
252
+ };
253
+ };
254
+ exports.parsePoll = parsePoll;
255
+ const buildOrganisationVerificationContent = ({ name, englishName, country, city, province, legalForm, department, domain, foreignDomain, serialNumber, confidence, reliabilityPolicy, employeeCount, pictureHash, latitude, longitude, population }) => {
256
+ if (!name || !country || !legalForm || (!domain && !foreignDomain))
257
+ throw new Error("Missing required fields");
258
+ if (!Object.values(constants_1.legalForms).includes(legalForm))
259
+ throw new Error("Invalid legal form " + legalForm);
260
+ if (employeeCount && !Object.values(constants_1.peopleCountBuckets).includes(employeeCount))
261
+ throw new Error("Invalid employee count " + employeeCount);
262
+ if (population && !Object.values(constants_1.peopleCountBuckets).includes(population))
263
+ throw new Error("Invalid population " + population);
264
+ if (confidence && !('' + confidence)?.match(/^[0-9.]+$/))
265
+ throw new Error("Invalid confidence " + confidence);
266
+ return "\n" +
267
+ "\t" + "Type: Organisation verification" + "\n" +
268
+ "\t" + "Description: We verified the following information about an organisation." + "\n" +
269
+ "\t" + "Name: " + name + "\n" +
270
+ (englishName ? "\t" + "English name: " + englishName + "\n" : "") +
271
+ "\t" + "Country: " + country + "\n" +
272
+ "\t" + "Legal form: " + legalForm + "\n" +
273
+ (domain ? "\t" + "Owner of the domain: " + domain + "\n" : "") +
274
+ (foreignDomain ? "\t" + "Foreign domain used for publishing statements: " + foreignDomain + "\n" : "") +
275
+ (department ? "\t" + "Department using the domain: " + department + "\n" : "") +
276
+ (province ? "\t" + "Province or state: " + province + "\n" : "") +
277
+ (serialNumber ? "\t" + "Business register number: " + serialNumber + "\n" : "") +
278
+ (city ? "\t" + "City: " + city + "\n" : "") +
279
+ (latitude ? "\t" + "Latitude: " + latitude + "\n" : "") +
280
+ (longitude ? "\t" + "Longitude: " + longitude + "\n" : "") +
281
+ (population ? "\t" + "Population: " + population + "\n" : "") +
282
+ (pictureHash ? "\t" + "Logo: " + pictureHash + "\n" : "") +
283
+ (employeeCount ? "\t" + "Employee count: " + employeeCount + "\n" : "") +
284
+ (reliabilityPolicy ? "\t" + "Reliability policy: " + reliabilityPolicy + "\n" : "") +
285
+ (confidence ? "\t" + "Confidence: " + confidence + "\n" : "") +
286
+ "";
287
+ };
288
+ exports.buildOrganisationVerificationContent = buildOrganisationVerificationContent;
289
+ const parseOrganisationVerification = (s) => {
290
+ const organisationVerificationRegex = new RegExp(''
291
+ + /^\n\tType: Organisation verification\n/.source
292
+ + /\tDescription: We verified the following information about an organisation.\n/.source
293
+ + /\tName: (?<name>[^\n]+?)\n/.source
294
+ + /(?:\tEnglish name: (?<englishName>[^\n]+?)\n)?/.source
295
+ + /\tCountry: (?<country>[^\n]+?)\n/.source
296
+ + /\tLegal (?:form|entity): (?<legalForm>[^\n]+?)\n/.source
297
+ + /(?:\tOwner of the domain: (?<domain>[^\n]+?)\n)?/.source
298
+ + /(?:\tForeign domain used for publishing statements: (?<foreignDomain>[^\n]+?)\n)?/.source
299
+ + /(?:\tDepartment using the domain: (?<department>[^\n]+?)\n)?/.source
300
+ + /(?:\tProvince or state: (?<province>[^\n]+?)\n)?/.source
301
+ + /(?:\tBusiness register number: (?<serialNumber>[^\n]+?)\n)?/.source
302
+ + /(?:\tCity: (?<city>[^\n]+?)\n)?/.source
303
+ + /(?:\tLatitude: (?<latitude>[^\n]+?)\n)?/.source
304
+ + /(?:\tLongitude: (?<longitude>[^\n]+?)\n)?/.source
305
+ + /(?:\tPopulation: (?<population>[^\n]+?)\n)?/.source
306
+ + /(?:\tLogo: (?<pictureHash>[^\n]+?)\n)?/.source
307
+ + /(?:\tEmployee count: (?<employeeCount>[01,+-]+?)\n)?/.source
308
+ + /(?:\tReliability policy: (?<reliabilityPolicy>[^\n]+?)\n)?/.source
309
+ + /(?:\tConfidence: (?<confidence>[0-9.]+?)\n)?/.source
310
+ + /$/.source);
311
+ const m = s.match(organisationVerificationRegex);
312
+ if (!m)
313
+ throw new Error("Invalid organisation verification format: " + s);
314
+ return {
315
+ name: m[1],
316
+ englishName: m[2],
317
+ country: m[3],
318
+ legalForm: m[4],
319
+ domain: m[5],
320
+ foreignDomain: m[6],
321
+ department: m[7],
322
+ province: m[8],
323
+ serialNumber: m[9],
324
+ city: m[10],
325
+ latitude: m[11] ? parseFloat(m[11]) : undefined,
326
+ longitude: m[12] ? parseFloat(m[12]) : undefined,
327
+ population: m[13],
328
+ pictureHash: m[14],
329
+ employeeCount: m[15],
330
+ reliabilityPolicy: m[16],
331
+ confidence: m[17] ? parseFloat(m[17]) : undefined,
332
+ };
333
+ };
334
+ exports.parseOrganisationVerification = parseOrganisationVerification;
335
+ const buildPersonVerificationContent = ({ name, countryOfBirth, cityOfBirth, ownDomain, foreignDomain, dateOfBirth, jobTitle, employer, verificationMethod, confidence, picture, reliabilityPolicy }) => {
336
+ if (!name || !countryOfBirth || !cityOfBirth || !dateOfBirth || (!ownDomain && !foreignDomain)) {
337
+ console.log("Missing required fields: ", { name, countryOfBirth, cityOfBirth, dateOfBirth, ownDomain, foreignDomain });
338
+ return "";
339
+ }
340
+ const [day, month, year] = dateOfBirth.toUTCString().split(' ').filter((i, j) => [1, 2, 3].includes(j));
341
+ let content = "\n" +
342
+ "\t" + "Type: Person verification" + "\n" +
343
+ "\t" + "Description: We verified the following information about a person." + "\n" +
344
+ "\t" + "Name: " + name + "\n" +
345
+ "\t" + "Date of birth: " + [day.replace(/$0/, ''), month, year].join(' ') + "\n" +
346
+ "\t" + "City of birth: " + cityOfBirth + "\n" +
347
+ "\t" + "Country of birth: " + countryOfBirth + "\n" +
348
+ (jobTitle ? "\t" + "Job title: " + jobTitle + "\n" : "") +
349
+ (employer ? "\t" + "Employer: " + employer + "\n" : "") +
350
+ (ownDomain ? "\t" + "Owner of the domain: " + ownDomain + "\n" : "") +
351
+ (foreignDomain ? "\t" + "Foreign domain used for publishing statements: " + foreignDomain + "\n" : "") +
352
+ (picture ? "\t" + "Picture: " + picture + "\n" : "") +
353
+ (verificationMethod ? "\t" + "Verification method: " + verificationMethod + "\n" : "") +
354
+ (confidence ? "\t" + "Confidence: " + confidence + "\n" : "") +
355
+ (reliabilityPolicy ? "\t" + "Reliability policy: " + reliabilityPolicy + "\n" : "") +
356
+ "";
357
+ return content;
358
+ };
359
+ exports.buildPersonVerificationContent = buildPersonVerificationContent;
360
+ const parsePersonVerification = (s) => {
361
+ const domainVerificationRegex = new RegExp(''
362
+ + /^\n\tType: Person verification\n/.source
363
+ + /\tDescription: We verified the following information about a person.\n/.source
364
+ + /\tName: (?<name>[^\n]+?)\n/.source
365
+ + /\tDate of birth: (?<dateOfBirth>[^\n]+?)\n/.source
366
+ + /\tCity of birth: (?<cityOfBirth>[^\n]+?)\n/.source
367
+ + /\tCountry of birth: (?<countryOfBirth>[^\n]+?)\n/.source
368
+ + /(?:\tJob title: (?<jobTitle>[^\n]+?)\n)?/.source
369
+ + /(?:\tEmployer: (?<employer>[^\n]+?)\n)?/.source
370
+ + /(?:\tOwner of the domain: (?<domain>[^\n]+?)\n)?/.source
371
+ + /(?:\tForeign domain used for publishing statements: (?<foreignDomain>[^\n]+?)\n)?/.source
372
+ + /(?:\tPicture: (?<picture>[^\n]+?)\n)?/.source
373
+ + /(?:\tVerification method: (?<verificationMethod>[^\n]+?)\n)?/.source
374
+ + /(?:\tConfidence: (?<confidence>[^\n]+?)\n)?/.source
375
+ + /(?:\tReliability policy: (?<reliabilityPolicy>[^\n]+?)\n)?/.source
376
+ + /$/.source);
377
+ const m = s.match(domainVerificationRegex);
378
+ if (!m)
379
+ throw new Error("Invalid person verification format: " + s);
380
+ if (m[2] && !m[2].match(utils_1.birthDateFormat))
381
+ throw new Error("Invalid birth date format: " + m[2]);
382
+ let { d, month, y } = m[2].match(utils_1.birthDateFormat)?.groups || {};
383
+ if (!d || !month || !y)
384
+ throw new Error("Invalid birth date format: " + m[2]);
385
+ return {
386
+ name: m[1],
387
+ dateOfBirth: new Date(Date.UTC(parseInt(y), (0, utils_1.monthIndex)(month), parseInt(d))),
388
+ cityOfBirth: m[3],
389
+ countryOfBirth: m[4],
390
+ jobTitle: m[5],
391
+ employer: m[6],
392
+ ownDomain: m[7],
393
+ foreignDomain: m[8],
394
+ picture: m[9],
395
+ verificationMethod: m[10],
396
+ confidence: m[11] ? parseFloat(m[11]) : undefined,
397
+ reliabilityPolicy: m[12]
398
+ };
399
+ };
400
+ exports.parsePersonVerification = parsePersonVerification;
401
+ const buildVoteContent = ({ pollHash, poll, vote }) => {
402
+ const content = "\n" +
403
+ "\t" + "Type: Vote" + "\n" +
404
+ "\t" + "Poll id: " + pollHash + "\n" +
405
+ "\t" + "Poll: " + poll + "\n" +
406
+ "\t" + "Option: " + vote + "\n" +
407
+ "";
408
+ return content;
409
+ };
410
+ exports.buildVoteContent = buildVoteContent;
411
+ const parseVote = (s) => {
412
+ const voteRegex = new RegExp(''
413
+ + /^\n\tType: Vote\n/.source
414
+ + /\tPoll id: (?<pollHash>[^\n]+?)\n/.source
415
+ + /\tPoll: (?<poll>[^\n]+?)\n/.source
416
+ + /\tOption: (?<vote>[^\n]+?)\n/.source
417
+ + /$/.source);
418
+ const m = s.match(voteRegex);
419
+ if (!m)
420
+ throw new Error("Invalid vote format: " + s);
421
+ return {
422
+ pollHash: m[1],
423
+ poll: m[2],
424
+ vote: m[3]
425
+ };
426
+ };
427
+ exports.parseVote = parseVote;
428
+ const buildDisputeAuthenticityContent = ({ hash, confidence, reliabilityPolicy }) => {
429
+ const content = "\n" +
430
+ "\t" + "Type: Dispute statement authenticity" + "\n" +
431
+ "\t" + "Description: We think that the referenced statement is not authentic.\n" +
432
+ "\t" + "Hash of referenced statement: " + hash + "\n" +
433
+ (confidence ? "\t" + "Confidence: " + confidence + "\n" : "") +
434
+ (reliabilityPolicy ? "\t" + "Reliability policy: " + reliabilityPolicy + "\n" : "") +
435
+ "";
436
+ return content;
437
+ };
438
+ exports.buildDisputeAuthenticityContent = buildDisputeAuthenticityContent;
439
+ const parseDisputeAuthenticity = (s) => {
440
+ const disputeRegex = new RegExp(''
441
+ + /^\n\tType: Dispute statement authenticity\n/.source
442
+ + /\tDescription: We think that the referenced statement is not authentic.\n/.source
443
+ + /\tHash of referenced statement: (?<hash>[^\n]+?)\n/.source
444
+ + /(?:\tConfidence: (?<confidence>[^\n]*?)\n)?/.source
445
+ + /(?:\tReliability policy: (?<reliabilityPolicy>[^\n]+?)\n)?/.source
446
+ + /$/.source);
447
+ const m = s.match(disputeRegex);
448
+ if (!m)
449
+ throw new Error("Invalid dispute authenticity format: " + s);
450
+ return {
451
+ hash: m[1],
452
+ confidence: m[2] ? parseFloat(m[2]) : undefined,
453
+ reliabilityPolicy: m[3]
454
+ };
455
+ };
456
+ exports.parseDisputeAuthenticity = parseDisputeAuthenticity;
457
+ const buildDisputeContentContent = ({ hash, confidence, reliabilityPolicy }) => {
458
+ const content = "\n" +
459
+ "\t" + "Type: Dispute statement content" + "\n" +
460
+ "\t" + "Description: We think that the content of the referenced statement is false.\n" +
461
+ "\t" + "Hash of referenced statement: " + hash + "\n" +
462
+ (confidence ? "\t" + "Confidence: " + confidence + "\n" : "") +
463
+ (reliabilityPolicy ? "\t" + "Reliability policy: " + reliabilityPolicy + "\n" : "") +
464
+ "";
465
+ return content;
466
+ };
467
+ exports.buildDisputeContentContent = buildDisputeContentContent;
468
+ const parseDisputeContent = (s) => {
469
+ const disputeRegex = new RegExp(''
470
+ + /^\n\tType: Dispute statement content\n/.source
471
+ + /\tDescription: We think that the content of the referenced statement is false.\n/.source
472
+ + /\tHash of referenced statement: (?<hash>[^\n]+?)\n/.source
473
+ + /(?:\tConfidence: (?<confidence>[^\n]*?)\n)?/.source
474
+ + /(?:\tReliability policy: (?<reliabilityPolicy>[^\n]+?)\n)?/.source
475
+ + /$/.source);
476
+ const m = s.match(disputeRegex);
477
+ if (!m)
478
+ throw new Error("Invalid dispute content format: " + s);
479
+ return {
480
+ hash: m[1],
481
+ confidence: m[2] ? parseFloat(m[2]) : undefined,
482
+ reliabilityPolicy: m[3]
483
+ };
484
+ };
485
+ exports.parseDisputeContent = parseDisputeContent;
486
+ const buildResponseContent = ({ hash, response }) => {
487
+ const content = "\n" +
488
+ "\t" + "Type: Response" + "\n" +
489
+ "\t" + "Hash of referenced statement: " + hash + "\n" +
490
+ "\t" + "Response: " + response + "\n" +
491
+ "";
492
+ return content;
493
+ };
494
+ exports.buildResponseContent = buildResponseContent;
495
+ const parseResponseContent = (s) => {
496
+ const disputeRegex = new RegExp(''
497
+ + /^\n\tType: Response\n/.source
498
+ + /\tHash of referenced statement: (?<hash>[^\n]+?)\n/.source
499
+ + /\tResponse: (?<response>[^\n]*?)\n/.source
500
+ + /$/.source);
501
+ const m = s.match(disputeRegex);
502
+ if (!m)
503
+ throw new Error("Invalid response content format: " + s);
504
+ return {
505
+ hash: m[1],
506
+ response: m[2]
507
+ };
508
+ };
509
+ exports.parseResponseContent = parseResponseContent;
510
+ const buildPDFSigningContent = ({ hash }) => {
511
+ const content = "\n" +
512
+ "\t" + "Type: Sign PDF" + "\n" +
513
+ "\t" + "Description: We hereby digitally sign the referenced PDF file.\n" +
514
+ "\t" + "PDF file hash: " + hash + "\n" +
515
+ "";
516
+ return content;
517
+ };
518
+ exports.buildPDFSigningContent = buildPDFSigningContent;
519
+ const parsePDFSigning = (s) => {
520
+ const signingRegex = new RegExp(''
521
+ + /^\n\tType: Sign PDF\n/.source
522
+ + /\tDescription: We hereby digitally sign the referenced PDF file.\n/.source
523
+ + /\tPDF file hash: (?<hash>[^\n]+?)\n/.source
524
+ + /$/.source);
525
+ const m = s.match(signingRegex);
526
+ if (!m)
527
+ throw new Error("Invalid PDF signing format: " + s);
528
+ return {
529
+ hash: m[1]
530
+ };
531
+ };
532
+ exports.parsePDFSigning = parsePDFSigning;
533
+ const buildRating = ({ subjectName, subjectType, subjectReference, documentFileHash, rating, quality, comment }) => {
534
+ if (![1, 2, 3, 4, 5].includes(rating))
535
+ throw new Error("Invalid rating: " + rating);
536
+ const content = "\n" +
537
+ "\t" + "Type: Rating" + "\n" +
538
+ (subjectType ? "\t" + "Subject type: " + subjectType + "\n" : "") +
539
+ "\t" + "Subject name: " + subjectName + "\n" +
540
+ (subjectReference ? "\t" + "URL that identifies the subject: " + subjectReference + "\n" : "") +
541
+ (documentFileHash ? "\t" + "Document file hash: " + documentFileHash + "\n" : "") +
542
+ (quality ? "\t" + "Rated quality: " + quality + "\n" : "") +
543
+ "\t" + "Our rating: " + rating + "/5 Stars\n" +
544
+ (comment ? "\t" + "Comment: " + comment + "\n" : "") +
545
+ "";
546
+ return content;
547
+ };
548
+ exports.buildRating = buildRating;
549
+ const parseRating = (s) => {
550
+ const ratingRegex = new RegExp(''
551
+ + /^\n\tType: Rating\n/.source
552
+ + /(?:\tSubject type: (?<subjectType>[^\n]*?)\n)?/.source
553
+ + /\tSubject name: (?<subjectName>[^\n]*?)\n/.source
554
+ + /(?:\tURL that identifies the subject: (?<subjectReference>[^\n]*?)\n)?/.source
555
+ + /(?:\tDocument file hash: (?<documentFileHash>[^\n]*?)\n)?/.source
556
+ + /(?:\tRated quality: (?<quality>[^\n]*?)\n)?/.source
557
+ + /\tOur rating: (?<rating>[1-5])\/5 Stars\n/.source
558
+ + /(?:\tComment: (?<comment>[\s\S]+?)\n)?/.source
559
+ + /$/.source);
560
+ const m = s.match(ratingRegex);
561
+ if (!m)
562
+ throw new Error("Invalid rating format: " + s);
563
+ const rating = parseInt(m[6]);
564
+ if (![1, 2, 3, 4, 5].includes(rating))
565
+ throw new Error("Invalid rating: " + m[6]);
566
+ if (m[1] && !['Organisation', 'Policy proposal', 'Regulation',
567
+ 'Treaty draft', 'Product', 'Research publication'].includes(m[1]))
568
+ throw new Error("Invalid subject type: " + m[1]);
569
+ if (!m[2])
570
+ throw new Error("Missing subject name");
571
+ return {
572
+ subjectType: m[1],
573
+ subjectName: m[2],
574
+ subjectReference: m[3],
575
+ documentFileHash: m[4],
576
+ quality: m[5],
577
+ rating,
578
+ comment: m[7]
579
+ };
580
+ };
581
+ exports.parseRating = parseRating;
582
+ const buildBounty = ({ motivation, bounty, reward, judge, judgePay }) => {
583
+ const content = "\n" +
584
+ "\t" + "Type: Bounty" + "\n" +
585
+ (motivation ? "\t" + "In order to: " + motivation + "\n" : "") +
586
+ "\t" + "We will reward any entity that: " + bounty + "\n" +
587
+ "\t" + "The reward is: " + reward + "\n" +
588
+ "\t" + "In case of dispute, bounty claims are judged by: " + judge + "\n" +
589
+ (judgePay ? "\t" + "The judge will be paid per investigated case with a maxium of: " + judgePay + "\n" : "") +
590
+ "";
591
+ return content;
592
+ };
593
+ exports.buildBounty = buildBounty;
594
+ const parseBounty = (s) => {
595
+ const bountyRegex = new RegExp(''
596
+ + /^\n\tType: Bounty\n/.source
597
+ + /(?:\tIn order to: (?<motivation>[^\n]*?)\n)?/.source
598
+ + /\tWe will reward any entity that: (?<bounty>[^\n]*?)\n/.source
599
+ + /\tThe reward is: (?<reward>[^\n]*?)\n/.source
600
+ + /\tIn case of dispute, bounty claims are judged by: (?<judge>[^\n]*?)\n/.source
601
+ + /(?:\tThe judge will be paid per investigated case with a maxium of: (?<judgePay>[^\n]*?)\n)?/.source
602
+ + /$/.source);
603
+ const m = s.match(bountyRegex);
604
+ if (!m)
605
+ throw new Error("Invalid bounty format: " + s);
606
+ return {
607
+ motivation: m[1],
608
+ bounty: m[2],
609
+ reward: m[3],
610
+ judge: m[4],
611
+ judgePay: m[5]
612
+ };
613
+ };
614
+ exports.parseBounty = parseBounty;
615
+ const buildObservation = ({ approach, confidence, reliabilityPolicy, subject, subjectReference, observationReference, property, value }) => {
616
+ const content = "\n" +
617
+ "\t" + "Type: Observation" + "\n" +
618
+ (approach ? "\t" + "Approach: " + approach + "\n" : "") +
619
+ (confidence ? "\t" + "Confidence: " + confidence + "\n" : "") +
620
+ (reliabilityPolicy ? "\t" + "Reliability policy: " + reliabilityPolicy + "\n" : "") +
621
+ "\t" + "Subject: " + subject + "\n" +
622
+ (subjectReference ? "\t" + "Subject identity reference: " + subjectReference + "\n" : "") +
623
+ (observationReference ? "\t" + "Observation reference: " + observationReference + "\n" : "") +
624
+ "\t" + "Observed property: " + property + "\n" +
625
+ (value ? "\t" + "Observed value: " + value + "\n" : "") +
626
+ "";
627
+ return content;
628
+ };
629
+ exports.buildObservation = buildObservation;
630
+ const parseObservation = (s) => {
631
+ const observationRegex = new RegExp(''
632
+ + /^\n\tType: Observation\n/.source
633
+ + /(?:\tApproach: (?<approach>[^\n]*?)\n)?/.source
634
+ + /(?:\tConfidence: (?<confidence>[^\n]*?)\n)?/.source
635
+ + /(?:\tReliability policy: (?<reliabilityPolicy>[^\n]+?)\n)?/.source
636
+ + /\tSubject: (?<subject>[^\n]*?)\n/.source
637
+ + /(?:\tSubject identity reference: (?<subjectReference>[^\n]*?)\n)?/.source
638
+ + /(?:\tObservation reference: (?<observationReference>[^\n]*?)\n)?/.source
639
+ + /\tObserved property: (?<property>[^\n]*?)\n/.source
640
+ + /(?:\tObserved value: (?<value>[\s\S]+?)\n)?/.source
641
+ + /$/.source);
642
+ const m = s.match(observationRegex);
643
+ if (!m)
644
+ throw new Error("Invalid observation format: " + s);
645
+ return {
646
+ approach: m[1],
647
+ confidence: m[2] ? parseFloat(m[2]) : undefined,
648
+ reliabilityPolicy: m[3],
649
+ subject: m[4],
650
+ subjectReference: m[5],
651
+ observationReference: m[6],
652
+ property: m[7],
653
+ value: m[8]
654
+ };
655
+ };
656
+ exports.parseObservation = parseObservation;
657
+ const buildBoycott = ({ description, subject, subjectReference }) => {
658
+ const content = "\n" +
659
+ "\t" + "Type: Boycott" + "\n" +
660
+ (description ? "\t" + "Description: " + description + "\n" : "") +
661
+ "\t" + "Subject: " + subject + "\n" +
662
+ (subjectReference ? "\t" + "Subject identity reference: " + subjectReference + "\n" : "") +
663
+ "";
664
+ return content;
665
+ };
666
+ exports.buildBoycott = buildBoycott;
667
+ const parseBoycott = (s) => {
668
+ const observationRegex = new RegExp(''
669
+ + /^\n\tType: Boycott\n/.source
670
+ + /(?:\tDescription: (?<description>[^\n]*?)\n)?/.source
671
+ + /\tSubject: (?<subject>[^\n]*?)\n/.source
672
+ + /(?:\tSubject identity reference: (?<subjectReference>[^\n]*?)\n)?/.source
673
+ + /$/.source);
674
+ const m = s.match(observationRegex);
675
+ if (!m)
676
+ throw new Error("Invalid observation format: " + s);
677
+ return {
678
+ description: m[1],
679
+ subject: m[2],
680
+ subjectReference: m[3],
681
+ };
682
+ };
683
+ exports.parseBoycott = parseBoycott;
684
+ //# sourceMappingURL=protocol.js.map