October 21, 2017, 09:44:23 am

Author Topic: Parsing Metadata with variable length fields  (Read 1539 times)


  • Newbie
  • *
  • Posts: 7
    • View Profile
Parsing Metadata with variable length fields
« on: May 28, 2014, 07:21:00 am »
I'm referring closely to https://github.com/OpenAssets/open-assets-protocol/blob/master/specification.mediawiki#Marker_output

The asset quantities are variable length leb128 encoded. OK.

The metadata is also variable length.

When I parse metadata in OP_RETURNs, how do I know where the asset quantity field ended and the metdata began?  I'm having trouble with this.



  • Admin
  • Administrator
  • Jr. Member
  • *****
  • Posts: 62
    • View Profile
    • Coinprism
Re: Parsing Metadata with variable length fields
« Reply #1 on: May 28, 2014, 05:39:26 pm »
Essentially, you first need to read the asset quantity count, let's say 3. Then you read 3 LEB128 encoded integers. LEB128 is variable size, but you can know how many bytes to read for each LEB128 by looking at the first bit of each byte.

See http://en.wikipedia.org/wiki/LEB128: In a LEB128 integer, 0 as the highest bit indicates this is the last byte of the integer.

For example, this is 624485 (0xE5 0x8E 0x26, from the example in Wikipedia):

11100101  10001110  00100110

The 0 indicates this is the last byte, so if you know there are 3 integers in the asset quantity field, the metadata starts when you have seen 3 bytes with 0 as the first byte.

I hope that makes sense and don't hesitate if there is something you don't understand.