moved base58 to only p2pool.bitcoin.data
[p2pool.git] / p2pool / bitcoin / data.py
1 from __future__ import division
2
3 import hashlib
4
5 from p2pool.util import bases, math, pack
6 from . import base58
7
8 class ChecksummedType(pack.Type):
9     def __init__(self, inner):
10         self.inner = inner
11     
12     def read(self, file):
13         obj, file = self.inner.read(file)
14         data = self.inner.pack(obj)
15         
16         checksum, file = pack.read(file, 4)
17         if checksum != hashlib.sha256(hashlib.sha256(data).digest()).digest()[:4]:
18             raise ValueError('invalid checksum')
19         
20         return obj, file
21     
22     def write(self, file, item):
23         data = self.inner.pack(item)
24         return (file, data), hashlib.sha256(hashlib.sha256(data).digest()).digest()[:4]
25
26 class FloatingInteger(object):
27     __slots__ = ['bits', '_target']
28     
29     @classmethod
30     def from_target_upper_bound(cls, target):
31         n = bases.natural_to_string(target)
32         if n and ord(n[0]) >= 128:
33             n = '\x00' + n
34         bits2 = (chr(len(n)) + (n + 3*chr(0))[:3])[::-1]
35         bits = pack.IntType(32).unpack(bits2)
36         return cls(bits)
37     
38     def __init__(self, bits, target=None):
39         self.bits = bits
40         self._target = None
41         if target is not None and self.target != target:
42             raise ValueError('target does not match')
43     
44     @property
45     def target(self):
46         res = self._target
47         if res is None:
48             res = self._target = math.shift_left(self.bits & 0x00ffffff, 8 * ((self.bits >> 24) - 3))
49         return res
50     
51     def __hash__(self):
52         return hash(self.bits)
53     
54     def __eq__(self, other):
55         return self.bits == other.bits
56     
57     def __ne__(self, other):
58         return not (self == other)
59     
60     def __cmp__(self, other):
61         assert False
62     
63     def __repr__(self):
64         return 'FloatingInteger(bits=%s, target=%s)' % (hex(self.bits), hex(self.target))
65
66 class FloatingIntegerType(pack.Type):
67     _inner = pack.IntType(32)
68     
69     def read(self, file):
70         bits, file = self._inner.read(file)
71         return FloatingInteger(bits), file
72     
73     def write(self, file, item):
74         return self._inner.write(file, item.bits)
75
76 address_type = pack.ComposedType([
77     ('services', pack.IntType(64)),
78     ('address', pack.IPV6AddressType()),
79     ('port', pack.IntType(16, 'big')),
80 ])
81
82 tx_type = pack.ComposedType([
83     ('version', pack.IntType(32)),
84     ('tx_ins', pack.ListType(pack.ComposedType([
85         ('previous_output', pack.PossiblyNoneType(dict(hash=0, index=2**32 - 1), pack.ComposedType([
86             ('hash', pack.IntType(256)),
87             ('index', pack.IntType(32)),
88         ]))),
89         ('script', pack.VarStrType()),
90         ('sequence', pack.PossiblyNoneType(2**32 - 1, pack.IntType(32))),
91     ]))),
92     ('tx_outs', pack.ListType(pack.ComposedType([
93         ('value', pack.IntType(64)),
94         ('script', pack.VarStrType()),
95     ]))),
96     ('lock_time', pack.IntType(32)),
97 ])
98
99 merkle_branch_type = pack.ListType(pack.IntType(256))
100
101 merkle_tx_type = pack.ComposedType([
102     ('tx', tx_type),
103     ('block_hash', pack.IntType(256)),
104     ('merkle_branch', merkle_branch_type),
105     ('index', pack.IntType(32)),
106 ])
107
108 block_header_type = pack.ComposedType([
109     ('version', pack.IntType(32)),
110     ('previous_block', pack.PossiblyNoneType(0, pack.IntType(256))),
111     ('merkle_root', pack.IntType(256)),
112     ('timestamp', pack.IntType(32)),
113     ('bits', FloatingIntegerType()),
114     ('nonce', pack.IntType(32)),
115 ])
116
117 block_type = pack.ComposedType([
118     ('header', block_header_type),
119     ('txs', pack.ListType(tx_type)),
120 ])
121
122 aux_pow_type = pack.ComposedType([
123     ('merkle_tx', merkle_tx_type),
124     ('merkle_branch', merkle_branch_type),
125     ('index', pack.IntType(32)),
126     ('parent_block_header', block_header_type),
127 ])
128
129
130 merkle_record_type = pack.ComposedType([
131     ('left', pack.IntType(256)),
132     ('right', pack.IntType(256)),
133 ])
134
135 def merkle_hash(hashes):
136     if not hashes:
137         return 0
138     hash_list = list(hashes)
139     while len(hash_list) > 1:
140         hash_list = [merkle_record_type.hash256(dict(left=left, right=left if right is None else right))
141             for left, right in zip(hash_list[::2], hash_list[1::2] + [None])]
142     return hash_list[0]
143
144 def calculate_merkle_branch(hashes, index):
145     # XXX optimize this
146     
147     hash_list = [(h, i == index, []) for i, h in enumerate(hashes)]
148     
149     while len(hash_list) > 1:
150         hash_list = [
151             (
152                 merkle_record_type.hash256(dict(left=left, right=right)),
153                 left_f or right_f,
154                 (left_l if left_f else right_l) + [dict(side=1, hash=right) if left_f else dict(side=0, hash=left)],
155             )
156             for (left, left_f, left_l), (right, right_f, right_l) in
157                 zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])
158         ]
159     
160     res = [x['hash'] for x in hash_list[0][2]]
161     
162     assert hash_list[0][1]
163     assert check_merkle_branch(hashes[index], index, res) == hash_list[0][0]
164     assert index == sum(k*2**i for i, k in enumerate([1-x['side'] for x in hash_list[0][2]]))
165     
166     return res
167
168 def check_merkle_branch(tip_hash, index, merkle_branch):
169     return reduce(lambda c, (i, h): merkle_record_type.hash256(
170         dict(left=h, right=c) if 2**i & index else
171         dict(left=c, right=h)
172     ), enumerate(merkle_branch), tip_hash)
173
174 def target_to_average_attempts(target):
175     return 2**256//(target + 1)
176
177 def target_to_difficulty(target):
178     return (0xffff0000 * 2**(256-64) + 1)/(target + 1)
179
180 # tx
181
182 def tx_get_sigop_count(tx):
183     return sum(script.get_sigop_count(txin['script']) for txin in tx['tx_ins']) + sum(script.get_sigop_count(txout['script']) for txout in tx['tx_outs'])
184
185 # human addresses
186
187 human_address_type = ChecksummedType(pack.ComposedType([
188     ('version', pack.IntType(8)),
189     ('pubkey_hash', pack.IntType(160)),
190 ]))
191
192 pubkey_type = pack.PassthruType()
193
194 def pubkey_hash_to_address(pubkey_hash, net):
195     return base58.encode(human_address_type.pack(dict(version=net.ADDRESS_VERSION, pubkey_hash=pubkey_hash)))
196
197 def pubkey_to_address(pubkey, net):
198     return pubkey_hash_to_address(pubkey_type.hash160(pubkey), net)
199
200 def address_to_pubkey_hash(address, net):
201     x = human_address_type.unpack(base58.decode(address))
202     if x['version'] != net.ADDRESS_VERSION:
203         raise ValueError('address not for this net!')
204     return x['pubkey_hash']
205
206 # transactions
207
208 def pubkey_to_script2(pubkey):
209     return ('\x41' + pubkey_type.pack(pubkey)) + '\xac'
210
211 def pubkey_hash_to_script2(pubkey_hash):
212     return '\x76\xa9' + ('\x14' + pack.IntType(160).pack(pubkey_hash)) + '\x88\xac'
213
214 def script2_to_address(script2, net):
215     try:
216         pubkey = script2[1:-1]
217         script2_test = pubkey_to_script2(pubkey)
218     except:
219         pass
220     else:
221         if script2_test == script2:
222             return pubkey_to_address(pubkey, net)
223     
224     try:
225         pubkey_hash = pack.IntType(160).unpack(script2[3:-2])
226         script2_test2 = pubkey_hash_to_script2(pubkey_hash)
227     except:
228         pass
229     else:
230         if script2_test2 == script2:
231             return pubkey_hash_to_address(pubkey_hash, net)
232
233 def script2_to_human(script2, net):
234     try:
235         pubkey = script2[1:-1]
236         script2_test = pubkey_to_script2(pubkey)
237     except:
238         pass
239     else:
240         if script2_test == script2:
241             return 'Pubkey. Address: %s' % (pubkey_to_address(pubkey, net),)
242     
243     try:
244         pubkey_hash = pack.IntType(160).unpack(script2[3:-2])
245         script2_test2 = pubkey_hash_to_script2(pubkey_hash)
246     except:
247         pass
248     else:
249         if script2_test2 == script2:
250             return 'Address. Address: %s' % (pubkey_hash_to_address(pubkey_hash, net),)
251     
252     return 'Unknown. Script: %s'  % (script2.encode('hex'),)