forked from jimmysong/programmingbitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjupyter.txt
More file actions
133 lines (122 loc) · 3.83 KB
/
jupyter.txt
File metadata and controls
133 lines (122 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import bloomfilter
import block
import ecc
import helper
import merkleblock
import network
import script
import tx
---
example1
---
example2
---
exercise1:
from helper import hash160
bit_field_size = 10
bit_field = [0] * bit_field_size
items = (b'hello world', b'goodbye')
# loop through each item
# hash160 the item
# interpret hash as a Big-Endian integer and mod by bit_field_size
# set that bit in bit_field to 1
# print the bit_field
---
example3
---
example4
---
exercise2:
from bloomfilter import BloomFilter, BIP37_CONSTANT
from helper import bit_field_to_bytes, murmur3
field_size = 10
function_count = 5
tweak = 99
items = (b'Hello World', b'Goodbye!')
# calculate the bitfield size
# create an empty bit field
# loop through items
# loop through function count
# calculate the seed
# get the murmur3 hash of the item using the seed
# mod by the bitfield size
# set the bit
# convert the bit field to bytes
# print the bytes in hex
---
exercise3:bloomfilter:BloomFilterTest:test_add
---
exercise4:bloomfilter:BloomFilterTest:test_filterload
---
exercise5:network:GetDataMessageTest:test_serialize
---
example5
---
exercise6:
import time
from block import Block
from bloomfilter import BloomFilter
from ecc import PrivateKey
from helper import hash256, little_endian_to_int, encode_varint, read_varint, decode_base58, SIGHASH_ALL
from merkleblock import MerkleBlock
from network import (
GetDataMessage,
GetHeadersMessage,
HeadersMessage,
NetworkEnvelope,
SimpleNode,
TX_DATA_TYPE,
FILTERED_BLOCK_DATA_TYPE,
)
from script import p2pkh_script, Script
from tx import Tx, TxIn, TxOut
last_block_hex = '' # FILL THIS IN
secret = little_endian_to_int(hash256(b'')) # FILL THIS IN
private_key = PrivateKey(secret=secret)
addr = private_key.point.address(testnet=True)
h160 = decode_base58(addr)
target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv'
target_h160 = decode_base58(target_address)
target_script = p2pkh_script(target_h160)
fee = 5000 # fee in satoshis
# connect to testnet.programmingbitcoin.com in testnet mode
# create a bloom filter of size 30 and 5 functions. Add a tweak.
# add the h160 to the bloom filter
# complete the handshake
# load the bloom filter with the filterload command
# set start block to last_block from above
# send a getheaders message with the starting block
# wait for the headers message
# store the last block as None
# initialize the GetDataMessage
# loop through the blocks in the headers
# check that the proof of work on the block is valid
# check that this block's prev_block is the last block
# add a new item to the get_data_message
# should be FILTERED_BLOCK_DATA_TYPE and block hash
# set the last block to the current hash
# send the getdata message
# initialize prev_tx and prev_index to None
# loop while prev_tx is None
# wait for the merkleblock or tx commands
# if we have the merkleblock command
# check that the MerkleBlock is valid
# else we have the tx command
# set the tx's testnet to be True
# loop through the tx outs
# if our output has the same address as our address we found it
# we found our utxo. set prev_tx, prev_index, and tx
# create the TxIn
# calculate the output amount (previous amount minus the fee)
# create a new TxOut to the target script with the output amount
# create a new transaction with the one input and one output
# sign the only input of the transaction
# serialize and hex to see what it looks like
# send this signed transaction on the network
# wait a sec so this message goes through with time.sleep(1)
# now ask for this transaction from the other node
# create a GetDataMessage
# ask for our transaction by adding it to the message
# send the message
# now wait for a Tx response
# if the received tx has the same id as our tx, we are done!