Skip to content

Commit

Permalink
simplify cohort-strategy relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Jun 28, 2023
1 parent 32e6a94 commit 3df4f18
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 121 deletions.
16 changes: 8 additions & 8 deletions src/characters/cbd-recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import { fromJSON, toJSON } from '../utils';

import { Porter } from './porter';

export type CbdTDecDecrypterJSON = {
export type ThresholdDecrypterJSON = {
porterUri: string;
ritualId: number;
threshold: number;
};

export class CbdTDecDecrypter {
export class ThresholdDecrypter {
// private readonly verifyingKey: Keyring;

private constructor(
Expand All @@ -40,7 +40,7 @@ export class CbdTDecDecrypter {
) {}

public static create(porterUri: string, dkgRitual: DkgRitual) {
return new CbdTDecDecrypter(
return new ThresholdDecrypter(
new Porter(porterUri),
dkgRitual.id,
dkgRitual.threshold
Expand Down Expand Up @@ -193,7 +193,7 @@ export class CbdTDecDecrypter {
return SessionStaticSecret.random();
}

public toObj(): CbdTDecDecrypterJSON {
public toObj(): ThresholdDecrypterJSON {
return {
porterUri: this.porter.porterUrl.toString(),
ritualId: this.ritualId,
Expand All @@ -209,15 +209,15 @@ export class CbdTDecDecrypter {
porterUri,
ritualId,
threshold,
}: CbdTDecDecrypterJSON) {
return new CbdTDecDecrypter(new Porter(porterUri), ritualId, threshold);
}: ThresholdDecrypterJSON) {
return new ThresholdDecrypter(new Porter(porterUri), ritualId, threshold);
}

public static fromJSON(json: string) {
return CbdTDecDecrypter.fromObj(fromJSON(json));
return ThresholdDecrypter.fromObj(fromJSON(json));
}

public equals(other: CbdTDecDecrypter): boolean {
public equals(other: ThresholdDecrypter): boolean {
return (
this.porter.porterUrl.toString() === other.porter.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 @@ -16,15 +16,15 @@ import { base64ToU8Receiver, bytesEquals, toJSON, zip } from '../utils';

import { Porter } from './porter';

export type PreTDecDecrypterJSON = {
export type PreDecrypterJSON = {
porterUri: string;
policyEncryptingKeyBytes: Uint8Array;
encryptedTreasureMapBytes: Uint8Array;
publisherVerifyingKeyBytes: Uint8Array;
bobSecretKeyBytes: Uint8Array;
};

export class PreTDecDecrypter {
export class PreDecrypter {
// private readonly verifyingKey: Keyring;

constructor(
Expand All @@ -41,8 +41,8 @@ export class PreTDecDecrypter {
policyEncryptingKey: PublicKey,
publisherVerifyingKey: PublicKey,
encryptedTreasureMap: EncryptedTreasureMap
): PreTDecDecrypter {
return new PreTDecDecrypter(
): PreDecrypter {
return new PreDecrypter(
new Porter(porterUri),
new Keyring(secretKey),
policyEncryptingKey,
Expand Down Expand Up @@ -149,7 +149,7 @@ export class PreTDecDecrypter {
});
}

public toObj(): PreTDecDecrypterJSON {
public toObj(): PreDecrypterJSON {
return {
porterUri: this.porter.porterUrl.toString(),
policyEncryptingKeyBytes: this.policyEncryptingKey.toCompressedBytes(),
Expand All @@ -170,8 +170,8 @@ export class PreTDecDecrypter {
encryptedTreasureMapBytes,
publisherVerifyingKeyBytes,
bobSecretKeyBytes,
}: PreTDecDecrypterJSON) {
return new PreTDecDecrypter(
}: PreDecrypterJSON) {
return new PreDecrypter(
new Porter(porterUri),
new Keyring(SecretKey.fromBEBytes(bobSecretKeyBytes)),
PublicKey.fromCompressedBytes(policyEncryptingKeyBytes),
Expand All @@ -182,10 +182,10 @@ export class PreTDecDecrypter {

public static fromJSON(json: string) {
const config = JSON.parse(json, base64ToU8Receiver);
return PreTDecDecrypter.fromObj(config);
return PreDecrypter.fromObj(config);
}

public equals(other: PreTDecDecrypter): boolean {
public equals(other: PreDecrypter): boolean {
return (
this.porter.porterUrl.toString() === other.porter.porterUrl.toString() &&
this.policyEncryptingKey.equals(other.policyEncryptingKey) &&
Expand Down
12 changes: 6 additions & 6 deletions src/dkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ export class DkgRitual {
export class DkgClient {
constructor(private readonly provider: ethers.providers.Web3Provider) {}

// TODO: Update API: Replace with getExistingRitual and support ritualId in Strategy
public async initializeRitual(ritualParams: {
shares: number;
threshold: number;
}): Promise<DkgRitual> {
public async initializeRitual(): Promise<DkgRitual> {
const ritualId = 2;
return this.getExistingRitual(ritualId);
}

public async getExistingRitual(ritualId: number): Promise<DkgRitual> {
const ritual = await DkgCoordinatorAgent.getRitual(this.provider, ritualId);
const dkgPkBytes = new Uint8Array([
...fromHexString(ritual.publicKey.word0),
Expand All @@ -102,7 +102,7 @@ export class DkgClient {
return {
id: ritualId,
dkgPublicKey: DkgPublicKey.fromBytes(dkgPkBytes),
threshold: ritualParams.threshold,
threshold: ritual.dkgSize, // TODO: dkgSize is not the threshold, but in this case shares == threshold == dkgSize
} as DkgRitual;
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export { Alice } from './characters/alice';
export { Bob, RemoteBob } from './characters/bob';
export { Enrico } from './characters/enrico';
export { PreTDecDecrypter } from './characters/pre-recipient';
export { PreDecrypter } from './characters/pre-recipient';
export { Porter } from './characters/porter';

// Policies
Expand Down
15 changes: 3 additions & 12 deletions src/policies/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ export type EnactedPolicy = {

type IPreEnactedPolicy = Omit<EnactedPolicy, 'txHash'>;

export type EnactedPolicyJSON = Omit<
EnactedPolicy,
'policyKey' | 'encryptedTreasureMap' | 'id'
> & {
policyKey: Uint8Array;
id: Uint8Array;
encryptedTreasureMap: Uint8Array;
};

export class PreEnactedPolicy implements IPreEnactedPolicy {
constructor(
public readonly id: HRAC,
Expand Down Expand Up @@ -138,18 +129,18 @@ export class BlockchainPolicy {
ursulas: readonly Ursula[],
verifiedKFrags: readonly VerifiedKeyFrag[]
): TreasureMap {
const assigned_kfrags: [Address, [PublicKey, VerifiedKeyFrag]][] = [];
const assignedKFrags: [Address, [PublicKey, VerifiedKeyFrag]][] = [];
zip(ursulas, verifiedKFrags).forEach(([ursula, kFrag]) => {
const ursulaAddress = new Address(
toCanonicalAddress(ursula.checksumAddress)
);
assigned_kfrags.push([ursulaAddress, [ursula.encryptingKey, kFrag]]);
assignedKFrags.push([ursulaAddress, [ursula.encryptingKey, kFrag]]);
});
return new TreasureMap(
this.publisher.signer,
this.hrac,
this.delegatingKey,
assigned_kfrags,
assignedKFrags,
this.threshold
);
}
Expand Down
48 changes: 14 additions & 34 deletions src/sdk/cohort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,33 @@ import { Porter } from '../characters/porter';
import { ChecksumAddress } from '../types';
import { objectEquals } from '../utils';

export type CohortConfiguration = {
readonly threshold: number;
readonly shares: number;
readonly porterUri: string;
};

export type CohortJSON = {
ursulaAddresses: ChecksumAddress[];
threshold: number;
shares: number;
porterUri: string;
};

export class Cohort {
private constructor(
public ursulaAddresses: ChecksumAddress[],
public readonly configuration: CohortConfiguration
public readonly ursulaAddresses: ChecksumAddress[],
public readonly porterUri: string
) {}

public static async create(
configuration: CohortConfiguration,
porterUri: string,
numUrsulas: number,
include: string[] = [],
exclude: string[] = []
) {
const porter = new Porter(configuration.porterUri);
const ursulas = await porter.getUrsulas(
configuration.shares,
exclude,
include.splice(0, configuration.shares)
);
const porter = new Porter(porterUri);
const ursulas = await porter.getUrsulas(numUrsulas, exclude, include);
const ursulaAddresses = ursulas.map((ursula) => ursula.checksumAddress);
return new Cohort(ursulaAddresses, configuration);
return new Cohort(ursulaAddresses, porterUri);
}

public get shares(): number {
public static fromUrsulas(ursulas: ChecksumAddress[], porterUri: string) {
return new Cohort(ursulas, porterUri);
}
public get numUrsulas(): number {
return this.ursulaAddresses.length;
}

Expand All @@ -49,26 +41,14 @@ export class Cohort {
return Cohort.fromObj(config);
}

public static fromObj({
ursulaAddresses,
threshold,
shares,
porterUri,
}: CohortJSON) {
const config = {
threshold: threshold,
shares: shares,
porterUri: porterUri,
};
return new Cohort(ursulaAddresses, config);
public static fromObj({ ursulaAddresses, porterUri }: CohortJSON) {
return new Cohort(ursulaAddresses, porterUri);
}

public toObj(): CohortJSON {
return {
ursulaAddresses: this.ursulaAddresses,
threshold: this.configuration.threshold,
shares: this.configuration.shares,
porterUri: this.configuration.porterUri,
porterUri: this.porterUri,
};
}

Expand Down
36 changes: 20 additions & 16 deletions src/sdk/strategy/cbd-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { ethers } from 'ethers';

import { bytesEqual } from '../../../test/utils';
import {
CbdTDecDecrypter,
CbdTDecDecrypterJSON,
ThresholdDecrypter,
ThresholdDecrypterJSON,
} from '../../characters/cbd-recipient';
import { Enrico } from '../../characters/enrico';
import { ConditionExpression, ConditionExpressionJSON } from '../../conditions';
Expand All @@ -18,7 +18,7 @@ export type CbdStrategyJSON = {
};

export type DeployedStrategyJSON = {
decrypter: CbdTDecDecrypterJSON;
decrypter: ThresholdDecrypterJSON;
dkgPublicKey: Uint8Array;
};

Expand All @@ -32,13 +32,9 @@ export class CbdStrategy {
public async deploy(
provider: ethers.providers.Web3Provider
): Promise<DeployedCbdStrategy> {
const dkgRitualParams = {
threshold: this.cohort.configuration.threshold,
shares: this.cohort.configuration.shares,
};
const dkgClient = new DkgClient(provider);
const dkgRitual = await dkgClient.initializeRitual(dkgRitualParams);
return DeployedCbdStrategy.create(this.cohort, dkgRitual);
const dkgRitual = await dkgClient.initializeRitual();
return DeployedCbdStrategy.create(dkgRitual, this.cohort.porterUri);
}

public static fromJSON(json: string) {
Expand Down Expand Up @@ -66,18 +62,26 @@ export class CbdStrategy {

export class DeployedCbdStrategy {
private constructor(
public readonly decrypter: CbdTDecDecrypter,
public readonly decrypter: ThresholdDecrypter,
public readonly dkgPublicKey: DkgPublicKey
) {}

public static create(cohort: Cohort, dkgRitual: DkgRitual) {
const decrypter = CbdTDecDecrypter.create(
cohort.configuration.porterUri,
dkgRitual
);
public static create(dkgRitual: DkgRitual, porterUri: string) {
const decrypter = ThresholdDecrypter.create(porterUri, dkgRitual);
return new DeployedCbdStrategy(decrypter, dkgRitual.dkgPublicKey);
}

// TODO: This is analogous to create() above, is it useful?
public static async fromRitualId(
provider: ethers.providers.Web3Provider,
porterUri: string,
ritualId: number
): Promise<DeployedCbdStrategy> {
const dkgClient = new DkgClient(provider);
const dkgRitual = await dkgClient.getExistingRitual(ritualId);
return DeployedCbdStrategy.create(dkgRitual, porterUri);
}

public makeEncrypter(conditionExpr: ConditionExpression): Enrico {
return new Enrico(this.dkgPublicKey, undefined, conditionExpr);
}
Expand All @@ -93,7 +97,7 @@ export class DeployedCbdStrategy {

private static fromObj({ decrypter, dkgPublicKey }: DeployedStrategyJSON) {
return new DeployedCbdStrategy(
CbdTDecDecrypter.fromObj(decrypter),
ThresholdDecrypter.fromObj(decrypter),
DkgPublicKey.fromBytes(dkgPublicKey)
);
}
Expand Down
Loading

1 comment on commit 3df4f18

@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: 89.84 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: 433.59 KB
build/module/test: 42.97 KB
build/module: 687.50 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: 89.84 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: 437.50 KB
build/main/test: 46.88 KB
build/main: 695.31 KB
build: 1.35 MB

Please sign in to comment.