Skip to content

Commit

Permalink
chore: persist domains
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed May 7, 2022
1 parent 45a4ae4 commit b47fe6b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 15 deletions.
14 changes: 10 additions & 4 deletions src/app/pages/choose-account-request/account-request.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect } from 'react';
import { logger } from '@shared/logger';

import { useRouteHeader } from '@app/common/hooks/use-route-header';
Expand All @@ -10,26 +11,30 @@ import {
sendRequestAccountResponseToTab,
sendUserDeniesAccountRequest,
} from '@app/common/actions/send-request-account-response';
import { useUserGrantsPermissionToAppDomain } from '@app/store/apps/apps.actions';

import { useAccountRequestSearchParams } from './use-account-request-search-params';
import { useEffect } from 'react';

export function AccountRequest() {
const accounts = useAccounts();
const { name: appName } = useAppDetails();
const grantDomainPermission = useUserGrantsPermissionToAppDomain();

const { tabId, id } = useAccountRequestSearchParams();
const { tabId, id, origin } = useAccountRequestSearchParams();

useRouteHeader(<Header hideActions />);

const returnAccountDetailsToApp = (index: number) => {
if (!accounts) throw new Error('Cannot request account details with no account');

if (!tabId || !id) {
logger.error('Missing either tabId or uuid. Both values are necessary to respond to app');
if (!tabId || !id || !origin) {
logger.error(
'Missing necessary search param values. All values are necessary to respond to app'
);
return;
}

grantDomainPermission(origin);
sendRequestAccountResponseToTab({ tabId, id, account: accounts[index] });
window.close();
};
Expand All @@ -45,6 +50,7 @@ export function AccountRequest() {
useEffect(() => {
window.addEventListener('beforeunload', handleUnmount);
return () => window.removeEventListener('beforeunload', handleUnmount);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function useAccountRequestSearchParams() {
() => ({
tabId: searchParams.get('tabId'),
id: searchParams.get('id'),
origin: searchParams.get('origin'),
}),
[searchParams]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ShowEditNonceAction, ShowEditNoncePlaceholder } from '@app/components/s
import { useTransactionError } from '@app/pages/transaction-request/hooks/use-transaction-error';
import { useFeeEstimationsState } from '@app/store/transactions/fees.hooks';
import { TransactionSigningSelectors } from '@tests/page-objects/transaction-signing.selectors';
import { TransactionErrorReason } from './transaction-error/transaction-error';

function BaseConfirmButton(props: ButtonProps): JSX.Element {
return (
Expand Down
17 changes: 17 additions & 0 deletions src/app/store/apps/apps.actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useCallback } from 'react';
import { useDispatch } from 'react-redux';

import { appsSlice } from './apps.slice';

export const appActions = appsSlice.actions;

export function useUserGrantsPermissionToAppDomain() {
const dispatch = useDispatch();
return useCallback(
(domain: string) => {
const host = new URL(domain).host;
dispatch(appActions.appConnected({ domain: host }));
},
[dispatch]
);
}
2 changes: 1 addition & 1 deletion src/app/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const persistConfig = {
version: 1,
storage,
serialize: true,
whitelist: ['keys', 'chains', 'onboarding', 'analytics'],
whitelist: ['apps', 'keys', 'chains', 'onboarding', 'analytics'],
};

const persistedReducer = persistReducer(persistConfig, rootReducer);
Expand Down
5 changes: 4 additions & 1 deletion src/app/store/wallet/wallet.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { finalizeAuthResponse } from '@app/common/actions/finalize-auth-response
import { logger } from '@shared/logger';
import { encryptedSecretKeyState, secretKeyState, walletState } from './wallet';
import { useKeyActions } from '@app/common/hooks/use-key-actions';
import { useUserGrantsPermissionToAppDomain } from '../apps/apps.actions';

export function useWalletState() {
return useAtom(walletState);
Expand Down Expand Up @@ -47,6 +48,7 @@ export function useSetLatestNonceCallback() {
export function useFinishSignInCallback() {
const { decodedAuthRequest, authRequest, appName, appIcon } = useOnboardingState();
const keyActions = useKeyActions();
const grantPermissionToDomain = useUserGrantsPermissionToAppDomain();
return useAtomCallback<void, number>(
useCallback(
async (get, _set, accountIndex) => {
Expand Down Expand Up @@ -84,9 +86,10 @@ export function useFinishSignInCallback() {
account,
});
keyActions.switchAccount(accountIndex);
grantPermissionToDomain(appURL.origin);
finalizeAuthResponse({ decodedAuthRequest, authRequest, authResponse });
},
[decodedAuthRequest, authRequest, appIcon, appName, keyActions]
[decodedAuthRequest, authRequest, appIcon, appName, keyActions, grantPermissionToDomain]
)
);
}
16 changes: 10 additions & 6 deletions src/inpage/inpage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,20 @@ const provider: StacksProvider = {
},

async request(method: RpcMethodNames, params?: any[]): Promise<RpcResponseArgs> {
return new Promise((resolve, _reject) => {
return new Promise((resolve, reject) => {
const id = crypto.randomUUID();
const event = new CustomEvent<RpcRequestArgs>(DomEventName.rpcRequest, {
detail: { jsonrpc: '2.0', id, method, params },
});
document.dispatchEvent(event);
console.log('request');
document.dispatchEvent(
new CustomEvent<RpcRequestArgs>(DomEventName.rpcRequest, {
detail: { jsonrpc: '2.0', id, method, params },
})
);
// @TODO: add RPC response type
const handleMessage = (event: MessageEvent<any>) => {
if (event.data.id !== id) return;
window.removeEventListener('message', handleMessage);
resolve(event.data);
if (event.data.error) reject(event.data.error);
resolve(event.data.result);
};
window.addEventListener('message', handleMessage);
});
Expand Down
6 changes: 3 additions & 3 deletions src/shared/message-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export type RpcResponseArgs = RpcSuccessResponseArgs | RpcErrorResponseArgs;

export enum RpcMethods {
stx_requestAccounts,
stx_testAnotherMethod,
stx_signTransactionRequest,
}

export type RpcMethodNames = keyof typeof RpcMethods;
Expand All @@ -73,9 +73,9 @@ interface RpcMessage<Method extends RpcMethodNames, Params = void> {
}

type RequestAccounts = RpcMessage<'stx_requestAccounts'>;
type TestAction = RpcMessage<'stx_testAnotherMethod'>;
type SignTransactionRequest = RpcMessage<'stx_signTransactionRequest'>;

export type SupportedRpcMessages = RequestAccounts | TestAction;
export type SupportedRpcMessages = RequestAccounts | SignTransactionRequest;

//
// Deprecated methods
Expand Down

0 comments on commit b47fe6b

Please sign in to comment.