Ejemplo n.º 1
0
	def processTransactionPayload(self, payload):
		peerSignature = payload[0]
		T2 = MultiSigTransaction.deserialize(payload[1])

		pubKey1, pubKey2 = self.getPublicKeyPair()
		if not verifyMultiSigSignature(
			T2.transaction, 0, [pubKey1, pubKey2], self.peerKey, peerSignature):
				raise Exception("Signature failure!") #TODO: what to do now?

		#TODO: lots of checks on T2 (IMPORTANT!)

		self.peerSignature = peerSignature
		self.T2_latest = T2
		self.T2_peerSigned = copy.deepcopy(T2)
Ejemplo n.º 2
0
	def makeWithdrawMessage(self, message):
		self.checkT1()

		#TODO: make some code to enter the "stopping" state prior to withdrawing,
		#and then wait until there are no more ongoing transactions.
		#This is not necessary for low-volume tests in controlled environments,
		#but as soon as people start transferring other peoples' transactions,
		#you want to be able to stop other peoples' transactions from
		#interfering with your withdrawal.
		if self.stage == stages["Ready"]:
			self.stage = stages["Stopping"]

		self.checkStopped()

		if message == None:
			if self.stage != stages["Stopped"]:
				raise Exception("Can not withdraw: channel must be Stopped, but is " + \
					stageNames[self.stage])

			self.makeWithdrawT2()
			self.stage = stages["OwnWithdraw_SendingT2"]
			return messages.Withdraw(
				self.ID, stage=self.stage, payload=[self.T2_latest.serialize()])

		elif self.stage == stages["Stopped"] and \
			message.stage == stages["OwnWithdraw_SendingT2"]:

			#Received T2
			self.T2_latest = MultiSigTransaction.deserialize(message.payload[0])
			#TODO: maybe re-serialize to check consistency
			#TODO: lots of checks on T2 (IMPORTANT!)
			pubKey1, pubKey2 = self.getPublicKeyPair()
			signature = signMultiSigTransaction(
				self.T2_latest.transaction, 0, [pubKey1, pubKey2], self.ownKey)
			self.stage = stages["PeerWithdraw_SendingSignature"]
			return messages.Withdraw(
				self.ID, stage=self.stage, payload=[signature])

		elif self.stage == stages["OwnWithdraw_SendingT2"] and \
			message.stage == stages["PeerWithdraw_SendingSignature"]:

			peerSignature = message.payload[0]
			pubKey1, pubKey2 = self.getPublicKeyPair()
			if not verifyMultiSigSignature(
				self.T2_latest.transaction, 0,
				[pubKey1, pubKey2], self.peerKey, peerSignature):
					raise Exception("Signature failure!") #TODO: what to do now?

			ownSignature = signMultiSigTransaction(
				self.T2_latest.transaction, 0, [pubKey1, pubKey2], self.ownKey)

			if self.hasFirstPublicKey:
				applyMultiSigSignatures(self.T2_latest.transaction, ownSignature, peerSignature)
			else:
				applyMultiSigSignatures(self.T2_latest.transaction, peerSignature, ownSignature)

			self.peerSignature = peerSignature
			self.T2_peerSigned = copy.deepcopy(self.T2_latest)
			self.stage = stages["Closed"]

			#Publish T2 in Bitcoind
			T2_serialized = self.T2_latest.transaction.serialize()
			self.bitcoind.sendRawTransaction(T2_serialized)

			return messages.Withdraw(
				self.ID, stage=self.stage, payload=[T2_serialized])

		elif self.stage == stages["PeerWithdraw_SendingSignature"] and \
			message.stage == stages["Closed"]:

			T2 = bitcointransaction.Transaction.deserialize(message.payload[0])
			#TODO: maybe re-serialize to check consistency
			#TODO: lots of checks on T2 (IMPORTANT!)

			#TODO:
			#self.T2_latest = T2
			#self.peerSignature = TODO
			#self.T2_peerSigned = copy.deepcopy(T2)
			self.stage = stages["Closed"]

			#Publish T2 in Bitcoind
			T2_serialized = message.payload[0]
			self.bitcoind.sendRawTransaction(T2_serialized)

			print "DONE"

		else:
			raise Exception("Received illegal withdraw message")

		return None
