Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Update dependencies & fix encode signed numbers #72

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion multiversx_sdk_core/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ def encode_unsigned_number(arg: int) -> bytes:


def encode_signed_number(arg: int) -> bytes:
return arg.to_bytes(INTEGER_MAX_NUM_BYTES, byteorder="big", signed=True).lstrip(bytes([255]))
if arg == 0:
return b''
length = ((arg + (arg < 0)).bit_length() + 7 + 1) // 8
return arg.to_bytes(length, byteorder="big", signed=True)


def decode_unsigned_number(arg: bytes) -> int:
Expand Down
75 changes: 75 additions & 0 deletions multiversx_sdk_core/codec_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from multiversx_sdk_core.codec import encode_signed_number

test_vectors_1 = [
[-1, 0xFF],
[1, 0x01],
[2, 0x02],
[-2, 0xFE],
[3, 0x03],
[-3, 0xFD],
[4, 0x04],
[-4, 0xFC],
]

test_vectors_2 = [
[-1, 0xFF],
[1, 0x01],
[2, 0x02],
[-2, 0xFE],
[3, 0x03],
[-3, 0xFD],
[4, 0x04],
[-4, 0xFC],
[5, 0x05],
[-5, 0xFB],
[6, 0x06],
[-6, 0xFA],
]

test_vectors_3 = [
[125, 0x7D],
[-125, 0x83],
[126, 0x7E],
[-126, 0x82],
[127, 0x7F],
[-127, 0x81],
[-128, 0x80],
]

test_vectors_4 = [
[128, [0x00, 0x80]],
[129, [0x00, 0x81]],
[-129, [0xFF, 0x7F]],
[130, [0x00, 0x82]],
[-130, [0xFF, 0x7E]],
[253, [0x00, 0xFD]],
[256, [0x01, 0x00]],
[-256, [0xFF, 0x00]],
[-257, [0xFE, 0xFF]],
[258, [0x01, 0x02]],
]


def test_encode_signed_number():
assert encode_signed_number(-256) == bytes([0xFF, 0x00])
assert encode_signed_number(-0x11) == bytes([0xEF])
assert encode_signed_number(-128) == bytes([0x80])
assert encode_signed_number(-1) == bytes([0xFF])
assert encode_signed_number(0) == bytes([])
assert encode_signed_number(1) == bytes([0x01])
assert encode_signed_number(256) == bytes([0x01, 0x00])
assert encode_signed_number(127) == bytes([0x7F])
assert encode_signed_number(0x11) == bytes([0x11])
assert encode_signed_number(255) == bytes([0x00, 0xFF])

for input_data, expected_data in test_vectors_1:
assert encode_signed_number(input_data) == bytes([expected_data])

for input_data, expected_data in test_vectors_2:
assert encode_signed_number(input_data) == bytes([expected_data])

for input_data, expected_data in test_vectors_3:
assert encode_signed_number(input_data) == bytes([expected_data])

for input_data, expected_data in test_vectors_4:
assert encode_signed_number(input_data) == bytes(expected_data) # type: ignore
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "multiversx-sdk-core"
version = "0.8.0"
version = "0.8.1"
authors = [
{ name="MultiversX" },
]
Expand All @@ -18,8 +18,8 @@ classifiers = [
"Operating System :: OS Independent",
]
dependencies = [
"pycryptodomex==3.16.0",
"protobuf==3.20.1"
"pycryptodomex==3.19.1",
"protobuf==3.20.2"
]

[project.urls]
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pycryptodomex==3.16.0
protobuf==3.20.1
pycryptodomex==3.19.1
protobuf==3.20.2
Loading