Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
mhw0 committed Sep 13, 2023
2 parents 77526b3 + b5a7785 commit 3718a6f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/abi.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,9 @@ int eth_abi_mpint(struct eth_abi *abi, mpz_t mpz) {
mpz_init(mpztmp);
mpz_and(mpztmp, mpz, mpzmask);

size = mpz_sizeinbase(mpztmp, 16) / 2;
mpz_export(&(bytes[32]) - size, NULL, 1, sizeof(uint8_t), 0, 0, mpz);
size = mpz_sizeinbase(mpztmp, 16);
size = (size % 2 == 0 ? size : size + 1) / 2;
mpz_export((bytes + 32) - size, NULL, 1, sizeof(uint8_t), 0, 0, mpz);
mpz_clears(mpztmp, mpzmask, NULL);

return eth_abi_bytes32(abi, bytes);
Expand Down
9 changes: 6 additions & 3 deletions test/test-eth-abi.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,22 @@ void test_eth_abi_mpint(void) {
struct eth_abi abi0, abi1;
size_t hexlen;
char *hex;
mpz_t mpz0, mpz1;
mpz_t mpz0, mpz1, mpz2;

mpz_init_set_str(mpz0, "0xff", 0);
mpz_init_set_str(mpz1, "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffbb", 0);
mpz_init_set_str(mpz1, "0xfff", 0);
mpz_init_set_str(mpz2, "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffbb", 0);

ok(eth_abi_init(&abi0, ETH_ABI_ENCODE) == 1);
ok(eth_abi_mpint(&abi0, mpz0) == 1);
ok(eth_abi_mpint(&abi0, mpz1) == 1);
ok(eth_abi_mpint(&abi0, mpz2) == 1);
ok(eth_abi_to_hex(&abi0, &hex, &hexlen) == 1);

is(hex, "00000000000000000000000000000000000000000000000000000000000000ff"
"0000000000000000000000000000000000000000000000000000000000000fff"
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
mpz_clears(mpz0, mpz1, NULL);
mpz_clears(mpz0, mpz1, mpz2, NULL);
free(hex);

ok(eth_abi_from_hex(&abi1,
Expand Down

0 comments on commit 3718a6f

Please sign in to comment.