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(kind:, content:, created_at: Time.now.to_i, tags: []) ⇒ 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:

  • created_at (Integer) (defaults to: Time.now.to_i)

    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>) (defaults to: [])

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

  • content (String)

    Arbitrary string.

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nostr/user.rb', line 57

def create_event(
  kind:,
  content:,
  created_at: Time.now.to_i,
  tags: []
)
  event = Event.new(
    pubkey: keypair.public_key,
    kind:,
    content:,
    created_at:,
    tags:
  )
  event.sign(keypair.private_key)
end