nostr-zap 0.2.3 → 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: b1feece47ab5f8862dd5c46ffce079eb5499fd850056bb3d71c4460142099fa6
4
- data.tar.gz: 91712e7614befeec32b360b409cc6e16c2ad555499fcffa1f2e785302eab28c1
3
+ metadata.gz: f0ce163602465238c8698d2efb563f03d4279855bbc00d19e32f85fa19cefc71
4
+ data.tar.gz: 1a65c9536e72070bbc98d19e8e3ec5fa719184e2a7fbce8e9646249de4b456a2
5
5
  SHA512:
6
- metadata.gz: f361d7b5d2204aeec5224251403595f868e50921859b7da12bd0c7e16cb6c575e76e2a9290bf6a2d73d1745ec177ecf1a6470ad71e6a1152c108fe7e609c1cb2
7
- data.tar.gz: b25fc3739c5e1412960b09c5a33109f0f7dcc2f5c3816b4b2492a0c63cbdab95fc0ad55f1ee5b83c49a24cacff032ecffd53438305f01624efda12e85e94c58b
6
+ metadata.gz: 5c8f06e168c2337ae0d0778fc1854d6fbd2bfb1459d10d83c391aadd64adf06c6d8961545a8627084d4ed86b75640270a602a962bfe82df8c29c8a7566244c58
7
+ data.tar.gz: e8d43ba83dbbfef3203cd5eee8b5964e9b70d3eaf9e1d1c269b4e4a0f3d1b38b65fccb34aa28d7f093072aaa524e842e83be517111a505520f8a730210240d05
data/README.md CHANGED
@@ -161,7 +161,7 @@ NostrZap::Error # base
161
161
  ```sh
162
162
  cd gems/nostr-zap
163
163
  bundle install
164
- bundle exec rspec # 86 specs
164
+ bundle exec rspec # 99 specs
165
165
  bundle exec rubocop
166
166
  ```
167
167
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NostrZap
4
- VERSION = '0.2.3'
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
- # - Have a 'relays' tag with at least one safe relay URL
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?
@@ -138,9 +159,17 @@ module NostrZap
138
159
  urls = relays_tag&.slice(1..)&.compact
139
160
  return fail_with("Missing required 'relays' tag with at least one relay URL") if urls.nil? || urls.empty?
140
161
 
141
- urls.all? do |url|
142
- (error = RelayUrlValidator.validate(url, check_private: false)) ? fail_with(error) : true
162
+ true
163
+ end
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)
143
171
  end
172
+ true
144
173
  end
145
174
 
146
175
  def event_id_valid?
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.3
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