From 5d17c040f8b43a580f434a96b9f2b12ec2aeeccb Mon Sep 17 00:00:00 2001 From: Govard Barkhatov Date: Thu, 19 Dec 2024 18:48:35 +0200 Subject: [PATCH] Update src/components/WalletProvider/index.stories.tsx Co-authored-by: David Totrashvili <8580261+totraev@users.noreply.github.com> --- .../WalletProvider/index.stories.tsx | 113 ++++++++---------- 1 file changed, 47 insertions(+), 66 deletions(-) diff --git a/src/components/WalletProvider/index.stories.tsx b/src/components/WalletProvider/index.stories.tsx index 55ef18e..d0708c1 100644 --- a/src/components/WalletProvider/index.stories.tsx +++ b/src/components/WalletProvider/index.stories.tsx @@ -90,55 +90,33 @@ export const WithBTCSigningFeatures: Story = { ), ], render: () => { - const { open } = useWidgetState(); + const { open, selectedWallets } = useWidgetState(); const [messageToSign, setMessageToSign] = useState(""); const [psbtToSign, setPsbtToSign] = useState(""); - const connectors = useChainProviders(); - const [walletData, setWalletData] = useState<{ - btcWallet?: IBTCProvider; - signedMessage?: string; - signedPsbt?: string; - }>({}); + const [signedMessage, setSignedMessage] = useState(""); + const [signedPsbt, setSignedPsbt] = useState(""); const [transaction, setTransaction] = useState(""); - useEffect(() => { - const btcUnsub = connectors.BTC?.on("connect", async (wallet) => { - if (wallet.provider) { - setWalletData((prev) => ({ - ...prev, - btcWallet: wallet.provider as IBTCProvider, - })); - } - }); - - return () => { - btcUnsub?.(); - }; - }, [connectors]); + const btcProvider = selectedWallets.BTC?.provider as IBTCProvider | undefined; const handleSignMessage = async () => { - if (!walletData.btcWallet || !messageToSign) return; + if (!btcProvider || !messageToSign) return; + try { - const signature = await walletData.btcWallet.signMessage(messageToSign, "ecdsa"); + const signature = await btcProvider.signMessage(messageToSign, "ecdsa"); console.log("handleSignMessage:", signature); - setWalletData((prev) => ({ - ...prev, - signedMessage: signature, - })); + setSignedMessage(signature); } catch (error) { console.error("Failed to sign message:", error); } }; const handleSignPsbt = async () => { - if (!walletData.btcWallet || !psbtToSign) return; + if (!btcProvider || !psbtToSign) return; try { - const signedPsbt = await walletData.btcWallet.signPsbt(psbtToSign); + const signedPsbt = await btcProvider.signPsbt(psbtToSign); console.log("handleSignPsbt:", signedPsbt); - setWalletData((prev) => ({ - ...prev, - signedPsbt, - })); + setSignedPsbt(signedPsbt); } catch (error) { console.error("Failed to sign PSBT:", error); } @@ -146,71 +124,74 @@ export const WithBTCSigningFeatures: Story = { return (
- + +
- {walletData.btcWallet && ( + {btcProvider && (
- - Sign Message - - setMessageToSign(e.target.value)} - placeholder="Enter message to sign" - className="b-mb-2 b-w-full b-rounded b-border b-p-2" - /> + + setMessageToSign(e.target.value)} + placeholder="Enter message to sign" + /> + + - {walletData.signedMessage && ( + + {signedMessage && (
- Signed Message: {walletData.signedMessage} + Signed Message: {signedMessage} - +
)}
- - Sign PSBT - - setPsbtToSign(e.target.value)} - placeholder="Enter PSBT hex" - className="b-mb-2 b-w-full b-rounded b-border b-p-2" - /> + + setPsbtToSign(e.target.value)} + placeholder="Enter PSBT hex" + /> + + - {walletData.signedPsbt && ( + + {signedPsbt && (
- Signed PSBT: {walletData.signedPsbt} + Signed PSBT: {signedPsbt}
)} - {walletData.signedPsbt && ( + + {signedPsbt && (
Transaction: {transaction}