Module: BSV::Wallet::Wire::Serializer

Defined in:
lib/bsv/wallet_interface/wire/serializer.rb

Overview

Serialises and deserialises all 28 BRC-100 wire protocol method calls.

Frame layout:

Request:  [UInt8 call_code] [UInt8 originator_length] [UTF-8 originator] [params…]
Response: [UInt8 error_code] [result…]
Error:    [UInt8 error_code ≠ 0] [VarInt msg_len] [UTF-8 message] [VarInt stack_len] [UTF-8 stack]

For every method:

serialize_request(method_name, args, originator:)  → binary String
deserialize_request(data)                          → [method_name, args, originator]
serialize_response(method_name, result)            → binary String (error_code=0 prefix)
deserialize_response(method_name, data)            → result Hash  (raises on error_code≠0)
serialize_error(code, message, stack_trace:)       → binary String

Constant Summary collapse

CALL_CODES =

Maps method name symbols to their wire call codes (1-28).

{
  create_action: 1,
  sign_action: 2,
  abort_action: 3,
  list_actions: 4,
  internalize_action: 5,
  list_outputs: 6,
  relinquish_output: 7,
  get_public_key: 8,
  reveal_counterparty_key_linkage: 9,
  reveal_specific_key_linkage: 10,
  encrypt: 11,
  decrypt: 12,
  create_hmac: 13,
  verify_hmac: 14,
  create_signature: 15,
  verify_signature: 16,
  acquire_certificate: 17,
  list_certificates: 18,
  prove_certificate: 19,
  relinquish_certificate: 20,
  discover_by_identity_key: 21,
  discover_by_attributes: 22,
  is_authenticated: 23,
  wait_for_authentication: 24,
  get_height: 25,
  get_header_for_height: 26,
  get_network: 27,
  get_version: 28
}.freeze
METHODS_BY_CODE =

Inverse mapping: call code Integer → method name Symbol.

CALL_CODES.invert.freeze
LIST_ACTIONS_INCLUDE_FLAGS =

  1. listActions


%i[
  include_labels include_inputs include_input_source_locking_scripts
  include_input_unlocking_scripts include_outputs include_output_locking_scripts
].freeze

Class Method Summary collapse

Class Method Details

.decode_base64_32(b64_str) ⇒ Object

Raises:

  • (ArgumentError)


242
243
244
245
246
247
248
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 242

def decode_base64_32(b64_str)
  require 'base64'
  raw = Base64.strict_decode64(b64_str)
  raise ArgumentError, "expected 32-byte base64 value, got #{raw.bytesize}" unless raw.bytesize == 32

  raw
end

.decode_base64_32_safe(str) ⇒ Object



335
336
337
338
339
340
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 335

def decode_base64_32_safe(str)
  require 'base64'
  Base64.strict_decode64(str)
rescue StandardError
  str.b
end

.deserialize_request(data) ⇒ Array(Symbol, Hash, String)

Deserialises a binary request frame.

Parameters:

  • data (String)

    binary frame

Returns:

  • (Array(Symbol, Hash, String))
    method_name, args, originator


87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 87

def deserialize_request(data)
  r = Reader.new(data)
  code = r.read_byte
  method_name = METHODS_BY_CODE.fetch(code) do
    raise ArgumentError, "unknown call code: #{code}"
  end

  orig_len = r.read_byte
  originator = r.read_bytes(orig_len).force_encoding('UTF-8')

  args = send(:"read_#{method_name}_params", r)
  [method_name, args, originator]
end

.deserialize_response(method_name, data) ⇒ Hash

Deserialises a response frame, raising a WalletError on non-zero error code.

Parameters:

  • method_name (Symbol)
  • data (String)

    binary frame

Returns:

  • (Hash)

    result



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 134

def deserialize_response(method_name, data)
  r = Reader.new(data)
  error_code = r.read_byte

  unless error_code.zero?
    message = r.read_utf8_string
    raise BSV::Wallet::WalletError.new(message, error_code)
  end

  send(:"read_#{method_name}_result", r)
end

.encode_base64(raw_bytes) ⇒ Object



250
251
252
253
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 250

def encode_base64(raw_bytes)
  require 'base64'
  Base64.strict_encode64(raw_bytes)
end

.read_abort_action_params(r) ⇒ Object



779
780
781
782
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 779

def read_abort_action_params(r)
  ref_bytes = r.read_remaining
  { reference: encode_base64(ref_bytes) }
end

.read_abort_action_result(_r) ⇒ Object



788
789
790
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 788

def read_abort_action_result(_r)
  { aborted: true }
end

.read_acquire_certificate_params(r) ⇒ Object



1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1593

def read_acquire_certificate_params(r)
  args = {}
  args[:type] = encode_base64(r.read_bytes(32))
  args[:certifier] = r.read_bytes(33).unpack1('H*')

  fields_count = r.read_varint
  args[:fields] = {}
  fields_count.times do
    k = r.read_utf8_string
    args[:fields][k] = r.read_utf8_string
  end

  args[:privileged], args[:privileged_reason] = r.read_privileged

  protocol_flag = r.read_byte
  if protocol_flag == 1
    args[:acquisition_protocol] = 'direct'
    args[:serial_number] = encode_base64(r.read_bytes(32))
    args[:revocation_outpoint] = read_outpoint_str(r)
    sig_len = r.read_varint
    args[:signature] = r.read_bytes(sig_len).unpack1('H*')
    kr_flag = r.read_byte
    if kr_flag == 11
      args[:keyring_revealer] = 'certifier'
    else
      remaining = r.read_bytes(32)
      args[:keyring_revealer] = ([kr_flag].pack('C') + remaining).unpack1('H*')
    end
    kr_count = r.read_varint
    args[:keyring_for_subject] = {}
    kr_count.times do
      k = r.read_utf8_string
      v_len = r.read_varint
      args[:keyring_for_subject][k] = encode_base64(r.read_bytes(v_len))
    end
  else
    args[:acquisition_protocol] = 'issuance'
    args[:certifier_url] = r.read_utf8_string
  end

  args
end

.read_acquire_certificate_result(r) ⇒ Object



1640
1641
1642
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1640

def read_acquire_certificate_result(r)
  read_cert_from_reader(r)
end

.read_cert_from_reader(r) ⇒ Object

Reads a certificate struct from the reader (without length prefix).



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 215

def read_cert_from_reader(r)
  require 'base64'
  type = encode_base64(r.read_bytes(32))
  subject = r.read_bytes(33).unpack1('H*')
  serial_number = encode_base64(r.read_bytes(32))
  certifier = r.read_bytes(33).unpack1('H*')
  revocation_outpoint = read_outpoint_str(r)
  sig_len = r.read_varint
  signature = r.read_bytes(sig_len).unpack1('H*')
  fields_count = r.read_varint
  fields = {}
  fields_count.times do
    k = r.read_utf8_string
    v = r.read_utf8_string
    fields[k] = v
  end
  {
    type: type,
    subject: subject,
    serial_number: serial_number,
    certifier: certifier,
    revocation_outpoint: revocation_outpoint,
    signature: signature,
    fields: fields
  }
end

.read_create_action_params(r) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 446

def read_create_action_params(r)
  args = {}
  args[:description] = r.read_utf8_string

  input_beef_raw = r.read_optional_byte_array
  args[:input_beef] = input_beef_raw&.bytes

  inputs_count = r.read_signed_varint
  args[:inputs] = if inputs_count == -1
                    nil
                  else
                    inputs_count.times.map do
                      inp = {}
                      inp[:outpoint] = read_outpoint_str(r)
                      script_len = r.read_signed_varint
                      if script_len >= 0
                        inp[:unlocking_script] = r.read_bytes(script_len).unpack1('H*')
                      else
                        inp[:unlocking_script] = nil
                        inp[:unlocking_script_length] = r.read_varint
                      end
                      inp[:input_description] = r.read_utf8_string
                      seq = r.read_signed_varint
                      inp[:sequence_number] = seq == -1 ? nil : seq
                      inp
                    end
                  end

  outputs_count = r.read_signed_varint
  args[:outputs] = if outputs_count == -1
                     nil
                   else
                     outputs_count.times.map do
                       out = {}
                       ls_len = r.read_varint
                       out[:locking_script] = r.read_bytes(ls_len).unpack1('H*')
                       out[:satoshis] = r.read_varint
                       out[:output_description] = r.read_utf8_string
                       out[:basket] = r.read_optional_utf8_string
                       out[:custom_instructions] = r.read_optional_utf8_string
                       out[:tags] = r.read_string_array
                       out
                     end
                   end

  lock_time = r.read_signed_varint
  args[:lock_time] = lock_time == -1 ? nil : lock_time
  version = r.read_signed_varint
  args[:version] = version == -1 ? nil : version
  args[:labels] = r.read_string_array

  opts_flag = r.read_int8
  if opts_flag == 1
    opts = {}
    opts[:sign_and_process] = r.read_optional_bool
    opts[:accept_delayed_broadcast] = r.read_optional_bool
    trust_self_flag = r.read_int8
    opts[:trust_self] = if trust_self_flag == -1
                          nil
                        elsif trust_self_flag == 1
                          'known'
                        end
    known_count = r.read_signed_varint
    opts[:known_txids] = if known_count == -1
                           nil
                         else
                           known_count.times.map { r.read_bytes(32).unpack1('H*') }
                         end
    opts[:return_txid_only] = r.read_optional_bool
    opts[:no_send] = r.read_optional_bool
    no_send_change_count = r.read_signed_varint
    opts[:no_send_change] = if no_send_change_count == -1
                              nil
                            else
                              no_send_change_count.times.map { read_outpoint_str(r) }
                            end
    send_with_count = r.read_signed_varint
    opts[:send_with] = if send_with_count == -1
                         nil
                       else
                         send_with_count.times.map { r.read_bytes(32).unpack1('H*') }
                       end
    opts[:randomize_outputs] = r.read_optional_bool
    args[:options] = opts
  else
    args[:options] = nil
  end

  args
