Skip to content

Commit eb15ee3

Browse files
committed
cleaned up encode_varint for segwit script op_push
1 parent f58126b commit eb15ee3

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ python-bitcoin-utils
33

44
This is a bitcoin library that provides tools/utilities to interact with the Bitcoin network. One of the primary goals of the library is to explain the low-level details of Bitcoin. The code is easy to read and properly documented explaining in detail all the thorny aspects of the implementation. It is a low-level library which assumes some high-level understanding of how Bitcoin works. In the future this might change.
55

6-
This is an early version of the library (v0.5.1) and currently, it supports private/public keys, all type of addresses and creation of any transaction (incl. segwit) with all SIGHASH types. All script op codes are included. Timelock and non-standanrd transactions are supported. Currently, a simple node proxy exists to enable easy calls to a Bitcoin core node. Extra functionality will be added continuously and the documentation will be improved as the work progresses.
6+
This is an early version of the library (v0.5.2) and currently, it supports private/public keys, all type of addresses and creation of any transaction (incl. segwit) with all SIGHASH types. All script op codes are included. Timelock and non-standanrd transactions are supported. Currently, a simple node proxy exists to enable easy calls to a Bitcoin core node. Extra functionality will be added continuously and the documentation will be improved as the work progresses.
77

88
The API documentation can be build with Sphinx but is also available as a PDF for convenience. One can currently use the library for experimenting and learning the inner workings of Bitcoin. It is not meant for production yet and parts of the API might be updated with new versions.
99

bitcoinutils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = '0.5.1'
1+
__version__ = '0.5.2'
22

bitcoinutils/script.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import struct
1313
import copy
1414
import hashlib
15-
from bitcoinutils.utils import prepend_compact_size, to_bytes, vi_to_int
15+
from bitcoinutils.utils import prepend_varint, to_bytes, vi_to_int
1616
from binascii import unhexlify, hexlify
1717

1818
import bitcoinutils.keys
@@ -314,16 +314,14 @@ def _op_push_data(self, data):
314314

315315
def _segwit_op_push_data(self, data):
316316
# expects data in hexadecimal characters and converts to bytes with
317-
# compact_size (or var_int) length prefix.
317+
# varint (or compact size) length prefix.
318318
#
319319
# TODO maybe, for convenience, also accept objects for public keys,
320320
# addresses, etc. and use isinstance and convert manually
321321
data_bytes = unhexlify(data)
322322

323-
# prepend compact size lenth to data bytes
324-
compact_size_data = prepend_compact_size(data_bytes)
325-
326-
return compact_size_data
323+
# return prepended varint (compact size) length to data bytes
324+
return prepend_varint(data_bytes)
327325

328326

329327

bitcoinutils/utils.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,31 @@ def to_satoshis(num):
2424

2525

2626
'''
27-
Counts bytes and returns them with their compact size (or varint) prepended.
28-
Accepts bytes and returns bytes. The length should be specified in
29-
little-endian (which is why we reverse the array bytes).
27+
Counts bytes and returns them with their varint (or compact size) prepended.
28+
Accepts bytes and returns bytes.
29+
'''
30+
def prepend_varint(data):
31+
varint_bytes = encode_varint( len(data) )
32+
return varint_bytes + data
33+
34+
35+
'''
36+
Encode a potentially very large integer into varint bytes. The length should be
37+
specified in little-endian.
3038
3139
https://bitcoin.org/en/developer-reference#compactsize-unsigned-integers
3240
'''
33-
def prepend_compact_size(data):
34-
prefix = b''
35-
size = len(data)
36-
if size >= 0 and size <= 252:
37-
prefix = unhexlify(format(size, '02x').encode())
38-
elif size >= 253 and size <= 0xffff:
39-
prefix = b'\xfd' + unhexlify(format(size, '04x'))[::-1]
40-
elif size >= 0x10000 and size <= 0xffffffff:
41-
prefix = b'\xfe' + unhexlify(format(size, '08x'))[::-1]
42-
elif size >= 0x100000000 and size <= 0xffffffffffffffff:
43-
prefix = b'\xff' + unhexlify(format(size, '016x'))[::-1]
41+
def encode_varint(i):
42+
if i < 253:
43+
return bytes([i])
44+
elif i < 0x10000:
45+
return b'\xfd' + i.to_bytes(2, 'little')
46+
elif i < 0x100000000:
47+
return b'\xfe' + i.to_bytes(4, 'little')
48+
elif i < 0x10000000000000000:
49+
return b'\xff' + i.to_bytes(8, 'little')
4450
else:
45-
raise ValueError("Data size not between 0 and 0xffffffffffffffff")
46-
47-
return prefix + data
51+
raise ValueError("Integer is too large: %d" % i)
4852

4953

5054
'''

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@
5858
# built documents.
5959
#
6060
# The short X.Y version.
61-
version = u'0.5.1'
61+
version = u'0.5.2'
6262
# The full version, including alpha/beta/rc tags.
63-
release = u'0.5.1'
63+
release = u'0.5.2'
6464

6565
# The language for content autogenerated by Sphinx. Refer to documentation
6666
# for a list of supported languages.

0 commit comments

Comments
 (0)