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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/AGENTS.md +12 -2
  3. data/CHANGELOG.md +135 -0
  4. data/README.md +3 -2
  5. data/docs/agent/cli.md +4 -2
  6. data/lib/mixin_bot/address.rb +73 -62
  7. data/lib/mixin_bot/api/attachment.rb +18 -8
  8. data/lib/mixin_bot/api/blaze.rb +7 -2
  9. data/lib/mixin_bot/api/blaze_async.rb +169 -4
  10. data/lib/mixin_bot/api/computer_api.rb +7 -7
  11. data/lib/mixin_bot/api/encrypted_message.rb +75 -52
  12. data/lib/mixin_bot/api/multisig.rb +9 -7
  13. data/lib/mixin_bot/api/pin.rb +9 -8
  14. data/lib/mixin_bot/api/tip.rb +1 -1
  15. data/lib/mixin_bot/api/transaction.rb +70 -41
  16. data/lib/mixin_bot/api/transfer.rb +3 -2
  17. data/lib/mixin_bot/api/user.rb +6 -6
  18. data/lib/mixin_bot/api.rb +1 -0
  19. data/lib/mixin_bot/bot_auth.rb +1 -1
  20. data/lib/mixin_bot/cli/schema.rb +2 -0
  21. data/lib/mixin_bot/cli.rb +9 -3
  22. data/lib/mixin_bot/client.rb +43 -19
  23. data/lib/mixin_bot/computer.rb +39 -23
  24. data/lib/mixin_bot/configuration.rb +48 -8
  25. data/lib/mixin_bot/invoice.rb +126 -77
  26. data/lib/mixin_bot/nfo.rb +54 -43
  27. data/lib/mixin_bot/proxy.rb +184 -0
  28. data/lib/mixin_bot/transaction/decoder.rb +201 -138
  29. data/lib/mixin_bot/transaction/encoder.rb +156 -118
  30. data/lib/mixin_bot/transaction.rb +15 -1
  31. data/lib/mixin_bot/utils/address.rb +1 -1
  32. data/lib/mixin_bot/utils/amount.rb +25 -0
  33. data/lib/mixin_bot/utils/base58.rb +97 -0
  34. data/lib/mixin_bot/utils/crypto.rb +188 -56
  35. data/lib/mixin_bot/utils/cursor.rb +147 -0
  36. data/lib/mixin_bot/utils/decoder.rb +111 -6
  37. data/lib/mixin_bot/utils/encoder.rb +90 -21
  38. data/lib/mixin_bot/utils/options.rb +26 -0
  39. data/lib/mixin_bot/utils/sodium.rb +169 -0
  40. data/lib/mixin_bot/utils.rb +17 -2
  41. data/lib/mixin_bot/uuid.rb +6 -13
  42. data/lib/mixin_bot/version.rb +1 -1
  43. data/lib/mixin_bot.rb +1 -3
  44. data/lib/mvm/client.rb +44 -9
  45. data/lib/mvm/registry.rb +45 -17
  46. data/llms.txt +1 -1
  47. metadata +6 -44
  48. data/lib/mixin_bot/transaction/buffer.rb +0 -34
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'proxy'
4
+
3
5
  module MixinBot
4
6
  ##
5
7
  # Configuration class for storing Mixin bot credentials and settings.
@@ -9,7 +11,7 @@ module MixinBot
9
11
  # - Session ID and private key
10
12
  # - Server public key
11
13
  # - Spend key and PIN
12
- # - API and Blaze host settings
14
+ # - API, Blaze host, and HTTP/HTTPS proxy settings
13
15
  #
14
16
  # == Usage
15
17
  #
@@ -52,6 +54,7 @@ module MixinBot
52
54
  pin
53
55
  api_host
54
56
  blaze_host
57
+ proxy
55
58
  http_timeout
56
59
  session_private_key_curve25519
57
60
  server_public_key_curve25519
@@ -68,7 +71,7 @@ module MixinBot
68
71
 
69
72
  DEFAULT_BLAZE_ACK_POLICY = :on_receipt
70
73
 
71
- attr_reader :blaze_handler, :blaze_ack_policy
74
+ attr_reader :blaze_handler, :blaze_ack_policy, :proxy_config
72
75
 
