Skip to content

Commit 46d21a3

Browse files
author
Vizeet Srivastava
committed
script P2PKH parsing done
1 parent cdcf94d commit 46d21a3

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

bitcoin_localapis.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import os
1414
import glob
1515

16-
myaddress = '1PstYL7f1iUCTtbWfUkYbqg8FkifHTyauK'
16+
myaddress = input('Enter Bitcoin Address: ')
1717

1818
rpc_connection = AuthServiceProxy("http://%s:%[email protected]:8332"%('alice', 'passw0rd'))
1919

bitcoin_networkapis.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import datetime
55
import pandas as pd
66

7-
myaddress = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
7+
myaddress = input('Enter Bitcoin Address: ')
88

99
btcval = 100000000.0 # in santoshis
1010

@@ -205,7 +205,7 @@ def getCurrentBuyPriceInInr():
205205
getreq.perform()
206206
getreq.close()
207207

208-
current_buy_rate_in_inr = json.loads(strbuf.getvalue())['buy']
208+
current_buy_rate_in_inr = int(json.loads(strbuf.getvalue())['buy'])
209209

210210
return current_buy_rate_in_inr
211211

@@ -218,7 +218,7 @@ def getCurrentSellPriceInInr():
218218
getreq.perform()
219219
getreq.close()
220220

221-
current_buy_rate_in_inr = json.loads(strbuf.getvalue())['sell']
221+
current_buy_rate_in_inr = int(json.loads(strbuf.getvalue())['sell'])
222222

223223
return current_buy_rate_in_inr
224224

script_parser.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#raw_txn_str = '01000000012f03082d300efd92837d3f6d910a21d9d19e868242cfebb21198beed7b440999000000004a493046022100c0f693e024f966dc5f834324baa38426bba05460a2b3f9920989d38322176460022100c523a3aa62da26db1fc1902a93741dce3489629df18be11ba68ff9586041821601ffffffff0100f2052a010000001976a9148773ec867e322378e216eefe55bfcede5263059b88ac00000000'
1717
#raw_txn_str = '01000000017f950ab790838e0c05e79856d25d586823fe139e1807405a3f207ff33f9b7663010000006b483045022100d8629403cd3b49950da9293653c6279149c029e6b7b15371342d0d2ce286c8f2022078787985a644e94fd9246f6c25733336c94af5f00d9d34a07dc2f9e0987ef990012102b726d7eae11a6d5cf3b2362e773e116a6140347dcee1b2943f4a2897351e5d90ffffffff021bf03c000000000017a91469f3757380a56820abc7052867216599e575cddd8777c1ca1c000000001976a914d5f950abe0b559b2b7a7ab3d18a507ea1c3e4ac688ac00000000'
1818
#txn_hash = '4269fdc239d027922dcec96f1ae283dbaff10e2d1bd49605661d091e79714956'
19-
txn_hash = '8ca45aed169b0434ad5a117804cdf6eec715208d57f13396c0ba18fb5a327e30'
19+
#txn_hash = '8ca45aed169b0434ad5a117804cdf6eec715208d57f13396c0ba18fb5a327e30' # P2PKH
20+
#txn_hash = '40eee3ae1760e3a8532263678cdf64569e6ad06abc133af64f735e52562bccc8'
21+
txn_hash = '7edb32d4ffd7a385b763c7a8e56b6358bcd729e747290624e18acdbe6209fc45' # P2SH
2022
block_hash = '0000000000000000009a8aa7b36b0e37a28bf98956097b7b844e172692e604e1'
2123

2224

@@ -592,6 +594,11 @@ def scriptParser(txn: dict, input_index: int):
592594
else: # Any non assigned opcode
593595
return False
594596

597+
print('stack = %s' % stack)
598+
for item in stack:
599+
if type(item) == bytes:
600+
print('stack item = %s' % bytes.decode(binascii.hexlify(item)))
601+
595602
return stack.pop()
596603

597604
def convertPKHToAddress(prefix, addr):
@@ -721,10 +728,11 @@ def unlockTxn(mptr: mmap):
721728
print('Network fees = %d' % (input_satoshis - out_satoshis))
722729
for index in range(input_count):
723730
status = scriptParser(txn, index)
731+
print ('status = %s' % status)
724732
if status == False:
725733
print('Invalid Transaction')
726-
return False
727-
return True
734+
return (False, 0)
735+
return (True, input_satoshis - out_satoshis)
728736

729737
g_block_header_size = 80
730738

@@ -747,20 +755,19 @@ def validate_all_transactions_of_block(block_hash_bigendian_b: bytes):
747755
getBlockHeader(mptr)
748756
txn_count = getTransactionCount(mptr)
749757
print('txn_count = %d' % txn_count)
750-
skip_coinbase_txn = getCoinbaseTransaction(mptr)
758+
coinbase_txn = getCoinbaseTransaction(mptr)
759+
net_fees = 0
751760
for index in range(1, txn_count):
752761
print('XXXXXXXXXXXXXXXX txn index = %d' % index)
753-
isValid = unlockTxn(mptr)
762+
isValid, satoshis = unlockTxn(mptr)
754763
if isValid == False:
755764
print('Invalid Transaction')
756765
exit()
757766
else:
758767
print('Valid Transaction')
759-
# block = getBlock(mptr, start - 8)
760-
# print('magic number = %s' % (block['block_pre_header']['magic_number']))
761-
# next_block_hash = block['block_header']['prev_block_hash']
762-
# print('next block hash = %s, n_file = %d, height = %d' % (next_block_hash, jsonobj['n_file'], jsonobj['height']))
763-
# next_block_hash_bigendian_b = binascii.unhexlify(next_block_hash)[::-1]
768+
net_fees += satoshis
769+
print('mining reward = %.8f' % (sum(out['satoshis'] for out in coinbase_txn['out']) / 100000000.0))
770+
print('net_fees = %.8f' % (net_fees / 100000000.0))
764771

765772
if __name__ == '__main__':
766773
txn_hash_bigendian = binascii.unhexlify(txn_hash)[::-1]
@@ -774,13 +781,11 @@ def validate_all_transactions_of_block(block_hash_bigendian_b: bytes):
774781
mptr = mmap.mmap(block_file.fileno(), 0, prot=mmap.PROT_READ) #File is open read-only
775782

776783
mptr.seek(block_offset + g_block_header_size + txn_offset)
777-
# print(bytes.decode(binascii.hexlify(mptr.read(1000))))
778-
# print(raw_txn_str)
779784
isValid = unlockTxn(mptr)
780785
if isValid == False:
781786
print('Invalid Transaction')
782787
else:
783788
print('Valid Transaction')
784789

785-
block_hash_bigendian_b = binascii.unhexlify(block_hash)[::-1]
786-
validate_all_transactions_of_block(block_hash_bigendian_b)
790+
# block_hash_bigendian_b = binascii.unhexlify(block_hash)[::-1]
791+
# validate_all_transactions_of_block(block_hash_bigendian_b)

script_utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def get_bytes_from_readable_script(script: str):
126126
if __name__ == '__main__':
127127
print(g_value_opcode_dict)
128128

129-
script_b = binascii.unhexlify('76a91460d47ac02b129c08f94232ea506d1826424fe7be88ac')
129+
# script_b = binascii.unhexlify('76a91460d47ac02b129c08f94232ea506d1826424fe7be88ac')
130+
script_b = binascii.unhexlify('a914e9c3dd0c07aac76179ebc76a6c78d4d67c6c160a87')
130131
script_str = get_readable_script(script_b)
131132
print('script :: %s' % script_str)

0 commit comments

Comments
 (0)