From 19773e9de48f8d7c9ceb4f6b9acb80750f7e2161 Mon Sep 17 00:00:00 2001 From: Dawid Sowa Date: Fri, 12 Apr 2024 22:36:04 +0200 Subject: [PATCH] feat: adjust account list message format --- src/chrome/background/message-handler.ts | 2 +- .../dev-tools/components/WalletSimulator.tsx | 6 +-- src/chrome/dev-tools/example.ts | 20 ++++--- src/chrome/messages/_types.ts | 5 +- src/chrome/messages/create-message.ts | 13 +---- .../wallet-connection/message-handler.ts | 30 ++--------- .../wallet-connection-client.ts | 6 +-- .../linked-wallet/linked-wallet.tsx | 10 +--- .../linked-wallet/shared-accounts.tsx | 19 +------ src/ledger/schemas.ts | 31 ++--------- src/pairing/components/connection-status.tsx | 52 +------------------ src/pairing/pairing.tsx | 7 +-- 12 files changed, 31 insertions(+), 170 deletions(-) diff --git a/src/chrome/background/message-handler.ts b/src/chrome/background/message-handler.ts index 467f12a..9a459cc 100644 --- a/src/chrome/background/message-handler.ts +++ b/src/chrome/background/message-handler.ts @@ -215,7 +215,7 @@ export const BackgroundMessageHandler = } case messageDiscriminator.walletToExtension: - if (message.data?.discriminator === 'accountListResponse') { + if (message.data?.discriminator === 'accountList') { return getConnections() .map((connections) => ConnectionsClient(connections).updateAccounts( diff --git a/src/chrome/dev-tools/components/WalletSimulator.tsx b/src/chrome/dev-tools/components/WalletSimulator.tsx index 44a2a45..56b4730 100644 --- a/src/chrome/dev-tools/components/WalletSimulator.tsx +++ b/src/chrome/dev-tools/components/WalletSimulator.tsx @@ -11,8 +11,7 @@ import { getSignEd25519TransactionPayload, getSignSecp256k1TransactionPayload, getDeriveAndDisplayPayload, - getRejectAccountListRequestPayload, - getAccountListResponsePayload, + getAccountListMessage, } from '../example' import { getExtensionOptions } from 'options' import { getConnections } from 'chrome/helpers/get-connections' @@ -33,8 +32,7 @@ export const WalletSimulator = () => { 'Sign TX (Curve25519)': getSignEd25519TransactionPayload(), 'Sign Auth (Curve25519)': getSignEd222519ChallengePayload(), 'Sign Auth (Secp256k1)': getSignSecp256k1ChallengePayload(), - 'Reject Account List Request': getRejectAccountListRequestPayload(), - 'Account List Response': getAccountListResponsePayload(), + 'Account List Message': getAccountListMessage(), } useEffect(() => { diff --git a/src/chrome/dev-tools/example.ts b/src/chrome/dev-tools/example.ts index 11191aa..0cd00c5 100644 --- a/src/chrome/dev-tools/example.ts +++ b/src/chrome/dev-tools/example.ts @@ -129,24 +129,22 @@ export const getSignSecp256k1ChallengePayload = () => }, ]) -export const getRejectAccountListRequestPayload = () => { +export const getAccountListMessage = () => { return { - interactionId: '', - discriminator: 'accountListRejectedResponse', - } -} - -export const getAccountListResponsePayload = () => { - return { - interactionId: '', - discriminator: 'accountListResponse', + discriminator: 'accountList', accounts: [ { - label: 'Account 1', + label: 'My Main Account', address: 'account_rdx12y4l35lh2543nff9pyyzvsh64ssu0dv6fq20gg8suslwmjvkylejgj', appearanceId: 0, }, + { + label: 'My Savings Account', + address: + 'account_tdx_2_12y9as9p2utmxf9zc9gvsalvd6s69zump236ng3ztvfkjngm757zsa3', + appearanceId: 1, + }, ], } } diff --git a/src/chrome/messages/_types.ts b/src/chrome/messages/_types.ts index 5b83712..3055c48 100644 --- a/src/chrome/messages/_types.ts +++ b/src/chrome/messages/_types.ts @@ -1,6 +1,6 @@ import { MessageLifeCycleEvent } from 'chrome/dapp/_types' import { - AccountListResponseInteraction, + AccountListMessage, LedgerRequest, LedgerResponse, } from 'ledger/schemas' @@ -14,7 +14,6 @@ export const messageDiscriminator = { getConnections: 'getConnections', setConnections: 'setConnections', setRadixConnectConfiguration: 'setRadixConnectConfiguration', - accountListRequestInteraction: 'accountListRequestInteraction', getExtensionOptions: 'getExtensionOptions', getSessionRouterData: 'getSessionRouterData', setSessionRouterData: 'setSessionRouterData', @@ -179,7 +178,7 @@ export type Messages = { > [messageDiscriminator.walletToExtension]: MessageBuilder< MessageDiscriminator['walletToExtension'], - { data: AccountListResponseInteraction; walletPublicKey: string } + { data: AccountListMessage; walletPublicKey: string } > [messageDiscriminator.walletToLedger]: MessageBuilder< MessageDiscriminator['walletToLedger'], diff --git a/src/chrome/messages/create-message.ts b/src/chrome/messages/create-message.ts index d6973ff..06e6f93 100644 --- a/src/chrome/messages/create-message.ts +++ b/src/chrome/messages/create-message.ts @@ -1,6 +1,6 @@ import { ConnectorExtensionOptions } from './../../options/index' import { - AccountListResponseInteraction, + AccountListMessage, LedgerRequest, LedgerResponse, } from 'ledger/schemas' @@ -26,15 +26,6 @@ export const createMessage = { discriminator: messageDiscriminator.getSessionRouterData, source: 'offscreen', }), - sendAccountListRequest: (walletPublicKey: string, interactionId: string) => ({ - discriminator: messageDiscriminator.accountListRequestInteraction, - source: 'popup', - messageId: crypto.randomUUID(), - data: { - walletPublicKey, - interactionId, - }, - }), setSessionRouterData: (data: Record) => ({ discriminator: messageDiscriminator.setSessionRouterData, data, @@ -138,7 +129,7 @@ export const createMessage = { }), walletToExtension: ( source: MessageSource, - message: AccountListResponseInteraction, + message: AccountListMessage, walletPublicKey: string, ): Messages['walletToExtension'] => ({ source, diff --git a/src/chrome/offscreen/wallet-connection/message-handler.ts b/src/chrome/offscreen/wallet-connection/message-handler.ts index d99f960..f25711f 100644 --- a/src/chrome/offscreen/wallet-connection/message-handler.ts +++ b/src/chrome/offscreen/wallet-connection/message-handler.ts @@ -3,12 +3,7 @@ import { MessagesRouter } from 'chrome/offscreen/wallet-connection/messages-rout import { ResultAsync, errAsync, okAsync } from 'neverthrow' import { Queue } from 'queues/queue' import { AppLogger, logger as appLogger } from 'utils/logger' - -import { - AccountListRequestInteraction, - LedgerResponse, - isLedgerRequest, -} from 'ledger/schemas' +import { LedgerResponse, isLedgerRequest } from 'ledger/schemas' import { sendMessage } from 'chrome/helpers/send-message' import { WalletInteractionWithOrigin } from '@radixdlt/radix-connect-schemas' import { @@ -26,7 +21,7 @@ export type WalletConnectionMessageHandler = ReturnType< > export const WalletConnectionMessageHandler = (input: { dAppRequestQueue: Queue - extensionToWalletQueue: Queue + extensionToWalletQueue: Queue incomingWalletMessageQueue: Queue messagesRouter: MessagesRouter sessionRouter: SessionRouter @@ -54,12 +49,8 @@ export const WalletConnectionMessageHandler = (input: { return sendMessageWithConfirmation( createMessage.walletToLedger('offScreen', message.data), ).map(() => ({ sendConfirmation: false })) - } else if ( - ['accountListResponse', 'accountListRejectedResponse'].includes( - message.data.discriminator, - ) - ) { - logger.debug('🪪 -> 📒: wallet to extension', message.data) + } else if (['accountList'].includes(message.data.discriminator)) { + logger.debug('wallet to extension', message.data) return sendMessageWithConfirmation( createMessage.walletToExtension( 'offScreen', @@ -159,19 +150,6 @@ export const WalletConnectionMessageHandler = (input: { .add(message.data, message.data.interactionId) .map(() => ({ sendConfirmation: false })) - case messageDiscriminator.accountListRequestInteraction: - if (message.data.walletPublicKey === walletPublicKey) { - return extensionToWalletQueue - .add( - { - discriminator: 'accountListRequest', - interactionId: message.data.interactionId, - }, - message.data.interactionId, - ) - .map(() => ({ sendConfirmation: true })) - } - case messageDiscriminator.closeDappTab: { const { tabId } = message return messagesRouter diff --git a/src/chrome/offscreen/wallet-connection/wallet-connection-client.ts b/src/chrome/offscreen/wallet-connection/wallet-connection-client.ts index 7b997a1..6c3c56d 100644 --- a/src/chrome/offscreen/wallet-connection/wallet-connection-client.ts +++ b/src/chrome/offscreen/wallet-connection/wallet-connection-client.ts @@ -5,7 +5,7 @@ import { } from '@radixdlt/radix-connect-webrtc' import { createMessage } from 'chrome/messages/create-message' import { config } from 'config' -import { AccountListRequestInteraction, LedgerResponse } from 'ledger/schemas' +import { LedgerResponse } from 'ledger/schemas' import { MessagesRouter } from 'chrome/offscreen/wallet-connection/messages-router' import { Queue } from 'queues/queue' import { AppLogger, logger as utilsLogger } from 'utils/logger' @@ -76,9 +76,7 @@ export const WalletConnectionClient = ({ ), }) - const extensionToWalletQueue = Queue< - LedgerResponse | AccountListRequestInteraction - >({ + const extensionToWalletQueue = Queue({ key: 'extensionToWalletQueue', logger, worker: Worker((job) => diff --git a/src/components/linked-wallet/linked-wallet.tsx b/src/components/linked-wallet/linked-wallet.tsx index 1f1b16a..550e4c2 100644 --- a/src/components/linked-wallet/linked-wallet.tsx +++ b/src/components/linked-wallet/linked-wallet.tsx @@ -11,16 +11,12 @@ export const LinkedWallet = ({ name, accounts, onRenameWalletLink, - pendingAccountRequest, onForgetWallet, - onRequestAccountList, }: { name: string accounts: Account[] - pendingAccountRequest?: boolean | undefined onRenameWalletLink: () => void onForgetWallet: () => void - onRequestAccountList: () => void }) => { const [anchorEl, setAnchorEl] = React.useState(null) const open = Boolean(anchorEl) @@ -118,11 +114,7 @@ export const LinkedWallet = ({ - + ) } diff --git a/src/components/linked-wallet/shared-accounts.tsx b/src/components/linked-wallet/shared-accounts.tsx index b0d3180..a2761d0 100644 --- a/src/components/linked-wallet/shared-accounts.tsx +++ b/src/components/linked-wallet/shared-accounts.tsx @@ -1,16 +1,10 @@ -import { Button } from 'components/button' import ChevronDown from './chevron-down.svg' import { Account as AccountType } from '@radixdlt/radix-connect-schemas' import { Account } from 'components/account/account' import { Box, Collapse } from '@mui/material' import { useState } from 'react' -import { Spinner } from 'components/spinner/spinner' -export const SharedAccounts = (props: { - accounts?: AccountType[] - pendingAccountRequest?: boolean | undefined - onRequestAccountList: () => void -}) => { +export const SharedAccounts = (props: { accounts?: AccountType[] }) => { const [isCollapsed, setIsCollapsed] = useState(true) return ( @@ -34,16 +28,7 @@ export const SharedAccounts = (props: { > - ) : ( - - )} + ) : null} ) diff --git a/src/ledger/schemas.ts b/src/ledger/schemas.ts index 84e77e9..2dd4d0f 100644 --- a/src/ledger/schemas.ts +++ b/src/ledger/schemas.ts @@ -144,38 +144,13 @@ export const SignatureOfSignerSchema = object({ signature: string(), }) -export type AccountListRequestInteraction = z.infer< - typeof AccountListRequestInteraction -> - -export const AccountListRequestInteraction = object({ - interactionId: string(), - discriminator: literal('accountListRequest'), -}) - -export type AccountListResponse = z.infer -export const AccountListResponse = object({ +export type AccountListMessage = z.infer +export const AccountListMessage = object({ interactionId: string(), - discriminator: literal('accountListResponse'), + discriminator: literal('accountList'), accounts: Account.array(), }) -export type AccountListRejectedResponse = z.infer< - typeof AccountListRejectedResponse -> -export const AccountListRejectedResponse = object({ - interactionId: string(), - discriminator: literal('accountListRejectedResponse'), -}) - -export type AccountListResponseInteraction = z.infer< - typeof AccountListResponseInteraction -> -export const AccountListResponseInteraction = z.union([ - AccountListResponse, - AccountListRejectedResponse, -]) - export type LinkClientInteraction = z.infer export const LinkClientInteraction = object({ discriminator: literal('linkClient'), diff --git a/src/pairing/components/connection-status.tsx b/src/pairing/components/connection-status.tsx index 410865d..bb27689 100644 --- a/src/pairing/components/connection-status.tsx +++ b/src/pairing/components/connection-status.tsx @@ -2,33 +2,20 @@ import { Box, Button } from '../../components' import { PairingHeader } from './pairing-header' import { useEffect, useState } from 'react' import { LinkedWallet } from 'components/linked-wallet/linked-wallet' -import { Link, useNavigate, useSearchParams } from 'react-router-dom' +import { Link, useNavigate } from 'react-router-dom' import { useConnectionsClient } from 'pairing/state/connections' import { ForgetThisWallet } from './forget-this-wallet' import { RenameWalletLink } from './rename-wallet-link' -import { sendMessage } from 'chrome/messages/send-message' -import { createMessage } from 'chrome/messages/create-message' -import { Message } from 'chrome/messages/_types' export const ConnectionStatus = () => { const navigate = useNavigate() - const [searchParams, setSearchParams] = useSearchParams() const connectionsClient = useConnectionsClient() const connections = connectionsClient.connections - const [pendingAccountRequest, setPendingAccountRequest] = useState< - Record - >({}) + const [connectionIdToForget, setConnectionIdToForget] = useState('') const [connectionIdToChangeName, setConnectionIdToChangeName] = useState('') - useEffect(() => { - if (searchParams.has('newWallet')) { - sendAccountListRequest(searchParams.get('newWallet') as string) - setSearchParams({}) - } - }, [searchParams]) - useEffect(() => { if (connectionsClient.isLoading()) return if (connectionsClient.hasNoConnections()) { @@ -36,26 +23,6 @@ export const ConnectionStatus = () => { } }, [connections]) - useEffect(() => { - const handler = (message: Message) => { - console.log(message) - if (message.discriminator === 'walletToExtension') { - const interactionId = message.data.interactionId - if (pendingAccountRequest[message.walletPublicKey] === interactionId) { - setPendingAccountRequest({ - ...pendingAccountRequest, - [message.walletPublicKey]: '', - }) - } - } - } - chrome.runtime.onMessage.addListener(handler) - - return () => { - chrome.runtime.onMessage.removeListener(handler) - } - }, [pendingAccountRequest]) - const forgetWallet = () => { connectionsClient.remove(connectionIdToForget) setConnectionIdToForget('') @@ -89,19 +56,6 @@ export const ConnectionStatus = () => { ) } - const sendAccountListRequest = (connectionId: string) => { - const interactionId = crypto.randomUUID() - if (pendingAccountRequest[connectionId]) return - setPendingAccountRequest({ - ...pendingAccountRequest, - [connectionId]: interactionId, - }) - - sendMessage( - createMessage.sendAccountListRequest(connectionId, interactionId), - ) - } - return ( <> { key={id} accounts={connection.accounts} name={connection.walletName} - pendingAccountRequest={!!pendingAccountRequest[id]} onForgetWallet={() => setConnectionIdToForget(id)} - onRequestAccountList={() => sendAccountListRequest(id)} onRenameWalletLink={() => setConnectionIdToChangeName(id)} /> ))} diff --git a/src/pairing/pairing.tsx b/src/pairing/pairing.tsx index 57115cf..1c83e4a 100644 --- a/src/pairing/pairing.tsx +++ b/src/pairing/pairing.tsx @@ -77,12 +77,7 @@ export const Pairing = () => { connectionsClient .addOrUpdate(password, interaction) .map(() => connectorClient.disconnect()) - .map(() => - navigate({ - pathname: '/', - search: `?newWallet=${interaction.publicKey}`, - }), - ) + .map(() => navigate('/')) }), )