73
76
  ##
74
77
  # Initializes a new Configuration instance.
@@ -83,6 +86,7 @@ module MixinBot
83
86
  # @option kwargs [String] :pin the PIN (defaults to spend_key if not provided)
84
87
  # @option kwargs [String] :api_host ('api.mixin.one') the API host
85
88
  # @option kwargs [String] :blaze_host ('blaze.mixin.one') the Blaze WebSocket host
89
+ # @option kwargs [String, URI, Hash, MixinBot::Proxy, nil] :proxy HTTP/HTTPS proxy URL or options hash
86
90
  # @option kwargs [Numeric] :http_timeout (nil) request timeout in seconds; nil keeps Faraday's default
87
91
  # @option kwargs [Boolean] :debug (false) enable debug logging
88
92
  #
@@ -110,6 +114,7 @@ module MixinBot
110
114
  @blaze_ack_policy = DEFAULT_BLAZE_ACK_POLICY
111
115
  self.blaze_handler = kwargs[:blaze_handler]
112
116
  self.blaze_ack_policy = kwargs[:blaze_ack_policy]
117
+ self.proxy = kwargs[:proxy]
113
118
 
114
119
  self.session_private_key = kwargs[:session_private_key] || kwargs[:private_key]
115
120
  self.server_public_key = kwargs[:server_public_key] || kwargs[:pin_token]
@@ -149,7 +154,7 @@ module MixinBot
149
154
  _private_key = decode_key key
150
155
  @session_private_key =
151
156
  if _private_key.size == 32
152
- JOSE::JWA::Ed25519.keypair(_private_key).last
157
+ MixinBot.utils.ed25519_keypair(_private_key).last
153
158
  else
154
159
  _private_key
155
160
  end
@@ -169,10 +174,12 @@ module MixinBot
169
174
  return if key.blank?
170
175
 
171
176
  @server_public_key = decode_key key
172
- # HEX encoded
177
+ # HEX encoded — sniff the whitespace-stripped copy so the classification
178
+ # matches +decode_key+, which accepts hex keys with stray spaces or
179
+ # wrapped lines; a raw sniff would skip the Curve25519 conversion.
173
180
  @server_public_key_curve25519 =
174
- if key.match?(/\A\h{32,}\z/i)
175
- JOSE::JWA::Ed25519.pk_to_curve25519 @server_public_key
181
+ if key.gsub(/\s+/, '').match?(/\A\h{32,}\z/i)
182
+ MixinBot.utils.ed25519_pk_to_curve25519 @server_public_key
176
183
  else
177
184
  server_public_key
178
185
  end
@@ -192,7 +199,7 @@ module MixinBot
192
199
  _private_key = decode_key key
193
200
  @spend_key =
194
201
  if _private_key.size == 32
195
- JOSE::JWA::Ed25519.keypair(_private_key).last
202
+ MixinBot.utils.ed25519_keypair(_private_key).last
196
203
  else
197
204
  _private_key
198
205
  end
@@ -212,7 +219,7 @@ module MixinBot
212
219
  _private_key = decode_key key
213
220
  @pin =
214
221
  if _private_key.size == 32
215
- JOSE::JWA::Ed25519.keypair(_private_key).last
222
+ MixinBot.utils.ed25519_keypair(_private_key).last
216
223
  else
217
224
  _private_key
218
225
  end
@@ -255,8 +262,41 @@ module MixinBot
255
262
  @blaze_ack_policy = policy
256
263
  end
257
264
 
265
+ ##
266
+ # Sets the HTTP/HTTPS proxy used by REST, attachment upload, Blaze WebSocket,
267
+ # Computer, and MVM clients.
268
+ #
269
+ # @param value [String, URI, Hash, MixinBot::Proxy, nil]
270
+ # @raise [MixinBot::ArgumentError] when the proxy format or scheme is invalid
271
+ #
272
+ def proxy=(value)
273
+ parsed = MixinBot::Proxy.parse(value)
274
+ if parsed.nil?
275
+ @proxy_config = nil
276
+ @proxy = nil
277
+ else
278
+ @proxy_config = parsed
279
+ @proxy = normalize_stored_proxy(value, parsed)
280
+ end
281
+ end
282
+
258
283
  private