end

.read_create_action_result(r) ⇒ Object



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 591

def read_create_action_result(r)
  result = {}
  txid_flag = r.read_int8
  result[:txid] = txid_flag == 1 ? r.read_bytes(32).unpack1('H*') : nil

  tx_flag = r.read_int8
  if tx_flag == 1
    tx_len = r.read_varint
    result[:tx] = r.read_bytes(tx_len).bytes
  else
    result[:tx] = nil
  end

  no_send_change_count = r.read_signed_varint
  result[:no_send_change] = if no_send_change_count == -1
                              nil
                            else
                              no_send_change_count.times.map { read_outpoint_str(r) }
                            end

  swr_count = r.read_signed_varint
  result[:send_with_results] = if swr_count == -1
                                 nil
                               else
                                 swr_count.times.map do
                                   txid = r.read_bytes(32).unpack1('H*')
                                   status = { 1 => 'unproven', 2 => 'sending', 3 => 'failed' }[r.read_int8] || 'failed'
                                   { txid: txid, status: status }
                                 end
                               end

  signable_flag = r.read_int8
  if signable_flag == 1
    tx_len = r.read_varint
    tx = r.read_bytes(tx_len).bytes
    ref_len = r.read_varint
    ref = encode_base64(r.read_bytes(ref_len))
    result[:signable_transaction] = { tx: tx, reference: ref }
  else
    result[:signable_transaction] = nil
  end

  result
end

.read_create_hmac_params(r) ⇒ Object



1401
1402
1403
1404
1405
1406
1407
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1401

def read_create_hmac_params(r)
  args = read_key_related_from_reader(r)
  data_len = r.read_varint
  args[:data] = r.read_bytes(data_len).bytes
  args[:seek_permission] = r.read_optional_bool
  args
end

.read_create_hmac_result(r) ⇒ Object



1415
1416
1417
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1415

def read_create_hmac_result(r)
  { hmac: r.read_remaining.bytes }
end

.read_create_signature_params(r) ⇒ Object



1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1473

def read_create_signature_params(r)
  args = read_key_related_from_reader(r)
  data_type = r.read_byte
  if data_type == 1
    data_len = r.read_varint
    args[:data] = r.read_bytes(data_len).bytes
  elsif data_type == 2
    args[:hash_to_directly_sign] = r.read_bytes(32).bytes
  end
  args[:seek_permission] = r.read_optional_bool
  args
end

.read_create_signature_result(r) ⇒ Object



1492
1493
1494
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1492

def read_create_signature_result(r)
  { signature: r.read_remaining.bytes }
end

.read_decrypt_params(r) ⇒ Object



1370
1371
1372
1373
1374
1375
1376
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1370

def read_decrypt_params(r)
  args = read_key_related_from_reader(r)
  ct_len = r.read_varint
  args[:ciphertext] = r.read_bytes(ct_len).bytes
  args[:seek_permission] = r.read_optional_bool
  args
end

.read_decrypt_result(r) ⇒ Object



1384
1385
1386
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1384

def read_decrypt_result(r)
  { plaintext: r.read_remaining.bytes }
end

.read_discover_by_attributes_params(r) ⇒ Object



1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1857

def read_discover_by_attributes_params(r)
  attr_count = r.read_varint
  attributes = {}
  attr_count.times do
    k = r.read_utf8_string
    attributes[k] = r.read_utf8_string
  end
  limit = r.read_signed_varint
  offset = r.read_signed_varint
  seek_permission = r.read_optional_bool
  { attributes: attributes,
    limit: limit == -1 ? nil : limit,
    offset: offset == -1 ? nil : offset,
    seek_permission: seek_permission }
end

.read_discover_by_attributes_result(r) ⇒ Object



1877
1878
1879
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1877

def read_discover_by_attributes_result(r)
  read_discovery_result(r)
end

.read_discover_by_identity_key_params(r) ⇒ Object



1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1822

def read_discover_by_identity_key_params(r)
  identity_key = r.read_bytes(33).unpack1('H*')
  limit = r.read_signed_varint
  offset = r.read_signed_varint
  seek_permission = r.read_optional_bool
  { identity_key: identity_key,
    limit: limit == -1 ? nil : limit,
    offset: offset == -1 ? nil : offset,
    seek_permission: seek_permission }
end

.read_discover_by_identity_key_result(r) ⇒ Object



1837
1838
1839
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1837

def read_discover_by_identity_key_result(r)
  read_discovery_result(r)
end

.read_discovery_result(r) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 294

def read_discovery_result(r)
  total = r.read_varint
  certs = total.times.map do
    cert_len = r.read_varint
    cert_r = Reader.new(r.read_bytes(cert_len))
    cert = read_cert_from_reader(cert_r)

    info_name = r.read_utf8_string
    info_icon = r.read_utf8_string
    info_desc = r.read_utf8_string
    info_trust = r.read_byte

    pub_count = r.read_varint
    public_keyring = {}
    pub_count.times do
      k = r.read_utf8_string
      v_len = r.read_varint
      public_keyring[k] = encode_base64(r.read_bytes(v_len))
    end

    dec_count = r.read_varint
    decrypted = {}
    dec_count.times do
      k = r.read_utf8_string
      decrypted[k] = r.read_utf8_string
    end

    cert.merge(
      certifier_info: {
        name: info_name,
        icon_url: info_icon,
        description: info_desc,
        trust: info_trust
      },
      publicly_revealed_keyring: public_keyring,
      decrypted_fields: decrypted
    )
  end
  { total_certificates: total, certificates: certs }
end

.read_encrypt_params(r) ⇒ Object



1339
1340
1341
1342
1343
1344
1345
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1339

def read_encrypt_params(r)
  args = read_key_related_from_reader(r)
  pt_len = r.read_varint
  args[:plaintext] = r.read_bytes(pt_len).bytes
  args[:seek_permission] = r.read_optional_bool
  args
end

.read_encrypt_result(r) ⇒ Object



1353
1354
1355
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1353

def read_encrypt_result(r)
  { ciphertext: r.read_remaining.bytes }
end

.read_get_header_for_height_params(r) ⇒ Object



1941
1942
1943
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1941

def read_get_header_for_height_params(r)
  { height: r.read_varint }
end

.read_get_header_for_height_result(r) ⇒ Object



1950
1951
1952
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1950

def read_get_header_for_height_result(r)
  { header: r.read_remaining.unpack1('H*') }
end

.read_get_height_params(_r) ⇒ Object



1921
1922
1923
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1921

def read_get_height_params(_r)
  {}
end

.read_get_height_result(r) ⇒ Object



1929
1930
1931
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1929

def read_get_height_result(r)
  { height: r.read_varint }
end

.read_get_network_params(_r) ⇒ Object



1960
1961
1962
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1960

def read_get_network_params(_r)
  {}
end

.read_get_network_result(r) ⇒ Object



1968
1969
1970
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1968

def read_get_network_result(r)
  { network: r.read_byte.zero? ? 'mainnet' : 'testnet' }
end

.read_get_public_key_params(r) ⇒ Object



1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1200

def read_get_public_key_params(r)
  args = {}
  identity_flag = r.read_byte
  args[:identity_key] = identity_flag == 1

  if args[:identity_key]
    args[:privileged], args[:privileged_reason] = r.read_privileged
  else
    args[:protocol_id] = r.read_protocol_id
    args[:key_id] = r.read_utf8_string
    args[:counterparty] = r.read_counterparty
    args[:privileged], args[:privileged_reason] = r.read_privileged
    args[:for_self] = r.read_optional_bool
  end

  args[:seek_permission] = r.read_optional_bool
  args
end

.read_get_public_key_result(r) ⇒ Object



1223
1224
1225
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1223

def read_get_public_key_result(r)
  { public_key: r.read_remaining.unpack1('H*') }
end

.read_get_version_params(_r) ⇒ Object



1978
1979
1980
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1978

def read_get_version_params(_r)
  {}
end

.read_get_version_result(r) ⇒ Object



1987
1988
1989
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1987

