-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwitness_nostr.js
executable file
·110 lines (92 loc) · 3.01 KB
/
witness_nostr.js
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
import * as fs from 'fs'
import { generateSecretKey, getPublicKey } from 'nostr-tools/pure'
import { finalizeEvent } from 'nostr-tools/pure'
import { Relay, useWebSocketImplementation } from 'nostr-tools/relay'
import { hexToBytes } from '@noble/hashes/utils' // already an installed dependency
import * as nip19 from 'nostr-tools/nip19'
import WebSocket from 'ws'
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { readCredentials } from './utils.js';
useWebSocketImplementation(WebSocket)
// const credentials_func = () => {
// const __filename = fileURLToPath(import.meta.url);
// const __dirname = dirname(__filename);
// return JSON.parse(fs.readFileSync(`${__dirname}/credentials.json`, "utf8"))
// }
const credentials = readCredentials();//credentials_func()
// const credentials = JSON.parse(fs.readFileSync(`${import.meta.dirname}/credentials.json`, "utf8"))
const skHex = credentials.nostr_sk
const relayUrl = 'wss://relay.damus.io'
const waitForEventAuthor = async (relay, pk) => {
return new Promise((resolve) => {
relay.subscribe([
{
kinds: [1],
authors: [pk],
},
], {
onevent(event) {
resolve(event); // Resolve the promise when the event is received
}
});
});
}
const waitForEventId = async (relay, id) => {
return new Promise((resolve) => {
relay.subscribe([
{
ids: [id],
},
], {
onevent(event) {
resolve(event); // Resolve the promise when the event is received
}
});
});
}
const witness = async (witnessEventVerificationHash) => {
if (skHex.length === 0 || !skHex) {
console.log("Nostr SK key is required. Please get an API key from https://snort.social/login/sign-up")
process.exit(1)
}
const sk = hexToBytes(skHex)
const pk = getPublicKey(sk)
const npub = nip19.npubEncode(pk)
// console.log("npub: ", npub)
console.log("Witness event verification hash: ", witnessEventVerificationHash)
console.log(`https://snort.social/${npub}`)
let event = finalizeEvent({
kind: 1,
created_at: Math.floor(Date.now() / 1000),
tags: [],
content: witnessEventVerificationHash,
}, sk)
const relay = await Relay.connect(relayUrl)
console.log(`connected to ${relay.url}`)
await relay.publish(event)
const publishEvent = await waitForEventAuthor(relay, pk);
relay.close()
const nevent = nip19.neventEncode(publishEvent)
const witnessTimestamp = publishEvent.created_at
console.log(`got event https://snort.social/${nevent}`)
return [nevent, npub, witnessTimestamp]
}
const verify = async (transactionHash, expectedMR, expectedTimestamp) => {
const { type, data } = nip19.decode(transactionHash)
if (type !== "nevent") {
return false
}
const relay = await Relay.connect(relayUrl)
const publishEvent = await waitForEventId(relay, data.id)
relay.close()
if (expectedTimestamp !== publishEvent.created_at) {
return false
}
const merkleRoot = publishEvent.content
return merkleRoot === expectedMR
}
export {
witness,
verify,
}