I am not new to Blockchain technology and have been studying the TPCoin python codes line by line. However, when I was looking into the code, I found that there is no validation method that will prevent the double spending issues. Have shared my code below:-
class Transaction:
def __init__( self, sender, recipient, value ):
self.sender = sender
self.recipient = recipient
self.value = value
self.time = datetime.datetime.now()
self.signer = ""
def to_dict( self ):
if self.sender == "Genesis":
identity = "Genesis"
else:
identity = self.sender.identity
return collections.OrderedDict( { 'sender': identity, 'recipient': self.recipient, 'value': self.value, 'time' : self.time } )
def sign_transaction( self ):
private_key = self.sender._private_key
signer = PKCS1_v1_5.new( private_key )
h = SHA.new( str( self.to_dict( ) ).encode( 'utf8' ) )
self.signer = binascii.hexlify( signer.sign( h ) ).decode( 'ascii' )
return self.signer
def display_transaction( self ):
dict = self.to_dict( )
print ("sender: " + dict['sender'])
print ('-----')
print ("recipient: " + dict['recipient'])
print ('-----')
print ("value: " + str(dict['value']))
print ('-----')
print ("time: " + str(dict['time']))
print ('-----')
print ("signature: " + self.signer)
print ('-----')
def validate_transaction( self ):
### Double spending? Signature Verification?
return
I think there should be a sort of validation function within the Transaction class...but not quite sure what to do. I want a few ideas on how to handle this which will be well appreciated!!