Changeset 277

Show
Ignore:
Timestamp:
04/24/09 16:17:24 (3 years ago)
Author:
ocjhb
Message:

protocols removed, functionality now in wallet and issuer

Location:
trunk/sandbox/jhb/oc2
Files:
1 removed
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/sandbox/jhb/oc2/containerbase.py

    r269 r277  
    9797    fields = [] 
    9898 
    99     def __init__(self,data={},**kwargs): 
    100         if not data and kwargs: 
    101             data = kwargs 
     99    def __init__(self,data={}): 
    102100        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() 
    109101 
    110102    def fromData(self,data): 
  • trunk/sandbox/jhb/oc2/documentation.py

    r276 r277  
    11""" 
     2############################################################################### 
    23Setup an issuer 
    34 
     
    1819 
    1920 
     21############################################################################### 
    2022mint (regularily) creates keypairs (pP,sP) for all denominations and id(p).  
    2123Master key holder generates keys certificate 
     
    3739>>> wallet = Wallet({}) 
    3840>>> import protocols 
    39 >>> serverside = protocols.GiveLatestCDD(issuer) 
    40 >>> clientside = protocols.AskLatestCDD(serverside.run) 
    41 >>> cdd == clientside.run() 
     41>>> cdd == wallet.askLatestCDD(issuer.giveLatestCDD) 
    4242True 
    4343 
     
    4646>>> import testserver 
    4747>>> transport = transports.HTTPTransport('http://localhost:%s/' % port) 
    48 >>> clientside = protocols.AskLatestCDD(transport) 
    4948>>> testserver.run_once(port,issuer) 
    50 >>> cdd2 =  clientside.run() 
     49>>> cdd2 = wallet.askLatestCDD(transport) 
    5150>>> cdd2.toString(True) == cdd.toString(True) 
    5251True 
     
    5453 
    5554 
     55############################################################################### 
    5656Wallet: fetches current public minting keys for denomination 
    5757 
    58 >>> clientside = protocols.FetchMintKeys(transport,denominations=['1','5']) 
    5958>>> testserver.run_once(port,issuer) 
    60 >>> mkcs =  clientside.run() 
     59>>> mkcs = wallet.fetchMintKeys(transport,denominations=['1','5']) 
    6160>>> mkcs[0].toString() == issuer.getCurrentMKCs()['1'].toString() 
    6261True 
     
    8079 
    8180 
     81############################################################################### 
    8282Lets try to get a coin minted 
    8383 
     
    9090>>> mint.addAuthKey(authpub) 
    9191>>> authorizer.setMKCs(mkcs) 
    92 >>> clientside = protocols.TransferRequest(transport,tid,'foo',[[mkc.keyId,blind]],[]) 
    9392 
    9493Lets have the authorizer denying the request 
    9594 
    9695>>> authorizer.deny = True 
    97 >>> testserver.run_once(port,mint=mint,authorizer=authorizer) 
    98 >>> clientside.run().header 
     96>>> testserver.run_once(port,issuer=issuer,mint=mint,authorizer=authorizer) 
     97>>> wallet.requestTransfer(transport,tid,'foo',[[mkc.keyId,blind]],[]).header 
    9998'TransferReject' 
    10099 
     
    102101 
    103102>>> 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]],[]) 
    106105>>> response.header 
    107106'TransferAccept' 
     
    123122 
    124123>>> mint.delay = True 
    125 >>> testserver.run_once(port,mint=mint,authorizer=authorizer) 
    126 >>> clientside.run().header 
     124>>> testserver.run_once(port,issuer=issuer,mint=mint,authorizer=authorizer) 
     125>>> wallet.requestTransfer(transport,tid,'foo',[[mkc.keyId,blind]],[]).header 
    127126'TransferDelay' 
    128127 
     
    132131 
    133132>>> issuer.delay = True 
    134 >>> clientside = protocols.TransferResume(transport,tid) 
    135133>>> testserver.run_once(port,issuer=issuer) 
    136 >>> clientside.run().header 
     134>>> wallet.resumeTransfer(transport,tid).header 
    137135'TransferDelay' 
    138136>>> issuer.delay = False 
     
    140138So we need to resume 
    141139 
    142 >>> clientside = protocols.TransferResume(transport,tid) 
    143140>>> testserver.run_once(port,issuer=issuer) 
    144 >>> response = clientside.run() 
    145 >>> response.header 
     141>>> wallet.resumeTransfer(transport,tid).header 
    146142'TransferAccept' 
    147143 
     
    156152 
    157153 
     154############################################################################### 
    158155Now, wallet to wallet. We setup an alice and a bob side. Alice announces 
    159156a sum, and bob dedices if he wants to accept it 
    160157 
     158>>> alice = wallet 
    161159>>> bobport = 9091 
    162 >>> bobwallet = Wallet({}) 
     160>>> bob = Wallet({}) 
    163161>>> 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')  
    169165"I don't like odd sums" 
    170166 
    171 >>> bobwallet.approval = True 
    172 >>> alice.run() 
     167>>> bob.approval = True 
     168>>> wallet.announceSum(bob.listenSum, alicetid, 5, 'foobar')  
    173169True 
    174170 
     
    179175 
    180176Lets have first a wrong transactionId 
    181  
    182 >>> bob = protocols.SpendListen(bobwallet) 
    183177>>> #transport = transports.YieldTransport(bob.run,[]) 
    184 >>> alice = protocols.SpendRequest(bob.run, wallet, 'foobar', [coin])  
    185 >>> alice.run() 
     178>>> alice.requestSpend(bob.listenSpend,'foobar',[coin]) 
    186179Traceback (most recent call last): 
    187180    .... 
     
    190183Or lets try to send a wrong amount 
    191184 
    192 >>> alice = protocols.SpendRequest(bob.run, wallet, alicetid, [])  
    193 >>> alice.run() 
     185>>> alice.requestSpend(bob.listenSpend,alicetid,[]) 
    194186Traceback (most recent call last): 
    195187    .... 
    196188SpendReject: amount of coins does not match announced one 
    197189 
    198 >>> alice = protocols.SpendRequest(bob.run, wallet, alicetid, [coin])  
    199 >>> alice.run() 
    200 True 
    201  
     190>>> alice.requestSpend(bob.listenSpend, alicetid, [coin])  
     191True 
     192 
     193 
     194 
     195############################################################################### 
    202196Now, lets first pretend we are on Bobs side. Fix that later, but assume we  
    203197received the coins, we know what cdd and mkc to use. We need to exchange now 
     
    205199>>> coins = [coin] 
    206200>>> key = mkc.publicKey 
    207 >>> bobblank = bobwallet._makeBlank(cdd,mkc) 
     201>>> bobblank = bob._makeBlank(cdd,mkc) 
    208202>>> bobsecret, bobblind = key.blindBlank(bobblank) 
    209203>>> blinds = [[mkc.keyId,bobblind]] 
    210204>>> bobtid = wallet.makeSerial() 
    211205 
    212 >>> clientside = protocols.TransferRequest(transport,tid,blinds = blinds, coins = coins) 
    213206>>> testserver.run_once(port,issuer=issuer,mint=mint) 
    214 >>> response = clientside.run() 
     207>>> response = bob.requestTransfer(transport,tid,blinds = blinds, coins = coins) 
    215208>>> bobblank.signature = key.unblind(bobsecret,response.signatures[0]) 
    216209>>> bobcoin = bobblank 
     
    221214 
    222215>>> import messages 
    223 >>> bobblank = bobwallet._makeBlank(cdd,mkc) 
     216>>> bobblank = bob._makeBlank(cdd,mkc) 
    224217>>> bobsecret, bobblind = key.blindBlank(bobblank) 
    225218>>> blinds = [[mkc.keyId,bobblind]] 
    226219>>> bobtid = wallet.makeSerial() 
    227 >>> clientside = protocols.TransferRequest(transport,tid,blinds = blinds, coins = coins) 
    228220>>> testserver.run_once(port,issuer=issuer,mint=mint) 
    229 >>> clientside.run().header 
     221>>> wallet.requestTransfer(transport,tid,blinds = blinds, coins = coins).header 
    230222'TransferReject' 
    231223 
    232224 
    233225 
     226############################################################################### 
    234227Last step - bob wants to redeem the coins 
    235228 
    236229>>> bobtid = wallet.makeSerial() 
    237 >>> clientside = protocols.TransferRequest(transport,tid,target='foo', coins = [bobcoin]) 
    238230>>> testserver.run_once(port,issuer=issuer,mint=mint) 
    239 >>> clientside.run().header 
     231>>> bob.requestTransfer(transport,tid,target='foo', coins = [bobcoin]).header 
    240232'TransferAccept' 
    241233 
  • trunk/sandbox/jhb/oc2/issuer.py

    r266 r277  
    11from entity import * 
    22import datetime 
     3import messages 
    34 
    45class Issuer(Entity): 
     
    9798        else:             
    9899            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  
    1414        message = transports.createMessage(data) 
    1515        if message.header == 'AskLatestCDD': 
    16             protocol = protocols.GiveLatestCDD(self.issuer) 
     16            answer = self.issuer.giveLatestCDD(message) 
    1717        elif message.header == 'FetchMintKeys': 
    18             protocol = protocols.GiveMintKeys(self.issuer) 
     18            answer = self.issuer.giveMintKeys(message) 
    1919        elif message.header == 'TransferRequest': 
    20             protocol = protocols.TransferHandling(self.mint,self.authorizer) 
     20            answer = self.issuer.handleTransferRequest(self.mint,self.authorizer,message) 
    2121        elif message.header == 'TransferResume': 
    22             protocol = protocols.TransferResumeHandling(self.issuer) 
     22            answer = self.issuer.resumeTransfer(message) 
    2323  
    24         answer = protocol.run(message) 
    2524        self.send_response(200) 
    2625        self.send_header("Content-type", "text/plain") 
  • trunk/sandbox/jhb/oc2/wallet.py

    r272 r277  
    33from container import * 
    44import occrypto 
     5import messages 
    56 
    67class Wallet(Entity): 
     
    4142        return approval 
    4243         
     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" 
    4355         
     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