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

25
sig/vendor/bech32.rbs vendored Normal file
View File

@@ -0,0 +1,25 @@
# Added only to satisfy the Steep requirements. Not 100% reliable.
module Bech32
SEPARATOR: String
BECH32M_CONST: Integer
def encode: (untyped hrp, untyped data, untyped spec) -> untyped
def self.encode: (untyped hrp, untyped data, untyped spec) -> untyped
def decode: (untyped bech, ?Integer max_length) -> [untyped, untyped, Integer]?
def self.decode: (untyped bech, ?Integer max_length) -> [untyped, untyped, Integer]?
def create_checksum: (untyped hrp, untyped data, untyped spec) -> Array[Integer]
def self.create_checksum: (untyped hrp, untyped data, untyped spec) -> Array[Integer]
def verify_checksum: (untyped hrp, untyped data) -> Integer?
def self.verify_checksum: (untyped hrp, untyped data) -> Integer?
def expand_hrp: (untyped hrp) -> untyped
def self.expand_hrp: (untyped hrp) -> untyped
def convert_bits: (untyped data, untyped from, untyped to, ?true padding) -> Array[Integer]?
def self.convert_bits: (untyped data, untyped from, untyped to, ?true padding) -> Array[Integer]?
def polymod: (untyped values) -> Integer
def self.polymod: (untyped values) -> Integer
module Encoding
BECH32: Integer
BECH32M: Integer
end
end

41
sig/vendor/bech32/nostr/entity.rbs vendored Normal file
View File

@@ -0,0 +1,41 @@
# Added only to satisfy the Steep requirements. Not 100% reliable.
module Bech32
module Nostr
class BareEntity
attr_reader hrp: untyped
attr_reader data: untyped
def initialize: (untyped hrp, untyped data) -> void
def encode: -> untyped
end
class TLVEntry
attr_reader type: (Float | Integer | String)?
attr_reader label: String?
attr_reader value: (Float | Integer | String)?
def initialize: ((Float | Integer | String)? `type`, (Float | Integer | String)? value, ?String? label) -> void
def to_payload: -> String
def to_s: -> String
private
def hex_string?: ((Float | Integer | String)? str) -> bool
end
class TLVEntity
TYPE_SPECIAL: Integer
TYPE_RELAY: Integer
TYPE_AUTHOR: Integer
TYPE_KIND: Integer
TYPES: [Integer, Integer, Integer, Integer]
attr_reader hrp: untyped
attr_reader entries: Array[TLVEntry]
def initialize: (untyped hrp, Array[TLVEntry] entries) -> void
def self.parse: (untyped hrp, untyped data) -> TLVEntity
def encode: -> untyped
end
end
end

20
sig/vendor/bech32/nostr/nip19.rbs vendored Normal file
View File

@@ -0,0 +1,20 @@
# Added only to satisfy the Steep requirements. Not 100% reliable.
module Bech32
module Nostr
module NIP19
HRP_PUBKEY: String
HRP_PRIVATE_KEY: String
HRP_NOTE_ID: String
HRP_PROFILE: String
HRP_EVENT: String
HRP_RELAY: String
HRP_EVENT_COORDINATE: String
BARE_PREFIXES: [String, String, String]
TLV_PREFIXES: [String, String, String, String]
ALL_PREFIXES: Array[String]
def decode: (untyped string) -> untyped
def self.decode: (untyped string) -> untyped
end
end
end

21
sig/vendor/bech32/segwit_addr.rbs vendored Normal file
View File

@@ -0,0 +1,21 @@
# Added only to satisfy the Steep requirements. Not 100% reliable.
module Bech32
class SegwitAddr
HRP_MAINNET: String
HRP_TESTNET: String
HRP_REGTEST: String
attr_accessor hrp: String
attr_accessor ver: (Float | Integer | String)?
attr_accessor prog: Array[(Float | Integer | String)?]
def initialize: (?nil addr) -> void
def to_script_pubkey: -> ((Float | Integer | String)?)
def script_pubkey=: (untyped script_pubkey) -> (Array[(Float | Integer | String)?])
def addr: -> untyped
private
def parse_addr: (untyped addr) -> nil
end
end

View File