259
284
 
285
+ def normalize_stored_proxy(value, parsed)
286
+ case value
287
+ when String
288
+ value.strip.freeze
289
+ when URI
290
+ value.dup.freeze
291
+ when Hash
292
+ value.transform_values do |v|
293
+ v.is_a?(String) || v.is_a?(URI) ? v.dup.freeze : v
294
+ end.freeze
295
+ else
296
+ parsed.origin.freeze
297
+ end
298
+ end
299
+
260
300
  def decode_key(key)
261
301
  MixinBot.utils.decode_key key
262
302
  end
@@ -4,11 +4,13 @@ module MixinBot
4
4
  class Invoice
5
5
  INVOICE_PREFIX = 'MIN'
6
6
  INVOICE_VERSION = 0x00
7
+ MAX_RECIPIENT_BYTES = 1024
8
+ MAX_ENTRIES = 128
7
9
 
8
10
  attr_accessor :version, :recipient, :entries, :address
9
11
 
10
12
  def initialize(**args)
11
- args = args.with_indifferent_access
13
+ args = MixinBot::Utils::Options.symbolize args
12
14
 
13
15
  if args[:address]
14
16
  @address = args[:address]
@@ -31,28 +33,24 @@ module MixinBot
31
33
  end
32
34
 
33
35
  def encode
34
- # Start with empty payload
35
- payload = []
36
+ raise InvalidInvoiceFormatError, 'invalid invoice version' unless version == INVOICE_VERSION
36
37
 
37
- # Add version
38
- payload += MixinBot.utils.encode_int(version)
38
+ payload = +''.b << version
39
+ recipient_bytes = recipient.payload
40
+ recipient_bytes = recipient_bytes.pack('C*') unless recipient_bytes.is_a?(String)
41
+ raise InvalidInvoiceFormatError, 'invoice recipient is too large' if recipient_bytes.bytesize > MAX_RECIPIENT_BYTES
39
42
 
40
- # Add recipient - ensure we're using the raw payload bytes
41
- recipient_payload = recipient.payload
42
- recipient_bytes = recipient_payload.is_a?(String) ? recipient_payload.bytes : recipient_payload
43
- payload += MixinBot.utils.encode_uint16(recipient_bytes.size)
43
+ payload << MixinBot.utils.pack_uint16(recipient_bytes.bytesize)
44
+ payload << recipient_bytes
44
45
 
45
- payload += recipient_bytes
46
+ raise InvalidInvoiceFormatError, 'too many invoice entries' if entries.size > MAX_ENTRIES
46
47
 
47
- # Add entries
48
- payload += MixinBot.utils.encode_int(entries.size)
49
- entries.each do |entry|
50
- payload += entry.encode
48
+ payload << entries.size
49
+ entries.each_with_index do |entry, index|
50
+ entry.validate_index_references(index)
51
+ entry.encode_to payload
51
52
  end
52
53
 
53
- # Convert payload to binary string
54
- payload = payload.pack('C*')
55
-
56
54
  # Calculate checksum
57
55
  checksum = SHA3::Digest::SHA3_256.digest(INVOICE_PREFIX + payload)[0...4]
58
56
 
@@ -60,6 +58,8 @@ module MixinBot
60
58
  self.address = INVOICE_PREFIX + Base64.urlsafe_encode64(payload + checksum, padding: false)
61
59
  end
62
60
 
61
+ # Reads the payload through a {Utils::Cursor}; a payload that ends before
62
+ # every field is read raises {InvalidInvoiceFormatError}.
63
63
  def decode
64
64
  prefix = address[0..2]
65
65
  raise MixinBot::InvalidInvoiceFormatError, 'invalid invoice prefix' unless prefix == INVOICE_PREFIX
@@ -71,102 +71,151 @@ module MixinBot
71
71
  checksum = SHA3::Digest::SHA3_256.digest(INVOICE_PREFIX + payload)[0...4]
72
72
  raise MixinBot::InvalidInvoiceFormatError, 'invalid invoice checksum' unless checksum == data[-4..]
73
73
 
74
- payload = payload.bytes
74
+ cursor = MixinBot::Utils::Cursor.new(payload)
75
75
 
76
76
  # Read version
