def test_transaction_v2_invalid_signature(self): # noinspection PyDictCreation tx_dumped = { 'from': 'hx48cd6eb32339d5c719dcc0af21e9bc3b67d733e6', 'to': 'hx22f72e44141bedd50d1e536455682863d3d8a484', 'value': '0x186a0', 'fee': '0xf4240', 'timestamp': '1558679280067963', 'nonce': '1', 'tx_hash': '34477b3bc76fa73aad0258ba9fd36f28a3c4b26956c1e5eb92ddda7d98df4e32', # valid hash 'signature': 'W/hW/PAo+ExeSsreD//yJVgNqmnkWKs+m0VUqE11O7Ek82yEINuczLRXtj1k515q8Ep4OLsRPPiPNjDM9vuhsgE=' } tx_dumped['signature'] = Signature(os.urandom( Signature.size)).to_base64str() # invalid signature tx_version, tx_type = self.tx_versioner.get_version(tx_dumped) ts = TransactionSerializer.new(tx_version, tx_type, self.tx_versioner) tx = ts.from_(tx_dumped) tv = TransactionVerifier.new(tx_version, tx_type, self.tx_versioner) self.assertRaises(TransactionInvalidSignatureError, lambda: tv.verify(tx)) self.assertRaises(TransactionInvalidSignatureError, lambda: tv.pre_verify(tx))
def test_block_invalid_vote(self): ratio = 0.67 block_hash = Hash32(os.urandom(Hash32.size)) block_votes = BlockVotes(self.reps, ratio, 0, 0, block_hash) invalid_block_vote = BlockVote.new(self.signers[0], 0, 0, 1, block_hash) self.assertRaises(RuntimeError, block_votes.add_vote, invalid_block_vote) invalid_block_vote = BlockVote.new(self.signers[0], 0, 1, 0, block_hash) self.assertRaises(RuntimeError, block_votes.add_vote, invalid_block_vote) invalid_block_vote = BlockVote.new(self.signers[0], 0, 0, 0, Hash32(os.urandom(32))) self.assertRaises(RuntimeError, block_votes.add_vote, invalid_block_vote) invalid_block_vote = BlockVote(rep=self.reps[0], timestamp=0, signature=Signature(os.urandom(65)), block_height=0, round_=0, block_hash=block_hash) self.assertRaises(RuntimeError, block_votes.add_vote, invalid_block_vote) block_vote = BlockVote.new(self.signers[0], 0, 0, 0, block_hash) block_votes.add_vote(block_vote) duplicate_block_vote = BlockVote.new(self.signers[0], 0, 0, 0, Hash32.empty()) self.assertRaises(votes.VoteDuplicateError, block_votes.add_vote, duplicate_block_vote)
def sign(self): if self.signature is not None: return self.signature if self.hash is None: raise RuntimeError self.signature = Signature(self.signer.sign_hash(self.hash)) return self.signature
def sign_transaction(self, tx: 'Transaction'): if self.signer.address != tx.signer_address.hex_hx(): raise RuntimeError( f"Signer not match. {self.signer.address} != {tx.signer_address.hex_hx()}" ) signature = Signature(self.signer.sign_hash(tx.hash)) raw_data = dict(tx.raw_data) raw_data["signature"] = signature.to_base64str() return Transaction(raw_data=raw_data, hash=tx.hash, signature=signature, timestamp=tx.timestamp, from_address=tx.from_address, to_address=tx.to_address, value=tx.value, fee=tx.fee, nonce=tx.nonce)
def test_leader_invalid_vote(self): ratio = 0.67 old_leader = self.reps[0] new_leader = self.reps[1] leader_votes = LeaderVotes(self.reps, ratio, 0, 0, old_leader) invalid_leader_vote = LeaderVote.new(self.signers[0], 0, 1, 0, old_leader, new_leader) self.assertRaises(RuntimeError, leader_votes.add_vote, invalid_leader_vote) invalid_leader_vote = LeaderVote.new(self.signers[0], 0, 0, 1, old_leader, new_leader) self.assertRaises(RuntimeError, leader_votes.add_vote, invalid_leader_vote) invalid_leader_vote = LeaderVote.new(self.signers[0], 0, 0, 0, new_leader, new_leader) self.assertRaises(RuntimeError, leader_votes.add_vote, invalid_leader_vote) invalid_leader_vote = LeaderVote(rep=self.reps[0], timestamp=0, signature=Signature(os.urandom(65)), block_height=0, round_=0, new_leader=new_leader, old_leader=old_leader) self.assertRaises(RuntimeError, leader_votes.add_vote, invalid_leader_vote) leader_vote = LeaderVote.new(self.signers[0], 0, 0, 0, old_leader, new_leader) leader_votes.add_vote(leader_vote) duplicate_leader_vote = LeaderVote.new(self.signers[0], 0, 0, 0, old_leader, self.reps[2]) self.assertRaises(votes.VoteDuplicateError, leader_votes.add_vote, duplicate_leader_vote)
def test_transaction_v3_invalid_signature(self): # noinspection PyDictCreation tx_dumped = { 'version': '0x3', 'from': 'hx48cd6eb32339d5c719dcc0af21e9bc3b67d733e6', 'to': 'hxe0a231fa5c80e45f51d7df5f7d127954320df829', 'stepLimit': '0xf4240', 'timestamp': '0x5899c717f92f8', 'nid': '0x3', 'value': '0x186a0', 'nonce': '0x64', 'data': 'test', 'dataType': 'message', 'signature': 'J84KdBtQR4w1bcBdBGF8g6aNoCXjsY/5T6vGV4RXeMwEvafj9xVRDVjzF+vN1JVYvXrAzjlYPCiiBXBQe6+tRAE=' } tx_dumped['signature'] = Signature(os.urandom( Signature.size)).to_base64str() # invalid signature tx_version, tx_type = self.tx_versioner.get_version(tx_dumped) ts = TransactionSerializer.new(tx_version, tx_type, self.tx_versioner) tx = ts.from_(tx_dumped) tv = TransactionVerifier.new(tx_version, tx_type, self.tx_versioner) self.assertRaises(TransactionInvalidSignatureError, lambda: tv.verify(tx)) self.assertRaises(TransactionInvalidSignatureError, lambda: tv.pre_verify(tx, nid=3))
def new(cls, signer: Signer, timestamp: int, **kwargs): rep_id: ExternalAddress = ExternalAddress.fromhex(signer.address) hash_ = cls.to_hash(rep_id, timestamp, **kwargs) signature = Signature(signer.sign_hash(hash_)) return cls(rep_id, timestamp, signature, **kwargs)
def sign(self): if self.hash is None: self.build_hash() self.signature = Signature(self.signer.sign_hash(self.hash)) return self.signature