mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-23 17:47:31 +00:00
bitcoin.py: make int_to_hex throw on overflow
This commit is contained in:
parent
79f4a8bae9
commit
1a8e8bc047
2 changed files with 29 additions and 3 deletions
|
@ -50,12 +50,18 @@ def rev_hex(s):
|
||||||
return bh2u(bfh(s)[::-1])
|
return bh2u(bfh(s)[::-1])
|
||||||
|
|
||||||
|
|
||||||
def int_to_hex(i, length=1):
|
def int_to_hex(i: int, length: int=1) -> str:
|
||||||
|
"""Converts int to little-endian hex string.
|
||||||
|
`length` is the number of bytes available
|
||||||
|
"""
|
||||||
if not isinstance(i, int):
|
if not isinstance(i, int):
|
||||||
raise TypeError('{} instead of int'.format(i))
|
raise TypeError('{} instead of int'.format(i))
|
||||||
|
range_size = pow(256, length)
|
||||||
|
if i < -range_size/2 or i >= range_size:
|
||||||
|
raise OverflowError('cannot convert int {} to hex ({} bytes)'.format(i, length))
|
||||||
if i < 0:
|
if i < 0:
|
||||||
# two's complement
|
# two's complement
|
||||||
i = pow(256, length) + i
|
i = range_size + i
|
||||||
s = hex(i)[2:].rstrip('L')
|
s = hex(i)[2:].rstrip('L')
|
||||||
s = "0"*(2*length - len(s)) + s
|
s = "0"*(2*length - len(s)) + s
|
||||||
return rev_hex(s)
|
return rev_hex(s)
|
||||||
|
|
|
@ -12,7 +12,7 @@ from lib.bitcoin import (
|
||||||
deserialize_privkey, serialize_privkey, is_segwit_address,
|
deserialize_privkey, serialize_privkey, is_segwit_address,
|
||||||
is_b58_address, address_to_scripthash, is_minikey, is_compressed, is_xpub,
|
is_b58_address, address_to_scripthash, is_minikey, is_compressed, is_xpub,
|
||||||
xpub_type, is_xprv, is_bip32_derivation, seed_type, EncodeBase58Check,
|
xpub_type, is_xprv, is_bip32_derivation, seed_type, EncodeBase58Check,
|
||||||
script_num_to_hex, push_script, add_number_to_script)
|
script_num_to_hex, push_script, add_number_to_script, int_to_hex)
|
||||||
from lib import ecc, crypto, ecc_fast
|
from lib import ecc, crypto, ecc_fast
|
||||||
from lib.ecc import number_to_string, string_to_number
|
from lib.ecc import number_to_string, string_to_number
|
||||||
from lib.transaction import opcodes
|
from lib.transaction import opcodes
|
||||||
|
@ -227,6 +227,26 @@ class Test_bitcoin(SequentialTestCase):
|
||||||
result = Hash(payload)
|
result = Hash(payload)
|
||||||
self.assertEqual(expected, result)
|
self.assertEqual(expected, result)
|
||||||
|
|
||||||
|
def test_int_to_hex(self):
|
||||||
|
self.assertEqual('00', int_to_hex(0, 1))
|
||||||
|
self.assertEqual('ff', int_to_hex(-1, 1))
|
||||||
|
self.assertEqual('00000000', int_to_hex(0, 4))
|
||||||
|
self.assertEqual('01000000', int_to_hex(1, 4))
|
||||||
|
self.assertEqual('7f', int_to_hex(127, 1))
|
||||||
|
self.assertEqual('7f00', int_to_hex(127, 2))
|
||||||
|
self.assertEqual('80', int_to_hex(128, 1))
|
||||||
|
self.assertEqual('80', int_to_hex(-128, 1))
|
||||||
|
self.assertEqual('8000', int_to_hex(128, 2))
|
||||||
|
self.assertEqual('ff', int_to_hex(255, 1))
|
||||||
|
self.assertEqual('ff7f', int_to_hex(32767, 2))
|
||||||
|
self.assertEqual('0080', int_to_hex(-32768, 2))
|
||||||
|
self.assertEqual('ffff', int_to_hex(65535, 2))
|
||||||
|
with self.assertRaises(OverflowError): int_to_hex(256, 1)
|
||||||
|
with self.assertRaises(OverflowError): int_to_hex(-129, 1)
|
||||||
|
with self.assertRaises(OverflowError): int_to_hex(-257, 1)
|
||||||
|
with self.assertRaises(OverflowError): int_to_hex(65536, 2)
|
||||||
|
with self.assertRaises(OverflowError): int_to_hex(-32769, 2)
|
||||||
|
|
||||||
def test_var_int(self):
|
def test_var_int(self):
|
||||||
for i in range(0xfd):
|
for i in range(0xfd):
|
||||||
self.assertEqual(var_int(i), "{:02x}".format(i) )
|
self.assertEqual(var_int(i), "{:02x}".format(i) )
|
||||||
|
|
Loading…
Add table
Reference in a new issue