Class: Siwe::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/siwe/parser.rb

Overview

Parser for the EIP-4361 Sign-In with Ethereum message format. Validates structure line-by-line and per-field via ABNF-aligned regexes. Returns a hash of typed fields plus an array of non-fatal warnings.

Constant Summary collapse

HEADER_SUFFIX =
" wants you to sign in with your Ethereum account:"
SCHEME_REGEX =
Util::SCHEME_REGEX
ADDRESS_REGEX =
/\A0x[0-9a-fA-F]{40}\z/
NONCE_REGEX =
/\A[A-Za-z0-9]{8,}\z/
CHAIN_REGEX =
/\A[0-9]+\z/
STATEMENT_REGEX =

ERC-4361 statement = reserved / unreserved / “ ” per RFC 3986. The regex unions ALPHA + DIGIT with the punctuation in those sets; excludes LF, “, %, and other characters outside RFC 3986. Built via Regexp.new to avoid accidental string interpolation of ‘#$&` inside a regex literal.

Regexp.new('\A[a-zA-Z0-9 !\x23\x24\x26-\x3B\x3D\x3F\x40\x5B\x5D\x5F\x7E]*\z')
REQUEST_ID_REGEX =

request-id = *pchar per ABNF — no “/” or “?”.

Util::PCHAR_REGEX

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Parser

Returns a new instance of Parser.



29
30
31
32
# File 'lib/siwe/parser.rb', line 29

def initialize(str)
  @str = str
  @warnings = []
end

Class Method Details

.parse(str) ⇒ Object



25
26
27
# File 'lib/siwe/parser.rb', line 25

def self.parse(str)
  new(str).parse
end

Instance Method Details

#parseObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/siwe/parser.rb', line 34

def parse # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  raise unable_to_parse("message is not a string") unless @str.is_a?(String)

  lines = @str.split("\n", -1)
  raise unable_to_parse("message too short") if lines.length < 6

  scheme, domain = parse_header(lines[0])
  address = parse_address(lines[1])

  uri_idx = lines.index { |l| l.start_with?("URI: ") }
  raise unable_to_parse("URI: line not found") if uri_idx.nil? || uri_idx < 3

  statement = parse_statement_block(lines[2...uri_idx])

  uri        = parse_uri_field(lines[uri_idx], "URI: ")
  version    = parse_field(lines[uri_idx + 1], "Version: ",   :invalid_message_version, /\A1\z/)
  chain_str  = parse_field(lines[uri_idx + 2], "Chain ID: ",  :unable_to_parse,         CHAIN_REGEX)
  nonce      = parse_field(lines[uri_idx + 3], "Nonce: ",     :invalid_nonce,           NONCE_REGEX)
  issued_at  = parse_iso_field(lines[uri_idx + 4], "Issued At: ", "issuedAt")

  cursor = uri_idx + 5
  expiration_time = nil
  not_before = nil
  request_id = nil
  resources = nil

  if cursor < lines.length && lines[cursor]&.start_with?("Expiration Time: ")
    expiration_time = parse_iso_field(lines[cursor], "Expiration Time: ", "expirationTime")
    cursor += 1
  end
  if cursor < lines.length && lines[cursor]&.start_with?("Not Before: ")
    not_before = parse_iso_field(lines[cursor], "Not Before: ", "notBefore")
    cursor += 1
  end
  if cursor < lines.length && lines[cursor]&.start_with?("Request ID: ")
    request_id = parse_field(lines[cursor], "Request ID: ", :unable_to_parse, REQUEST_ID_REGEX)
    cursor += 1
  end
  if cursor < lines.length && lines[cursor] == "Resources:"
    cursor += 1
    resources = parse_resources(lines, cursor)
    cursor += resources.length
  end

  validate_trailing(lines, cursor)
  classify_address!(address)

  {
    fields: {
      scheme: scheme,
      domain: domain,
      address: address,
      statement: statement,
      uri: uri,
      version: version,
      chain_id: chain_str.to_i,
      nonce: nonce,
      issued_at: issued_at,
      expiration_time: expiration_time,
      not_before: not_before,
      request_id: request_id,
      resources: resources
    },
    warnings: @warnings
  }
end