-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsbog.js
More file actions
55 lines (41 loc) · 1.27 KB
/
sbog.js
File metadata and controls
55 lines (41 loc) · 1.27 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
import nacl from './lib/nacl-fast-es.js'
import { keys } from './keys.js'
import { decode, encode } from './lib/base64.js'
import { logs } from './log.js'
import { make, find } from './blob.js'
export async function publish (data) {
const pubkey = await keys.pubkey()
const privkey = await keys.privkey()
const datahash = await make(data)
const timestamp = Date.now()
const msg = timestamp + pubkey + datahash
const hash = encode(
Array.from(
new Uint8Array(
await crypto.subtle.digest("SHA-256", new TextEncoder().encode(msg))
)
)
)
let previous = await logs.getLatestHash(pubkey)
if (!previous) {
previous = hash
}
const next = msg + previous + hash
const sig = encode(nacl.sign(new TextEncoder().encode(next), decode(privkey)))
const done = pubkey + sig
logs.add(done)
return done
}
export async function open (msg) {
const opened = new TextDecoder().decode(nacl.sign.open(decode(msg.substring(44)), decode(msg.substring(0, 44))))
const obj = {
timestamp: parseInt(opened.substring(0, 13)),
author: opened.substring(13, 57),
hash : opened.substring(145),
previous: opened.substring(101, 145),
data: opened.substring(57, 101),
raw: msg
}
obj.text = await find(obj.data)
return obj
}