Class: OpenAssets::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/openassets/payload.rb

Overview

the open assets payload

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quantities = [], metadata = '') ⇒ Payload

Returns a new instance of Payload.



14
15
16
17
# File 'lib/openassets/payload.rb', line 14

def initialize(quantities = [],  = '')
  @quantities = quantities
  @metadata = 
end

Instance Attribute Details

#metadataObject

Returns the value of attribute metadata.



12
13
14
# File 'lib/openassets/payload.rb', line 12

def 
  @metadata
end

#quantitiesObject

Returns the value of attribute quantities.



11
12
13
# File 'lib/openassets/payload.rb', line 11

def quantities
  @quantities
end

Class Method Details

.parse_from_payload(payload) ⇒ Payload

parse open assets payload

Returns:

  • (Payload)

    a open assets payload object, if payload is invalid, return nil.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/openassets/payload.rb', line 21

def self.parse_from_payload(payload)
  buf = StringIO.new(payload)
  marker = buf.read(2)
  version = buf.read(2)
  return nil if marker != MARKER || version != VERSION
  count = Bitcoin.unpack_var_int_from_io(buf)
  return nil unless count
  quantities = []
  count.times do
    quantities << LEB128.decode_unsigned(buf, buf.pos)
  end
   = Bitcoin.unpack_var_int_from_io(buf)
  return nil if .nil? || buf.length <  + buf.pos
   = buf.read().each_byte.map(&:chr).join
  new(quantities, )
rescue
  # LEB128#decode_unsigned raise 'undefined method `unpack' for nil:NilClass'
  # for invalid format such as "018f8f" (the most significant bit of the last byte should be 0)
  nil
end

Instance Method Details

#to_payloadObject

generate binary payload



43
44
45
46
47
48
49
50
# File 'lib/openassets/payload.rb', line 43

def to_payload
  payload = String.new
  payload << MARKER
  payload << VERSION
  payload << Bitcoin.pack_var_int(quantities.size) << quantities.map{|q| LEB128.encode_unsigned(q).read }.join
  payload << Bitcoin.pack_var_int(.length) << .bytes.map{|b| sprintf("%02x", b)}.join.htb
  payload
end