|
| 1 | +NIP-RTC WebRTC |
| 2 | +============== |
| 3 | +`draft` `optional` `author:jacany` `author:riccardobl` |
| 4 | +based on https://github.com/nostr-protocol/nips/blob/ead1cd6ca6b5b789d70e0d146d17266a2e8e2fba/100.md |
| 5 | + |
| 6 | +This NIP defines how to do WebRTC communication over nostr. |
| 7 | +--------------------------------- |
| 8 | + |
| 9 | +## Defining Rooms |
| 10 | +Rooms are essentially the space that you will be connected to. |
| 11 | +Rooms are created by generating a random secret key from wich the derived public key is used as the room id. |
| 12 | +Event an one to one connection requires a room. |
| 13 | +To comunicate with someone in the room the events must use a `r` tag with the room id and a `p` tag with the public key of the person you are trying to connect to. |
| 14 | +Some events will require only the `r` tag and as such are broadcasted to the entire room, others require the `p` tag and are sent to a specific person in the room. |
| 15 | + |
| 16 | +Clients MUST listen for both the `r` and `p` tag containing their pubkey so they know that event is intended for them, for the offer, answer and candidate events, and only to the `r` tag for connect and disconnect events. |
| 17 | + |
| 18 | +## Encryption |
| 19 | +The `content` field is encrypted with `NIP-44`, twice, first with the conversation key derives by sender priv + receiver pub and then with the conversation key derived by room priv + receiver pub. |
| 20 | + |
| 21 | +Pseudo code: |
| 22 | + |
| 23 | +```javascript |
| 24 | +function encrypt(localPrivKey, content, recipientPubkey, roomSecretKey){ |
| 25 | + const serialized = JSON.stringify(content); |
| 26 | + let encrypted = nip44Encrypt(serialized, nip44ConversationKey(localPrivKey, recipientPubkey)); |
| 27 | + encrypted = nip44Encrypt(encrypted, nip44ConversationKey(roomSecretKey, recipientPubkey)); |
| 28 | + return encrypted; |
| 29 | +} |
| 30 | + |
| 31 | +function decrypt(localPrivKey, content, senderPubkey, roomPublicKey){ |
| 32 | + content = nip44Decrypt(content, nip44ConversationKey(localPrivKey, roomPublicKey)); |
| 33 | + content = nip44Decrypt(content, nip44ConversationKey(localPrivKey, senderPubkey)); |
| 34 | + return JSON.parse(decrypted); |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +### Broadcasting that you are present |
| 39 | +Announces that the client is ready to connect to others. |
| 40 | +The connection ID is inferred from the provided pubkey. |
| 41 | +```json |
| 42 | +{ |
| 43 | + "kind": 25050, |
| 44 | + "pubkey": "<sender pubkey>", |
| 45 | + "tags": [ |
| 46 | + ["t", "connect"], |
| 47 | + ["r", "room id"] |
| 48 | + ["expiration", "<expiration>"] |
| 49 | + ] |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### Closing |
| 54 | +This is used when disconnecting from everybody else. Ex: when browser tab is closed. |
| 55 | +```json |
| 56 | +{ |
| 57 | + "kind": 25050, |
| 58 | + "pubkey": "<sender pubkey>", |
| 59 | + "tags": [ |
| 60 | + ["type", "disconnect"], |
| 61 | + ["r", "room id"] |
| 62 | + ] |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +### Offering to connect |
| 67 | +Used when responding to a `type:connect`. Offering to connect to that peer. |
| 68 | +```json |
| 69 | +{ |
| 70 | + "kind": 25050, |
| 71 | + "pubkey": "<sender pubkey>", |
| 72 | + "tags": [ |
| 73 | + ["type", "offer"], |
| 74 | + ["p", "<reciever>"], |
| 75 | + ["r", "<room id>"] |
| 76 | + ], |
| 77 | + "content": encrypt({ |
| 78 | + "offer": "<offer>", |
| 79 | + "turn": ["protocol://turnserver:port", "protocol://turnserver2:port"], |
| 80 | + ...misc |
| 81 | + }) |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### Answering an Offer |
| 86 | +```json |
| 87 | +{ |
| 88 | + "kind": 25050, |
| 89 | + "pubkey": "<sender pubkey>", |
| 90 | + "tags": [ |
| 91 | + ["type", "answer"], |
| 92 | + ["p", "<reciever>"], |
| 93 | + ["r", "<room id>"] |
| 94 | + ], |
| 95 | + "content": encrypt({ |
| 96 | + "sdp": "<sdp>", |
| 97 | + "turn": ["protocol://turnserver:port", "protocol://turnserver2:port"], |
| 98 | + ...misc |
| 99 | + }) |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +### Broadcasting ICE Candidate |
| 104 | +```json |
| 105 | +{ |
| 106 | + "kind": 25050, |
| 107 | + "pubkey": "<sender pubkey>", |
| 108 | + "tags": [ |
| 109 | + ["type", "candidate"], |
| 110 | + ["p", "<reciever>"], |
| 111 | + ["r", "<room id>"] |
| 112 | + ], |
| 113 | + "content": encrypt({ |
| 114 | + "candidates": ["<sdp>"], |
| 115 | + ...misc |
| 116 | + }) |
| 117 | +} |
| 118 | +``` |
0 commit comments