Skip to content

Commit 43786ce

Browse files
authored
Decrypt API Rework (#560)
2 parents ae5186f + 1e528aa commit 43786ce

File tree

26 files changed

+464
-356
lines changed

26 files changed

+464
-356
lines changed

examples/taco/nextjs/src/hooks/useTaco.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
encrypt,
77
initialize,
88
ThresholdMessageKit,
9+
USER_ADDRESS_PARAM_DEFAULT,
910
} from '@nucypher/taco';
1011
import { ethers } from 'ethers';
1112
import { useCallback, useEffect, useState } from 'react';
@@ -32,12 +33,13 @@ export default function useTaco({
3233
}
3334
const messageKit = ThresholdMessageKit.fromBytes(encryptedBytes);
3435
const authProvider = new EIP4361AuthProvider(provider, signer);
35-
return decrypt(
36-
provider,
37-
domain,
38-
messageKit,
36+
const conditionContext =
37+
conditions.context.ConditionContext.fromMessageKit(messageKit);
38+
conditionContext.addAuthProvider(
39+
USER_ADDRESS_PARAM_DEFAULT,
3940
authProvider,
4041
);
42+
return decrypt(provider, domain, messageKit, conditionContext);
4143
},
4244
[isInit, provider, domain],
4345
);

examples/taco/nodejs/src/index.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { format } from 'node:util';
22

33
import {
4+
EIP4361AuthProvider,
5+
ThresholdMessageKit,
6+
USER_ADDRESS_PARAM_DEFAULT,
47
conditions,
58
decrypt,
69
domains,
7-
EIP4361AuthProvider,
810
encrypt,
911
fromBytes,
1012
initialize,
1113
isAuthorized,
12-
ThresholdMessageKit,
1314
toBytes,
1415
toHexString,
1516
} from '@nucypher/taco';
@@ -108,17 +109,22 @@ const decryptFromBytes = async (encryptedBytes: Uint8Array) => {
108109
domain: 'localhost',
109110
uri: 'http://localhost:3000',
110111
};
111-
const authProvider = new EIP4361AuthProvider(
112-
provider,
113-
consumerSigner,
114-
siweParams,
115-
);
116-
return decrypt(
117-
provider,
118-
domain,
119-
messageKit,
120-
authProvider,
121-
);
112+
const conditionContext =
113+
conditions.context.ConditionContext.fromMessageKit(messageKit);
114+
115+
// illustrative optional example of checking what context parameters are required
116+
// unnecessary if you already know what the condition contains
117+
if (
118+
conditionContext.requestedContextParameters.has(USER_ADDRESS_PARAM_DEFAULT)
119+
) {
120+
const authProvider = new EIP4361AuthProvider(
121+
provider,
122+
consumerSigner,
123+
siweParams,
124+
);
125+
conditionContext.addAuthProvider(USER_ADDRESS_PARAM_DEFAULT, authProvider);
126+
}
127+
return decrypt(provider, domain, messageKit, conditionContext);
122128
};
123129

124130
const runExample = async () => {

examples/taco/react/src/hooks/useTaco.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
encrypt,
77
initialize,
88
ThresholdMessageKit,
9+
USER_ADDRESS_PARAM_DEFAULT,
910
} from '@nucypher/taco';
1011
import { ethers } from 'ethers';
1112
import { useCallback, useEffect, useState } from 'react';
@@ -32,12 +33,13 @@ export default function useTaco({
3233
}
3334
const messageKit = ThresholdMessageKit.fromBytes(encryptedBytes);
3435
const authProvider = new EIP4361AuthProvider(provider, signer);
35-
return decrypt(
36-
provider,
37-
domain,
38-
messageKit,
36+
const conditionContext =
37+
conditions.context.ConditionContext.fromMessageKit(messageKit);
38+
conditionContext.addAuthProvider(
39+
USER_ADDRESS_PARAM_DEFAULT,
3940
authProvider,
4041
);
42+
return decrypt(provider, domain, messageKit, conditionContext);
4143
},
4244
[isInit, provider, domain],
4345
);