def read_get_version_result(r)
  { version: r.read_remaining.force_encoding('UTF-8') }
end

.read_internalize_action_params(r) ⇒ Object



1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1000

def read_internalize_action_params(r)
  args = {}
  tx_len = r.read_varint
  args[:tx] = r.read_bytes(tx_len).bytes

  outputs_count = r.read_varint
  args[:outputs] = outputs_count.times.map do
    out = {}
    out[:output_index] = r.read_varint
    proto_flag = r.read_byte
    if proto_flag == 1
      out[:protocol] = 'wallet payment'
      rem = {}
      rem[:sender_identity_key] = r.read_bytes(33).unpack1('H*')
      pref_len = r.read_varint
      rem[:derivation_prefix] = encode_base64(r.read_bytes(pref_len))
      suf_len = r.read_varint
      rem[:derivation_suffix] = encode_base64(r.read_bytes(suf_len))
      out[:payment_remittance] = rem
    else
      out[:protocol] = 'basket insertion'
      rem = {}
      rem[:basket] = r.read_utf8_string
      rem[:custom_instructions] = r.read_optional_utf8_string
      tags_count = r.read_varint
      rem[:tags] = tags_count.times.map { r.read_utf8_string }
      out[:insertion_remittance] = rem
    end
    out
  end

  args[:labels] = r.read_string_array
  args[:description] = r.read_utf8_string
  args[:seek_permission] = r.read_optional_bool
  args
end

.read_internalize_action_result(_r) ⇒ Object



1041
1042
1043
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1041

def read_internalize_action_result(_r)
  { accepted: true }
end

.read_is_authenticated_params(_r) ⇒ Object



1887
1888
1889
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1887

def read_is_authenticated_params(_r)
  {}
end

.read_is_authenticated_result(r) ⇒ Object



1895
1896
1897
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1895

def read_is_authenticated_result(r)
  { authenticated: r.read_byte == 1 }
end

Reads key-related params: protocolID, keyID, counterparty, privileged, privilegedReason.



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 173

def read_key_related_from_reader(r)
  protocol_id = r.read_protocol_id
  key_id = r.read_utf8_string
  counterparty = r.read_counterparty
  privileged, privileged_reason = r.read_privileged
  {
    protocol_id: protocol_id,
    key_id: key_id,
    counterparty: counterparty,
    privileged: privileged,
    privileged_reason: privileged_reason
  }
end

.read_list_actions_params(r) ⇒ Object



824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 824

def read_list_actions_params(r)
  args = {}
  label_count = r.read_varint
  args[:labels] = label_count.times.map { r.read_utf8_string }

  lqm_flag = r.read_int8
  args[:label_query_mode] = case lqm_flag
                            when 1 then 'any'
                            when 2 then 'all'
                            end

  LIST_ACTIONS_INCLUDE_FLAGS.each do |flag|
    args[flag] = r.read_optional_bool
  end

  limit = r.read_signed_varint
  args[:limit] = limit == -1 ? nil : limit
  offset = r.read_signed_varint
  args[:offset] = offset == -1 ? nil : offset
  args[:seek_permission] = r.read_optional_bool
  args
end

.read_list_actions_result(r) ⇒ Object



899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 899

def read_list_actions_result(r)
  total = r.read_varint
  actions = total.times.map do
    action = {}
    action[:txid] = r.read_bytes(32).unpack1('H*')
    action[:satoshis] = r.read_varint
    action[:status] = {
      1 => 'completed', 2 => 'unprocessed', 3 => 'sending',
      4 => 'unproven', 5 => 'unsigned', 6 => 'nosend',
      7 => 'nonfinal', 8 => 'failed'
    }[r.read_int8]
    action[:is_outgoing] = r.read_int8 == 1
    action[:description] = r.read_utf8_string
    action[:labels] = r.read_string_array
    action[:version] = r.read_varint
    action[:lock_time] = r.read_varint

    inputs_count = r.read_signed_varint
    action[:inputs] = if inputs_count == -1
                        nil
                      else
                        inputs_count.times.map do
                          inp = {}
                          inp[:source_outpoint] = read_outpoint_str(r)
                          inp[:source_satoshis] = r.read_varint
                          sls = r.read_optional_byte_array
                          inp[:source_locking_script] = sls&.unpack1('H*')
                          us = r.read_optional_byte_array
                          inp[:unlocking_script] = us&.unpack1('H*')
                          inp[:input_description] = r.read_utf8_string
                          inp[:sequence_number] = r.read_varint
                          inp
                        end
                      end

    outputs_count = r.read_signed_varint
    action[:outputs] = if outputs_count == -1
                         nil
                       else
                         outputs_count.times.map do
                           out = {}
                           out[:output_index] = r.read_varint
                           out[:satoshis] = r.read_varint
                           ls = r.read_optional_byte_array
                           out[:locking_script] = ls&.unpack1('H*')
                           out[:spendable] = r.read_int8 == 1
                           out[:output_description] = r.read_utf8_string
                           out[:basket] = r.read_optional_utf8_string
                           out[:tags] = r.read_string_array
                           out[:custom_instructions] = r.read_optional_utf8_string
                           out
                         end
                       end

    action
  end

  { total_actions: total, actions: actions }
end

.read_list_certificates_params(r) ⇒ Object



1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1662

def read_list_certificates_params(r)
  args = {}
  cert_count = r.read_varint
  args[:certifiers] = cert_count.times.map { r.read_bytes(33).unpack1('H*') }

  types_count = r.read_varint
  args[:types] = types_count.times.map { encode_base64(r.read_bytes(32)) }

  limit = r.read_signed_varint
  args[:limit] = limit == -1 ? nil : limit
  offset = r.read_signed_varint
  args[:offset] = offset == -1 ? nil : offset
  args[:privileged], args[:privileged_reason] = r.read_privileged
  args
end

.read_list_certificates_result(r) ⇒ Object



1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1709

def read_list_certificates_result(r)
  total = r.read_varint
  certs = total.times.map do
    cert_len = r.read_varint
    cert_r = Reader.new(r.read_bytes(cert_len))
    cert = read_cert_from_reader(cert_r)

    keyring_flag = r.read_int8
    if keyring_flag == 1
      kr_count = r.read_varint
      keyring = {}
      kr_count.times do
        k = r.read_utf8_string
        v_len = r.read_varint
        keyring[k] = encode_base64(r.read_bytes(v_len))
      end
      cert[:keyring] = keyring
    else
      cert[:keyring] = {}
    end

    ver_len = r.read_varint
    cert[:verifier] = ver_len.zero? ? nil : r.read_bytes(ver_len).unpack1('H*')
    cert
  end
  { total_certificates: total, certificates: certs }
end

.read_list_outputs_params(r) ⇒ Object



1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1086

def read_list_outputs_params(r)
  args = {}
  args[:basket] = r.read_utf8_string

  tags_count = r.read_varint
  args[:tags] = tags_count.zero? ? nil : tags_count.times.map { r.read_utf8_string }

  tqm_flag = r.read_int8
  args[:tag_query_mode] = case tqm_flag
                          when 1 then 'all'
                          when 2 then 'any'
                          end

  inc_flag = r.read_int8
  args[:include] = case inc_flag
                   when 1 then 'locking scripts'
                   when 2 then 'entire transactions'
                   end

  args[:include_custom_instructions] = r.read_optional_bool
  args[:include_tags] = r.read_optional_bool
  args[:include_labels] = r.read_optional_bool

  limit = r.read_signed_varint
  args[:limit] = limit == -1 ? nil : limit
  offset = r.read_signed_varint
  args[:offset] = offset == -1 ? nil : offset
  args[:seek_permission] = r.read_optional_bool
  args
end

.read_list_outputs_result(r) ⇒ Object



1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1140

def read_list_outputs_result(r)
  total = r.read_varint
  beef_raw = r.read_optional_byte_array
  beef = beef_raw&.bytes

  outputs = total.times.map do
    out = {}
    out[:outpoint] = read_outpoint_str(r)
    out[:satoshis] = r.read_varint
    ls = r.read_optional_byte_array
    out[:locking_script] = ls&.unpack1('H*')
    out[:custom_instructions] = r.read_optional_utf8_string
    out[:tags] = r.read_string_array
    out[:labels] = r.read_string_array
    out
  end

  { total_outputs: total, beef: beef, outputs: outputs }
end

.read_outpoint_str(r) ⇒ Object



158
159
160
161
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 158

def read_outpoint_str(r)
  txid, index = r.read_outpoint
  "#{txid}.#{index}"
end

.read_prove_certificate_params(r) ⇒ Object



1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1753

def read_prove_certificate_params(r)
  cert = read_cert_from_reader(r)

  ftr_count = r.read_varint
  fields_to_reveal = ftr_count.times.map { r.read_utf8_string }

  verifier = r.read_bytes(33).unpack1('H*')
  privileged, privileged_reason = r.read_privileged

  { certificate: cert, fields_to_reveal: fields_to_reveal,
    verifier: verifier, privileged: privileged, privileged_reason: privileged_reason }
end

