-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-1_create_bot_account.ts
61 lines (51 loc) · 1.57 KB
/
2-1_create_bot_account.ts
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
import {
type Event,
finishEvent,
getPublicKey,
relayInit,
} from "nostr-tools";
import { currUnixtime } from "./utils.ts";
/* Q-1: Bot用に新しい秘密鍵を生成して、ここに設定しよう */
const BOT_PRIVATE_KEY_HEX = ???;
const relayUrl = "wss://relay-jp.nostr.wirednet.jp";
/**
* メタデータ(プロフィール)イベントを組み立てる
*/
const composeMetadata = (): Event => {
/* Q-2: Botアカウントのプロフィールを設定しよう */
const profile = {
name: "", // スクリーンネーム
display_name: "", // 表示名
about: "", // 説明欄(bio)
};
/* Q-3: メタデータ(プロフィール)イベントのフィールドを埋めよう */
// pubkeyは以下の処理で自動で設定されるため、ここで設定する必要はありません
const ev = {
kind: ???,
content: ???,
tags: [],
created_at: currUnixtime(),
};
// イベントID(ハッシュ値)計算・署名
return finishEvent(ev, BOT_PRIVATE_KEY_HEX);
}
const main = async () => {
const relay = relayInit(relayUrl);
relay.on("error", () => {
console.error("failed to connect");
});
await relay.connect();
// メタデータ(プロフィール)イベントを組み立てる
const metadata = composeMetadata();
// メタデータイベントを送信
const pub = relay.publish(metadata);
pub.on("ok", () => {
console.log("succeess!");
relay.close();
});
pub.on("failed", () => {
console.log("failed to send event");
relay.close();
});
};
main().catch((e) => console.error(e));