URI: 
       tbasic functions and tests for multisig transactions and bip 32 - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit e1504ba80b108a4b4eabb04a43fe77f0eb5d2408
   DIR parent 515b3412b034ed670cfb257b64ea95fa4b2ddbe9
  HTML Author: thomasv <thomasv@gitorious>
       Date:   Wed, 30 Jan 2013 20:13:31 +0100
       
       basic functions and tests for multisig transactions and bip 32
       
       Diffstat:
         M lib/bitcoin.py                      |     264 ++++++++++++++++++++++++++-----
         M lib/wallet.py                       |      20 ++++++++++----------
       
       2 files changed, 234 insertions(+), 50 deletions(-)
       ---
   DIR diff --git a/lib/bitcoin.py b/lib/bitcoin.py
       t@@ -29,6 +29,7 @@ def int_to_hex(i, length=1):
            return rev_hex(s)
        
        def var_int(i):
       +    # https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integer
            if i<0xfd:
                return int_to_hex(i)
            elif i<=0xffff:
       t@@ -69,22 +70,22 @@ def i2d_ECPrivateKey(pkey, compressed=False):
                      '%064x' % _r + \
                      '020101a144034200'
                
       -    return key.decode('hex') + i2o_ECPublicKey(pkey, compressed)
       +    return key.decode('hex') + i2o_ECPublicKey(pkey.pubkey, compressed)
            
       -def i2o_ECPublicKey(pkey, compressed=False):
       +def i2o_ECPublicKey(pubkey, compressed=False):
            # public keys are 65 bytes long (520 bits)
            # 0x04 + 32-byte X-coordinate + 32-byte Y-coordinate
            # 0x00 = point at infinity, 0x02 and 0x03 = compressed, 0x04 = uncompressed
            # compressed keys: <sign> <x> where <sign> is 0x02 if y is even and 0x03 if y is odd
            if compressed:
       -        if pkey.pubkey.point.y() & 1:
       -            key = '03' + '%064x' % pkey.pubkey.point.x()
       +        if pubkey.point.y() & 1:
       +            key = '03' + '%064x' % pubkey.point.x()
                else:
       -            key = '02' + '%064x' % pkey.pubkey.point.x()
       +            key = '02' + '%064x' % pubkey.point.x()
            else:
                key = '04' + \
       -              '%064x' % pkey.pubkey.point.x() + \
       -              '%064x' % pkey.pubkey.point.y()
       +              '%064x' % pubkey.point.x() + \
       +              '%064x' % pubkey.point.y()
                    
            return key.decode('hex')
                    
       t@@ -93,8 +94,6 @@ def i2o_ECPublicKey(pkey, compressed=False):
                                                        
                    
        ############ functions from pywallet ##################### 
       -            
       -addrtype = 0
        
        def hash_160(public_key):
            try:
       t@@ -111,7 +110,7 @@ def public_key_to_bc_address(public_key):
            h160 = hash_160(public_key)
            return hash_160_to_bc_address(h160)
        
       -def hash_160_to_bc_address(h160):
       +def hash_160_to_bc_address(h160, addrtype = 0):
            vh160 = chr(addrtype) + h160
            h = Hash(vh160)
            addr = vh160 + h[0:4]
       t@@ -119,7 +118,7 @@ def hash_160_to_bc_address(h160):
        
        def bc_address_to_hash_160(addr):
            bytes = b58decode(addr, 25)
       -    return bytes[1:21]
       +    return ord(bytes[0]), bytes[1:21]
        
        def encode_point(pubkey, compressed=False):
            order = generator_secp256k1.order()
       t@@ -200,12 +199,12 @@ def DecodeBase58Check(psz):
        def PrivKeyToSecret(privkey):
            return privkey[9:9+32]
        
       -def SecretToASecret(secret, compressed=False):
       +def SecretToASecret(secret, compressed=False, addrtype=0):
            vchIn = chr((addrtype+128)&255) + secret
            if compressed: vchIn += '\01'
            return EncodeBase58Check(vchIn)
        
       -def ASecretToSecret(key):
       +def ASecretToSecret(key, addrtype=0):
            vch = DecodeBase58Check(key)
            if vch and vch[0] == chr((addrtype+128)&255):
                return vch[1:]
       t@@ -220,8 +219,8 @@ def regenerate_key(sec):
            secret = int('0x' + b.encode('hex'), 16)
            return EC_KEY(secret)
        
       -def GetPubKey(pkey, compressed=False):
       -    return i2o_ECPublicKey(pkey, compressed)
       +def GetPubKey(pubkey, compressed=False):
       +    return i2o_ECPublicKey(pubkey, compressed)
        
        def GetPrivKey(pkey, compressed=False):
            return i2d_ECPrivateKey(pkey, compressed)
       t@@ -252,48 +251,233 @@ class EC_KEY(object):
                self.pubkey = ecdsa.ecdsa.Public_key( generator_secp256k1, generator_secp256k1 * secret )
                self.privkey = ecdsa.ecdsa.Private_key( self.pubkey, secret )
                self.secret = secret
       +
       +
       +###################################### BIP32 ##############################
       +
       +def bip32_init(seed):
       +    import hmac
                
       +    I = hmac.new("Bitcoin seed", seed, hashlib.sha512).digest()
       +
       +    print "seed", seed.encode('hex')
       +    master_secret = I[0:32]
       +    master_chain = I[32:]
       +
       +    # public key
       +    curve = SECP256k1
       +    master_private_key = ecdsa.SigningKey.from_string( master_secret, curve = SECP256k1 )
       +    master_public_key = master_private_key.get_verifying_key()
       +    K = master_public_key.to_string()
       +    K_compressed = GetPubKey(master_public_key.pubkey,True)
       +    return master_secret, master_chain, K, K_compressed
       +
       +    
       +def CKD(k, c, n):
       +    import hmac
       +    from ecdsa.util import string_to_number, number_to_string
       +    order = generator_secp256k1.order()
       +    keypair = EC_KEY(string_to_number(k))
       +    K = GetPubKey(keypair.pubkey,True)
       +    I = hmac.new(c, K + rev_hex(int_to_hex(n,4)).decode('hex'), hashlib.sha512).digest()
       +    k_n = number_to_string( (string_to_number(I[0:32]) * string_to_number(k)) % order , order )
       +    c_n = I[32:]
       +    return k_n, c_n
       +
       +
       +def CKD_prime(K, c, n):
       +    import hmac
       +    from ecdsa.util import string_to_number, number_to_string
       +    order = generator_secp256k1.order()
        
       +    K_public_key = ecdsa.VerifyingKey.from_string( K, curve = SECP256k1 )
       +    K_compressed = GetPubKey(K_public_key.pubkey,True)
        
       -def filter(s): 
       +    I = hmac.new(c, K_compressed + rev_hex(int_to_hex(n,4)).decode('hex'), hashlib.sha512).digest()
       +
       +    #pubkey = ecdsa.ecdsa.Public_key( generator_secp256k1, string_to_number(I[0:32]) * K_public_key.pubkey.point )
       +    public_key = ecdsa.VerifyingKey.from_public_point( string_to_number(I[0:32]) * K_public_key.pubkey.point, curve = SECP256k1 )
       +    K_n = public_key.to_string()
       +    K_n_compressed = GetPubKey(public_key.pubkey,True)
       +    c_n = I[32:]
       +
       +    return K_n, K_n_compressed, c_n
       +
       +
       +
       +
       +
       +################################## transactions
       +
       +
       +def tx_filter(s): 
            out = re.sub('( [^\n]*|)\n','',s)
            out = out.replace(' ','')
            out = out.replace('\n','')
            return out
        
       -# https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integer
        def raw_tx( inputs, outputs, for_sig = None ):
       -    s  = int_to_hex(1,4)                                     +   '     version\n' 
       -    s += var_int( len(inputs) )                              +   '     number of inputs\n'
       +    s  = int_to_hex(1,4)                                         # version
       +    s += var_int( len(inputs) )                                  # number of inputs
            for i in range(len(inputs)):
       -        _, _, p_hash, p_index, p_script, pubkey, sig = inputs[i]
       -        s += p_hash.decode('hex')[::-1].encode('hex')        +  '     prev hash\n'
       -        s += int_to_hex(p_index,4)                           +  '     prev index\n'
       +        _, _, p_hash, p_index, p_script, pubkeysig = inputs[i]
       +        s += p_hash.decode('hex')[::-1].encode('hex')            # prev hash
       +        s += int_to_hex(p_index,4)                               # prev index
       +
                if for_sig is None:
       -            sig = sig + chr(1)                               # hashtype
       -            script  = int_to_hex( len(sig))                  +  '     push %d bytes\n'%len(sig)
       -            script += sig.encode('hex')                      +  '     sig\n'
       -            script += int_to_hex( len(pubkey))               +  '     push %d bytes\n'%len(pubkey)
       -            script += pubkey.encode('hex')                   +  '     pubkey\n'
       +            if len(pubkeysig) == 1:
       +                pubkey, sig = pubkeysig[0]
       +                sig = sig + chr(1)                               # hashtype
       +                script  = int_to_hex( len(sig))
       +                script += sig.encode('hex')
       +                script += int_to_hex( len(pubkey))
       +                script += pubkey.encode('hex')
       +            else:
       +                pubkey0, sig0 = pubkeysig[0]
       +                pubkey1, sig1 = pubkeysig[1]
       +                sig0 = sig0 + chr(1)
       +                sig1 = sig1 + chr(1)
       +                inner_script = multisig_script([pubkey0, pubkey1])
       +                script = '00'                                    # op_0
       +                script += int_to_hex(len(sig0))
       +                script += sig0.encode('hex')
       +                script += int_to_hex(len(sig1))
       +                script += sig1.encode('hex')
       +                script += var_int(len(inner_script)/2)
       +                script += inner_script
       +
                elif for_sig==i:
       -            script = p_script                                +  '     scriptsig \n'
       +            if len(pubkeysig) > 1:
       +                script = multisig_script(pubkeysig)              # p2sh uses the inner script
       +            else:
       +                script = p_script                                # scriptsig
                else:
                    script=''
       -        s += var_int( len(filter(script))/2 )                +  '     script length \n'
       +        s += var_int( len(tx_filter(script))/2 )                 # script length
                s += script
       -        s += "ffffffff"                                      +  '     sequence\n'
       -    s += var_int( len(outputs) )                             +  '     number of outputs\n'
       +        s += "ffffffff"                                          # sequence
       +
       +    s += var_int( len(outputs) )                                 # number of outputs
            for output in outputs:
                addr, amount = output
       -        s += int_to_hex( amount, 8)                          +  '     amount: %d\n'%amount 
       -        script = '76a9'                                      # op_dup, op_hash_160
       -        script += '14'                                       # push 0x14 bytes
       -        script += bc_address_to_hash_160(addr).encode('hex')
       -        script += '88ac'                                     # op_equalverify, op_checksig
       -        s += var_int( len(filter(script))/2 )                +  '     script length \n'
       -        s += script                                          +  '     script \n'
       -    s += int_to_hex(0,4)                                     # lock time
       -    if for_sig is not None: s += int_to_hex(1, 4)            # hash type
       +        s += int_to_hex( amount, 8)                              # amount
       +        addrtype, hash_160 = bc_address_to_hash_160(addr)
       +        if addrtype == 0:
       +            script = '76a9'                                      # op_dup, op_hash_160
       +            script += '14'                                       # push 0x14 bytes
       +            script += hash_160.encode('hex')
       +            script += '88ac'                                     # op_equalverify, op_checksig
       +        elif addrtype == 5:
       +            script = 'a9'                                        # op_hash_160
       +            script += '14'                                       # push 0x14 bytes
       +            script += hash_160.encode('hex')
       +            script += '87'                                       # op_equal
       +        else:
       +            raise
       +            
       +        s += var_int( len(tx_filter(script))/2 )                #  script length
       +        s += script                                             #  script
       +    s += int_to_hex(0,4)                                        #  lock time
       +    if for_sig is not None: s += int_to_hex(1, 4)               #  hash type
       +    return tx_filter(s)
       +
       +
       +def multisig_script(public_keys):
       +    # supports only "2 of 2", and "2 of 3" transactions
       +    n = len(public_keys)
       +    s = '52'
       +    for k in public_keys:
       +        s += var_int(len(k)/2)
       +        s += k
       +    if n==2:
       +        s += '52'
       +    elif n==3:
       +        s += '53'
       +    else:
       +        raise
       +    s += 'ae'
            return s
        
        
       +
       +def test_bip32():
       +    seed = "ff000000000000000000000000000000".decode('hex')
       +    master_secret, master_chain, master_public_key, master_public_key_compressed = bip32_init(seed)
       +        
       +    print "secret key", master_secret.encode('hex')
       +    print "chain code", master_chain.encode('hex')
       +
       +    key_id = hash_160(master_public_key_compressed)
       +    print "keyid", key_id.encode('hex')
       +    print "base58"
       +    print "address", hash_160_to_bc_address(key_id)
       +    print "secret key", SecretToASecret(master_secret, True)
       +
       +    print "-- m/0 --"
       +    k0, c0 = CKD(master_secret, master_chain, 0)
       +    print "secret", k0.encode('hex')
       +    print "chain", c0.encode('hex')
       +    print "secret key", SecretToASecret(k0, True)
       +    
       +    K0, K0_compressed, c0 = CKD_prime(master_public_key, master_chain, 0)
       +    print "address", hash_160_to_bc_address(hash_160(K0_compressed))
       +    
       +    print "-- m/0/1 --"
       +    K01, K01_compressed, c01 = CKD_prime(K0, c0, 1)
       +    print "address", hash_160_to_bc_address(hash_160(K01_compressed))
       +    
       +    print "-- m/0/1/3 --"
       +    K013, K013_compressed, c013 = CKD_prime(K01, c01, 3)
       +    print "address", hash_160_to_bc_address(hash_160(K013_compressed))
       +    
       +    print "-- m/0/1/3/7 --"
       +    K0137, K0137_compressed, c0137 = CKD_prime(K013, c013, 7)
       +    print "address", hash_160_to_bc_address(hash_160(K0137_compressed))
       +        
       +
       +def test_p2sh():
       +
       +    print "2 of 2"
       +    pubkeys = ["04e89a79651522201d756f14b1874ae49139cc984e5782afeca30ffe84e5e6b2cfadcfe9875c490c8a1a05a4debd715dd57471af8886ab5dfbb3959d97f087f77a",
       +               "0455cf4a3ab68a011b18cb0a86aae2b8e9cad6c6355476de05247c57a9632d127084ac7630ad89893b43c486c5a9f7ec6158fb0feb708fa9255d5c4d44bc0858f8"]
       +    s = multisig_script(pubkeys)
       +    print "address", hash_160_to_bc_address(hash_160(s.decode('hex')), 5)
       +
       +
       +    print "Gavin's tutorial: redeem p2sh:  http://blockchain.info/tx-index/30888901"
       +    pubkey1 = "0491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f86"
       +    pubkey2 = "04865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec6874"
       +    pubkey3 = "048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d46213"
       +    pubkeys = [pubkey1, pubkey2, pubkey3]
       +
       +    tx_for_sig = raw_tx( [(None, None, '3c9018e8d5615c306d72397f8f5eef44308c98fb576a88e030c25456b4f3a7ac', 0, 'a914f815b036d9bbbce5e9f2a00abd1bf3dc91e9551087', pubkeys)],
       +                         [('1GtpSrGhRGY5kkrNz4RykoqRQoJuG2L6DS',1000000)], for_sig = 0)
       +
       +    print "tx for sig", tx_for_sig
       +
       +    signature1 = "304502200187af928e9d155c4b1ac9c1c9118153239aba76774f775d7c1f9c3e106ff33c0221008822b0f658edec22274d0b6ae9de10ebf2da06b1bbdaaba4e50eb078f39e3d78"
       +    signature2 = "30440220795f0f4f5941a77ae032ecb9e33753788d7eb5cb0c78d805575d6b00a1d9bfed02203e1f4ad9332d1416ae01e27038e945bc9db59c732728a383a6f1ed2fb99da7a4"
       +
       +    for pubkey in pubkeys:
       +        import traceback, sys
       +
       +        public_key = ecdsa.VerifyingKey.from_string(pubkey[2:].decode('hex'), curve = SECP256k1)
       +
       +        try:
       +            public_key.verify_digest( signature1.decode('hex'), Hash( tx_for_sig.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
       +            print True
       +        except ecdsa.keys.BadSignatureError:
       +            #traceback.print_exc(file=sys.stdout)
       +            print False
       +
       +        try:
       +            public_key.verify_digest( signature2.decode('hex'), Hash( tx_for_sig.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
       +            print True
       +        except ecdsa.keys.BadSignatureError:
       +            #traceback.print_exc(file=sys.stdout)
       +            print False
       +
       +if __name__ == '__main__':
       +    #test_bip32()
       +    test_p2sh()
       +
   DIR diff --git a/lib/wallet.py b/lib/wallet.py
       t@@ -130,7 +130,7 @@ class Wallet:
                
                # rebuild private and public key from regenerated secret
                private_key = GetPrivKey(pkey, compressed)
       -        public_key = GetPubKey(pkey, compressed)
       +        public_key = GetPubKey(pkey.pubkey, compressed)
                address = public_key_to_bc_address(public_key)
                
                if address in self.all_addresses():
       t@@ -168,10 +168,10 @@ class Wallet:
                ADDRESS_RE = re.compile('[1-9A-HJ-NP-Za-km-z]{26,}\\Z')
                if not ADDRESS_RE.match(addr): return False
                try:
       -            h = bc_address_to_hash_160(addr)
       +            addrtype, h = bc_address_to_hash_160(addr)
                except:
                    return False
       -        return addr == hash_160_to_bc_address(h)
       +        return addr == hash_160_to_bc_address(h, addrtype)
        
            def stretch_key(self,seed):
                oldseed = seed
       t@@ -216,7 +216,7 @@ class Wallet:
                    compressed = False
                    pkey = EC_KEY(secexp)
        
       -        public_key = GetPubKey(pkey, compressed)
       +        public_key = GetPubKey(pkey.pubkey, compressed)
                addr = public_key_to_bc_address(public_key)
                if addr != address:
                    print_error('Invalid password with correct decoding')
       t@@ -606,7 +606,7 @@ class Wallet:
                    addr = item.get('address')
                    v = item.get('value')
                    total += v
       -            inputs.append((addr, v, item['tx_hash'], item['index'], item['raw_output_script'], None, None) )
       +            inputs.append((addr, v, item['tx_hash'], item['index'], item['raw_output_script'], [(None,None)] ))
                    fee = self.fee*len(inputs) if fixed_fee is None else fixed_fee
                    if total >= amount + fee: break
                else:
       t@@ -628,18 +628,18 @@ class Wallet:
            def sign_inputs( self, inputs, outputs, password ):
                s_inputs = []
                for i in range(len(inputs)):
       -            addr, v, p_hash, p_pos, p_scriptPubKey, _, _ = inputs[i]
       +            addr, v, p_hash, p_pos, p_scriptPubKey, _ = inputs[i]
                    secexp, compressed = self.get_private_key(addr, password)
                    private_key = ecdsa.SigningKey.from_secret_exponent( secexp, curve = SECP256k1 )
                    public_key = private_key.get_verifying_key()
        
                    pkey = EC_KEY(secexp)
       -            pubkey = GetPubKey(pkey, compressed)
       +            pubkey = GetPubKey(pkey.pubkey, compressed)
        
       -            tx = filter( raw_tx( inputs, outputs, for_sig = i ) )
       +            tx = raw_tx( inputs, outputs, for_sig = i )
                    sig = private_key.sign_digest( Hash( tx.decode('hex') ), sigencode = ecdsa.util.sigencode_der )
                    assert public_key.verify_digest( sig, Hash( tx.decode('hex') ), sigdecode = ecdsa.util.sigdecode_der)
       -            s_inputs.append( (addr, v, p_hash, p_pos, p_scriptPubKey, pubkey, sig) )
       +            s_inputs.append( (addr, v, p_hash, p_pos, p_scriptPubKey, [(pubkey, sig)] ) )
                return s_inputs
        
            def pw_encode(self, s, password):
       t@@ -841,7 +841,7 @@ class Wallet:
        
            def signed_tx(self, inputs, outputs, password):
                s_inputs = self.sign_inputs( inputs, outputs, password )
       -        tx = filter( raw_tx( s_inputs, outputs ) )
       +        tx = raw_tx( s_inputs, outputs )
                return tx
        
            def sendtx(self, tx):