monerorequest 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4efb26bf1513834ee0b68535033a34350a5f159adba869ff3e8682d8612cdabf
4
+ data.tar.gz: 48ee1c05ae0db4195692ecc06a14bc2a26504982a0c81c9ab8ec1ee390832cb9
5
+ SHA512:
6
+ metadata.gz: c27b72a81046b8bf050f0118f5c0cb683b7929a0921ffb592ec45388f99d6bb2c82c9695e9e95225a6a051165f7ac6c2c6bfe6d9520dd2d4f1725d89520d4a1a
7
+ data.tar.gz: 65e8f263bf3280a640471114d45658e148725c50dc88d4455c75a3620dc27f0b4e61eced65c9150427ee140f8c72948b7d8899b99a22bc530dcafa3b036605ad
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.6
7
+ NewCops: enable
8
+
9
+ Style/StringLiterals:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Style/StringLiteralsInInterpolation:
14
+ Enabled: true
15
+ EnforcedStyle: double_quotes
16
+
17
+ Layout/LineLength:
18
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-05-04
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in monerorequest.gemspec
6
+ gemspec
7
+
8
+ group :development, :test do
9
+ gem "faker", "~> 3.3"
10
+ gem "rake", "~> 13.0"
11
+ gem "rspec", "~> 3.0"
12
+ gem "rubocop", "~> 1.21"
13
+ gem "rubocop-rake", "~> 0.6"
14
+ gem "rubocop-rspec", "~> 2.29"
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,82 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ monerorequest (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ concurrent-ruby (1.2.3)
11
+ diff-lcs (1.5.1)
12
+ faker (3.3.1)
13
+ i18n (>= 1.8.11, < 2)
14
+ i18n (1.14.5)
15
+ concurrent-ruby (~> 1.0)
16
+ json (2.7.2)
17
+ language_server-protocol (3.17.0.3)
18
+ parallel (1.24.0)
19
+ parser (3.3.1.0)
20
+ ast (~> 2.4.1)
21
+ racc
22
+ racc (1.7.3)
23
+ rainbow (3.1.1)
24
+ rake (13.2.1)
25
+ regexp_parser (2.9.0)
26
+ rexml (3.2.6)
27
+ rspec (3.13.0)
28
+ rspec-core (~> 3.13.0)
29
+ rspec-expectations (~> 3.13.0)
30
+ rspec-mocks (~> 3.13.0)
31
+ rspec-core (3.13.0)
32
+ rspec-support (~> 3.13.0)
33
+ rspec-expectations (3.13.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.13.0)
36
+ rspec-mocks (3.13.0)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.13.0)
39
+ rspec-support (3.13.1)
40
+ rubocop (1.63.4)
41
+ json (~> 2.3)
42
+ language_server-protocol (>= 3.17.0)
43
+ parallel (~> 1.10)
44
+ parser (>= 3.3.0.2)
45
+ rainbow (>= 2.2.2, < 4.0)
46
+ regexp_parser (>= 1.8, < 3.0)
47
+ rexml (>= 3.2.5, < 4.0)
48
+ rubocop-ast (>= 1.31.1, < 2.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (>= 2.4.0, < 3.0)
51
+ rubocop-ast (1.31.3)
52
+ parser (>= 3.3.1.0)
53
+ rubocop-capybara (2.20.0)
54
+ rubocop (~> 1.41)
55
+ rubocop-factory_bot (2.25.1)
56
+ rubocop (~> 1.41)
57
+ rubocop-rake (0.6.0)
58
+ rubocop (~> 1.0)
59
+ rubocop-rspec (2.29.2)
60
+ rubocop (~> 1.40)
61
+ rubocop-capybara (~> 2.17)
62
+ rubocop-factory_bot (~> 2.22)
63
+ rubocop-rspec_rails (~> 2.28)
64
+ rubocop-rspec_rails (2.28.3)
65
+ rubocop (~> 1.40)
66
+ ruby-progressbar (1.13.0)
67
+ unicode-display_width (2.5.0)
68
+
69
+ PLATFORMS
70
+ x86_64-linux
71
+
72
+ DEPENDENCIES
73
+ faker (~> 3.3)
74
+ monerorequest!
75
+ rake (~> 13.0)
76
+ rspec (~> 3.0)
77
+ rubocop (~> 1.21)
78
+ rubocop-rake (~> 0.6)
79
+ rubocop-rspec (~> 2.29)
80
+
81
+ BUNDLED WITH
82
+ 2.4.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 David Havlicek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Monerorequest
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/monerorequest`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/monerorequest.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monerorequest
4
+ # class to Decode an encoded Monerorequest
5
+ class Decoder
6
+ def initialize(request)
7
+ @request = request
8
+ end
9
+
10
+ def decode
11
+ _, version, encoded_str = @request.split(":")
12
+ raise RequestVersionError, "Only Request Version 1 is supported." unless version == "1"
13
+
14
+ compressed_data = Base64.decode64(encoded_str)
15
+ json_str = Zlib::GzipReader.new(StringIO.new(compressed_data)).read
16
+ JSON.parse(json_str)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require "date"
5
+ require "json"
6
+ require "stringio"
7
+ require "uri"
8
+ require "zlib"
9
+
10
+ module Monerorequest
11
+ # class to Encode a Monerorequest hash
12
+ class Encoder
13
+ class InvalidRequest < StandardError; end
14
+
15
+ attr_accessor :request
16
+ attr_reader :errors
17
+
18
+ def initialize(request)
19
+ @request = request
20
+ @errors = []
21
+
22
+ validate!
23
+ end
24
+
25
+ def encode(version)
26
+ raise RequestVersionError, "Only Request Version 1 is supported." unless version == 1
27
+
28
+ json_str = @request.sort.to_h.to_json.force_encoding("ascii")
29
+ compressed_data = StringIO.new
30
+ gz = Zlib::GzipWriter.new(compressed_data, 9)
31
+ gz.mtime = 0
32
+ gz.write(json_str)
33
+ gz.close
34
+ encoded_str = Base64.encode64(compressed_data.string).gsub("\n", "")
35
+ "monero-request:1:#{encoded_str}"
36
+ end
37
+
38
+ private
39
+
40
+ def validate!
41
+ validate_custom_label!
42
+ validate_sellers_wallet!
43
+ validate_currency!
44
+ validate_amount!
45
+ validate_payment_id!
46
+ validate_start_date!
47
+ validate_days_per_billing_cycle!
48
+ validate_change_indicator_url!
49
+ end
50
+
51
+ def validate_custom_label!
52
+ @errors.push("custom_label must be present.") unless @request.key?("custom_label")
53
+ @errors.push("custom_label must be a String.") unless @request["custom_label"].is_a?(String)
54
+ end
55
+
56
+ def validate_sellers_wallet!
57
+ @errors.push("sellers_wallet must be present.") unless @request.key?("sellers_wallet")
58
+ return if MoneroAddress.valid?(@request["sellers_wallet"])
59
+
60
+ @errors.push("sellers_wallet must be a main Monero address.")
61
+ end
62
+
63
+ def validate_currency!
64
+ @errors.push("currency must be present.") unless @request.key?("currency")
65
+ @errors.push("currency must be a String.") unless @request["currency"].is_a?(String)
66
+ end
67
+
68
+ def validate_amount!
69
+ @errors.push("amount must be present.") unless @request.key?("amount")
70
+ @errors.push("amount must be a Numeric.") unless @request["amount"].is_a?(Numeric)
71
+ end
72
+
73
+ def validate_payment_id!
74
+ @errors.push("payment_id must be present.") unless @request.key?("payment_id")
75
+ @errors.push("payment_id must be a Monero Payment ID.") unless MoneroPaymentID.valid?(@request["payment_id"])
76
+ end
77
+
78
+ def validate_start_date!
79
+ @errors.push("start_date must be present.") unless @request.key?("start_date")
80
+ unless @request["start_date"].is_a?(String)
81
+ @errors.push("start_date must be a String.") && @request["start_date"] = ""
82
+ end
83
+ begin
84
+ DateTime.rfc3339(@request["start_date"])
85
+ rescue Date::Error
86
+ @errors.push("start_date must be an RFC3339 timestamp.")
87
+ end
88
+ end
89
+
90
+ def validate_days_per_billing_cycle!
91
+ @errors.push("days_per_billing_cycle must be present.") unless @request.key?("days_per_billing_cycle")
92
+ @errors.push("days_per_billing_cycle must be a Integer.") unless @request["days_per_billing_cycle"].is_a?(Integer)
93
+ end
94
+
95
+ def validate_change_indicator_url!
96
+ @errors.push("change_indicator_url must be present.") unless @request.key?("change_indicator_url")
97
+ unless @request["change_indicator_url"].is_a?(String)
98
+ @errors.push("change_indicator_url must be a String.") && @request["change_indicator_url"] = ""
99
+ end
100
+ return if @request["change_indicator_url"] =~ URI::DEFAULT_PARSER.make_regexp
101
+
102
+ @errors.push("change_indicator_url must be a URL.")
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monerorequest
4
+ # provide a validator for a Monero main address
5
+ class MoneroAddress
6
+ def self.valid?(address)
7
+ return false unless address.is_a?(String)
8
+ return false unless address[0] == "4"
9
+ return false unless address[1] =~ /[0-9AB]/
10
+ return false unless address.length == 95
11
+
12
+ true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monerorequest
4
+ # provide a validator for a Monero Payment ID
5
+ class MoneroPaymentID
6
+ def self.valid?(payment_id)
7
+ return false unless payment_id.is_a?(String)
8
+ return false unless payment_id.length == 16
9
+ return false unless payment_id =~ /[0-9a-f]/
10
+
11
+ true
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monerorequest
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require "date"
5
+ require "json"
6
+ require "stringio"
7
+ require "uri"
8
+ require "zlib"
9
+ require_relative "monerorequest/decoder"
10
+ require_relative "monerorequest/encoder"
11
+ require_relative "monerorequest/monero_address"
12
+ require_relative "monerorequest/monero_payment_id"
13
+ require_relative "monerorequest/version"
14
+
15
+ module Monerorequest
16
+ class RequestVersionError < StandardError; end
17
+ end
@@ -0,0 +1,4 @@
1
+ module Monerorequest
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monerorequest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Havlicek
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-05-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - snex00@protonmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - lib/monerorequest.rb
29
+ - lib/monerorequest/decoder.rb
30
+ - lib/monerorequest/encoder.rb
31
+ - lib/monerorequest/monero_address.rb
32
+ - lib/monerorequest/monero_payment_id.rb
33
+ - lib/monerorequest/version.rb
34
+ - sig/monerorequest.rbs
35
+ homepage: https://github.com/snex/monerorequest-ruby
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ allowed_push_host: https://rubygems.org
40
+ homepage_uri: https://github.com/snex/monerorequest-ruby
41
+ source_code_uri: https://github.com/snex/monerorequest-ruby
42
+ changelog_uri: https://github.com/snex/monerorequest-ruby/blob/master/CHANGELOG.md
43
+ rubygems_mfa_required: 'true'
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.6.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.4.10
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Ruby Implementation of the Monero Payment Request Standard. https://github.com/lukeprofits/Monero_Payment_Request_Standard
63
+ test_files: []