-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclock-updater.mjs
More file actions
58 lines (48 loc) · 1.64 KB
/
clock-updater.mjs
File metadata and controls
58 lines (48 loc) · 1.64 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
/**
* Clock Updater - Updates the Solid clock every second using Nostr auth
* Usage: node clock-updater.mjs
*/
import { getPublicKey, finalizeEvent } from 'nostr-tools';
import { getToken } from 'nostr-tools/nip98';
// Nostr keypair (in production, load from env/file)
const SK_HEX = '3f188544fb81bd324ead7be9697fd9503d18345e233a7b0182915b0b582ddd70';
const sk = Uint8Array.from(Buffer.from(SK_HEX, 'hex'));
const pk = getPublicKey(sk);
const CLOCK_URL = 'https://solid.social/melvin/public/clock.json';
async function updateClock() {
const now = Math.floor(Date.now() / 1000);
const isoDate = new Date(now * 1000).toISOString();
const clockData = {
'@context': { 'schema': 'http://schema.org/' },
'@id': '#clock',
'@type': 'schema:Clock',
'schema:dateModified': isoDate,
'schema:value': now
};
try {
const token = await getToken(CLOCK_URL, 'PUT', (e) => finalizeEvent(e, sk));
const res = await fetch(CLOCK_URL, {
method: 'PUT',
headers: {
'Content-Type': 'application/ld+json',
'Authorization': 'Nostr ' + token
},
body: JSON.stringify(clockData)
});
const time = isoDate.split('T')[1].replace('Z', '');
if (res.ok) {
process.stdout.write(`\r${time} - Updated`);
} else {
console.log(`\n${time} - Error: ${res.status} ${res.statusText}`);
}
} catch (err) {
console.log(`\nError: ${err.message}`);
}
}
console.log('Clock Updater started');
console.log('did:nostr:', 'did:nostr:' + pk);
console.log('Target:', CLOCK_URL);
console.log('Press Ctrl+C to stop\n');
// Run immediately, then every second
updateClock();
setInterval(updateClock, 1000);