Skip to content

Commit

Permalink
refactor: simplify equals method in protocol objects
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jun 30, 2023
1 parent b9ddedc commit 982c8f8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 40 deletions.
18 changes: 7 additions & 11 deletions src/characters/pre-recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Keyring } from '../keyring';
import { PolicyMessageKit } from '../kits/message';
import { RetrievalResult } from '../kits/retrieval';
import { PorterClient } from '../porter';
import { base64ToU8Receiver, bytesEquals, toJSON, zip } from '../utils';
import { base64ToU8Receiver, toJSON, zip } from '../utils';

export type PreDecrypterJSON = {
porterUri: string;
Expand Down Expand Up @@ -185,15 +185,11 @@ export class PreDecrypter {
}

public equals(other: PreDecrypter): boolean {
return (
this.porter.porterUrl.toString() === other.porter.porterUrl.toString() &&
this.policyEncryptingKey.equals(other.policyEncryptingKey) &&
// TODO: Replace with `equals` after https://github.com/nucypher/nucypher-core/issues/56 is fixed
bytesEquals(
this.encryptedTreasureMap.toBytes(),
other.encryptedTreasureMap.toBytes()
) &&
this.publisherVerifyingKey.equals(other.publisherVerifyingKey)
);
return [
this.porter.porterUrl.toString() === other.porter.porterUrl.toString(),
this.policyEncryptingKey.equals(other.policyEncryptingKey),
this.encryptedTreasureMap.equals(other.encryptedTreasureMap),
this.publisherVerifyingKey.equals(other.publisherVerifyingKey),
].every(Boolean);
}
}
8 changes: 4 additions & 4 deletions src/conditions/condition-expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ export class ConditionExpression {
}

public equals(other: ConditionExpression): boolean {
return (
this.version === other.version &&
objectEquals(this.condition.toObj(), other.condition.toObj())
);
return [
this.version === other.version,
objectEquals(this.condition.toObj(), other.condition.toObj()),
].every(Boolean);
}
}
13 changes: 6 additions & 7 deletions src/dkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { ethers } from 'ethers';

import { DkgCoordinatorAgent } from './agents/coordinator';
import { bytesEquals, fromHexString } from './utils';
import { fromHexString } from './utils';

// TODO: Expose from @nucypher/nucypher-core
export enum FerveoVariant {
Expand Down Expand Up @@ -75,12 +75,11 @@ export class DkgRitual {
}

public equals(other: DkgRitual): boolean {
return (
this.id === other.id &&
// TODO: Replace with `equals` after https://github.com/nucypher/nucypher-core/issues/56 is fixed
bytesEquals(this.dkgPublicKey.toBytes(), other.dkgPublicKey.toBytes()) &&
this.threshold === other.threshold
);
return [
this.id === other.id,
this.dkgPublicKey.equals(other.dkgPublicKey),
this.threshold === other.threshold,
].every(Boolean);
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/sdk/strategy/cbd-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { DkgPublicKey } from '@nucypher/nucypher-core';
import { ethers } from 'ethers';

import { bytesEqual } from '../../../test/utils';
import {
ThresholdDecrypter,
ThresholdDecrypterJSON,
Expand Down Expand Up @@ -110,9 +109,9 @@ export class DeployedCbdStrategy {
}

public equals(other: DeployedCbdStrategy) {
return (
this.decrypter.equals(other.decrypter) &&
bytesEqual(this.dkgPublicKey.toBytes(), other.dkgPublicKey.toBytes())
);
return [
this.decrypter.equals(other.decrypter),
this.dkgPublicKey.equals(other.dkgPublicKey),
].every(Boolean);
}
}
26 changes: 13 additions & 13 deletions src/sdk/strategy/pre-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,20 @@ export class PreStrategy {
}

public equals(other: PreStrategy) {
return (
this.cohort.equals(other.cohort) &&
// TODO: Replace with `equals` after https://github.com/nucypher/nucypher-core/issues/56 is fixed
return [
this.cohort.equals(other.cohort),
// TODO: Replace with `equals` after https://github.com/nucypher/rust-umbral/pull/125 is released
bytesEquals(
this.aliceSecretKey.toBEBytes(),
other.aliceSecretKey.toBEBytes()
) &&
),
bytesEquals(
this.bobSecretKey.toBEBytes(),
other.bobSecretKey.toBEBytes()
) &&
this.startDate.toString() === other.startDate.toString() &&
this.endDate.toString() === other.endDate.toString()
);
),
this.startDate.toString() === other.startDate.toString(),
this.endDate.toString() === other.endDate.toString(),
].every(Boolean);
}
}

Expand Down Expand Up @@ -203,10 +203,10 @@ export class DeployedPreStrategy {
}

public equals(other: DeployedPreStrategy) {
return (
this.cohort.equals(other.cohort) &&
this.decrypter.equals(other.decrypter) &&
this.policyKey.equals(other.policyKey)
);
return [
this.cohort.equals(other.cohort),
this.decrypter.equals(other.decrypter),
this.policyKey.equals(other.policyKey),
].every(Boolean);
}
}

1 comment on commit 982c8f8

@github-actions
Copy link

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/types/ethers-contracts/factories: 82.03 KB
build/module/types/ethers-contracts: 152.34 KB
build/module/types: 156.25 KB
build/module/src/conditions/predefined: 19.53 KB
build/module/src/conditions/context: 42.97 KB
build/module/src/conditions/base: 54.69 KB
build/module/src/conditions: 156.25 KB
build/module/src/kits: 19.53 KB
build/module/src/agents: 35.16 KB
build/module/src/characters: 74.22 KB
build/module/src/policies: 19.53 KB
build/module/src/sdk/strategy: 35.16 KB
build/module/src/sdk: 46.88 KB
build/module/src: 425.78 KB
build/module: 636.72 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/src/conditions/predefined: 19.53 KB
build/main/src/conditions/context: 42.97 KB
build/main/src/conditions/base: 54.69 KB
build/main/src/conditions: 156.25 KB
build/main/src/kits: 19.53 KB
build/main/src/agents: 35.16 KB
build/main/src/characters: 74.22 KB
build/main/src/policies: 19.53 KB
build/main/src/sdk/strategy: 35.16 KB
build/main/src/sdk: 46.88 KB
build/main/src: 429.69 KB
build/main: 640.63 KB
build: 1.25 MB

Please sign in to comment.