77
- self.version = MixinBot.utils.decode_int payload.shift(1)
77
+ self.version = cursor.read_int(1)
78
78
  raise MixinBot::InvalidInvoiceFormatError, 'invalid invoice version' unless version == INVOICE_VERSION
79
79
 
80
80
  # Read recipient with proper size handling
81
- recipient_size = MixinBot.utils.decode_uint16 payload.shift(2)
82
- recipient_bytes = payload.shift(recipient_size)
83
- self.recipient = MixinBot::MixAddress.new(payload: recipient_bytes.pack('C*'))
84
-
85
- # decode entries
86
- entries_size = MixinBot.utils.decode_int payload.shift(1)
87
- entries = []
88
- entries_size.times do
89
- next if payload.empty?
90
-
91
- trace_id_bytes = payload.shift(16)
92
- trace_id = MixinBot::UUID.new(raw: trace_id_bytes.pack('C*')).unpacked
93
-
94
- asset_id_bytes = payload.shift(16)
95
- asset_id = MixinBot::UUID.new(raw: asset_id_bytes.pack('C*')).unpacked
96
-
97
- amount_size = MixinBot.utils.decode_int payload.shift(1)
98
- amount_bytes = payload.shift(amount_size)
99
- amount = amount_bytes.pack('C*').to_d
100
-
101
- extra_size = MixinBot.utils.decode_int payload.shift(2)
102
- extra = payload.shift(extra_size).pack('C*')
103
-
104
- references_count = MixinBot.utils.decode_int payload.shift(1)
105
- hash_references = []
106
- index_references = []
107
-
108
- references_count.times do
109
- rv = MixinBot.utils.decode_int payload.shift(1)
110
- case rv
111
- when 0
112
- hash_references << payload.shift(32).pack('C*').unpack1('H*')
113
- when 1
114
- index_references << MixinBot.utils.decode_int(payload.shift(1))
115
- else
116
- raise MixinBot::InvalidInvoiceFormatError, "invalid invoice reference type: #{rv}"
117
- end
118
- end
81
+ recipient_size = cursor.read_uint16
82
+ raise InvalidInvoiceFormatError, 'invoice recipient is too large' if recipient_size > MAX_RECIPIENT_BYTES
83
+
84
+ self.recipient = MixinBot::MixAddress.new(payload: cursor.read(recipient_size))
119
85
 
120
- entries << InvoiceEntry.new(trace_id:, asset_id:, amount:, extra:, index_references:, hash_references:)
86
+ entries_size = cursor.read_uint8
87
+ raise InvalidInvoiceFormatError, 'too many invoice entries' if entries_size > MAX_ENTRIES
88
+
89
+ self.entries = Array.new(entries_size) do |index|
90
+ entry = InvoiceEntry.decode_from(cursor)
91
+ entry.validate_index_references(index)
92
+ entry
121
93
  end
94
+ raise InvalidInvoiceFormatError, 'trailing invoice bytes' unless cursor.empty?
122
95
 
123
- self.entries = entries
96
+ entries
97
+ rescue MixinBot::ArgumentError, ::ArgumentError => e
98
+ raise MixinBot::InvalidInvoiceFormatError, "invalid invoice payload: #{e.message}"
124
99
  end
125
100
  end
126
101
 
127
102
  class InvoiceEntry
103
+ MAX_REFERENCES = 16
104
+ MAX_AMOUNT_BYTES = 128
128
105
  attr_accessor :trace_id, :asset_id, :amount, :extra, :index_references, :hash_references
129
106
 
130
107
  def initialize(**args)
131
- args = args.with_indifferent_access
108
+ args = MixinBot::Utils::Options.symbolize args
132
109
 
133
110
  @trace_id = args[:trace_id]
134
111
  @asset_id = args[:asset_id]
135
- @amount = args[:amount].to_d
112
+ @amount = BigDecimal(args[:amount].to_s, exception: false)
113
+ raise InvalidInvoiceFormatError, 'invalid invoice amount' unless @amount&.finite? && !@amount.negative?
114
+
136
115
  @extra = args[:extra]
137
116
  @index_references = args[:index_references]
138
117
  @hash_references = args[:hash_references]
139
118
  end
140
119
 
