Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WalletConnect send and getAddress #174

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions src/contexts/WalletConnectContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,44 @@ function CreateOfferDialog({ params }: CommandDialogProps<'chia_createOffer'>) {
);
}

function SendDialog({ params }: CommandDialogProps<'chia_send'>) {
const walletState = useWalletState();

return (
<div className='space-y-2'>
<div>
<div className='font-medium'>Address</div>
<div className='text-sm truncate text-muted-foreground'>
{params.address}
</div>
</div>
<div>
<div className='font-medium'>Amount</div>
<div className='text-sm text-muted-foreground'>
{toDecimal(
params.amount,
params.assetId ? 3 : walletState.sync.unit.decimals,
)}{' '}
{params.assetId ? 'CAT' : walletState.sync.unit.ticker}
</div>
</div>
<div>
<div className='font-medium'>Fee</div>
<div className='text-sm text-muted-foreground'>
{toDecimal(params.fee || 0, walletState.sync.unit.decimals)}{' '}
{walletState.sync.unit.ticker}
</div>
</div>
{params.assetId && (
<div>
<div className='font-medium'>Asset Id</div>
<div className='text-sm text-muted-foreground'>{params.assetId}</div>
</div>
)}
</div>
);
}

function DefaultCommandDialog({ params }: { params: unknown }) {
return (
<div className='p-4'>
Expand All @@ -498,6 +536,7 @@ const COMMAND_COMPONENTS: {
chip0002_signMessage: SignMessageDialog,
chia_takeOffer: TakeOfferDialog,
chia_createOffer: CreateOfferDialog,
chia_send: SendDialog,
};

const COMMAND_METADATA: {
Expand Down Expand Up @@ -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;

Expand All @@ -552,14 +591,14 @@ function RequestDialog({
<DialogHeader>
{peerMetadata && (
<div className='text-sm text-muted-foreground mb-4'>
Request from {peerMetadata.name}
From {peerMetadata.name}
</div>
)}
<DialogTitle>{metadata.title}</DialogTitle>
<DialogDescription>{metadata.description}</DialogDescription>
</DialogHeader>

<div className='max-h-[60vh] overflow-y-auto'>
<div className='max-h-[60vh] overflow-y-auto mb-2'>
{CommandComponent && (
<CommandComponent params={parsedParams as any} />
)}
Expand Down
19 changes: 7 additions & 12 deletions src/walletconnect/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/walletconnect/commands/high-level.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { commands } from '@/bindings';
import { useWalletState } from '@/state';
import { Params, Return } from '../commands';

export async function handleGetNfts(
Expand Down Expand Up @@ -65,3 +66,11 @@ export async function handleSend(

return {};
}

export async function handleGetAddress(
_params: Params<'chia_getAddress'>,
): Promise<Return<'chia_getAddress'>> {
return {
address: useWalletState.getState().sync.receive_address,
};
}
8 changes: 7 additions & 1 deletion src/walletconnect/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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}`);
}
Expand Down
Loading