-
Notifications
You must be signed in to change notification settings - Fork 23
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
Decrypt API Rework #560
Decrypt API Rework #560
Changes from all commits
38eca23
bb99d4a
ee262c4
9e443e2
f2c8358
68788f6
53a83d2
9c5548c
dce202a
4f7c2ac
e613d3a
e9e332f
3c7cefb
cfc669d
8a751c6
83585f4
8c30bc4
81d9006
e872fff
99732a9
1e528aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
import { format } from 'node:util'; | ||
|
||
import { | ||
EIP4361AuthProvider, | ||
ThresholdMessageKit, | ||
USER_ADDRESS_PARAM_DEFAULT, | ||
conditions, | ||
decrypt, | ||
domains, | ||
EIP4361AuthProvider, | ||
encrypt, | ||
fromBytes, | ||
initialize, | ||
isAuthorized, | ||
ThresholdMessageKit, | ||
toBytes, | ||
toHexString, | ||
} from '@nucypher/taco'; | ||
|
@@ -108,17 +109,22 @@ const decryptFromBytes = async (encryptedBytes: Uint8Array) => { | |
domain: 'localhost', | ||
uri: 'http://localhost:3000', | ||
}; | ||
const authProvider = new EIP4361AuthProvider( | ||
provider, | ||
consumerSigner, | ||
siweParams, | ||
); | ||
return decrypt( | ||
provider, | ||
domain, | ||
messageKit, | ||
authProvider, | ||
); | ||
const conditionContext = | ||
conditions.context.ConditionContext.fromMessageKit(messageKit); | ||
|
||
// illustrative optional example of checking what context parameters are required | ||
// unnecessary if you already know what the condition contains | ||
if ( | ||
conditionContext.requestedContextParameters.has(USER_ADDRESS_PARAM_DEFAULT) | ||
) { | ||
const authProvider = new EIP4361AuthProvider( | ||
provider, | ||
consumerSigner, | ||
siweParams, | ||
); | ||
conditionContext.addAuthProvider(USER_ADDRESS_PARAM_DEFAULT, authProvider); | ||
} | ||
return decrypt(provider, domain, messageKit, conditionContext); | ||
Comment on lines
+118
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this avoids having to hardcode context parameters – i.e. the app doesn't need to know the conditions in advance in order to match them to the right authentication method? That's pretty amazing! One thing I'm wondering is – what resource is being queried here? Do we need to create a public resource that maps conditions to context parameters? Or is this a list that developers will create for their own domain? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Indeed. This is the case where an application doesn't just use the same condition every time, but perhaps some permutations of a set of conditions, each with varying context parameters. In this way the dev can use logic to get the set of expected parameters and populate them based on their own logic. They could have dynamic logic like the following: if (requestedContextParmeters.has(":tokenId")) {
// user subjected to condition 1 which uses ":tokenId" custom context parameter
const value = ...
conditionContext.addCustomContextParameters({":tokenId": value});
}
if (requestedContextParmeters.has(":otherParam")) {
// user subjected to condition 2 which uses ":otherParam" custom context parameter
const otherValue = ...
conditionContext.addCustomContextParameters({":otherParam": otherValue});
}
if (requestedContextParmeters.has(":userAddress")) {
// user subjected to condition with taco SIWE
const authProvider = new EIP4361AuthProvider(...);
conditionContext.addAuthProvider(":userAddress", auth provider);
}
if (requestedContextParmeters.has(":userAddressEIP4361")) {
// user subjected to condition with single-sign on SIWE
const authProvider = SingleSignOnEIP4361AuthProvider.fromExistingSiweInfo(...)
conditionContext.addAuthProvider(":userAddressEIP4361", authProvider);
}
...
Assuming I correctly understand the question, the
Interesting. To me, it's the latter. Devs can decide how they want to get/generate the values to use for conditions. With something like a public mapping resource (condition string/type -> context parameters...?), it may be easy for simple conditions eg. |
||
}; | ||
|
||
const runExample = async () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,5 @@ | ||
import { AuthSignature } from './auth-sig'; | ||
import { EIP4361AuthProvider } from './providers'; | ||
|
||
export const EIP4361_AUTH_METHOD = 'EIP4361'; | ||
|
||
export interface AuthProvider { | ||
getOrCreateAuthSignature(): Promise<AuthSignature>; | ||
} | ||
|
||
export type AuthProviders = { | ||
[EIP4361_AUTH_METHOD]?: EIP4361AuthProvider; | ||
// Fallback to satisfy type checking | ||
[key: string]: AuthProvider | undefined; | ||
}; | ||
|
||
export const USER_ADDRESS_PARAM_DEFAULT = ':userAddress'; | ||
|
||
export const AUTH_METHOD_FOR_PARAM: Record<string, string> = { | ||
[USER_ADDRESS_PARAM_DEFAULT]: EIP4361_AUTH_METHOD, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { SiweMessage } from 'siwe'; | ||
import { z } from 'zod'; | ||
|
||
export const EIP4361_AUTH_METHOD = 'EIP4361'; | ||
|
||
const isSiweMessage = (message: string): boolean => { | ||
try { | ||
new SiweMessage(message); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
|
||
export const EIP4361TypedDataSchema = z | ||
.string() | ||
.refine(isSiweMessage, { message: 'Invalid SIWE message' }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './eip4361'; | ||
export * from './external-eip4361'; | ||
export * from './eip4361/eip4361'; | ||
export * from './eip4361/external-eip4361'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beautiful! 🚀