siwe 0.1.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6672768ccc989b67fd335a571585a7bbae2f88ccd8c095f7332a308f39037dab
4
- data.tar.gz: 3612d9b498a36fa37e0cb70e81e2ae64450baf34d7ef9d75cc666957957c15de
3
+ metadata.gz: 3ee917f6a6a6cfe1877f32dd8c70cd2628b745c6a941f7d6bcaebd1e0c19c592
4
+ data.tar.gz: 4162a2428f5a0efce7d190a1ca1bbff06c3574447853fa9dfae183301f084ab7
5
5
  SHA512:
6
- metadata.gz: 6f42e272519ac16310a8ee9bf75139e121acaf52b446b0ed35e8868bb17bfcb816caced089dc7e342e5a686a1ab532cab6a9d385a3b80cbc7b6a15c668e019aa
7
- data.tar.gz: 93c9e1a87a4dae94562f89b44de590f03a23c673dfb30962f2cd47c52aa4547f3b8c0a0b0a3263773b4113783dcfcd97154de7d9401b689935dfa3da671b001a
6
+ metadata.gz: 648ec9080c223f0ffea368c522f4a29aa5f9b3203fe91a090133ca4143d7d637bd9b7f586de5883a2ed3e9a06473aaedb8e57ff94aa67ffc45462681cb259500
7
+ data.tar.gz: e3212a3795f6dd22d584d77d59eacfa3150aa0ddf284f30f85ea129253f1f3d77afe4c1d049df8f655f50f3dafbe2748ce0df7468af562aa9f69768dd858da9d
data/Gemfile.lock CHANGED
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- siwe (0.1.4)
5
- eth (~> 0.5.0)
4
+ siwe (1.0.0)
5
+ eth (~> 0.5.1)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
@@ -12,17 +12,17 @@ GEM
12
12
  benchmark (0.2.0)
13
13
  diff-lcs (1.5.0)
14
14
  e2mmap (0.1.0)
15
- eth (0.5.0)
15
+ eth (0.5.1)
16
16
  keccak (~> 1.3)
17
17
  konstructor (~> 1.0)
18
- openssl (~> 3.0)
18
+ openssl (~> 2.2)
19
19
  rbsecp256k1 (~> 5.1)
20
- rlp (~> 0.7)
21
20
  scrypt (~> 3.0)
22
21
  ffi (1.15.5)
23
22
  ffi-compiler (1.0.1)
24
23
  ffi (>= 1.0.0)
25
24
  rake
25
+ ipaddr (1.2.3)
26
26
  jaro_winkler (1.5.4)
27
27
  keccak (1.3.0)
28
28
  konstructor (1.0.2)
@@ -33,7 +33,8 @@ GEM
33
33
  mini_portile2 (2.7.1)
34
34
  nokogiri (1.13.1-x86_64-linux)
35
35
  racc (~> 1.4)
36
- openssl (3.0.0)
36
+ openssl (2.2.1)
37
+ ipaddr
37
38
  parallel (1.21.0)
38
39
  parser (3.1.0.0)
39
40
  ast (~> 2.4.1)
@@ -49,7 +50,6 @@ GEM
49
50
  reverse_markdown (2.1.1)
50
51
  nokogiri
51
52
  rexml (3.2.5)
52
- rlp (0.7.3)
53
53
  rspec (3.10.0)
54
54
  rspec-core (~> 3.10.0)
55
55
  rspec-expectations (~> 3.10.0)
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Siwe
4
+ # Used when the message is already expired. (Expires At < Time.now)
5
+ class ExpiredMessage < StandardError
6
+ def initialize(msg = "Message expired.")
7
+ super
8
+ end
9
+ end
10
+
11
+ # Used when the message is not yet valid. (Not Before > Time.now)
12
+ class NotValidMessage < StandardError
13
+ def initialize(msg = "Message not yet valid.")
14
+ super
15
+ end
16
+ end
17
+
18
+ # Used when the signature doesn't correspond to the address of the message.
19
+ class InvalidSignature < StandardError
20
+ def initialize(msg = "Signature doesn't match message.")
21
+ super
22
+ end
23
+ end
24
+ end
data/lib/siwe/message.rb CHANGED
@@ -74,9 +74,6 @@ module Siwe
74
74
  # expressed as RFC 3986 URIs separated by `\n- `.
75
75
  attr_accessor :resources
76
76
 
77
- # Signature of the message signed by the wallet.
78
- attr_accessor :signature
79
-
80
77
  def initialize(domain, address, uri, version, options = {})
81
78
  @domain = domain
82
79
  @address = address