.read_prove_certificate_result(r) ⇒ Object



1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1777

def read_prove_certificate_result(r)
  kr_count = r.read_varint
  keyring = {}
  kr_count.times do
    k = r.read_utf8_string
    v_len = r.read_varint
    keyring[k] = encode_base64(r.read_bytes(v_len))
  end
  { keyring_for_verifier: keyring }
end

.read_relinquish_certificate_params(r) ⇒ Object



1798
1799
1800
1801
1802
1803
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1798

def read_relinquish_certificate_params(r)
  type = encode_base64(r.read_bytes(32))
  serial_number = encode_base64(r.read_bytes(32))
  certifier = r.read_bytes(33).unpack1('H*')
  { type: type, serial_number: serial_number, certifier: certifier }
end

.read_relinquish_certificate_result(_r) ⇒ Object



1807
1808
1809
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1807

def read_relinquish_certificate_result(_r)
  { relinquished: true }
end

.read_relinquish_output_params(r) ⇒ Object



1169
1170
1171
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1169

def read_relinquish_output_params(r)
  { basket: r.read_utf8_string, output: read_outpoint_str(r) }
end

.read_relinquish_output_result(_r) ⇒ Object



1175
1176
1177
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1175

def read_relinquish_output_result(_r)
  { relinquished: true }
end

.read_reveal_counterparty_key_linkage_params(r) ⇒ Object



1237
1238
1239
1240
1241
1242
1243
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1237

def read_reveal_counterparty_key_linkage_params(r)
  privileged, privileged_reason = r.read_privileged
  counterparty = r.read_bytes(33).unpack1('H*')
  verifier = r.read_bytes(33).unpack1('H*')
  { privileged: privileged, privileged_reason: privileged_reason,
    counterparty: counterparty, verifier: verifier }
end

.read_reveal_counterparty_key_linkage_result(r) ⇒ Object



1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1260

def read_reveal_counterparty_key_linkage_result(r)
  prover = r.read_bytes(33).unpack1('H*')
  verifier = r.read_bytes(33).unpack1('H*')
  counterparty = r.read_bytes(33).unpack1('H*')
  revelation_time = r.read_utf8_string
  el_len = r.read_varint
  encrypted_linkage = r.read_bytes(el_len).bytes
  ep_len = r.read_varint
  encrypted_linkage_proof = r.read_bytes(ep_len).bytes
  { prover: prover, verifier: verifier, counterparty: counterparty,
    revelation_time: revelation_time, encrypted_linkage: encrypted_linkage,
    encrypted_linkage_proof: encrypted_linkage_proof }
end

.read_reveal_specific_key_linkage_params(r) ⇒ Object



1283
1284
1285
1286
1287
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1283

def read_reveal_specific_key_linkage_params(r)
  args = read_key_related_from_reader(r)
  args[:verifier] = r.read_bytes(33).unpack1('H*')
  args
end

.read_reveal_specific_key_linkage_result(r) ⇒ Object



1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1307

def read_reveal_specific_key_linkage_result(r)
  prover = r.read_bytes(33).unpack1('H*')
  verifier = r.read_bytes(33).unpack1('H*')
  counterparty = r.read_bytes(33).unpack1('H*')
  level = r.read_byte
  protocol = r.read_utf8_string
  key_id = r.read_utf8_string
  el_len = r.read_varint
  encrypted_linkage = r.read_bytes(el_len).bytes
  ep_len = r.read_varint
  encrypted_linkage_proof = r.read_bytes(ep_len).bytes
  proof_type = r.read_byte
  { prover: prover, verifier: verifier, counterparty: counterparty,
    protocol_id: [level, protocol], key_id: key_id,
    encrypted_linkage: encrypted_linkage,
    encrypted_linkage_proof: encrypted_linkage_proof,
    proof_type: proof_type }
end

.read_sign_action_params(r) ⇒ Object



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 678

def read_sign_action_params(r)
  spends = {}
  spend_count = r.read_varint
  spend_count.times do
    idx = r.read_varint
    us_len = r.read_varint
    us = r.read_bytes(us_len).unpack1('H*')
    seq = r.read_signed_varint
    spends[idx] = { unlocking_script: us, sequence_number: seq == -1 ? nil : seq }
  end

  ref_len = r.read_varint
  reference = encode_base64(r.read_bytes(ref_len))

  opts_flag = r.read_int8
  options = if opts_flag == 1
              opts = {}
              opts[:accept_delayed_broadcast] = r.read_optional_bool
              opts[:return_txid_only] = r.read_optional_bool
              opts[:no_send] = r.read_optional_bool
              sw_count = r.read_signed_varint
              opts[:send_with] = if sw_count == -1
                                   nil
                                 else
                                   sw_count.times.map { r.read_bytes(32).unpack1('H*') }
                                 end
              opts
            end

  { spends: spends, reference: reference, options: options }
end

.read_sign_action_result(r) ⇒ Object



742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 742

def read_sign_action_result(r)
  result = {}
  txid_flag = r.read_int8
  result[:txid] = txid_flag == 1 ? r.read_bytes(32).unpack1('H*') : nil

  tx_flag = r.read_int8
  if tx_flag == 1
    tx_len = r.read_varint
    result[:tx] = r.read_bytes(tx_len).bytes
  else
    result[:tx] = nil
  end

  swr_count = r.read_signed_varint
  result[:send_with_results] = if swr_count == -1
                                 nil
                               else
                                 swr_count.times.map do
                                   txid = r.read_bytes(32).unpack1('H*')
                                   status = { 1 => 'unproven', 2 => 'sending', 3 => 'failed' }[r.read_int8] || 'failed'
                                   { txid: txid, status: status }
                                 end
                               end

  result
end

.read_verify_hmac_params(r) ⇒ Object



1435
1436
1437
1438
1439
1440
1441
1442
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1435

def read_verify_hmac_params(r)
  args = read_key_related_from_reader(r)
  args[:hmac] = r.read_bytes(32).bytes
  data_len = r.read_varint
  args[:data] = r.read_bytes(data_len).bytes
  args[:seek_permission] = r.read_optional_bool
  args
end

.read_verify_hmac_result(_r) ⇒ Object



1446
1447
1448
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1446

def read_verify_hmac_result(_r)
  { valid: true }
end

.read_verify_signature_params(r) ⇒ Object



1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1524

def read_verify_signature_params(r)
  args = read_key_related_from_reader(r)
  args[:for_self] = r.read_optional_bool
  sig_len = r.read_varint
  args[:signature] = r.read_bytes(sig_len).bytes
  data_type = r.read_byte
  if data_type == 1
    data_len = r.read_varint
    args[:data] = r.read_bytes(data_len).bytes
  elsif data_type == 2
    args[:hash_to_directly_verify] = r.read_bytes(32).bytes
  end
  args[:seek_permission] = r.read_optional_bool
  args
end

.read_verify_signature_result(_r) ⇒ Object



1542
1543
1544
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1542

def read_verify_signature_result(_r)
  { valid: true }
end

.read_wait_for_authentication_params(_r) ⇒ Object



1905
1906
1907
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1905

def read_wait_for_authentication_params(_r)
  {}
end

.read_wait_for_authentication_result(_r) ⇒ Object



1911
1912
1913
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1911

def read_wait_for_authentication_result(_r)
  { authenticated: true }
end

.serialize_error(code, message, stack_trace: nil) ⇒ String

Serialises an error response.

Parameters:

  • code (Integer)

    non-zero error code

  • message (String)
  • stack_trace (String, nil) (defaults to: nil)

Returns:

  • (String)

    binary frame



121
122
123
124
125
126
127
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 121

def serialize_error(code, message, stack_trace: nil)
  w = Writer.new
  w.write_byte(code)
  w.write_utf8_string(message.to_s)
  w.write_utf8_string(stack_trace.to_s)
  w.to_binary
end

.serialize_request(method_name, args, originator: nil) ⇒ String

Serialises a method call request into a binary frame.

Parameters:

  • method_name (Symbol)
  • args (Hash)
  • originator (String, nil) (defaults to: nil)

    FQDN of the calling application

Returns:

  • (String)

    binary frame (ASCII-8BIT)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 63

def serialize_request(method_name, args, originator: nil)
  code = CALL_CODES.fetch(method_name) do
    raise ArgumentError, "unknown method: #{method_name}"
  end

  w = Writer.new
  w.write_byte(code)

  orig = originator.to_s
  orig_bytes = orig.encode('UTF-8').b
  w.write_byte(orig_bytes.bytesize)
  w.write_bytes(orig_bytes)

  params_writer = Writer.new
  send(:"write_#{method_name}_params", params_writer, args)
  w.write_bytes(params_writer.to_binary)

  w.to_binary
end

.serialize_response(method_name, result) ⇒ String

Serialises a successful response.

Parameters:

  • method_name (Symbol)
  • result (Hash)

Returns:

  • (String)

    binary frame



106
107
108
109
110
111
112
113
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 106

