Skip to content

Commit 021daed

Browse files
committed
refactor: replace remaining binascii method calls
1 parent f4328eb commit 021daed

File tree

7 files changed

+14
-19
lines changed

7 files changed

+14
-19
lines changed

contrib/linearize/linearize-data.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import time
1818
import glob
1919
from collections import namedtuple
20-
from binascii import unhexlify
2120

2221
settings = {}
2322

@@ -332,7 +331,7 @@ def run(self):
332331
settings['max_out_sz'] = int(settings['max_out_sz'])
333332
settings['split_timestamp'] = int(settings['split_timestamp'])
334333
settings['file_timestamp'] = int(settings['file_timestamp'])
335-
settings['netmagic'] = unhexlify(settings['netmagic'].encode('utf-8'))
334+
settings['netmagic'] = bytes.fromhex(settings['netmagic'])
336335
settings['out_of_order_cache_sz'] = int(settings['out_of_order_cache_sz'])
337336
settings['debug_output'] = settings['debug_output'].lower()
338337

contrib/signet/miner

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import sys
1515
import time
1616
import subprocess
1717

18-
from binascii import unhexlify
1918
from io import BytesIO
2019

2120
PATH_BASE_CONTRIB_SIGNET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
@@ -202,7 +201,7 @@ def finish_block(block, signet_solution, grind_cmd):
202201

203202
def generate_psbt(tmpl, reward_spk, *, blocktime=None):
204203
signet_spk = tmpl["signet_challenge"]
205-
signet_spk_bin = unhexlify(signet_spk)
204+
signet_spk_bin = bytes.fromhex(signet_spk)
206205

207206
cbtx = create_coinbase(height=tmpl["height"], value=tmpl["coinbasevalue"], spk=reward_spk)
208207
cbtx.vin[0].nSequence = 2**32-2
@@ -258,7 +257,7 @@ def get_reward_addr_spk(args, height):
258257
return args.address, args.reward_spk
259258

260259
reward_addr = get_reward_address(args, height)
261-
reward_spk = unhexlify(json.loads(args.bcli("getaddressinfo", reward_addr))["scriptPubKey"])
260+
reward_spk = bytes.fromhex(json.loads(args.bcli("getaddressinfo", reward_addr))["scriptPubKey"])
262261
if args.address is not None:
263262
# will always be the same, so cache
264263
args.reward_spk = reward_spk

contrib/zmq/zmq_sub.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
https://github.com/bitcoin/bitcoin/blob/37a7fe9e440b83e2364d5498931253937abe9294/contrib/zmq/zmq_sub.py
2424
"""
2525

26-
import binascii
2726
import asyncio
2827
import zmq
2928
import zmq.asyncio
@@ -58,18 +57,18 @@ async def handle(self) :
5857
sequence = str(struct.unpack('<I', seq)[-1])
5958
if topic == b"hashblock":
6059
print('- HASH BLOCK ('+sequence+') -')
61-
print(binascii.hexlify(body))
60+
print(body.hex())
6261
elif topic == b"hashtx":
6362
print('- HASH TX ('+sequence+') -')
64-
print(binascii.hexlify(body))
63+
print(body.hex())
6564
elif topic == b"rawblock":
6665
print('- RAW BLOCK HEADER ('+sequence+') -')
67-
print(binascii.hexlify(body[:80]))
66+
print(body[:80].hex())
6867
elif topic == b"rawtx":
6968
print('- RAW TX ('+sequence+') -')
70-
print(binascii.hexlify(body))
69+
print(body.hex())
7170
elif topic == b"sequence":
72-
hash = binascii.hexlify(body[:32])
71+
hash = body[:32].hex()
7372
label = chr(body[32])
7473
mempool_sequence = None if len(body) != 32+1+8 else struct.unpack("<Q", body[32+1:])[0]
7574
print('- SEQUENCE ('+sequence+') -')

share/rpcauth/rpcauth.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55

66
from argparse import ArgumentParser
77
from base64 import urlsafe_b64encode
8-
from binascii import hexlify
98
from getpass import getpass
109
from os import urandom
1110

1211
import hmac
1312

1413
def generate_salt(size):
1514
"""Create size byte hex salt"""
16-
return hexlify(urandom(size)).decode()
15+
return urandom(size).hex()
1716

1817
def generate_password():
1918
"""Create 32 byte b64 password"""

src/test/serfloat_tests.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ BOOST_AUTO_TEST_CASE(double_serfloat_tests) {
102102
Python code to generate the below hashes:
103103
104104
def reversed_hex(x):
105-
return binascii.hexlify(''.join(reversed(x)))
105+
return bytes(reversed(x)).hex()
106+
106107
def dsha256(x):
107108
return hashlib.sha256(hashlib.sha256(x).digest()).digest()
108109
109-
reversed_hex(dsha256(''.join(struct.pack('<d', x) for x in range(0,1000)))) == '43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96'
110+
reversed_hex(dsha256(b''.join(struct.pack('<d', x) for x in range(0,1000)))) == '43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96'
110111
*/
111112
BOOST_AUTO_TEST_CASE(doubles)
112113
{

test/functional/test_framework/messages.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
by tests, compromising their intended effect.
2020
"""
2121
from base64 import b32decode, b32encode
22-
from codecs import encode
2322
import copy
2423
import hashlib
2524
from io import BytesIO
@@ -681,7 +680,7 @@ def calc_sha256(self):
681680
r += struct.pack("<I", self.nBits)
682681
r += struct.pack("<I", self.nNonce)
683682
self.sha256 = uint256_from_str(hash256(r))
684-
self.hash = encode(hash256(r)[::-1], 'hex_codec').decode('ascii')
683+
self.hash = hash256(r)[::-1].hex()
685684

686685
def rehash(self):
687686
self.sha256 = None

test/util/bitcoin-util-test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
Can also be run manually."""
1111

1212
import argparse
13-
import binascii
1413
import configparser
1514
import difflib
1615
import json
@@ -167,7 +166,7 @@ def parse_output(a, fmt):
167166
if fmt == 'json': # json: compare parsed data
168167
return json.loads(a)
169168
elif fmt == 'hex': # hex: parse and compare binary data
170-
return binascii.a2b_hex(a.strip())
169+
return bytes.fromhex(a.strip())
171170
else:
172171
raise NotImplementedError("Don't know how to compare %s" % fmt)
173172

0 commit comments

Comments
 (0)