Last active
November 4, 2015 06:13
-
-
Save lsparrish/2468956 to your computer and use it in GitHub Desktop.
simple bitoin-like program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #==========Bitzap: a tiny bitcoin-like program.==========# | |
| # The goal is secure published transactions. Not yet completed. | |
| # Install Notes: | |
| # Uses dbdict, a recipe from: http://code.activestate.com/recipes/576642-persistent-dict-with-multiple-standard-file-format/ | |
| # On Ubuntu the Crypto library can be installed with "sudo apt-get install python-crypto" | |
| # Details: | |
| # Addresses are first 32 chars of a hash of the public key. | |
| # Transaction IDs are first 32 chars of the buyer's signature. | |
| # Receipts are lists of debits and credits that have been applied to each address. | |
| # Buyer is sender, seller is receiver. | |
| #===============================# | |
| from Crypto.Hash import SHA256 | |
| from Crypto.PublicKey import RSA | |
| from Crypto import Random | |
| import time | |
| #from dbdict import PersistentDict | |
| from pprint import pprint | |
| #===============================# | |
| #d = PersistentDict('bitzap.json',flag='c',format='json') | |
| def r(): return Random.new().read | |
| def k(): return RSA.generate(1024,r()) | |
| def newitem(): | |
| key=k(); pkey=key.publickey() | |
| text=pkey.exportKey() | |
| return SHA256.new(text).hexdigest()[:32], key, pkey | |
| def privateaddress(address, key, pkey): | |
| return {address:key.exportKey()} | |
| def publicaddress(address, key, pkey): | |
| return {address:pkey.exportKey()} | |
| def newaddress(d,item): | |
| d['private'].update(privateaddress(*item)) | |
| d['public'].update(publicaddress(*item)) | |
| d['receipt']['debit'].update({item[0]:[]}) | |
| d['receipt']['credit'].update({item[0]:[]}) | |
| def newreciept(transid): | |
| buyer,seller,amount=transinfo(transid)[:3] | |
| d['receipt']['credit'][seller].append(transid) | |
| d['receipt']['debit'][buyer].append(transid) | |
| #---lookup functions---# | |
| def getpkey(address): | |
| text=d['public'][address] | |
| return RSA.importKey(text) | |
| def getkey(address): | |
| text=d['private'][address] | |
| return RSA.importKey(text) | |
| def transinfo(transid): | |
| return d['transaction'][transid][1] | |
| def transbuyer(transid): | |
| return transinfo(transid)[0] | |
| def transseller(transid): | |
| return transinfo(transid)[1] | |
| def transamount(transid): | |
| return transinfo(transid)[2] | |
| #---transaction functions---# | |
| def createtransaction(buyer,seller,amount): | |
| key=getkey(buyer) | |
| transinfo=[buyer,seller,amount,time.ctime()] | |
| transtext=str(transinfo) | |
| transsig=key.sign(transtext,'') | |
| transid=str(transsig[0])[:32] | |
| return {transid:[transsig,transinfo]} | |
| def addtransaction(buyer,seller,amount): | |
| trans=createtransaction(buyer,seller,amount) | |
| d['transaction'].update(trans) | |
| transid=trans.keys()[0] | |
| newreciept(transid) | |
| return transid | |
| def verifytranssig(transid): | |
| trans=d['transaction'][transid] | |
| transsig=trans[0] | |
| transinfo=trans[1] | |
| pkey=getpkey(transinfo[0]) | |
| return pkey.verify(str(transinfo),transsig) | |
| def getamounts(address,transtype): | |
| translist=d['receipt'][transtype][address] | |
| total=[] | |
| for transid in translist: | |
| total.append(transamount(transid)) | |
| return sum(total) | |
| def verifytransamount(transid): | |
| available=getamounts(transbuyer(transid),'credit')-getamounts(transbuyer(transid),'debit') | |
| return available >= transamount(transid) | |
| def verifytransaction(transid): | |
| return verifytranssig(transid) & verifytransamount(transid) | |
| #---test---# | |
| def init(): | |
| d={}; d['private'],d['public'],d['transaction'],d['receipt']={},{},{},{'credit':{},'debit':{}} | |
| return d | |
| d=init() | |
| item=newitem() | |
| newaddress(d,item) | |
| buyer=item[0] | |
| item=newitem() | |
| newaddress(d,item) | |
| seller=item[0] | |
| amount=100 | |
| trans=addtransaction(buyer, seller, amount) | |
| def p(text): pprint(d[text]) | |
| p('receipt') | |
| print verifytransamount(trans) | |
| def publishtrans(transid): | |
| transinfo=transinfo(transid) | |
| if verifytransaction(transid): | |
| sendtrans(transid) | |
| else: | |
| deletetrans(transid) | |
| def deletetrans(transid): | |
| trans=d['transaction'].pop(transid) | |
| buyer,seller=trans[1][:2] | |
| d['receipt']['debit'].pop(buyer) | |
| d['receipt']['credit'].pop(seller) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment