認證(NIP-42)
車道上的 AUTH 是可選的,只有要用 #p 訂閱自己的公鑰時才需要。
什麼時候需要
| 動作 | 需要 AUTH? |
|---|---|
| 發布任何事件 | 不需要 |
用標籤或 authors 訂閱 | 不需要 |
用 #p = 自己的 pubkey 訂閱 | 需要 |
運作方式
- 連線時中繼會送出
["AUTH", <challenge>],不使用 AUTH 的客戶端可以忽略。 - 要認證的話,回傳一則簽過名的 kind
22242事件,帶challenge標籤,以及指向本中繼網址的relay標籤(NIP-42)。 - 該事件必須是 10 分鐘內產生的,
relay標籤必須指向你連線的那個主機。 - 認證成功後,這條連線上發布的事件改以你認證的金鑰計算速率。
// Continues the Quick start; also import getPublicKey from "nostr-tools/pure".
// The relay sends ["AUTH", <challenge>] as soon as you connect.
// Answer it only if you need #p subscriptions (your own inbox).
await relay.auth(async (template) => finalizeEvent(template, sk));
relay.subscribe([{ kinds: [20078], "#p": [getPublicKey(sk)] }], {
onevent(invite) {
/* someone signalled you */
},
});