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:
147 # This only revert byte order, nothing more
149 raise Exception('hash must have 64 hexa chars')
151 return ''.join([ h[56-i:64-i] for i in range(0, 64, 8) ])
154 return sha256(sha256(b).digest()).digest()
159 def bits_to_target(bits):
160 return struct.unpack('<L', bits[:3] + b'\0')[0] * 2**(8*(int(bits[3], 16) - 3))
162 def address_to_pubkeyhash(addr):
164 addr = b58decode(addr, 25)
173 cksumB = doublesha(addr[:-4])[:4]
178 return (ver, addr[1:-4])
180 def ser_uint256_be(u):
181 '''ser_uint256 to big endian'''
184 rs += struct.pack(">I", u & 0xFFFFFFFFL)
188 def deser_uint256_be(f):
191 t = struct.unpack(">I", f.read(4))[0]
196 # For encoding nHeight into coinbase
205 def script_to_address(addr):
206 d = address_to_pubkeyhash(addr)
208 raise ValueError('invalid address')
209 (ver, pubkeyhash) = d
210 return b'\x76\xa9\x14' + pubkeyhash + b'\x88\xac'