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 http_timeout session_private_key_curve25519 server_public_key_curve25519 debug ].freeze
- BLAZE_ACK_POLICIES =
Acknowledgement policies for the Blaze message loop:
:on_receipt(default): ack immediately on receipt, before the handler runs (at-most-once dispatch).:after_handler: ack only after the handler completes without raising (at-least-once dispatch; unacked messages are redelivered on reconnect).
%i[on_receipt after_handler].freeze
- DEFAULT_BLAZE_ACK_POLICY =
:on_receipt
Instance Attribute Summary collapse
-
#blaze_ack_policy ⇒ Object
Returns the value of attribute blaze_ack_policy.
-
#blaze_handler ⇒ Object
Returns the value of attribute blaze_handler.
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.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mixin_bot/configuration.rb', line 97 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' @http_timeout = kwargs[:http_timeout] if @http_timeout && (@http_timeout.is_a?(Numeric) ? @http_timeout <= 0 : true) raise ArgumentError, "http_timeout must be a positive number of seconds, got #{@http_timeout.inspect}" end @debug = kwargs[:debug] || false @blaze_ack_policy = DEFAULT_BLAZE_ACK_POLICY self.blaze_handler = kwargs[:blaze_handler] self.blaze_ack_policy = kwargs[:blaze_ack_policy] 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 Attribute Details
#blaze_ack_policy ⇒ Object
Returns the value of attribute blaze_ack_policy.
71 72 73 |
# File 'lib/mixin_bot/configuration.rb', line 71 def blaze_ack_policy @blaze_ack_policy end |
#blaze_handler ⇒ Object
Returns the value of attribute blaze_handler.
71 72 73 |
# File 'lib/mixin_bot/configuration.rb', line 71 def blaze_handler @blaze_handler 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.
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/mixin_bot/configuration.rb', line 209 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.
168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/mixin_bot/configuration.rb', line 168 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
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mixin_bot/configuration.rb', line 146 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.
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/mixin_bot/configuration.rb', line 189 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
131 132 133 134 135 |
# File 'lib/mixin_bot/configuration.rb', line 131 def valid? %i[app_id session_id session_private_key server_public_key].all? do |attr| send(attr).present? end end |