Workaround for bug in Python's math.log function
[bitcoin:eloipool.git] / bitcoin / script.py
1 # Eloipool - Python Bitcoin pool server
2 # Copyright (C) 2011-2012  Luke Dashjr <luke-jr+eloipool@utopios.org>
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as
6 # published by the Free Software Foundation, either version 3 of the
7 # License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 from base58 import b58decode
18 from util import dblsha
19
20 def _Address2PKH(addr):
21         try:
22                 addr = b58decode(addr, 25)
23         except:
24                 return None
25         if addr is None:
26                 return None
27         ver = addr[0]
28         cksumA = addr[-4:]
29         cksumB = dblsha(addr[:-4])[:4]
30         if cksumA != cksumB:
31                 return None
32         return (ver, addr[1:-4])
33
34 class BitcoinScript:
35         @classmethod
36         def toAddress(cls, addr):
37                 d = _Address2PKH(addr)
38                 if not d:
39                         raise ValueError('invalid address')
40                 (ver, pubkeyhash) = d
41                 return b'\x76\xa9\x14' + pubkeyhash + b'\x88\xac'
42
43 def countSigOps(s):
44         # FIXME: don't count data as ops
45         c = 0
46         for ch in s:
47                 if 0xac == ch & 0xfe:
48                         c += 1
49                 elif 0xae == ch & 0xfe:
50                         c += 20
51         return c
52
53 # NOTE: This does not work for signed numbers (set the high bit) or zero (use b'\0')
54 def encodeUNum(n):
55         s = bytearray(b'\1')
56         while n > 127:
57                 s[0] += 1
58                 s.append(n % 256)
59                 n //= 256
60         s.append(n)
61         return bytes(s)
62
63 def encodeNum(n):
64         if n == 0:
65                 return b'\0'
66         if n > 0:
67                 return encodeUNum(n)
68         s = encodeUNum(abs(n))
69         s = bytearray(s)
70         s[-1] = s[-1] | 0x80
71         return bytes(s)
72
73 # tests
74 def _test():
75         assert b'\0' == encodeNum(0)
76         assert b'\1\x55' == encodeNum(0x55)
77         assert b'\2\xfd\0' == encodeNum(0xfd)
78         assert b'\3\xff\xff\0' == encodeNum(0xffff)
79         assert b'\3\0\0\x01' == encodeNum(0x10000)
80         assert b'\5\xff\xff\xff\xff\0' == encodeNum(0xffffffff)
81
82 _test()