Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save landgenoot/4858232422bd896f26c9ee5e9a8da4ff to your computer and use it in GitHub Desktop.
Save landgenoot/4858232422bd896f26c9ee5e9a8da4ff to your computer and use it in GitHub Desktop.
#!/bin/python
# Calculate address from public key according to http://www.multichain.com/developers/address-key-format/
import asn1
import base58
import base64
import binascii
import hashlib
import os
import sys
from pypassport import epassport, reader
from pypassport import fingerPrint
from pypassport.doc9303 import converter
from ecdsa import VerifyingKey, BRAINPOOLP320r1
from pyasn1.codec.der.decoder import decode
from pyasn1.type import univ, namedtype
sys.dont_write_bytecode = True
def main():
r = reader.ReaderManager().waitForCard(10)
MRZ = "" # enter last line of MRZ here
ep = epassport.EPassport(r, MRZ)
ep.doBasicAccessControl()
public = ep._getDG(15).body
received_record, rest_of_substrate = decode(public)
bitList = received_record.__getitem__(1)
bits = ''
for x in bitList:
bits = bits + str(x)
# step 2
pubkey = hex(int(bits, 2))[3:-1]
print "The multichain address of document " + MRZ[:9] + " is " + pubkeyToAddress(pubkey)
def pubkeyToAddress(pubkey):
# Taken from params.dat
version = ['00','8c','b5','d6']
# version = ['00','af','ea','21']
addressChecksum = '5afce7b2'
# addressChecksum = '953abc69'
# step 3
pubkeyHash = hashlib.sha256(binascii.unhexlify(pubkey))
h = hashlib.new('ripemd160')
h.update(pubkeyHash.digest())
# step 4
pubkey160Hash = h.hexdigest()
# step 5
# pubkey160HashWithVersion = ''
# for x in range(4):
# pubkey160HashWithVersion = pubkey160HashWithVersion + version[x] + pubkey160Hash[(x*10):(x*10)+10]
# step 6
sha256of160hash = hashlib.sha256(binascii.unhexlify(pubkey160Hash))
sha256of160hash.hexdigest()
# step 7
sha256ofPrevSha256 = hashlib.sha256(sha256of160hash.digest())
# step 8
checksum = sha256ofPrevSha256.hexdigest()[0:8]
# step 9
# xorChecksum = '{:08x}'.format(int(int(checksum, 16) ^ int(addressChecksum, 16)))
# step 10
binaryAddress = pubkey160Hash + checksum
# step 11
return base58.b58encode(binascii.unhexlify(binaryAddress))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment