Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v07b #405

Merged
merged 214 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
214 commits
Select commit Hold shift + click to select a range
a0c5fc6
[DEL] Remove deprecated redeemscript methods
mccwdev Mar 10, 2023
cbc7f0a
Merge branch 'master' into release-v07
mccwdev Sep 19, 2023
cb3c73d
[REF] Use segwit for keys and wallets by default
mccwdev Sep 22, 2023
08ef79f
[REF] Use distinguisable prefixes for bitcoinlib_testnet network
mccwdev Sep 22, 2023
51253b8
[REF] Fix unittests with legacy witness type
mccwdev Sep 22, 2023
90f7817
[REF] Use segwit in test_tools
mccwdev Sep 22, 2023
171b18a
[ADD] Option to use wallet with mixed witness types: segwit, p2sh-seg…
mccwdev Sep 25, 2023
d8e790d
[REF] Update db to use segwit by default and add mixed witness type
mccwdev Sep 25, 2023
9d6e4ae
[ADD] Allow to create mixed type wallet, use segwit as default
mccwdev Sep 25, 2023
dca94d1
[REV] Removed extra witness_type 'mixed'
mccwdev Sep 25, 2023
9e6a1d9
[ADD] witness type to new_key method
mccwdev Oct 1, 2023
b807b1e
[REV] Remove witness_type from create key methods
mccwdev Oct 13, 2023
fb4e04a
[REV] Fix f*@ed up reverted commits
mccwdev Oct 13, 2023
86bb946
[REF] Use segwit as default in HDKey, Key
mccwdev Oct 13, 2023
33c2ed7
[ADD] Method to get purpose, encoding, key_path from witness_type
mccwdev Oct 13, 2023
b55529e
[UPD] Update docstrings
mccwdev Oct 13, 2023
1687372
[REF] Add witness type to other key methods
mccwdev Oct 13, 2023
8e0a377
[REF] Allow witness parameter in bulk key methods
mccwdev Oct 14, 2023
f68947e
[ADD] Create new wallet account with different witness type
mccwdev Oct 15, 2023
61cbe59
[ADD] Get public master keys with different witness types
mccwdev Oct 16, 2023
6126104
[REF] Add unittest for mixed witness_type wallets
mccwdev Oct 16, 2023
bfa9c5d
[FIX] Return correct account key in Wallet.new_account
mccwdev Oct 18, 2023
1035655
[REF] Add witness_type to new_key_change
mccwdev Oct 18, 2023
8d1434e
[ADD] Unittests for mixed witness_type wallets
mccwdev Oct 20, 2023
d2328a6
[REF] Define witnesstype in WalletKey class
mccwdev Oct 30, 2023
5950e5e
[ADD] Mixed witness_type send unittest
mccwdev Oct 30, 2023
abda2a4
[FIX] Typo in unittest
mccwdev Oct 30, 2023
2996676
[REF] Raise warning when trying to use mixed witness type in watch on…
mccwdev Oct 31, 2023
20594e8
[ADD] Multiplication, addition, substraction of private keys
mccwdev Oct 31, 2023
e37fa6f
[ADD] Inverse Key method
mccwdev Oct 31, 2023
28a5e5c
[ADD] Get used witness types in wallet and use in scan method
mccwdev Nov 4, 2023
1566bd2
[FIX] Sending from wallets with multiple witness types
mccwdev Nov 8, 2023
fe14fda
[REF] Assume bitcoin network when using passphrase
mccwdev Nov 9, 2023
7bc7d94
[ADD] Use multiple witnesses in multisig wallet
mccwdev Nov 9, 2023
0749ed4
[ADD] sha256 method for convenience
mccwdev Nov 13, 2023
da75253
[ADD] Inverse method, allow to inverse public keys
mccwdev Nov 19, 2023
3f523a5
[ADD] Unittest to test inverse method
mccwdev Nov 19, 2023
d69a2b4
[ADD] Allow to create key using point (x,y) on curve
mccwdev Nov 21, 2023
507046f
[UPD] Test with bitcoind regtest network and update example
mccwdev Dec 11, 2023
46b3fe4
Rewrite commandline wallet part 1
mccwdev Dec 31, 2023
4273ba1
Rewrite commandlinewallet part 2
mccwdev Jan 12, 2024
ec29267
Merge remote-tracking branch 'refs/remotes/origin/release-v07' into r…
mccwdev Jan 12, 2024
79694d3
Solve unittest problems and merge conflicts
Jan 16, 2024
29ce79b
Rewrite commandline wallet and fix unittests
Jan 19, 2024
56e6c82
Remove testdbs
Jan 19, 2024
79d9448
Add interactive mode to commandline wallet. Change shortcut options
Jan 19, 2024
c105918
Fix argument shortcuts in unittests
mccwdev Jan 19, 2024
bfadb98
Add quiet en interactive mode
mccwdev Jan 20, 2024
38ecfa4
Pass network and witness_type arguments to bip38 decrypt
mccwdev Jan 20, 2024
aaa51fc
Specify values in Value object format, ie '0.1 mBTC'
mccwdev Jan 20, 2024
b1c80a2
Add commandline wallet unittest to send and empty wallet
mccwdev Jan 21, 2024
aee0f3f
Add clw unittest for more advanced transactions
Jan 22, 2024
e486c8e
Update commandline wallet manual
Jan 22, 2024
705734d
Add example usage of multisig commandline wallet
Jan 22, 2024
e61f66b
Fix clw unittests for Windows
Jan 22, 2024
20f2bbb
Fix clw unittests for Windows 2
Jan 22, 2024
3eb3790
Remove support for Dash and Dash testnet
Jan 22, 2024
38f79ca
Remove deprecated methods from Wallet, Key and Transaction class
Jan 22, 2024
11c2857
Add replace-by-fee option to Wallet, Transaction and clw
Jan 22, 2024
82df674
Resize key database fields
Jan 23, 2024
38cbb49
Disable insecure connection warnings
Jan 23, 2024
6cefd37
Fix old database updates
Jan 24, 2024
f5071a1
Rewrite wallet key generation system (6 times faster when creating ke…
Jan 26, 2024
bf488c2
Cleanup key create methods, update docstrings
Jan 26, 2024
ed98341
Add unittest for multisig multinetwork wallets
Jan 26, 2024
f2542a9
Add tool to test speed of bitcoinlib library
Feb 2, 2024
67437e4
Fix mysql issues and database unittesting
Feb 3, 2024
1961bf7
Add teardown methods to wallet unittests
Feb 5, 2024
78a5599
Fix PermissionErrors in Windows wallet unittest
Feb 5, 2024
b930fe1
Remove parameterized and use global database variable instead
Feb 5, 2024
5d67936
Fix issues with autoincrement key_id and postgresql
Feb 5, 2024
1545324
Add unittests for mysql database
Feb 6, 2024
556e97f
Use other mysql login in tools unittest
Feb 6, 2024
18d3c22
Use correct mysql database credentials
Feb 6, 2024
2c8b53f
Start mysql server when testing:
Feb 6, 2024
c0d06f6
Try with different mariadb setup
Feb 6, 2024
d78faaf
Try with different mariadb user
Feb 6, 2024
6c1bb7b
Try with different mariadb user 2
Feb 6, 2024
742b55d
Remove mysql unittests
Feb 6, 2024
1bce120
Update cache database code
Feb 7, 2024
4546bc1
Fix link to sqlite database for testing
Feb 7, 2024
52e64fb
Fix link to sqlite database for testing
Feb 7, 2024
429e2df
Fix cache database for PostgreSQL
Feb 7, 2024
e9dc5c1
Enable unittests for PostgreSQL
Feb 7, 2024
409aa79
Start PostgreSQL server before unittesting
Feb 7, 2024
dd6086d
Use psycopg3 everywhere for PostgreSQL database
Feb 7, 2024
f97e733
Use latest version of github actions
Feb 7, 2024
2871358
Try with same database
Feb 8, 2024
8386242
Use biginteger for output_n in tx output
mccwdev Feb 8, 2024
6e214f6
Add encrypted field test for mysql and postgresql
mccwdev Feb 8, 2024
9d7ff00
Unittest postgresql database
Feb 11, 2024
b752749
Add unittest for database field sizes
Feb 11, 2024
59746a0
Fix created database in wrong directory during unittests
Feb 11, 2024
1f8fec5
Add unittests for mysql database
Feb 12, 2024
b404b7f
Change mysql password
Feb 12, 2024
4d9de6c
Update mysql login in unittests
Feb 12, 2024
d46f1b6
Fix: Use correct database when unittesting
Feb 12, 2024
197331f
Use correct password for mysql server
Feb 12, 2024
929856c
And update mysql pass for latest unittest
Feb 12, 2024
54a2553
Skip mysql cache unittest
Feb 12, 2024
ab039de
Update database and encryption documentation
Feb 12, 2024
cf76b27
Add encryption warnings in docs
Feb 12, 2024
8ce0c58
Add tool to migrate wallets from old bitcoinlib database
Feb 12, 2024
71296b2
Store order of transactions in block in Transaction objects
mccwdev Feb 13, 2024
a3ae415
Add index, transaction order in block
Feb 13, 2024
fa04fcb
Add index when parsing block transactions as dict
Feb 13, 2024
bc0abb6
Move script definitions to config file
Feb 13, 2024
0c3f242
Fix import enum
Feb 13, 2024
0e70f48
Add bech32m unittest for valid addresses
mccwdev Feb 15, 2024
db8a1bf
Remove duplicate bech32m tests
Feb 15, 2024
3c0b58e
Check unittest asserts
mccwdev Feb 16, 2024
20fdf08
Merge branch 'release-v07' of github.com:1200wd/bitcoinlib into relea…
mccwdev Feb 16, 2024
dd843dc
Remove a to_bytes method in keys
mccwdev Feb 17, 2024
bb73ba9
Fix unittest Invalid padding bits warning
mccwdev Feb 17, 2024
d76117b
Move wallet db session to separate method
mccwdev Feb 18, 2024
45828ac
Remove references to Dash network
mccwdev Feb 18, 2024
9403f24
Encrypt private key field with key or password in environment
mccwdev Feb 22, 2024
a005eee
Fix to store encrypted fields in postgresql
Feb 24, 2024
5905559
Correctly store encrypted fields as bytes in psql database
Feb 24, 2024
2c6a3c5
Show clearer warnings when try to open encrypted database
Feb 25, 2024
2d621bf
Rename database to avoid gitignore
Feb 25, 2024
8a4d817
Skip pwd test if no pwd in environment
Feb 25, 2024
b329eb2
Add documentation for clw and encrypted private data
Feb 25, 2024
a143424
Update encrypted field documentation
Feb 25, 2024
bcb6b97
Move config unittest file
Feb 25, 2024
dbf6883
Move all unittest configfiles to unittest dir
Feb 25, 2024
0f60ee8
Change config.ini reference
Feb 26, 2024
f6aa339
Fix config.ini reference
Feb 26, 2024
8017a40
Remove duplicate queries in transaction_update method
Feb 26, 2024
ccd0351
Remove old code
Feb 26, 2024
8553404
Add method to create intermediate passwords for EC multiplied BIP38 keys
Mar 1, 2024
5d75a0a
wft is scrypt doing
Mar 1, 2024
48a5b19
Working to add EC multiplied keys
Mar 2, 2024
1855ada
Fix key math methods and unittests
Mar 2, 2024
5c6b2cc
Inverse of uncompressed public key does not exist
Mar 2, 2024
b6054ef
Add EC point multiplication
Mar 3, 2024
7b1c016
Add BIP38 ec multiplied keys
Mar 6, 2024
b652ee2
Add method to decrypt ec multiplied bip38 keys
Mar 7, 2024
04897b6
Add EC multiply unittests
Mar 8, 2024
2f4de7b
Fix issue with uncompressed keys create from public point
Mar 9, 2024
a93920b
Skip encrypt unittest for EC multiplied keys
Mar 9, 2024
871813c
Add more unittests for EC multiplied BIP38 keys
Mar 9, 2024
f43aa39
Get EC BIP38 key multiplication working with ecdsa module
Mar 11, 2024
e38d4e5
Add BIP38 key examples
Mar 11, 2024
75afa63
Add more inverse key unittests
Mar 11, 2024
9b7ff64
Add multi witnesstype unittest with passphrases
Mar 11, 2024
9732b2b
Fix issue with parsing nonstandard input scripts
Mar 12, 2024
b1e5d98
Use anti-fee-snipping and set locktime to current block+1 by default …
Mar 12, 2024
e125827
Add unittests for anti fee snipping for wallets
mccwdev Mar 13, 2024
a05bf88
Add anti-fee-sniping option to commandline wallet
mccwdev Mar 13, 2024
bdba7a5
Fix typo in database: sniping
mccwdev Mar 13, 2024
784e994
Fix unittest with incorrect blockcounts
Mar 14, 2024
54a2ae1
Increase delta in blockcount unittests
Mar 14, 2024
63d8bcf
Fix script issues, test with CLTV scripts
Mar 18, 2024
62200d6
Fix incorrect data attribute
Mar 18, 2024
f9767f0
Enable timelock unittests
Mar 18, 2024
6c75717
Check for invalid transaction locktime
Mar 18, 2024
85e316e
Avoid type conversions when parsing transactions
Mar 18, 2024
d9b6c02
Fix calling wrong method when parsing transaction
Apr 5, 2024
70d35db
Allow to derive hardened change and address_index keys, ie used in bi…
Apr 6, 2024
8595801
Define standard key paths in configuration
Apr 7, 2024
482e9bc
Add example connect to bitcoin core node
Apr 7, 2024
2917687
Merge bugfix-2024-apr in release-v07
Apr 8, 2024
07b8464
Merge again: Fix multi wallet connection bitcoind client with optiona…
n1rna Jan 22, 2024
41bd1bc
Add unittest for bitcoincore style key derivation
Apr 8, 2024
b267e67
Move bitcoind wallet name to providerclient
Apr 8, 2024
a595854
Fix issue with 1 byte data items
Apr 12, 2024
8822adb
Add Script.view method
Apr 12, 2024
b85cc50
Add as_bytes and as_hex method to Script
Apr 15, 2024
48aabd8
Add method to print Script object in human-readable string
Apr 15, 2024
c6271c0
Fix wrong p2pk input address
Apr 15, 2024
1ea9443
Add method to update Transactions input scripts
Apr 15, 2024
5938b3f
Add p2pk unittest
Apr 15, 2024
7c72d94
Add as_hex and as_bytes to other classes
Apr 15, 2024
00b8029
Use segwit as default for Address class
Apr 16, 2024
7171d5f
Rename script to locking_script
Apr 16, 2024
3de9927
Cleanup Input method
Apr 16, 2024
96006b5
Rename offline parameter for broadcast in send methods
Apr 19, 2024
fcb3eb1
Fix removing wallet transactions
Apr 22, 2024
87c1f07
Allow to disable random tx outputs order
Apr 22, 2024
0150aa4
Adjust fee estimates for segwit transactions
Apr 22, 2024
1f0c5db
Add Transaction method bumpfee
Apr 28, 2024
5413b31
Improve sweep method fee estimation
Apr 28, 2024
b7bed7c
Fix Windows delete tx unittest
Apr 28, 2024
e975baf
Fix unittest for providers returning out-of-date data
Apr 28, 2024
814abfe
Remove bitaps, bitgo from some unittests
May 5, 2024
a2784a1
Fix issue with excluded service providers for non-bitcoin networks
May 6, 2024
3e49628
Add exclude providers unittest
May 6, 2024
19ca898
Add bumpfee method to wallet
May 7, 2024
702acbe
Add method to remove unconfirmed transactions
May 8, 2024
fc64228
Allow to specify age when deleting txs
May 9, 2024
ad6e32c
Fix: Compare correct UTC times
May 14, 2024
4c3ed2a
Try to fix session problems
May 14, 2024
1393e97
Remove bitgo and bitaps providers for testnet, reenable blockcypher
May 14, 2024
fc3a0ab
Fix issue in unittest if provider blockcount is not working
May 14, 2024
f165a23
Add some debug info to failing unittest
May 14, 2024
d15dc38
Fix dust to fee unittest for windows
May 14, 2024
44b0ba3
Fix restore saved tx unittest
May 14, 2024
48e9b2f
Fix some database errors
May 15, 2024
7b3bf2a
Small update WalletKey docstring
May 16, 2024
46fe2d8
Add instructions for a readonly bitcoinlib wallet from bitcoin core
May 16, 2024
0d5b039
Remove confusing script_code attribute from Input
May 18, 2024
eeb44a4
Increase delta in blockcount unittest
May 18, 2024
a98dfdb
Add redeemscript to Input, cleanup Input init method
May 19, 2024
f39ccc0
Cleanup Script and script_types
May 22, 2024
18083ad
Add trace method to Script evaluate method
May 24, 2024
4fffc73
Distinct redeemscript from rest of script
May 28, 2024
3c4bd3b
op_checkmultisig should leave 1 on stack when succesfull
May 29, 2024
7fc61a3
Fix issue when trying to add existing key to wallet
May 29, 2024
7d1b1d8
Add script locking type unittest
May 30, 2024
f84552e
Use segwit as default for Input class
Jun 1, 2024
5d32d2d
Add and fix witness_type in Address and Output class
Jun 2, 2024
33dd52d
Pass hash_type to sign methods and add unsupported warning
Jun 3, 2024
0a07438
Merge with master branch
Jun 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/unittests-mysql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Bitcoinlib Tests Ubuntu - MySQL
on: [push]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
architecture: 'x64'
- name: Set up MySQL
env:
DB_DATABASE: bitcoinlib_test
DB_USER: root
DB_PASSWORD: root
run: |
sudo /etc/init.d/mysql start
mysql -e 'CREATE DATABASE ${{ env.DB_DATABASE }};' -u${{ env.DB_USER }} -p${{ env.DB_PASSWORD }}
- name: Install dependencies
run: |
python -m pip install .[dev]
- name: Test with coverage
env:
BCL_CONFIG_FILE: ${{ github.workspace }}/tests/config.ini.unittest
UNITTEST_DATABASE: mysql
run: coverage run --source=bitcoinlib -m unittest -v
9 changes: 4 additions & 5 deletions .github/workflows/unittests-noscrypt.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Bitcoinlib Unittests Coveralls Ubuntu - No scrypt
name: Bitcoinlib Tests Ubuntu - No scrypt
on: [push]

jobs:
Expand All @@ -7,9 +7,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: '3.10'
architecture: 'x64'
Expand All @@ -19,7 +19,6 @@ jobs:
pip uninstall -y scrypt
- name: Test with coverage
env:
BCL_CONFIG_FILE: config_encryption.ini.unittest
UNITTESTS_FULL_DATABASE_TEST: False
BCL_CONFIG_FILE: ${{ github.workspace }}/tests/config_encryption.ini.unittest
DB_FIELD_ENCRYPTION_KEY: 11223344556677889900aabbccddeeff11223344556677889900aabbccddeeff
run: coverage run --source=bitcoinlib -m unittest -v
40 changes: 40 additions & 0 deletions .github/workflows/unittests-postgresql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Bitcoinlib Tests Ubuntu - PostgreSQL
on: [push]

jobs:
test:
runs-on: ubuntu-latest

services:
postgres:
image: postgres

env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: bitcoinlib_test

options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
architecture: 'x64'
- name: Install dependencies
run: |
python -m pip install .[dev]
- name: Test with coverage
env:
BCL_CONFIG_FILE: ${{ github.workspace }}/tests/config.ini.unittest
UNITTEST_DATABASE: postgresql
DB_FIELD_ENCRYPTION_PASSWORD: verybadpassword
run: coverage run --source=bitcoinlib -m unittest -v
11 changes: 5 additions & 6 deletions .github/workflows/unittests.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Bitcoinlib Unittests Coveralls Ubuntu
name: Bitcoinlib Tests Ubuntu
on: [push]

jobs:
Expand All @@ -7,12 +7,12 @@ jobs:

strategy:
matrix:
python: ["3.8", "3.10", "3.11"]
python: ["3.8", "3.11"]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
architecture: 'x64'
Expand All @@ -21,8 +21,7 @@ jobs:
python -m pip install .[dev]
- name: Test with coverage
env:
BCL_CONFIG_FILE: config.ini.unittest
UNITTESTS_FULL_DATABASE_TEST: False
BCL_CONFIG_FILE: ${{ github.workspace }}/tests/config.ini.unittest
run: coverage run --source=bitcoinlib -m unittest -v

- name: Coveralls
Expand Down
9 changes: 4 additions & 5 deletions .github/workflows/unittests_windows.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Bitcoinlib Windows Unittests
name: Bitcoinlib Tests Windows
on: [push]

jobs:
Expand All @@ -7,9 +7,9 @@ jobs:
runs-on: windows-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: '3.11'
architecture: 'x64'
Expand All @@ -18,7 +18,6 @@ jobs:
python -m pip install .[dev]
- name: Test with coverage
env:
BCL_CONFIG_FILE: config.ini.unittest
UNITTESTS_FULL_DATABASE_TEST: False
BCL_CONFIG_FILE: ${{ github.workspace }}/tests/config.ini.unittest
PYTHONUTF8: 1
run: coverage run --source=bitcoinlib -m unittest -v
86 changes: 8 additions & 78 deletions bitcoinlib/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,13 @@ def parse_bytesio(cls, raw, block_hash=None, height=None, parse_transactions=Fal
raw.seek(tx_start_pos)
transactions = []

index = 0
while parse_transactions and raw.tell() < txs_data_size:
if limit != 0 and len(transactions) >= limit:
break
t = Transaction.parse_bytesio(raw, strict=False)
t = Transaction.parse_bytesio(raw, strict=False, index=index)
transactions.append(t)
index += 1
# TODO: verify transactions, need input value from previous txs
# if verify and not t.verify():
# raise ValueError("Could not verify transaction %s in block %s" % (t.txid, block_hash))
Expand All @@ -270,81 +272,6 @@ def parse_bytesio(cls, raw, block_hash=None, height=None, parse_transactions=Fal
block.tx_count = tx_count
return block

@classmethod
@deprecated
def from_raw(cls, raw, block_hash=None, height=None, parse_transactions=False, limit=0, network=DEFAULT_NETWORK): # pragma: no cover
"""
Create Block object from raw serialized block in bytes.

Get genesis block:

>>> from bitcoinlib.services.services import Service
>>> srv = Service()
>>> b = srv.getblock(0)
>>> b.block_hash.hex()
'000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'

:param raw: Raw serialize block
:type raw: bytes
:param block_hash: Specify block hash if known to verify raw block. Value error will be raised if calculated block hash is different than specified.
:type block_hash: bytes
:param height: Specify height if known. Will be derived from coinbase transaction if not provided.
:type height: int
:param parse_transactions: Indicate if transactions in raw block need to be parsed and converted to Transaction objects. Default is False
:type parse_transactions: bool
:param limit: Maximum number of transactions to parse. Default is 0: parse all transactions. Only used if parse_transaction is set to True
:type limit: int
:param network: Name of network
:type network: str

:return Block:
"""
block_hash_calc = double_sha256(raw[:80])[::-1]
if not block_hash:
block_hash = block_hash_calc
elif block_hash != block_hash_calc:
raise ValueError("Provided block hash does not correspond to calculated block hash %s" %
block_hash_calc.hex())

version = raw[0:4][::-1]
prev_block = raw[4:36][::-1]
merkle_root = raw[36:68][::-1]
time = raw[68:72][::-1]
bits = raw[72:76][::-1]
nonce = raw[76:80][::-1]
tx_count, size = varbyteint_to_int(raw[80:89])
txs_data = BytesIO(raw[80+size:])

# Parse coinbase transaction so we can extract extra information
# transactions = [Transaction.parse(txs_data, network=network)]
# txs_data = BytesIO(txs_data[transactions[0].size:])
# block_txs_data = txs_data.read()
txs_data_size = txs_data.seek(0, 2)
txs_data.seek(0)
transactions = []

while parse_transactions and txs_data and txs_data.tell() < txs_data_size:
if limit != 0 and len(transactions) >= limit:
break
t = Transaction.parse_bytesio(txs_data, strict=False)
transactions.append(t)
# t = transaction_deserialize(txs_data, network=network, check_size=False)
# transactions.append(t)
# txs_data = txs_data[t.size:]
# TODO: verify transactions, need input value from previous txs
# if verify and not t.verify():
# raise ValueError("Could not verify transaction %s in block %s" % (t.txid, block_hash))

if parse_transactions and limit == 0 and tx_count != len(transactions):
raise ValueError("Number of found transactions %d is not equal to expected number %d" %
(len(transactions), tx_count))

block = cls(block_hash, version, prev_block, merkle_root, time, bits, nonce, transactions, height,
network=network)
block.txs_data = txs_data
block.tx_count = tx_count
return block

def parse_transactions(self, limit=0):
"""
Parse raw transactions from Block, if transaction data is available in txs_data attribute. Creates
Expand Down Expand Up @@ -373,11 +300,13 @@ def parse_transactions_dict(self):
"""
transactions_dict = []
txs_data_orig = deepcopy(self.txs_data)
index = 0
while self.txs_data and len(self.transactions) < self.tx_count:
tx = self.parse_transaction_dict()
tx = self.parse_transaction_dict(index)
if not tx:
break
transactions_dict.append(tx)
index += 1
self.txs_data = txs_data_orig
return transactions_dict

Expand All @@ -394,7 +323,7 @@ def parse_transaction(self):
return t
return False

def parse_transaction_dict(self):
def parse_transaction_dict(self, index=None):
"""
Parse a single transaction from Block, if transaction data is available in txs_data attribute. Add
Transaction object in Block and return the transaction
Expand Down Expand Up @@ -480,6 +409,7 @@ def parse_transaction_dict(self):
tx['txid'] = double_sha256(tx['version'][::-1] + raw_n_inputs + inputs_raw + raw_n_outputs + outputs_raw
+ tx_locktime)[::-1]
tx['size'] = len(tx['rawtx'])
tx['index'] = index
# TODO: tx['vsize'] = len(tx['rawtx'])
return tx
return False
Expand Down
Loading
Loading