From 2964f2e8558f97d0f262fc23eac5ac1fe3e8459d Mon Sep 17 00:00:00 2001 From: Andrew Kozlik Date: Mon, 22 Mar 2021 21:15:23 +0100 Subject: [PATCH] refactor(core/cardano): Clean up variable_length_encode(). --- core/src/apps/cardano/helpers/utils.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/core/src/apps/cardano/helpers/utils.py b/core/src/apps/cardano/helpers/utils.py index 6246581fe2..e0af3ada0b 100644 --- a/core/src/apps/cardano/helpers/utils.py +++ b/core/src/apps/cardano/helpers/utils.py @@ -18,17 +18,12 @@ def variable_length_encode(number: int) -> bytes: if number < 0: raise ValueError("Negative numbers not supported. Number supplied: %s" % number) - encoded = [] - - bit_length = len(bin(number)[2:]) - encoded.append(number & 127) - - while bit_length > 7: + encoded = [number & 0x7F] + while number > 0x7F: number >>= 7 - bit_length -= 7 - encoded.insert(0, (number & 127) + 128) + encoded.append((number & 0x7F) + 0x80) - return bytes(encoded) + return bytes(reversed(encoded)) def to_account_path(path: list[int]) -> list[int]: