nostr-zap 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a37e06d0b8cbcaf43059fa05aef8de40e96c735d7aba1e0a0d7b2f22060d3051
4
- data.tar.gz: 15d1f6384ed3af2eb9413414f934dce327193b60e1aacfbfcae777594035cef0
3
+ metadata.gz: f0ce163602465238c8698d2efb563f03d4279855bbc00d19e32f85fa19cefc71
4
+ data.tar.gz: 1a65c9536e72070bbc98d19e8e3ec5fa719184e2a7fbce8e9646249de4b456a2
5
5
  SHA512:
6
- metadata.gz: 734a48257c00913f893257978dbf7b4c4b6f422d10530a2ccd0fa15112c3c107a0cd93363e5a0e567434bbb73f22e08d232fc6e652109923289197fdc2f35d63
7
- data.tar.gz: edb0bc4efa7ce7b22765796faeb6ac3ee9b2f48f76eb002936449b49caecfa20bd32ee99ca3e0772e61fd769b33d0255b76f5ab58382a9c6bf55a24a6e8a2558
6
+ metadata.gz: 5c8f06e168c2337ae0d0778fc1854d6fbd2bfb1459d10d83c391aadd64adf06c6d8961545a8627084d4ed86b75640270a602a962bfe82df8c29c8a7566244c58
7
+ data.tar.gz: e8d43ba83dbbfef3203cd5eee8b5964e9b70d3eaf9e1d1c269b4e4a0f3d1b38b65fccb34aa28d7f093072aaa524e842e83be517111a505520f8a730210240d05
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NostrZap
4
- VERSION = '0.2.4'
4
+ VERSION = '0.2.5'
5
5
  end
@@ -8,10 +8,12 @@ module NostrZap
8
8
  #
9
9
  # A valid zap request must:
10
10
  # - Be a valid JSON string representing a NOSTR event
11
+ # - Have an integer Unix timestamp in 'created_at'
11
12
  # - Have kind = 9734
12
13
  # - Have a valid Schnorr signature (when verification is enabled)
13
14
  # - Have a 'p' tag with the recipient's pubkey
14
15
  # - Have a 'relays' tag with at least one relay URL
16
+ # - Express any 'amount' in millisatoshis as a string of decimal digits
15
17
  #
16
18
  # @example
17
19
  # validator = NostrZap::Zap::RequestValidator.new(json_string)
@@ -24,6 +26,7 @@ module NostrZap
24
26
  #
25
27
  class RequestValidator
26
28
  ZAP_REQUEST_KIND = 9734
29
+ AMOUNT_FORMAT = /\A[0-9]+\z/
27
30
 
28
31
  attr_reader :errors, :parsed_event
29
32
 
@@ -61,7 +64,8 @@ module NostrZap
61
64
 
62
65
  # @return [Integer, nil] amount in millisatoshis
63
66
  def amount_msats
64
- find_tag_value('amount')&.to_i
67
+ value = find_tag_value('amount')
68
+ value.to_i if value.is_a?(String) && value.match?(AMOUNT_FORMAT)
65
69
  end
66
70
 
67
71
  # @return [String, nil] event ID being zapped
@@ -84,16 +88,26 @@ module NostrZap
84
88
  end
85
89
 
86
90
  def parse_json
87
- @parsed_event = JSON.parse(@zap_request_json)
88
- unless @parsed_event.is_a?(Hash)
89
- @parsed_event = nil
90
- return fail_with('Invalid event format')
91
- end
92
- true
93
- rescue JSON::ParserError => e
91
+ input = @zap_request_json
92
+ return fail_with("Expected a JSON string, got #{input.class}") unless input.is_a?(String)
93
+
94
+ input = input.dup.force_encoding(Encoding::UTF_8) if input.encoding == Encoding::BINARY
95
+ return fail_with("Invalid JSON: invalid #{input.encoding.name}") unless input.valid_encoding?
96
+
97
+ event = JSON.parse(input, allow_duplicate_key: false, on_load: method(:validate_json_encoding).to_proc)
98
+ return fail_with('Invalid event format') unless event.is_a?(Hash)
99
+
100
+ @parsed_event = event
101
+ rescue JSON::ParserError, EncodingError => e
94
102
  fail_with("Invalid JSON: #{e.message}")
