vbuterin / pybitcointools

SImple, common-sense Bitcoin-themed Python ECC library
1.28k stars 857 forks source link

Setting vbyte to 196 in script_to_address results in a mainnet address #67

Closed jdb6167 closed 9 years ago

jdb6167 commented 9 years ago
def script_to_address(script, vbyte=0):
    if re.match('^[0-9a-fA-F]*$', script):
        script = binascii.unhexlify(script)
    if script[:3] == '\x76\xa9\x14' and script[-2:] == '\x88\xac' and len(script) == 25:
        return bin_to_b58check(script[3:-2], vbyte)  # pubkey hash addresses
    else:
        if vbyte == 111:
            # Testnet
            scripthash_byte = 196
        else:
            scripthash_byte = 5
        # BIP0016 scripthash addresses
        return bin_to_b58check(script[2:-1], scripthash_byte)

The testnet vbyte for P2SH addresses is 196. If you call script_to_address(script, 196), this code sets scripthash_byte to 5 instead. It's avoidable by setting vbyte to 111 instead, but it seems odd to use the P2PKH vbyte for a function that creates a P2SH address. I can make a pull request for this if you'd like, but it's a one-line fix.