Implement NIP-19 bech32-encoded private and public keys

https://github.com/nostr-protocol/nips/blob/master/19.md
This commit is contained in:
Wilson Silva
2023-11-20 09:59:27 +07:00
parent 3fffcd1a4e
commit 3520cf8219
58 changed files with 1189 additions and 104 deletions

View File

@@ -5,16 +5,16 @@ module Nostr
def initialize: -> void
def connect: (Relay relay) -> Thread
def subscribe: (?subscription_id: String, ?filter: Filter) -> Subscription
def unsubscribe: (String subscription_id) -> untyped
def unsubscribe: (String subscription_id) -> void
def publish: (Event event) -> untyped
private
attr_reader subscriptions: Hash[String, Subscription]
attr_reader parent_to_child_channel: untyped
attr_reader child_to_parent_channel: untyped
attr_reader parent_to_child_channel: EventMachine::Channel
attr_reader child_to_parent_channel: EventMachine::Channel
def execute_within_an_em_thread: { -> untyped } -> Thread
def initialize_channels: -> untyped
def execute_within_an_em_thread: { -> void } -> Thread
def initialize_channels: -> void
end
end

View File

@@ -4,13 +4,13 @@ module Nostr
CIPHER_CURVE: String
CIPHER_ALGORITHM: String
def encrypt_text: (String, String, String) -> String
def decrypt_text: (String, String, String) -> String
def sign_event: (Event, String) -> Event
def encrypt_text: (PrivateKey, PublicKey, String) -> String
def decrypt_text: (PrivateKey, PublicKey, String) -> String
def sign_event: (Event, PrivateKey) -> Event
private
def compute_shared_key: (String, String) -> String
def compute_shared_key: (PrivateKey, PublicKey) -> String
def hash_event:(Event) -> String
end
end

View File

@@ -0,0 +1,4 @@
module Nostr
class Error < StandardError
end
end

View File

@@ -0,0 +1,6 @@
module Nostr
class InvalidHRPError < KeyValidationError
def initialize: (String, String) -> void
end
end

View File

@@ -0,0 +1,5 @@
module Nostr
class InvalidKeyFormatError < KeyValidationError
def initialize: (String) -> void
end
end

View File

@@ -0,0 +1,5 @@
module Nostr
class InvalidKeyLengthError < KeyValidationError
def initialize: (String) -> void
end
end

View File

@@ -0,0 +1,5 @@
module Nostr
class InvalidKeyTypeError < KeyValidationError
def initialize: (String) -> void
end
end

View File

@@ -0,0 +1,4 @@
module Nostr
class KeyValidationError < Error
end
end

View File

@@ -1,6 +1,6 @@
module Nostr
class Event
attr_reader pubkey: String
attr_reader pubkey: PublicKey
attr_reader created_at: Integer
attr_reader kind: Integer
attr_reader tags: Array[Array[String]]
@@ -9,7 +9,7 @@ module Nostr
attr_accessor sig: String?|nil
def initialize: (
pubkey: String,
pubkey: PublicKey,
kind: Integer,
content: String,
?created_at: Integer,
@@ -31,9 +31,9 @@ module Nostr
}
def ==: (Event other) -> bool
def sign:(String) -> Event
def sign:(PrivateKey) -> Event
def add_event_reference: (String) -> Array[Array[String]]
def add_pubkey_reference: (String) -> Array[Array[String]]
def add_pubkey_reference: (PublicKey) -> Array[Array[String]]
end
end

View File

@@ -3,8 +3,8 @@ module Nostr
class EncryptedDirectMessage < Event
def initialize: (
plain_text: String,
sender_private_key: String,
recipient_public_key: String,
sender_private_key: PrivateKey,
recipient_public_key: PublicKey,
?previous_direct_message: String|nil
) -> void
end

16
sig/nostr/key.rbs Normal file
View File

@@ -0,0 +1,16 @@
module Nostr
class Key < String
FORMAT: Regexp
LENGTH: int
def self.from_bech32: (String) -> Key
def self.hrp: -> String
def initialize: (String) -> void
def to_bech32: -> String
private
def validate_hex_value: (String) -> nil
end
end

View File

@@ -1,9 +1,13 @@
# Classes
module Nostr
class KeyPair
attr_reader private_key: String
attr_reader public_key: String
attr_reader private_key: PrivateKey
attr_reader public_key: PublicKey
def initialize: (private_key: String, public_key: String) -> void
def initialize: (private_key: PrivateKey, public_key: PublicKey) -> void
private
def validate_keys: (PrivateKey, PublicKey) -> void
end
end

View File

@@ -3,11 +3,14 @@ module Nostr
class Keygen
def initialize: -> void
def generate_key_pair: -> KeyPair
def generate_private_key: -> String
def extract_public_key: (String private_key) -> String
def generate_private_key: -> PrivateKey
def extract_public_key: (PrivateKey private_key) -> PublicKey
def get_key_pair_from_private_key: (PrivateKey private_key) -> KeyPair
private
attr_reader group: untyped
def validate_private_key: (PrivateKey private_key) -> void
end
end

View File

@@ -0,0 +1,4 @@
module Nostr
class PrivateKey < Key
end
end

4
sig/nostr/public_key.rbs Normal file
View File

@@ -0,0 +1,4 @@
module Nostr
class PublicKey < Key
end
end