|
11 | 11 |
|
12 | 12 | from __future__ import absolute_import, division, print_function, unicode_literals |
13 | 13 |
|
14 | | -import struct |
15 | | -import socket |
16 | 14 | import binascii |
17 | 15 | import hashlib |
| 16 | +import socket |
| 17 | +import struct |
| 18 | +import sys |
18 | 19 |
|
19 | 20 | from .script import CScript |
20 | 21 |
|
|
35 | 36 | def MoneyRange(nValue): |
36 | 37 | return 0 <= nValue <= MAX_MONEY |
37 | 38 |
|
| 39 | +def py2_x(h): |
| 40 | + """Convert a hex string to bytes""" |
| 41 | + return binascii.unhexlify(h) |
| 42 | + |
38 | 43 | def x(h): |
39 | 44 | """Convert a hex string to bytes""" |
40 | | - import sys |
41 | | - if sys.version > '3': |
42 | | - return binascii.unhexlify(h.encode('utf8')) |
43 | | - else: |
44 | | - return binascii.unhexlify(h) |
| 45 | + return binascii.unhexlify(h.encode('utf8')) |
| 46 | + |
| 47 | +def py2_b2x(b): |
| 48 | + """Convert bytes to a hex string""" |
| 49 | + return binascii.hexlify(b) |
45 | 50 |
|
46 | 51 | def b2x(b): |
47 | 52 | """Convert bytes to a hex string""" |
48 | | - if sys.version > '3': |
49 | | - return binascii.hexlify(b).decode('utf8') |
50 | | - else: |
51 | | - return binascii.hexlify(b) |
| 53 | + return binascii.hexlify(b).decode('utf8') |
| 54 | + |
| 55 | +def py2_lx(h): |
| 56 | + """Convert a little-endian hex string to bytes |
| 57 | +
|
| 58 | + Lets you write uint256's and uint160's the way the Satoshi codebase shows |
| 59 | + them. |
| 60 | + """ |
| 61 | + return binascii.unhexlify(h)[::-1] |
52 | 62 |
|
53 | 63 | def lx(h): |
54 | 64 | """Convert a little-endian hex string to bytes |
55 | 65 |
|
56 | 66 | Lets you write uint256's and uint160's the way the Satoshi codebase shows |
57 | 67 | them. |
58 | 68 | """ |
59 | | - import sys |
60 | | - if sys.version > '3': |
61 | | - return binascii.unhexlify(h.encode('utf8'))[::-1] |
62 | | - else: |
63 | | - return binascii.unhexlify(h)[::-1] |
| 69 | + return binascii.unhexlify(h.encode('utf8'))[::-1] |
| 70 | + |
| 71 | +def py2_b2lx(b): |
| 72 | + """Convert bytes to a little-endian hex string |
| 73 | +
|
| 74 | + Lets you show uint256's and uint160's the way the Satoshi codebase shows |
| 75 | + them. |
| 76 | + """ |
| 77 | + return binascii.hexlify(b[::-1]) |
64 | 78 |
|
65 | 79 | def b2lx(b): |
66 | 80 | """Convert bytes to a little-endian hex string |
67 | 81 |
|
68 | 82 | Lets you show uint256's and uint160's the way the Satoshi codebase shows |
69 | 83 | them. |
70 | 84 | """ |
71 | | - if sys.version > '3': |
72 | | - return binascii.hexlify(b[::-1]).decode('utf8') |
73 | | - else: |
74 | | - return binascii.hexlify(b[::-1]) |
| 85 | + return binascii.hexlify(b[::-1]).decode('utf8') |
| 86 | + |
| 87 | +if not (sys.version > '3'): |
| 88 | + x = py2_x |
| 89 | + b2x = py2_b2x |
| 90 | + lx = py2_lx |
| 91 | + b2lx = py2_b2lx |
| 92 | + |
| 93 | +del py2_x |
| 94 | +del py2_b2x |
| 95 | +del py2_lx |
| 96 | +del py2_b2lx |
| 97 | + |
75 | 98 |
|
76 | 99 | def str_money_value(value): |
77 | 100 | """Convert an integer money value to a fixed point string""" |
|
0 commit comments