- Timestamp:
- 04/24/09 16:17:24 (3 years ago)
- Location:
- trunk/sandbox/jhb/oc2
- Files:
-
- 1 removed
- 5 modified
-
containerbase.py (modified) (1 diff)
-
documentation.py (modified) (16 diffs)
-
issuer.py (modified) (2 diffs)
-
protocols.py (deleted)
-
testserver.py (modified) (1 diff)
-
wallet.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/sandbox/jhb/oc2/containerbase.py
r269 r277 97 97 fields = [] 98 98 99 def __init__(self,data={},**kwargs): 100 if not data and kwargs: 101 data = kwargs 99 def __init__(self,data={}): 102 100 self.fromData(data) 103 104 def __xrepr__(self):105 return "<%s(%s)>" % (self.__class__.__name__,self.getData(True))106 107 def __xstr__(self):108 return self.toString()109 101 110 102 def fromData(self,data): -
trunk/sandbox/jhb/oc2/documentation.py
r276 r277 1 1 """ 2 ############################################################################### 2 3 Setup an issuer 3 4 … … 18 19 19 20 21 ############################################################################### 20 22 mint (regularily) creates keypairs (pP,sP) for all denominations and id(p). 21 23 Master key holder generates keys certificate … … 37 39 >>> wallet = Wallet({}) 38 40 >>> import protocols 39 >>> serverside = protocols.GiveLatestCDD(issuer) 40 >>> clientside = protocols.AskLatestCDD(serverside.run) 41 >>> cdd == clientside.run() 41 >>> cdd == wallet.askLatestCDD(issuer.giveLatestCDD) 42 42 True 43 43 … … 46 46 >>> import testserver 47 47 >>> transport = transports.HTTPTransport('http://localhost:%s/' % port) 48 >>> clientside = protocols.AskLatestCDD(transport)49 48 >>> testserver.run_once(port,issuer) 50 >>> cdd2 = clientside.run()49 >>> cdd2 = wallet.askLatestCDD(transport) 51 50 >>> cdd2.toString(True) == cdd.toString(True) 52 51 True … … 54 53 55 54 55 ############################################################################### 56 56 Wallet: fetches current public minting keys for denomination 57 57 58 >>> clientside = protocols.FetchMintKeys(transport,denominations=['1','5'])59 58 >>> testserver.run_once(port,issuer) 60 >>> mkcs = clientside.run()59 >>> mkcs = wallet.fetchMintKeys(transport,denominations=['1','5']) 61 60 >>> mkcs[0].toString() == issuer.getCurrentMKCs()['1'].toString() 62 61 True … … 80 79 81 80 81 ############################################################################### 82 82 Lets try to get a coin minted 83 83 … … 90 90 >>> mint.addAuthKey(authpub) 91 91 >>> authorizer.setMKCs(mkcs) 92 >>> clientside = protocols.TransferRequest(transport,tid,'foo',[[mkc.keyId,blind]],[])93 92 94 93 Lets have the authorizer denying the request 95 94 96 95 >>> authorizer.deny = True 97 >>> testserver.run_once(port, mint=mint,authorizer=authorizer)98 >>> clientside.run().header96 >>> testserver.run_once(port,issuer=issuer,mint=mint,authorizer=authorizer) 97 >>> wallet.requestTransfer(transport,tid,'foo',[[mkc.keyId,blind]],[]).header 99 98 'TransferReject' 100 99 … … 102 101 103 102 >>> authorizer.deny = False 104 >>> testserver.run_once(port, mint=mint,authorizer=authorizer)105 >>> response = clientside.run()103 >>> testserver.run_once(port,issuer=issuer,mint=mint,authorizer=authorizer) 104 >>> response = wallet.requestTransfer(transport,tid,'foo',[[mkc.keyId,blind]],[]) 106 105 >>> response.header 107 106 'TransferAccept' … … 123 122 124 123 >>> mint.delay = True 125 >>> testserver.run_once(port, mint=mint,authorizer=authorizer)126 >>> clientside.run().header124 >>> testserver.run_once(port,issuer=issuer,mint=mint,authorizer=authorizer) 125 >>> wallet.requestTransfer(transport,tid,'foo',[[mkc.keyId,blind]],[]).header 127 126 'TransferDelay' 128 127 … … 132 131 133 132 >>> issuer.delay = True 134 >>> clientside = protocols.TransferResume(transport,tid)135 133 >>> testserver.run_once(port,issuer=issuer) 136 >>> clientside.run().header134 >>> wallet.resumeTransfer(transport,tid).header 137 135 'TransferDelay' 138 136 >>> issuer.delay = False … … 140 138 So we need to resume 141 139 142 >>> clientside = protocols.TransferResume(transport,tid)143 140 >>> testserver.run_once(port,issuer=issuer) 144 >>> response = clientside.run() 145 >>> response.header 141 >>> wallet.resumeTransfer(transport,tid).header 146 142 'TransferAccept' 147 143 … … 156 152 157 153 154 ############################################################################### 158 155 Now, wallet to wallet. We setup an alice and a bob side. Alice announces 159 156 a sum, and bob dedices if he wants to accept it 160 157 158 >>> alice = wallet 161 159 >>> bobport = 9091 162 >>> bob wallet= Wallet({})160 >>> bob = Wallet({}) 163 161 >>> import test 164 >>> alicetid = wallet.makeSerial() 165 >>> bob = protocols.SumAnnounceListen(bobwallet) 166 >>> alice = protocols.SumAnnounce(bob.run, wallet, alicetid, 5, 'foobar') 167 >>> bobwallet.approval = "I don't like odd sums" 168 >>> alice.run() 162 >>> bob.approval = "I don't like odd sums" 163 >>> alicetid = alice.makeSerial() 164 >>> alice.announceSum(bob.listenSum, alicetid, 5, 'foobar') 169 165 "I don't like odd sums" 170 166 171 >>> bob wallet.approval = True172 >>> alice.run()167 >>> bob.approval = True 168 >>> wallet.announceSum(bob.listenSum, alicetid, 5, 'foobar') 173 169 True 174 170 … … 179 175 180 176 Lets have first a wrong transactionId 181 182 >>> bob = protocols.SpendListen(bobwallet)183 177 >>> #transport = transports.YieldTransport(bob.run,[]) 184 >>> alice = protocols.SpendRequest(bob.run, wallet, 'foobar', [coin]) 185 >>> alice.run() 178 >>> alice.requestSpend(bob.listenSpend,'foobar',[coin]) 186 179 Traceback (most recent call last): 187 180 .... … … 190 183 Or lets try to send a wrong amount 191 184 192 >>> alice = protocols.SpendRequest(bob.run, wallet, alicetid, []) 193 >>> alice.run() 185 >>> alice.requestSpend(bob.listenSpend,alicetid,[]) 194 186 Traceback (most recent call last): 195 187 .... 196 188 SpendReject: amount of coins does not match announced one 197 189 198 >>> alice = protocols.SpendRequest(bob.run, wallet, alicetid, [coin]) 199 >>> alice.run() 200 True 201 190 >>> alice.requestSpend(bob.listenSpend, alicetid, [coin]) 191 True 192 193 194 195 ############################################################################### 202 196 Now, lets first pretend we are on Bobs side. Fix that later, but assume we 203 197 received the coins, we know what cdd and mkc to use. We need to exchange now … … 205 199 >>> coins = [coin] 206 200 >>> key = mkc.publicKey 207 >>> bobblank = bob wallet._makeBlank(cdd,mkc)201 >>> bobblank = bob._makeBlank(cdd,mkc) 208 202 >>> bobsecret, bobblind = key.blindBlank(bobblank) 209 203 >>> blinds = [[mkc.keyId,bobblind]] 210 204 >>> bobtid = wallet.makeSerial() 211 205 212 >>> clientside = protocols.TransferRequest(transport,tid,blinds = blinds, coins = coins)213 206 >>> testserver.run_once(port,issuer=issuer,mint=mint) 214 >>> response = clientside.run()207 >>> response = bob.requestTransfer(transport,tid,blinds = blinds, coins = coins) 215 208 >>> bobblank.signature = key.unblind(bobsecret,response.signatures[0]) 216 209 >>> bobcoin = bobblank … … 221 214 222 215 >>> import messages 223 >>> bobblank = bob wallet._makeBlank(cdd,mkc)216 >>> bobblank = bob._makeBlank(cdd,mkc) 224 217 >>> bobsecret, bobblind = key.blindBlank(bobblank) 225 218 >>> blinds = [[mkc.keyId,bobblind]] 226 219 >>> bobtid = wallet.makeSerial() 227 >>> clientside = protocols.TransferRequest(transport,tid,blinds = blinds, coins = coins)228 220 >>> testserver.run_once(port,issuer=issuer,mint=mint) 229 >>> clientside.run().header221 >>> wallet.requestTransfer(transport,tid,blinds = blinds, coins = coins).header 230 222 'TransferReject' 231 223 232 224 233 225 226 ############################################################################### 234 227 Last step - bob wants to redeem the coins 235 228 236 229 >>> bobtid = wallet.makeSerial() 237 >>> clientside = protocols.TransferRequest(transport,tid,target='foo', coins = [bobcoin])238 230 >>> testserver.run_once(port,issuer=issuer,mint=mint) 239 >>> clientside.run().header231 >>> bob.requestTransfer(transport,tid,target='foo', coins = [bobcoin]).header 240 232 'TransferAccept' 241 233 -
trunk/sandbox/jhb/oc2/issuer.py
r266 r277 1 1 from entity import * 2 2 import datetime 3 import messages 3 4 4 5 class Issuer(Entity): … … 97 98 else: 98 99 return self.storage.setdefault('transactions',{}).get(transactionId,None) 100 101 def giveLatestCDD(self,message): 102 answer = messages.GiveLatestCDD() 103 answer.cdd = self.getCDD() 104 return answer 105 106 107 def giveMintKeys(self,message): 108 keys = [] 109 if message.denominations: 110 keyslist = self.getCurrentMKCs() 111 for d in message.denominations: 112 keys.append(keyslist.get(d)) 113 elif message.keyids: 114 for id in message.keyids: 115 keys.append(self.getKeyById(id)) 116 117 answer = messages.GiveMintKeys() 118 answer.keys = keys 119 return answer 120 121 def handleTransferRequest(self,mint,authorizer,message): 122 options = dict(message.options) 123 requesttype = options['type'] 124 125 if requesttype == 'mint': 126 authorizedMessage = authorizer.authorize(message) 127 if type(authorizedMessage) == messages.Error: 128 return messages.TransferReject() 129 else: 130 return mint.handleMintingRequest(authorizedMessage) 131 132 elif requesttype == 'exchange': 133 return mint.handleExchangeRequest(message) 134 135 elif requesttype == 'redeem': 136 return mint.handleRedeemRequest(message) 137 138 else: 139 return messages.TransferReject() 140 141 def resumeTransfer(self,message): 142 signatures = self.getTransactionResult(message.transactionId) 143 if signatures: 144 answer = messages.TransferAccept() 145 answer.signatures = signatures 146 else: 147 answer = messages.TransferDelay() 148 answer.transactionId = message.transactionId 149 answer.reason = 'issuer has no coins yet' 150 return answer 151 -
trunk/sandbox/jhb/oc2/testserver.py
r266 r277 14 14 message = transports.createMessage(data) 15 15 if message.header == 'AskLatestCDD': 16 protocol = protocols.GiveLatestCDD(self.issuer)16 answer = self.issuer.giveLatestCDD(message) 17 17 elif message.header == 'FetchMintKeys': 18 protocol = protocols.GiveMintKeys(self.issuer)18 answer = self.issuer.giveMintKeys(message) 19 19 elif message.header == 'TransferRequest': 20 protocol = protocols.TransferHandling(self.mint,self.authorizer)20 answer = self.issuer.handleTransferRequest(self.mint,self.authorizer,message) 21 21 elif message.header == 'TransferResume': 22 protocol = protocols.TransferResumeHandling(self.issuer)22 answer = self.issuer.resumeTransfer(message) 23 23 24 answer = protocol.run(message)25 24 self.send_response(200) 26 25 self.send_header("Content-type", "text/plain") -
trunk/sandbox/jhb/oc2/wallet.py
r272 r277 3 3 from container import * 4 4 import occrypto 5 import messages 5 6 6 7 class Wallet(Entity): … … 41 42 return approval 42 43 44 45 def askLatestCDD(self,transport): 46 response = transport(messages.AskLatestCDD()) 47 return response.cdd 48 49 50 def fetchMintKeys(self,transport,denominations=None,keyids=None): 51 if denominations and keyids: 52 raise "you can't ask for denominations and keyids at the same time" 53 if not (denominations or keyids): 54 raise "you need to ask at least for one" 43 55 56 message = messages.FetchMintKeys() 57 message.denominations = denominations 58 message.keyids = keyids 59 60 response = transport(message) 61 62 if response.header == 'MINTING_KEY_FAILURE': 63 raise message 64 else: 65 return response.keys 66 67 68 def requestTransfer(self,transport,transactionId,target=None,blinds=None,coins=None): 69 if target and blinds: 70 requesttype = 'mint' 71 elif target and coins: 72 requesttype = 'redeem' 73 elif blinds and coins: 74 requesttype = 'exchange' 75 else: 76 raise 'Not a valid combination of options' 77 78 message = messages.TransferRequest() 79 message.transactionId = transactionId 80 message.target = target 81 message.blinds = blinds 82 message.coins = coins 83 message.options = dict(type=requesttype).items() 84 85 response = transport(message) 86 return response 87 88 def resumeTransfer(self,transport,transactionId): 89 message = messages.TransferResume() 90 message.transactionId = transactionId 91 response = transport(message) 92 return response 93 94 95 def announceSum(self,transport,tid,amount,target): 96 message = messages.SumAnnounce() 97 message.transactionId = tid 98 message.amount = amount 99 message.target = target 100 self.addOutgoing(message) 101 response = transport(message) 102 if response.header == 'SumReject': 103 return response.reason 104 else: 105 return True 106 107 def listenSum(self,message): 108 approval = self.getApproval(message) 109 if approval == True: 110 answer = messages.SumAccept() 111 else: 112 answer = messages.SumReject() 113 answer.reason = approval 114 answer.transactionId = message.transactionId 115 return answer 116 117 def requestSpend(self,transport,tid,coins): 118 message = messages.SpendRequest() 119 message.transactionId = tid 120 message.coins = coins 121 response = transport(message) 122 if response.header == 'SpendReject': 123 raise response 124 else: 125 return True 126 127 def listenSpend(self,message): 128 tid = message.transactionId 129 amount = sum([int(m.denomination) for m in message.coins]) 130 #check transactionid 131 orig = self.getIncoming(tid) 132 if not orig: 133 answer = messages.SpendReject() 134 answer.reason = 'unknown transactionId' 135 return answer 136 #check sum 137 if amount != int(orig.amount): 138 answer = messages.SpendReject() 139 answer.reason = 'amount of coins does not match announced one' 140 return answer 141 #do exchange 142 143 144 answer = messages.SpendAccept() 145 answer.transactionId = tid 146 return answer 147
