From f25c7e672c23ca5463fa5c0fcb5e5f424d956862 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 1 May 2022 07:48:57 -0300 Subject: migrate nips from main nostr repo. --- 04.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 04.md (limited to '04.md') diff --git a/04.md b/04.md new file mode 100644 index 0000000..801dd53 --- /dev/null +++ b/04.md @@ -0,0 +1,43 @@ +NIP-04 +====== + +Encrypted Direct Message +------------------------ + +`draft` `optional` `author:arcbtc` + +A special event with kind `4`, meaning "encrypted direct message". It is supposed to have the following attributes: + +**`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": "?iv="`. + +**`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", ""]`. + +**`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", ""]`. + +Code sample for generating such an event in JavaScript: + +```js +import crypto from 'crypto' +import * as secp from 'noble-secp256k1' + +let sharedPoint = secp.getSharedSecret(ourPrivateKey, '02' + theirPublicKey) +let sharedX = sharedPoint.substr(2, 64) + +let iv = crypto.randomFillSync(new Uint8Array(16)) +var cipher = crypto.createCipheriv( + 'aes-256-cbc', + Buffer.from(sharedX, 'hex'), + iv +) +let encryptedMessage = cipher.update(text, 'utf8', 'base64') +encryptedMessage += cipher.final('base64') +let ivBase64 = Buffer.from(iv.buffer).toString('base64') + +let event = { + pubkey: ourPubKey, + created_at: Math.floor(Date.now() / 1000), + kind: 4, + tags: [['p', theirPublicKey]], + content: encryptedMessage + '?iv=' + ivBase64 +} +``` -- cgit v1.2.3