Changeset 299
- Timestamp:
- 05/01/09 21:38:45 (3 years ago)
- Location:
- trunk/sandbox/jhb
- Files:
-
- 3 modified
-
mobile/ocwallet.py (modified) (6 diffs)
-
oc2/documentation.py (modified) (1 diff)
-
oc2/wallet.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/sandbox/jhb/mobile/ocwallet.py
r297 r299 12 12 self.makeWalletMenu() 13 13 self.displayWalletMenu() 14 self.actions=[(u'Send',u'Send coins to someone',self. getDetails),15 (u'Receive',u'Receive coins',self. getReceiveDetails),14 self.actions=[(u'Send',u'Send coins to someone',self.spendCoins), 15 (u'Receive',u'Receive coins',self.receiveCoins), 16 16 (u'Freshen up',u'Freshen up the coins',self.freshenUp), 17 (u' Buy',u'Buy new coins',self.mintCoins),18 (u' Sell',u'Sell coins',self.redeemCoins),17 (u'Mint',u'new coins from issuer',self.mintCoins), 18 (u'Redeem',u'redeem from issuer',self.redeemCoins), 19 19 (u'Details',u'See what coins you hold',self.inspectCurrency),] 20 20 21 self.methods=[(u'Internet',u'Use the internet'),22 (u'Bluetooth',u'Mobile to mobile')]23 21 24 22 self.todo = {} … … 94 92 self.execute() 95 93 96 def getReceiveDetails(self): 97 method = self.getMethod() 98 if method ==1: 99 appuifw.note(u'we are reachable at:','conf') 100 self.execute() 101 102 103 104 def getMethod(self): 105 methodlist = [u'mobile to mobile',u'internet'] 106 method = appuifw.popup_menu(methodlist) 107 self.todo['method'] = method 108 return method 109 94 110 95 def inspectCurrency(self): 111 96 #print 'inspect' … … 115 100 coinlist = [] 116 101 for coin in coins: 117 coinlist.append(u' coin: %s' % coin.denomination)102 coinlist.append(u'%s %s' % (coin.denomination,cdd.currencyId)) 118 103 self.currency_menu = appuifw.Listbox(coinlist,self.inspectCoin) 119 104 self.currency_menu.bind(EKeyRightArrow,self.inspectCoin) … … 128 113 coin = coins[self.currency_menu.current()] 129 114 details = [] 130 details.append((u nicode(coin.standardId),u'Standard Id'))131 details.append((u nicode(coin.currencyId),u'Currency Id'))132 details.append((u nicode(coin.denomination),u'Denomination'))115 details.append((u'Standard Id',unicode(coin.standardId))) 116 details.append((u'Currency Id',unicode(coin.currencyId))) 117 details.append((u'Denomination',unicode(coin.denomination))) 133 118 self.coin_menu = appuifw.Listbox(details,self.inspectCurrency) 134 119 self.coin_menu.bind(EKeyLeftArrow,self.inspectCurrency) … … 210 195 transport = transports.HTTPTransport(url) 211 196 return transport 197 198 def receiveCoins(self): 199 methodlist = [u'internet',u'bluetooth'] 200 method = appuifw.popup_menu(methodlist) 201 if method ==1: 202 appuifw.note(u'bt not implemented yet','conf') 203 else: 204 cdd,alreadythere = self.getCurrentCurrency() 205 transport = transports.HTTPTransport(cdd.issuerServiceLocation) 206 self.receiveCoinsHTTP(transport) 207 208 self.makeWalletMenu() 209 self.displayWalletMenu() 210 211 212 213 def receiveCoinsHTTP(self,transport): 214 import BaseHTTPServer, urllib 215 216 class OCHandler(BaseHTTPServer.BaseHTTPRequestHandler): 217 218 def do_POST(self): 219 #print self.server 220 if self.path == '/stop': 221 raise 'foobar' 222 length = self.headers.get('Content-Length') 223 data = self.rfile.read(int(length)) 224 data = urllib.unquote(data) 225 message = transports.createMessage(data) 226 if message.header == 'SumAnnounce': 227 answer = self.wallet.listenSum(message) 228 if message.header == 'SpendRequest': 229 answer = self.wallet.listenSpend(message,transport) 230 self.send_response(200) 231 self.send_header("Content-type", "text/plain") 232 self.wfile.write('\r\n') 233 self.wfile.write(answer.toString(True)) 234 235 port = int(appuifw.query(u'port','number','9091')) 236 OCHandler.wallet = self.wallet 237 OCHandler.wallet.getApproval = self.getApproval 238 self.startInternet() 239 240 #hack to open internet 241 #r = urllib.urlopen('http://google.com') 242 243 httpd = BaseHTTPServer.HTTPServer(("",port),OCHandler) 244 appuifw.note(u'waiting at %s:%s' % ('localhost',port),'conf') 245 httpd.handle_request() 246 httpd.handle_request() 247 self.stopInternet() 248 249 def spendCoins(self): 250 251 amount = self.getAmount() 252 if not amount: 253 return 254 255 target = self.getTarget() 256 if not target: 257 return 258 cdd,alreadythere = self.getCurrentCurrency() 259 url = appuifw.query(u'url','text',u'http://192.168.2.105:9091') 260 transport = transports.HTTPTransport(url) 261 262 self.wallet.spendCoins(transport,cdd.currencyId,amount,target) 263 self.makeWalletMenu() 264 self.displayWalletMenu() 265 266 267 268 269 def getApproval(self,message): 270 amount = message.amount 271 target = message.target 272 return appuifw.query(u'Accept %s (ref "%s")?' % (amount,target),'query') 273 274 275 276 277 278 212 279 213 280 def startInternet(self): … … 217 284 #sys.modules['socket'] = __import__('btsocket') 218 285 import btsocket as socket 219 #apid = socket.select_access_point()220 #apo = socket.access_point(apid)221 #socket.set_default_access_point(apo)222 #apo.start()223 #self.apo = apo224 #self.ip = apo.ip()286 apid = socket.select_access_point() 287 apo = socket.access_point(apid) 288 socket.set_default_access_point(apo) 289 apo.start() 290 self.apo = apo 291 self.ip = apo.ip() 225 292 226 293 except ImportError: 227 294 import socket 295 self.ip = '127.0.0.2' 228 296 229 297 -
trunk/sandbox/jhb/oc2/documentation.py
r279 r299 39 39 >>> from wallet import Wallet 40 40 >>> wallet = Wallet({}) 41 >>> import protocols42 41 >>> cdd == wallet.askLatestCDD(issuer.giveLatestCDD) 43 42 True -
trunk/sandbox/jhb/oc2/wallet.py
r298 r299 38 38 target = message.target 39 39 approval = getattr(self,'approval',True) #get that from ui 40 if approval == True:41 self.addIncoming(message)42 40 return approval 43 41 … … 107 105 if approval == True: 108 106 answer = messages.SumAccept() 107 self.addIncoming(message) 109 108 else: 110 109 answer = messages.SumReject() … … 123 122 return True 124 123 125 def listenSpend(self,message): 124 125 def listenSpend(self,message,transport=None): 126 126 tid = message.transactionId 127 127 amount = sum([int(m.denomination) for m in message.coins]) … … 138 138 return answer 139 139 #do exchange 140 140 if transport: 141 cdd = self.askLatestCDD(transport) 142 currency = self.getCurrency(cdd.currencyId) 143 newcoins = message.coins 144 self.freshenUp(transport,cdd,newcoins) 141 145 142 146 answer = messages.SpendAccept() … … 159 163 out = [] 160 164 for key,currency in self.storage.items(): 161 cdd = currency['cdds'][-1] 162 amount = sum([int(coin.denomination) for coin in currency['coins']]) 163 out.append((cdd,amount)) 165 try: 166 cdd = currency['cdds'][-1] 167 amount = sum([int(coin.denomination) for coin in currency['coins']]) 168 out.append((cdd,amount)) 169 except: 170 del(self.storage[key]) 164 171 return out 165 172 … … 260 267 261 268 262 def freshenUp(self,transport,cdd ):269 def freshenUp(self,transport,cdd,newcoins=[]): 263 270 currency = self.getCurrency(cdd.currencyId) 264 paycoins,secrets,data = self.prepare4exchange(transport,cdd,currency['coins'], [])271 paycoins,secrets,data = self.prepare4exchange(transport,cdd,currency['coins'],newcoins) 265 272 if secrets: 266 273 tid = self.makeSerial() 267 response = self.requestTransfer(transport,tid,None,data,paycoins )274 response = self.requestTransfer(transport,tid,None,data,paycoins+newcoins) 268 275 coins = currency['coins'] 269 276 for coin in paycoins: … … 293 300 else: 294 301 return [],[],[] 302 303 304 def spendCoins(self,transport,currencyId,amount,target): 305 currency = self.getCurrency(currencyId) 306 coins = currency['coins'] 307 picked = self.pickForSpending(amount,coins) 308 tid = self.makeSerial() 309 310 self.announceSum(transport,tid,amount,target) 311 312 response = self.requestSpend(transport,tid,picked) 313 if response == True: 314 newcoins = [c for c in coins if c not in picked] 315 currency['coins'] = newcoins 316 self.storage.save() 317 318
