import { ParaClient } from '../para-client.js';
import type { Wallet } from '../types.js';
export const definition = {
name: 'wait_for_wallet_ready',
description:
'Poll a wallet until its status becomes "ready" (MPC key generation complete). Polls every 2 seconds up to a configurable max wait time (default 30s).',
inputSchema: {
type: 'object' as const,
properties: {
walletId: {
type: 'string',
description: 'The wallet ID to poll',
},
maxWaitMs: {
type: 'number',
description: 'Maximum time to wait in milliseconds (default 30000)',
},
},
required: ['walletId'],
},
};
export async function handler(client: ParaClient, args: Record<string, unknown>) {
const walletId = args.walletId as string;
const maxWaitMs = (args.maxWaitMs as number) ?? 30_000;
const pollIntervalMs = 2_000;
const start = Date.now();
while (Date.now() - start < maxWaitMs) {
const wallet = await client.requestWithRetry<Wallet>(`/v1/wallets/${walletId}`);
if (wallet.status === 'ready') {
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(
{ ...wallet, _note: 'Wallet is ready. You can now use sign_raw to sign data.' },
null,
2,
),
},
],
};
}
if (wallet.status === 'error') {
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(
{ ...wallet, _note: 'Wallet creation failed. Try creating a new wallet.' },
null,
2,
),
},
],
isError: true,
};
}
await new Promise((r) => setTimeout(r, pollIntervalMs));
}
return {
content: [
{
type: 'text' as const,
text: `Wallet ${walletId} did not become ready within ${maxWaitMs}ms. Current status is still "creating". You can call wait_for_wallet_ready again to keep polling.`,
},
],
isError: true,
};
}