Skip to content

Commit

Permalink
apply pr suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Sep 6, 2023
1 parent cb258e4 commit dcc741b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 50 deletions.
36 changes: 18 additions & 18 deletions src/characters/cbd-recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { ethers } from 'ethers';

import { DkgCoordinatorAgent, DkgParticipant } from '../agents/coordinator';
import { ConditionExpression } from '../conditions';
import { ConditionContext } from '../conditions';
import { DkgRitual } from '../dkg';
import { PorterClient } from '../porter';
import { fromJSON, objectEquals, toJSON } from '../utils';
Expand Down Expand Up @@ -42,13 +42,11 @@ export class ThresholdDecrypter {
// Retrieve and decrypt ciphertext using provider and condition expression
public async retrieveAndDecrypt(
provider: ethers.providers.Provider,
conditionExpr: ConditionExpression,
thresholdMessageKit: ThresholdMessageKit,
signer?: ethers.Signer
): Promise<Uint8Array> {
const decryptionShares = await this.retrieve(
provider,
conditionExpr,
thresholdMessageKit,
signer
);
Expand All @@ -59,23 +57,25 @@ export class ThresholdDecrypter {
// Retrieve decryption shares
public async retrieve(
provider: ethers.providers.Provider,
conditionExpr: ConditionExpression,
thresholdMessageKit: ThresholdMessageKit,
signer?: ethers.Signer
): Promise<DecryptionShareSimple[]> {
const dkgParticipants = await DkgCoordinatorAgent.getParticipants(
provider,
this.ritualId
);
const contextStr = await conditionExpr
.buildContext(provider, {}, signer)
.toJson();
const { sharedSecrets, encryptedRequests } = this.makeDecryptionRequests(
this.ritualId,
new Context(contextStr),
dkgParticipants,
thresholdMessageKit
);
const wasmContext = await ConditionContext.fromAccessControlPolicy(
provider,
thresholdMessageKit.acp,
signer
).toWASMContext();
const { sharedSecrets, encryptedRequests } =
await this.makeDecryptionRequests(
this.ritualId,
wasmContext,
dkgParticipants,
thresholdMessageKit
);

const { encryptedResponses, errors } = await this.porter.cbdDecrypt(
encryptedRequests,
Expand Down Expand Up @@ -117,21 +117,21 @@ export class ThresholdDecrypter {
);
}

private makeDecryptionRequests(
private async makeDecryptionRequests(
ritualId: number,
conditionContext: Context,
wasmContext: Context,
dkgParticipants: Array<DkgParticipant>,
thresholdMessageKit: ThresholdMessageKit
): {
): Promise<{
sharedSecrets: Record<string, SessionSharedSecret>;
encryptedRequests: Record<string, EncryptedThresholdDecryptionRequest>;
} {
}> {
const decryptionRequest = new ThresholdDecryptionRequest(
ritualId,
FerveoVariant.simple,
thresholdMessageKit.ciphertextHeader,
thresholdMessageKit.acp,
conditionContext
wasmContext
);

const ephemeralSessionKey = this.makeSessionKey();
Expand Down
12 changes: 6 additions & 6 deletions src/conditions/condition-expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Conditions as WASMConditions } from '@nucypher/nucypher-core';
import { ethers } from 'ethers';
import { SemVer } from 'semver';

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

import { Condition } from './condition';
import { ConditionContext, CustomContextParam } from './context';
Expand All @@ -13,7 +13,7 @@ export type ConditionExpressionJSON = {
};

export class ConditionExpression {
static VERSION = '1.0.0';
public static VERSION = '1.0.0';

constructor(
public readonly condition: Condition,
Expand Down Expand Up @@ -61,6 +61,10 @@ export class ConditionExpression {
return new WASMConditions(toJSON(this.toObj()));
}

public static fromWASMConditions(conditions: WASMConditions) {
return ConditionExpression.fromJSON(conditions.toString());
}

public buildContext(
provider: ethers.providers.Provider,
customParameters: Record<string, CustomContextParam> = {},
Expand All @@ -78,10 +82,6 @@ export class ConditionExpression {
return this.condition.requiresSigner();
}

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

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

public equals(other: Condition) {
return objectEquals(this, other);
return objectEquals(this.toObj(), other.toObj());
}
}
35 changes: 28 additions & 7 deletions src/conditions/context/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Conditions as WASMConditions } from '@nucypher/nucypher-core';
import {
AccessControlPolicy,
Context,
Conditions as WASMConditions,
} from '@nucypher/nucypher-core';
import { ethers } from 'ethers';

import { fromJSON, toJSON } from '../../utils';
import { Condition } from '../condition';
import { ConditionExpression } from '../condition-expr';
import { USER_ADDRESS_PARAM } from '../const';

import { TypedSignature, WalletAuthenticationProvider } from './providers';
Expand Down Expand Up @@ -31,7 +36,7 @@ export class ConditionContext {
this.validate();
}

public requiresSigner(): boolean {
private requiresSigner(): boolean {
return this.conditions.some((cond) => cond.requiresSigner());
}

Expand Down Expand Up @@ -120,19 +125,35 @@ export class ConditionContext {
return parameters;
};

public toJson = async (): Promise<string> => {
public async toJson(): Promise<string> {
const parameters = await this.toObj();
return toJSON(parameters);
};
}

public withCustomParams = (
public withCustomParams(
params: Record<string, CustomContextParam>
): ConditionContext => {
): ConditionContext {
return new ConditionContext(
this.provider,
this.conditions,
params,
this.signer
);
};
}

public async toWASMContext(): Promise<Context> {
const asJson = await this.toJson();
return new Context(asJson);
}

public static fromAccessControlPolicy(
provider: ethers.providers.Provider,
acp: AccessControlPolicy,
signer?: ethers.Signer
): ConditionContext {
const conditions = acp.conditions
? [ConditionExpression.fromWASMConditions(acp.conditions).condition]
: [];
return new ConditionContext(provider, conditions, {}, signer);
}
}
1 change: 0 additions & 1 deletion test/unit/cbd-strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ describe('CbdDeployedStrategy', () => {
const decryptedMessage =
await deployedStrategy.decrypter.retrieveAndDecrypt(
aliceProvider,
conditionExpr,
thresholdMessageKit,
aliceSigner
);
Expand Down
7 changes: 7 additions & 0 deletions test/unit/conditions/condition-expr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ describe('condition set', () => {
expect(conditionExprFromJson.equals(conditionExprFromJson)).toBeTruthy();
});

it('serializes to and from WASM conditions', () => {
const conditionExpr = new ConditionExpression(erc721BalanceCondition);
const wasmConditions = conditionExpr.toWASMConditions();
const fromWasm = ConditionExpression.fromWASMConditions(wasmConditions);
expect(conditionExpr.equals(fromWasm)).toBeTruthy();
});

it('incompatible version', () => {
const currentVersion = new SemVer(ConditionExpression.VERSION);
const invalidVersion = currentVersion.inc('major');
Expand Down
4 changes: 0 additions & 4 deletions test/unit/conditions/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,16 @@ describe('context parameters', () => {
};
const condition = new ContractCondition(conditionObj);
const conditionExpr = new ConditionExpression(condition);
const conditionContext = conditionExpr.buildContext(provider, {}, signer);
expect(conditionExpr.contextRequiresSigner()).toBe(true);
expect(conditionContext.requiresSigner()).toBe(true);
});

it('detects if a signer is not required', () => {
const condition = new RpcCondition(testRpcConditionObj);
const conditionExpr = new ConditionExpression(condition);
const conditionContext = conditionExpr.buildContext(provider, {}, signer);
expect(JSON.stringify(condition.toObj()).includes(USER_ADDRESS_PARAM)).toBe(
false
);
expect(conditionExpr.contextRequiresSigner()).toBe(false);
expect(conditionContext.requiresSigner()).toBe(false);
});

describe('custom method parameters', () => {
Expand Down
15 changes: 2 additions & 13 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
DkgPublicKey,
EncryptedThresholdDecryptionResponse,
EncryptedTreasureMap,
encryptForDkg,
EthereumAddress,
FerveoVariant,
Keypair,
Expand Down Expand Up @@ -323,16 +322,8 @@ export const fakeTDecFlow = async ({
threshold,
receivedMessages,
message,
conditionExpr,
dkgPublicKey,
thresholdMessageKit,
}: FakeDkgRitualFlow) => {
const [_ciphertext, authenticatedData] = encryptForDkg(
message,
dkgPublicKey,
conditionExpr.toWASMConditions()
);

// Having aggregated the transcripts, the validators can now create decryption shares
const decryptionShares: (
| DecryptionSharePrecomputed
Expand All @@ -349,7 +340,7 @@ export const fakeTDecFlow = async ({
const decryptionShare = aggregate.createDecryptionShareSimple(
dkg,
thresholdMessageKit.ciphertextHeader,
authenticatedData.aad(),
thresholdMessageKit.acp.aad(),
keypair
);
decryptionShares.push(decryptionShare);
Expand All @@ -362,7 +353,6 @@ export const fakeTDecFlow = async ({
throw new Error('Decryption failed');
}
return {
authenticatedData,
decryptionShares,
plaintext,
sharedSecret,
Expand Down Expand Up @@ -393,7 +383,7 @@ export const fakeDkgTDecFlowE2E = async (
conditionExpr
);

const { decryptionShares, authenticatedData } = await fakeTDecFlow({
const { decryptionShares } = await fakeTDecFlow({
...ritual,
message,
conditionExpr,
Expand All @@ -405,7 +395,6 @@ export const fakeDkgTDecFlowE2E = async (
...ritual,
message,
decryptionShares,
authenticatedData,
thresholdMessageKit,
};
};
Expand Down

0 comments on commit dcc741b

Please sign in to comment.