diff options
Diffstat (limited to '04.md')
| -rw-r--r-- | 04.md | 43 |
1 files changed, 43 insertions, 0 deletions
| @@ -0,0 +1,43 @@ | |||
| 1 | NIP-04 | ||
| 2 | ====== | ||
| 3 | |||
| 4 | Encrypted Direct Message | ||
| 5 | ------------------------ | ||
| 6 | |||
| 7 | `draft` `optional` `author:arcbtc` | ||
| 8 | |||
| 9 | A special event with kind `4`, meaning "encrypted direct message". It is supposed to have the following attributes: | ||
| 10 | |||
| 11 | **`content`** MUST be equal to the base64-encoded, aes-256-cbc encrypted string of anything a user wants to write, encrypted using a shared cipher generated by combining the recipient's public-key with the sender's private-key; this appended by the base64-encoded initialization vector as if it was a querystring parameter named "iv". The format is the following: `"content": "<encrypted_text>?iv=<initialization_vector>"`. | ||
| 12 | |||
| 13 | **`tags`** MUST contain an entry identifying the receiver of the message (such that relays may naturally forward this event to them), in the form `["p", "<pubkey, as a hex string>"]`. | ||
| 14 | |||
| 15 | **`tags`** MAY contain an entry identifying the previous message in a conversation or a message we are explicitly replying to (such that contextual, more organized conversations may happen), in the form `["e", "<event_id>"]`. | ||
| 16 | |||
| 17 | Code sample for generating such an event in JavaScript: | ||
| 18 | |||
| 19 | ```js | ||
| 20 | import crypto from 'crypto' | ||
| 21 | import * as secp from 'noble-secp256k1' | ||
| 22 | |||
| 23 | let sharedPoint = secp.getSharedSecret(ourPrivateKey, '02' + theirPublicKey) | ||
| 24 | let sharedX = sharedPoint.substr(2, 64) | ||
| 25 | |||
| 26 | let iv = crypto.randomFillSync(new Uint8Array(16)) | ||
| 27 | var cipher = crypto.createCipheriv( | ||
| 28 | 'aes-256-cbc', | ||
| 29 | Buffer.from(sharedX, 'hex'), | ||
| 30 | iv | ||
| 31 | ) | ||
| 32 | let encryptedMessage = cipher.update(text, 'utf8', 'base64') | ||
| 33 | encryptedMessage += cipher.final('base64') | ||
| 34 | let ivBase64 = Buffer.from(iv.buffer).toString('base64') | ||
| 35 | |||
| 36 | let event = { | ||
| 37 | pubkey: ourPubKey, | ||
| 38 | created_at: Math.floor(Date.now() / 1000), | ||
| 39 | kind: 4, | ||
| 40 | tags: [['p', theirPublicKey]], | ||
| 41 | content: encryptedMessage + '?iv=' + ivBase64 | ||
| 42 | } | ||
| 43 | ``` | ||