Skip to content

Commit

Permalink
Add has_enough_shares method
Browse files Browse the repository at this point in the history
  • Loading branch information
vxgmichel committed Jul 29, 2023
1 parent 859ca39 commit 873d143
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 4 additions & 2 deletions parsec/core/invite/claimer.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ def remaining_recipients(self) -> list[ShamirRecoveryRecipient]:
recipient for recipient in self.recipients if recipient.user_id not in self.recovered
]

def has_enough_shares(self) -> bool:
return len(self.shares) >= self.threshold

def get_initial_ctx(self, recipient: ShamirRecoveryRecipient) -> ShamirRecoveryClaimInitialCtx:
return ShamirRecoveryClaimInitialCtx(
recipient=recipient,
Expand All @@ -228,10 +231,9 @@ def get_initial_ctx(self, recipient: ShamirRecoveryRecipient) -> ShamirRecoveryC

def add_shares(self, recipient_user_id: UserID, shares: list[ShamirShare]) -> bool:
self.recovered[recipient_user_id] = shares
return len(self.shares) >= self.threshold
return self.has_enough_shares()

async def retrieve_recovery_device(self) -> LocalDevice:
assert len(self.shares) >= self.threshold
recovered_secret = ShamirRecoverySecret.load(
shamir_recover_secret(self.threshold, self.shares)
)
Expand Down
2 changes: 2 additions & 0 deletions tests/core/test_shamir.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ async def _run_first_alice_claimer():
alice_ctx = await alice_ctx.do_wait_peer_trust()
enough_shares = await alice_ctx.do_recover_share()
assert not enough_shares
assert not prelude_ctx.has_enough_shares()
assert len(prelude_ctx.shares) == 3
assert prelude_ctx.remaining_recipients == [adam_recipient]
alice_done_with_bob.set()
Expand Down Expand Up @@ -361,6 +362,7 @@ async def _run_first_alice_claimer():
alice_ctx = await alice_ctx.do_wait_peer_trust()
enough_shares = await alice_ctx.do_recover_share()
assert enough_shares
assert prelude_ctx.has_enough_shares()
assert len(prelude_ctx.shares) == 5
assert prelude_ctx.remaining_recipients == []
await adam_done_with_alice.wait()
Expand Down

0 comments on commit 873d143

Please sign in to comment.