Fix popup window regressions (#159)

This commit is contained in:
Armani Ferrante 2021-04-02 13:53:52 -07:00 committed by GitHub
parent 39c9ac7277
commit 699558770e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 14 deletions

View File

@ -244,6 +244,7 @@ function WalletSelector() {
hardwareWalletAccount, hardwareWalletAccount,
setHardwareWalletAccount, setHardwareWalletAccount,
setWalletSelector, setWalletSelector,
addAccount,
} = useWalletSelector(); } = useWalletSelector();
const [anchorEl, setAnchorEl] = useState(null); const [anchorEl, setAnchorEl] = useState(null);
const [addAccountOpen, setAddAccountOpen] = useState(false); const [addAccountOpen, setAddAccountOpen] = useState(false);
@ -287,6 +288,7 @@ function WalletSelector() {
open={addAccountOpen} open={addAccountOpen}
onClose={() => setAddAccountOpen(false)} onClose={() => setAddAccountOpen(false)}
onAdd={({ name, importedAccount }) => { onAdd={({ name, importedAccount }) => {
addAccount({ name, importedAccount });
setWalletSelector({ setWalletSelector({
walletIndex: importedAccount ? undefined : accounts.length, walletIndex: importedAccount ? undefined : accounts.length,
importedPubkey: importedAccount importedPubkey: importedAccount

View File

@ -49,7 +49,6 @@ export default function PopupPage({ opener }) {
const [connectedAccount, setConnectedAccount] = useState(null); const [connectedAccount, setConnectedAccount] = useState(null);
const hasConnectedAccount = !!connectedAccount; const hasConnectedAccount = !!connectedAccount;
const [requests, setRequests] = useState(getInitialRequests); const [requests, setRequests] = useState(getInitialRequests);
const [autoApprove, setAutoApprove] = useState(false); const [autoApprove, setAutoApprove] = useState(false);
const postMessage = useCallback( const postMessage = useCallback(
@ -66,6 +65,22 @@ export default function PopupPage({ opener }) {
[opener, origin], [opener, origin],
); );
// Hack to keep selectedWallet and wallet in sync. TODO: remove this block.
useEffect(() => {
if (!isExtension) {
if (!wallet) {
setWallet(selectedWallet);
} else if (!wallet.publicKey.equals(selectedWallet.publicKey)) {
setWallet(selectedWallet);
}
}
}, [
wallet,
wallet.publicKey,
selectedWallet,
selectedWallet.publicKey,
]);
// (Extension only) Fetch connected wallet for site from local storage. // (Extension only) Fetch connected wallet for site from local storage.
useEffect(() => { useEffect(() => {
if (isExtension) { if (isExtension) {
@ -254,9 +269,19 @@ export default function PopupPage({ opener }) {
} }
async function sendAllSignatures(messages) { async function sendAllSignatures(messages) {
const signatures = await Promise.all( console.log('wallet', wallet);
messages.map((m) => wallet.createSignature(m)), let signatures;
); // Ledger must sign one by one.
if (wallet.type === 'ledger') {
signatures = [];
for (let k = 0; k < messages.length; k += 1) {
signatures.push(await wallet.createSignature(messages[k]));
}
} else {
signatures = await Promise.all(
messages.map((m) => wallet.createSignature(m)),
);
}
postMessage({ postMessage({
result: { result: {
signatures, signatures,
@ -344,11 +369,11 @@ const useStyles = makeStyles((theme) => ({
function ApproveConnectionForm({ origin, onApprove }) { function ApproveConnectionForm({ origin, onApprove }) {
const wallet = useWallet(); const wallet = useWallet();
const { accounts } = useWalletSelector(); const { accounts, hardwareWalletAccount } = useWalletSelector();
// TODO better way to do this // TODO better way to do this
const account = accounts.find((account) => const account = accounts
account.address.equals(wallet.publicKey), .concat([hardwareWalletAccount])
); .find((account) => account && account.address.equals(wallet.publicKey));
const classes = useStyles(); const classes = useStyles();
const [autoApprove, setAutoApprove] = useState(false); const [autoApprove, setAutoApprove] = useState(false);
let [dismissed, setDismissed] = useLocalStorageState( let [dismissed, setDismissed] = useLocalStorageState(

View File

@ -172,7 +172,7 @@ export function WalletProvider({ children }) {
{}, {},
); );
// `walletSelector` identifies which wallet to use. // `walletSelector` identifies which wallet to use.
const [walletSelector, setWalletSelector] = useLocalStorageState( let [walletSelector, setWalletSelector] = useLocalStorageState(
'walletSelector', 'walletSelector',
DEFAULT_WALLET_SELECTOR, DEFAULT_WALLET_SELECTOR,
); );
@ -181,6 +181,11 @@ export function WalletProvider({ children }) {
// `walletCount` is the number of HD wallets. // `walletCount` is the number of HD wallets.
const [walletCount, setWalletCount] = useLocalStorageState('walletCount', 1); const [walletCount, setWalletCount] = useLocalStorageState('walletCount', 1);
if (walletSelector.ledger && !_hardwareWalletAccount) {
walletSelector = DEFAULT_WALLET_SELECTOR;
setWalletSelector(DEFAULT_WALLET_SELECTOR);
}
useEffect(() => { useEffect(() => {
(async () => { (async () => {
if (!seed) { if (!seed) {
@ -191,7 +196,7 @@ export function WalletProvider({ children }) {
try { try {
const onDisconnect = () => { const onDisconnect = () => {
setWalletSelector(DEFAULT_WALLET_SELECTOR); setWalletSelector(DEFAULT_WALLET_SELECTOR);
setHardwareWalletAccount(undefined); setHardwareWalletAccount(null);
}; };
const args = { const args = {
onDisconnect, onDisconnect,
@ -208,7 +213,7 @@ export function WalletProvider({ children }) {
} }
enqueueSnackbar(message, { variant: 'error' }); enqueueSnackbar(message, { variant: 'error' });
setWalletSelector(DEFAULT_WALLET_SELECTOR); setWalletSelector(DEFAULT_WALLET_SELECTOR);
setHardwareWalletAccount(undefined); setHardwareWalletAccount(null);
return; return;
} }
} }
@ -246,7 +251,6 @@ export function WalletProvider({ children }) {
enqueueSnackbar, enqueueSnackbar,
derivationPath, derivationPath,
]); ]);
function addAccount({ name, importedAccount, ledger }) { function addAccount({ name, importedAccount, ledger }) {
if (importedAccount === undefined) { if (importedAccount === undefined) {
name && localStorage.setItem(`name${walletCount}`, name); name && localStorage.setItem(`name${walletCount}`, name);

View File

@ -125,7 +125,6 @@ export async function solana_ledger_sign_bytes(
) { ) {
var num_paths = Buffer.alloc(1); var num_paths = Buffer.alloc(1);
num_paths.writeUInt8(1); num_paths.writeUInt8(1);
const payload = Buffer.concat([num_paths, derivation_path, msg_bytes]); const payload = Buffer.concat([num_paths, derivation_path, msg_bytes]);
return solana_send(transport, INS_SIGN_MESSAGE, P1_CONFIRM, payload); return solana_send(transport, INS_SIGN_MESSAGE, P1_CONFIRM, payload);

View File

@ -52,7 +52,7 @@ export class LedgerWalletProvider {
createSignature = async (message) => { createSignature = async (message) => {
const sig_bytes = await solana_ledger_sign_bytes( const sig_bytes = await solana_ledger_sign_bytes(
this.transport, this.transport,
this.solanaDerivationpath, this.solanaDerivationPath,
message, message,
); );
return bs58.encode(sig_bytes); return bs58.encode(sig_bytes);