Class: Nostr::User

Inherits:
Object
  • Object
show all
Defined in:
lib/nostr/user.rb

Overview

Each user has a keypair. Signatures, public key, and encodings are done according to the Schnorr signatures standard for the curve secp256k1.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keypair: nil, keygen: Keygen.new) ⇒ User

Instantiates a user

Examples:

Creating a user with no keypair

user = Nostr::User.new

Creating a user with a keypair

user = Nostr::User.new(keypair: keypair)

Parameters:

  • keypair (Keypair) (defaults to: nil)

    A pair of private and public keys

  • keygen (Keygen) (defaults to: Keygen.new)

    A private key and public key generator



36
37
38
# File 'lib/nostr/user.rb', line 36

def initialize(keypair: nil, keygen: Keygen.new)
  @keypair = keypair || keygen.generate_key_pair
end

Instance Attribute Details

#keypairKeyPair (readonly)

A pair of private and public keys

Examples:

user.keypair # #<Nostr::KeyPair:0x0000000107bd3550
 @private_key="893c4cc8088924796b41dc788f7e2f746734497010b1a9f005c1faad7074b900",
 @public_key="2d7661527d573cc8e84f665fa971dd969ba51e2526df00c149ff8e40a58f9558">

Returns:



21
22
23
# File 'lib/nostr/user.rb', line 21

def keypair
  @keypair
end

Instance Method Details

#create_event(event_attributes) ⇒ Event

Builds an Event

Examples:

Creating a note event

event = user.create_event(
  kind: Nostr::EventKind::TEXT_NOTE,
  content: 'Your feedback is appreciated, now pay $8'
)

Parameters:

  • event_attributes (Hash)

Options Hash (event_attributes):

  • :pubkey (String)

    32-bytes hex-encoded public key of the event creator.

  • :created_at (Integer)

    Date of the creation of the vent. A UNIX timestamp, in seconds.

  • :kind (Integer)

    The kind of the event. An integer from 0 to 3.

  • :tags (Array<Array>)

    An array of tags. Each tag is an array of strings.

  • :content (String)

    Arbitrary string.

Returns:



59
60
61
62
# File 'lib/nostr/user.rb', line 59

def create_event(event_attributes)
  event = Event.new(**event_attributes.merge(pubkey: keypair.public_key))
  event.sign(keypair.private_key)
end