Class: MixinBot::Configuration
- Inherits:
-
Object
- Object
- MixinBot::Configuration
- 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
-
#initialize(**kwargs) ⇒ Configuration
constructor
Initializes a new Configuration instance.
-
#pin=(key) ⇒ Object
Sets the PIN with automatic format conversion.
-
#server_public_key=(key) ⇒ Object
Sets the server public key with automatic format conversion.
-
#session_private_key=(key) ⇒ Object
Sets the session private key with automatic format conversion.
-
#spend_key=(key) ⇒ Object
Sets the spend key with automatic format conversion.
-
#valid? ⇒ Boolean
Validates if the configuration has all required credentials.
Constructor Details
#initialize(**kwargs) ⇒ Configuration
Initializes a new Configuration instance.
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.
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.
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
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.
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
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 |