【发布时间】:2026-02-18 13:30:02
【问题描述】:
我一直在从基础学习区块链,并逐行研究TPCoin python代码。 在查看代码时,我发现没有验证方法可以防止双花问题或拒绝签名无效的交易请求。
我正在研究以下文章: https://morioh.com/p/7bfc126c22c2
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
我认为 Transaction 类中应该有一种验证功能......但是 不太确定该怎么做。 我希望看到一些关于如何处理这个问题的绝妙想法。
【问题讨论】:
标签: python blockchain bitcoin