Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect Condition parsing #239

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/characters/porter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import axios, { AxiosResponse } from 'axios';
import qs from 'qs';

import { ConditionContext } from '../conditions';
import { Base64EncodedBytes, ChecksumAddress, HexEncodedBytes } from '../types';
import { fromBase64, fromHexString, toBase64, toHexString } from '../utils';

Expand Down Expand Up @@ -138,18 +137,15 @@ export class Porter {
aliceVerifyingKey: PublicKey,
bobEncryptingKey: PublicKey,
bobVerifyingKey: PublicKey,
conditionsContext?: ConditionContext
conditionContextJSON?: string | undefined
): Promise<readonly RetrieveCFragsResult[]> {
const context = conditionsContext
? await conditionsContext.toJson()
: undefined;
const data: PostRetrieveCFragsRequest = {
treasure_map: toBase64(treasureMap.toBytes()),
retrieval_kits: retrievalKits.map((rk) => toBase64(rk.toBytes())),
alice_verifying_key: toHexString(aliceVerifyingKey.toCompressedBytes()),
bob_encrypting_key: toHexString(bobEncryptingKey.toCompressedBytes()),
bob_verifying_key: toHexString(bobVerifyingKey.toCompressedBytes()),
context,
context: conditionContextJSON,
};
const resp: AxiosResponse<PostRetrieveCFragsResponse> = await axios.post(
new URL('/retrieve_cfrags', this.porterUrl).toString(),
Expand Down
18 changes: 9 additions & 9 deletions src/characters/pre-recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@nucypher/nucypher-core';
import { ethers } from 'ethers';

import { Condition, ConditionContext } from '../conditions';
import { ConditionContext, ConditionExpression } from '../conditions';
import { Keyring } from '../keyring';
import { PolicyMessageKit } from '../kits/message';
import { RetrievalResult } from '../kits/retrieval';
Expand Down Expand Up @@ -103,14 +103,11 @@ export class PreTDecDecrypter {
const conditions = messageKits
.map((mk) => mk.conditions)
.filter((condition): condition is Conditions => !!condition)
.map((condition) => JSON.parse(condition.toString()))
.reduce((acc: Record<string, string>[], val) => acc.concat(val), []);
.map((condition) => ConditionExpression.fromJSON(condition.toString()))
.reduce((acc: ConditionExpression[], val) => acc.concat(val), [])
.map((condExpr: ConditionExpression) => condExpr.condition);
Comment on lines -106 to +108
Copy link
Contributor Author

@piotr-roslaniec piotr-roslaniec Jul 7, 2023

Choose a reason for hiding this comment

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

This out-of-date condition handling was the root cause


const conditionsList = conditions.map((ele: Record<string, string>) => {
return Condition.fromObj(ele);
});

const conditionContext = new ConditionContext(conditionsList, provider);
const conditionContext = new ConditionContext(conditions, provider);

const policyMessageKits = messageKits.map((mk) =>
PolicyMessageKit.fromMessageKit(
Expand All @@ -121,13 +118,16 @@ export class PreTDecDecrypter {
);

const retrievalKits = policyMessageKits.map((pk) => pk.asRetrievalKit());
const conditionContextJSON = conditionContext
? await conditionContext.toJson()
: undefined;
const retrieveCFragsResponses = await this.porter.retrieveCFrags(
treasureMap,
retrievalKits,
this.publisherVerifyingKey,
this.decryptingKey,
this.keyring.publicKey,
conditionContext
conditionContextJSON
);

return zip(policyMessageKits, retrieveCFragsResponses).map((pair) => {
Expand Down
2 changes: 1 addition & 1 deletion test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const fakeAlice = (aliceKey = 'fake-secret-key-32-bytes-alice-x') => {
};

export const fakeWeb3Provider = (
secretKeyBytes: Uint8Array,
secretKeyBytes = SecretKey.random().toBEBytes(),
blockNumber?: number,
blockTimestamp?: number
): ethers.providers.Web3Provider => {
Expand Down