Ejemplo n.º 3
0
	def makeDepositMessage(self, message):
		if self.stage == stages["OwnDeposit_Initial"] and \
			message == None:

			#Initial message:
			self.stage = stages["OwnDeposit_SendingPublicKey"]
			return messages.Deposit(
				self.ID, self.getType(), isInitial=True, stage=self.stage,
				payload=[self.ownKey.getPublicKey(), self.escrowKey.getPublicKey()])

		elif self.stage == stages["PeerDeposit_Initial"] and \
			message.stage == stages["OwnDeposit_SendingPublicKey"]:

			#Received deposit message with public key from peer
			self.peerKey = crypto.Key()
			self.peerKey.setPublicKey(message.payload[0])
			self.escrowKey = crypto.Key()
			self.escrowKey.setPublicKey(message.payload[1])
			#TODO: check whether the escrow key is accepted
			self.stage = stages["PeerDeposit_SendingPublicKey"]
			return messages.Deposit(
				self.ID, self.getType(), stage=self.stage,
				payload=[self.ownKey.getPublicKey()])

		elif self.stage == stages["OwnDeposit_SendingPublicKey"] and \
			message.stage == stages["PeerDeposit_SendingPublicKey"]:

			#Received reply on own deposit message
			self.peerKey = crypto.Key()
			self.peerKey.setPublicKey(message.payload[0])
			self.makeT1AndT2()
			self.stage = stages["OwnDeposit_SendingT2"]
			return messages.Deposit(
				self.ID, self.getType(), stage=self.stage,
				payload=[self.T2_latest.serialize()])

		elif self.stage == stages["PeerDeposit_SendingPublicKey"] and \
			message.stage == stages["OwnDeposit_SendingT2"]:

			#Received T2
			self.T2_latest = MultiSigTransaction.deserialize(message.payload[0])
			#TODO: maybe re-serialize to check consistency
			self.amountRemote = sum(tx.amount for tx in self.T2_latest.transaction.tx_out)
			assert not self.hasFirstPublicKey
			signature = signMultiSigTransaction(
				self.T2_latest.transaction, 0,
				[self.peerKey.getPublicKey(), self.ownKey.getPublicKey()],
				self.ownKey)
			self.stage = stages["PeerDeposit_SendingSignature"]
			return messages.Deposit(
				self.ID, self.getType(), stage=self.stage, payload=[signature])

		elif self.stage == stages["OwnDeposit_SendingT2"] and \
			message.stage == stages["PeerDeposit_SendingSignature"]:

			signature = message.payload[0]
			assert self.hasFirstPublicKey
			if not verifyMultiSigSignature(
				self.T2_latest.transaction, 0,
				[self.ownKey.getPublicKey(), self.peerKey.getPublicKey()],
				self.peerKey, signature):
					raise Exception("Signature failure!") #TODO: what to do now?
			self.peerSignature = signature
			self.T2_peerSigned = copy.deepcopy(self.T2_latest)
			T1_serialized = self.T1.serialize()
			self.stage = stages["WaitingForT1"]

			#Publish T1 in Bitcoind
			self.bitcoind.sendRawTransaction(T1_serialized)

			return messages.Deposit(
				self.ID, self.getType(), stage=self.stage, payload=[T1_serialized])

		elif self.stage == stages["PeerDeposit_SendingSignature"] and \
			message.stage == stages["WaitingForT1"]:

			T1_serialized = message.payload[0]
			self.T1 = bitcointransaction.Transaction.deserialize(T1_serialized)
			#TODO: maybe re-serialize to check consistency
			#TODO: check T1 (and that it matches T2)

			self.stage = stages["WaitingForT1"]

			#Publish T1 in Bitcoind
			self.bitcoind.sendRawTransaction(T1_serialized)

			print "DONE"

		else:
			raise Exception("Received illegal deposit message")

		return None