diff --git a/pbcabi/binaryreader.py b/pbcabi/binaryreader.py index 2c2153c..bbe81e8 100644 --- a/pbcabi/binaryreader.py +++ b/pbcabi/binaryreader.py @@ -26,6 +26,12 @@ class BinaryReader: self.size = len(buf) self.position = 0 + def __repr__(self) -> str: + return 'BinaryReader[{} / {}]'.format(self.position, self.size) + + def __str__(self) -> str: + return repr(self) + def readBytes(self, num_bytes: int) -> bytes: ''' Reads a number of bytes from stream. @@ -86,9 +92,9 @@ class BinaryReader: ''' # TODO: Test! result = self.readUIntBigEndian(num_bytes) - half = 2**(num_bytes * 8) - if result > half: - result -= half + full = 2**(num_bytes * 8) + if result >= full//2: + result -= full return result def readUIntLittleEndian(self, num_bytes: int) -> int: @@ -107,9 +113,9 @@ class BinaryReader: ''' # TODO: Test! result = self.readUIntLittleEndian(num_bytes) - half = 2**(num_bytes * 8) - if result > half: - result -= half + full = 2**(num_bytes * 8) + if result >= full//2: + result -= full return result def readLeb128(self) -> int: diff --git a/pbcabi/data.py b/pbcabi/data.py index 6b0cd17..67ee7e5 100644 --- a/pbcabi/data.py +++ b/pbcabi/data.py @@ -95,9 +95,7 @@ class BlockchainAddress(ByteData): return repr(self) def shard_id(self, num_shards: int) -> str: - reader = BinaryReader(self.data) - reader.readBytes(17) - shard = reader.readUInt32BigEndian() + shard = BinaryReader(self.data[17:]).readSignedIntBigEndian(4) shard = abs(shard) % num_shards return f'Shard{shard}'