From 7c7d4e99cf65eaa6e1eced916def693f0c0ae93a Mon Sep 17 00:00:00 2001 From: Rigidity Date: Sat, 21 Dec 2024 21:25:43 -0500 Subject: [PATCH] Add WalletConnect send and getAddress --- src/contexts/WalletConnectContext.tsx | 47 ++++++++++++++++++++++-- src/walletconnect/commands.ts | 19 ++++------ src/walletconnect/commands/high-level.ts | 9 +++++ src/walletconnect/handler.ts | 8 +++- 4 files changed, 66 insertions(+), 17 deletions(-) diff --git a/src/contexts/WalletConnectContext.tsx b/src/contexts/WalletConnectContext.tsx index 0526d6f7..e2ec9285 100644 --- a/src/contexts/WalletConnectContext.tsx +++ b/src/contexts/WalletConnectContext.tsx @@ -480,6 +480,44 @@ function CreateOfferDialog({ params }: CommandDialogProps<'chia_createOffer'>) { ); } +function SendDialog({ params }: CommandDialogProps<'chia_send'>) { + const walletState = useWalletState(); + + return ( +
+
+
Address
+
+ {params.address} +
+
+
+
Amount
+
+ {toDecimal( + params.amount, + params.assetId ? 3 : walletState.sync.unit.decimals, + )}{' '} + {params.assetId ? 'CAT' : walletState.sync.unit.ticker} +
+
+
+
Fee
+
+ {toDecimal(params.fee || 0, walletState.sync.unit.decimals)}{' '} + {walletState.sync.unit.ticker} +
+
+ {params.assetId && ( +
+
Asset Id
+
{params.assetId}
+
+ )} +
+ ); +} + function DefaultCommandDialog({ params }: { params: unknown }) { return (
@@ -498,6 +536,7 @@ const COMMAND_COMPONENTS: { chip0002_signMessage: SignMessageDialog, chia_takeOffer: TakeOfferDialog, chia_createOffer: CreateOfferDialog, + chia_send: SendDialog, }; const COMMAND_METADATA: { @@ -534,8 +573,8 @@ function RequestDialog({ const params = request.params.request.params; const commandInfo = walletConnectCommands[method]; const metadata = COMMAND_METADATA[method] ?? { - title: 'Confirm Action', - description: `Confirm ${method.replace(/_/g, ' ')}`, + title: 'WalletConnect Request', + description: `Would you like to authorize the "${method.split('_').slice(1).join(' ')}" request?`, }; const peerMetadata = signClient?.session.get(request.topic)?.peer.metadata; @@ -552,14 +591,14 @@ function RequestDialog({ {peerMetadata && (
- Request from {peerMetadata.name} + From {peerMetadata.name}
)} {metadata.title} {metadata.description}
-
+
{CommandComponent && ( )} diff --git a/src/walletconnect/commands.ts b/src/walletconnect/commands.ts index b9ddd7d8..7cc8ac27 100644 --- a/src/walletconnect/commands.ts +++ b/src/walletconnect/commands.ts @@ -147,18 +147,6 @@ export const walletConnectCommands = { }), confirm: false, }, - chia_transfer: { - paramsType: z.object({ - to: z.string(), - amount: safeAmount, - memos: z.array(z.string()).optional(), - assetId: z.string(), - }), - returnType: z.object({ - id: z.string(), - }), - confirm: true, - }, chia_takeOffer: { paramsType: z.object({ offer: z.string(), @@ -203,6 +191,13 @@ export const walletConnectCommands = { returnType: z.object({}), confirm: true, }, + chia_getAddress: { + paramsType: z.object({}), + returnType: z.object({ + address: z.string(), + }), + confirm: false, + }, } as const; // Define a union of valid commands diff --git a/src/walletconnect/commands/high-level.ts b/src/walletconnect/commands/high-level.ts index a543dcc6..f9526b60 100644 --- a/src/walletconnect/commands/high-level.ts +++ b/src/walletconnect/commands/high-level.ts @@ -1,4 +1,5 @@ import { commands } from '@/bindings'; +import { useWalletState } from '@/state'; import { Params, Return } from '../commands'; export async function handleGetNfts( @@ -65,3 +66,11 @@ export async function handleSend( return {}; } + +export async function handleGetAddress( + _params: Params<'chia_getAddress'>, +): Promise> { + return { + address: useWalletState.getState().sync.receive_address, + }; +} diff --git a/src/walletconnect/handler.ts b/src/walletconnect/handler.ts index 25c7528c..b1e19fe3 100644 --- a/src/walletconnect/handler.ts +++ b/src/walletconnect/handler.ts @@ -10,7 +10,11 @@ import { handleSignCoinSpends, handleSignMessage, } from './commands/chip0002'; -import { handleGetNfts, handleSend } from './commands/high-level'; +import { + handleGetAddress, + handleGetNfts, + handleSend, +} from './commands/high-level'; import { handleCreateOffer, handleTakeOffer } from './commands/offers'; export const handleCommand = async ( @@ -44,6 +48,8 @@ export const handleCommand = async ( return await handleGetNfts(parseCommand(command, params)); case 'chia_send': return await handleSend(parseCommand(command, params)); + case 'chia_getAddress': + return await handleGetAddress(parseCommand(command, params)); default: throw new Error(`Unknown command: ${command}`); }