examples/taco/webpack-5/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
fromBytes,
88
initialize,
99
toBytes,
10+
USER_ADDRESS_PARAM_DEFAULT,
1011
} from '@nucypher/taco';
1112
import { ethers } from 'ethers';
1213
import { hexlify } from 'ethers/lib/utils';
@@ -61,11 +62,14 @@ const runExample = async () => {
6162

6263
console.log('Decrypting message...');
6364
const authProvider = new EIP4361AuthProvider(provider, signer);
65+
const conditionContext =
66+
conditions.context.ConditionContext.fromMessageKit(messageKit);
67+
conditionContext.addAuthProvider(USER_ADDRESS_PARAM_DEFAULT, authProvider);
6468
const decryptedBytes = await decrypt(
6569
provider,
6670
domain,
6771
messageKit,
68-
authProvider,
72+
conditionContext,
6973
);
7074
const decryptedMessage = fromBytes(decryptedBytes);
7175
console.log('Decrypted message:', decryptedMessage);

packages/shared/src/porter.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ export const getPorterUri = async (domain: Domain): Promise<string> => {
3838
return (await getPorterUris(domain))[0];
3939
};
4040

41-
export const getPorterUris = async (
42-
domain: Domain,
43-
): Promise<string[]> => {
41+
export const getPorterUris = async (domain: Domain): Promise<string[]> => {
4442
const fullList = [];
4543
const uri = defaultPorterUri[domain];
4644
if (!uri) {

packages/shared/test/porter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('getPorterUris', () => {
5757
it('Get URIs from source', async () => {
5858
for (const domain of Object.values(domains)) {
5959
const uris = await getPorterUrisFromSource(domain);
60-
expect(uris.length).toBeGreaterThan(0);
60+
expect(uris.length).toBeGreaterThanOrEqual(0);
6161
const fullList = await getPorterUris(domain);
6262
expect(fullList).toEqual(expect.arrayContaining(uris));
6363
}
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
import { AuthSignature } from './auth-sig';
2-
import { EIP4361AuthProvider } from './providers';
3-
4-
export const EIP4361_AUTH_METHOD = 'EIP4361';
52

63
export interface AuthProvider {
74
getOrCreateAuthSignature(): Promise<AuthSignature>;
85
}
9-
10-
export type AuthProviders = {
11-
[EIP4361_AUTH_METHOD]?: EIP4361AuthProvider;
12-
// Fallback to satisfy type checking
13-
[key: string]: AuthProvider | undefined;
14-
};
15-
16-
export const USER_ADDRESS_PARAM_DEFAULT = ':userAddress';
17-
18-
export const AUTH_METHOD_FOR_PARAM: Record<string, string> = {
19-
[USER_ADDRESS_PARAM_DEFAULT]: EIP4361_AUTH_METHOD,
20-
};

packages/taco-auth/src/auth-sig.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { EthAddressSchema } from '@nucypher/shared';
22
import { z } from 'zod';
33

4-
import { EIP4361_AUTH_METHOD } from './auth-provider';
5-
import { EIP4361TypedDataSchema } from './providers';
4+
import {
5+
EIP4361_AUTH_METHOD,
6+
EIP4361TypedDataSchema,
7+
} from './providers/eip4361/common';
68

79
export const authSignatureSchema = z.object({
810
signature: z.string(),
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { SiweMessage } from 'siwe';
2+
import { z } from 'zod';
3+
4+
export const EIP4361_AUTH_METHOD = 'EIP4361';
5+
6+
const isSiweMessage = (message: string): boolean => {
7+
try {
8+
new SiweMessage(message);
9+
return true;
10+
} catch {
11+
return false;
12+
}
13+
};
14+
15+
export const EIP4361TypedDataSchema = z
16+
.string()
17+
.refine(isSiweMessage, { message: 'Invalid SIWE message' });

packages/taco-auth/src/providers/eip4361.ts renamed to packages/taco-auth/src/providers/eip4361/eip4361.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
import { ethers } from 'ethers';
22
import { generateNonce, SiweMessage } from 'siwe';
3-
import { z } from 'zod';
43

5-
import { EIP4361_AUTH_METHOD } from '../auth-provider';
6-
import { AuthSignature } from '../auth-sig';
7-
import { LocalStorage } from '../storage';
4+
import { AuthSignature } from '../../auth-sig';
5+
import { LocalStorage } from '../../storage';
86

9-
const isSiweMessage = (message: string): boolean => {
10-
try {
11-
new SiweMessage(message);
12-
return true;
13-
} catch {
14-
return false;
15-
}
16-
};
7+
import { EIP4361_AUTH_METHOD } from './common';
178

18-
export const EIP4361TypedDataSchema = z
19-
.string()
20-
.refine(isSiweMessage, { message: 'Invalid SIWE message' });
9+
export const USER_ADDRESS_PARAM_DEFAULT = ':userAddress';
2110

2211
export type EIP4361AuthProviderParams = {
2312
domain: string;

0 commit comments

Comments
 (0)