-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
36fd43e
commit 2d6d92e
Showing
9 changed files
with
203 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Ciphertext, ferveoEncrypt } from '@nucypher/nucypher-core'; | ||
import { ethers } from 'ethers'; | ||
|
||
import { ThresholdDecrypter } from './characters/cbd-recipient'; | ||
import { ConditionExpression } from './conditions'; | ||
import { DkgClient } from './dkg'; | ||
import { toBytes } from './utils'; | ||
|
||
export interface TacoMessageKit { | ||
ciphertext: Ciphertext; | ||
aad: Uint8Array; | ||
ritualId: number; | ||
threshold: number; | ||
} | ||
|
||
export const encrypt = async ( | ||
web3Provider: ethers.providers.Web3Provider, | ||
message: string, | ||
conditions: ConditionExpression, | ||
ritualId: number | ||
): Promise<TacoMessageKit> => { | ||
const dkgRitual = await DkgClient.getFinalizedRitual(web3Provider, ritualId); | ||
const aad = conditions.asAad(); | ||
const ciphertext = ferveoEncrypt( | ||
toBytes(message), | ||
aad, | ||
dkgRitual.dkgPublicKey | ||
); | ||
return { | ||
ciphertext, | ||
aad, | ||
ritualId, | ||
threshold: dkgRitual.dkgParams.threshold, | ||
}; | ||
}; | ||
|
||
export const decrypt = async ( | ||
web3Provider: ethers.providers.Web3Provider, | ||
messageKit: TacoMessageKit, | ||
porterUri: string | ||
): Promise<Uint8Array> => { | ||
const decrypter = ThresholdDecrypter.create( | ||
porterUri, | ||
messageKit.ritualId, | ||
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? | ||
return decrypter.retrieveAndDecrypt( | ||
web3Provider, | ||
condExpr, | ||
messageKit.ciphertext | ||
); | ||
}; | ||
|
||
export const taco = { | ||
encrypt, | ||
decrypt, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
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, | ||
fakePorterUri, | ||
fakeTDecFlow, | ||
fakeWeb3Provider, | ||
mockCbdDecrypt, | ||
mockGetExistingRitual, | ||
mockGetParticipants, | ||
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 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 getExistingRitualSpy = mockGetExistingRitual(mockedDkgRitual); | ||
|
||
const tacoMk = await taco.encrypt( | ||
web3Provider, | ||
message, | ||
conditionExpr, | ||
mockedDkg.ritualId | ||
); | ||
|
||
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 getParticipantsSpy = mockGetParticipants(participants); | ||
const sessionKeySpy = mockRandomSessionStaticSecret(requesterSessionKey); | ||
|
||
const decryptedMessage = await taco.decrypt( | ||
web3Provider, | ||
tacoMk, | ||
fakePorterUri | ||
); | ||
expect(getParticipantsSpy).toHaveBeenCalled(); | ||
expect(sessionKeySpy).toHaveBeenCalled(); | ||
expect(decryptSpy).toHaveBeenCalled(); | ||
expect(decryptedMessage).toEqual(toBytes(message)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2d6d92e
There was a problem hiding this comment.
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/module/src/kits: 19.53 KB
build/module/src/characters: 74.22 KB
build/module/src/policies: 19.53 KB
build/module/src/agents: 39.06 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions: 156.25 KB
build/module/src/sdk/strategy: 31.25 KB
build/module/src/sdk: 42.97 KB
build/module/src: 433.59 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/test: 42.97 KB
build/module: 687.50 KB
build/main/src/kits: 19.53 KB
build/main/src/characters: 74.22 KB
build/main/src/policies: 19.53 KB
build/main/src/agents: 39.06 KB
build/main/src/conditions/predefined: 19.53 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions: 156.25 KB
build/main/src/sdk/strategy: 35.16 KB
build/main/src/sdk: 46.88 KB
build/main/src: 445.31 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/test: 46.88 KB
build/main: 703.13 KB
build: 1.36 MB