120
+ ##
121
+ # This entry's wire bytes as an Array of bytes.
122
+ #
123
+ # @return [Array<Integer>]
124
+ #
141
125
  def encode
142
- bytes = []
126
+ encode_to(+''.b).bytes
127
+ end
128
+
129
+ ##
130
+ # Appends this entry's wire bytes to +buffer+.
131
+ #
132
+ # @param buffer [String] binary String to append to
133
+ # @return [String] +buffer+
134
+ #
135
+ def encode_to(buffer)
136
+ [trace_id, asset_id].each do |id|
137
+ unless id.is_a?(String) && id.match?(/\A(?:\h{32}|\h{8}-\h{4}-\h{4}-\h{4}-\h{12})\z/)
138
+ raise InvalidInvoiceFormatError, 'invalid invoice entry UUID'
139
+ end
140
+
141
+ buffer << MixinBot::UUID.new(hex: id).packed
142
+ end
143
+
144
+ whole, fraction = MixinBot.utils.safe_amount(amount).split('.', 2)
145
+ amount_string = "#{whole}.#{fraction.to_s.ljust(8, '0')}"
146
+ raise InvalidInvoiceFormatError, 'invoice amount is too large' if amount_string.bytesize > MAX_AMOUNT_BYTES
143
147
 
144
- bytes.concat MixinBot::UUID.new(hex: trace_id).packed.bytes
145
- bytes.concat MixinBot::UUID.new(hex: asset_id).packed.bytes
148
+ buffer << amount_string.bytesize << amount_string
146
149
 
147
- amount_string = amount.to_d.to_s('F')
148
- amount_bytes = amount_string.bytes
149
- bytes.concat MixinBot.utils.encode_int(amount_bytes.size)
150
- bytes.concat amount_bytes
150
+ raise InvalidInvoiceFormatError, 'invoice extra is too large' if extra.bytesize > 65_535
151
151
 
152
- extra_bytes = extra.bytes
153
- bytes.concat MixinBot.utils.encode_uint16(extra_bytes.size)
154
- bytes.concat extra_bytes
152
+ buffer << MixinBot.utils.pack_uint16(extra.bytesize)
153
+ buffer << extra.b
155
154
 
156
155
  references_count = (index_references || []).size + (hash_references || []).size
157
- bytes.concat MixinBot.utils.encode_int(references_count)
156
+ raise InvalidInvoiceFormatError, 'too many invoice references' if references_count > MAX_REFERENCES
157
+
158
+ validate_index_references(256)
159
+ buffer << references_count
158
160
 
159
161
  index_references&.each do |index|
160
- bytes.concat MixinBot.utils.encode_int(1)
161
- bytes.concat MixinBot.utils.encode_int(index)
162
+ buffer << 1 << index
162
163
  end
163
164
 
164
165
  hash_references&.each do |hash|
165
- bytes.concat MixinBot.utils.encode_int(0)
166
- bytes.concat [hash].pack('H*').bytes
166
+ raise InvalidInvoiceFormatError, 'invalid invoice hash reference' unless hash.is_a?(String) && hash.match?(/\A\h{64}\z/)
167
+
168
+ buffer << 0
169
+ buffer << [hash].pack('H*')
170
+ end
171
+
172
+ buffer
173
+ end
174
+
175
+ def validate_index_references(entry_index)
176
+ return if (index_references || []).all? { |reference| reference.is_a?(Integer) && reference.between?(0, entry_index - 1) }
177
+
178
+ raise InvalidInvoiceFormatError, 'invoice index reference must point to a previous entry'
179
+ end
180
+
181
+ ##
182
+ # Reads one entry from +cursor+.
183
+ #
184
+ # @param cursor [MixinBot::Utils::Cursor]
185
+ # @return [InvoiceEntry]
186
+ #
187
+ def self.decode_from(cursor)
188
+ trace_id = MixinBot::UUID.new(raw: cursor.read(16)).unpacked
189
+ asset_id = MixinBot::UUID.new(raw: cursor.read(16)).unpacked
190
+
191
+ amount_size = cursor.read_uint8
192
+ raise InvalidInvoiceFormatError, 'invalid invoice amount size' unless amount_size.between?(1, MAX_AMOUNT_BYTES)
193
+
194
+ amount = BigDecimal(cursor.read(amount_size), exception: false)
195
+ raise InvalidInvoiceFormatError, 'invalid invoice amount' unless amount&.finite? && !amount.negative?
196
+
197
+ extra_size = cursor.read_uint16
198
+ extra = cursor.read(extra_size)
199
+
200
+ references_count = cursor.read_uint8
201
+ raise InvalidInvoiceFormatError, 'too many invoice references' if references_count > MAX_REFERENCES
202
+
203
+ hash_references = []
204
+ index_references = []
205
+
206
+ references_count.times do
207
+ rv = cursor.read_int(1)
208
+ case rv
209
+ when 0
210
+ hash_references << cursor.read(32).unpack1('H*')
211
+ when 1
212
+ index_references << cursor.read_int(1)
213
+ else
214
+ raise MixinBot::InvalidInvoiceFormatError, "invalid invoice reference type: #{rv}"
215
+ end
167
216
  end
