Skip to content

Commit

Permalink
Update DKG states based on last minute changes to Coordinator contrac…
Browse files Browse the repository at this point in the history
…t. (#398)
  • Loading branch information
derekpierre authored Nov 17, 2023
2 parents 202f060 + 9720e26 commit f92f9f9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"dependencies": {
"@ethersproject/abi": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@nucypher/nucypher-contracts": "0.10.1",
"@nucypher/nucypher-contracts": "0.12.0",
"@nucypher/nucypher-core": "0.13.0-alpha.1",
"axios": "^1.6.0",
"deep-equal": "^2.2.1",
Expand Down
11 changes: 6 additions & 5 deletions packages/shared/src/contracts/agents/coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ export type DkgParticipant = {

export enum DkgRitualState {
NON_INITIATED,
AWAITING_TRANSCRIPTS,
AWAITING_AGGREGATIONS,
TIMEOUT,
INVALID,
FINALIZED,
DKG_AWAITING_TRANSCRIPTS,
DKG_AWAITING_AGGREGATIONS,
DKG_TIMEOUT,
DKG_INVALID,
ACTIVE,
EXPIRED,
}

export class DkgCoordinatorAgent {
Expand Down
4 changes: 2 additions & 2 deletions packages/taco/src/dkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ export class DkgClient {
);
}

public static async getFinalizedRitual(
public static async getActiveRitual(
provider: ethers.providers.Provider,
domain: Domain,
ritualId: number,
): Promise<DkgRitual> {
const ritual = await DkgClient.getRitual(provider, domain, ritualId);
if (ritual.state !== DkgRitualState.FINALIZED) {
if (ritual.state !== DkgRitualState.ACTIVE) {
throw new Error(ERR_RITUAL_NOT_FINALIZED(ritualId, ritual));
}
return ritual;
Expand Down
16 changes: 6 additions & 10 deletions packages/taco/src/taco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { DkgClient } from './dkg';
import { retrieveAndDecrypt } from './tdec';

/**
* Encrypts a message under given conditions using a public key from a finalized DKG ritual.
* Encrypts a message under given conditions using a public key from an active DKG ritual.
*
* @export
* @param {ethers.providers.Provider} provider - Instance of ethers provider which is used to interact with
Expand All @@ -42,7 +42,7 @@ import { retrieveAndDecrypt } from './tdec';
* @returns {Promise<ThresholdMessageKit>} Returns Promise that resolves with an instance of ThresholdMessageKit.
* It represents the encrypted message.
*
* @throws {Error} If the finalized DKG Ritual cannot be retrieved an error is thrown.
* @throws {Error} If the active DKG Ritual cannot be retrieved an error is thrown.
*/
export const encrypt = async (
provider: ethers.providers.Provider,
Expand All @@ -64,11 +64,7 @@ export const encrypt = async (
// // Given that we just initialized the ritual, this should never happen
// throw new Error('Ritual ID is undefined');
// }
const dkgRitual = await DkgClient.getFinalizedRitual(
provider,
domain,
ritualId,
);
const dkgRitual = await DkgClient.getActiveRitual(provider, domain, ritualId);

return await encryptWithPublicKey(
message,
Expand All @@ -85,7 +81,7 @@ export const encrypt = async (
* @param {Uint8Array | string} message - The message to be encrypted.
* @param {Condition} condition - Condition under which the message will be encrypted. Those conditions must be
* satisfied in order to decrypt the message.
* @param {DkgPublicKey} dkgPublicKey - The public key of a finalized DKG Ritual to be used for encryption
* @param {DkgPublicKey} dkgPublicKey - The public key of an active DKG Ritual to be used for encryption
* @param {ethers.Signer} authSigner - The signer that will be used to sign the encrypter authorization.
*
* @returns {Promise<ThresholdMessageKit>} Returns Promise that resolves with an instance of ThresholdMessageKit.
Expand Down Expand Up @@ -138,7 +134,7 @@ export const encryptWithPublicKey = async (
*
* @returns {Promise<Uint8Array>} Returns Promise that resolves with a decrypted message
*
* @throws {Error} If the finalized DKG Ritual cannot be retrieved or decryption process throws an error,
* @throws {Error} If the active DKG Ritual cannot be retrieved or decryption process throws an error,
* an error is thrown.
*/
export const decrypt = async (
Expand All @@ -158,7 +154,7 @@ export const decrypt = async (
domain,
messageKit.acp.publicKey,
);
const ritual = await DkgClient.getFinalizedRitual(provider, domain, ritualId);
const ritual = await DkgClient.getActiveRitual(provider, domain, ritualId);
return retrieveAndDecrypt(
provider,
domain,
Expand Down
6 changes: 3 additions & 3 deletions packages/taco/test/taco.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { conditions, domains, toBytes } from '../src';
import {
fakeDkgRitual,
mockDkgParticipants,
mockGetFinalizedRitual,
mockGetActiveRitual,
mockGetParticipants,
mockMakeSessionKey,
} from './test-utils';
Expand All @@ -44,7 +44,7 @@ describe('taco', () => {
const mockedDkgRitual = fakeDkgRitual(mockedDkg);
const provider = fakeProvider(aliceSecretKeyBytes);
const signer = fakeSigner(aliceSecretKeyBytes);
const getFinalizedRitualSpy = mockGetFinalizedRitual(mockedDkgRitual);
const getFinalizedRitualSpy = mockGetActiveRitual(mockedDkgRitual);

const messageKit = await taco.encrypt(
provider,
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('taco', () => {
const getRitualIdFromPublicKey = mockGetRitualIdFromPublicKey(
mockedDkg.ritualId,
);
const getRitualSpy = mockGetFinalizedRitual(mockedDkgRitual);
const getRitualSpy = mockGetActiveRitual(mockedDkgRitual);

const decryptedMessage = await taco.decrypt(
provider,
Expand Down
6 changes: 3 additions & 3 deletions packages/taco/test/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export const fakeDkgRitual = (ritual: {
ritual.dkg.publicKey(),
ritual.sharesNum,
ritual.threshold,
DkgRitualState.FINALIZED,
DkgRitualState.ACTIVE,
);
};

Expand All @@ -181,8 +181,8 @@ export const mockGetRitual = (): SpyInstance => {
});
};

export const mockGetFinalizedRitual = (dkgRitual: DkgRitual): SpyInstance => {
return vi.spyOn(DkgClient, 'getFinalizedRitual').mockImplementation(() => {
export const mockGetActiveRitual = (dkgRitual: DkgRitual): SpyInstance => {
return vi.spyOn(DkgClient, 'getActiveRitual').mockImplementation(() => {
return Promise.resolve(dkgRitual);
});
};
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f92f9f9

Please sign in to comment.