95
103
  end
96
104
 
105
+ def validate_json_encoding(value)
106
+ raise JSON::ParserError, 'invalid UTF-8' if value.is_a?(String) && !value.valid_encoding?
107
+
108
+ value
109
+ end
110
+
97
111
  def structure_valid?
98
112
  missing_fields = %w[id pubkey created_at kind tags content sig] - @parsed_event.keys
99
113
  return true if missing_fields.empty?
@@ -102,6 +116,10 @@ module NostrZap
102
116
  end
103
117
 
104
118
  def fields_format_valid?
119
+ unless @parsed_event['created_at'].is_a?(Integer)
120
+ return fail_with("Invalid 'created_at': expected an integer timestamp")
121
+ end
122
+
105
123
  { 'id' => 64, 'pubkey' => 64, 'sig' => 128 }.all? do |field, length|
106
124
  value = @parsed_event[field]
107
125
  if value.is_a?(String) && value.length == length && value.match?(/\A[0-9a-f]+\z/i)
@@ -120,7 +138,10 @@ module NostrZap
120
138
  end
121
139
 
122
140
  def tags_valid?
123
- p_tag_valid? && relays_tag_valid?
141
+ tags = @parsed_event['tags']
142
+ return fail_with("Invalid 'tags': expected an array of arrays") unless tags.is_a?(Array) && tags.all?(Array)
143
+
144
+ p_tag_valid? && relays_tag_valid? && amount_tags_valid?
124
145
  end
125
146
 
126
147
  def p_tag_valid?
@@ -141,6 +162,16 @@ module NostrZap
141
162
  true
142
163
  end
143
164
 
165
+ def amount_tags_valid?
166
+ @parsed_event['tags'].each do |name, value|
167
+ next unless name == 'amount'
168
+
169
+ return fail_with("Invalid 'amount' tag: expected a string value") unless value.is_a?(String)
170
+ return fail_with("Invalid 'amount' tag: expected decimal digits") unless value.match?(AMOUNT_FORMAT)
171
+ end
172
+ true
173
+ end
174
+
144
175
  def event_id_valid?
145
176
  computed = Crypto.compute_event_id(
146
177
  pubkey: @parsed_event['pubkey'],
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nostr-zap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Zykov
8
+ autorequire:
8
9
  bindir: bin
9
10
  cert_chain: []
10
- date: 1980-01-02 00:00:00.000000000 Z
11
+ date: 2026-12-02 00:00:00.000000000 Z
11
12
  dependencies:
12
13
  - !ruby/object:Gem::Dependency
13
14
  name: bip-schnorr
@@ -23,6 +24,20 @@ dependencies:
23
24
  - - "~>"
24
25
  - !ruby/object:Gem::Version
25
26
  version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.19.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.19.2
26
41
  - !ruby/object:Gem::Dependency
27
42
  name: websocket
28
43
  requirement: !ruby/object:Gem::Requirement
@@ -67,6 +82,7 @@ metadata:
67
82
  source_code_uri: https://github.com/sashazykov/nostr-zap
68
83
  changelog_uri: https://github.com/sashazykov/nostr-zap/blob/main/CHANGELOG.md
69
84
  rubygems_mfa_required: 'true'
85
+ post_install_message:
70
86
  rdoc_options: []
71
87
  require_paths:
72
88
  - lib
@@ -81,7 +97,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
97
  - !ruby/object:Gem::Version
82
98
  version: '0'
83
99
  requirements: []
84
- rubygems_version: 3.6.9
100
+ rubygems_version: 3.1.6
101
+ signing_key:
85
102
  specification_version: 4
86
103
  summary: NIP-57 Zaps for NOSTR — validate zap requests, build zap receipts, publish
87
104
  to relays