1 '''Various helper methods. It probably needs some cleanup.'''
6 from hashlib import sha256
9 nit = struct.unpack("<B", f.read(1))[0]
11 nit = struct.unpack("<H", f.read(2))[0]
13 nit = struct.unpack("<I", f.read(4))[0]
15 nit = struct.unpack("<Q", f.read(8))[0]
20 return chr(len(s)) + s
21 elif len(s) < 0x10000:
22 return chr(253) + struct.pack("<H", len(s)) + s
23 elif len(s) < 0x100000000L:
24 return chr(254) + struct.pack("<I", len(s)) + s
25 return chr(255) + struct.pack("<Q", len(s)) + s
30 t = struct.unpack("<I", f.read(4))[0]
37 rs += struct.pack("<I", u & 0xFFFFFFFFL)
41 def uint256_from_str(s):
43 t = struct.unpack("<IIIIIIII", s[:32])
48 def uint256_from_str_be(s):
50 t = struct.unpack(">IIIIIIII", s[:32])
55 def uint256_from_compact(c):
56 nbytes = (c >> 24) & 0xFF
57 v = (c & 0xFFFFFFL) << (8 * (nbytes - 3))
60 def deser_vector(f, c):
61 nit = struct.unpack("<B", f.read(1))[0]
63 nit = struct.unpack("<H", f.read(2))[0]
65 nit = struct.unpack("<I", f.read(4))[0]
67 nit = struct.unpack("<Q", f.read(8))[0]
79 elif len(l) < 0x10000:
80 r = chr(253) + struct.pack("<H", len(l))
81 elif len(l) < 0x100000000L:
82 r = chr(254) + struct.pack("<I", len(l))
84 r = chr(255) + struct.pack("<Q", len(l))
89 def deser_uint256_vector(f):
90 nit = struct.unpack("<B", f.read(1))[0]
92 nit = struct.unpack("<H", f.read(2))[0]
94 nit = struct.unpack("<I", f.read(4))[0]
96 nit = struct.unpack("<Q", f.read(8))[0]
103 def ser_uint256_vector(l):
107 elif len(l) < 0x10000:
108 r = chr(253) + struct.pack("<H", len(l))
109 elif len(l) < 0x100000000L:
110 r = chr(254) + struct.pack("<I", len(l))
112 r = chr(255) + struct.pack("<Q", len(l))
117 __b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
118 __b58base = len(__b58chars)
120 def b58decode(v, length):
121 """ decode v into a string of len bytes
124 for (i, c) in enumerate(v[::-1]):
125 long_value += __b58chars.find(c) * (__b58base**i)
128 while long_value >= 256:
129 div, mod = divmod(long_value, 256)
130 result = chr(mod) + result
132 result = chr(long_value) + result
136 if c == __b58chars[0]: nPad += 1
139 result = chr(0)*nPad + result
140 if length is not None and len(result) != length:
146 # This only revert byte order, nothing more
148 raise Exception('hash must have 64 hexa chars')
150 return ''.join([ h[56-i:64-i] for i in range(0, 64, 8) ])
153 return sha256(sha256(b).digest()).digest()
155 def bits_to_target(bits):
156 return struct.unpack('<L', bits[:3] + b'\0')[0] * 2**(8*(int(bits[3], 16) - 3))
158 def address_to_pubkeyhash(addr):
160 addr = b58decode(addr, 25)
169 cksumB = doublesha(addr[:-4])[:4]
174 return (ver, addr[1:-4])
176 def ser_uint256_be(u):
177 '''ser_uint256 to big endian'''
180 rs += struct.pack(">I", u & 0xFFFFFFFFL)
184 def deser_uint256_be(f):
187 t = struct.unpack(">I", f.read(4))[0]
192 # For encoding nHeight into coinbase
193 d = struct.pack("<I", num).rstrip("\x00")
194 return chr(len(d)) + d
196 def script_to_address(addr):
197 d = address_to_pubkeyhash(addr)
199 raise ValueError('invalid address')
200 (ver, pubkeyhash) = d
201 return b'\x76\xa9\x14' + pubkeyhash + b'\x88\xac'