Browse Source

prefer int.from_bytes over int('0x'+hex, 16)

3.3.3.1
SomberNight 6 years ago
parent
commit
d7c5949365
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 2
      electrum/bip32.py
  2. 9
      electrum/blockchain.py

2
electrum/bip32.py

@ -127,7 +127,7 @@ def deserialize_xkey(xkey, prv, *, net=None):
fingerprint = xkey[5:9] fingerprint = xkey[5:9]
child_number = xkey[9:13] child_number = xkey[9:13]
c = xkey[13:13+32] c = xkey[13:13+32]
header = int('0x' + bh2u(xkey[0:4]), 16) header = int.from_bytes(xkey[0:4], byteorder='big')
headers = net.XPRV_HEADERS if prv else net.XPUB_HEADERS headers = net.XPRV_HEADERS if prv else net.XPUB_HEADERS
if header not in headers.values(): if header not in headers.values():
raise InvalidMasterKeyVersionBytes('Invalid extended key format: {}' raise InvalidMasterKeyVersionBytes('Invalid extended key format: {}'

9
electrum/blockchain.py

@ -56,7 +56,7 @@ def deserialize_header(s: bytes, height: int) -> dict:
raise InvalidHeader('Invalid header: {}'.format(s)) raise InvalidHeader('Invalid header: {}'.format(s))
if len(s) != HEADER_SIZE: if len(s) != HEADER_SIZE:
raise InvalidHeader('Invalid header length: {}'.format(len(s))) raise InvalidHeader('Invalid header length: {}'.format(len(s)))
hex_to_int = lambda s: int('0x' + bh2u(s[::-1]), 16) hex_to_int = lambda s: int.from_bytes(s, byteorder='little')
h = {} h = {}
h['version'] = hex_to_int(s[0:4]) h['version'] = hex_to_int(s[0:4])
h['prev_block_hash'] = hash_encode(s[4:36]) h['prev_block_hash'] = hash_encode(s[4:36])
@ -187,8 +187,9 @@ class Blockchain(util.PrintError):
bits = self.target_to_bits(target) bits = self.target_to_bits(target)
if bits != header.get('bits'): if bits != header.get('bits'):
raise Exception("bits mismatch: %s vs %s" % (bits, header.get('bits'))) raise Exception("bits mismatch: %s vs %s" % (bits, header.get('bits')))
if int('0x' + _hash, 16) > target: block_hash_as_num = int.from_bytes(bfh(_hash), byteorder='big')
raise Exception("insufficient proof of work: %s vs target %s" % (int('0x' + _hash, 16), target)) if block_hash_as_num > target:
raise Exception(f"insufficient proof of work: {block_hash_as_num} vs target {target}")
def verify_chunk(self, index: int, data: bytes) -> None: def verify_chunk(self, index: int, data: bytes) -> None:
num = len(data) // HEADER_SIZE num = len(data) // HEADER_SIZE
@ -384,7 +385,7 @@ class Blockchain(util.PrintError):
c = ("%064x" % target)[2:] c = ("%064x" % target)[2:]
while c[:2] == '00' and len(c) > 6: while c[:2] == '00' and len(c) > 6:
c = c[2:] c = c[2:]
bitsN, bitsBase = len(c) // 2, int('0x' + c[:6], 16) bitsN, bitsBase = len(c) // 2, int.from_bytes(bfh(c[:6]), byteorder='big')
if bitsBase >= 0x800000: if bitsBase >= 0x800000:
bitsN += 1 bitsN += 1
bitsBase >>= 8 bitsBase >>= 8

Loading…
Cancel
Save