Skip to content

Commit

Permalink
refactor: don't use old sdk in urlRequestHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Feb 14, 2025
1 parent 9c766e5 commit bc7cc9d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 24 deletions.
10 changes: 4 additions & 6 deletions src/store/plugins/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class AccountStore extends AccountBase {
this.#store = store;
}

sign(data, { signal }) {
sign(data, { signal } = {}) {
return this.#store.dispatch('accounts/sign', { data, signal });
}

signTransaction(transaction, { signal }) {
signTransaction(transaction, { signal } = {}) {
return this.#store.dispatch('accounts/signTransaction', { transaction, signal });
}
}
Expand All @@ -54,10 +54,8 @@ export default (store) => {
getters: {
node: (_, { currentNetwork }) => new Node(currentNetwork.url),
middleware: (_, { currentNetwork }) => new Middleware(currentNetwork.middlewareUrl),
sdk: (_, getters) => new AeSdkMethods({
onNode: getters.node,
onAccount: new AccountStore(getters['accounts/active']?.address, store),
}),
account: (_, getters) => new AccountStore(getters['accounts/active']?.address, store),
sdk: (_, { node, account }) => new AeSdkMethods({ onNode: node, onAccount: account, }),
},
mutations: {
setNetworkId(state, networkId) {
Expand Down
84 changes: 66 additions & 18 deletions src/store/plugins/ui/urlRequestHandler.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
import { times } from 'lodash-es';
import { ensureLoggedIn, mergeEnterHandlers } from '../../../router/utils';

const urlRequestMethods = ['address', 'sign', 'signTransaction'];

export default (store) => {
const handleUrlRequest = async (url) => {
const method = url.path.replace('/', '');
async function ensureActiveAccountAccess(appHost) {
// TODO: extract duplicate code
const accessToAccounts = store.getters.getApp(appHost)?.permissions.accessToAccounts ?? [];
const getActiveAddress = () => store.getters['accounts/active'].address;
if (accessToAccounts.includes(getActiveAddress())) return;

const controller = new AbortController();
const unsubscribe = store.watch(
() => getActiveAddress(),
(address) => accessToAccounts.includes(address) && controller.abort(),
);
try {
await store.dispatch(
'modals/open',
{ name: 'confirmAccountAccess', signal: controller.signal, appHost },
);
store.commit('toggleAccessToAccount', { appHost, accountAddress: getActiveAddress() });
} catch (error) {
if (error.message === 'Modal aborted') return;
throw error;
} finally {
unsubscribe();
}
}

async function ensureRoutedToTransfer() {
await new Promise((resolve) => {
const unsubscribe = store.watch(
(state) => state.route.name,
(name) => {
if (name !== 'transfer') return;
resolve();
unsubscribe();
},
{ immediate: true },
)
});
}

const urlRequestHandlers = {
async address(host) {
await ensureActiveAccountAccess(host);
return store.getters['accounts/active'].address;
},
async sign(_host, data) {
return store.getters.account.sign(data);
},
signTransaction(_host, tx) {
return store.getters.account.signTransaction(tx);
},
};

const handleUrlRequest = async (url, method) => {
const callbackUrl = new URL(url.query.callback);
const lastParamIdx = Math.max(
-1,
Expand Down Expand Up @@ -37,31 +86,30 @@ export default (store) => {
reply({ error: new Error(`Unknown protocol: ${callbackUrl.protocol}`) });
return;
}
if (!urlRequestMethods.includes(method)) {
reply({ error: new Error(`Unknown method: ${method}`) });
return;
}
try {
await store.state.sdk;
reply({
result: await store.state.sdk[method](
...params,
store.state.sdk.getApp(callbackUrl.host),
),
});
await ensureRoutedToTransfer();
reply({ result: await urlRequestHandlers[method](callbackUrl.host, ...params) });
} catch (error) {
reply({ error });
}
};

urlRequestMethods.forEach((methodName) => store.dispatch('router/addRoute', {
// Each 'router/addRoute' call reevaluates beforeEnter, but we need to call `handleUrlRequest` once
let handling = false;
Object.keys(urlRequestHandlers).forEach((methodName) => store.dispatch('router/addRoute', {
name: methodName,
path: `/${methodName}`,
beforeEnter: mergeEnterHandlers(
ensureLoggedIn,
(to, from, next) => {
handleUrlRequest(to);
async (to, _from, next) => {
next(false);
if (handling) return;
handling = true;
try {
await handleUrlRequest(to, methodName);
} finally {
handling = false;
}
},
),
}));
Expand Down

0 comments on commit bc7cc9d

Please sign in to comment.