Class: Nostr::Events::EncryptedDirectMessage

Inherits:
Nostr::Event show all
Defined in:
lib/nostr/events/encrypted_direct_message.rb

Overview

An event whose content is encrypted. It can only be decrypted by the owner of the private key that pairs the event’s pubkey.

Instance Attribute Summary

Attributes inherited from Nostr::Event

#content, #created_at, #id, #kind, #pubkey, #sig, #tags

Instance Method Summary collapse

Methods inherited from Nostr::Event

#==, #add_event_reference, #add_pubkey_reference, #serialize, #sign, #to_h, #verify_signature

Constructor Details

#initialize(plain_text:, sender_private_key:, recipient_public_key:, previous_direct_message: nil) ⇒ EncryptedDirectMessage

Instantiates a new encrypted direct message

conversation or a message we are explicitly replying to (such that contextual, more organized conversations may happen

Examples:

Instantiating a new encrypted direct message

Nostr::Events::EncryptedDirectMessage.new(
  sender_private_key: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
  recipient_public_key: '48df4af6e240ac5f7c5de89bf5941b249880be0e87d03685b178ccb1a315f52e',
  plain_text: 'Your feedback is appreciated, now pay $8',
)

Instantiating a new encrypted direct message that references a previous direct message

Nostr::Events::EncryptedDirectMessage.new(
  sender_private_key: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
  recipient_public_key: '48df4af6e240ac5f7c5de89bf5941b249880be0e87d03685b178ccb1a315f52e',
  plain_text: 'Your feedback is appreciated, now pay $8',
  previous_direct_message: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460'
)

Parameters:

  • plain_text (String)

    The content of the encrypted message.

  • sender_private_key (PrivateKey)

    32-bytes hex-encoded private key of the message’s author.

  • recipient_public_key (PublicKey)

    32-bytes hex-encoded public key of the recipient of the encrypted message.

  • previous_direct_message (String) (defaults to: nil)

    32-bytes hex-encoded id identifying the previous message in a



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/nostr/events/encrypted_direct_message.rb', line 36

def initialize(plain_text:, sender_private_key:, recipient_public_key:, previous_direct_message: nil)
  crypto = Crypto.new
  keygen = Keygen.new

  encrypted_content = crypto.encrypt_text(sender_private_key, recipient_public_key, plain_text)
  sender_public_key = keygen.extract_public_key(sender_private_key)

  super(
    pubkey: sender_public_key,
    kind: Nostr::EventKind::ENCRYPTED_DIRECT_MESSAGE,
    content: encrypted_content,
  )

  add_pubkey_reference(recipient_public_key)
  add_event_reference(previous_direct_message) if previous_direct_message
end