#!/usr/bin/python2.5 libdir = '/home/joerg/opencoin' datadir = '/home/joerg/opencoin/webwallet/data' ################################## import sys, os sys.path.append(libdir) import cgitb; cgitb.enable() import cgi import oc2 from oc2 import storage as oc2storage from oc2 import wallet, transports #for key,value in os.environ.items(): # print '%s: %s
\n' % (key,value) #print str(username) class CGIWallet: def __init__(self,storage): self.storage = storage self.wallet = wallet.Wallet(storage) self.output = [] self.mimetype = 'text/html' def out(self,text): if type(text) != type(''): text = str(text) self.output.append(text) def printout(self): print """Content-type:%s""" % self.mimetype print "\r" if self.mimetype == 'text/html': print '' #print 'Logout' % (baseurl) print '
\n'.join(self.output) print '' else: print '\n'.join(self.output) def getCurrency(self,currencyId): return dict(self.getCurrencies())[currencyId] def getCurrencies(self): return [(cdd.currencyId,(cdd,amount)) for cdd,amount in self.wallet.listCurrencies()] def dispatchRequest(self): self.env = os.environ self.action = action = self.form.getfirst('action','') self.method = method = self.env['REQUEST_METHOD'].lower() postmapping = dict(addcurrency = self.addCurrency, mint = self.mintCoins, redeem = self.redeemCoins, delcurrency = self.delCurrency, spend = self.spendCoins, login = self.displayMain) getmapping = dict(addcurrency = self.displayAddCurrency, mint = self.displayMint, redeem = self.displayRedeem, spend = self.displaySpend, delcurrency = self.displayDelCurrency, freshenup = self.freshenUp) if method == 'post': if postmapping.has_key(action): postmapping[action]() else: self.receiveCoins() elif method == 'get': if getmapping.has_key(action): getmapping[action]() else: self.displayMain() ################################ main ################################################ def displayMain(self): tmp = [(cdd.currencyId,cdd,amount) for cdd,amount in self.wallet.listCurrencies()] tmp.sort() currencies = [(t[1],t[2]) for t in tmp] items = [] for cdd,amount in currencies: entry = """

%(amount)s %(cid)ss
Pay Get change Withdraw Exchange back Remove

""" % dict(amount=amount, isl=cdd.issuerServiceLocation, cid=cdd.currencyId, baseurl=baseurl) items.append(entry) items = '\n'.join(items) html = """

Wallet content

%s ----
Add a currency """ % (items,baseurl) self.out(html) ############################### add a currency #################################### def addCurrency(self): url = self.form.getfirst('url','') if url: transport = transports.HTTPTransport(url) self.wallet.addCurrency(transport) self.storage.save() self.displayMain() def displayAddCurrency(self): html="""

Add a currency

The url of the issuer:

""" % baseurl self.out(html) ############################### delete a currency #################################### def displayDelCurrency(self): currencyId = self.form.getfirst('currencyId','') cdd,amount = self.getCurrency(currencyId) html="""

Remove %s

Really really delete %s with %s coins - there is no way to recover

or go back to main screen

""" % (currencyId,baseurl,currencyId,amount,currencyId,baseurl,currencyId) self.out(html) def delCurrency(self): id = self.form.getfirst('currencyId','') self.wallet.deleteCurrency(id) self.storage.save() self.displayMain() ############################### minting #################################### def mintCoins(self): amount = int(self.form.getfirst('amount',1)) reference = self.form.getfirst('reference') cdd,wehave = self.getCurrency(self.form.getfirst('currencyId')) transport = transports.HTTPTransport(cdd.issuerServiceLocation) self.wallet.mintCoins(transport,amount,reference) self.storage.save() self.displayMain() def displayMint(self): currencyId = self.form.getfirst('currencyId','coin') html="""

Get new coins

How many %ss

Optional message

""" % (baseurl,currencyId,currencyId) self.out(html) ############################### redeem #################################### def redeemCoins(self): amount = int(self.form.getfirst('amount',1)) reference = self.form.getfirst('reference') cdd,wehave = self.getCurrency(self.form.getfirst('currencyId')) transport = transports.HTTPTransport(cdd.issuerServiceLocation) self.wallet.redeemCoins(transport,amount,reference) self.storage.save() self.displayMain() def displayRedeem(self): currencyId = self.form.getfirst('currencyId','coin') html="""

Redeem coins

How many %ss

Optional message

""" % (baseurl,currencyId,currencyId) self.out(html) ############################### spend #################################### def spendCoins(self): amount = int(self.form.getfirst('amount',1)) reference = self.form.getfirst('reference') url = self.form.getfirst('url') if not url.startswith('http://'): url = 'http://%s' % url cid = self.form.getfirst('currencyId') transport = transports.HTTPTransport(url) self.wallet.spendCoins(transport,cid,amount,reference) self.storage.save() self.displayMain() def displaySpend(self): currencyId = self.form.getfirst('currencyId','coin') html="""

Pay someone

Recipient

How many %ss

Optional message

Please make sure the other side is ready to accept!
""" % (baseurl,currencyId,currencyId) self.out(html) ############################### freshenUp #################################### def freshenUp(self): cdd,wehave = self.getCurrency(self.form.getfirst('currencyId')) transport = transports.HTTPTransport(cdd.issuerServiceLocation) self.wallet.freshenUp(transport,cdd) self.storage.save() self.displayMain() ############################### freshenUp #################################### def receiveCoins(self): message = transports.createMessage(self.form.list[0].name) if message.header == 'SumAnnounce': answer = self.wallet.listenSum(message) if message.header == 'SpendRequest': cdd,wehave = self.getCurrency(message.coins[0].currencyId) transport = transports.HTTPTransport(cdd.issuerServiceLocation) answer = self.wallet.listenSpend(transport,message) self.storage.save() self.mimetype='text/plain' self.out(answer.toString(True)) form = cgi.FieldStorage(keep_blank_values=1) baseserver = "http://%s:%s" % (os.environ['SERVER_NAME'],os.environ['SERVER_PORT']) username = os.environ.get('PATH_INFO','') baseurl = os.environ['SCRIPT_NAME']+username def die(string): print 'Content-type:text/plain\r\n' print string sys.exit(0) if username == '/' or not username: die('username required, no direct access allowed. Try %s%s/YOURNAME' % (baseserver,baseurl)) username = username[1:] if username.startswith('.') or '/' in username: die('hacking in, ey') if 0: password = None if form.has_key('password'): password = form.getfirst('password') print "Set-Cookie:%s=%s" % (username,password) elif form.getfirst('action','') == 'logout': print "Set-Cookie:%s=%s" % (username,'') elif os.environ.has_key('HTTP_COOKIE'): for cookie in [c.strip() for c in os.environ['HTTP_COOKIE'].split(';')]: if not cookie: continue (key, value ) = cookie.split('='); if key == username: password = value filepath = datadir+'/%s.bin' % username storage = oc2storage.CryptedStorage() storage.setPassword(password) storage.setFilename(filepath) message = '' if password and not os.path.exists(filepath): storage.save() elif form.getfirst('action','') not in ['logout','']: try: storage.restore() storage.save() except: print "Set-Cookie:%s=%s" % (username,'') password = None message = 'Wrong password
' if not password: print """Content-type:text/html
%s Enter your password:
""" % (baseurl,message) sys.exit(0) filepath = datadir+'/%s.bin' % username storage = oc2storage.Storage() storage.setFilename(filepath) storage.restore() w = CGIWallet(storage) w.form = form w.dispatchRequest() w.printout() #for key,value in os.environ.items(): # print '%s: %s
\n' % (key,value) #print str(username)