Skip to content

Commit

Permalink
code clean
Browse files Browse the repository at this point in the history
  • Loading branch information
icodeface committed Jul 23, 2020
1 parent 9b73005 commit 1d01cfa
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 26 deletions.
12 changes: 5 additions & 7 deletions electrum/bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,17 @@
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import binascii
import hashlib
from typing import List, Tuple, TYPE_CHECKING, Optional, Union, NamedTuple
from eth_abi import encode_abi, decode_abi
from eth_utils import function_abi_to_4byte_selector
import enum
from enum import IntEnum, Enum

from .util import bfh, bh2u, BitcoinException, assert_bytes, to_bytes, inv_dict
from . import version
from .util import bfh, BitcoinException, assert_bytes, to_bytes, inv_dict
from . import segwit_addr
from . import constants
from . import ecc
from .crypto import sha256d, sha256, hash_160, hmac_oneshot
from .crypto import sha256d, sha256, hash_160

if TYPE_CHECKING:
from .network import Network
Expand Down Expand Up @@ -459,7 +457,7 @@ def address_to_script(addr: str, *, net=None) -> str:
script = pubkeyhash_to_p2pkh_script(hash_160_.hex())
elif addrtype == net.ADDRTYPE_P2SH:
script = opcodes.OP_HASH160.hex()
script += push_script(bh2u(hash_160_))
script += push_script(hash_160_.hex())
script += opcodes.OP_EQUAL.hex()
else:
raise BitcoinException(f'unknown address type: {addrtype}')
Expand Down Expand Up @@ -506,7 +504,7 @@ def address_to_scripthash(addr: str) -> str:

def script_to_scripthash(script: str) -> str:
h = sha256(bfh(script))[0:32]
return bh2u(bytes(reversed(h)))
return bytes(reversed(h)).hex()


def public_key_to_p2pk_script(pubkey: str) -> str:
Expand Down Expand Up @@ -612,7 +610,7 @@ def DecodeBase58Check(psz: Union[bytes, str]) -> bytes:
csum_found = vchRet[-4:]
csum_calculated = sha256d(payload)[0:4]
if csum_calculated != csum_found:
raise InvalidChecksum(f'calculated {bh2u(csum_calculated)}, found {bh2u(csum_found)}')
raise InvalidChecksum(f'calculated {csum_calculated.hex()}, found {csum_found.hex()}')
else:
return payload

Expand Down
2 changes: 1 addition & 1 deletion electrum/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from .bitcoin import hash_encode, int_to_hex, rev_hex, var_int
from .crypto import sha256d
from . import constants
from .util import bfh, bh2u, unpack_uint16_from, unpack_int32_from, unpack_uint32_from, unpack_int64_from, unpack_uint64_from
from .util import bfh, unpack_uint16_from, unpack_int32_from, unpack_uint32_from, unpack_int64_from, unpack_uint64_from
from .simple_config import SimpleConfig
from .logging import get_logger, Logger

Expand Down
2 changes: 1 addition & 1 deletion electrum/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class QtumTestnet(AbstractNet):
QIP5_FORK_HEIGHT = 446320
QIP9_FORK_HEIGHT = 446320
OFFLINE_STAKE_HEIGHT = 625000
USE_OPSENDER = False
USE_OPSENDER = True

LN_REALM_BYTE = 0
LN_DNS_SEEDS = []
Expand Down
13 changes: 5 additions & 8 deletions electrum/gui/qt/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -3359,25 +3359,22 @@ def call_add_delegation(self, addr, staker, fee, gas_limit, gas_price, dialog):
"""
:param staker: hash160 str
"""
password = None
if self.wallet.has_keystore_encryption():
password = self.password_dialog(_("Enter your password to proceed"))
if not password:
return
else:
password = None
if not password: return

pod = self.wallet.sign_message(addr, staker, password)
args = [staker.lower(), fee, pod]
self.sendto_smart_contract(DELEGATION_CONTRACT, DELEGATE_ABI[1], args,
gas_limit, gas_price, 0, addr, dialog, False, password, tx_desc="update delegation")

def call_remove_delegation(self, addr, gas_limit, gas_price, dialog):
password = None
if self.wallet.has_keystore_encryption():
password = self.password_dialog(_("Enter your password to proceed"))
if not password:
return
else:
password = None
if not password: return

self.sendto_smart_contract(DELEGATION_CONTRACT, DELEGATE_ABI[0], [],
gas_limit, gas_price, 0, addr, dialog, False, password, tx_desc="remove delegation")

Expand Down
4 changes: 2 additions & 2 deletions electrum/mnemonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from typing import Sequence, Dict
from types import MappingProxyType

from .util import resource_path, bfh, bh2u, randrange
from .util import resource_path, bfh, randrange
from .crypto import hmac_oneshot
from . import version
from .logging import Logger
Expand Down Expand Up @@ -217,7 +217,7 @@ def make_seed(self, seed_type=None, *, num_bits=132) -> str:

def is_new_seed(x: str, prefix=version.SEED_PREFIX) -> bool:
x = normalize_text(x)
s = bh2u(hmac_oneshot(b"Seed version", x.encode('utf8'), hashlib.sha512))
s = hmac_oneshot(b"Seed version", x.encode('utf8'), hashlib.sha512).hex()
return s.startswith(prefix)


Expand Down
11 changes: 5 additions & 6 deletions electrum/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@

from . import util
from .util import (log_exceptions, ignore_exceptions,
bfh, SilentTaskGroup, make_aiohttp_session, send_exception_to_crash_reporter,
is_hash256_str, is_non_negative_integer, bh2u, MyEncoder, NetworkRetryManager)
SilentTaskGroup, make_aiohttp_session, send_exception_to_crash_reporter,
MyEncoder, NetworkRetryManager)

from .bitcoin import COIN, b58_address_to_hash160, TOKEN_TRANSFER_TOPIC, DELEGATION_CONTRACT, DELEGATE_ABI, eth_output_decode
from . import constants
from . import blockchain
from . import bitcoin
from .blockchain import Blockchain
from . import dns_hacks
from .transaction import Transaction
from .blockchain import Blockchain
Expand Down Expand Up @@ -1337,18 +1336,18 @@ async def call_contract(self, address, data, sender):

async def request_token_balance(self, bind_addr, contract_addr):
__, hash160 = b58_address_to_hash160(bind_addr)
hash160 = bh2u(hash160)
hash160 = hash160.hex()
datahex = '70a08231{}'.format(hash160.zfill(64))
return await self.interface.session.send_request('blockchain.contract.call', [contract_addr, datahex, '', 'int'])

async def request_token_history(self, bind_addr, contract_addr):
__, hash160 = b58_address_to_hash160(bind_addr)
hash160 = bh2u(hash160)
hash160 = hash160.hex()
return await self.interface.session.send_request('blockchain.contract.event.get_history', [hash160, contract_addr, TOKEN_TRANSFER_TOPIC])

async def request_delegation_info(self, addr):
__, hash160 = b58_address_to_hash160(addr)
hash160 = bh2u(hash160)
hash160 = hash160.hex()
datahex = 'bffe3486{}'.format(hash160.zfill(64))
result = await self.call_contract(DELEGATION_CONTRACT, datahex, '')
return eth_output_decode(DELEGATE_ABI[2], result)
2 changes: 1 addition & 1 deletion electrum/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ def parse_URI(uri: str, on_pr: Callable = None, *, loop=None) -> dict:
raise InvalidBitcoinURI(f"failed to parse 'exp' field: {repr(e)}") from e
if 'sig' in out:
try:
out['sig'] = bh2u(bitcoin.base_decode(out['sig'], base=58))
out['sig'] = bitcoin.base_decode(out['sig'], base=58).hex()
except Exception as e:
raise InvalidBitcoinURI(f"failed to parse 'sig' field: {repr(e)}") from e

Expand Down

0 comments on commit 1d01cfa

Please sign in to comment.