Permalink
Browse files

Update TRANSFER_TOKEN_ACCEPT to return a list as a second argument. P…

…erform checks on the list as needed.

Start filling out logic of wallet getting information from protocols it has run
Change sums to strings
  • Loading branch information...
ocmathew
ocmathew committed Mar 1, 2008
1 parent 2693845 commit ecde30f6f5c00e4bf0381ee4df8cfbd3eba5552f
Showing with 72 additions and 31 deletions.
  1. +23 −11 pyopencoin/oc/entities.py
  2. +45 −16 pyopencoin/oc/protocols.py
  3. +4 −4 pyopencoin/oc/tests.py
@@ -23,10 +23,11 @@ def getTime():
#################### Wallet ###############################
class Wallet(Entity):
"Just a testwallet. Does nothing, really"
"A Wallet"
def __init__(self):
self.coins = []
self.waitingTransfers = []
self.getTime = getTime
@@ -41,27 +42,38 @@ def sendMoney(self,transport):
protocol = protocols.WalletSenderProtocol(self)
transport.setProtocol(protocol)
transport.start()
transport.start()
#Trigger execution of the protocol
protocol.newMessage(Message(None))
protocol.newMessage(Message(None))
def receiveMoney(self,transport):
protocol = protocols.WalletRecipientProtocol(self)
transport.setProtocol(protocol)
transport.start()
def sendCoins(self,transport,amount,target,coins=None):
#FIXME: Doing a broken thing and using something from tests!
if not coins:
from tests import coins as testcoins
coin1 = testcoins[0][0] # denomination of 1
coin2 = testcoins[1][0] # denomination of 2
coins = [coin1,coin2]
protocol = protocols.CoinSpendSender(coins,target)
def sendCoins(self,transport,target,amount=None,coins=None):
"""sendCoins sends coins over a transport to a target of a certain amount or using given coins."""
# The coins argument for sendCoins is being phased out. Need to make the amount part work first though.
if coins: #FIXME: phase this out
touse = coins
amount = sum(coins)
elif amount:
#FIXME: get coins
touse = None
else:
raise Exception()
protocol = protocols.CoinSpendSender(touse,target)
transport.setProtocol(protocol)
transport.start()
protocol.newMessage(Message(None))
if protocol.state == protocol.finish:
# FIXME: remove coins
if not protocol.coins:
raise Exception()
pass
def listen(self,transport):
"""
>>> import transports
@@ -63,6 +63,7 @@ def initiateHandshake(self,message):
self.newState(self.firstStep)
return Message('HANDSHAKE',{'protocol': 'opencoin 1.0'})
ProtocolErrorMessage = lambda: Message('PROTOCOL_ERROR', 'send again')
class answerHandshakeProtocol(Protocol):
@@ -108,7 +109,7 @@ class CoinSpendSender(Protocol):
>>> css.state(Message(None))
<Message('HANDSHAKE',{'protocol': 'opencoin 1.0'})>
>>> css.state(Message('HANDSHAKE_ACCEPT'))
<Message('SUM_ANNOUNCE',['...', 3, 'foobar'])>
<Message('SUM_ANNOUNCE',['...', '3', 'foobar'])>
>>> css.state(Message('SUM_ACCEPT'))
<Message('COIN_SPEND',['...', [[(...)], [(...)]], 'foobar'])>
>>> css.state(Message('COIN_ACCEPT'))
@@ -122,25 +123,26 @@ def __init__(self,coins,target):
self.amount = sum(coins)
self.target = target
import crypto,base64
id = crypto._r.getRandomString(128) #_r is an instance of Random() (FIXME: should it be namd something else?)
self.transaction_id = base64.b64encode(id)
import base64
from crypto import _r as Random
self.transaction_id = Random.getRandomString(128)
self.encoded_transaction_id = base64.b64encode(self.transaction_id)
Protocol.__init__(self)
self.state = self.initiateHandshake
def firstStep(self,message):
self.state = self.spendCoin
return Message('SUM_ANNOUNCE',[self.transaction_id,
self.amount,
return Message('SUM_ANNOUNCE',[self.encoded_transaction_id,
str(self.amount),
self.target])
def spendCoin(self,message):
if message.type == 'SUM_ACCEPT':
self.state = self.goodbye
jsonCoins = [c.toPython() for c in self.coins]
return Message('COIN_SPEND',[self.transaction_id,
return Message('COIN_SPEND',[self.encoded_transaction_id,
jsonCoins,
self.target])
@@ -158,7 +160,7 @@ class CoinSpendRecipient(Protocol):
>>> coin2 = coins[1][0].toPython() # Denomination of 2
>>> w = entities.Wallet()
>>> csr = CoinSpendRecipient(w)
>>> csr.state(Message('SUM_ANNOUNCE',['123',3,'a book']))
>>> csr.state(Message('SUM_ANNOUNCE',['123','3','a book']))
<Message('SUM_ACCEPT',None)>
>>> csr.state(Message('COIN_SPEND',['123', [coin1, coin2], 'a book']))
<Message('COIN_ACCEPT',None)>
@@ -178,7 +180,7 @@ def __init__(self,wallet,issuer_transport = None):
def start(self,message):
if message.type == 'SUM_ANNOUNCE':
self.transaction_id = message.data[0]
self.sum = message.data[1]
self.sum = int(message.data[1])
self.target = message.data[2]
#get some feedback from interface somehow
action = self.wallet.confirmReceiveCoins('the other wallet id',self.sum,self.target)
@@ -240,11 +242,12 @@ class TransferTokenSender(Protocol):
<Message('HANDSHAKE',{'protocol': 'opencoin 1.0'})>
>>> tts.state(Message('HANDSHAKE_ACCEPT'))
<Message('TRANSFER_TOKEN_REQUEST',['...', 'my account', [], [[(...)], [(...)]], [['type', 'redeem']]])>
>>> tts.state(Message('TRANSFER_TOKEN_ACCEPT',3))
>>> tts.state(Message('TRANSFER_TOKEN_ACCEPT',[tts.transaction_id, []]))
<Message('GOODBYE',None)>
"""
# NOTE: transaction_id is kept in it's base64 form in this class
def __init__(self,target,blanks,coins,**kwargs):
import base64
@@ -271,10 +274,35 @@ def firstStep(self,message):
return Message('TRANSFER_TOKEN_REQUEST',data)
def goodbye(self,message):
if message.type == 'TRANSFER_TOKEN_ACCEPT':
self.result = 1
else:
self.result = 0
import base64
try:
if message.type == 'TRANSFER_TOKEN_ACCEPT':
transaction_id, blinds = message.data
if transaction_id != self.transaction_id:
return Message('PROTOCOL_ERROR', 'incorrect transaction_id')
if self.kwargs['type'] == 'exchange' or self.kwargs['type'] == 'mint':
if len(blinds) == 0:
return ProtocolErrorMessage()
try:
self.blinds = [base64.b64decode(blind) for blind in blinds]
except TypeError:
return ProtocolErrorMessage()
else:
if len(blinds) != 0:
return ProtocolErrorMessage()
self.result = 1
else:
self.result = 0
except ValueError:
return ProtocolErrorMessage()
self.state = self.finish
return Message('GOODBYE')
@@ -309,7 +337,7 @@ class TransferTokenRecipient(Protocol):
<Message('TRANSFER_TOKEN_REJECT',['1234', 'Token', 'See detail', ['Rejected']])>
>>> ttr.state(Message('TRANSFER_TOKEN_REQUEST',['1234', 'my account', [], [coin1, coin2], [['type', 'redeem']]]))
<Message('TRANSFER_TOKEN_ACCEPT',['1234', '3'])>
<Message('TRANSFER_TOKEN_ACCEPT',['1234', []])>
Try to double spend. Should not work.
>>> ttr.state = ttr.start
@@ -399,7 +427,8 @@ def start(self,message):
return Message('PROTOCOL_ERROR', 'send again')
self.state = self.goodbye
return Message('TRANSFER_TOKEN_ACCEPT',[encoded_transaction_id, str(sum(coins))])
return Message('TRANSFER_TOKEN_ACCEPT',[encoded_transaction_id, []])
elif options['type'] == 'mint':
#check that we have the keys
@@ -44,7 +44,7 @@
Client <Message('HANDSHAKE',{'protocol': 'opencoin 1.0'})>
Server <Message('HANDSHAKE_ACCEPT',None)>
Client <Message('TRANSFER_TOKEN_REQUEST',['...', 'myaccount', [], [[(...)], [(...)]], [['type', 'redeem']]])>
Server <Message('TRANSFER_TOKEN_ACCEPT',['...', '3'])>
Server <Message('TRANSFER_TOKEN_ACCEPT',['...', []])>
Client <Message('GOODBYE',None)>
Server <Message('GOODBYE',None)>
Client <Message('finished',None)>
@@ -56,16 +56,16 @@
>>> t = ClientTest(walletB.listen,clientnick='walletA',servernick='walletB')
>>> t2 = ClientTest(issuer.listen,clientnick='walletB',servernick='issuer')
>>> walletB.issuer_transport = t2
>>> walletA.sendCoins(t,amount=1,target='a book',coins = [coinB])
>>> walletA.sendCoins(t,amount=1,target='a book',coins = [coin1])
walletA <Message('HANDSHAKE',{'protocol': 'opencoin 1.0'})>
walletB <Message('HANDSHAKE_ACCEPT',None)>
walletA <Message('SUM_ANNOUNCE',['...', 1, 'a book'])>
walletA <Message('SUM_ANNOUNCE',['...', '1', 'a book'])>
walletB <Message('SUM_ACCEPT',None)>
walletA <Message('COIN_SPEND',['...', [[(...)]], 'a book'])>
walletB <Message('HANDSHAKE',{'protocol': 'opencoin 1.0'})>
issuer <Message('HANDSHAKE_ACCEPT',None)>
walletB <Message('TRANSFER_TOKEN_REQUEST',['...', 'my account', [], [[(...)]], [['type', 'redeem']]])>
issuer <Message('TRANSFER_TOKEN_ACCEPT',['...', '1'])>
issuer <Message('TRANSFER_TOKEN_ACCEPT',['...', []])>
walletB <Message('GOODBYE',None)>
issuer <Message('GOODBYE',None)>
walletB <Message('finished',None)>

0 comments on commit ecde30f

Please sign in to comment.