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 average_attempts_to_target(average_attempts):
223 assert average_attempts > 0
224 return min(int(2**256/average_attempts - 1 + 0.5), 2**256-1)
226 def target_to_difficulty(target):
227 assert 0 <= target and isinstance(target, (int, long)), target
228 if target >= 2**256: warnings.warn('target >= 2**256!')
229 return (0xffff0000 * 2**(256-64) + 1)/(target + 1)
231 def difficulty_to_target(difficulty):
232 assert difficulty >= 0
233 if difficulty == 0: return 2**256-1
234 return min(int((0xffff0000 * 2**(256-64) + 1)/difficulty - 1 + 0.5), 2**256-1)
238 base58_alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
240 def base58_encode(bindata):
241 bindata2 = bindata.lstrip(chr(0))
242 return base58_alphabet[0]*(len(bindata) - len(bindata2)) + math.natural_to_string(math.string_to_natural(bindata2), base58_alphabet)
244 def base58_decode(b58data):
245 b58data2 = b58data.lstrip(base58_alphabet[0])
246 return chr(0)*(len(b58data) - len(b58data2)) + math.natural_to_string(math.string_to_natural(b58data2, base58_alphabet))
248 human_address_type = ChecksummedType(pack.ComposedType([
249 ('version', pack.IntType(8)),
250 ('pubkey_hash', pack.IntType(160)),
253 def pubkey_hash_to_address(pubkey_hash, net):
254 return base58_encode(human_address_type.pack(dict(version=net.ADDRESS_VERSION, pubkey_hash=pubkey_hash)))
256 def pubkey_to_address(pubkey, net):
257 return pubkey_hash_to_address(hash160(pubkey), net)
259 def address_to_pubkey_hash(address, net):
260 x = human_address_type.unpack(base58_decode(address))
261 if x['version'] != net.ADDRESS_VERSION:
262 raise ValueError('address not for this net!')
263 return x['pubkey_hash']
267 def pubkey_to_script2(pubkey):
268 assert len(pubkey) <= 75
269 return (chr(len(pubkey)) + pubkey) + '\xac'
271 def pubkey_hash_to_script2(pubkey_hash):
272 return '\x76\xa9' + ('\x14' + pack.IntType(160).pack(pubkey_hash)) + '\x88\xac'
274 def script2_to_address(script2, net):
276 pubkey = script2[1:-1]
277 script2_test = pubkey_to_script2(pubkey)
281 if script2_test == script2:
282 return pubkey_to_address(pubkey, net)
285 pubkey_hash = pack.IntType(160).unpack(script2[3:-2])
286 script2_test2 = pubkey_hash_to_script2(pubkey_hash)
290 if script2_test2 == script2:
291 return pubkey_hash_to_address(pubkey_hash, net)
293 def script2_to_human(script2, net):
295 pubkey = script2[1:-1]
296 script2_test = pubkey_to_script2(pubkey)
300 if script2_test == script2:
301 return 'Pubkey. Address: %s' % (pubkey_to_address(pubkey, net),)
304 pubkey_hash = pack.IntType(160).unpack(script2[3:-2])
305 script2_test2 = pubkey_hash_to_script2(pubkey_hash)
309 if script2_test2 == script2:
310 return 'Address. Address: %s' % (pubkey_hash_to_address(pubkey_hash, net),)
312 return 'Unknown. Script: %s' % (script2.encode('hex'),)