Skip to content

fix: PCZT details prompt #89

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

Merged
merged 2 commits into from
May 12, 2025
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
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/ChainSafe/WebZjs.git"
},
"source": {
"shasum": "gjWQCyynhkKtUwZ5PUNQq9zfnJRqy0Iwwb7G4hSLVic=",
"shasum": "QsvZB2xwdiLaYU2MudldcoEqXbq0VA89TvudQNDBUTY=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
12 changes: 9 additions & 3 deletions packages/snap/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let wasm: InitOutput;
* @returns The ViewingKey
* @throws If the request method is not valid for this snap.
*/
export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
export const onRpcRequest: OnRpcRequestHandler = async ({ request, origin }) => {
if (!wasm) {
wasm = initialiseWasm();
}
Expand All @@ -35,8 +35,14 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
case 'getViewingKey':
return await getViewingKey();
case 'signPczt':
assert(request.params, object({ pcztHexTring: string() }));
return await signPczt(request.params as SignPcztParams);
assert(request.params, object({
pcztHexTring: string(),
signDetails: object({
recipient: string(),
amount: string()
}),
}));
return await signPczt(request.params as SignPcztParams, origin);
case 'getSeedFingerprint':
return await getSeedFingerprint();
case 'setBirthdayBlock':
Expand Down
14 changes: 10 additions & 4 deletions packages/snap/src/rpc/signPczt.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, Heading, Text } from '@metamask/snaps-sdk/jsx';
import { Box, Copyable, Divider, Heading, Text } from '@metamask/snaps-sdk/jsx';
import {
SeedFingerprint,
UnifiedSpendingKey,
Expand All @@ -10,16 +10,22 @@ import { SignPcztParams } from 'src/types';



export async function signPczt({ pcztHexTring }: SignPcztParams): Promise<string> {
export async function signPczt({ pcztHexTring, signDetails }: SignPcztParams, origin: string): Promise<string> {

const result = await snap.request({
method: 'snap_dialog',
params: {
type: 'confirmation',
content: (
<Box>
<Heading>Are you sure you want to sign this PCZT?</Heading>
<Text>Description</Text>
<Heading>Sing PCZT</Heading>
<Divider />
<Text>Origin: {origin}</Text>
<Text>Recipient: {signDetails.recipient}</Text>
<Text>Amount: {signDetails.amount}</Text>
<Divider />
<Text>PCZT hex to sign</Text>
<Copyable value={pcztHexTring}/>
</Box>
),
},
Expand Down
8 changes: 7 additions & 1 deletion packages/snap/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import type { Json } from '@metamask/snaps-sdk';

export type SetBirthdayBlockParams = { latestBlock: number };

export type SignPcztParams = { pcztHexTring: string };
export type SignPcztParams = {
pcztHexTring: string;
signDetails: {
recipient: string;
amount: string;
};
};

export interface SnapState extends Record<string, Json> {
webWalletSyncStartBlock: string;
Expand Down
19 changes: 15 additions & 4 deletions packages/web-wallet/src/hooks/usePCZT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useInvokeSnap } from './snaps/useInvokeSnap';
import { zecToZats } from '../utils';
import { useWebZjsActions } from './useWebzjsActions';
import { useState } from 'react';
import { SignPcztDetails } from '../types/snap';

interface IUsePczt {
handlePcztTransaction: (
Expand Down Expand Up @@ -56,15 +57,22 @@ export const usePczt = (): IUsePczt => {
}
};

const signPczt = async (pczt: Pczt): Promise<string> => {
const signPczt = async (
pczt: Pczt,
signDetails: { recipient: string; amount: string },
): Promise<string> => {
try {
const pcztBytes = pczt.serialize();

const pcztHexTring = Buffer.from(pcztBytes).toString('hex');
const params: SignPcztDetails = {
pcztHexTring,
signDetails,
};

return (await invokeSnap({
method: 'signPczt',
params: { pcztHexTring },
params,
})) as string;
} catch (error) {
console.error('Error signing PCZT:', error);
Expand Down Expand Up @@ -108,7 +116,10 @@ export const usePczt = (): IUsePczt => {

//Signing PCZT
setPcztTransferStatus(PcztTransferStatus.SIGNING_PCZT);
const pcztHexStringSigned = await signPczt(pczt);
const pcztHexStringSigned = await signPczt(pczt, {
recipient: toAddress,
amount: value,
});

const pcztBufferSigned = Buffer.from(pcztHexStringSigned, 'hex');

Expand All @@ -128,7 +139,7 @@ export const usePczt = (): IUsePczt => {
await triggerRescan();
} catch (error) {
console.error(error);
setPcztTransferStatus(PcztTransferStatus.SEND_ERROR)
setPcztTransferStatus(PcztTransferStatus.SEND_ERROR);
}
};

Expand Down
8 changes: 8 additions & 0 deletions packages/web-wallet/src/types/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ export type Snap = {
version: string;
initialPermissions: Record<string, unknown>;
};

export type SignPcztDetails = {
pcztHexTring: string;
signDetails: {
recipient: string;
amount: string;
};
};