@@ -1,9 +1,16 @@
# Added only to satisfy the Steep requirements. Not 100% reliable.
module EventEmitter
def add_listener: (untyped `type`, ?{once: true} params) -> Integer
interface _Event
def data: -> String
def message: -> String
def code: -> Integer
def reason: -> String
end
def add_listener: (Symbol event_name) { (_Event event) -> void } -> void
alias on add_listener
def remove_listener: (untyped id_or_type) -> Array[untyped]?
def emit: (untyped `type`, *untyped data) -> Array[untyped]
def once: (untyped `type`) -> Integer
def emit: (Symbol `type`, *untyped data) -> Array[untyped]
def once: (Symbol `type`) -> Integer
end

View File

@@ -6,7 +6,7 @@ module EventMachine
def initialize: -> void
def num_subscribers: -> Integer
def subscribe: (*untyped a) ?{ -> untyped } -> Integer
def subscribe: (*untyped a) ?{ (untyped) -> untyped } -> Integer
def unsubscribe: (untyped name) -> untyped
def push: (*untyped items) -> untyped
alias << push

30
sig/vendor/faye/websocket.rbs vendored Normal file
View File

@@ -0,0 +1,30 @@
# Added only to satisfy the Steep requirements. Not 100% reliable.
module Faye
class WebSocket
ADAPTERS: Hash[String, :Goliath | :Rainbows | :Thin]
@url: String
@driver_started: false
@stream: Stream
@driver: bot
def self.determine_url: (untyped env, ?[String, String] schemes) -> String
def self.ensure_reactor_running: -> nil
def self.load_adapter: (untyped backend) -> bool?
def self.secure_request?: (untyped env) -> bool
def self.websocket?: (untyped env) -> untyped
attr_reader env: untyped
def initialize: (untyped env, ?nil protocols, ?Hash[untyped, untyped] options) -> void
def start_driver: -> nil
def rack_response: -> [Integer, Hash[untyped, untyped], Array[untyped]]
class Stream
@socket_object: bot
def fail: -> untyped
def receive: (untyped data) -> untyped
end
end
end

45
sig/vendor/faye/websocket/api.rbs vendored Normal file
View File

@@ -0,0 +1,45 @@
# Added only to satisfy the Steep requirements. Not 100% reliable.
module Faye
class WebSocket
module API
CONNECTING: Integer
OPEN: Integer
CLOSING: Integer
CLOSED: Integer
CLOSE_TIMEOUT: Integer
@driver: untyped
@ping: nil
@ping_id: Integer
@stream: nil
@proxy: nil
@ping_timer: nil
@close_timer: nil
@close_params: [String, Integer]?
@onerror: nil
@onclose: nil
@onmessage: nil
@onopen: nil
attr_reader url: untyped
attr_reader ready_state: Integer
attr_reader buffered_amount: Integer
def initialize: (?Hash[untyped, untyped] options) -> void
def write: (untyped data) -> untyped
def send: (untyped message) -> false
def ping: (?String message) -> false
def close: (?nil code, ?nil reason) -> untyped
def protocol: -> String
private
def open: -> nil
def receive_message: (untyped data) -> nil
def emit_error: (untyped message) -> nil
def begin_close: (String reason, Integer code, ?Hash[untyped, untyped] options) -> nil
def finalize_close: -> nil
def parse: (untyped data) -> untyped
end
end
end

43
sig/vendor/faye/websocket/client.rbs vendored Normal file
View File

@@ -0,0 +1,43 @@
# Added only to satisfy the Steep requirements. Not 100% reliable.
module Faye
class WebSocket
class Client
DEFAULT_PORTS: Hash[String, Integer]
SECURE_PROTOCOLS: [String, String]
include EventEmitter
include API
@url: untyped
@endpoint: untyped
@origin_tls: Hash[untyped, untyped]
@socket_tls: Hash[untyped, untyped]
@driver: bot
@proxy: nil
@ssl_verifier: untyped
@stream: untyped
def initialize: (untyped url, ?Array[String] protocols, ?Hash[untyped, untyped] options) -> void
private
def configure_proxy: (Hash[untyped, untyped] proxy) -> nil
def start_tls: (untyped uri, Hash[untyped, untyped] options) -> nil
def on_connect: (untyped stream) -> untyped
def on_network_error: (nil error) -> untyped
def ssl_verify_peer: (untyped cert) -> untyped
def ssl_handshake_completed: -> untyped
module Connection
attr_accessor parent: bot
def connection_completed: -> untyped
def ssl_verify_peer: (untyped cert) -> untyped
def ssl_handshake_completed: -> untyped
def receive_data: (untyped data) -> untyped
def unbind: (?nil error) -> untyped
def write: (untyped data) -> nil
end
end
end
end