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

[GAL-285] Fix gnosis safe auth #934

Merged
merged 2 commits into from
Aug 11, 2022
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
5 changes: 5 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Gallery",
"description": "Share your collection with the world.",
"iconPath": "https://gallery.so/favicon.ico"
}
Comment on lines +1 to +5
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added this so that we can be recognized as a "Safe App" by gnosis safe. there's more we can do but this at least gets us over the first step

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! great idea

Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export default function useLoginOrRedirectToOnboarding() {
{
pathname: '/welcome',
query: {
chain: 'Ethereum',
authMechanismType: 'eoa',
chain: authMechanism.mechanism.eoa.chainAddress.chain,
address: authMechanism.mechanism.eoa.chainAddress.address,
nonce: authMechanism.mechanism.eoa.nonce,
signature: authMechanism.mechanism.eoa.signature,
Expand All @@ -81,9 +82,19 @@ export default function useLoginOrRedirectToOnboarding() {
);
}

// TODO: does the gnosis safe flow lead to account creation, even without a signature?
// the only inputs for its AuthMechanism are `address` and `nonce`
if (authMechanism.mechanism.gnosisSafe) {
push(
{
pathname: '/welcome',
query: {
authMechanismType: 'gnosisSafe',
address: authMechanism.mechanism.gnosisSafe.address,
nonce: authMechanism.mechanism.gnosisSafe.nonce,
userFriendlyWalletName,
},
},
'/welcome'
);
}

return;
Expand Down
40 changes: 35 additions & 5 deletions src/hooks/api/users/useAuthPayloadQuery.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,60 @@
import { useRouter } from 'next/router';

export type AuthPayloadVariables = {
chain: 'Ethereum';
type EoaPayloadVariables = {
authMechanismType: 'eoa' | 'gnosisSafe';
chain: 'Ethereum' | 'Tezos';
address: string;
nonce: string;
signature: string;
userFriendlyWalletName: string;
};

type GnosisPayloadVariables = {
authMechanismType: 'eoa' | 'gnosisSafe';
address: string;
nonce: string;
userFriendlyWalletName: string;
};

export type AuthPayloadVariables = EoaPayloadVariables | GnosisPayloadVariables;

export function isEoaPayload(payload: AuthPayloadVariables): payload is EoaPayloadVariables {
return payload.authMechanismType === 'eoa';
}

export default function useAuthPayloadQuery(): AuthPayloadVariables | null {
const { query } = useRouter();

// need weird typechecking logic in this func due to the fact that nextjs queries can be
// a variety of types, and doesn't offer generics
if (
typeof query.authMechanismType !== 'string' ||
typeof query.address !== 'string' ||
typeof query.nonce !== 'string' ||
typeof query.signature !== 'string' ||
Array.isArray(query.userFriendlyWalletName)
) {
return null;
}

if (query.authMechanismType === 'eoa') {
if (typeof query.signature !== 'string') {
return null;
}

return {
authMechanismType: 'eoa',
chain: query.chain as EoaPayloadVariables['chain'],
address: query.address,
nonce: query.nonce,
signature: query.signature,
userFriendlyWalletName: query.userFriendlyWalletName || 'unknown',
};
}

return {
chain: 'Ethereum',
authMechanismType: 'gnosisSafe',
address: query.address,
nonce: query.nonce,
signature: query.signature,
userFriendlyWalletName: query.userFriendlyWalletName || 'unknown',
};
}
30 changes: 25 additions & 5 deletions src/hooks/api/users/useCreateUser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { usePromisifiedMutation } from 'hooks/usePromisifiedMutation';
import { useCallback } from 'react';
import { graphql } from 'relay-runtime';
import { AuthPayloadVariables } from './useAuthPayloadQuery';
import { AuthPayloadVariables, isEoaPayload } from './useAuthPayloadQuery';

export default function useCreateUser() {
const [createUser] = usePromisifiedMutation<any>(
Expand Down Expand Up @@ -38,13 +38,33 @@ export default function useCreateUser() {

return useCallback(
async (authPayloadVariables: AuthPayloadVariables, username: string, bio: string) => {
const { chain, address, nonce, signature } = authPayloadVariables;
let authMechanism;

if (isEoaPayload(authPayloadVariables)) {
const { chain, address, nonce, signature } = authPayloadVariables;
authMechanism = {
eoa: {
chainAddress: {
chain,
address,
},
nonce,
signature,
},
};
} else {
const { address, nonce } = authPayloadVariables;
authMechanism = {
gnosisSafe: {
address,
nonce,
},
};
}

const response = await createUser({
variables: {
authMechanism: {
eoa: { chainAddress: { address, chain }, nonce, signature },
},
authMechanism,
username,
bio,
},
Expand Down