1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 14:15:06 +01:00
metamask-extension/ui/hooks/flask/useTransactionInsightSnap.js
Frederik Bolding 025ee2cb48
[FLASK] Expose transaction origin to transaction insight snaps (#16671)
* Expose transaction origin to transaction insight snaps

* Support multiple icons for each label for a permission

* Add transaction insight origin permission

* Fix fencing

* Fix import and permission crash

* Use function properly for connected accounts
2022-12-01 14:38:56 +01:00

60 lines
1.6 KiB
JavaScript

import { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { getTransactionOriginCaveat } from '@metamask/snaps-controllers';
import { handleSnapRequest } from '../../store/actions';
import { getPermissionSubjects } from '../../selectors';
const INSIGHT_PERMISSION = 'endowment:transaction-insight';
export function useTransactionInsightSnap({
transaction,
chainId,
origin,
snapId,
}) {
const subjects = useSelector(getPermissionSubjects);
const permission = subjects[snapId]?.permissions[INSIGHT_PERMISSION];
if (!permission) {
throw new Error(
'This snap does not have the transaction insight endowment.',
);
}
const hasTransactionOriginCaveat = getTransactionOriginCaveat(permission);
const transactionOrigin = hasTransactionOriginCaveat ? origin : null;
const [loading, setLoading] = useState(true);
const [data, setData] = useState(undefined);
const [error, setError] = useState(undefined);
useEffect(() => {
async function fetchInsight() {
try {
setError(undefined);
setLoading(true);
const d = await handleSnapRequest({
snapId,
origin: '',
handler: 'onTransaction',
request: {
jsonrpc: '2.0',
method: ' ',
params: { transaction, chainId, transactionOrigin },
},
});
setData(d);
} catch (err) {
setError(err);
} finally {
setLoading(false);
}
}
if (transaction) {
fetchInsight();
}
}, [snapId, transaction, chainId, transactionOrigin]);
return { data, error, loading };
}