def serialize_response(method_name, result)
  w = Writer.new
  w.write_byte(0) # error_code = 0
  result_writer = Writer.new
  send(:"write_#{method_name}_result", result_writer, result)
  w.write_bytes(result_writer.to_binary)
  w.to_binary
end

.write_abort_action_params(w, args) ⇒ Object


  1. abortAction — reference is the entire params payload (no length prefix)




773
774
775
776
777
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 773

def write_abort_action_params(w, args)
  require 'base64'
  ref_bytes = Base64.strict_decode64(args[:reference].to_s)
  w.write_bytes(ref_bytes)
end

.write_abort_action_result(w, _result) ⇒ Object



784
785
786
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 784

def write_abort_action_result(w, _result)
  # No payload — response is just the zero error byte written by serialize_response
end

.write_acquire_certificate_params(w, args) ⇒ Object


  1. acquireCertificate




1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1550

def write_acquire_certificate_params(w, args)
  w.write_bytes(decode_base64_32(args[:type].to_s))
  w.write_bytes([args[:certifier].to_s].pack('H*'))

  fields = args[:fields] || {}
  w.write_varint(fields.length)
  fields.each do |k, v|
    w.write_utf8_string(k.to_s)
    w.write_utf8_string(v.to_s)
  end

  w.write_privileged(args[:privileged], args[:privileged_reason])

  if args[:acquisition_protocol] == 'direct'
    w.write_byte(1)
    w.write_bytes(decode_base64_32(args[:serial_number].to_s))
    write_outpoint_str(w, args[:revocation_outpoint].to_s)
    sig_bytes = [args[:signature].to_s].pack('H*')
    w.write_varint(sig_bytes.bytesize)
    w.write_bytes(sig_bytes)

    keyring_revealer = args[:keyring_revealer]
    if keyring_revealer == 'certifier'
      w.write_byte(11)
    else
      kr_bytes = [keyring_revealer.to_s].pack('H*')
      w.write_bytes(kr_bytes)
    end

    keyring = args[:keyring_for_subject] || {}
    w.write_varint(keyring.length)
    keyring.each do |k, v|
      w.write_utf8_string(k.to_s)
      v_bytes = decode_base64_32_safe(v.to_s)
      w.write_varint(v_bytes.bytesize)
      w.write_bytes(v_bytes)
    end
  else
    w.write_byte(2)
    w.write_utf8_string(args[:certifier_url].to_s)
  end
end

.write_acquire_certificate_result(w, result) ⇒ Object



1636
1637
1638
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1636

def write_acquire_certificate_result(w, result)
  write_certificate_fields(w, result)
end

.write_certificate_fields(w, cert) ⇒ Object

Writes a certificate struct to the writer (without length prefix).



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 190

def write_certificate_fields(w, cert)
  # type: 32 bytes (base64-decoded)
  w.write_bytes(decode_base64_32(cert[:type]))
  # subject: 33 bytes (hex-decoded compressed pubkey)
  w.write_bytes([cert[:subject]].pack('H*'))
  # serial_number: 32 bytes (base64-decoded)
  w.write_bytes(decode_base64_32(cert[:serial_number]))
  # certifier: 33 bytes
  w.write_bytes([cert[:certifier]].pack('H*'))
  # revocation_outpoint
  write_outpoint_str(w, cert[:revocation_outpoint])
  # signature: VarInt-prefixed hex
  sig_bytes = [cert[:signature]].pack('H*')
  w.write_varint(sig_bytes.bytesize)
  w.write_bytes(sig_bytes)
  # fields: map
  fields = cert[:fields] || {}
  w.write_varint(fields.length)
  fields.each do |k, v|
    w.write_utf8_string(k.to_s)
    w.write_utf8_string(v.to_s)
  end
end

.write_create_action_params(w, args) ⇒ Object


  1. createAction




346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 346

def write_create_action_params(w, args)
  w.write_utf8_string(args[:description].to_s)
  w.write_optional_byte_array(args[:input_beef]&.pack('C*'))

  inputs = args[:inputs]
  if inputs.nil?
    w.write_signed_varint(-1)
  else
    w.write_varint(inputs.length)
    inputs.each do |inp|
      write_outpoint_str(w, inp[:outpoint])
      if inp[:unlocking_script]
        script_bytes = [inp[:unlocking_script]].pack('H*')
        w.write_varint(script_bytes.bytesize)
        w.write_bytes(script_bytes)
      else
        w.write_signed_varint(-1)
        w.write_varint(inp[:unlocking_script_length] || 0)
      end
      w.write_utf8_string(inp[:input_description].to_s)
      if inp[:sequence_number]
        w.write_varint(inp[:sequence_number])
      else
        w.write_signed_varint(-1)
      end
    end
  end

  outputs = args[:outputs]
  if outputs.nil?
    w.write_signed_varint(-1)
  else
    w.write_varint(outputs.length)
    outputs.each do |out|
      script_bytes = [out[:locking_script].to_s].pack('H*')
      w.write_varint(script_bytes.bytesize)
      w.write_bytes(script_bytes)
      w.write_varint(out[:satoshis].to_i)
      w.write_utf8_string(out[:output_description].to_s)
      w.write_optional_utf8_string(out[:basket])
      w.write_optional_utf8_string(out[:custom_instructions])
      w.write_string_array(out[:tags])
    end
  end

  if args[:lock_time]
    w.write_varint(args[:lock_time])
  else
    w.write_signed_varint(-1)
  end

  if args[:version]
    w.write_varint(args[:version])
  else
    w.write_signed_varint(-1)
  end

  w.write_string_array(args[:labels])

  opts = args[:options]
  if opts.nil?
    w.write_int8(0)
  else
    w.write_int8(1)
    w.write_optional_bool(opts[:sign_and_process])
    w.write_optional_bool(opts[:accept_delayed_broadcast])
    if opts[:trust_self] == 'known'
      w.write_int8(1)
    elsif opts[:trust_self].nil?
      w.write_int8(-1)
    else
      w.write_int8(0)
    end
    known_txids = opts[:known_txids]
    if known_txids.nil?
      w.write_signed_varint(-1)
    else
      w.write_varint(known_txids.length)
      known_txids.each { |txid| w.write_bytes([txid].pack('H*')) }
    end
    w.write_optional_bool(opts[:return_txid_only])
    w.write_optional_bool(opts[:no_send])
    no_send_change = opts[:no_send_change]
    if no_send_change.nil?
      w.write_signed_varint(-1)
    else
      w.write_varint(no_send_change.length)
      no_send_change.each { |op| write_outpoint_str(w, op) }
    end
    send_with = opts[:send_with]
    if send_with.nil?
      w.write_signed_varint(-1)
    else
      w.write_varint(send_with.length)
      send_with.each { |txid| w.write_bytes([txid].pack('H*')) }
    end
    w.write_optional_bool(opts[:randomize_outputs])
  end
end

.write_create_action_result(w, result) ⇒ Object



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 537

def write_create_action_result(w, result)
  txid = result[:txid]
  if txid && !txid.empty?
    w.write_int8(1)
    w.write_bytes([txid].pack('H*'))
  else
    w.write_int8(0)
  end

  tx = result[:tx]
  if tx
    w.write_int8(1)
    tx_bytes = tx.is_a?(Array) ? tx.pack('C*') : tx
    w.write_varint(tx_bytes.bytesize)
    w.write_bytes(tx_bytes)
  else
    w.write_int8(0)
  end

  no_send_change = result[:no_send_change]
  if no_send_change
    w.write_varint(no_send_change.length)
    no_send_change.each { |op| write_outpoint_str(w, op) }
  else
    w.write_signed_varint(-1)
  end

  send_with_results = result[:send_with_results]
  if send_with_results
    w.write_varint(send_with_results.length)
    send_with_results.each do |swr|
      w.write_bytes([swr[:txid]].pack('H*'))
      status_code = { 'unproven' => 1, 'sending' => 2, 'failed' => 3 }[swr[:status]] || 1
      w.write_int8(status_code)
    end
  else
    w.write_signed_varint(-1)
  end

  signable = result[:signable_transaction]
  if signable
    w.write_int8(1)
    tx_bytes = signable[:tx].is_a?(Array) ? signable[:tx].pack('C*') : signable[:tx]
    w.write_varint(tx_bytes.bytesize)
    w.write_bytes(tx_bytes)
    require 'base64'
    ref_bytes = Base64.strict_decode64(signable[:reference])
    w.write_varint(ref_bytes.bytesize)
    w.write_bytes(ref_bytes)
  else
    w.write_int8(0)
  end
end

.write_create_hmac_params(w, args) ⇒ Object


  1. createHmac




1392
1393
1394
1395
1396
1397
1398
1399
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1392

def write_create_hmac_params(w, args)
  write_key_related_params(w, args)
  data = args[:data]
  data_bytes = data.is_a?(Array) ? data.pack('C*') : data.to_s.b
  w.write_varint(data_bytes.bytesize)
  w.write_bytes(data_bytes)
  w.write_optional_bool(args[:seek_permission])
end

.write_create_hmac_result(w, result) ⇒ Object



