- Timestamp:
- 06/18/11 18:23:42 (1 year ago)
- Location:
- trunk/sandbox/jhb/oc2
- Files:
-
- 3 added
- 6 modified
- 9 moved
-
README (modified) (1 diff)
-
demoissuer.py (moved) (moved from trunk/sandbox/jhb/oc2/testissuer.py) (2 diffs)
-
doc/README.txt (modified) (1 diff)
-
doc/message_dump.txt (added)
-
documentation.py (modified) (12 diffs)
-
messages.py (modified) (1 diff)
-
oldstuff (added)
-
oldstuff/issuerservice.py (moved) (moved from trunk/sandbox/jhb/oc2/issuerservice.py)
-
oldstuff/occrypto_tlslite.py (moved) (moved from trunk/sandbox/jhb/oc2/occrypto_tlslite.py)
-
oldstuff/test_issuer.py (moved) (moved from trunk/sandbox/jhb/oc2/test_issuer.py)
-
oldstuff/test_playground.py (moved) (moved from trunk/sandbox/jhb/oc2/test_playground.py)
-
oldstuff/testclient.py (moved) (moved from trunk/sandbox/jhb/oc2/testclient.py)
-
oldstuff/testmobile.py (moved) (moved from trunk/sandbox/jhb/oc2/testmobile.py)
-
oldstuff/testwallet.py (moved) (moved from trunk/sandbox/jhb/oc2/testwallet.py)
-
simplejson (added)
-
testutils.py (moved) (moved from trunk/sandbox/jhb/oc2/testserver.py) (3 diffs)
-
transports.py (modified) (5 diffs)
-
wallet.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/sandbox/jhb/oc2/README
r310 r338 1 (c) 2009 Joerg Baach, License: GPL V3 1 (c) 2009 Joerg Baach, License: GPL 2 3 Please check out svn.opencoin.org/svn/opencoin/trunk/sandbox/jhb/simplejson 4 either into this directory or symlink it into this directory (oc2). 5 6 Most documentation is in documentation.py, but also have a look into docs. 7 8 IMPORTANT: the whole crypto implementation was done to have a pure python 9 version suitable for Nokia phones. Its only purpose is to demo the feasibiltiy 10 11 !!! DON'T USE FOR PRODUCTION !!! 12 13 14 15 16 17 18 -
trunk/sandbox/jhb/oc2/demoissuer.py
r320 r338 1 """run this file to run an issuer. If required, it will setup a currency""" 2 3 ### minmal config ### 4 5 #where can this server be reached? 6 baseurl = 'http://192.168.2.101' 7 #what port to run on? 8 port = 9090 9 10 ### nothing to config below this ### 11 1 12 import BaseHTTPServer 2 13 import issuer, mint,authorizer,storage 3 14 from testserver import Handler 4 5 6 7 8 9 10 port = 909011 15 12 16 issuerstorage = storage.Storage().setFilename('data/issuerstorage.bin').restore() … … 24 28 print 'issuer: setup currency discription' 25 29 denominations=[1,2,5,10,20,50,100,200,500,1000,2000,5000,10000,20000,50000,100000,200000,500000] 26 cdd = issuer.makeCDD('TestCent','tc',[str(d) for d in denominations],' http://192.168.2.101:%s/' % port,'')30 cdd = issuer.makeCDD('TestCent','tc',[str(d) for d in denominations],'%s:%s/' % (baseurl,port),'') 27 31 mint.setCDD(cdd) 28 32 -
trunk/sandbox/jhb/oc2/doc/README.txt
r335 r338 1 1 The sequence diagrams are made with http://sdedit.sourceforge.net/index.html 2 3 Please check the doctests in documentation.py one level up. -
trunk/sandbox/jhb/oc2/documentation.py
r337 r338 1 1 """ 2 This is documentation (and doctest) file. It shows how the API is used, and 3 how the system is supposed to work. 4 5 Please have a look at doc/message_dump.txt, or turn on the transports.printmessages 6 below, and generate the output for yourself. The section headers in the output 7 are generated by the 'printSection' commands in this file. 8 9 10 >>> import transports 11 >>> from transports import DirectTransport as DT 12 >>> from transports import printSection 13 14 toggle printing of the messages 15 >>> transports.printmessages = 0 16 17 2 18 3 19 ############################################################################### … … 33 49 34 50 51 >>> import storage 35 52 36 53 Wallet fetches cdd from issuer 37 54 38 >>> #faked request 55 >>> printSection('Basic Setup') 56 57 we could use the method directly 39 58 >>> from wallet import Wallet 40 >>> wallet = Wallet( {})59 >>> wallet = Wallet(storage.EmptyStorage()) 41 60 >>> cdd == wallet.askLatestCDD(issuer.giveLatestCDD) 42 61 True 43 62 44 >>> #using http 45 >>> import transports 46 >>> import testserver 63 but its better to use more real transports 64 >>> import testutils 47 65 >>> transport = transports.HTTPTransport('http://localhost:%s/' % port) 48 >>> test server.run_once(port,issuer)66 >>> testutils.run_once(port,issuer) 49 67 >>> cdd2 = wallet.askLatestCDD(transport) 50 68 >>> cdd2.toString(True) == cdd.toString(True) … … 56 74 Wallet: fetches current public minting keys for denomination 57 75 58 >>> testserver.run_once(port,issuer) 76 77 >>> printSection('Prepare Blinds') 78 79 >>> testutils.run_once(port,issuer) 59 80 >>> mkcs = wallet.fetchMintKeys(transport,cdd,denominations=['1','5']) 60 81 >>> mkcs[0].toString() == issuer.getCurrentMKCs()['1'].toString() … … 83 104 84 105 We first need to setup an authorizer, to (surpise) authorize the request. Nils says 85 the mint should more or lessjust mint106 the mint should just mint 86 107 87 108 >>> from authorizer import Authorizer … … 93 114 Lets have the authorizer denying the request 94 115 116 >>> printSection('Blinding I') 117 95 118 >>> authorizer.deny = True 96 >>> test server.run_once(port,issuer=issuer,mint=mint,authorizer=authorizer)119 >>> testutils.run_once(port,issuer=issuer,mint=mint,authorizer=authorizer) 97 120 >>> wallet.requestTransfer(transport,tid,'foo',[[mkc.keyId,blind]],[]).header 98 121 'TransferReject' … … 100 123 Now have a well working one 101 124 125 >>> printSection('Blinding II') 126 102 127 >>> authorizer.deny = False 103 >>> test server.run_once(port,issuer=issuer,mint=mint,authorizer=authorizer)128 >>> testutils.run_once(port,issuer=issuer,mint=mint,authorizer=authorizer) 104 129 >>> response = wallet.requestTransfer(transport,tid,'foo',[[mkc.keyId,blind]],[]) 105 130 >>> response.header … … 121 146 The mint can also be a bit slow 122 147 148 >>> printSection('Delay I') 149 123 150 >>> mint.delay = True 124 >>> test server.run_once(port,issuer=issuer,mint=mint,authorizer=authorizer)151 >>> testutils.run_once(port,issuer=issuer,mint=mint,authorizer=authorizer) 125 152 >>> wallet.requestTransfer(transport,tid,'foo',[[mkc.keyId,blind]],[]).header 126 153 'TransferDelay' … … 130 157 Or the issuer is slow 131 158 159 >>> printSection('Delay II') 160 132 161 >>> issuer.delay = True 133 >>> test server.run_once(port,issuer=issuer)162 >>> testutils.run_once(port,issuer=issuer) 134 163 >>> wallet.resumeTransfer(transport,tid).header 135 164 'TransferDelay' … … 138 167 So we need to resume 139 168 140 >>> testserver.run_once(port,issuer=issuer) 169 >>> printSection('Resume') 170 171 >>> testutils.run_once(port,issuer=issuer) 141 172 >>> wallet.resumeTransfer(transport,tid).header 142 173 'TransferAccept' … … 156 187 a sum, and bob dedices if he wants to accept it 157 188 189 >>> printSection('W2W: Announce') 158 190 >>> alice = wallet 159 >>> bobport = 9091 160 >>> bob = Wallet({}) 161 >>> import test 191 >>> bob = Wallet(storage.EmptyStorage()) 162 192 >>> bob.approval = "I don't like odd sums" 163 193 >>> alicetid = alice.makeSerial() 164 >>> alice.announceSum( bob.listenSum, alicetid, 5, 'foobar')194 >>> alice.announceSum(DT(bob.listenSum), alicetid, 5, 'foobar') 165 195 "I don't like odd sums" 166 196 167 197 >>> bob.approval = True 168 >>> alice.announceSum( bob.listenSum, alicetid, 5, 'foobar')198 >>> alice.announceSum(DT(bob.listenSum), alicetid, 5, 'foobar') 169 199 True 170 200 … … 173 203 serial and signature) 174 204 205 For testing this we need to have a special transport that actually 206 allows the two consecutive requests that bob needs to make 207 208 >>> tht = transports.TestingHTTPTransport(port,issuer=issuer,mint=mint) 209 210 >>> printSection('W2W: Rejected I') 211 175 212 Lets have first a wrong transactionId 176 >>> #transport = transports.YieldTransport(bob.run,[]) 177 >>> alice.requestSpend(bob.listenSpend,'foobar',[coin]) 213 >>> alice.requestSpend(DT(bob.listenSpend,tht),'foobar',[coin]) 178 214 Traceback (most recent call last): 179 215 .... … … 182 218 Or lets try to send a wrong amount 183 219 184 >>> alice.announceSum(bob.listenSum, alicetid, 5, 'foobar') 185 True 186 >>> alice.requestSpend(bob.listenSpend,alicetid,[]) 220 >>> printSection('W2W: Rejected II') 221 >>> alice.announceSum(DT(bob.listenSum), alicetid, 5, 'foobar') 222 True 223 >>> alice.requestSpend(DT(bob.listenSpend,tht),alicetid,[]) 187 224 Traceback (most recent call last): 188 225 .... 189 226 SpendReject: amount of coins does not match announced one. Announced: 5, got 0 190 227 191 192 >>> alice.announceSum(bob.listenSum, alicetid, 5, 'foobar') 193 True 194 >>> alice.requestSpend(bob.listenSpend, alicetid, [coin]) 195 True 196 197 198 199 ############################################################################### 200 Now, lets first pretend we are on Bobs side. Fix that later, but assume we 201 received the coins, we know what cdd and mkc to use. We need to exchange now 202 203 >>> coins = [coin] 204 >>> key = mkc.publicKey 205 >>> bobblank = bob._makeBlank(cdd,mkc) 206 >>> bobsecret, bobblind = key.blindBlank(bobblank) 207 >>> blinds = [[mkc.keyId,bobblind]] 228 And now, finally 229 230 >>> printSection('W2W: Success') 231 >>> alice.announceSum(DT(bob.listenSum), alicetid, 5, 'foobar') 232 True 233 >>> alice.requestSpend(DT(bob.listenSpend,tht), alicetid, [coin]) 234 True 235 236 That was so fun, lets do it again 237 238 >>> printSection('W2W: Double Spend') 239 >>> alicetid = alice.makeSerial() 240 >>> bob.approval = True 241 >>> alice.announceSum(DT(bob.listenSum), alicetid, 5, 'foobar') 242 True 243 >>> alice.requestSpend(DT(bob.listenSpend,tht), alicetid, [coin]) 244 Traceback (most recent call last): 245 .... 246 SpendReject: did not go through 247 248 249 >>> printSection('Redeem') 208 250 >>> bobtid = wallet.makeSerial() 209 210 >>> testserver.run_once(port,issuer=issuer,mint=mint) 211 >>> response = bob.requestTransfer(transport,tid,blinds = blinds, coins = coins) 212 >>> bobblank.signature = key.unblind(bobsecret,response.signatures[0]) 213 >>> bobcoin = bobblank 214 >>> key.verifyContainerSignature(bobcoin) 215 True 216 217 Lets try to double spend 218 219 >>> import messages 220 >>> bobblank = bob._makeBlank(cdd,mkc) 221 >>> bobsecret, bobblind = key.blindBlank(bobblank) 222 >>> blinds = [[mkc.keyId,bobblind]] 223 >>> bobtid = wallet.makeSerial() 224 >>> testserver.run_once(port,issuer=issuer,mint=mint) 225 >>> wallet.requestTransfer(transport,tid,blinds = blinds, coins = coins).header 226 'TransferReject' 227 228 229 230 ############################################################################### 231 Last step - bob wants to redeem the coins 232 233 >>> bobtid = wallet.makeSerial() 234 >>> testserver.run_once(port,issuer=issuer,mint=mint) 235 >>> bob.requestTransfer(transport,tid,target='foo', coins = [bobcoin]).header 251 >>> testutils.run_once(port,issuer=issuer,mint=mint) 252 >>> bobscoins = bob.storage[cdd.currencyId]['coins'] 253 >>> bob.requestTransfer(transport,tid,target='foo', coins = bobscoins).header 236 254 'TransferAccept' 255 237 256 238 257 """ -
trunk/sandbox/jhb/oc2/messages.py
r274 r338 83 83 ] 84 84 85 class TransferResume(Message):86 fields = Message.fields + [87 Field('transactionId'),88 ]89 90 class TransferResume(Message):91 fields = Message.fields + [92 Field('transactionId'),93 ]94 95 85 class SumAnnounce(Message): 96 86 fields = Message.fields + [ -
trunk/sandbox/jhb/oc2/testutils.py
r290 r338 1 """This file is needed by documentation.py and testissuer.py""" 2 1 3 import BaseHTTPServer, threading 2 4 import issuer, mint, transports, urllib … … 27 29 self.wfile.write(answer.toString(True)) 28 30 31 class TestingHandler(Handler): 32 """Supress output to stderr""" 29 33 34 def log_message(self, format, *args): 35 pass 30 36 31 37 def run_once(port,issuer=None,mint=None,authorizer=None): … … 35 41 Handler.mint = mint 36 42 Handler.authorizer = authorizer 37 httpd = BaseHTTPServer.HTTPServer(("", port), Handler)43 httpd = BaseHTTPServer.HTTPServer(("", port), TestingHandler) 38 44 import threading 39 45 t = threading.Thread(target=httpd.handle_request) -
trunk/sandbox/jhb/oc2/transports.py
r320 r338 1 import messages, simplejson 1 import messages, simplejson, urllib, sys 2 from pprint import pformat 3 4 5 printmessages = 0 6 7 def printMessage(message): 8 sys.stderr.write('\n') 9 sys.stderr.write('-'*30) 10 sys.stderr.write('\n\n') 11 sys.stderr.write(pformat(message.getData(1))+'\n') 12 13 def printSection(header): 14 if printmessages: 15 out = """ 16 17 #################################################################### 18 %s 19 #################################################################### 20 21 """ % header 22 23 sys.stderr.write(out) 24 2 25 3 26 def createMessage(data): … … 19 42 20 43 def __call__(self,message): 21 import urllib 44 45 if printmessages: 46 printMessage(message) 47 22 48 response = urllib.urlopen(self.url,message.toString(True)) 23 return createMessage(response.read()) 49 reply = createMessage(response.read()) 50 51 if printmessages: 52 printMessage(reply) 53 54 return reply 24 55 25 import sys 56 class TestingHTTPTransport(object): 57 58 def __init__(self,port,**kwargs): 59 self.port = port 60 self.kwargs = kwargs 61 62 def __call__(self,message): 63 import testutils 64 testutils.run_once(self.port,**self.kwargs) 65 transport = HTTPTransport('http://localhost:%s/' % self.port) 66 return transport(message) 67 26 68 27 69 class BTTransport(object): … … 48 90 return createMessage(received) 49 91 92 93 class DirectTransport(object): 94 95 def __init__(self,target,transport2=None): 96 self.target = target 97 self.transport2 = transport2 98 99 def __call__(self,message): 50 100 101 if printmessages: 102 printMessage(message) 103 104 if self.transport2: 105 response = self.target(message,self.transport2) 106 else: 107 response = self.target(message) 108 109 if printmessages: 110 printMessage(response) 111 112 return response 51 113 52 114 class YieldTransport(object): … … 70 132 self.results = list(args) 71 133 self.debug = 0 134 72 135 def __call__(self,message): 73 136 if self.debug: … … 75 138 result = self.results.pop(0) 76 139 77 if type(result) == type([]) or type(result) == type(()):140 if type(result) == list or type(result) == tuple: 78 141 method = result[0] 79 142 args = list(result[1:]) -
trunk/sandbox/jhb/oc2/wallet.py
r337 r338 151 151 answer.reason = 'unknown transactionId' 152 152 return answer 153 153 154 #check sum 154 155 if amount != int(orig.amount): … … 156 157 answer.reason = 'amount of coins does not match announced one. Announced: %s, got %s' % (orig.amount, amount) 157 158 return answer 159 158 160 #do exchange 159 161 if transport: … … 161 163 currency = self.getCurrency(cdd.currencyId) 162 164 newcoins = message.coins 163 self.freshenUp(transport,cdd,newcoins) 164 165 answer = self.freshenUp(transport,cdd,newcoins) 166 if answer.header != 'TransferAccept': 167 answer = messages.SpendReject() 168 answer.reason = 'did not go through' 169 return answer 170 165 171 answer = messages.SpendAccept() 166 172 answer.transactionId = tid … … 305 311 tid = self.makeSerial() 306 312 response = self.requestTransfer(transport,tid,None,data,paycoins+newcoins) 313 if response.header != 'TransferAccept': 314 return response 307 315 coins = currency['coins'] 308 316 for coin in paycoins: … … 310 318 coins.extend(self.unblindWithSignatures(secrets,response.signatures)) 311 319 self.storage.save() 320 return response 321 else: 322 return messages.Error() 312 323 313 324 def prepare4exchange(self,transport,cdd,oldcoins,newcoins):
