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

feat: sync spaces custom domain with walletconnect allowed origins #487

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ BROVIDER_URL=https://rpc.snapshot.org # optional
STARKNET_RPC_URL= # optional
# Secret for laser
AUTH_SECRET=1dfd84a695705665668c260222ded178d1f1d62d251d7bee8148428dac6d0487 # optional
WALLET_CONNECT_PROJECT_ID= # optional
wa0x6e marked this conversation as resolved.
Show resolved Hide resolved
REOWN_SECRET= # optional
35 changes: 35 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,38 @@ export function captureError(e: any, context?: any, ignoredErrorCodes?: number[]
export async function clearStampCache(type: string, id: string) {
return fetch(`https://cdn.stamp.fyi/clear/${type}/${type === 'avatar' ? 'eth:' : ''}${id}`);
}

export async function removeFromWalletConnectWhitelist(domain: string) {
return updateWalletConnectWhitelist(domain, 'DELETE');
}

export async function addToWalletConnectWhitelist(domain: string) {
return updateWalletConnectWhitelist(domain, 'POST');
}

async function updateWalletConnectWhitelist(
domain: string,
method: 'POST' | 'DELETE'
): Promise<boolean> {
if (!domain || !process.env.REOWN_SECRET || !process.env.WALLET_CONNECT_PROJECT_ID) return false;

try {
await fetch(`https://cloud.reown.com/api/set-allowed-domains`, {
method,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: process.env.REOWN_SECRET
},
body: JSON.stringify({
projectId: process.env.WALLET_CONNECT_PROJECT_ID,
origins: [`https://${domain}`]
})
});
} catch (e) {
capture(e, { domain, method });
return false;
}

return true;
}
20 changes: 16 additions & 4 deletions src/writer/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import isEqual from 'lodash/isEqual';
import { addOrUpdateSpace, getSpace } from '../helpers/actions';
import log from '../helpers/log';
import db from '../helpers/mysql';
import { clearStampCache, DEFAULT_NETWORK, jsonParse } from '../helpers/utils';
import {
addToWalletConnectWhitelist,
clearStampCache,
DEFAULT_NETWORK,
jsonParse,
removeFromWalletConnectWhitelist
} from '../helpers/utils';

const SNAPSHOT_ENV = process.env.NETWORK || 'testnet';
const broviderUrl = process.env.BROVIDER_URL || 'https://rpc.snapshot.org';
Expand Down Expand Up @@ -120,9 +126,10 @@ export async function verify(body): Promise<any> {
export async function action(body): Promise<void> {
const msg = jsonParse(body.msg);
const space = msg.space.toLowerCase();
const existingSettings = JSON.parse(
(await db.queryAsync('SELECT settings FROM spaces WHERE id = ?', [space])?.[0]) || '{}'
);
const existingSpace = (
await db.queryAsync('SELECT domain, settings FROM spaces WHERE id = ? LIMIT 1', [space])
)?.[0];
const existingSettings = JSON.parse(existingSpace?.settings || '{}');

try {
await addOrUpdateSpace(space, msg.payload);
Expand All @@ -133,4 +140,9 @@ export async function action(body): Promise<void> {
}

if (existingSettings.avatar !== msg.payload.avatar) await clearStampCache('space', space);

if (existingSpace?.domain != msg.payload.domain) {
await addToWalletConnectWhitelist(msg.payload.domain);
Copy link
Member

Choose a reason for hiding this comment

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

I am still wondering if we want to add all domains (any space to add it) or just turbo spaces

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to decide whether we allow domain update for all spaces, or just turbo

Copy link
Member

Choose a reason for hiding this comment

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

If that's simpler we can allow only for turbo and the ones who already had a domain set to update domain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need to set boundaries on the definition of "already had a domain".

For now, if a non-turbo space has already set a domain, he can change it.

But what happen when a non-turbo space currently has a domain, but remove it (by error, etc ...) ? The way it work now, the space lose the ability to put it back. To enable this, we need a way to mark these spaces as custom domain enabled

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am still wondering if we want to add all domains (any space to add it) or just turbo spaces

From the various discussions, it seems that the solution would be to whitelist all custom domains, and limit custom domain edition to turbo space, and existing spaces with custom domain

Copy link
Member

Choose a reason for hiding this comment

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

All i worry about this PR is that someone may add many unwanted domains to walletconnect as any space can add a domain

Copy link
Member

Choose a reason for hiding this comment

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

Otherwise looks good

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All i worry about this PR is that someone may add many unwanted domains to walletconnect as any space can add a domain

Working on a PR to prevent that. domain edition will be limited to turbo, and spaces with existing domains only

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #488

This PR can be merged after the other one

await removeFromWalletConnectWhitelist(existingSpace?.domain);
}
}
Loading