Skip to content

Commit

Permalink
Check arbitrary data is signed correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
yasincaliskan committed Sep 27, 2023
1 parent 5286d75 commit 9668013
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
15 changes: 10 additions & 5 deletions src/core/home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
33 changes: 23 additions & 10 deletions src/core/home/sign-txn/SignTxn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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}`);
}
}
Expand Down

0 comments on commit 9668013

Please sign in to comment.