1409
1410
1411
1412
1413
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1409

def write_create_hmac_result(w, result)
  hmac = result[:hmac]
  hmac_bytes = hmac.is_a?(Array) ? hmac.pack('C*') : hmac.to_s.b
  w.write_bytes(hmac_bytes)
end

.write_create_signature_params(w, args) ⇒ Object


  1. createSignature




1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1454

def write_create_signature_params(w, args)
  write_key_related_params(w, args)
  if args[:data]
    w.write_byte(1)
    data = args[:data]
    data_bytes = data.is_a?(Array) ? data.pack('C*') : data.to_s.b
    w.write_varint(data_bytes.bytesize)
    w.write_bytes(data_bytes)
  elsif args[:hash_to_directly_sign]
    w.write_byte(2)
    hash = args[:hash_to_directly_sign]
    hash_bytes = hash.is_a?(Array) ? hash.pack('C*') : hash.to_s.b
    w.write_bytes(hash_bytes)
  else
    w.write_byte(0)
  end
  w.write_optional_bool(args[:seek_permission])
end

.write_create_signature_result(w, result) ⇒ Object



1486
1487
1488
1489
1490
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1486

def write_create_signature_result(w, result)
  sig = result[:signature]
  sig_bytes = sig.is_a?(Array) ? sig.pack('C*') : sig.to_s.b
  w.write_bytes(sig_bytes)
end

.write_decrypt_params(w, args) ⇒ Object


  1. decrypt




1361
1362
1363
1364
1365
1366
1367
1368
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1361

def write_decrypt_params(w, args)
  write_key_related_params(w, args)
  ct = args[:ciphertext]
  ct_bytes = ct.is_a?(Array) ? ct.pack('C*') : ct.to_s.b
  w.write_varint(ct_bytes.bytesize)
  w.write_bytes(ct_bytes)
  w.write_optional_bool(args[:seek_permission])
end

.write_decrypt_result(w, result) ⇒ Object



1378
1379
1380
1381
1382
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1378

def write_decrypt_result(w, result)
  pt = result[:plaintext]
  pt_bytes = pt.is_a?(Array) ? pt.pack('C*') : pt.to_s.b
  w.write_bytes(pt_bytes)
end

.write_discover_by_attributes_params(w, args) ⇒ Object


  1. discoverByAttributes




1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1845

def write_discover_by_attributes_params(w, args)
  attributes = args[:attributes] || {}
  w.write_varint(attributes.length)
  attributes.each do |k, v|
    w.write_utf8_string(k.to_s)
    w.write_utf8_string(v.to_s)
  end
  w.write_signed_varint(args[:limit].nil? ? -1 : args[:limit])
  w.write_signed_varint(args[:offset].nil? ? -1 : args[:offset])
  w.write_optional_bool(args[:seek_permission])
end

.write_discover_by_attributes_result(w, result) ⇒ Object



1873
1874
1875
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1873

def write_discover_by_attributes_result(w, result)
  write_discovery_result(w, result)
end

.write_discover_by_identity_key_params(w, args) ⇒ Object


  1. discoverByIdentityKey




1815
1816
1817
1818
1819
1820
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1815

def write_discover_by_identity_key_params(w, args)
  w.write_bytes([args[:identity_key].to_s].pack('H*'))
  w.write_signed_varint(args[:limit].nil? ? -1 : args[:limit])
  w.write_signed_varint(args[:offset].nil? ? -1 : args[:offset])
  w.write_optional_bool(args[:seek_permission])
end

.write_discover_by_identity_key_result(w, result) ⇒ Object



1833
1834
1835
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1833

def write_discover_by_identity_key_result(w, result)
  write_discovery_result(w, result)
end

.write_discovery_result(w, result) ⇒ Object

Serialises a discovery certificate result (shared by discoverByIdentityKey/Attributes).



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 256

def write_discovery_result(w, result)
  certs = result[:certificates] || []
  w.write_varint(result[:total_certificates] || certs.length)
  certs.each do |cert|
    # Write certificate binary (length-prefixed)
    cert_w = Writer.new
    write_certificate_fields(cert_w, cert)
    cert_bin = cert_w.to_binary
    w.write_varint(cert_bin.bytesize)
    w.write_bytes(cert_bin)

    # certifierInfo
    info = cert[:certifier_info] || {}
    w.write_utf8_string(info[:name].to_s)
    w.write_utf8_string(info[:icon_url].to_s)
    w.write_utf8_string(info[:description].to_s)
    w.write_byte(info[:trust].to_i)

    # publiclyRevealedKeyring: map of field→base64
    keyring = cert[:publicly_revealed_keyring] || {}
    w.write_varint(keyring.length)
    keyring.each do |k, v|
      w.write_utf8_string(k.to_s)
      val_bytes = decode_base64_32_safe(v.to_s)
      w.write_varint(val_bytes.bytesize)
      w.write_bytes(val_bytes)
    end

    # decryptedFields: map of field→utf8
    decrypted = cert[:decrypted_fields] || {}
    w.write_varint(decrypted.length)
    decrypted.each do |k, v|
      w.write_utf8_string(k.to_s)
      w.write_utf8_string(v.to_s)
    end
  end
end

.write_encrypt_params(w, args) ⇒ Object


  1. encrypt




1330
1331
1332
1333
1334
1335
1336
1337
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1330

def write_encrypt_params(w, args)
  write_key_related_params(w, args)
  pt = args[:plaintext]
  pt_bytes = pt.is_a?(Array) ? pt.pack('C*') : pt.to_s.b
  w.write_varint(pt_bytes.bytesize)
  w.write_bytes(pt_bytes)
  w.write_optional_bool(args[:seek_permission])
end

.write_encrypt_result(w, result) ⇒ Object



1347
1348
1349
1350
1351
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1347

def write_encrypt_result(w, result)
  ct = result[:ciphertext]
  ct_bytes = ct.is_a?(Array) ? ct.pack('C*') : ct.to_s.b
  w.write_bytes(ct_bytes)
end

.write_get_header_for_height_params(w, args) ⇒ Object


  1. getHeaderForHeight




1937
1938
1939
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1937

def write_get_header_for_height_params(w, args)
  w.write_varint(args[:height].to_i)
end

.write_get_header_for_height_result(w, result) ⇒ Object



1945
1946
1947
1948
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1945

def write_get_header_for_height_result(w, result)
  header_bytes = [result[:header].to_s].pack('H*')
  w.write_bytes(header_bytes)
end

.write_get_height_params(w, _args) ⇒ Object


  1. getHeight (no params)




1919
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1919

def write_get_height_params(w, _args); end

.write_get_height_result(w, result) ⇒ Object



1925
1926
1927
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1925

def write_get_height_result(w, result)
  w.write_varint(result[:height].to_i)
end

.write_get_network_params(w, _args) ⇒ Object


  1. getNetwork (no params)




1958
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1958

def write_get_network_params(w, _args); end

.write_get_network_result(w, result) ⇒ Object



1964
1965
1966
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1964

def write_get_network_result(w, result)
  w.write_byte(result[:network] == 'mainnet' ? 0 : 1)
end

.write_get_public_key_params(w, args) ⇒ Object


  1. getPublicKey




1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1183

def write_get_public_key_params(w, args)
  identity = args[:identity_key] ? 1 : 0
  w.write_byte(identity)

  if identity == 1
    w.write_privileged(args[:privileged], args[:privileged_reason])
  else
    w.write_protocol_id(args[:protocol_id])
    w.write_utf8_string(args[:key_id].to_s)
    w.write_counterparty(args[:counterparty])
    w.write_privileged(args[:privileged], args[:privileged_reason])
    w.write_optional_bool(args[:for_self])
  end

  w.write_optional_bool(args[:seek_permission])
end

.write_get_public_key_result(w, result) ⇒ Object



1219
1220
1221
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1219

def write_get_public_key_result(w, result)
  w.write_bytes([result[:public_key].to_s].pack('H*'))
end

.write_get_version_params(w, _args) ⇒ Object


  1. getVersion (no params)




1976
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1976

def write_get_version_params(w, _args); end

.write_get_version_result(w, result) ⇒ Object



1982
1983
1984
1985
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1982

def write_get_version_result(w, result)
  version_bytes = result[:version].to_s.encode('UTF-8').b
  w.write_bytes(version_bytes)
end

.write_internalize_action_params(w, args) ⇒ Object


  1. internalizeAction




963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 963

