Class: MixinBot::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/mixin_bot/configuration.rb

Overview

Configuration class for storing Mixin bot credentials and settings.

This class handles the configuration of bot credentials including:

  • Application ID and secret

  • Session ID and private key

  • Server public key

  • Spend key and PIN

  • API and Blaze host settings

Usage

Configure globally:

MixinBot.configure do
  app_id = 'your-app-id'
  session_id = 'your-session-id'
  session_private_key = 'your-private-key'
  server_public_key = 'server-public-key'
  spend_key = 'your-spend-key'
end

Or create a specific configuration instance:

config = MixinBot::Configuration.new(
  app_id: 'your-app-id',
  session_id: 'your-session-id',
  session_private_key: 'your-private-key',
  server_public_key: 'server-public-key'
)

Key Conversion

The configuration automatically handles key format conversions:

  • Ed25519 keys are converted from seed format (32 bytes) to full format (64 bytes)

  • Keys are converted to Curve25519 format when needed

  • Keys can be provided in various encodings (Base64, hex, etc.)

Constant Summary collapse

CONFIGURABLE_ATTRS =

List of configurable attributes.

%i[
  app_id
  client_secret
  session_id
  session_private_key
  server_public_key
  spend_key
  pin
  api_host
  blaze_host
  session_private_key_curve25519
  server_public_key_curve25519
  debug
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Configuration

Initializes a new Configuration instance.

Examples:

config = MixinBot::Configuration.new(
  app_id: '25696f85-b7b4-4509-8c3f-2684a8fc4a2a',
  session_id: '25696f85-b7b4-4509-8c3f-2684a8fc4a2a',
  session_private_key: 'base64_encoded_key',
  server_public_key: 'base64_encoded_key'
)

Parameters:

  • kwargs (Hash)

    configuration options

Options Hash (**kwargs):

  • :app_id (String)

    the application ID (or :client_id)

  • :client_secret (String)

    the client secret

  • :session_id (String)

    the session ID

  • :session_private_key (String)

    the session private key (or :private_key)

  • :server_public_key (String)

    the server public key (or :pin_token)

  • :spend_key (String)

    the spend private key

  • :pin (String)

    the PIN (defaults to spend_key if not provided)

  • :api_host (String) — default: 'api.mixin.one'

    the API host

  • :blaze_host (String) — default: 'blaze.mixin.one'

    the Blaze WebSocket host

  • :debug (Boolean) — default: false

    enable debug logging



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mixin_bot/configuration.rb', line 84

def initialize(**kwargs)
  @app_id = kwargs[:app_id] || kwargs[:client_id]
  @client_secret = kwargs[:client_secret]
  @session_id = kwargs[:session_id]
  @api_host = kwargs[:api_host] || 'api.mixin.one'
  @blaze_host = kwargs[:blaze_host] || 'blaze.mixin.one'
  @debug = kwargs[:debug] || false

  self.session_private_key = kwargs[:session_private_key] || kwargs[:private_key]
  self.server_public_key = kwargs[:server_public_key] || kwargs[:pin_token]
  self.spend_key = kwargs[:spend_key]
  self.pin = kwargs[:pin] || spend_key
end

Instance Method Details

#pin=(key) ⇒ Object

Sets the PIN with automatic format conversion.

The PIN is used for certain operations requiring additional authorization. Defaults to the spend_key if not explicitly set.

Parameters:

  • key (String)

    the PIN key



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/mixin_bot/configuration.rb', line 187

def pin=(key)
  return if key.blank?

  _private_key = decode_key key
  @pin =
    if _private_key.size == 32
      JOSE::JWA::Ed25519.keypair(_private_key).last
    else
      _private_key
    end
end

#server_public_key=(key) ⇒ Object

Sets the server public key with automatic format conversion.

Converts Ed25519 public key to Curve25519 format when needed. Handles both hex-encoded and Base64-encoded keys.

Parameters:

  • key (String)

    the server public key



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/mixin_bot/configuration.rb', line 146

def server_public_key=(key)
  return if key.blank?

  @server_public_key = decode_key key
  # HEX encoded
  @server_public_key_curve25519 =
    if key.match?(/\A\h{32,}\z/i)
      JOSE::JWA::Ed25519.pk_to_curve25519 @server_public_key
    else
      server_public_key
    end
end

#session_private_key=(key) ⇒ Object

Sets the session private key with automatic format conversion.

Handles Ed25519 key conversion:

  • If key is 32 bytes (seed), converts to 64-byte keypair

  • Automatically converts to Curve25519 format for encryption

Parameters:

  • key (String)

    the session private key in various formats



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mixin_bot/configuration.rb', line 124

def session_private_key=(key)
  return if key.blank?

  _private_key = decode_key key
  @session_private_key =
    if _private_key.size == 32
      JOSE::JWA::Ed25519.keypair(_private_key).last
    else
      _private_key
    end

  @session_private_key_curve25519 = JOSE::JWA::Ed25519.sk_to_curve25519(@session_private_key) if @session_private_key.size == 64
end

#spend_key=(key) ⇒ Object

Sets the spend key with automatic format conversion.

Used for signing transactions in the Safe API. Converts from seed format to full keypair if needed.

Parameters:

  • key (String)

    the spend private key



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/mixin_bot/configuration.rb', line 167

def spend_key=(key)
  return if key.blank?

  _private_key = decode_key key
  @spend_key =
    if _private_key.size == 32
      JOSE::JWA::Ed25519.keypair(_private_key).last
    else
      _private_key
    end
end

#valid?Boolean

Validates if the configuration has all required credentials.

Required fields are:

  • app_id

  • session_id

  • session_private_key

  • server_public_key

Returns:

  • (Boolean)

    true if all required fields are present



109
110
111
112
113
# File 'lib/mixin_bot/configuration.rb', line 109

def valid?
  %i[app_id session_id session_private_key server_public_key].all? do |attr|
    send(attr).present?
  end
end