Skip to content

Commit

Permalink
chore(refactor): divide shared package into pre and taco
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-roslaniec committed Sep 25, 2023
1 parent 76123ef commit 0e5470e
Show file tree
Hide file tree
Showing 47 changed files with 565 additions and 593 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ examples/*/pnpm-lock.yaml
pnpm-debug.log
docs-json
./docs
.next
3 changes: 1 addition & 2 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type-check": "tsc -p tsconfig.build.json"
},
"dependencies": {
"@nucypher/shared": "workspace:*",
"@nucypher/pre": "workspace:*",
"@types/node": "20.6.3",
"@types/react": "18.2.22",
"@types/react-dom": "18.2.7",
Expand All @@ -22,7 +22,6 @@
"typescript": "5.2.2"
},
"peerDependencies": {
"@nucypher/shared": "workspace:*",
"ethers": "^5.7.2",
"typescript": "5.2.2"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
initialize,
SecretKey,
toHexString,
} from '@nucypher/shared';
} from '@nucypher/pre';
import {ethers} from 'ethers';
import {useEffect, useState} from 'react';

Expand Down
4 changes: 3 additions & 1 deletion examples/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"type-check": "tsc"
},
"dependencies": {
"@nucypher/shared": "workspace:*",
"@nucypher/pre": "workspace:*"
},
"peerDependencies": {
"ethers": "^5.7.2"
}
}
2 changes: 1 addition & 1 deletion examples/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
initialize,
SecretKey,
toBytes,
} from '@nucypher/shared';
} from '@nucypher/pre';
import { ethers } from 'ethers';

const makeAlice = () => {
Expand Down
5 changes: 5 additions & 0 deletions examples/pre/nextjs/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
6 changes: 4 additions & 2 deletions examples/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
]
},
"dependencies": {
"@nucypher/shared": "workspace:*",
"ethers": "^5.7.2",
"@nucypher/pre": "workspace:*",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand All @@ -33,5 +32,8 @@
"@types/react": "^18.2.21",
"@types/react-dom": "^18.2.7",
"react-scripts": "^5.0.1"
},
"peerDependencies": {
"ethers": "^5.7.2"
}
}
2 changes: 1 addition & 1 deletion examples/react/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
initialize,
SecretKey,
toHexString,
} from '@nucypher/shared';
} from '@nucypher/pre';
import { ethers } from 'ethers';
import { useEffect, useState } from 'react';

Expand Down
5 changes: 5 additions & 0 deletions examples/taco/nextjs/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
6 changes: 4 additions & 2 deletions examples/webpack-5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
"type-check": "tsc"
},
"dependencies": {
"@nucypher/shared": "workspace:*",
"ethers": "^5.7.2"
"@nucypher/pre": "workspace:*"
},
"devDependencies": {
"copy-webpack-plugin": "^10.2.4",
"esbuild-loader": "^2.11.0",
"webpack": "^5.4.0",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.7.4"
},
"peerDependencies": {
"ethers": "^5.7.2"
}
}
8 changes: 1 addition & 7 deletions examples/webpack-5/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
Alice,
Bob,
SecretKey,
getPorterUri,
initialize,
} from '@nucypher/shared';
import { Alice, Bob, SecretKey, getPorterUri, initialize } from '@nucypher/pre';
import { ethers } from 'ethers';

declare global {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Signer,
VerifiedKeyFrag,
} from '@nucypher/nucypher-core';
import { ChecksumAddress, PorterClient } from '@nucypher/shared';
import { ethers } from 'ethers';

import { Keyring } from '../keyring';
Expand All @@ -13,8 +14,6 @@ import {
EnactedPolicy,
PreEnactedPolicy,
} from '../policy';
import { PorterClient } from '../porter';
import { ChecksumAddress } from '../types';

import { RemoteBob } from './bob';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import {
SecretKey,
Signer,
} from '@nucypher/nucypher-core';
import { PorterClient, zip } from '@nucypher/shared';

import { Keyring } from '../keyring';
import { PolicyMessageKit, RetrievalResult } from '../kits';
import { PorterClient } from '../porter';
import { zip } from '../utils';

export class RemoteBob {
private constructor(
Expand Down
39 changes: 39 additions & 0 deletions packages/pre/src/characters/enrico.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { MessageKit, PublicKey, SecretKey } from '@nucypher/nucypher-core';
import { ConditionExpression, toBytes } from '@nucypher/shared';

import { Keyring } from '../keyring';

export class Enrico {
public readonly encryptingKey: PublicKey;
private readonly keyring: Keyring;
public conditions?: ConditionExpression | undefined;

constructor(
encryptingKey: PublicKey,
verifyingKey?: SecretKey,
conditions?: ConditionExpression,
) {
this.encryptingKey = encryptingKey;
this.keyring = new Keyring(verifyingKey ?? SecretKey.random());
this.conditions = conditions;
}

public get verifyingKey(): PublicKey {
return this.keyring.publicKey;
}

public encryptMessagePre(
plaintext: Uint8Array | string,
withConditions?: ConditionExpression,
): MessageKit {
if (!withConditions) {
withConditions = this.conditions;
}

return new MessageKit(
this.encryptingKey,
plaintext instanceof Uint8Array ? plaintext : toBytes(plaintext),
withConditions ? withConditions.toWASMConditions() : null,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './alice';
export * from './bob';
export * from './enrico';
export * from './pre-recipient';
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import {
base64ToU8Receiver,
ConditionContext,
ConditionExpression,
Keyring,
PolicyMessageKit,
PorterClient,
RetrievalResult,
toJSON,
zip,
} from '@nucypher/shared';
import { ethers } from 'ethers';

import { Keyring } from '../keyring';
import { PolicyMessageKit, RetrievalResult } from '../kits';

export type PreDecrypterJSON = {
porterUri: string;
policyEncryptingKeyBytes: Uint8Array;
Expand Down
21 changes: 7 additions & 14 deletions packages/pre/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
// TODO: Create a pre module and export it here
// Similarly to how taco works
// export {pre} from './pre';
// What goes into the pre module? Should we re-export the basic building blocks and/or remake the helper methods?
export {
Alice,
BlockchainPolicyParameters,
Bob,
Cohort,
EnactedPolicy,
Enrico,
Keyring,
PolicyMessageKit,
PorterClient,
PreEnactedPolicy,
RemoteBob,
conditions,
fromHexString,
getPorterUri,
toBytes,
toHexString,
} from '@nucypher/shared';

export {
Expand All @@ -27,8 +17,11 @@ export {
SecretKey,
Signer,
TreasureMap,
initialize,
} from '@nucypher/nucypher-core';

export { DeployedPreStrategy, PreStrategy } from './pre-strategy';

export { PreDecrypter } from './pre-recipient';
export { Alice, Bob, PreDecrypter } from './characters';

export { EnactedPolicy } from './policy';
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
Signer,
VerifiedKeyFrag,
} from '@nucypher/nucypher-core';
import { toBytes } from '@nucypher/shared';

import { PolicyMessageKit } from './kits';
import { toBytes } from './utils';

export class Keyring {
constructor(public readonly secretKey: SecretKey) {}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
RetrievalKit,
SecretKey,
} from '@nucypher/nucypher-core';

import { ChecksumAddress } from '../types';
import { ChecksumAddress } from '@nucypher/shared';

import { RetrievalResult } from './retrieval';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { VerifiedCapsuleFrag } from '@nucypher/nucypher-core';

import { ChecksumAddress } from '../types';
import { ChecksumAddress } from '@nucypher/shared';

export class RetrievalResult {
constructor(
Expand Down
12 changes: 8 additions & 4 deletions packages/shared/src/policy.ts → packages/pre/src/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import {
TreasureMap,
VerifiedKeyFrag,
} from '@nucypher/nucypher-core';
import {
PreSubscriptionManagerAgent,
toBytes,
toCanonicalAddress,
toEpoch,
Ursula,
zip,
} from '@nucypher/shared';
import { ethers } from 'ethers';

import { Alice, RemoteBob } from './characters';
import { PreSubscriptionManagerAgent } from './contracts';
import { Ursula } from './porter';
import { toBytes, toEpoch, zip } from './utils';
import { toCanonicalAddress } from './web3';

export type EnactedPolicy = {
readonly id: HRAC;
Expand Down
13 changes: 8 additions & 5 deletions packages/pre/src/pre-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { PublicKey, SecretKey } from '@nucypher/nucypher-core';
import {
Alice,
base64ToU8Receiver,
Bob,
Cohort,
CohortJSON,
ConditionExpression,
EnactedPolicy,
Enrico,
toJSON,
} from '@nucypher/shared';
import { ethers } from 'ethers';

import { PreDecrypter, PreDecrypterJSON } from './pre-recipient';
import {
Alice,
Bob,
Enrico,
PreDecrypter,
PreDecrypterJSON,
} from './characters';
import { EnactedPolicy } from './policy';

export type PreStrategyJSON = {
cohort: CohortJSON;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// Disabling because we want to access Alice.keyring which is a private property
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
ConditionExpression,
Enrico,
ERC721Ownership,
PolicyMessageKit,
RetrievalResult,
toBytes,
} from '@nucypher/shared';
import {
bytesEqual,
fakeAlice,
Expand All @@ -9,15 +17,6 @@ import {
} from '@nucypher/test-utils';
import { expect, test } from 'vitest';

import {
ConditionExpression,
Enrico,
ERC721Ownership,
PolicyMessageKit,
RetrievalResult,
toBytes,
} from '../../src';

test('enrico', () => {
test('alice decrypts message encrypted by enrico', async () => {
const label = 'fake-label';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { MessageKit, toBytes } from '@nucypher/shared';
import { fakeBob } from '@nucypher/test-utils';
import { expect, test } from 'vitest';

import { MessageKit, toBytes } from '../../src';

test('message kit', () => {
test('bob decrypts', () => {
const bob = fakeBob();
Expand Down
Loading

0 comments on commit 0e5470e

Please sign in to comment.