mixin_bot 3.0.3 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/AGENTS.md +12 -2
- data/CHANGELOG.md +135 -0
- data/README.md +3 -2
- data/docs/agent/cli.md +4 -2
- data/lib/mixin_bot/address.rb +73 -62
- data/lib/mixin_bot/api/attachment.rb +18 -8
- data/lib/mixin_bot/api/blaze.rb +7 -2
- data/lib/mixin_bot/api/blaze_async.rb +169 -4
- data/lib/mixin_bot/api/computer_api.rb +7 -7
- data/lib/mixin_bot/api/encrypted_message.rb +75 -52
- data/lib/mixin_bot/api/multisig.rb +9 -7
- data/lib/mixin_bot/api/pin.rb +9 -8
- data/lib/mixin_bot/api/tip.rb +1 -1
- data/lib/mixin_bot/api/transaction.rb +70 -41
- data/lib/mixin_bot/api/transfer.rb +3 -2
- data/lib/mixin_bot/api/user.rb +6 -6
- data/lib/mixin_bot/api.rb +1 -0
- data/lib/mixin_bot/bot_auth.rb +1 -1
- data/lib/mixin_bot/cli/schema.rb +2 -0
- data/lib/mixin_bot/cli.rb +9 -3
- data/lib/mixin_bot/client.rb +43 -19
- data/lib/mixin_bot/computer.rb +39 -23
- data/lib/mixin_bot/configuration.rb +48 -8
- data/lib/mixin_bot/invoice.rb +126 -77
- data/lib/mixin_bot/nfo.rb +54 -43
- data/lib/mixin_bot/proxy.rb +184 -0
- data/lib/mixin_bot/transaction/decoder.rb +201 -138
- data/lib/mixin_bot/transaction/encoder.rb +156 -118
- data/lib/mixin_bot/transaction.rb +15 -1
- data/lib/mixin_bot/utils/address.rb +1 -1
- data/lib/mixin_bot/utils/amount.rb +25 -0
- data/lib/mixin_bot/utils/base58.rb +97 -0
- data/lib/mixin_bot/utils/crypto.rb +188 -56
- data/lib/mixin_bot/utils/cursor.rb +147 -0
- data/lib/mixin_bot/utils/decoder.rb +111 -6
- data/lib/mixin_bot/utils/encoder.rb +90 -21
- data/lib/mixin_bot/utils/options.rb +26 -0
- data/lib/mixin_bot/utils/sodium.rb +169 -0
- data/lib/mixin_bot/utils.rb +17 -2
- data/lib/mixin_bot/uuid.rb +6 -13
- data/lib/mixin_bot/version.rb +1 -1
- data/lib/mixin_bot.rb +1 -3
- data/lib/mvm/client.rb +44 -9
- data/lib/mvm/registry.rb +45 -17
- data/llms.txt +1 -1
- metadata +6 -44
- data/lib/mixin_bot/transaction/buffer.rb +0 -34
|
@@ -7,8 +7,9 @@ module MixinBot
|
|
|
7
7
|
OUTPUT_TYPE_SCRIPT = 0x00
|
|
8
8
|
OUTPUT_TYPE_WITHDRAW_SUBMIT = 0xa1
|
|
9
9
|
XIN_ASSET_ID = 'c94ac88f-4671-3976-b60a-09064f1811e8'
|
|
10
|
-
EXTRA_SIZE_STORAGE_CAPACITY =
|
|
11
|
-
EXTRA_STORAGE_PRICE_STEP = 0.0001
|
|
10
|
+
EXTRA_SIZE_STORAGE_CAPACITY = MixinBot::Transaction::SAFE_MAX_EXTRA_SIZE
|
|
11
|
+
EXTRA_STORAGE_PRICE_STEP = BigDecimal('0.0001')
|
|
12
|
+
EXTRA_SIZE_GENERAL_LIMIT = 256
|
|
12
13
|
|
|
13
14
|
def create_safe_keys(*payload, access_token: nil)
|
|
14
15
|
raise ArgumentError, 'payload should be an array' unless payload.is_a? Array
|
|
@@ -37,22 +38,7 @@ module MixinBot
|
|
|
37
38
|
next if recipient[:mix_address].blank?
|
|
38
39
|
|
|
39
40
|
if recipient[:members].all?(&->(m) { m.start_with? MixinBot::MAIN_ADDRESS_PREFIX })
|
|
40
|
-
|
|
41
|
-
gk = {
|
|
42
|
-
mask: key[0].unpack1('H*'),
|
|
43
|
-
keys: []
|
|
44
|
-
}
|
|
45
|
-
recipient[:members].each do |member|
|
|
46
|
-
payload = MixinBot.utils.parse_main_address member
|
|
47
|
-
spend_key = payload[0...32]
|
|
48
|
-
view_key = payload[-32..]
|
|
49
|
-
|
|
50
|
-
ghost_public_key = MixinBot.utils.derive_ghost_public_key(key[1], view_key, spend_key, index)
|
|
51
|
-
|
|
52
|
-
gk[:keys] << ghost_public_key.unpack1('H*')
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
ghost_keys[index] = gk.with_indifferent_access
|
|
41
|
+
ghost_keys[index] = MixinBot.utils.generate_xin_ghost_keys(recipient[:members], index).with_indifferent_access
|
|
56
42
|
|
|
57
43
|
elsif recipient[:members].none?(&->(m) { m.start_with? MixinBot::MAIN_ADDRESS_PREFIX })
|
|
58
44
|
uuid_recipients.push(
|
|
@@ -110,6 +96,11 @@ module MixinBot
|
|
|
110
96
|
raise ArgumentError, 'utxos too many' if utxos.size > 256
|
|
111
97
|
|
|
112
98
|
recipients = receivers.map do |receiver|
|
|
99
|
+
if receiver[:destination].present?
|
|
100
|
+
next { destination: receiver[:destination], tag: receiver[:tag].to_s,
|
|
101
|
+
amount: MixinBot.utils.safe_amount(receiver[:amount]) }.with_indifferent_access
|
|
102
|
+
end
|
|
103
|
+
|
|
113
104
|
MixinBot.utils.build_safe_recipient(
|
|
114
105
|
members: receiver[:members],
|
|
115
106
|
threshold: receiver[:threshold],
|
|
@@ -117,16 +108,21 @@ module MixinBot
|
|
|
117
108
|
).with_indifferent_access
|
|
118
109
|
end
|
|
119
110
|
|
|
120
|
-
inputs_sum = utxos.sum
|
|
121
|
-
outputs_sum = recipients.sum
|
|
111
|
+
inputs_sum = utxos.sum { |utxo| MixinBot.utils.safe_amount_units(utxo['amount']) }
|
|
112
|
+
outputs_sum = recipients.sum { |recipient| MixinBot.utils.safe_amount_units(recipient['amount']) }
|
|
122
113
|
change = inputs_sum - outputs_sum
|
|
123
|
-
|
|
114
|
+
if change.negative?
|
|
115
|
+
inputs_amount = MixinBot.utils.format_safe_amount(inputs_sum)
|
|
116
|
+
outputs_amount = MixinBot.utils.format_safe_amount(outputs_sum)
|
|
117
|
+
raise InsufficientBalanceError, "inputs sum #{inputs_amount} < outputs sum #{outputs_amount}"
|
|
118
|
+
end
|
|
124
119
|
|
|
125
120
|
if change.positive?
|
|
121
|
+
change_receiver = kwargs[:change_receiver]&.with_indifferent_access
|
|
126
122
|
recipients << MixinBot.utils.build_safe_recipient(
|
|
127
|
-
members: utxos.first['receivers'],
|
|
128
|
-
threshold: utxos.first['receivers_threshold'],
|
|
129
|
-
amount: change
|
|
123
|
+
members: change_receiver ? change_receiver[:members] : utxos.first['receivers'],
|
|
124
|
+
threshold: change_receiver ? change_receiver[:threshold] : utxos.first['receivers_threshold'],
|
|
125
|
+
amount: MixinBot.utils.format_safe_amount(change)
|
|
130
126
|
).with_indifferent_access
|
|
131
127
|
end
|
|
132
128
|
raise ArgumentError, 'recipients too many' if recipients.size > 256
|
|
@@ -142,6 +138,20 @@ module MixinBot
|
|
|
142
138
|
end
|
|
143
139
|
|
|
144
140
|
asset = mixin_asset_for.call(utxos[0])
|
|
141
|
+
extra = (kwargs[:extra] || '').to_s
|
|
142
|
+
if extra.bytesize > EXTRA_SIZE_GENERAL_LIMIT
|
|
143
|
+
storage = recipients.select do |recipient|
|
|
144
|
+
recipient[:threshold] == 64 && recipient[:members]&.one? && recipient[:members].first.start_with?(MAIN_ADDRESS_PREFIX)
|
|
145
|
+
end
|
|
146
|
+
storage_amount = storage.map { |recipient| recipient[:amount].to_d }.max
|
|
147
|
+
unless storage_amount && asset == SHA3::Digest::SHA3_256.hexdigest(XIN_ASSET_ID)
|
|
148
|
+
raise ArgumentError, "extra exceeds general transaction limit (#{EXTRA_SIZE_GENERAL_LIMIT} bytes)"
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
paid_limit = (storage_amount / EXTRA_STORAGE_PRICE_STEP).floor * 1024
|
|
152
|
+
raise ArgumentError, 'extra exceeds paid storage capacity' if extra.bytesize > [paid_limit, EXTRA_SIZE_STORAGE_CAPACITY].min
|
|
153
|
+
end
|
|
154
|
+
|
|
145
155
|
inputs = []
|
|
146
156
|
utxos.each do |utxo|
|
|
147
157
|
raise ArgumentError, 'utxo asset not match' unless mixin_asset_for.call(utxo) == asset
|
|
@@ -152,7 +162,7 @@ module MixinBot
|
|
|
152
162
|
}
|
|
153
163
|
end
|
|
154
164
|
|
|
155
|
-
ghosts = generate_safe_keys(recipients)
|
|
165
|
+
ghosts = kwargs[:ghosts] || generate_safe_keys(recipients)
|
|
156
166
|
|
|
157
167
|
outputs = []
|
|
158
168
|
recipients.each_with_index do |recipient, index|
|
|
@@ -166,11 +176,14 @@ module MixinBot
|
|
|
166
176
|
}
|
|
167
177
|
}
|
|
168
178
|
else
|
|
179
|
+
ghost = ghosts[index]&.with_indifferent_access
|
|
180
|
+
raise InvalidTransactionFormatError, "missing ghost keys for output #{index}" unless ghost
|
|
181
|
+
|
|
169
182
|
{
|
|
170
183
|
type: OUTPUT_TYPE_SCRIPT,
|
|
171
184
|
amount: recipient['amount'],
|
|
172
|
-
keys:
|
|
173
|
-
mask:
|
|
185
|
+
keys: ghost['keys'],
|
|
186
|
+
mask: ghost['mask'],
|
|
174
187
|
script: build_threshold_script(recipient['threshold'])
|
|
175
188
|
}
|
|
176
189
|
end
|
|
@@ -181,7 +194,7 @@ module MixinBot
|
|
|
181
194
|
asset:,
|
|
182
195
|
inputs:,
|
|
183
196
|
outputs:,
|
|
184
|
-
extra
|
|
197
|
+
extra:,
|
|
185
198
|
references: kwargs[:references] || []
|
|
186
199
|
}
|
|
187
200
|
end
|
|
@@ -191,8 +204,6 @@ module MixinBot
|
|
|
191
204
|
r = r.with_indifferent_access
|
|
192
205
|
{ request_id: r[:request_id], raw: r[:raw] }
|
|
193
206
|
end
|
|
194
|
-
create_safe_transaction_request(requests.first[:request_id], requests.first[:raw]) if requests.one?
|
|
195
|
-
|
|
196
207
|
path = '/safe/transaction/requests'
|
|
197
208
|
client.post path, *requests
|
|
198
209
|
end
|
|
@@ -276,26 +287,39 @@ module MixinBot
|
|
|
276
287
|
end
|
|
277
288
|
|
|
278
289
|
raw = kwargs[:raw]
|
|
279
|
-
|
|
290
|
+
transaction = MixinBot::Transaction.new(hex: raw).decode
|
|
291
|
+
raise ArgumentError, 'expected an unsigned transaction' if transaction.signatures.present? || transaction.aggregated
|
|
292
|
+
|
|
293
|
+
encoder = MixinBot::Transaction::Encoder.new(transaction)
|
|
294
|
+
unless encoder.encode(compute_hash: false).hex.casecmp?(raw)
|
|
295
|
+
raise ArgumentError, 'transaction does not have canonical unsigned bytes'
|
|
296
|
+
end
|
|
297
|
+
|
|
280
298
|
utxos = kwargs[:utxos]
|
|
281
299
|
request = kwargs[:request]
|
|
282
300
|
spend_key = MixinBot.utils.decode_key(kwargs[:spend_key]) || config.spend_key
|
|
283
301
|
spend_key = Digest::SHA512.digest spend_key[...32]
|
|
284
302
|
|
|
285
|
-
msg = [raw].pack('H*')
|
|
303
|
+
msg = transaction.safe_wire? ? [transaction.hash].pack('H*') : Digest::Blake3.digest([raw].pack('H*'))
|
|
286
304
|
|
|
287
305
|
y_scalar = JOSE::JWA::FieldElement.new(
|
|
288
306
|
JOSE::JWA::X25519.clamp_scalar(spend_key[...32]).x,
|
|
289
307
|
JOSE::JWA::Edwards25519Point::L
|
|
290
308
|
)
|
|
291
309
|
|
|
292
|
-
|
|
293
|
-
|
|
310
|
+
transaction.signatures = []
|
|
311
|
+
transaction.inputs.each_with_index do |input, index|
|
|
294
312
|
utxo = utxos[index]
|
|
295
|
-
|
|
313
|
+
unless utxo && input['hash'] == utxo['transaction_hash'] && input['index'] == utxo['output_index']
|
|
314
|
+
raise ArgumentError, 'utxo not match'
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
view_hex = request['views']&.[](index)
|
|
318
|
+
raise ArgumentError, 'invalid transaction view scalar' unless view_hex.is_a?(String) && view_hex.match?(/\A\h{64}\z/)
|
|
296
319
|
|
|
297
|
-
view = [
|
|
320
|
+
view = [view_hex].pack('H*')
|
|
298
321
|
x_scalar = MixinBot.utils.scalar_from_bytes(view)
|
|
322
|
+
raise ArgumentError, 'invalid transaction view scalar' unless view == x_scalar.to_bytes(256)
|
|
299
323
|
|
|
300
324
|
t_scalar = x_scalar + y_scalar
|
|
301
325
|
key = t_scalar.to_bytes(JOSE::JWA::Edwards25519Point::B)
|
|
@@ -304,14 +328,19 @@ module MixinBot
|
|
|
304
328
|
key_index = utxo['keys'].index pub.unpack1('H*')
|
|
305
329
|
raise ArgumentError, 'cannot find valid key' unless key_index.is_a? Integer
|
|
306
330
|
|
|
307
|
-
signature = MixinBot.utils.
|
|
331
|
+
signature = MixinBot.utils.sign_transaction_hash(msg, key:, public_key: pub)
|
|
308
332
|
signature = signature.unpack1('H*')
|
|
309
333
|
sig = {}
|
|
310
334
|
sig[key_index] = signature
|
|
311
|
-
|
|
335
|
+
transaction.signatures << sig
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
signature_bytes = encoder.signature_bytes
|
|
339
|
+
if transaction.safe_wire? && (raw.bytesize / 2) - 2 + signature_bytes.bytesize > transaction.max_extra_size
|
|
340
|
+
raise ArgumentError, 'signed transaction is too large'
|
|
312
341
|
end
|
|
313
342
|
|
|
314
|
-
|
|
343
|
+
raw[...-4].downcase + signature_bytes.unpack1('H*')
|
|
315
344
|
end
|
|
316
345
|
|
|
317
346
|
def build_object_transaction(extra, **)
|
|
@@ -321,10 +350,10 @@ module MixinBot
|
|
|
321
350
|
# calculate fee base on extra length
|
|
322
351
|
amount = EXTRA_STORAGE_PRICE_STEP * ((extra.bytesize / 1024) + 1)
|
|
323
352
|
|
|
324
|
-
#
|
|
353
|
+
# Kernel storage recipient
|
|
325
354
|
receivers = [
|
|
326
355
|
{
|
|
327
|
-
members: [MixinBot
|
|
356
|
+
members: [MixinBot::MainAddress::STORAGE_ADDRESS],
|
|
328
357
|
threshold: 64,
|
|
329
358
|
amount:
|
|
330
359
|
}
|
|
@@ -117,8 +117,9 @@ module MixinBot
|
|
|
117
117
|
|
|
118
118
|
raise ArgumentError, 'utxos or asset_id required' if utxos.blank? && asset_id.blank?
|
|
119
119
|
|
|
120
|
-
amount
|
|
121
|
-
|
|
120
|
+
raise ArgumentError, 'amount required' if kwargs[:amount].blank?
|
|
121
|
+
|
|
122
|
+
amount = MixinBot.utils.safe_amount(kwargs[:amount]).to_d
|
|
122
123
|
|
|
123
124
|
members = [kwargs[:members]].flatten.compact
|
|
124
125
|
raise ArgumentError, 'members required' if members.blank?
|
data/lib/mixin_bot/api/user.rb
CHANGED
|
@@ -51,7 +51,7 @@ module MixinBot
|
|
|
51
51
|
def create_user(full_name, key: nil, force: false, increment: CREATE_USER_BILLING_INCREMENT)
|
|
52
52
|
ensure_app_billing_credit!(force:, increment:)
|
|
53
53
|
|
|
54
|
-
keypair =
|
|
54
|
+
keypair = MixinBot.utils.ed25519_keypair key
|
|
55
55
|
session_secret = Base64.urlsafe_encode64 keypair[0], padding: false
|
|
56
56
|
private_key = keypair[1].unpack1('H*')
|
|
57
57
|
|
|
@@ -108,8 +108,8 @@ module MixinBot
|
|
|
108
108
|
# preflight fails
|
|
109
109
|
#
|
|
110
110
|
def create_safe_user(name, private_key: nil, spend_key: nil, force: false, increment: CREATE_USER_BILLING_INCREMENT)
|
|
111
|
-
session_keypair =
|
|
112
|
-
spend_keypair =
|
|
111
|
+
session_keypair = MixinBot.utils.ed25519_keypair private_key
|
|
112
|
+
spend_keypair = MixinBot.utils.ed25519_keypair spend_key
|
|
113
113
|
|
|
114
114
|
spend_key_hex = spend_keypair[1].unpack1('H*')
|
|
115
115
|
|
|
@@ -157,7 +157,7 @@ module MixinBot
|
|
|
157
157
|
spend_key_bytes = MixinBot.utils.decode_key spend_key
|
|
158
158
|
raise ArgumentError, 'invalid spend_key' if spend_key_bytes.nil? || spend_key_bytes.size < 32
|
|
159
159
|
|
|
160
|
-
keypair =
|
|
160
|
+
keypair = MixinBot.utils.ed25519_keypair spend_key_bytes[...32]
|
|
161
161
|
public_key = keypair[0].unpack1('H*')
|
|
162
162
|
# Normalize to a 64-byte signing key in hex so that callers may pass a
|
|
163
163
|
# 32-byte seed without crashing the downstream signer.
|
|
@@ -168,7 +168,7 @@ module MixinBot
|
|
|
168
168
|
# match. See bot-api-go-client safe_user.go +RegisterSafeBareUser+.
|
|
169
169
|
app_id_hash = SHA3::Digest::SHA3_256.hexdigest config.app_id
|
|
170
170
|
signature = Base64.urlsafe_encode64(
|
|
171
|
-
|
|
171
|
+
MixinBot.utils.ed25519_sign([app_id_hash].pack('H*'), keypair[1]),
|
|
172
172
|
padding: false
|
|
173
173
|
)
|
|
174
174
|
|
|
@@ -204,7 +204,7 @@ module MixinBot
|
|
|
204
204
|
profile = me['data']
|
|
205
205
|
return true if profile['has_safe']
|
|
206
206
|
|
|
207
|
-
spend_keypair =
|
|
207
|
+
spend_keypair = MixinBot.utils.ed25519_keypair spend_key
|
|
208
208
|
spend_key_hex = spend_keypair[1].unpack1('H*')
|
|
209
209
|
|
|
210
210
|
if profile['tip_key_base64'].blank?
|
data/lib/mixin_bot/api.rb
CHANGED
|
@@ -167,6 +167,7 @@ module MixinBot
|
|
|
167
167
|
# @option kwargs [String] :session_private_key the session private key
|
|
168
168
|
# @option kwargs [String] :server_public_key the server public key
|
|
169
169
|
# @option kwargs [String] :spend_key the spend private key (for Safe API)
|
|
170
|
+
# @option kwargs [String, URI, Hash] :proxy HTTP/HTTPS proxy URL or options hash
|
|
170
171
|
#
|
|
171
172
|
# @example Using global configuration
|
|
172
173
|
# api = MixinBot::API.new
|
data/lib/mixin_bot/bot_auth.rb
CHANGED
|
@@ -49,7 +49,7 @@ module MixinBot
|
|
|
49
49
|
|
|
50
50
|
u_pk = Base64.urlsafe_decode64(session['public_key'])
|
|
51
51
|
sk = @api.config.session_private_key_curve25519
|
|
52
|
-
shared =
|
|
52
|
+
shared = MixinBot.utils.x25519(sk, u_pk[0, 32])
|
|
53
53
|
@cache.put(user_id, shared)
|
|
54
54
|
@cache.put("#{PLATFORM_PREFIX}#{user_id}", session['platform'].to_s.b) if session['platform']
|
|
55
55
|
shared
|
data/lib/mixin_bot/cli/schema.rb
CHANGED
|
@@ -254,6 +254,8 @@ module MixinBot
|
|
|
254
254
|
global_args = [
|
|
255
255
|
{ 'name' => '--apihost', 'type' => 'string', 'required' => false, 'default' => 'api.mixin.one',
|
|
256
256
|
'aliases' => ['-a'] },
|
|
257
|
+
{ 'name' => '--proxy', 'type' => 'string', 'required' => false,
|
|
258
|
+
'description' => 'HTTP/HTTPS proxy URL (e.g. http://127.0.0.1:7890)' },
|
|
257
259
|
{ 'name' => '--output', 'type' => 'string', 'required' => false,
|
|
258
260
|
'enum' => CLIOutput::OUTPUT_FORMATS, 'aliases' => ['-o'],
|
|
259
261
|
'description' => 'Output format; defaults to pretty in TTY, json when piped' },
|
data/lib/mixin_bot/cli.rb
CHANGED
|
@@ -19,6 +19,7 @@ module MixinBot
|
|
|
19
19
|
UI = ::CLI::UI
|
|
20
20
|
|
|
21
21
|
class_option :apihost, type: :string, aliases: '-a', default: 'api.mixin.one', desc: 'Specify mixin api host'
|
|
22
|
+
class_option :proxy, type: :string, desc: 'HTTP/HTTPS proxy URL (e.g. http://127.0.0.1:7890)'
|
|
22
23
|
class_option :output, type: :string, aliases: '-o', enum: CLIOutput::OUTPUT_FORMATS,
|
|
23
24
|
desc: 'Output format: pretty, json, yaml (default: pretty in TTY, json when piped)'
|
|
24
25
|
class_option :pretty, type: :boolean, aliases: '-r', default: true,
|
|
@@ -41,6 +42,7 @@ module MixinBot
|
|
|
41
42
|
|
|
42
43
|
def setup_api_instance!
|
|
43
44
|
MixinBot.config.api_host = options[:apihost] if options[:apihost].present?
|
|
45
|
+
MixinBot.config.proxy = options[:proxy] if options[:proxy].present?
|
|
44
46
|
|
|
45
47
|
if options[:keystore].blank?
|
|
46
48
|
@api_instance = MixinBot::API.new
|
|
@@ -76,15 +78,19 @@ module MixinBot
|
|
|
76
78
|
end
|
|
77
79
|
|
|
78
80
|
def build_api_from_keystore(store)
|
|
79
|
-
|
|
81
|
+
attrs = {
|
|
80
82
|
app_id: store['app_id'] || store['client_id'],
|
|
81
83
|
session_id: store['session_id'],
|
|
82
84
|
server_public_key: store['server_public_key'] || store['pin_token'],
|
|
83
85
|
session_private_key: store['session_private_key'] || store['private_key'],
|
|
84
86
|
spend_key: store['spend_key'],
|
|
85
87
|
client_secret: store['client_secret'],
|
|
86
|
-
pin: store['pin']
|
|
87
|
-
|
|
88
|
+
pin: store['pin'],
|
|
89
|
+
api_host: options[:apihost].presence || store['api_host'],
|
|
90
|
+
proxy: options[:proxy].presence || store['proxy'].presence || MixinBot.config.proxy
|
|
91
|
+
}.compact
|
|
92
|
+
|
|
93
|
+
MixinBot::API.new(**attrs)
|
|
88
94
|
end
|
|
89
95
|
|
|
90
96
|
def parse_json_data(json_string, label: 'data')
|
data/lib/mixin_bot/client.rb
CHANGED
|
@@ -10,23 +10,22 @@ module MixinBot
|
|
|
10
10
|
class Client
|
|
11
11
|
SERVER_SCHEME = 'https'
|
|
12
12
|
|
|
13
|
-
attr_reader :config
|
|
13
|
+
attr_reader :config
|
|
14
14
|
|
|
15
15
|
def initialize(config)
|
|
16
16
|
@config = config || MixinBot.config
|
|
17
|
-
@
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
f.response :logger if @config.debug
|
|
17
|
+
@conn_mutex = Mutex.new
|
|
18
|
+
rebuild_connection!
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def conn
|
|
22
|
+
key = connection_key
|
|
23
|
+
return @conn if @conn && @conn_key == key
|
|
24
|
+
|
|
25
|
+
@conn_mutex.synchronize do
|
|
26
|
+
key = connection_key
|
|
27
|
+
rebuild_connection!(key) if @conn.nil? || @conn_key != key
|
|
28
|
+
@conn
|
|
30
29
|
end
|
|
31
30
|
end
|
|
32
31
|
|
|
@@ -45,7 +44,7 @@ module MixinBot
|
|
|
45
44
|
full_path = kwargs.empty? ? path : "#{path}?#{URI.encode_www_form(kwargs.sort_by { |k, _| k.to_s })}"
|
|
46
45
|
|
|
47
46
|
token = access_token.presence || sign_token('GET', full_path, body, exp_in:, scp:)
|
|
48
|
-
response =
|
|
47
|
+
response = conn.get(full_path, nil, authorization_headers(token))
|
|
49
48
|
parse_response!(verb: 'GET', path: full_path, body:, response:)
|
|
50
49
|
end
|
|
51
50
|
|
|
@@ -67,7 +66,7 @@ module MixinBot
|
|
|
67
66
|
end
|
|
68
67
|
|
|
69
68
|
token = access_token.presence || sign_token('POST', path, body, exp_in:, scp:)
|
|
70
|
-
response =
|
|
69
|
+
response = conn.post(path, body, authorization_headers(token))
|
|
71
70
|
parse_response!(verb: 'POST', path:, body:, response:)
|
|
72
71
|
end
|
|
73
72
|
|
|
@@ -80,7 +79,7 @@ module MixinBot
|
|
|
80
79
|
body = ''
|
|
81
80
|
full_path = q.empty? ? path : "#{path}?#{URI.encode_www_form(q.sort_by { |k, _| k.to_s })}"
|
|
82
81
|
token = access_token.presence || sign_token('GET', full_path, body, exp_in:, scp:)
|
|
83
|
-
response =
|
|
82
|
+
response = conn.get(full_path, nil, authorization_headers(token))
|
|
84
83
|
parse_response!(verb: 'GET', path: full_path, body:, response:)
|
|
85
84
|
end
|
|
86
85
|
|
|
@@ -90,7 +89,7 @@ module MixinBot
|
|
|
90
89
|
def fetch_post(path, body:, access_token: nil, exp_in: 300, scp: 'FULL')
|
|
91
90
|
payload = body.is_a?(String) ? body : body.compact.to_json
|
|
92
91
|
token = access_token.presence || sign_token('POST', path, payload, exp_in:, scp:)
|
|
93
|
-
response =
|
|
92
|
+
response = conn.post(path, payload, authorization_headers(token))
|
|
94
93
|
parse_response!(verb: 'POST', path:, body: payload, response:)
|
|
95
94
|
end
|
|
96
95
|
|
|
@@ -100,12 +99,37 @@ module MixinBot
|
|
|
100
99
|
def fetch_post_array(path, array_body, access_token: nil, exp_in: 300, scp: 'FULL')
|
|
101
100
|
payload = array_body.to_json
|
|
102
101
|
token = access_token.presence || sign_token('POST', path, payload, exp_in:, scp:)
|
|
103
|
-
response =
|
|
102
|
+
response = conn.post(path, payload, authorization_headers(token))
|
|
104
103
|
parse_response!(verb: 'POST', path:, body: payload, response:)
|
|
105
104
|
end
|
|
106
105
|
|
|
107
106
|
private
|
|
108
107
|
|
|
108
|
+
def connection_key
|
|
109
|
+
[@config.api_host, @config.http_timeout, @config.debug, @config.proxy_config]
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def rebuild_connection!(key = connection_key)
|
|
113
|
+
conn_opts = {
|
|
114
|
+
url: "#{SERVER_SCHEME}://#{@config.api_host}",
|
|
115
|
+
headers: {
|
|
116
|
+
'Content-Type' => 'application/json',
|
|
117
|
+
'User-Agent' => "mixin_bot/#{MixinBot::VERSION}"
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
conn_opts[:proxy] = @config.proxy_config.faraday_options if @config.proxy_config
|
|
121
|
+
|
|
122
|
+
@conn = Faraday.new(**conn_opts) do |f|
|
|
123
|
+
f.request :json
|
|
124
|
+
f.request :retry, max: 2, interval: 0.5, interval_randomness: 0.5, backoff_factor: 2,
|
|
125
|
+
exceptions: [Faraday::ConnectionFailed, Faraday::TimeoutError]
|
|
126
|
+
f.options.timeout = @config.http_timeout if @config.http_timeout
|
|
127
|
+
f.response :json, parser_options: { decoder: [JSONDecoder, :parse] }
|
|
128
|
+
f.response :logger if @config.debug
|
|
129
|
+
end
|
|
130
|
+
@conn_key = key
|
|
131
|
+
end
|
|
132
|
+
|
|
109
133
|
def authorization_headers(token)
|
|
110
134
|
return {} if token.blank?
|
|
111
135
|
|
data/lib/mixin_bot/computer.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require 'faraday'
|
|
4
4
|
|
|
5
5
|
require_relative 'json_decoder'
|
|
6
|
+
require_relative 'proxy'
|
|
6
7
|
|
|
7
8
|
module MixinBot
|
|
8
9
|
# Mixin Computer API client (https://computer.mixin.one).
|
|
@@ -14,21 +15,36 @@ module MixinBot
|
|
|
14
15
|
SOLANA_CHAIN_ID = '64692c23-8971-4cf4-84a7-4dd1271dd887'
|
|
15
16
|
|
|
16
17
|
class << self
|
|
17
|
-
def connection
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
def connection(proxy: MixinBot.config.proxy_config, timeout: MixinBot.config.http_timeout)
|
|
19
|
+
proxy_config = MixinBot::Proxy.parse(proxy)
|
|
20
|
+
key = [proxy_config, timeout]
|
|
21
|
+
return @connection if @connection && @connection_key == key
|
|
22
|
+
|
|
23
|
+
(@connection_mutex ||= Mutex.new).synchronize do
|
|
24
|
+
return @connection if @connection && @connection_key == key
|
|
25
|
+
|
|
26
|
+
conn_opts = { url: BASE_URI }
|
|
27
|
+
conn_opts[:proxy] = proxy_config.faraday_options if proxy_config
|
|
28
|
+
|
|
29
|
+
@connection = Faraday.new(**conn_opts) do |f|
|
|
30
|
+
f.request :json
|
|
31
|
+
f.options.timeout = timeout if timeout
|
|
32
|
+
f.response :json, parser_options: { decoder: [MixinBot::JSONDecoder, :parse] }
|
|
33
|
+
f.adapter Faraday.default_adapter
|
|
34
|
+
end
|
|
35
|
+
@connection_key = key
|
|
36
|
+
@connection
|
|
22
37
|
end
|
|
23
38
|
end
|
|
24
39
|
|
|
25
|
-
def request(method, path, body = nil)
|
|
40
|
+
def request(method, path, body = nil, proxy: MixinBot.config.proxy_config, timeout: MixinBot.config.http_timeout)
|
|
41
|
+
conn = connection(proxy:, timeout:)
|
|
26
42
|
response =
|
|
27
43
|
case method.to_s.upcase
|
|
28
44
|
when 'GET'
|
|
29
|
-
|
|
45
|
+
conn.get(path)
|
|
30
46
|
when 'POST'
|
|
31
|
-
|
|
47
|
+
conn.post(path, body)
|
|
32
48
|
else
|
|
33
49
|
raise ArgumentError, "unsupported method #{method}"
|
|
34
50
|
end
|
|
@@ -37,46 +53,46 @@ module MixinBot
|
|
|
37
53
|
response.body
|
|
38
54
|
end
|
|
39
55
|
|
|
40
|
-
def info
|
|
41
|
-
request 'GET', '/'
|
|
56
|
+
def info(proxy: MixinBot.config.proxy_config)
|
|
57
|
+
request 'GET', '/', nil, proxy:
|
|
42
58
|
end
|
|
43
59
|
alias get_computer_info info
|
|
44
60
|
|
|
45
|
-
def user(addr)
|
|
46
|
-
request 'GET', "/users/#{addr}"
|
|
61
|
+
def user(addr, proxy: MixinBot.config.proxy_config)
|
|
62
|
+
request 'GET', "/users/#{addr}", nil, proxy:
|
|
47
63
|
end
|
|
48
64
|
alias get_computer_user user
|
|
49
65
|
|
|
50
|
-
def deployed_assets
|
|
51
|
-
request 'GET', '/deployed_assets'
|
|
66
|
+
def deployed_assets(proxy: MixinBot.config.proxy_config)
|
|
67
|
+
request 'GET', '/deployed_assets', nil, proxy:
|
|
52
68
|
end
|
|
53
69
|
alias get_computer_deployed_assets deployed_assets
|
|
54
70
|
|
|
55
|
-
def system_call(id)
|
|
56
|
-
request 'GET', "/system_calls/#{id}"
|
|
71
|
+
def system_call(id, proxy: MixinBot.config.proxy_config)
|
|
72
|
+
request 'GET', "/system_calls/#{id}", nil, proxy:
|
|
57
73
|
end
|
|
58
74
|
alias get_computer_system_call system_call
|
|
59
75
|
|
|
60
|
-
def deploy_external_assets(assets)
|
|
76
|
+
def deploy_external_assets(assets, proxy: MixinBot.config.proxy_config)
|
|
61
77
|
assets = Array(assets)
|
|
62
78
|
raise ArgumentError, "cannot deploy asset from Solana: #{SOLANA_CHAIN_ID}" if assets.include?(SOLANA_CHAIN_ID)
|
|
63
79
|
|
|
64
|
-
request 'POST', '/deployed_assets', assets
|
|
80
|
+
request 'POST', '/deployed_assets', assets, proxy:
|
|
65
81
|
end
|
|
66
82
|
alias computer_deploy_external_asset deploy_external_assets
|
|
67
83
|
|
|
68
|
-
def lock_nonce_account(mix)
|
|
69
|
-
request 'POST', '/nonce_accounts', { mix: }
|
|
84
|
+
def lock_nonce_account(mix, proxy: MixinBot.config.proxy_config)
|
|
85
|
+
request 'POST', '/nonce_accounts', { mix: }, proxy:
|
|
70
86
|
end
|
|
71
87
|
alias lock_computer_nonce_account lock_nonce_account
|
|
72
88
|
|
|
73
|
-
def fee_on_xin_from_sol(sol_amount)
|
|
74
|
-
request 'POST', '/fee', { sol_amount: sol_amount.to_s }
|
|
89
|
+
def fee_on_xin_from_sol(sol_amount, proxy: MixinBot.config.proxy_config)
|
|
90
|
+
request 'POST', '/fee', { sol_amount: sol_amount.to_s }, proxy:
|
|
75
91
|
end
|
|
76
92
|
alias get_fee_on_xin_based_on_sol fee_on_xin_from_sol
|
|
77
93
|
|
|
78
94
|
def register_computer(api)
|
|
79
|
-
info_data = info
|
|
95
|
+
info_data = info(proxy: api.config.proxy_config)
|
|
80
96
|
mix = MixinBot::MixAddress.from_members(members: [api.config.app_id], threshold: 1).address
|
|
81
97
|
memo = encode_mtg_extra(
|
|
82
98
|
info_data.dig('members', 'app_id'),
|