From b2cf2e46a6acc4c24422c7262cd462144caa0f12 Mon Sep 17 00:00:00 2001 From: Piotr Roslaniec Date: Mon, 14 Aug 2023 13:16:02 +0200 Subject: [PATCH] draft light encryption method --- src/taco.ts | 42 ++++++++++++++++++++++++++++++++---------- test/unit/taco.test.ts | 1 - 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/taco.ts b/src/taco.ts index 3d4228c24..816d24943 100644 --- a/src/taco.ts +++ b/src/taco.ts @@ -1,4 +1,8 @@ -import { Ciphertext, ferveoEncrypt } from '@nucypher/nucypher-core'; +import { + Ciphertext, + DkgPublicKey, + ferveoEncrypt, +} from '@nucypher/nucypher-core'; import { ethers } from 'ethers'; import { ThresholdDecrypter } from './characters/cbd-recipient'; @@ -9,7 +13,10 @@ import { toBytes } from './utils'; export interface TacoMessageKit { ciphertext: Ciphertext; aad: Uint8Array; + // 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; + // We need to know the threshold in order to create DecryptionRequests threshold: number; } @@ -20,17 +27,30 @@ export const encrypt = async ( ritualId: number ): Promise => { const dkgRitual = await DkgClient.getFinalizedRitual(web3Provider, ritualId); - const aad = conditions.asAad(); - const ciphertext = ferveoEncrypt( - toBytes(message), - aad, - dkgRitual.dkgPublicKey + return await encryptLight( + message, + conditions, + dkgRitual.dkgPublicKey, + dkgRitual.dkgParams.threshold, + ritualId ); +}; + +export const encryptLight = async ( + message: string, + conditions: ConditionExpression, + dkgPublicKey: DkgPublicKey, + // TODO: Remove these parameters after fixing TacoMessageKit + threshold: number, + ritualId: number +): Promise => { + const aad = conditions.asAad(); + const ciphertext = ferveoEncrypt(toBytes(message), aad, dkgPublicKey); return { ciphertext, aad, + threshold, ritualId, - threshold: dkgRitual.dkgParams.threshold, }; }; @@ -45,9 +65,10 @@ export const decrypt = async ( messageKit.threshold ); const condExpr = ConditionExpression.fromAad(messageKit.aad); - // TODO: Need web3Provider to fetch participants from Coordinator to make decryption requests. - // Should we put them into the message kit instead? - // Consider case where participants are changing over time. Is that an issue we should consider now? + // 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, @@ -57,5 +78,6 @@ export const decrypt = async ( export const taco = { encrypt, + encryptLight, decrypt, }; diff --git a/test/unit/taco.test.ts b/test/unit/taco.test.ts index fe74043dd..907b58f86 100644 --- a/test/unit/taco.test.ts +++ b/test/unit/taco.test.ts @@ -36,7 +36,6 @@ const ownsNFT = new ERC721Ownership({ }); const conditionExpr = new ConditionExpression(ownsNFT); const variant = FerveoVariant.precomputed; -// const ritualId = 0; const message = 'this is a secret'; describe('taco', () => {