Skip to content

Commit

Permalink
feat(signPsbt): add txid to return payload, closes #5292
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Jan 22, 2025
1 parent e9c5e0d commit c19a902
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 69 deletions.
1 change: 1 addition & 0 deletions src/app/components/info-card/info-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export function InfoCardFooter({ children }: InfoCardFooterProps) {
alignItems="center"
bg={{ base: 'ink.background-primary', md: '' }}
bottom="0"
left="0"
justifyContent="center"
p="space.05"
position={{ base: 'fixed', md: 'unset' }}
Expand Down
48 changes: 37 additions & 11 deletions src/app/pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface BroadcastSignedPsbtTxArgs {
addressTaprootTotal: Money;
fee: Money;
tx: string;
psbt: string;
}
export function useRpcSignPsbt() {
const navigate = useNavigate();
Expand All @@ -52,18 +53,29 @@ export function useRpcSignPsbt() {
addressTaprootTotal,
fee,
tx,
psbt,
}: BroadcastSignedPsbtTxArgs) {
void analytics.track('user_approved_sign_and_broadcast_psbt', {
origin: origin || 'no_origin',
});

const transferTotalAsMoney = sumMoney([addressNativeSegwitTotal, addressTaprootTotal]);

await broadcastTx({
return await broadcastTx({
tx,
// skip utxos check for psbt txs
skipSpendableCheckUtxoIds: 'all',
async onSuccess(txid) {
if (!requestId) throw new Error('Invalid request id');

chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('signPsbt', {
id: requestId,
result: { hex: psbt, txid },
})
);

await filteredUtxosQuery.refetch();

const psbtTxSummaryState = {
Expand All @@ -83,6 +95,15 @@ export function useRpcSignPsbt() {
navigate(RouteUrls.RpcSignPsbtSummary, { state: psbtTxSummaryState });
},
onError(e) {
if (!requestId) throw new Error('Invalid request id');

chrome.tabs.sendMessage(
tabId,
makeRpcErrorResponse('signPsbt', {
id: requestId,
error: { code: 4002, message: 'Failed to broadcast transaction' },
})
);
navigate(RouteUrls.RequestError, {
state: { message: isError(e) ? e.message : '', title: 'Failed to broadcast' },
});
Expand All @@ -106,16 +127,27 @@ export function useRpcSignPsbt() {

const psbt = signedTx.toPSBT();

chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('signPsbt', { id: requestId, result: { hex: bytesToHex(psbt) } })
);
if (!broadcast) {
chrome.tabs.sendMessage(
tabId,
makeRpcSuccessResponse('signPsbt', { id: requestId, result: { hex: bytesToHex(psbt) } })
);
return;
}

// Optional args are handled here bc we support two request apis,
// but we only support broadcasting using the rpc request method
if (broadcast && addressNativeSegwitTotal && addressTaprootTotal && fee) {
try {
signedTx.finalize();

await broadcastSignedPsbtTx({
addressNativeSegwitTotal,
addressTaprootTotal,
fee,
tx: signedTx.hex,
psbt: bytesToHex(psbt),
});
} catch (e) {
return navigate(RouteUrls.RequestError, {
state: {
Expand All @@ -125,12 +157,6 @@ export function useRpcSignPsbt() {
});
}

await broadcastSignedPsbtTx({
addressNativeSegwitTotal,
addressTaprootTotal,
fee,
tx: signedTx.hex,
});
return;
}
closeWindow();
Expand Down
2 changes: 1 addition & 1 deletion tests/page-object-models/onboarding.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createCounter, delay } from '@leather.io/utils';

import { RouteUrls } from '@shared/route-urls';

const TEST_ACCOUNT_SECRET_KEY = process.env.TEST_ACCOUNT_SECRET_KEY ?? '';
export const TEST_ACCOUNT_SECRET_KEY = process.env.TEST_ACCOUNT_SECRET_KEY ?? '';

// If default wallet state changes, we'll need to update this
export const testSoftwareAccountDefaultWalletState = {
Expand Down
Loading

0 comments on commit c19a902

Please sign in to comment.