def write_internalize_action_params(w, args)
  tx = args[:tx]
  tx_bytes = tx.is_a?(Array) ? tx.pack('C*') : tx.to_s.b
  w.write_varint(tx_bytes.bytesize)
  w.write_bytes(tx_bytes)

  outputs = args[:outputs] || []
  w.write_varint(outputs.length)
  outputs.each do |out|
    w.write_varint(out[:output_index].to_i)
    if out[:protocol] == 'wallet payment'
      w.write_byte(1)
      rem = out[:payment_remittance] || {}
      w.write_bytes([rem[:sender_identity_key].to_s].pack('H*'))
      require 'base64'
      pref = Base64.strict_decode64(rem[:derivation_prefix].to_s)
      w.write_varint(pref.bytesize)
      w.write_bytes(pref)
      suf = Base64.strict_decode64(rem[:derivation_suffix].to_s)
      w.write_varint(suf.bytesize)
      w.write_bytes(suf)
    else
      w.write_byte(2)
      rem = out[:insertion_remittance] || {}
      w.write_utf8_string(rem[:basket].to_s)
      w.write_optional_utf8_string(rem[:custom_instructions])
      tags = rem[:tags] || []
      w.write_varint(tags.length)
      tags.each { |t| w.write_utf8_string(t) }
    end
  end

  w.write_string_array(args[:labels])
  w.write_utf8_string(args[:description].to_s)
  w.write_optional_bool(args[:seek_permission])
end

.write_internalize_action_result(w, _result) ⇒ Object



1037
1038
1039
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1037

def write_internalize_action_result(w, _result)
  # No payload
end

.write_is_authenticated_params(w, _args) ⇒ Object


  1. isAuthenticated (no params)




1885
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1885

def write_is_authenticated_params(w, _args); end

.write_is_authenticated_result(w, result) ⇒ Object



1891
1892
1893
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1891

def write_is_authenticated_result(w, result)
  w.write_byte(result[:authenticated] ? 1 : 0)
end

— key-related params (shared by encrypt/decrypt/hmac/signature/revealSpecific) —



165
166
167
168
169
170
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 165

def write_key_related_params(w, args)
  w.write_protocol_id(args[:protocol_id])
  w.write_utf8_string(args[:key_id].to_s)
  w.write_counterparty(args[:counterparty])
  w.write_privileged(args[:privileged], args[:privileged_reason])
end

.write_list_actions_params(w, args) ⇒ Object



801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 801

def write_list_actions_params(w, args)
  labels = args[:labels] || []
  w.write_varint(labels.length)
  labels.each { |l| w.write_utf8_string(l) }

  lqm = args[:label_query_mode]
  if lqm.nil?
    w.write_int8(-1)
  elsif lqm == 'any'
    w.write_int8(1)
  else
    w.write_int8(2)
  end

  LIST_ACTIONS_INCLUDE_FLAGS.each do |flag|
    w.write_optional_bool(args[flag])
  end

  w.write_signed_varint(args[:limit].nil? ? -1 : args[:limit])
  w.write_signed_varint(args[:offset].nil? ? -1 : args[:offset])
  w.write_optional_bool(args[:seek_permission])
end

.write_list_actions_result(w, result) ⇒ Object



847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 847

def write_list_actions_result(w, result)
  actions = result[:actions] || []
  w.write_varint(result[:total_actions] || actions.length)
  actions.each do |action|
    w.write_bytes([action[:txid]].pack('H*'))
    w.write_varint(action[:satoshis].to_i)
    status_code = {
      'completed' => 1, 'unprocessed' => 2, 'sending' => 3,
      'unproven' => 4, 'unsigned' => 5, 'nosend' => 6,
      'nonfinal' => 7, 'failed' => 8
    }[action[:status]] || -1
    w.write_int8(status_code)
    w.write_int8(action[:is_outgoing] ? 1 : 0)
    w.write_utf8_string(action[:description].to_s)
    w.write_string_array(action[:labels])
    w.write_varint(action[:version].to_i)
    w.write_varint(action[:lock_time].to_i)

    inputs = action[:inputs]
    if inputs.nil?
      w.write_signed_varint(-1)
    else
      w.write_varint(inputs.length)
      inputs.each do |inp|
        write_outpoint_str(w, inp[:source_outpoint])
        w.write_varint(inp[:source_satoshis].to_i)
        w.write_optional_byte_array(inp[:source_locking_script] ? [inp[:source_locking_script]].pack('H*') : nil)
        w.write_optional_byte_array(inp[:unlocking_script] ? [inp[:unlocking_script]].pack('H*') : nil)
        w.write_utf8_string(inp[:input_description].to_s)
        w.write_varint(inp[:sequence_number].to_i)
      end
    end

    outputs = action[:outputs]
    if outputs.nil?
      w.write_signed_varint(-1)
    else
      w.write_varint(outputs.length)
      outputs.each do |out|
        w.write_varint(out[:output_index].to_i)
        w.write_varint(out[:satoshis].to_i)
        w.write_optional_byte_array(out[:locking_script] ? [out[:locking_script]].pack('H*') : nil)
        w.write_int8(out[:spendable] ? 1 : 0)
        w.write_utf8_string(out[:output_description].to_s)
        w.write_optional_utf8_string(out[:basket])
        w.write_string_array(out[:tags])
        w.write_optional_utf8_string(out[:custom_instructions])
      end
    end
  end
end

.write_list_certificates_params(w, args) ⇒ Object


  1. listCertificates




1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1648

def write_list_certificates_params(w, args)
  certifiers = args[:certifiers] || []
  w.write_varint(certifiers.length)
  certifiers.each { |c| w.write_bytes([c].pack('H*')) }

  types = args[:types] || []
  w.write_varint(types.length)
  types.each { |t| w.write_bytes(decode_base64_32(t)) }

  w.write_signed_varint(args[:limit].nil? ? -1 : args[:limit])
  w.write_signed_varint(args[:offset].nil? ? -1 : args[:offset])
  w.write_privileged(args[:privileged], args[:privileged_reason])
end

.write_list_certificates_result(w, result) ⇒ Object



1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1678

def write_list_certificates_result(w, result)
  certs = result[:certificates] || []
  w.write_varint(result[:total_certificates] || certs.length)
  certs.each do |cert|
    cert_w = Writer.new
    write_certificate_fields(cert_w, cert)
    cert_bin = cert_w.to_binary
    w.write_varint(cert_bin.bytesize)
    w.write_bytes(cert_bin)

    keyring = cert[:keyring]
    if keyring && !keyring.empty?
      w.write_int8(1)
      w.write_varint(keyring.length)
      keyring.each do |k, v|
        w.write_utf8_string(k.to_s)
        v_bytes = decode_base64_32_safe(v.to_s)
        w.write_varint(v_bytes.bytesize)
        w.write_bytes(v_bytes)
      end
    else
      w.write_int8(0)
    end

    verifier_hex = cert[:verifier].to_s
    verifier_bytes = verifier_hex.empty? ? ''.b : [verifier_hex].pack('H*')
    w.write_varint(verifier_bytes.bytesize)
    w.write_bytes(verifier_bytes)
  end
end

.write_list_outputs_params(w, args) ⇒ Object


  1. listOutputs




1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1049

def write_list_outputs_params(w, args)
  w.write_utf8_string(args[:basket].to_s)

  tags = args[:tags]
  if tags.nil? || tags.empty?
    w.write_varint(0)
  else
    w.write_varint(tags.length)
    tags.each { |t| w.write_utf8_string(t) }
  end

  tqm = args[:tag_query_mode]
  if tqm == 'all'
    w.write_int8(1)
  elsif tqm == 'any'
    w.write_int8(2)
  else
    w.write_int8(-1)
  end

  inc = args[:include]
  if inc == 'locking scripts'
    w.write_int8(1)
  elsif inc == 'entire transactions'
    w.write_int8(2)
  else
    w.write_int8(-1)
  end

  w.write_optional_bool(args[:include_custom_instructions])
  w.write_optional_bool(args[:include_tags])
  w.write_optional_bool(args[:include_labels])
  w.write_signed_varint(args[:limit].nil? ? -1 : args[:limit])
  w.write_signed_varint(args[:offset].nil? ? -1 : args[:offset])
  w.write_optional_bool(args[:seek_permission])
end

.write_list_outputs_result(w, result) ⇒ Object



1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1117

def write_list_outputs_result(w, result)
  outputs = result[:outputs] || []
  w.write_varint(result[:total_outputs] || outputs.length)

  beef = result[:beef]
  if beef
    beef_bytes = beef.is_a?(Array) ? beef.pack('C*') : beef
    w.write_varint(beef_bytes.bytesize)
    w.write_bytes(beef_bytes)
  else
    w.write_signed_varint(-1)
  end

  outputs.each do |out|
    write_outpoint_str(w, out[:outpoint])
    w.write_varint(out[:satoshis].to_i)
    w.write_optional_byte_array(out[:locking_script] ? [out[:locking_script]].pack('H*') : nil)
    w.write_optional_utf8_string(out[:custom_instructions])
    w.write_string_array(out[:tags])
    w.write_string_array(out[:labels])
  end
end

.write_outpoint_str(w, outpoint_str) ⇒ Object

— write_outpoint / read_outpoint helpers —



153
154
155
156
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 153

def write_outpoint_str(w, outpoint_str)
  txid, index = outpoint_str.split('.')
  w.write_outpoint(txid, index.to_i)
end

.write_prove_certificate_params(w, args) ⇒ Object


  1. proveCertificate




1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1741

