Skip to content

Commit

Permalink
chore: rebase over main
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Nov 27, 2023
1 parent b891d08 commit f4f9bd8
Show file tree
Hide file tree
Showing 38 changed files with 770 additions and 386 deletions.
3 changes: 2 additions & 1 deletion demos/taco-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"react": "^18.2.0",
"react-copy-to-clipboard": "^5.1.0",
"react-dom": "^18.2.0",
"react-spinners": "^0.13.6"
"react-spinners": "^0.13.6",
"viem": "^1.19.9"
},
"devDependencies": {
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
Expand Down
3 changes: 2 additions & 1 deletion demos/taco-nft-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"react": "^18.2.0",
"react-copy-to-clipboard": "^5.1.0",
"react-dom": "^18.2.0",
"react-spinners": "^0.13.6"
"react-spinners": "^0.13.6",
"viem": "^1.19.9"
},
"devDependencies": {
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
Expand Down
13 changes: 8 additions & 5 deletions examples/pre/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
},
"dependencies": {
"@nucypher/pre": "workspace:*",
"ethers": "^5.7.2",
"next": "14.0.3",
"react": "18.2.0",
"react-dom": "18.2.0",
"viem": "^1.19.9"
},
"devDependencies": {
"@types/node": "20.6.3",
"@types/react": "18.2.37",
"@types/react-dom": "18.2.14",
"eslint": "8.53.0",
"eslint-config-next": "14.0.2",
"ethers": "^5.7.2",
"next": "14.0.3",
"react": "18.2.0",
"react-dom": "18.2.0"
"eslint-config-next": "14.0.2"
}
}
7 changes: 5 additions & 2 deletions examples/pre/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
},
"dependencies": {
"@nucypher/pre": "workspace:*",
"dotenv": "^16.3.1",
"ethers": "^5.7.2"
"ethers": "^5.7.2",
"viem": "^1.19.9"
},
"devDependencies": {
"dotenv": "^16.3.1"
}
}
3 changes: 2 additions & 1 deletion examples/pre/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"@nucypher/pre": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"ethers": "^5.7.2"
"ethers": "^5.7.2",
"viem": "^1.19.9"
},
"devDependencies": {
"@types/node": "^16.18.50",
Expand Down
3 changes: 2 additions & 1 deletion examples/pre/webpack-5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"type-check": "tsc"
},
"dependencies": {
"@nucypher/pre": "workspace:*"
"@nucypher/pre": "workspace:*",
"viem": "^1.19.9"
},
"devDependencies": {
"copy-webpack-plugin": "^11.0.0",
Expand Down
14 changes: 8 additions & 6 deletions examples/taco/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
"dependencies": {
"@nucypher/shared": "workspace:*",
"@nucypher/taco": "workspace:*",
"@types/node": "20.6.3",
"@types/react": "18.2.37",
"@types/react-dom": "18.2.14",
"eslint": "8.53.0",
"eslint-config-next": "14.0.2",
"ethers": "^5.7.2",
"next": "14.0.3",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "5.2.2"
"viem": "^1.19.9"
},
"devDependencies": {
"@types/node": "20.6.3",
"@types/react": "18.2.37",
"@types/react-dom": "18.2.14",
"eslint": "8.53.0",
"eslint-config-next": "14.0.2"
}
}
2 changes: 1 addition & 1 deletion examples/taco/nextjs/src/hooks/useTaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function useTaco({
}: {
ritualId: number;
domain: Domain;
provider: ethers.providers.Provider | undefined;
publicClient: PublicClient | undefined;
}) {
const [isInit, setIsInit] = useState(false);

Expand Down
7 changes: 5 additions & 2 deletions examples/taco/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
},
"dependencies": {
"@nucypher/taco": "workspace:*",
"dotenv": "^16.3.1",
"ethers": "^5.7.2"
"ethers": "^5.7.2",
"viem": "^1.19.9"
},
"devDependencies": {
"dotenv": "^16.3.1"
}
}
7 changes: 3 additions & 4 deletions examples/taco/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@
"dependencies": {
"@nucypher/shared": "workspace:*",
"@nucypher/taco": "workspace:*",
"ethers": "^5.7.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"viem": "^1.19.9"
},
"devDependencies": {
"@types/node": "^16.18.50",
"@types/react": "^18.2.37",
"@types/react-dom": "^18.2.14",
"react-scripts": "^5.0.1"
},
"peerDependencies": {
"ethers": "^5.7.2"
}
}
2 changes: 1 addition & 1 deletion examples/taco/react/src/hooks/useTaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function useTaco({
}: {
ritualId: number;
domain: Domain;
provider: ethers.providers.Provider | undefined;
publicClient: PublicClient | undefined;
}) {
const [isInit, setIsInit] = useState(false);

Expand Down
7 changes: 3 additions & 4 deletions examples/taco/webpack-5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
"type-check": "tsc"
},
"dependencies": {
"@nucypher/taco": "workspace:*"
"@nucypher/taco": "workspace:*",
"viem": "^1.19.9",
"ethers": "^5.7.2"
},
"devDependencies": {
"copy-webpack-plugin": "^11.0.0",
"esbuild-loader": "^2.11.0",
"webpack": "^5.4.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.7.4"
},
"peerDependencies": {
"ethers": "^5.7.2"
}
}
3 changes: 2 additions & 1 deletion packages/pre/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"dependencies": {
"@nucypher/nucypher-core": "^0.13.0-alpha.1",
"@nucypher/shared": "workspace:*",
"ethers": "^5.7.2"
"ethers": "^5.7.2",
"viem": "^1.19.9"
},
"devDependencies": {
"@nucypher/test-utils": "workspace:*"
Expand Down
34 changes: 20 additions & 14 deletions packages/pre/src/characters/alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import {
Signer,
VerifiedKeyFrag,
} from '@nucypher/nucypher-core';
import { ChecksumAddress, Domain, PorterClient } from '@nucypher/shared';
import { ethers } from 'ethers';
import {
ChecksumAddress,
Domain,
PorterClient,
toPublicClient,
} from '@nucypher/shared';
import { PublicClient, WalletClient } from 'viem';
import { getBlock, getBlockNumber } from 'viem/actions';

import { Keyring } from '../keyring';
import {
Expand Down Expand Up @@ -41,8 +47,7 @@ export class Alice {
}

public async grant(
provider: ethers.providers.Provider,
signer: ethers.Signer,
walletClient: WalletClient,
domain: Domain,
porterUri: string,
policyParameters: BlockchainPolicyParameters,
Expand All @@ -55,12 +60,12 @@ export class Alice {
excludeUrsulas,
includeUrsulas,
);
const policy = await this.createPolicy(provider, policyParameters);
return await policy.enact(provider, signer, domain, ursulas);
const policy = await this.createPolicy(walletClient, policyParameters);
return await policy.enact(walletClient, domain, ursulas);
}

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

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

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

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

const blockNumber = await provider.getBlockNumber();
const block = await provider.getBlock(blockNumber);
const blockTime = new Date(block.timestamp * 1000);
const blockNumber = await getBlockNumber(publicClient);
const block = await getBlock(publicClient, { blockNumber });
const blockTime = new Date(Number(block.timestamp) * 1000);
if (endDate < blockTime) {
throw new Error(
`End date must be in the future, ${endDate} is earlier than block time ${blockTime}).`,
Expand Down
27 changes: 14 additions & 13 deletions packages/pre/src/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import {
toBytes,
toCanonicalAddress,
toEpoch,
toPublicClient,
Ursula,
zip,
} from '@nucypher/shared';
import { ethers } from 'ethers';
import { WalletClient } from 'viem';

import { Alice, RemoteBob } from './characters';

Expand Down Expand Up @@ -47,35 +48,36 @@ export class PreEnactedPolicy implements IPreEnactedPolicy {
) {}

public async enact(
provider: ethers.providers.Provider,
signer: ethers.Signer,
walletClient: WalletClient,
domain: Domain,
): Promise<EnactedPolicy> {
const txHash = await this.publish(provider, signer, domain);
const txHash = await this.publish(walletClient, domain);
return {
...this,
txHash,
};
}

private async publish(
provider: ethers.providers.Provider,
signer: ethers.Signer,
walletClient: WalletClient,
domain: Domain,
): Promise<string> {
const publicClient = toPublicClient(walletClient);
const startTimestamp = toEpoch(this.startTimestamp);
const endTimestamp = toEpoch(this.endTimestamp);
const ownerAddress = await signer.getAddress();
const ownerAddress = await walletClient.account?.address;
if (!ownerAddress) {
throw new Error('No account set');
}
const value = await PreSubscriptionManagerAgent.getPolicyCost(
provider,
publicClient,
domain,
this.size,
startTimestamp,
endTimestamp,
);
const tx = await PreSubscriptionManagerAgent.createPolicy(
provider,
signer,
walletClient,
domain,
value,
this.id.toBytes(),
Expand Down Expand Up @@ -119,13 +121,12 @@ export class BlockchainPolicy {
}

public async enact(
provider: ethers.providers.Provider,
signer: ethers.Signer,
walletClient: WalletClient,
domain: Domain,
ursulas: readonly Ursula[],
): Promise<EnactedPolicy> {
const preEnacted = await this.generatePreEnactedPolicy(ursulas);
return await preEnacted.enact(provider, signer, domain);
return await preEnacted.enact(walletClient, domain);
}

public async generatePreEnactedPolicy(
Expand Down
6 changes: 2 additions & 4 deletions packages/pre/test/acceptance/alice-grants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import { ChecksumAddress, domains, initialize, Ursula } from '@nucypher/shared';
import {
bytesEqual,
fakePorterUri,
fakeProvider,
fakeSigner,
fakeUrsulas,
fakeWalletClient,
fromBytes,
mockGetUrsulas,
mockRetrieveCFragsRequest,
Expand Down Expand Up @@ -71,8 +70,7 @@ describe('story: alice shares message with bob through policy', () => {
endDate,
};
policy = await alice.grant(
fakeProvider(),
fakeSigner(),
fakeWalletClient,
domains.DEV,
fakePorterUri,
policyParams,
Expand Down
Loading

0 comments on commit f4f9bd8

Please sign in to comment.