168
217
 
169
- bytes
218
+ new(trace_id:, asset_id:, amount:, extra:, index_references:, hash_references:)
170
219
  end
171
220
 
172
221
  def to_hash
data/lib/mixin_bot/nfo.rb CHANGED
@@ -38,20 +38,20 @@ module MixinBot
38
38
  end
39
39
 
40
40
  def unique_token_id
41
- bytes = []
42
- bytes.concat MixinBot::UUID.new(hex: chain).packed.bytes
43
- bytes.concat [nm_class].pack('H*').bytes
44
- bytes.concat MixinBot::UUID.new(hex: collection).packed.bytes
45
- bytes.concat MixinBot.utils.encode_int token
41
+ bytes = +''.b
42
+ bytes << MixinBot::UUID.new(hex: chain).packed
43
+ bytes << [nm_class].pack('H*')
44
+ bytes << MixinBot::UUID.new(hex: collection).packed
45
+ bytes << token_bytes
46
46
 
47
47
  md5 = Digest::MD5.new
48
- md5.update bytes.pack('c*')
49
- digest = [md5.hexdigest].pack('H*').bytes
48
+ md5.update bytes
49
+ digest = [md5.hexdigest].pack('H*')
50
50
 
51
- digest[6] = (digest[6] & 0x0f) | 0x30
52
- digest[8] = (digest[8] & 0x3f) | 0x80
51
+ digest.setbyte(6, (digest.getbyte(6) & 0x0f) | 0x30)
52
+ digest.setbyte(8, (digest.getbyte(8) & 0x3f) | 0x80)
53
53
 
54
- hex = digest.pack('c*').unpack1('H*')
54
+ hex = digest.unpack1('H*')
55
55
 
56
56
  MixinBot::UUID.new(hex:).unpacked
57
57
  end
@@ -64,44 +64,51 @@ module MixinBot
64
64
  end
65
65
  end
66
66
 
67
+ # Builds the memo in one binary String buffer (see {Utils::Encoder#pack_int}).
67
68
  def encode
68
- bytes = []
69
+ bytes = +''.b
69
70
 
70
- bytes.concat prefix.bytes
71
- bytes.push(version)
71
+ bytes << prefix
72
+ bytes << version
72
73
 
73
74
  if mask.zero?
74
75
  bytes << 0
75
76
  else
76
77
  bytes << 1
77
- bytes.concat MixinBot.utils.encode_uint64 mask
78
- bytes.concat MixinBot::UUID.new(hex: chain).packed.bytes
78
+ bytes << MixinBot.utils.pack_uint64(mask)
79
+ bytes << MixinBot::UUID.new(hex: chain).packed
79
80
 
80
- class_bytes = [nm_class].pack('H*').bytes
81
- bytes.concat MixinBot.utils.encode_int class_bytes.size
82
- bytes.concat class_bytes
81
+ class_bytes = [nm_class].pack('H*')
82
+ append_slice bytes, class_bytes
83
83
 
84
- collection_bytes = collection.split('-').pack('H* H* H* H* H*').bytes
85
- bytes.concat MixinBot.utils.encode_int collection_bytes.size
86
- bytes.concat collection_bytes
84
+ collection_bytes = collection.split('-').pack('H* H* H* H* H*')
85
+ append_slice bytes, collection_bytes
87
86
 
