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 return pack.IntType(160).unpack(hashlib.new('ripemd160', hashlib.sha256(data).digest()).digest())
17 return pack.IntType(256).unpack(__import__('ltc_scrypt').getPoWHash(data))
19 class ChecksummedType(pack.Type):
20 def __init__(self, inner, checksum_func=lambda data: hashlib.sha256(hashlib.sha256(data).digest()).digest()[:4]):
22 self.checksum_func = checksum_func
25 obj, file = self.inner.read(file)
26 data = self.inner.pack(obj)
28 calculated_checksum = self.checksum_func(data)
29 checksum, file = pack.read(file, len(calculated_checksum))
30 if checksum != calculated_checksum:
31 raise ValueError('invalid checksum')
35 def write(self, file, item):
36 data = self.inner.pack(item)
37 return (file, data), self.checksum_func(data)
39 class FloatingInteger(object):
40 __slots__ = ['bits', '_target']
43 def from_target_upper_bound(cls, target):
44 n = math.natural_to_string(target)
45 if n and ord(n[0]) >= 128:
47 bits2 = (chr(len(n)) + (n + 3*chr(0))[:3])[::-1]
48 bits = pack.IntType(32).unpack(bits2)
51 def __init__(self, bits, target=None):
54 if target is not None and self.target != target:
55 raise ValueError('target does not match')
61 res = self._target = math.shift_left(self.bits & 0x00ffffff, 8 * ((self.bits >> 24) - 3))
65 return hash(self.bits)
67 def __eq__(self, other):
68 return self.bits == other.bits
70 def __ne__(self, other):
71 return not (self == other)
73 def __cmp__(self, other):
77 return 'FloatingInteger(bits=%s, target=%s)' % (hex(self.bits), hex(self.target))
79 class FloatingIntegerType(pack.Type):
80 _inner = pack.IntType(32)
83 bits, file = self._inner.read(file)
84 return FloatingInteger(bits), file
86 def write(self, file, item):
87 return self._inner.write(file, item.bits)
89 address_type = pack.ComposedType([
90 ('services', pack.IntType(64)),
91 ('address', pack.IPV6AddressType()),
92 ('port', pack.IntType(16, 'big')),
95 tx_type = pack.ComposedType([
96 ('version', pack.IntType(32)),
97 ('timestamp', pack.IntType(32)), # txn timestamp
98 ('tx_ins', pack.ListType(pack.ComposedType([
99 ('previous_output', pack.PossiblyNoneType(dict(hash=0, index=2**32 - 1), pack.ComposedType([
100 ('hash', pack.IntType(256)),
101 ('index', pack.IntType(32)),
103 ('script', pack.VarStrType()),
104 ('sequence', pack.PossiblyNoneType(2**32 - 1, pack.IntType(32))),
106 ('tx_outs', pack.ListType(pack.ComposedType([
107 ('value', pack.IntType(64)),
108 ('script', pack.VarStrType()),
110 ('lock_time', pack.IntType(32)),
113 merkle_link_type = pack.ComposedType([
114 ('branch', pack.ListType(pack.IntType(256))),
115 ('index', pack.IntType(32)),
118 merkle_tx_type = pack.ComposedType([
120 ('block_hash', pack.IntType(256)),
121 ('merkle_link', merkle_link_type),
124 block_header_type = pack.ComposedType([
125 ('version', pack.IntType(32)),
126 ('previous_block', pack.PossiblyNoneType(0, pack.IntType(256))),
127 ('merkle_root', pack.IntType(256)),
128 ('timestamp', pack.IntType(32)),
129 ('bits', FloatingIntegerType()),
130 ('nonce', pack.IntType(32)),
133 block_type = pack.ComposedType([
134 ('header', block_header_type),
135 ('txs', pack.ListType(tx_type)),
136 ('signature', pack.VarStrType()), # header signature field
141 aux_pow_type = pack.ComposedType([
142 ('merkle_tx', merkle_tx_type),
143 ('merkle_link', merkle_link_type),
144 ('parent_block_header', block_header_type),
147 aux_pow_coinbase_type = pack.ComposedType([
148 ('merkle_root', pack.IntType(256, 'big')),
149 ('size', pack.IntType(32)),
150 ('nonce', pack.IntType(32)),
153 def make_auxpow_tree(chain_ids):
154 for size in (2**i for i in xrange(31)):
155 if size < len(chain_ids):
158 for chain_id in chain_ids:
159 pos = (1103515245 * chain_id + 1103515245 * 12345 + 12345) % size
165 raise AssertionError()
169 merkle_record_type = pack.ComposedType([
170 ('left', pack.IntType(256)),
171 ('right', pack.IntType(256)),
174 def merkle_hash(hashes):
177 hash_list = list(hashes)
178 while len(hash_list) > 1:
179 hash_list = [hash256(merkle_record_type.pack(dict(left=left, right=right)))
180 for left, right in zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])]
183 def calculate_merkle_link(hashes, index):
186 hash_list = [(lambda _h=h: _h, i == index, []) for i, h in enumerate(hashes)]
188 while len(hash_list) > 1:
191 lambda _left=left, _right=right: hash256(merkle_record_type.pack(dict(left=_left(), right=_right()))),
193 (left_l if left_f else right_l) + [dict(side=1, hash=right) if left_f else dict(side=0, hash=left)],
195 for (left, left_f, left_l), (right, right_f, right_l) in
196 zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])
199 res = [x['hash']() for x in hash_list[0][2]]
201 assert hash_list[0][1]
203 new_hashes = [random.randrange(2**256) if x is None else x
205 assert check_merkle_link(new_hashes[index], dict(branch=res, index=index)) == merkle_hash(new_hashes)
206 assert index == sum(k*2**i for i, k in enumerate([1-x['side'] for x in hash_list[0][2]]))
208 return dict(branch=res, index=index)
210 def check_merkle_link(tip_hash, link):
211 if link['index'] >= 2**len(link['branch']):
212 raise ValueError('index too large')
213 return reduce(lambda c, (i, h): hash256(merkle_record_type.pack(
214 dict(left=h, right=c) if (link['index'] >> i) & 1 else
215 dict(left=c, right=h)
216 )), enumerate(link['branch']), tip_hash)
220 def target_to_average_attempts(target):
221 assert 0 <= target and isinstance(target, (int, long)), target
222 if target >= 2**256: warnings.warn('target >= 2**256!')
223 return 2**256//(target + 1)
225 def average_attempts_to_target(average_attempts):
226 assert average_attempts > 0
227 return min(int(2**256/average_attempts - 1 + 0.5), 2**256-1)
229 def target_to_difficulty(target):
230 assert 0 <= target and isinstance(target, (int, long)), target
231 if target >= 2**256: warnings.warn('target >= 2**256!')
232 return (0xffff0000 * 2**(256-64) + 1)/(target + 1)
234 def difficulty_to_target(difficulty):
235 assert difficulty >= 0
236 if difficulty == 0: return 2**256-1
237 return min(int((0xffff0000 * 2**(256-64) + 1)/difficulty - 1 + 0.5), 2**256-1)
241 base58_alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
243 def base58_encode(bindata):
244 bindata2 = bindata.lstrip(chr(0))
245 return base58_alphabet[0]*(len(bindata) - len(bindata2)) + math.natural_to_string(math.string_to_natural(bindata2), base58_alphabet)
247 def base58_decode(b58data):
248 b58data2 = b58data.lstrip(base58_alphabet[0])
249 return chr(0)*(len(b58data) - len(b58data2)) + math.natural_to_string(math.string_to_natural(b58data2, base58_alphabet))
251 human_address_type = ChecksummedType(pack.ComposedType([
252 ('version', pack.IntType(8)),
253 ('pubkey_hash', pack.IntType(160)),
256 def pubkey_hash_to_address(pubkey_hash, net):
257 return base58_encode(human_address_type.pack(dict(version=net.ADDRESS_VERSION, pubkey_hash=pubkey_hash)))
259 def pubkey_to_address(pubkey, net):
260 return pubkey_hash_to_address(hash160(pubkey), net)
262 def address_to_pubkey_hash(address, net):
263 x = human_address_type.unpack(base58_decode(address))
264 if x['version'] != net.ADDRESS_VERSION:
265 raise ValueError('address not for this net!')
266 return x['pubkey_hash']
268 def address_to_script(address, net):
269 x = human_address_type.unpack(base58_decode(address))
271 print x['pubkey_hash']
273 if x['version'] != net.ADDRESS_VERSION:
274 raise ValueError('address not for this net!')
275 return '\x76\xa9' + ('\x14' + pack.IntType(160).pack(x['pubkey_hash'])) + '\x88\xac'
280 def pubkey_to_script2(pubkey):
281 assert len(pubkey) <= 75
282 return (chr(len(pubkey)) + pubkey) + '\xac'
284 def pubkey_hash_to_script2(pubkey_hash):
285 return '\x76\xa9' + ('\x14' + pack.IntType(160).pack(pubkey_hash)) + '\x88\xac'
287 def script2_to_address(script2, net):
289 pubkey = script2[1:-1]
290 script2_test = pubkey_to_script2(pubkey)
294 if script2_test == script2:
295 return pubkey_to_address(pubkey, net)
298 pubkey_hash = pack.IntType(160).unpack(script2[3:-2])
299 script2_test2 = pubkey_hash_to_script2(pubkey_hash)
303 if script2_test2 == script2:
304 return pubkey_hash_to_address(pubkey_hash, net)
306 def script2_to_human(script2, net):
308 pubkey = script2[1:-1]
309 script2_test = pubkey_to_script2(pubkey)
313 if script2_test == script2:
314 return 'Pubkey. Address: %s' % (pubkey_to_address(pubkey, net),)
317 pubkey_hash = pack.IntType(160).unpack(script2[3:-2])
318 script2_test2 = pubkey_hash_to_script2(pubkey_hash)
322 if script2_test2 == script2:
323 return 'Address. Address: %s' % (pubkey_hash_to_address(pubkey_hash, net),)
325 return 'Unknown. Script: %s' % (script2.encode('hex'),)