From 9601f50bc7633553783177f365a059f91aef07cb Mon Sep 17 00:00:00 2001 From: Doris Benda Date: Mon, 16 Sep 2024 11:04:43 +0300 Subject: [PATCH] Clean up --- .../frontend/src/App.tsx | 18 +++++++----------- .../frontend/src/components/Admin/Admin.tsx | 10 +++++----- .../components/Admin/AdminGetAccountData.tsx | 11 +++++------ .../Admin/AdminGetPendingApprovals.tsx | 10 +++++----- .../src/components/Admin/AdminSetClaimed.tsx | 13 ++++++------- .../src/components/TweetSubmission.tsx | 9 ++++----- .../src/components/ZkProofSubmission.tsx | 18 +++++++++--------- .../frontend/src/utils.ts | 2 +- .../indexer-and-server/src/bin/server.rs | 11 ++++++----- .../indexer-and-server/src/constants.rs | 6 +----- 10 files changed, 49 insertions(+), 59 deletions(-) diff --git a/compliant-reward-distribution/frontend/src/App.tsx b/compliant-reward-distribution/frontend/src/App.tsx index 3853e624..e24f080f 100644 --- a/compliant-reward-distribution/frontend/src/App.tsx +++ b/compliant-reward-distribution/frontend/src/App.tsx @@ -2,17 +2,17 @@ import { useEffect, useRef, useState } from 'react'; import { BrowserRouter as Router, Route, Routes, Link } from 'react-router-dom'; import { Button } from 'react-bootstrap'; +import { GrpcWebFetchTransport } from '@protobuf-ts/grpcweb-transport'; +import { ConcordiumGRPCClient } from '@concordium/web-sdk'; + +import './styles.scss'; import { WalletProvider } from './wallet-connection'; import { version } from '../package.json'; -import './styles.scss'; - +import { GRPC_NODE_URL } from './constants'; import { ConnectWallet } from './components/ConnectWallet'; import { ZkProofSubmission } from './components/ZkProofSubmission'; import { Admin } from './components/Admin/Admin'; import { TweetSubmission } from './components/TweetSubmission'; -import { ConcordiumGRPCClient } from '@concordium/web-sdk'; -import { GrpcWebFetchTransport } from '@protobuf-ts/grpcweb-transport'; -import { GRPC_NODE_URL } from './constants'; export const App = () => { const [provider, setProvider] = useState(); @@ -30,7 +30,6 @@ export const App = () => { const connectProvider = async (provider: WalletProvider) => { const accounts = await provider.connect(); - // TODO if no account or wrong network; report error. if (accounts && accounts?.length != 0) { setAccount(accounts[0]); } @@ -74,17 +73,14 @@ export const App = () => { /> } + element={} /> } /> - } - /> + } /> } /> diff --git a/compliant-reward-distribution/frontend/src/components/Admin/Admin.tsx b/compliant-reward-distribution/frontend/src/components/Admin/Admin.tsx index f2d09029..6301d4f5 100644 --- a/compliant-reward-distribution/frontend/src/components/Admin/Admin.tsx +++ b/compliant-reward-distribution/frontend/src/components/Admin/Admin.tsx @@ -6,19 +6,19 @@ import { AdminSetClaimed } from './AdminSetClaimed'; import { WalletProvider } from '../../wallet-connection'; interface Props { - accountAddress: string | undefined; + signer: string | undefined; grpcClient: ConcordiumGRPCClient | undefined; provider: WalletProvider | undefined; } export function Admin(props: Props) { - const { accountAddress, grpcClient, provider } = props; + const { signer, grpcClient, provider } = props; return (
- - - + + +
); } diff --git a/compliant-reward-distribution/frontend/src/components/Admin/AdminGetAccountData.tsx b/compliant-reward-distribution/frontend/src/components/Admin/AdminGetAccountData.tsx index 5349f39d..7439cd6a 100644 --- a/compliant-reward-distribution/frontend/src/components/Admin/AdminGetAccountData.tsx +++ b/compliant-reward-distribution/frontend/src/components/Admin/AdminGetAccountData.tsx @@ -17,28 +17,27 @@ interface Props { } export function AdminGetAccountData(props: Props) { - const { provider, signer, grpcClient } = props; + const { signer, provider, grpcClient } = props; + + const [error, setError] = useState(undefined); + const [accountData, setAccountData] = useState(undefined); interface FormType { address: string; } const { control, register, formState, handleSubmit } = useForm({ mode: 'all' }); - const [address] = useWatch({ control: control, name: ['address'], }); - const [error, setError] = useState(undefined); - const [accountData, setAccountData] = useState(undefined); - async function onSubmit() { setError(undefined); setAccountData(undefined); try { if (!signer) { - throw Error(`'signer' is undefined. Connect your wallet.`); + throw Error(`'signer' is undefined. Connect your wallet. Have an account in your wallet.`); } const { blockHash: recentBlockHash, blockHeight: recentBlockHeight } = await getRecentBlock(grpcClient); diff --git a/compliant-reward-distribution/frontend/src/components/Admin/AdminGetPendingApprovals.tsx b/compliant-reward-distribution/frontend/src/components/Admin/AdminGetPendingApprovals.tsx index f6a6878f..a1a07363 100644 --- a/compliant-reward-distribution/frontend/src/components/Admin/AdminGetPendingApprovals.tsx +++ b/compliant-reward-distribution/frontend/src/components/Admin/AdminGetPendingApprovals.tsx @@ -11,26 +11,26 @@ import { LIMIT, OFFSET, SCHEMA_GET_PENDING_APPROVALS_MESSAGE } from '../../const import { getPendingApprovals } from '../../apiReqeuests'; interface Props { - provider: WalletProvider | undefined; signer: string | undefined; grpcClient: ConcordiumGRPCClient | undefined; + provider: WalletProvider | undefined; } export function AdminGetPendingApprovals(props: Props) { - const { provider, signer, grpcClient } = props; - - const { handleSubmit } = useForm<[]>({ mode: 'all' }); + const { signer, grpcClient, provider } = props; const [error, setError] = useState(undefined); const [pendingApprovals, setPendingApprovals] = useState(undefined); + const { handleSubmit } = useForm<[]>({ mode: 'all' }); + async function onSubmit() { setError(undefined); setPendingApprovals(undefined); try { if (!signer) { - throw Error(`'signer' is undefined. Connect your wallet.`); + throw Error(`'signer' is undefined. Connect your wallet. Have an account in your wallet.`); } const { blockHash: recentBlockHash, blockHeight: recentBlockHeight } = await getRecentBlock(grpcClient); diff --git a/compliant-reward-distribution/frontend/src/components/Admin/AdminSetClaimed.tsx b/compliant-reward-distribution/frontend/src/components/Admin/AdminSetClaimed.tsx index 5eb959b1..4d7ad185 100644 --- a/compliant-reward-distribution/frontend/src/components/Admin/AdminSetClaimed.tsx +++ b/compliant-reward-distribution/frontend/src/components/Admin/AdminSetClaimed.tsx @@ -10,34 +10,33 @@ import { SCHEMA_SET_CLAIMED_MESSAGE } from '../../constants'; import { setClaimed } from '../../apiReqeuests'; interface Props { - provider: WalletProvider | undefined; signer: string | undefined; grpcClient: ConcordiumGRPCClient | undefined; + provider: WalletProvider | undefined; } export function AdminSetClaimed(props: Props) { - const { provider, signer, grpcClient } = props; + const { signer, grpcClient, provider } = props; + + const [error, setError] = useState(undefined); + const [successfulSubmission, setSuccessfulSubmission] = useState(undefined); interface FormType { address: string; } const { control, register, formState, handleSubmit } = useForm({ mode: 'all' }); - const [address] = useWatch({ control: control, name: ['address'], }); - const [error, setError] = useState(undefined); - const [successfulSubmission, setSuccessfulSubmission] = useState(undefined); - async function onSubmit() { setError(undefined); setSuccessfulSubmission(undefined); try { if (!signer) { - throw Error(`'signer' is undefined. Connect your wallet.`); + throw Error(`'signer' is undefined. Connect your wallet. Have an account in your wallet.`); } const { blockHash: recentBlockHash, blockHeight: recentBlockHeight } = await getRecentBlock(grpcClient); diff --git a/compliant-reward-distribution/frontend/src/components/TweetSubmission.tsx b/compliant-reward-distribution/frontend/src/components/TweetSubmission.tsx index 9b2d862e..ac0fb272 100644 --- a/compliant-reward-distribution/frontend/src/components/TweetSubmission.tsx +++ b/compliant-reward-distribution/frontend/src/components/TweetSubmission.tsx @@ -28,19 +28,18 @@ const checkTweetUrlFormat = (url: string) => { export function TweetSubmission(props: Props) { const { signer, grpcClient, provider } = props; + const [error, setError] = useState(undefined); + const [successfulSubmission, setSuccessfulSubmission] = useState(undefined); + interface FormType { tweet: string; } const { control, register, formState, handleSubmit } = useForm({ mode: 'all' }); - const [tweet] = useWatch({ control: control, name: ['tweet'], }); - const [error, setError] = useState(undefined); - const [successfulSubmission, setSuccessfulSubmission] = useState(undefined); - async function onSubmit() { setError(undefined); setSuccessfulSubmission(undefined); @@ -49,7 +48,7 @@ export function TweetSubmission(props: Props) { checkTweetUrlFormat(tweet); if (!signer) { - throw Error(`'signer' is undefined. Connect your wallet.`); + throw Error(`'signer' is undefined. Connect your wallet. Have an account in your wallet.`); } const { blockHash: recentBlockHash, blockHeight: recentBlockHeight } = await getRecentBlock(grpcClient); diff --git a/compliant-reward-distribution/frontend/src/components/ZkProofSubmission.tsx b/compliant-reward-distribution/frontend/src/components/ZkProofSubmission.tsx index 415c49e3..a3517a1b 100644 --- a/compliant-reward-distribution/frontend/src/components/ZkProofSubmission.tsx +++ b/compliant-reward-distribution/frontend/src/components/ZkProofSubmission.tsx @@ -17,13 +17,13 @@ import { CONTEXT_STRING } from '../constants'; import { getStatement, submitZkProof } from '../apiReqeuests'; interface Props { - accountAddress: string | undefined; - provider: WalletProvider | undefined; + prover: string | undefined; grpcClient: ConcordiumGRPCClient | undefined; + provider: WalletProvider | undefined; } export function ZkProofSubmission(props: Props) { - const { provider, grpcClient, accountAddress } = props; + const { prover, provider, grpcClient } = props; const [error, setError] = useState(undefined); const [successfulSubmission, setSuccessfulSubmission] = useState(undefined); @@ -47,12 +47,12 @@ export function ZkProofSubmission(props: Props) { try { if (!zkStatement) { - throw Error(`'zkStatement' is undefined`); + throw Error(`'zkStatement' is undefined.`); } - if (!provider || !accountAddress) { + if (!provider || !prover) { throw Error( - `'provider' or 'accountAddress' are undefined. Connect your wallet and have an account created in it.`, + `'provider' or 'prover' are undefined. Connect your wallet. Have an account in your wallet.`, ); } @@ -61,9 +61,9 @@ export function ZkProofSubmission(props: Props) { const digest = [recentBlockHash, Buffer.from(CONTEXT_STRING)]; const challenge = sha256(digest.flatMap((item) => Array.from(item))); - const accountInfo = await grpcClient?.getAccountInfo(AccountAddress.fromBase58(accountAddress)); + const accountInfoProver = await grpcClient?.getAccountInfo(AccountAddress.fromBase58(prover)); const credIdConnectedAccount = ( - accountInfo?.accountCredentials[0].value.contents as CredentialDeploymentValues + accountInfoProver?.accountCredentials[0].value.contents as CredentialDeploymentValues ).credId; const presentation = await provider.requestVerifiablePresentation(challenge, [zkStatement]); @@ -76,7 +76,7 @@ export function ZkProofSubmission(props: Props) { ) ) { throw Error( - `When approving the ZK proof in the wallet, select your connected account from the drop-down menu in the wallet (expect proof for account: ${accountAddress}).`, + `When approving the ZK proof in the wallet, select your connected account from the drop-down menu in the wallet (expect proof for account: ${prover}).`, ); } diff --git a/compliant-reward-distribution/frontend/src/utils.ts b/compliant-reward-distribution/frontend/src/utils.ts index caabb296..fcbcb789 100644 --- a/compliant-reward-distribution/frontend/src/utils.ts +++ b/compliant-reward-distribution/frontend/src/utils.ts @@ -68,7 +68,7 @@ export async function requestSignature( provider: WalletProvider | undefined, ): Promise { if (!provider) { - throw Error(`'provider' is undefined. Connect your wallet.`); + throw Error(`'provider' is undefined. Connect your wallet. Have an account in your wallet.`); } const signatures = await provider.signMessage(signer, message, recentBlockHash, schema); diff --git a/compliant-reward-distribution/indexer-and-server/src/bin/server.rs b/compliant-reward-distribution/indexer-and-server/src/bin/server.rs index 59089201..1e420f07 100644 --- a/compliant-reward-distribution/indexer-and-server/src/bin/server.rs +++ b/compliant-reward-distribution/indexer-and-server/src/bin/server.rs @@ -26,9 +26,9 @@ use concordium_rust_sdk::{ use handlebars::{no_escape, Handlebars}; use indexer::{ constants::{ - CONTEXT_STRING, CONTEXT_STRING_2, CURRENT_TWEET_VERIFICATION_VERSION, - CURRENT_ZK_PROOF_VERIFICATION_VERSION, MAX_REQUEST_LIMIT, - SIGNATURE_AND_PROOF_EXPIRY_DURATION_BLOCKS, TESTNET_GENESIS_BLOCK_HASH, ZK_STATEMENTS, + CONTEXT_STRING, CURRENT_TWEET_VERIFICATION_VERSION, CURRENT_ZK_PROOF_VERIFICATION_VERSION, + MAX_REQUEST_LIMIT, SIGNATURE_AND_PROOF_EXPIRY_DURATION_BLOCKS, TESTNET_GENESIS_BLOCK_HASH, + ZK_STATEMENTS, }, db::{AccountData, Database, StoredAccountData}, error::ServerError, @@ -364,7 +364,8 @@ async fn check_zk_proof( // SIGNATURE_AND_PROOF_EXPIRY_DURATION_BLOCKS. The `CONTEXT_STRING` ensures // that the proof is generated for this specific service. These checks are // done similarly in the `signature` verification flow in this service. - let challenge_hash = sha2::Sha256::digest([block_hash.as_ref(), &CONTEXT_STRING].concat()); + let challenge_hash = + sha2::Sha256::digest([block_hash.as_ref(), &CONTEXT_STRING.as_bytes()].concat()); let challenge = Challenge::try_from(challenge_hash.as_slice()) .map_err(|e| ServerError::TypeConversion("challenge".to_string(), e))?; @@ -504,7 +505,7 @@ where // Concordium services), and the message. let message_signed_in_wallet = MessageSigned { block_hash: hex::encode(block_hash), - context_string: CONTEXT_STRING_2.to_string(), + context_string: CONTEXT_STRING.to_string(), message, }; diff --git a/compliant-reward-distribution/indexer-and-server/src/constants.rs b/compliant-reward-distribution/indexer-and-server/src/constants.rs index f4e2c5aa..717a76f1 100644 --- a/compliant-reward-distribution/indexer-and-server/src/constants.rs +++ b/compliant-reward-distribution/indexer-and-server/src/constants.rs @@ -12,11 +12,7 @@ pub const TESTNET_GENESIS_BLOCK_HASH: [u8; 32] = [ /// can be used in different Concordium services without the risk of re-playing /// signatures/zk-proofs across the different services due to this context /// string. -pub const CONTEXT_STRING: [u8; 45] = [ - 67, 79, 78, 67, 79, 82, 68, 73, 85, 77, 95, 67, 79, 77, 80, 76, 73, 65, 78, 84, 95, 82, 69, 87, - 65, 82, 68, 95, 68, 73, 83, 84, 82, 73, 66, 85, 84, 73, 79, 78, 95, 68, 65, 80, 80, -]; -pub const CONTEXT_STRING_2: &str = "CONCORDIUM_COMPLIANT_REWARD_DISTRIBUTION_DAPP"; +pub const CONTEXT_STRING: &str = "CONCORDIUM_COMPLIANT_REWARD_DISTRIBUTION_DAPP"; /// The number of blocks after that a generated signature or ZK proof is /// considered expired.