88
- # token_bytes = memo[:token].split('-').pack('H* H* H* H* H*').bytes
89
- token_bytes = MixinBot.utils.encode_int token
90
- bytes.concat MixinBot.utils.encode_int token_bytes.size
91
- bytes.concat token_bytes
87
+ append_slice bytes, token_bytes
92
88
  end
93
89
 
94
- extra_bytes = [extra].pack('H*').bytes
95
- bytes.concat MixinBot.utils.encode_int extra_bytes.size
96
- bytes.concat extra_bytes
90
+ extra_bytes = [extra].pack('H*')
91
+ append_slice bytes, extra_bytes
97
92
 
98
- @raw = bytes.pack('C*')
93
+ @raw = bytes
99
94
  @hex = raw.unpack1('H*')
100
95
  @memo = Base64.urlsafe_encode64 raw, padding: false
101
96
 
102
97
  self
103
98
  end
104
99
 
100
+ def token_bytes
101
+ token&.zero? ? ''.b : MixinBot.utils.pack_int(token)
102
+ end
103
+
104
+ def append_slice(buffer, bytes)
105
+ raise InvalidNfoFormatError, 'NFO slice exceeds 128 bytes' if bytes.bytesize > 128
106
+
107
+ buffer << bytes.bytesize << bytes
108
+ end
109
+
110
+ private :token_bytes, :append_slice
111
+
105
112
  def decode
106
113
  @raw =
107
114
  if memo.present?
@@ -119,35 +126,39 @@ module MixinBot
119
126
  self
120
127
  end
121
128
 
129
+ # Reads the memo through a {Utils::Cursor}; a memo that ends before every
130
+ # field is read raises {InvalidNfoFormatError}.
122
131
  def decode_bytes
123
- bytes = raw.bytes
132
+ cursor = MixinBot::Utils::Cursor.new(raw)
124
133
 
125
- _prefix = bytes.shift(3).pack('C*')
134
+ _prefix = cursor.read(3)
126
135
  raise MixinBot::InvalidNfoFormatError, "NFO prefix #{_prefix}" if _prefix != prefix
127
136
 
128
- _version = bytes.shift
137
+ _version = cursor.read_uint8
129
138
  raise MixinBot::InvalidNfoFormatError, "NFO version #{prefix}" if _version != version
130
139
 
131
- hint = bytes.shift
140
+ hint = cursor.read_uint8
132
141
  if hint == 1
133
- @mask = bytes.shift(8).reverse.pack('C*').unpack1('Q*')
142
+ @mask = cursor.read_uint64
134
143
 
135
- @chain = MixinBot::UUID.new(hex: bytes.shift(16).pack('C*').unpack1('H*')).unpacked
144
+ @chain = MixinBot::UUID.new(hex: cursor.read(16).unpack1('H*')).unpacked
136
145
 
137
- class_length = bytes.shift
138
- @nm_class = bytes.shift(class_length).pack('C*').unpack1('H*')
146
+ class_length = cursor.read_uint8
147
+ @nm_class = cursor.read(class_length).unpack1('H*')
139
148
 
140
- collection_length = bytes.shift
141
- @collection = MixinBot::UUID.new(hex: bytes.shift(collection_length).pack('C*').unpack1('H*')).unpacked
149
+ collection_length = cursor.read_uint8
150
+ @collection = MixinBot::UUID.new(hex: cursor.read(collection_length).unpack1('H*')).unpacked
142
151
 
143
- token_length = bytes.shift
144
- @token = MixinBot.utils.decode_int bytes.shift(token_length)
152
+ token_length = cursor.read_uint8
153
+ @token = cursor.read_int(token_length)
145
154
  end
146
155
 
147
- extra_length = bytes.shift
148
- @extra = bytes.shift(extra_length).pack('C*').unpack1('H*')
156
+ extra_length = cursor.read_uint8
157
+ @extra = cursor.read(extra_length).unpack1('H*')
149
158
 
150
159
  self
160
+ rescue MixinBot::ArgumentError => e
161
+ raise MixinBot::InvalidNfoFormatError, "truncated NFO memo: #{e.message}"
151
162
  end
152
163
 
153
164
  def to_h