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

Seperate signer from provider in user-facing API #270

Merged
merged 3 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@nucypher/nucypher-core": "^0.12.0",
"axios": "^0.21.1",
"deep-equal": "^2.2.1",
"ethers": "^5.4.1",
"ethers": "^5.7.2",
"joi": "^17.7.0",
"qs": "^6.10.1",
"semver": "^7.5.2"
Expand Down
16 changes: 9 additions & 7 deletions src/agents/coordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ export class DkgCoordinatorAgent {
}

public static async initializeRitual(
provider: ethers.providers.Web3Provider,
provider: ethers.providers.Provider,
signer: ethers.Signer,
providers: ChecksumAddress[]
): Promise<number> {
const Coordinator = await this.connectReadWrite(provider);
const Coordinator = await this.connectReadWrite(provider, signer);
const tx = await Coordinator.initiateRitual(providers);
const txReceipt = await tx.wait(DEFAULT_WAIT_N_CONFIRMATIONS);
const [ritualStartEvent] = txReceipt.events ?? [];
Expand All @@ -79,15 +80,15 @@ export class DkgCoordinatorAgent {
}

public static async getRitualState(
provider: ethers.providers.Web3Provider,
provider: ethers.providers.Provider,
ritualId: number
): Promise<DkgRitualState> {
const Coordinator = await this.connectReadOnly(provider);
return await Coordinator.getRitualState(ritualId);
}

public static async onRitualEndEvent(
provider: ethers.providers.Web3Provider,
provider: ethers.providers.Provider,
ritualId: number,
callback: (successful: boolean) => void
): Promise<void> {
Expand All @@ -109,14 +110,15 @@ export class DkgCoordinatorAgent {
}

private static async connectReadWrite(
web3Provider: ethers.providers.Web3Provider
provider: ethers.providers.Provider,
signer: ethers.Signer
) {
return await this.connect(web3Provider, web3Provider.getSigner());
return await this.connect(provider, signer);
}

private static async connect(
provider: ethers.providers.Provider,
signer?: ethers.providers.JsonRpcSigner
signer?: ethers.Signer
): Promise<Coordinator> {
const network = await provider.getNetwork();
const contractAddress = getContract(network.chainId, 'COORDINATOR');
Expand Down
12 changes: 7 additions & 5 deletions src/agents/subscription-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import { DEFAULT_WAIT_N_CONFIRMATIONS, getContract } from './contracts';

export class PreSubscriptionManagerAgent {
public static async createPolicy(
web3Provider: ethers.providers.Web3Provider,
provider: ethers.providers.Provider,
signer: ethers.Signer,
valueInWei: BigNumber,
policyId: Uint8Array,
size: number,
startTimestamp: number,
endTimestamp: number,
ownerAddress: ChecksumAddress
): Promise<ContractTransaction> {
const SubscriptionManager = await this.connectReadWrite(web3Provider);
const SubscriptionManager = await this.connectReadWrite(provider, signer);
const overrides = {
value: valueInWei.toString(),
};
Expand Down Expand Up @@ -66,14 +67,15 @@ export class PreSubscriptionManagerAgent {
}

private static async connectReadWrite(
web3Provider: ethers.providers.Web3Provider
provider: ethers.providers.Provider,
signer: ethers.Signer
) {
return await this.connect(web3Provider, web3Provider.getSigner());
return await this.connect(provider, signer);
}

private static async connect(
provider: ethers.providers.Provider,
signer?: ethers.providers.JsonRpcSigner
signer?: ethers.Signer
derekpierre marked this conversation as resolved.
Show resolved Hide resolved
): Promise<SubscriptionManager> {
const network = await provider.getNetwork();
const contractAddress = getContract(
Expand Down
21 changes: 11 additions & 10 deletions src/characters/alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export class Alice {
}

public async grant(
web3Provider: ethers.providers.Web3Provider,
provider: ethers.providers.Provider,
signer: ethers.Signer,
porterUri: string,
policyParameters: BlockchainPolicyParameters,
includeUrsulas?: readonly ChecksumAddress[],
Expand All @@ -54,12 +55,12 @@ export class Alice {
excludeUrsulas,
includeUrsulas
);
const policy = await this.createPolicy(web3Provider, policyParameters);
return await policy.enact(web3Provider, ursulas);
const policy = await this.createPolicy(provider, policyParameters);
return await policy.enact(provider, signer, ursulas);
}

public async generatePreEnactedPolicy(
web3Provider: ethers.providers.Web3Provider,
provider: ethers.providers.Provider,
porterUri: string,
policyParameters: BlockchainPolicyParameters,
includeUrsulas?: readonly ChecksumAddress[],
Expand All @@ -71,7 +72,7 @@ export class Alice {
excludeUrsulas,
includeUrsulas
);
const policy = await this.createPolicy(web3Provider, policyParameters);
const policy = await this.createPolicy(provider, policyParameters);
return await policy.generatePreEnactedPolicy(ursulas);
}

Expand All @@ -94,11 +95,11 @@ export class Alice {
}

private async createPolicy(
web3Provider: ethers.providers.Web3Provider,
provider: ethers.providers.Provider,
rawParameters: BlockchainPolicyParameters
): Promise<BlockchainPolicy> {
const { bob, label, threshold, shares, startDate, endDate } =
await this.validatePolicyParameters(web3Provider, rawParameters);
await this.validatePolicyParameters(provider, rawParameters);
const { delegatingKey, verifiedKFrags } = this.generateKFrags(
bob,
label,
Expand All @@ -119,7 +120,7 @@ export class Alice {
}

private async validatePolicyParameters(
web3Provider: ethers.providers.Web3Provider,
provider: ethers.providers.Provider,
rawParams: BlockchainPolicyParameters
): Promise<BlockchainPolicyParameters> {
const startDate = rawParams.startDate ?? new Date();
Expand All @@ -141,8 +142,8 @@ export class Alice {
);
}

const blockNumber = await web3Provider.getBlockNumber();
const block = await web3Provider.getBlock(blockNumber);
const blockNumber = await provider.getBlockNumber();
const block = await provider.getBlock(blockNumber);
const blockTime = new Date(block.timestamp * 1000);
if (endDate < blockTime) {
throw new Error(
Expand Down
22 changes: 14 additions & 8 deletions src/characters/cbd-recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ export class ThresholdDecrypter {

// Retrieve and decrypt ciphertext using provider and condition expression
public async retrieveAndDecrypt(
provider: ethers.providers.Web3Provider,
provider: ethers.providers.Provider,
signer: ethers.Signer,
conditionExpr: ConditionExpression,
ciphertext: Ciphertext
): Promise<Uint8Array> {
const acp = await this.makeAcp(provider, conditionExpr, ciphertext);
const acp = await this.makeAcp(provider, signer, conditionExpr, ciphertext);

const decryptionShares = await this.retrieve(
provider,
conditionExpr,
ciphertext,
acp
acp,
signer
);

const sharedSecret = combineDecryptionSharesSimple(decryptionShares);
Expand All @@ -67,7 +69,8 @@ export class ThresholdDecrypter {
}

private async makeAcp(
provider: ethers.providers.Web3Provider,
provider: ethers.providers.Provider,
signer: ethers.Signer,
conditionExpr: ConditionExpression,
ciphertext: Ciphertext
) {
Expand All @@ -81,23 +84,26 @@ export class ThresholdDecrypter {
);

const headerHash = keccak256(ciphertext.header.toBytes());
const authorization = await provider.getSigner().signMessage(headerHash);
const authorization = await signer.signMessage(headerHash);

return new AccessControlPolicy(authData, toBytes(authorization));
}

// Retrieve decryption shares
public async retrieve(
provider: ethers.providers.Web3Provider,
provider: ethers.providers.Provider,
conditionExpr: ConditionExpression,
ciphertext: Ciphertext,
acp: AccessControlPolicy
acp: AccessControlPolicy,
signer?: ethers.Signer
): Promise<DecryptionShareSimple[]> {
const dkgParticipants = await DkgCoordinatorAgent.getParticipants(
provider,
this.ritualId
);
const contextStr = await conditionExpr.buildContext(provider).toJson();
const contextStr = await conditionExpr
.buildContext(provider, {}, signer)
.toJson();
const { sharedSecrets, encryptedRequests } = this.makeDecryptionRequests(
this.ritualId,
ciphertext,
Expand Down
23 changes: 17 additions & 6 deletions src/characters/pre-recipient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,15 @@ export class PreDecrypter {
}

public async retrieveAndDecrypt(
messageKits: readonly MessageKit[],
provider: ethers.providers.Web3Provider
provider: ethers.providers.Provider,
signer: ethers.Signer,
messageKits: readonly MessageKit[]
): Promise<readonly Uint8Array[]> {
const policyMessageKits = await this.retrieve(messageKits, provider);
const policyMessageKits = await this.retrieve(
provider,
signer,
messageKits
);

policyMessageKits.forEach((mk: PolicyMessageKit) => {
if (!mk.isDecryptableByReceiver()) {
Expand All @@ -90,8 +95,9 @@ export class PreDecrypter {
}

public async retrieve(
messageKits: readonly MessageKit[],
provider: ethers.providers.Web3Provider
provider: ethers.providers.Provider,
signer: ethers.Signer,
messageKits: readonly MessageKit[]
): Promise<readonly PolicyMessageKit[]> {
const treasureMap = this.encryptedTreasureMap.decrypt(
this.keyring.secretKey,
Expand All @@ -106,7 +112,12 @@ export class PreDecrypter {
.reduce((acc: ConditionExpression[], val) => acc.concat(val), [])
.map((condExpr: ConditionExpression) => condExpr.condition);

const conditionContext = new ConditionContext(conditions, provider);
const conditionContext = new ConditionContext(
provider,
conditions,
{},
signer
);

const policyMessageKits = messageKits.map((mk) =>
PolicyMessageKit.fromMessageKit(
Expand Down
5 changes: 5 additions & 0 deletions src/conditions/base/condition.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Joi from 'joi';

import { objectEquals } from '../../utils';
import { USER_ADDRESS_PARAM } from '../const';

type Map = Record<string, unknown>;

Expand All @@ -19,6 +20,10 @@ export class Condition {
return this.schema.validate(newValue);
}

public requiresSigner(): boolean {
return JSON.stringify(this.value).includes(USER_ADDRESS_PARAM);
}

public toObj(): Map {
const { error, value } = this.validate(this.value);
if (error) {
Expand Down
17 changes: 14 additions & 3 deletions src/conditions/condition-expr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from './base';
import { BLOCKTIME_METHOD } from './base/time';
import { CompoundCondition } from './compound-condition';
import { ConditionContext } from './context';
import { ConditionContext, CustomContextParam } from './context';

export type ConditionExpressionJSON = {
version: string;
Expand Down Expand Up @@ -85,9 +85,20 @@ export class ConditionExpression {
}

public buildContext(
provider: ethers.providers.Web3Provider
provider: ethers.providers.Provider,
customParameters: Record<string, CustomContextParam> = {},
signer?: ethers.Signer
): ConditionContext {
return new ConditionContext([this.condition], provider);
return new ConditionContext(
provider,
[this.condition],
customParameters,
signer
);
}

public contextRequiresSigner(): boolean {
return this.condition.requiresSigner();
}

public asAad(): Uint8Array {
Expand Down
Loading
Loading