Skip to content

Commit

Permalink
update after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Sep 4, 2023
1 parent 0dc72d2 commit 5983ed0
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 64 deletions.
7 changes: 4 additions & 3 deletions src/characters/cbd-recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { ethers } from 'ethers';

import { DkgCoordinatorAgent, DkgParticipant } from '../agents/coordinator';
import { ConditionExpression } from '../conditions';
import { DkgClient, DkgRitual } from '../dkg';
import { PorterClient } from '../porter';
import { fromJSON, objectEquals, toJSON } from '../utils';

Expand Down Expand Up @@ -43,12 +42,14 @@ export class ThresholdDecrypter {
public async retrieveAndDecrypt(
web3Provider: ethers.providers.Provider,
conditionExpr: ConditionExpression,
thresholdMessageKit: ThresholdMessageKit
thresholdMessageKit: ThresholdMessageKit,
signer?: ethers.Signer
): Promise<Uint8Array> {
const decryptionShares = await this.retrieve(
web3Provider,
conditionExpr,
thresholdMessageKit
thresholdMessageKit,
signer
);
const sharedSecret = combineDecryptionSharesSimple(decryptionShares);
return thresholdMessageKit.decryptWithSharedSecret(sharedSecret);
Expand Down
11 changes: 1 addition & 10 deletions src/conditions/condition-expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { Conditions as WASMConditions } from '@nucypher/nucypher-core';
import { ethers } from 'ethers';
import { SemVer } from 'semver';

import { fromBytes } from '../../test/utils';
import { objectEquals, toBytes, toJSON } from '../utils';
import { objectEquals, toJSON } from '../utils';

import {
Condition,
Expand Down Expand Up @@ -102,14 +101,6 @@ export class ConditionExpression {
return this.condition.requiresSigner();
}

public asAad(): Uint8Array {
return toBytes(this.toJson());
}

public static fromAad(aad: Uint8Array): ConditionExpression {
return ConditionExpression.fromJSON(fromBytes(aad));
}

public equals(other: ConditionExpression): boolean {
return [
this.version === other.version,
Expand Down
2 changes: 1 addition & 1 deletion src/conditions/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class ConditionContext {

if (this.requiresSigner() && !this.signer) {
throw new Error(
`Cannot use ${USER_ADDRESS_PARAM} as custom parameter without a signer`
`Cannot use ${USER_ADDRESS_PARAM} as a parameter without a signer`
);
}

Expand Down
34 changes: 16 additions & 18 deletions src/taco.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import {
Ciphertext,
DkgPublicKey,
ferveoEncrypt,
} from '@nucypher/nucypher-core';
import { DkgPublicKey, ThresholdMessageKit } from '@nucypher/nucypher-core';
import { ethers } from 'ethers';

import { ThresholdDecrypter } from './characters/cbd-recipient';
import { Enrico } from './characters/enrico';
import { Condition, ConditionExpression } from './conditions';
import { DkgClient } from './dkg';
import { getPorterUri } from './porter';
import { toBytes } from './utils';

export interface TacoMessageKit {
ciphertext: Ciphertext;
aad: Uint8Array;
thresholdMessageKit: ThresholdMessageKit;
conditionExpr: ConditionExpression;
// TODO: How do we get rid of these two fields? We need them for decrypting
// We ritualId in order to fetch the DKG participants and create DecryptionRequests for them
ritualId: number;
Expand Down Expand Up @@ -45,35 +42,36 @@ export const encryptLight = async (
threshold: number,
ritualId: number
): Promise<TacoMessageKit> => {
const aad = new ConditionExpression(condition).asAad();
const ciphertext = ferveoEncrypt(toBytes(message), aad, dkgPublicKey);
const encrypter = new Enrico(dkgPublicKey);
const conditionExpr = new ConditionExpression(condition);
const thresholdMessageKit = await encrypter.encryptMessageCbd(
toBytes(message),
conditionExpr
);
return {
ciphertext,
aad,
thresholdMessageKit,
threshold,
ritualId,
conditionExpr,
};
};

export const decrypt = async (
web3Provider: ethers.providers.Web3Provider,
messageKit: TacoMessageKit,
signer?: ethers.Signer,
porterUri = getPorterUri('tapir')
): Promise<Uint8Array> => {
const decrypter = ThresholdDecrypter.create(
porterUri,
messageKit.ritualId,
messageKit.threshold
);
const condExpr = ConditionExpression.fromAad(messageKit.aad);
// TODO: We need web3Provider to fetch participants from Coordinator to make decryption requests.
// Removing this dependency is tied to release of ThresholdMessageKit
// Blocked by changes to nucypher-core and nucypher:
// https://github.com/nucypher/nucypher/pull/3194
return decrypter.retrieveAndDecrypt(
web3Provider,
condExpr,
messageKit.ciphertext
messageKit.conditionExpr,
messageKit.thresholdMessageKit,
signer
);
};

Expand Down
18 changes: 10 additions & 8 deletions test/unit/cbd-strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
fakeDkgParticipants,
fakeDkgRitual,
fakeProvider,
fakeSigner,
fakeTDecFlow,
fakeUrsulas,
makeCohort,
Expand All @@ -28,8 +29,9 @@ const {
} = conditions;

// Shared test variables
const aliceSecretKey = SecretKey.fromBEBytes(aliceSecretKeyBytes);
const aliceProvider = fakeProvider(aliceSecretKey.toBEBytes());
const secretKey = SecretKey.fromBEBytes(aliceSecretKeyBytes);
const provider = fakeProvider(secretKey.toBEBytes());
const signer = fakeSigner(secretKey.toBEBytes());
const ownsNFT = new ERC721Ownership({
contractAddress: '0x1e988ba4692e52Bc50b375bcC8585b95c48AaD77',
parameters: [3591],
Expand All @@ -39,7 +41,6 @@ const conditionExpr = new ConditionExpression(ownsNFT);
const ursulas = fakeUrsulas();
const variant = FerveoVariant.precomputed;
const ritualId = 0;
const web3Provider = fakeProvider(aliceSecretKey.toBEBytes());

const makeCbdStrategy = async () => {
const cohort = await makeCohort(ursulas);
Expand All @@ -55,7 +56,7 @@ async function makeDeployedCbdStrategy() {
const getUrsulasSpy = mockGetUrsulas(ursulas);
const getExistingRitualSpy = mockGetExistingRitual(mockedDkgRitual);

const deployedStrategy = await strategy.deploy(web3Provider, ritualId);
const deployedStrategy = await strategy.deploy(provider, ritualId);

expect(getUrsulasSpy).toHaveBeenCalled();
expect(getExistingRitualSpy).toHaveBeenCalled();
Expand Down Expand Up @@ -107,14 +108,14 @@ describe('CbdDeployedStrategy', () => {
.encryptMessageCbd(message);

// Setup mocks for `retrieveAndDecrypt`
const { decryptionShares } = await fakeTDecFlow({
const { decryptionShares } = fakeTDecFlow({
...mockedDkg,
message: toBytes(message),
conditionExpr,
dkgPublicKey: mockedDkg.dkg.publicKey(),
thresholdMessageKit,
});
const { participantSecrets, participants } = await fakeDkgParticipants(
const { participantSecrets, participants } = fakeDkgParticipants(
mockedDkg.ritualId
);
const requesterSessionKey = SessionStaticSecret.random();
Expand All @@ -130,9 +131,10 @@ describe('CbdDeployedStrategy', () => {

const decryptedMessage =
await deployedStrategy.decrypter.retrieveAndDecrypt(
aliceProvider,
provider,
conditionExpr,
thresholdMessageKit
thresholdMessageKit,
signer
);
expect(getUrsulasSpy).toHaveBeenCalled();
expect(getParticipantsSpy).toHaveBeenCalled();
Expand Down
9 changes: 0 additions & 9 deletions test/unit/conditions/condition-expr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,6 @@ describe('condition set', () => {
expect(conditionExprFromJson.equals(conditionExprFromJson)).toBeTruthy();
});

it('serializes to and from aad', () => {
const conditionExpr = new ConditionExpression(rpcCondition);
const conditionExprAad = conditionExpr.asAad();
const conditionExprFromAad =
ConditionExpression.fromAad(conditionExprAad);
expect(conditionExprFromAad).toBeDefined();
expect(conditionExprFromAad.equals(conditionExpr)).toBeTruthy();
});

it('incompatible version', () => {
const currentVersion = new SemVer(ConditionExpression.VERSION);
const invalidVersion = currentVersion.inc('major');
Expand Down
19 changes: 11 additions & 8 deletions test/unit/taco.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import {
fakeDkgParticipants,
fakeDkgRitual,
fakePorterUri,
fakeProvider,
fakeSigner,
fakeTDecFlow,
fakeWeb3Provider,
mockCbdDecrypt,
mockGetExistingRitual,
mockGetParticipants,
Expand All @@ -40,11 +41,12 @@ describe('taco', () => {
it('encrypts and decrypts', async () => {
const mockedDkg = fakeDkgFlow(variant, 0, 4, 4);
const mockedDkgRitual = fakeDkgRitual(mockedDkg);
const web3Provider = fakeWeb3Provider(aliceSecretKey.toBEBytes());
const provider = fakeProvider(aliceSecretKey.toBEBytes());
const signer = fakeSigner(aliceSecretKey.toBEBytes());
const getExistingRitualSpy = mockGetExistingRitual(mockedDkgRitual);

const tacoMk = await taco.encrypt(
web3Provider,
provider,
message,
ownsNFT,
mockedDkg.ritualId
Expand All @@ -55,12 +57,12 @@ describe('taco', () => {
const { decryptionShares } = fakeTDecFlow({
...mockedDkg,
message: toBytes(message),
aad: tacoMk.aad,
ciphertext: tacoMk.ciphertext,
conditionExpr: tacoMk.conditionExpr,
dkgPublicKey: mockedDkg.dkg.publicKey(),
thresholdMessageKit: tacoMk.thresholdMessageKit,
});
const { participantSecrets, participants } = fakeDkgParticipants(
mockedDkg.ritualId,
variant
mockedDkg.ritualId
);
const requesterSessionKey = SessionStaticSecret.random();
const decryptSpy = mockCbdDecrypt(
Expand All @@ -73,8 +75,9 @@ describe('taco', () => {
const sessionKeySpy = mockRandomSessionStaticSecret(requesterSessionKey);

const decryptedMessage = await taco.decrypt(
web3Provider,
provider,
tacoMk,
signer,
fakePorterUri
);
expect(getParticipantsSpy).toHaveBeenCalled();
Expand Down
14 changes: 7 additions & 7 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ interface FakeDkgRitualFlow {
thresholdMessageKit: ThresholdMessageKit;
}

export const fakeTDecFlow = async ({
export const fakeTDecFlow = ({
validators,
validatorKeypairs,
ritualId,
Expand Down Expand Up @@ -375,7 +375,7 @@ const fakeConditionExpr = () => {
return new ConditionExpression(erc721Balance);
};

export const fakeDkgTDecFlowE2E = async (
export const fakeDkgTDecFlowE2E = (
ritualId = 0,
variant: FerveoVariant = FerveoVariant.precomputed,
conditionExpr: ConditionExpression = fakeConditionExpr(),
Expand All @@ -390,7 +390,7 @@ export const fakeDkgTDecFlowE2E = async (
conditionExpr
);

const { decryptionShares, authenticatedData } = await fakeTDecFlow({
const { decryptionShares, authenticatedData } = fakeTDecFlow({
...ritual,
message,
conditionExpr,
Expand Down Expand Up @@ -442,13 +442,13 @@ export const fakeCoordinatorRitual = async (
};
};

export const fakeDkgParticipants = async (
export const fakeDkgParticipants = (
ritualId: number
): Promise<{
): {
participants: DkgParticipant[];
participantSecrets: Record<string, SessionStaticSecret>;
}> => {
const ritual = await fakeDkgTDecFlowE2E(ritualId);
} => {
const ritual = fakeDkgTDecFlowE2E(ritualId);
const label = toBytes(`${ritualId}`);

const participantSecrets: Record<string, SessionStaticSecret> =
Expand Down

1 comment on commit 5983ed0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bundled size for the package is listed below:

build/main/src/characters: 78.13 KB
build/main/src/kits: 19.53 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions: 156.25 KB
build/main/src/agents: 39.06 KB
build/main/src/sdk/strategy: 35.16 KB
build/main/src/sdk: 46.88 KB
build/main/src/policies: 19.53 KB
build/main/src: 453.13 KB
build/main/types/ethers-contracts/factories: 82.03 KB
build/main/types/ethers-contracts: 152.34 KB
build/main/types: 156.25 KB
build/main: 664.06 KB
build/module/src/characters: 78.13 KB
build/module/src/kits: 19.53 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions: 156.25 KB
build/module/src/agents: 39.06 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 42.97 KB
build/module/src/policies: 19.53 KB
build/module/src: 441.41 KB
build/module/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 152.34 KB
build/module/types: 156.25 KB
build/module: 652.34 KB
build: 1.29 MB

Please sign in to comment.