Skip to content

Commit

Permalink
Merge pull request #9 from perawallet/fix/check-arbitrary-data
Browse files Browse the repository at this point in the history
Check arbitrary data is signed correctly
  • Loading branch information
yasincaliskan authored Oct 3, 2023
2 parents c8183e7 + eac7bb5 commit d678a04
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/core/home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ function Home() {

setAccountAddress(newAccounts[0]);
} catch (e) {
console.log(e)
console.log(e);
handleSetLog(`${e}`);
}
}

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 d678a04

Please sign in to comment.