Module: BSV::Wallet::Serializer::ListOutputs

Defined in:
lib/bsv/wallet/serializer/list_outputs.rb

Overview

BRC-103 wire codec for the list_outputs call (call byte 6).

Args wire layout:

[varint-str: basket]
[string-slice: tags]  nil → NegativeOne
[1 byte: tag_query_mode]  1=all, 2=any, 0xFF=nil
[1 byte: include]  1=locking_scripts, 2=entire_transactions, 0xFF=nil
[optional_bool: include_custom_instructions]
[optional_bool: include_tags]
[optional_bool: include_labels]
[optional_uint32: limit]
[optional_uint32: offset]
[optional_bool: seek_permission]

Result wire layout:

[varint: total_outputs]
[varint: beef_len, or NegativeOne if nil][beef bytes]
per output:
  [32-byte wire txid][varint vout]
  [varint: satoshis]
  [varint: locking_script_len, or NegativeOne][script bytes]
  [varint-str: custom_instructions]  0-length string if nil
  [string-slice: tags]
  [string-slice: labels]

Constant Summary collapse

TAG_QUERY_MODE_ALL =
1
TAG_QUERY_MODE_ANY =
2
INCLUDE_LOCKING_SCRIPTS =
1
INCLUDE_ENTIRE_TRANSACTIONS =
2
NEGATIVE_ONE_BYTE =
0xFF

Class Method Summary collapse

Class Method Details

.deserialize_args(bytes) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bsv/wallet/serializer/list_outputs.rb', line 67

def deserialize_args(bytes)
  r = Wire::Reader.new(bytes)
  basket = r.read_str_with_varint_len
  tags   = r.read_string_slice

  mode = case r.read_byte
         when TAG_QUERY_MODE_ALL then :all
         when TAG_QUERY_MODE_ANY then :any
         end

  inc = case r.read_byte
        when INCLUDE_LOCKING_SCRIPTS     then :locking_scripts
        when INCLUDE_ENTIRE_TRANSACTIONS then :entire_transactions
        end

  {
    basket: basket,
    tags: tags,
    tag_query_mode: mode,
    include: inc,
    include_custom_instructions: r.read_optional_bool,
    include_tags: r.read_optional_bool,
    include_labels: r.read_optional_bool,
    limit: r.read_optional_uint32,
    offset: r.read_optional_uint32,
    seek_permission: r.read_optional_bool
  }
end

.deserialize_result(bytes) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/bsv/wallet/serializer/list_outputs.rb', line 126

def deserialize_result(bytes)
  r = Wire::Reader.new(bytes)
  total_outputs = r.read_varint

  beef_len = r.read_varint
  beef = if beef_len == 0xFFFF_FFFF_FFFF_FFFF
           nil
         else
           r.read_bytes(beef_len)
         end

  outputs = total_outputs.times.map do
    outpoint_data = r.read_outpoint
    outpoint = "#{outpoint_data[:txid_hex]}.#{outpoint_data[:vout]}"
    satoshis = r.read_varint
    locking_script_len = r.read_varint
    locking_script = if locking_script_len == 0xFFFF_FFFF_FFFF_FFFF
                       nil
                     else
                       r.read_bytes(locking_script_len)
                     end
    custom_instructions = r.read_optional_string
    tags   = r.read_string_slice
    labels = r.read_string_slice

    {
      outpoint: outpoint,
      satoshis: satoshis,
      locking_script: locking_script,
      custom_instructions: custom_instructions,
      tags: tags,
      labels: labels,
      spendable: true
    }
  end

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

.serialize_args(args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bsv/wallet/serializer/list_outputs.rb', line 39

def serialize_args(args)
  w = Wire::Writer.new
  w.write_str_with_varint_len(args.fetch(:basket, ''))
  w.write_string_slice(args[:tags])

  mode_byte = case args[:tag_query_mode]
              when :all then TAG_QUERY_MODE_ALL
              when :any then TAG_QUERY_MODE_ANY
              else           NEGATIVE_ONE_BYTE
              end
  w.write_byte(mode_byte)

  include_byte = case args[:include]
                 when :locking_scripts      then INCLUDE_LOCKING_SCRIPTS
                 when :entire_transactions  then INCLUDE_ENTIRE_TRANSACTIONS
                 else                            NEGATIVE_ONE_BYTE
                 end
  w.write_byte(include_byte)

  w.write_optional_bool(args[:include_custom_instructions])
  w.write_optional_bool(args[:include_tags])
  w.write_optional_bool(args[:include_labels])
  w.write_optional_uint32(args[:limit])
  w.write_optional_uint32(args[:offset])
  w.write_optional_bool(args[:seek_permission])
  w.buf
end

.serialize_result(result) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/bsv/wallet/serializer/list_outputs.rb', line 96

def serialize_result(result)
  w = Wire::Writer.new
  outputs = result[:outputs] || []
  w.write_varint(outputs.length)

  beef = result[:beef]
  if beef
    w.write_int_bytes(beef.b)
  else
    w.write_negative_one
  end

  outputs.each do |output|
    outpoint = output[:outpoint] || ''
    txid_hex, vout = outpoint.to_s.split('.', 2)
    w.write_outpoint(txid_hex.to_s, vout.to_i)
    w.write_varint(output[:satoshis].to_i)
    locking_script = output[:locking_script]
    if locking_script && !locking_script.empty?
      w.write_int_bytes(locking_script.b)
    else
      w.write_negative_one
    end
    w.write_optional_string(output[:custom_instructions])
    w.write_string_slice(output[:tags])
    w.write_string_slice(output[:labels])
  end
  w.buf
end