This repository has been archived by the owner on Oct 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Endorse Badge.txt
57 lines (56 loc) · 5.46 KB
/
Endorse Badge.txt
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
Type: Endorse Badge
Version: 2.0
Description: Endorse a badge.
Template: {"badge":{"type":"str","desc":"ID van de te endorsen badge.","name":"Badge"},"endorsementComment":{"type":"str","desc":"Comment over deze endorsement.","name":"Comment"}}
Init: YXdhaXQgcXVlcnkoIkNSRUFURSIsICJlbmRvcnNlbWVudGJhZGdlcyIsICIoaWQgQklHSU5UIFBSSU1BUlkgS0VZLCBlbnRpdHkgVkFSQ0hBUigzNSkgTk9UIE5VTEwgUkVGRVJFTkNFUyAiICsNCiAgICAiZW50aXRpZXMoZW50aXR5KSwgYmFkZ2UgVkFSQ0hBUigyNTYpIE5PVCBOVUxMLCBqc29uIEpTT04gTk9UIE5VTEwsIGlzc3VlZF9vbiBCSUdJTlQgTk9UIE5VTEwsIFVOSVFVRSAoZW50aXR5LCBiYWRnZSkpOyIsIFtdKTs=
Code: aWYgKHBheWxvYWQuYmFkZ2UubGVuZ3RoID09PSAwIHx8IHBheWxvYWQuYmFkZ2UubGVuZ3RoID4gMjU2KSB7DQogICAgcmV0dXJuICJJbnZhbGlkIGJhZGdlIGlkLiI7DQp9DQovL0NoZWNrIGlmIHRoZSBlbnRpdHkgZXhpc3RzIGFuZCBpcyBub3QgcmV2b2tlZC4NCmNvbnN0IGVudGl0eSA9IChhd2FpdCBxdWVyeSgiU0VMRUNUIiwgImVudGl0aWVzIiwgIldIRVJFIGVudGl0eSA9ICQxOyIsIFtmcm9tXSkpLnJvd3NbMF07DQppZiAoZW50aXR5ID09PSB1bmRlZmluZWQpIHsNCiAgICByZXR1cm4gIkVudGl0eSBkb2VzIG5vdCB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bXBdKTsNCmlmIChjcmVhdGVkLnJvd0NvdW50ID09PSAwKSB7DQogICAgcmV0dXJuICJCYWRnZSBhbHJlYWR5IGVuZG9yc2VkIGJ5IGVudGl0eSwgY2Fubm90IHVwZGF0ZSBlbmRvcnNlbWVudC4iOw0KfQ0KcmV0dXJuICJPSyI7
Init as text:
await query("CREATE", "endorsementbadges", "(id BIGINT PRIMARY KEY, entity VARCHAR(35) NOT NULL REFERENCES " +
"entities(entity), badge VARCHAR(256) NOT NULL, json JSON NOT NULL, issued_on BIGINT NOT NULL, UNIQUE (entity, badge));", []);
Code as text:
if (payload.badge.length === 0 || payload.badge.length > 256) {
return "Invalid badge id.";
}
//Check if the entity exists and is not revoked.
const entity = (await query("SELECT", "entities", "WHERE entity = $1;", [from])).rows[0];
if (entity === undefined) {
return "Entity does not exists or user is not an entity.";
}
if (entity.revoked !== null) {
return "Entity is withdrawn.";
}
//Check if the institution is not withdrawn (we are sure it exists if the entity exists)
const institution = (await query("SELECT", "institutions", "WHERE institution = $1;", [entity.institution])).rows[0];
if (institution.revoked !== null) {
return "Institution of entity is withdrawn.";
}
//The the url on which the endorsement needs to be made available
const url = (await query("SELECT", "settings", "WHERE key = 'url';", [])).rows[0];
if (url === undefined) {
return "URL not set by Surf.";
}
//Assign an unique id to the endorsement (we avoid using SERIAL as it doesn't work in combination with ROLLBACK)
const oldId = (await query("SELECT", "endorsementbadges", "ORDER BY id DESC LIMIT 1;", [])).rows[0];
const newId = oldId === undefined ? 0 : oldId.id + 1;
//Create the endorsement json
const endorsement = {
"@context": "https://w3id.org/openbadges/v2",
"type": "Endorsement",
"id": `${url.value}endorsementbadge?${newId}`,
"issuer": institution.iri,
"claim": {
id: payload.badge,
endorsementComment: payload.endorsementComment
},
"issuedOn": new Date(previousBlockTimestamp).toISOString().split(".")[0] + "Z",
"verification": {
type: "hosted"
}
};
//Create the badge
const created = await query("INSERT", "endorsementbadges", "(id, badge, entity, json, issued_on) VALUES ($1, $2, $3, $4, $5) " +
"ON CONFLICT DO NOTHING;", [newId, payload.badge, from, JSON.stringify(endorsement), previousBlockTimestamp]);
if (created.rowCount === 0) {
return "Badge already endorsed by entity, cannot update endorsement.";
}
return "OK";