Skip to content

Commit

Permalink
draft taco api
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Aug 11, 2023
1 parent 752320c commit 24abb07
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/taco.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Ciphertext } from '@nucypher/nucypher-core';
import { ethers } from 'ethers';

import { makeCohort } from '../test/utils';

import { ThresholdDecrypter } from './characters/cbd-recipient';
import { ConditionExpression } from './conditions';
import { CbdStrategy } from './sdk/strategy/cbd-strategy';

export interface TacoMessageKit {
ciphertext: Ciphertext;
aad: Uint8Array;
decrypter: ThresholdDecrypter;
conditions: ConditionExpression;
}

export const encrypt = async (
message: string,
conditions: ConditionExpression,
ritualId: number,
web3Provider: ethers.providers.Web3Provider,
): Promise<TacoMessageKit> => {
const cohort = await makeCohort([]);
const strategy = CbdStrategy.create(cohort);
const deployedStrategy = await strategy.deploy(web3Provider, ritualId);
const { ciphertext, aad } = deployedStrategy
.makeEncrypter(conditions)
.encryptMessageCbd(message);

return {
ciphertext,
aad,
decrypter: deployedStrategy.decrypter,
conditions,
};
};

export const decrypt = async (
messageKit: TacoMessageKit,
web3Provider: ethers.providers.Web3Provider,
): Promise<Uint8Array> => {
return await messageKit.decrypter.retrieveAndDecrypt(
web3Provider,
messageKit.conditions,
messageKit.ciphertext,
);
};

export const taco = {
encrypt,
decrypt,
};
93 changes: 93 additions & 0 deletions test/unit/taco.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import {
FerveoVariant,
SecretKey,
SessionStaticSecret,
} from '@nucypher/nucypher-core';

import { conditions } from '../../src';
import { taco } from '../../src/taco';
import { toBytes } from '../../src/utils';
import {
fakeDkgFlow,
fakeDkgParticipants,
fakeDkgRitual,
fakeTDecFlow,
fakeUrsulas,
fakeWeb3Provider,
mockCbdDecrypt,
mockGetExistingRitual,
mockGetParticipants,
mockGetRitualState,
mockGetUrsulas,
mockRandomSessionStaticSecret,
} from '../utils';

import { aliceSecretKeyBytes } from './testVariables';

const {
predefined: { ERC721Ownership },
ConditionExpression,
} = conditions;

// Shared test variables
const aliceSecretKey = SecretKey.fromBEBytes(aliceSecretKeyBytes);
const ownsNFT = new ERC721Ownership({
contractAddress: '0x1e988ba4692e52Bc50b375bcC8585b95c48AaD77',
parameters: [3591],
chain: 5,
});
const conditionExpr = new ConditionExpression(ownsNFT);
const ursulas = fakeUrsulas();
const variant = FerveoVariant.precomputed;
// const ritualId = 0;
const message = 'this is a secret';

describe('taco', () => {
it('encrypts and decrypts', async () => {
const mockedDkg = fakeDkgFlow(variant, 0, 4, 4);
const mockedDkgRitual = fakeDkgRitual(mockedDkg);
const web3Provider = fakeWeb3Provider(aliceSecretKey.toBEBytes());
const getUrsulasSpy = mockGetUrsulas(ursulas);
const getExistingRitualSpy = mockGetExistingRitual(mockedDkgRitual);

const tacoMk = await taco.encrypt(
message,
conditionExpr,
mockedDkg.ritualId,
web3Provider
);

expect(getUrsulasSpy).toHaveBeenCalled();
expect(getExistingRitualSpy).toHaveBeenCalled();

const { decryptionShares } = fakeTDecFlow({
...mockedDkg,
message: toBytes(message),
aad: tacoMk.aad,
ciphertext: tacoMk.ciphertext,
});
const { participantSecrets, participants } = fakeDkgParticipants(
mockedDkg.ritualId,
variant
);
const requesterSessionKey = SessionStaticSecret.random();
const decryptSpy = mockCbdDecrypt(
mockedDkg.ritualId,
decryptionShares,
participantSecrets,
requesterSessionKey.publicKey()
);
const mockGetRitualStateSpy = mockGetRitualState();
const getParticipantsSpy = mockGetParticipants(participants);
const getUrsulasSpy2 = mockGetUrsulas(ursulas);
const sessionKeySpy = mockRandomSessionStaticSecret(requesterSessionKey);

const decryptedMessage = await taco.decrypt(tacoMk, web3Provider);
expect(mockGetRitualStateSpy).toHaveBeenCalled();
expect(getUrsulasSpy2).toHaveBeenCalled();
expect(getParticipantsSpy).toHaveBeenCalled();
expect(sessionKeySpy).toHaveBeenCalled();
expect(decryptSpy).toHaveBeenCalled();
expect(decryptedMessage).toEqual(toBytes(message));
});
});

0 comments on commit 24abb07

Please sign in to comment.