From 96680131962aa6e4517d1d8dfb7324bfa663ac63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20=C3=87al=C4=B1=C5=9Fkan?= Date: Wed, 27 Sep 2023 14:55:14 +0200 Subject: [PATCH] Check arbitrary data is signed correctly --- src/core/home/Home.tsx | 15 +++++++++----- src/core/home/sign-txn/SignTxn.tsx | 33 +++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/core/home/Home.tsx b/src/core/home/Home.tsx index 7728311..ad3c703 100644 --- a/src/core/home/Home.tsx +++ b/src/core/home/Home.tsx @@ -198,11 +198,16 @@ function Home() { } async function handleConnectWalletClick() { - const newAccounts = await peraWallet.connect(); - - handleSetLog("Connected to Pera Wallet"); - - setAccountAddress(newAccounts[0]); + try { + const newAccounts = await peraWallet.connect(); + + handleSetLog("Connected to Pera Wallet"); + + setAccountAddress(newAccounts[0]); + } catch (e) { + console.log(e); + handleSetLog(`${e}`); + } } function handleDisconnectWalletClick() { diff --git a/src/core/home/sign-txn/SignTxn.tsx b/src/core/home/sign-txn/SignTxn.tsx index d7102a3..d69502a 100644 --- a/src/core/home/sign-txn/SignTxn.tsx +++ b/src/core/home/sign-txn/SignTxn.tsx @@ -2,6 +2,7 @@ import {useState} from "react"; import {Button, List, ListItem} from "@hipo/react-ui-toolkit"; import {PeraWalletConnect} from "@perawallet/connect"; import {SignerTransaction} from "@perawallet/connect/dist/util/model/peraWalletModels"; +import algosdk from "algosdk"; import {mainnetScenarios, Scenario, scenarios} from "./util/signTxnUtils"; import {ChainType, clientForChain} from "../../utils/algod/algod"; @@ -90,23 +91,35 @@ function SignTxn({ async function signArbitraryData() { try { + const unsignedData = [ + { + data: new Uint8Array(Buffer.from(`timestamp//${Date.now()}`)), + message: "Timestamp confirmation" + }, + { + data: new Uint8Array(Buffer.from(`agent//${navigator.userAgent}`)), + message: "User agent confirmation" + } + ]; const signedData: Uint8Array[] = await peraWallet.signData( - [ - { - data: new Uint8Array(Buffer.from(`timestamp//${Date.now()}`)), - message: "Timestamp confirmation" - }, - { - data: new Uint8Array(Buffer.from(`agent//${navigator.userAgent}`)), - message: "User agent confirmation" - } - ], + unsignedData, accountAddress ); + unsignedData.forEach((data, index) => { + const isVerified = algosdk.verifyBytes(data.data, signedData[index], accountAddress) + + console.log({data, signedData: signedData[index], isVerified}); + + if (!isVerified) { + handleSetLog(`Arbitrary data did not match with signed data!`); + } + }); + console.log({signedData}); handleSetLog("Data signed successfully"); } catch (error) { + console.log(error) handleSetLog(`${error}`); } }