Skip to content

Commit

Permalink
Dedupe signatures in build_and_sign
Browse files Browse the repository at this point in the history
Users can sometimes pass same signing keys, which leads to duplicated signatures, resulting the actual fee to be greater than estimated fee. This commit will remove deplicated signatures and therefore eliminate this type of issue.
  • Loading branch information
cffls committed Mar 17, 2023
1 parent fe977af commit 8f17630
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pycardano/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ def __eq__(self, other):
def __repr__(self) -> str:
return self.to_json()

def __hash__(self):
return hash(self.payload)


class SigningKey(Key):
def sign(self, data: bytes) -> bytes:
Expand Down
2 changes: 1 addition & 1 deletion pycardano/txbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,7 @@ def build_and_sign(
witness_set = self.build_witness_set()
witness_set.vkey_witnesses = []

for signing_key in signing_keys:
for signing_key in set(signing_keys):
signature = signing_key.sign(tx_body.hash())
witness_set.vkey_witnesses.append(
VerificationKeyWitness(signing_key.to_verification_key(), signature)
Expand Down
15 changes: 15 additions & 0 deletions test/pycardano/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,18 @@ def test_key_save():
SK.save(f.name)
sk = PaymentSigningKey.load(f.name)
assert SK == sk


def test_key_hash():
sk = PaymentSigningKey.generate()
vk = PaymentVerificationKey.from_signing_key(sk)

sk_set = set()
vk_set = set()

for _ in range(2):
sk_set.add(sk)
vk_set.add(vk)

assert len(sk_set) == 1
assert len(vk_set) == 1

0 comments on commit 8f17630

Please sign in to comment.