1 '''Various helper methods. It probably needs some cleanup.'''
6 from hashlib import sha256
7 from ltc_scrypt import getPoWHash
10 nit = struct.unpack("<B", f.read(1))[0]
12 nit = struct.unpack("<H", f.read(2))[0]
14 nit = struct.unpack("<I", f.read(4))[0]
16 nit = struct.unpack("<Q", f.read(8))[0]
21 return chr(len(s)) + s
22 elif len(s) < 0x10000:
23 return chr(253) + struct.pack("<H", len(s)) + s
24 elif len(s) < 0x100000000L:
25 return chr(254) + struct.pack("<I", len(s)) + s
26 return chr(255) + struct.pack("<Q", len(s)) + s
31 t = struct.unpack("<I", f.read(4))[0]
38 rs += struct.pack("<I", u & 0xFFFFFFFFL)
42 def uint256_from_str(s):
44 t = struct.unpack("<IIIIIIII", s[:32])
49 def uint256_from_str_be(s):
51 t = struct.unpack(">IIIIIIII", s[:32])
56 def uint256_from_compact(c):
57 nbytes = (c >> 24) & 0xFF
58 v = (c & 0xFFFFFFL) << (8 * (nbytes - 3))
61 def deser_vector(f, c):
62 nit = struct.unpack("<B", f.read(1))[0]
64 nit = struct.unpack("<H", f.read(2))[0]
66 nit = struct.unpack("<I", f.read(4))[0]
68 nit = struct.unpack("<Q", f.read(8))[0]
80 elif len(l) < 0x10000:
81 r = chr(253) + struct.pack("<H", len(l))
82 elif len(l) < 0x100000000L:
83 r = chr(254) + struct.pack("<I", len(l))
85 r = chr(255) + struct.pack("<Q", len(l))
90 def deser_uint256_vector(f):
91 nit = struct.unpack("<B", f.read(1))[0]
93 nit = struct.unpack("<H", f.read(2))[0]
95 nit = struct.unpack("<I", f.read(4))[0]
97 nit = struct.unpack("<Q", f.read(8))[0]
104 def ser_uint256_vector(l):
108 elif len(l) < 0x10000:
109 r = chr(253) + struct.pack("<H", len(l))
110 elif len(l) < 0x100000000L:
111 r = chr(254) + struct.pack("<I", len(l))
113 r = chr(255) + struct.pack("<Q", len(l))
118 __b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
119 __b58base = len(__b58chars)
121 def b58decode(v, length):
122 """ decode v into a string of len bytes
125 for (i, c) in enumerate(v[::-1]):
126 long_value += __b58chars.find(c) * (__b58base**i)
129 while long_value >= 256:
130 div, mod = divmod(long_value, 256)
131 result = chr(mod) + result
133 result = chr(long_value) + result
137 if c == __b58chars[0]: nPad += 1
140 result = chr(0)*nPad + result
141 if length is not None and len(result) != length:
146 def b58encode(value):
147 """ encode integer 'value' as a base58 string; returns string
150 while value >= __b58base:
151 div, mod = divmod(value, __b58base)
152 encoded = __b58chars[mod] + encoded # add to left
154 encoded = __b58chars[value] + encoded # most significant remainder
158 # This only revert byte order, nothing more
160 raise Exception('hash must have 64 hexa chars')
162 return ''.join([ h[56-i:64-i] for i in range(0, 64, 8) ])
165 return sha256(sha256(b).digest()).digest()
170 def bits_to_target(bits):
171 return struct.unpack('<L', bits[:3] + b'\0')[0] * 2**(8*(int(bits[3], 16) - 3))
173 def address_to_pubkeyhash(addr):
175 addr = b58decode(addr, 25)
184 cksumB = doublesha(addr[:-4])[:4]
189 return (ver, addr[1:-4])
191 def ser_uint256_be(u):
192 '''ser_uint256 to big endian'''
195 rs += struct.pack(">I", u & 0xFFFFFFFFL)
199 def deser_uint256_be(f):
202 t = struct.unpack(">I", f.read(4))[0]
207 # For encoding nHeight into coinbase
216 def script_to_address(addr):
217 d = address_to_pubkeyhash(addr)
219 raise ValueError('invalid address')
220 (ver, pubkeyhash) = d
221 return b'\x76\xa9\x14' + pubkeyhash + b'\x88\xac'