URI: 
       tnanotube's patch for compressed keys - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit e631e9c89c646746969bac9ef4851d8c11b575eb
   DIR parent 38f93cb593a31e27401173ca87d55c3a9371eb51
  HTML Author: thomasv <thomasv@gitorious>
       Date:   Tue, 12 Jun 2012 10:47:00 +0200
       
       nanotube's patch for compressed keys
       
       Diffstat:
         M lib/wallet.py                       |      29 +++++++++++++++++++++++------
       
       1 file changed, 23 insertions(+), 6 deletions(-)
       ---
   DIR diff --git a/lib/wallet.py b/lib/wallet.py
       t@@ -50,6 +50,16 @@ def bc_address_to_hash_160(addr):
            bytes = b58decode(addr, 25)
            return bytes[1:21]
        
       +def encode_point(pubkey, compressed=False):
       +    order = generator_secp256k1.order()
       +    p = pubkey.pubkey.point
       +    x_str = ecdsa.util.number_to_string(p.x(), order)
       +    y_str = ecdsa.util.number_to_string(p.y(), order)
       +    if compressed:
       +        return chr(2 + (p.y() & 1)) + x_str
       +    else:
       +        return chr(4) + pubkey.to_string() #x_str + y_str
       +
        __b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
        __b58base = len(__b58chars)
        
       t@@ -435,8 +445,8 @@ class Wallet:
                        continue
                else:
                    raise BaseException("error: cannot sign message")
       -        
       -            
       +
       +
            def verify_message(self, address, signature, message):
                """ See http://www.secg.org/download/aid-780/sec1-v2.pdf for the math """
                from ecdsa import numbertheory, ellipticcurve, util
       t@@ -448,7 +458,16 @@ class Wallet:
                sig = base64.b64decode(signature)
                if len(sig) != 65: raise BaseException("Wrong encoding")
                r,s = util.sigdecode_string(sig[1:], order)
       -        recid = ord(sig[0]) - 27
       +        nV = ord(sig[0])
       +        if nV < 27 or nV >= 35:
       +            raise BaseException("Bad encoding")
       +        if nV >= 31:
       +            compressed = True
       +            nV -= 4
       +        else:
       +            compressed = False
       +
       +        recid = nV - 27
                # 1.1
                x = r + (recid/2) * order
                # 1.3
       t@@ -468,10 +487,8 @@ class Wallet:
                # check that Q is the public key
                public_key.verify_digest( sig[1:], h, sigdecode = ecdsa.util.sigdecode_string)
                # check that we get the original signing address
       -        addr = public_key_to_bc_address( '04'.decode('hex') + public_key.to_string() )
       -        # print addr
       +        addr = public_key_to_bc_address( encode_point(public_key, compressed) )
                if address != addr:
       -            print "bad signature"
                    raise BaseException("Bad signature")