Skip to content

Commit

Permalink
add Diffie-Hellman
Browse files Browse the repository at this point in the history
  • Loading branch information
dr497 committed Oct 15, 2021
1 parent 926af80 commit 165b6fe
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ function WalletSuggestionDialog({ open, onClose, onIgnore }) {
>
<div>
<img
alt=""
style={{ height: '39px' }}
src="https://raw.githubusercontent.com/solana-labs/wallet-adapter/master/packages/wallets/icons/phantom.svg"
/>
Expand Down Expand Up @@ -187,6 +188,7 @@ function WalletSuggestionDialog({ open, onClose, onIgnore }) {
>
<div>
<img
alt=""
style={{ height: '39px' }}
src="https://raw.githubusercontent.com/solana-labs/wallet-adapter/master/packages/wallets/icons/solflare.svg"
/>
Expand Down
16 changes: 11 additions & 5 deletions src/components/NavigationFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import AppBar from '@material-ui/core/AppBar';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import { useConnectionConfig } from '../utils/connection';
import {CLUSTERS, clusterForEndpoint, getClusters, addCustomCluster, customClusterExists} from '../utils/clusters';
import {
clusterForEndpoint,
getClusters,
addCustomCluster,
customClusterExists,
} from '../utils/clusters';
import Button from '@material-ui/core/Button';
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';
Expand Down Expand Up @@ -36,7 +41,7 @@ import { Badge } from '@material-ui/core';
import { useConnectedWallets } from '../utils/connected-wallets';
import { usePage } from '../utils/page';
import { MonetizationOn, OpenInNew } from '@material-ui/icons';
import AddCustomClusterDialog from "./AddCustomClusterDialog";
import AddCustomClusterDialog from './AddCustomClusterDialog';

const useStyles = makeStyles((theme) => ({
content: {
Expand Down Expand Up @@ -267,9 +272,10 @@ function NetworkSelector() {
setCustomNetworkOpen(true);
}}
>
<ListItemIcon className={classes.menuItemIcon}>
</ListItemIcon>
{customClusterExists() ? 'Edit Custom Endpoint' : 'Add Custom Endpoint'}
<ListItemIcon className={classes.menuItemIcon}></ListItemIcon>
{customClusterExists()
? 'Edit Custom Endpoint'
: 'Add Custom Endpoint'}
</MenuItem>
</Menu>
</>
Expand Down
5 changes: 5 additions & 0 deletions src/components/SignFormContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export default function SignFormContent({
case 'hex':
messageTxt = '0x' + toHex(message);
break;
case 'diffieHellman':
messageTxt = 'Create Diffie-Hellman keys';
break;
default:
throw new Error('Unexpected message type: ' + messageDisplay);
}
Expand All @@ -64,6 +67,8 @@ export default function SignFormContent({
{`Sign data with account ${wallet.publicKey}`}
</>
);
case 'diffieHellman':
return `Create Diffie-Hellman keys`;
default:
throw new Error('Unexpected message display type: ' + messageDisplay);
}
Expand Down
59 changes: 38 additions & 21 deletions src/pages/PopupPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ import WarningIcon from '@material-ui/icons/Warning';
import { useLocalStorageState, isExtension } from '../utils/utils';
import SignTransactionFormContent from '../components/SignTransactionFormContent';
import SignFormContent from '../components/SignFormContent';
import { generateDiffieHelllman } from '../utils/diffie-hellman';

const AUTHORIZED_METHODS = [
'signTransaction',
'signAllTransactions',
'sign',
'diffieHellman',
];

function getInitialRequests() {
if (!isExtension) {
Expand All @@ -36,7 +44,7 @@ function getInitialRequests() {

const urlParams = new URLSearchParams(window.location.hash.slice(1));
const request = JSON.parse(urlParams.get('request'));

if (request.method === 'sign') {
const dataObj = request.params.data;
// Deserialize `data` into a Uint8Array
Expand All @@ -60,7 +68,8 @@ export default function PopupPage({ opener }) {
return params.get('origin');
}, []);
const selectedWallet = useWallet();
const selectedWalletAddress = selectedWallet && selectedWallet.publicKey.toBase58();
const selectedWalletAddress =
selectedWallet && selectedWallet.publicKey.toBase58();
const { accounts, setWalletSelector } = useWalletSelector();
const [wallet, setWallet] = useState(isExtension ? null : selectedWallet);

Expand All @@ -87,11 +96,10 @@ export default function PopupPage({ opener }) {
if (!isExtension) {
setWallet(selectedWallet);
}
// using stronger condition here
// eslint-disable-next-line react-hooks/exhaustive-deps
// using stronger condition here
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedWalletAddress]);


// (Extension only) Fetch connected wallet for site from local storage.
useEffect(() => {
if (isExtension) {
Expand All @@ -106,16 +114,16 @@ export default function PopupPage({ opener }) {
}
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [origin]);

// (Extension only) Set wallet once connectedWallet is retrieved.
useEffect(() => {
if (isExtension && connectedAccount) {
setWallet(selectedWallet);
}
// using stronger condition here
// eslint-disable-next-line react-hooks/exhaustive-deps
// using stronger condition here
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [connectedAccount, selectedWalletAddress]);

// Send a disconnect event if this window is closed, this component is
Expand Down Expand Up @@ -149,11 +157,7 @@ export default function PopupPage({ opener }) {
useEffect(() => {
function messageHandler(e) {
if (e.origin === origin && e.source === window.opener) {
if (
e.data.method !== 'signTransaction' &&
e.data.method !== 'signAllTransactions' &&
e.data.method !== 'sign'
) {
if (!AUTHORIZED_METHODS.includes(e.data.method)) {
postMessage({ error: 'Unsupported method', id: e.data.id });
}

Expand All @@ -169,9 +173,14 @@ export default function PopupPage({ opener }) {

const { messages, messageDisplay } = useMemo(() => {
if (!request || request.method === 'connect') {
return { messages: [], messageDisplay: 'tx' }
return { messages: [], messageDisplay: 'tx' };
}
switch (request.method) {
case 'diffieHellman':
return {
messages: [request.params.publicKey],
messageDisplay: 'diffieHellman',
};
case 'signTransaction':
return {
messages: [bs58.decode(request.params.message)],
Expand All @@ -189,7 +198,7 @@ export default function PopupPage({ opener }) {
return {
messages: [request.params.data],
messageDisplay: request.params.display === 'utf8' ? 'utf8' : 'hex',
}
};
default:
throw new Error('Unexpected method: ' + request.method);
}
Expand Down Expand Up @@ -258,16 +267,13 @@ export default function PopupPage({ opener }) {
return <ApproveConnectionForm origin={origin} onApprove={connect} />;
}

assert(
(request.method === 'signTransaction' ||
request.method === 'signAllTransactions' ||
request.method === 'sign') &&
wallet,
);
assert(AUTHORIZED_METHODS.includes(request.method) && wallet);

async function onApprove() {
popRequest();
switch (request.method) {
case 'diffieHellman':
return diffieHellman(messages[0]);
case 'signTransaction':
case 'sign':
sendSignature(messages[0]);
Expand Down Expand Up @@ -312,6 +318,17 @@ export default function PopupPage({ opener }) {
});
}

function diffieHellman(publicKey) {
const keys = generateDiffieHelllman(
publicKey,
wallet.provider.account.secretKey,
);
postMessage({
result: keys,
id: request.id,
});
}

function sendReject() {
popRequest();
postMessage({
Expand Down
Loading

0 comments on commit 165b6fe

Please sign in to comment.