eips icon indicating copy to clipboard operation
eips copied to clipboard

[EIP-0004] Give reference of algorithm `intToVlq`

Open scalahub opened this issue 4 years ago • 2 comments

In EIP-0004, the encoding in R4 is given as '\x0e' + intToVlq(byteArray.length) + byteArray Would be good to have a link or a code snippet explaining how to implement intToVlq

scalahub avatar Jun 05 '20 17:06 scalahub

FYI, it is Base 128 Varints encoding - https://developers.google.com/protocol-buffers/docs/encoding, not VLQ

gagarin55 avatar Jul 03 '20 09:07 gagarin55

From testing and minting NFT's that match anon_real's implementation on auctionhouse, I think it is VLQ. Sample Java below;

public static byte[] vlqEncode(long n) {

  int numRelevantBits = 64 - Long.numberOfLeadingZeros(n);
  int numBytes = (numRelevantBits + 6) / 7;
  if (numBytes == 0)
    numBytes = 1;
  byte[] output = new byte[numBytes];
  for (int i = numBytes - 1; i >= 0; i--)
  {
    int curByte = (int)(n & 0x7F);
    if (i != (numBytes - 1))
      curByte |= 0x80;
    output[i] = (byte)curByte;
    n >>>= 7;
  }
  return output;
}

N.B. If encoding a hex string, halve the value of it's string length that you feed into this.

HazeyOneKenobi avatar May 28 '22 13:05 HazeyOneKenobi