upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/04.md
diff options
context:
space:
mode:
Diffstat (limited to '04.md')
-rw-r--r--04.md43
1 files changed, 43 insertions, 0 deletions
diff --git a/04.md b/04.md
new file mode 100644
index 0000000..801dd53
--- /dev/null
+++ b/04.md
@@ -0,0 +1,43 @@
1NIP-04
2======
3
4Encrypted Direct Message
5------------------------
6
7`draft` `optional` `author:arcbtc`
8
9A 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
17Code sample for generating such an event in JavaScript:
18
19```js
20import crypto from 'crypto'
21import * as secp from 'noble-secp256k1'
22
23let sharedPoint = secp.getSharedSecret(ourPrivateKey, '02' + theirPublicKey)
24let sharedX = sharedPoint.substr(2, 64)
25
26let iv = crypto.randomFillSync(new Uint8Array(16))
27var cipher = crypto.createCipheriv(
28 'aes-256-cbc',
29 Buffer.from(sharedX, 'hex'),
30 iv
31)
32let encryptedMessage = cipher.update(text, 'utf8', 'base64')
33encryptedMessage += cipher.final('base64')
34let ivBase64 = Buffer.from(iv.buffer).toString('base64')
35
36let 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```