Skip to main content

💎 Ruby

The Ruby implementation of Sign in with Ethereum can be found here:

signinwithethereum/siwe-rb

Installation

Dependencies

brew install automake openssl libtool pkg-config gmp libffi

Install the gem:

gem install siwe

Usage

Creating a SIWE Message

require 'siwe'
require 'time'

# Only mandatory arguments
Siwe::Message.new("domain.example", "0x9D85ca56217D2bb651b00f15e694EB7E713637D4", "some.uri", "1")

# Complete SIWE message with default values
Siwe::Message.new("domain.example", "0x9D85ca56217D2bb651b00f15e694EB7E713637D4", "some.uri", "1", {
issued_at: Time.now.utc.iso8601,
statement: "Example statement for SIWE",
nonce: Siwe::Util.generate_nonce,
chain_id: "1",
expiration_time: "",
not_before: "",
request_id: "",
resources: []
})

Parsing a SIWE Message

# From EIP-4361 format
Siwe::Message.from_message "domain.example wants you to sign in with your Ethereum account:..."

# From JSON string
Siwe::Message.from_json_string "{\"domain\":\"domain.example\",\"address\":\"0x9D85ca56217D2bb651b00f15e694EB7E713637D4\",...}"

Verifying and Authenticating a SIWE Message

begin
message.validate(signature) # returns true if valid, throws otherwise
rescue Siwe::ExpiredMessage
# Handle expired message
rescue Siwe::InvalidSignature
# Handle invalid signature
end

Error Handling

The library provides specific exception types for different validation failures:

  • Siwe::ExpiredMessage: When the message has expired
  • Siwe::InvalidSignature: When the signature is invalid
  • Other validation errors as appropriate