1 from __future__ import division
8 from p2pool.util import math, pack
11 return pack.IntType(256).unpack(hashlib.sha256(hashlib.sha256(data).digest()).digest())
14 if data == '04ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1b3d8090496b53256786bf5c82932ec23c3b74d9f05a6f95a8b5529352656664b'.decode('hex'):
15 return 0x384f570ccc88ac2e7e00b026d1690a3fca63dd0 # hack for people who don't have openssl - this is the only value that p2pool ever hashes
16 return pack.IntType(160).unpack(hashlib.new('ripemd160', hashlib.sha256(data).digest()).digest())
18 class ChecksummedType(pack.Type):
19 def __init__(self, inner, checksum_func=lambda data: hashlib.sha256(hashlib.sha256(data).digest()).digest()[:4]):
21 self.checksum_func = checksum_func
24 obj, file = self.inner.read(file)
25 data = self.inner.pack(obj)
27 calculated_checksum = self.checksum_func(data)
28 checksum, file = pack.read(file, len(calculated_checksum))
29 if checksum != calculated_checksum:
30 raise ValueError('invalid checksum')
34 def write(self, file, item):
35 data = self.inner.pack(item)
36 return (file, data), self.checksum_func(data)
38 class FloatingInteger(object):
39 __slots__ = ['bits', '_target']
42 def from_target_upper_bound(cls, target):
43 n = math.natural_to_string(target)
44 if n and ord(n[0]) >= 128:
46 bits2 = (chr(len(n)) + (n + 3*chr(0))[:3])[::-1]
47 bits = pack.IntType(32).unpack(bits2)
50 def __init__(self, bits, target=None):
53 if target is not None and self.target != target:
54 raise ValueError('target does not match')
60 res = self._target = math.shift_left(self.bits & 0x00ffffff, 8 * ((self.bits >> 24) - 3))
64 return hash(self.bits)
66 def __eq__(self, other):
67 return self.bits == other.bits
69 def __ne__(self, other):
70 return not (self == other)
72 def __cmp__(self, other):
76 return 'FloatingInteger(bits=%s, target=%s)' % (hex(self.bits), hex(self.target))
78 class FloatingIntegerType(pack.Type):
79 _inner = pack.IntType(32)
82 bits, file = self._inner.read(file)
83 return FloatingInteger(bits), file
85 def write(self, file, item):
86 return self._inner.write(file, item.bits)
88 address_type = pack.ComposedType([
89 ('services', pack.IntType(64)),
90 ('address', pack.IPV6AddressType()),
91 ('port', pack.IntType(16, 'big')),
94 tx_type = pack.ComposedType([
95 ('version', pack.IntType(32)),
96 ('tx_ins', pack.ListType(pack.ComposedType([
97 ('previous_output', pack.PossiblyNoneType(dict(hash=0, index=2**32 - 1), pack.ComposedType([
98 ('hash', pack.IntType(256)),
99 ('index', pack.IntType(32)),
101 ('script', pack.VarStrType()),
102 ('sequence', pack.PossiblyNoneType(2**32 - 1, pack.IntType(32))),
104 ('tx_outs', pack.ListType(pack.ComposedType([
105 ('value', pack.IntType(64)),
106 ('script', pack.VarStrType()),
108 ('lock_time', pack.IntType(32)),
111 merkle_link_type = pack.ComposedType([
112 ('branch', pack.ListType(pack.IntType(256))),
113 ('index', pack.IntType(32)),
116 merkle_tx_type = pack.ComposedType([
118 ('block_hash', pack.IntType(256)),
119 ('merkle_link', merkle_link_type),
122 block_header_type = pack.ComposedType([
123 ('version', pack.IntType(32)),
124 ('previous_block', pack.PossiblyNoneType(0, pack.IntType(256))),
125 ('merkle_root', pack.IntType(256)),
126 ('timestamp', pack.IntType(32)),
127 ('bits', FloatingIntegerType()),
128 ('nonce', pack.IntType(32)),
131 block_type = pack.ComposedType([
132 ('header', block_header_type),
133 ('txs', pack.ListType(tx_type)),
138 aux_pow_type = pack.ComposedType([
139 ('merkle_tx', merkle_tx_type),
140 ('merkle_link', merkle_link_type),
141 ('parent_block_header', block_header_type),
144 aux_pow_coinbase_type = pack.ComposedType([
145 ('merkle_root', pack.IntType(256, 'big')),
146 ('size', pack.IntType(32)),
147 ('nonce', pack.IntType(32)),
150 def make_auxpow_tree(chain_ids):
151 for size in (2**i for i in xrange(31)):
152 if size < len(chain_ids):
155 for chain_id in chain_ids:
156 pos = (1103515245 * chain_id + 1103515245 * 12345 + 12345) % size
162 raise AssertionError()
166 merkle_record_type = pack.ComposedType([
167 ('left', pack.IntType(256)),
168 ('right', pack.IntType(256)),
171 def merkle_hash(hashes):
174 hash_list = list(hashes)
175 while len(hash_list) > 1:
176 hash_list = [hash256(merkle_record_type.pack(dict(left=left, right=right)))
177 for left, right in zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])]
180 def calculate_merkle_link(hashes, index):
183 hash_list = [(lambda _h=h: _h, i == index, []) for i, h in enumerate(hashes)]
185 while len(hash_list) > 1:
188 lambda _left=left, _right=right: hash256(merkle_record_type.pack(dict(left=_left(), right=_right()))),
190 (left_l if left_f else right_l) + [dict(side=1, hash=right) if left_f else dict(side=0, hash=left)],
192 for (left, left_f, left_l), (right, right_f, right_l) in
193 zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])
196 res = [x['hash']() for x in hash_list[0][2]]
198 assert hash_list[0][1]
200 new_hashes = [random.randrange(2**256) if x is None else x
202 assert check_merkle_link(new_hashes[index], dict(branch=res, index=index)) == merkle_hash(new_hashes)
203 assert index == sum(k*2**i for i, k in enumerate([1-x['side'] for x in hash_list[0][2]]))
205 return dict(branch=res, index=index)
207 def check_merkle_link(tip_hash, link):
208 if link['index'] >= 2**len(link['branch']):
209 raise ValueError('index too large')
210 return reduce(lambda c, (i, h): hash256(merkle_record_type.pack(
211 dict(left=h, right=c) if (link['index'] >> i) & 1 else
212 dict(left=c, right=h)
213 )), enumerate(link['branch']), tip_hash)
217 def target_to_average_attempts(target):
218 assert 0 <= target and isinstance(target, (int, long)), target
219 if target >= 2**256: warnings.warn('target >= 2**256!')
220 return 2**256//(target + 1)
222 def target_to_difficulty(target):
223 assert 0 <= target and isinstance(target, (int, long)), target
224 if target >= 2**256: warnings.warn('target >= 2**256!')
225 return (0xffff0000 * 2**(256-64) + 1)/(target + 1)
227 def difficulty_to_target(difficulty):
228 assert difficulty >= 0
229 return min(int((0xffff0000 * 2**(256-64) + 1)/difficulty - 1 + 0.5), 2**256-1)
233 base58_alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
235 def base58_encode(bindata):
236 bindata2 = bindata.lstrip(chr(0))
237 return base58_alphabet[0]*(len(bindata) - len(bindata2)) + math.natural_to_string(math.string_to_natural(bindata2), base58_alphabet)
239 def base58_decode(b58data):
240 b58data2 = b58data.lstrip(base58_alphabet[0])
241 return chr(0)*(len(b58data) - len(b58data2)) + math.natural_to_string(math.string_to_natural(b58data2, base58_alphabet))
243 human_address_type = ChecksummedType(pack.ComposedType([
244 ('version', pack.IntType(8)),
245 ('pubkey_hash', pack.IntType(160)),
248 def pubkey_hash_to_address(pubkey_hash, net):
249 return base58_encode(human_address_type.pack(dict(version=net.ADDRESS_VERSION, pubkey_hash=pubkey_hash)))
251 def pubkey_to_address(pubkey, net):
252 return pubkey_hash_to_address(hash160(pubkey), net)
254 def address_to_pubkey_hash(address, net):
255 x = human_address_type.unpack(base58_decode(address))
256 if x['version'] != net.ADDRESS_VERSION:
257 raise ValueError('address not for this net!')
258 return x['pubkey_hash']
262 def pubkey_to_script2(pubkey):
263 assert len(pubkey) <= 75
264 return (chr(len(pubkey)) + pubkey) + '\xac'
266 def pubkey_hash_to_script2(pubkey_hash):
267 return '\x76\xa9' + ('\x14' + pack.IntType(160).pack(pubkey_hash)) + '\x88\xac'
269 def script2_to_address(script2, net):
271 pubkey = script2[1:-1]
272 script2_test = pubkey_to_script2(pubkey)
276 if script2_test == script2:
277 return pubkey_to_address(pubkey, net)
280 pubkey_hash = pack.IntType(160).unpack(script2[3:-2])
281 script2_test2 = pubkey_hash_to_script2(pubkey_hash)
285 if script2_test2 == script2:
286 return pubkey_hash_to_address(pubkey_hash, net)
288 def script2_to_human(script2, net):
290 pubkey = script2[1:-1]
291 script2_test = pubkey_to_script2(pubkey)
295 if script2_test == script2:
296 return 'Pubkey. Address: %s' % (pubkey_to_address(pubkey, net),)
299 pubkey_hash = pack.IntType(160).unpack(script2[3:-2])
300 script2_test2 = pubkey_hash_to_script2(pubkey_hash)
304 if script2_test2 == script2:
305 return 'Address. Address: %s' % (pubkey_hash_to_address(pubkey_hash, net),)
307 return 'Unknown. Script: %s' % (script2.encode('hex'),)