@@ -91,13 +88,11 @@ module Siwe
91
88
  @not_before = options.fetch :not_before, ""
92
89
  @request_id = options.fetch :request_id, ""
93
90
  @resources = options.fetch :resources, []
94
- @signature = options.fetch :signature, ""
95
- validate(true)
96
91
  end
97
92
 
98
93
  def self.from_message(msg)
99
94
  if (message = msg.match SIWE_MESSAGE)
100
- msg = new(
95
+ new(
101
96
  message[:domain],
102
97
  message[:address],
103
98
  message[:uri],
@@ -113,8 +108,7 @@ module Siwe
113
108
  resources: message[:resources]&.split("\n- ")&.drop(1) || []
114
109
  }
115
110
  )
116
- msg.validate(true)
117
- msg
111
+
118
112
  else
119
113
  throw "Invalid message input."
120
114
  end
@@ -133,15 +127,14 @@ module Siwe
133
127
  expiration_time: @expiration_time,
134
128
  not_before: @not_before,
135
129
  request_id: @request_id,
136
- resources: @resources,
137
- signature: @signature
130
+ resources: @resources
138
131
  }
139
132
  obj.to_json
140
133
  end
141
134
 
142
135
  def self.from_json_string(str)
143
136
  obj = JSON.parse str, { symbolize_names: true }
144
- msg = Siwe::Message.new(
137
+ Siwe::Message.new(
145
138
  obj[:domain],
146
139
  obj[:address],
147
140
  obj[:uri],
@@ -153,30 +146,30 @@ module Siwe
153
146
  expiration_time: obj[:expiration_time],
154
147
  not_before: obj[:not_before],
155
148
  request_id: obj[:request_id],
156
- resources: obj[:resources],
157
- signature: obj[:signature]
149
+ resources: obj[:resources]
158
150
  }
159
151
  )
160
- msg.validate(true)
161
- msg
162
152
  end
163
153
 
164
- def validate(skip_signature = false)
165
- raise "Message expired." if !@expiration_time.empty? && Time.now.utc > Time.parse(@expiration_time)
166
- raise "Message not yet valid." if !@not_before.empty? && Time.now.utc < Time.parse(@not_before)
154
+ def validate(signature)
155
+ raise Siwe::ExpiredMessage if !@expiration_time.empty? && Time.now.utc > Time.parse(@expiration_time)
156
+ raise Siwe::NotValidMessage if !@not_before.empty? && Time.now.utc < Time.parse(@not_before)
167
157
 
168
- unless skip_signature
169
- raise "Missing signature field." if @signature.empty?
158
+ raise Siwe::InvalidSignature if signature.empty?
170
159
 
171
- pub_key = Eth::Signature.personal_recover personal_sign, @signature
160
+ begin
161
+ pub_key = Eth::Signature.personal_recover prepare_message, signature
172
162
  signature_address = Eth::Util.public_key_to_address pub_key
173
- raise "Signature doesn't match message." unless signature_address.to_s.downcase.eql? @address.to_s.downcase
163
+ rescue StandardError
164
+ raise Siwe::InvalidSignature
174
165
  end
175
166
 
167
+ raise Siwe::InvalidSignature unless signature_address.to_s.downcase.eql? @address.to_s.downcase
168
+
176
169
  true
177
170
  end
178
171
 
179
- def personal_sign
172
+ def prepare_message
180
173
  greeting = "#{@domain} wants you to sign in with your Ethereum account:"
181
174
  address = @address
182
175
  statement = "\n#{@statement}\n"
data/lib/siwe/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Siwe
4
- VERSION = "0.1.5"
4
+ VERSION = "1.0.0"
5
5
  end
data/lib/siwe.rb CHANGED
@@ -6,6 +6,9 @@ require_relative "siwe/version"
6
6
  module Siwe
7
7
  autoload :Message, "siwe/message"
8
8
  autoload :Util, "siwe/util"
9
+ autoload :ExpiredMessage, "siwe/exceptions"
10
+ autoload :NotValidMessage, "siwe/exceptions"
11
+ autoload :InvalidSignature, "siwe/exceptions"
9
12
 
10
13
  class Error < StandardError; end
11
14
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: siwe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Spruce Systems Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-24 00:00:00.000000000 Z
11
+ date: 2022-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eth
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.5.0
19
+ version: 0.5.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.5.0
26
+ version: 0.5.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -101,6 +101,7 @@ files:
101
101
  - bin/console
102
102
  - bin/setup
103
103
  - lib/siwe.rb
104
+ - lib/siwe/exceptions.rb
104
105
  - lib/siwe/message.rb
105
106
  - lib/siwe/util.rb
106
107
  - lib/siwe/version.rb