| 3 | |
| 4 | # This function expects that pubkey begin with \x04 |
| 5 | def calculateBitcoinAddressFromPubkey(pubkey): |
| 6 | if len(pubkey) != 65: |
| 7 | print 'Could not calculate Bitcoin address from pubkey because function was passed a pubkey that was', len(pubkey), 'bytes long rather than 65.' |
| 8 | return "error" |
| 9 | ripe = hashlib.new('ripemd160') |
| 10 | sha = hashlib.new('sha256') |
| 11 | sha.update(pubkey) |
| 12 | ripe.update(sha.digest()) |
| 13 | ripeWithProdnetPrefix = '\x00' + ripe.digest() |
| 14 | |
| 15 | checksum = hashlib.sha256(hashlib.sha256( |
| 16 | ripeWithProdnetPrefix).digest()).digest()[:4] |
| 17 | binaryBitcoinAddress = ripeWithProdnetPrefix + checksum |
| 18 | numberOfZeroBytesOnBinaryBitcoinAddress = 0 |
| 19 | while binaryBitcoinAddress[0] == '\x00': |
| 20 | numberOfZeroBytesOnBinaryBitcoinAddress += 1 |
| 21 | binaryBitcoinAddress = binaryBitcoinAddress[1:] |
| 22 | base58encoded = arithmetic.changebase(binaryBitcoinAddress, 256, 58) |
| 23 | return "1" * numberOfZeroBytesOnBinaryBitcoinAddress + base58encoded |
| 24 | |
| 25 | |
| 26 | def calculateTestnetAddressFromPubkey(pubkey): |