Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
DOBEN committed Sep 16, 2024
1 parent 663863c commit 9601f50
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 59 deletions.
18 changes: 7 additions & 11 deletions compliant-reward-distribution/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<WalletProvider>();
Expand All @@ -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]);
}
Expand Down Expand Up @@ -74,17 +73,14 @@ export const App = () => {
/>
<Route
path="/zkProofSubmission"
element={<ZkProofSubmission accountAddress={account} provider={provider} grpcClient={grpcClient} />}
element={<ZkProofSubmission prover={account} provider={provider} grpcClient={grpcClient} />}
/>
<Route
path="/tweetSubmission"
element={<TweetSubmission signer={account} provider={provider} grpcClient={grpcClient} />}
/>

<Route
path="/Admin"
element={<Admin provider={provider} accountAddress={account} grpcClient={grpcClient} />}
/>
<Route path="/Admin" element={<Admin signer={account} provider={provider} grpcClient={grpcClient} />} />
<Route path="/" element={<div></div>} />
</Routes>
</Router>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className="centered">
<AdminGetPendingApprovals provider={provider} signer={accountAddress} grpcClient={grpcClient} />
<AdminGetAccountData provider={provider} signer={accountAddress} grpcClient={grpcClient} />
<AdminSetClaimed provider={provider} signer={accountAddress} grpcClient={grpcClient} />
<AdminGetPendingApprovals provider={provider} signer={signer} grpcClient={grpcClient} />
<AdminGetAccountData provider={provider} signer={signer} grpcClient={grpcClient} />
<AdminSetClaimed provider={provider} signer={signer} grpcClient={grpcClient} />
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined>(undefined);
const [accountData, setAccountData] = useState<string | undefined>(undefined);

interface FormType {
address: string;
}
const { control, register, formState, handleSubmit } = useForm<FormType>({ mode: 'all' });

const [address] = useWatch({
control: control,
name: ['address'],
});

const [error, setError] = useState<string | undefined>(undefined);
const [accountData, setAccountData] = useState<string | undefined>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined>(undefined);
const [pendingApprovals, setPendingApprovals] = useState<string | undefined>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined>(undefined);
const [successfulSubmission, setSuccessfulSubmission] = useState<boolean | undefined>(undefined);

interface FormType {
address: string;
}
const { control, register, formState, handleSubmit } = useForm<FormType>({ mode: 'all' });

const [address] = useWatch({
control: control,
name: ['address'],
});

const [error, setError] = useState<string | undefined>(undefined);
const [successfulSubmission, setSuccessfulSubmission] = useState<boolean | undefined>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ const checkTweetUrlFormat = (url: string) => {
export function TweetSubmission(props: Props) {
const { signer, grpcClient, provider } = props;

const [error, setError] = useState<string | undefined>(undefined);
const [successfulSubmission, setSuccessfulSubmission] = useState<boolean | undefined>(undefined);

interface FormType {
tweet: string;
}
const { control, register, formState, handleSubmit } = useForm<FormType>({ mode: 'all' });

const [tweet] = useWatch({
control: control,
name: ['tweet'],
});

const [error, setError] = useState<string | undefined>(undefined);
const [successfulSubmission, setSuccessfulSubmission] = useState<boolean | undefined>(undefined);

async function onSubmit() {
setError(undefined);
setSuccessfulSubmission(undefined);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined>(undefined);
const [successfulSubmission, setSuccessfulSubmission] = useState<boolean | undefined>(undefined);
Expand All @@ -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.`,
);
}

Expand All @@ -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]);
Expand All @@ -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}).`,
);
}

Expand Down
2 changes: 1 addition & 1 deletion compliant-reward-distribution/frontend/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function requestSignature(
provider: WalletProvider | undefined,
): Promise<string> {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))?;

Expand Down Expand Up @@ -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,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9601f50

Please sign in to comment.