def write_prove_certificate_params(w, args)
  cert = args[:certificate] || {}
  write_certificate_fields(w, cert)

  fields_to_reveal = args[:fields_to_reveal] || []
  w.write_varint(fields_to_reveal.length)
  fields_to_reveal.each { |f| w.write_utf8_string(f) }

  w.write_bytes([args[:verifier].to_s].pack('H*'))
  w.write_privileged(args[:privileged], args[:privileged_reason])
end

.write_prove_certificate_result(w, result) ⇒ Object



1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1766

def write_prove_certificate_result(w, result)
  keyring = result[:keyring_for_verifier] || {}
  w.write_varint(keyring.length)
  keyring.each do |k, v|
    w.write_utf8_string(k.to_s)
    v_bytes = decode_base64_32_safe(v.to_s)
    w.write_varint(v_bytes.bytesize)
    w.write_bytes(v_bytes)
  end
end

.write_relinquish_certificate_params(w, args) ⇒ Object


  1. relinquishCertificate




1792
1793
1794
1795
1796
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1792

def write_relinquish_certificate_params(w, args)
  w.write_bytes(decode_base64_32(args[:type].to_s))
  w.write_bytes(decode_base64_32(args[:serial_number].to_s))
  w.write_bytes([args[:certifier].to_s].pack('H*'))
end

.write_relinquish_certificate_result(w, _result) ⇒ Object



1805
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1805

def write_relinquish_certificate_result(w, _result); end

.write_relinquish_output_params(w, args) ⇒ Object


  1. relinquishOutput




1164
1165
1166
1167
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1164

def write_relinquish_output_params(w, args)
  w.write_utf8_string(args[:basket].to_s)
  write_outpoint_str(w, args[:output].to_s)
end

.write_relinquish_output_result(w, _result) ⇒ Object



1173
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1173

def write_relinquish_output_result(w, _result); end

.write_reveal_counterparty_key_linkage_params(w, args) ⇒ Object


  1. revealCounterpartyKeyLinkage




1231
1232
1233
1234
1235
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1231

def write_reveal_counterparty_key_linkage_params(w, args)
  w.write_privileged(args[:privileged], args[:privileged_reason])
  w.write_bytes([args[:counterparty].to_s].pack('H*'))
  w.write_bytes([args[:verifier].to_s].pack('H*'))
end

.write_reveal_counterparty_key_linkage_result(w, result) ⇒ Object



1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1245

def write_reveal_counterparty_key_linkage_result(w, result)
  w.write_bytes([result[:prover].to_s].pack('H*'))
  w.write_bytes([result[:verifier].to_s].pack('H*'))
  w.write_bytes([result[:counterparty].to_s].pack('H*'))
  w.write_utf8_string(result[:revelation_time].to_s)
  enc_link = result[:encrypted_linkage]
  enc_link = enc_link.is_a?(Array) ? enc_link.pack('C*') : enc_link.to_s.b
  w.write_varint(enc_link.bytesize)
  w.write_bytes(enc_link)
  enc_proof = result[:encrypted_linkage_proof]
  enc_proof = enc_proof.is_a?(Array) ? enc_proof.pack('C*') : enc_proof.to_s.b
  w.write_varint(enc_proof.bytesize)
  w.write_bytes(enc_proof)
end

.write_reveal_specific_key_linkage_params(w, args) ⇒ Object


  1. revealSpecificKeyLinkage




1278
1279
1280
1281
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1278

def write_reveal_specific_key_linkage_params(w, args)
  write_key_related_params(w, args)
  w.write_bytes([args[:verifier].to_s].pack('H*'))
end

.write_reveal_specific_key_linkage_result(w, result) ⇒ Object



1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1289

def write_reveal_specific_key_linkage_result(w, result)
  w.write_bytes([result[:prover].to_s].pack('H*'))
  w.write_bytes([result[:verifier].to_s].pack('H*'))
  w.write_bytes([result[:counterparty].to_s].pack('H*'))
  w.write_byte(result[:protocol_id][0].to_i)
  w.write_utf8_string(result[:protocol_id][1].to_s)
  w.write_utf8_string(result[:key_id].to_s)
  el = result[:encrypted_linkage]
  el = el.is_a?(Array) ? el.pack('C*') : el.to_s.b
  w.write_varint(el.bytesize)
  w.write_bytes(el)
  ep = result[:encrypted_linkage_proof]
  ep = ep.is_a?(Array) ? ep.pack('C*') : ep.to_s.b
  w.write_varint(ep.bytesize)
  w.write_bytes(ep)
  w.write_byte(result[:proof_type].to_i)
end

.write_sign_action_params(w, args) ⇒ Object


  1. signAction




640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 640

def write_sign_action_params(w, args)
  spends = args[:spends] || {}
  w.write_varint(spends.length)
  spends.each do |idx, spend|
    w.write_varint(idx.to_i)
    script_bytes = [spend[:unlocking_script].to_s].pack('H*')
    w.write_varint(script_bytes.bytesize)
    w.write_bytes(script_bytes)
    if spend[:sequence_number]
      w.write_varint(spend[:sequence_number])
    else
      w.write_signed_varint(-1)
    end
  end

  require 'base64'
  ref_bytes = Base64.strict_decode64(args[:reference].to_s)
  w.write_varint(ref_bytes.bytesize)
  w.write_bytes(ref_bytes)

  opts = args[:options]
  if opts.nil?
    w.write_int8(0)
  else
    w.write_int8(1)
    w.write_optional_bool(opts[:accept_delayed_broadcast])
    w.write_optional_bool(opts[:return_txid_only])
    w.write_optional_bool(opts[:no_send])
    send_with = opts[:send_with]
    if send_with.nil?
      w.write_signed_varint(-1)
    else
      w.write_varint(send_with.length)
      send_with.each { |txid| w.write_bytes([txid].pack('H*')) }
    end
  end
end

.write_sign_action_result(w, result) ⇒ Object



710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 710

def write_sign_action_result(w, result)
  txid = result[:txid]
  if txid && !txid.empty?
    w.write_int8(1)
    w.write_bytes([txid].pack('H*'))
  else
    w.write_int8(0)
  end

  tx = result[:tx]
  if tx
    w.write_int8(1)
    tx_bytes = tx.is_a?(Array) ? tx.pack('C*') : tx
    w.write_varint(tx_bytes.bytesize)
    w.write_bytes(tx_bytes)
  else
    w.write_int8(0)
  end

  swr = result[:send_with_results]
  if swr
    w.write_varint(swr.length)
    swr.each do |item|
      w.write_bytes([item[:txid]].pack('H*'))
      status_code = { 'unproven' => 1, 'sending' => 2, 'failed' => 3 }[item[:status]] || 1
      w.write_int8(status_code)
    end
  else
    w.write_signed_varint(-1)
  end
end

.write_verify_hmac_params(w, args) ⇒ Object


  1. verifyHmac




1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1423

def write_verify_hmac_params(w, args)
  write_key_related_params(w, args)
  hmac = args[:hmac]
  hmac_bytes = hmac.is_a?(Array) ? hmac.pack('C*') : hmac.to_s.b
  w.write_bytes(hmac_bytes) # fixed 32 bytes, no length prefix
  data = args[:data]
  data_bytes = data.is_a?(Array) ? data.pack('C*') : data.to_s.b
  w.write_varint(data_bytes.bytesize)
  w.write_bytes(data_bytes)
  w.write_optional_bool(args[:seek_permission])
end

.write_verify_hmac_result(w, _result) ⇒ Object



1444
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1444

def write_verify_hmac_result(w, _result); end

.write_verify_signature_params(w, args) ⇒ Object


  1. verifySignature




1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1500

def write_verify_signature_params(w, args)
  write_key_related_params(w, args)
  w.write_optional_bool(args[:for_self])
  sig = args[:signature]
  sig_bytes = sig.is_a?(Array) ? sig.pack('C*') : sig.to_s.b
  w.write_varint(sig_bytes.bytesize)
  w.write_bytes(sig_bytes)
  if args[:data]
    w.write_byte(1)
    data = args[:data]
    data_bytes = data.is_a?(Array) ? data.pack('C*') : data.to_s.b
    w.write_varint(data_bytes.bytesize)
    w.write_bytes(data_bytes)
  elsif args[:hash_to_directly_verify]
    w.write_byte(2)
    hash = args[:hash_to_directly_verify]
    hash_bytes = hash.is_a?(Array) ? hash.pack('C*') : hash.to_s.b
    w.write_bytes(hash_bytes)
  else
    w.write_byte(0)
  end
  w.write_optional_bool(args[:seek_permission])
end

.write_verify_signature_result(w, _result) ⇒ Object



1540
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1540

def write_verify_signature_result(w, _result); end

.write_wait_for_authentication_params(w, _args) ⇒ Object


  1. waitForAuthentication (no params)




1903
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1903

def write_wait_for_authentication_params(w, _args); end

.write_wait_for_authentication_result(w, _result) ⇒ Object



1909
# File 'lib/bsv/wallet_interface/wire/serializer.rb', line 1909

def write_wait_for_authentication_result(w, _result); end