siwe-rb 0.1.0

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.
data/lib/siwe/util.rb ADDED
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+ require "time"
5
+ require "date"
6
+ require "eth"
7
+
8
+ module Siwe
9
+ module Util
10
+ module_function
11
+
12
+ NONCE_LENGTH = 17
13
+
14
+ ISO8601_REGEX = /
15
+ \A
16
+ (?<date>\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01]))
17
+ [Tt]
18
+ (?:[01]\d|2[0-3]):
19
+ [0-5]\d:
20
+ (?:[0-5]\d|60)
21
+ (?:\.\d+)?
22
+ (?:[Zz]|[+-](?:[01]\d|2[0-3]):[0-5]\d)
23
+ \z
24
+ /x
25
+
26
+ def generate_nonce
27
+ SecureRandom.alphanumeric(NONCE_LENGTH)
28
+ end
29
+
30
+ def valid_iso8601?(str)
31
+ return false if str.nil? || str.empty?
32
+
33
+ m = ISO8601_REGEX.match(str)
34
+ return false if m.nil?
35
+
36
+ year, month, day = m[:date].split("-").map(&:to_i)
37
+ return false unless Date.valid_date?(year, month, day)
38
+
39
+ Time.iso8601(str)
40
+ true
41
+ rescue ArgumentError
42
+ false
43
+ end
44
+
45
+ def valid_address?(addr)
46
+ return false if addr.nil? || addr.empty?
47
+
48
+ Eth::Address.new(addr)
49
+ true
50
+ rescue StandardError
51
+ false
52
+ end
53
+
54
+ def address_case(addr)
55
+ return :invalid unless addr.is_a?(String) && addr.match?(/\A0x[0-9a-fA-F]{40}\z/)
56
+
57
+ hex = addr[2..]
58
+ return :lower if hex == hex.downcase
59
+ return :upper if hex == hex.upcase
60
+
61
+ eip55 = checksum_address(addr)
62
+ eip55 == addr ? :checksum : :invalid_checksum
63
+ end
64
+
65
+ def checksum_address(addr)
66
+ Eth::Address.new(addr).to_s
67
+ rescue StandardError
68
+ nil
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Siwe
4
+ VERSION = "0.1.0"
5
+ end
data/lib/siwe.rb ADDED
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "siwe/version"
4
+
5
+ module Siwe
6
+ autoload :Error, "siwe/error"
7
+ autoload :ErrorType, "siwe/error_type"
8
+ autoload :Response, "siwe/response"
9
+ autoload :Util, "siwe/util"
10
+ autoload :Parser, "siwe/parser"
11
+ autoload :Message, "siwe/message"
12
+ autoload :Config, "siwe/config"
13
+ autoload :Adapter, "siwe/adapter"
14
+ autoload :Rpc, "siwe/rpc"
15
+ autoload :SmartWallet, "siwe/smart_wallet"
16
+ autoload :Eip6492, "siwe/eip6492"
17
+
18
+ class << self
19
+ def configure
20
+ builder = Config::Builder.new(**config.to_h)
21
+ yield(builder) if block_given?
22
+ @config = builder.build.freeze
23
+ end
24
+
25
+ def config
26
+ @config ||= Config.new.freeze
27
+ end
28
+
29
+ def reset_config!
30
+ @config = nil
31
+ end
32
+
33
+ def generate_nonce
34
+ Util.generate_nonce
35
+ end
36
+ end
37
+ end
data/siwe-rb.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/siwe/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "siwe-rb"
7
+ spec.version = Siwe::VERSION
8
+ spec.authors = ["1001 Digital"]
9
+ spec.email = ["jalil@1001.digital"]
10
+
11
+ spec.summary = "Sign-In with Ethereum (EIP-4361) for Ruby"
12
+ spec.description = "EIP-4361 message construction, parsing, and signature verification, " \
13
+ "with built-in support for ERC-1271 and EIP-6492 smart contract wallets."
14
+ spec.homepage = "https://github.com/signinwithethereum/siwe-rb"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = ">= 3.2"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = spec.homepage
20
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
21
+ spec.metadata["rubygems_mfa_required"] = "true"
22
+
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ f == __FILE__ ||
26
+ f.start_with?("spec/", "test/", "features/", "bin/", "gems/") ||
27
+ f.match?(/\A\.(?:git|github|rubocop|ruby-version|rspec)/)
28
+ end
29
+ end
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency "eth", ">= 0.5.11", "< 1.0"
33
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: siwe-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - 1001 Digital
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: eth
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 0.5.11
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: 0.5.11
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '1.0'
32
+ description: EIP-4361 message construction, parsing, and signature verification, with
33
+ built-in support for ERC-1271 and EIP-6492 smart contract wallets.
34
+ email:
35
+ - jalil@1001.digital
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - CHANGELOG.md
41
+ - Gemfile
42
+ - Gemfile.lock
43
+ - LICENSE-MIT
44
+ - README.md
45
+ - Rakefile
46
+ - lib/siwe.rb
47
+ - lib/siwe/adapter.rb
48
+ - lib/siwe/config.rb
49
+ - lib/siwe/eip6492.rb
50
+ - lib/siwe/error.rb
51
+ - lib/siwe/error_type.rb
52
+ - lib/siwe/message.rb
53
+ - lib/siwe/parser.rb
54
+ - lib/siwe/response.rb
55
+ - lib/siwe/rpc.rb
56
+ - lib/siwe/smart_wallet.rb
57
+ - lib/siwe/util.rb
58
+ - lib/siwe/version.rb
59
+ - siwe-rb.gemspec
60
+ homepage: https://github.com/signinwithethereum/siwe-rb
61
+ licenses:
62
+ - MIT
63
+ metadata:
64
+ homepage_uri: https://github.com/signinwithethereum/siwe-rb
65
+ source_code_uri: https://github.com/signinwithethereum/siwe-rb
66
+ changelog_uri: https://github.com/signinwithethereum/siwe-rb/blob/main/CHANGELOG.md
67
+ rubygems_mfa_required: 'true'
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 4.0.3
83
+ specification_version: 4
84
+ summary: Sign-In with Ethereum (EIP-4361) for Ruby
85
+ test_files: []