Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
DOBEN committed Sep 23, 2024
1 parent 8e6089d commit 0d4361b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 39 deletions.
49 changes: 25 additions & 24 deletions compliant-reward-distribution/frontend/src/apiReqeuests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AccountAddress, AtomicStatementV2, CredentialStatement, VerifiablePresentation } from '@concordium/web-sdk';
import JSONBig from 'json-bigint';
import { AtomicStatementV2, CredentialStatement, VerifiablePresentation } from '@concordium/web-sdk';

/**
* Represents the stored data in the database of an indexed account (excluding tweet/zkProof data).
Expand All @@ -11,7 +12,7 @@ import { AccountAddress, AtomicStatementV2, CredentialStatement, VerifiablePrese
* conditions via a ZK proof. A manual check of the completed tasks is required before releasing the reward.
*/
interface StateData {
accountAddress: AccountAddress.Type;
accountAddress: string;
blockTime: string;
transactionHash: string;
claimed: boolean;
Expand All @@ -31,7 +32,7 @@ interface StateData {
* @property tweetSubmitTime - The timestamp when the tweet was submitted.
*/
interface TweetData {
accountAddress: AccountAddress.Type;
accountAddress: string;
tweetId: string | undefined;
tweetValid: boolean;
tweetVerificationVersion: number;
Expand All @@ -51,7 +52,7 @@ interface TweetData {
* @property zkProofVerificationSubmitTime - The timestamp when the ZK proof was submitted.
*/
interface ZkProofData {
accountAddress: AccountAddress.Type;
accountAddress: string;
uniquenessHash: string;
zkProofValid: boolean;
zkProofVerificationVersion: number;
Expand All @@ -65,7 +66,7 @@ interface ZkProofData {
* @property tweetData - Stored data of a submitted tweet if present.
* @property zkProofData - Stored data of a submitted ZK proof if present.
*/
interface AccountData {
export interface AccountData {
stateData: StateData | undefined;
tweetData: TweetData | undefined;
zkProofData: ZkProofData | undefined;
Expand All @@ -79,7 +80,7 @@ interface AccountData {
* @returns The request options for the specified method.
* @throws An error if the method is invalid or if the body is incorrectly provided for the method.
*/
function createRequestOptions(method: string, body?: string): RequestInit {
function createRequestOptions(method: string, body?: object): RequestInit {
switch (method) {
case 'GET':
return {
Expand All @@ -92,7 +93,7 @@ function createRequestOptions(method: string, body?: string): RequestInit {
return {
method: 'POST',
headers: new Headers({ 'content-type': 'application/json' }),
body: body,
body: JSONBig.stringify(body),
};
default:
throw new Error(`Invalid method: ${method}`);
Expand All @@ -110,7 +111,7 @@ function createRequestOptions(method: string, body?: string): RequestInit {
* @throws An error if the method is invalid, if the body is incorrectly provided for the method,
* or if the backend responses with an error.
*/
async function sendBackendRequest(endpoint: string, method: string, body?: string): Promise<Response> {
async function sendBackendRequest(endpoint: string, method: string, body?: object): Promise<Response> {
const api = `api/${endpoint}`;

const requestOption = createRequestOptions(method, body);
Expand Down Expand Up @@ -145,7 +146,7 @@ async function sendBackendRequest(endpoint: string, method: string, body?: strin
async function parseResponse<T = undefined>(response: Response): Promise<T> {
try {
// Parse the response as type `T`
return (await response.json()) as T;
return JSONBig.parse(await response.text()) as T;
} catch (e) {
throw new Error(`Failed to parse the response from the backend into expected type.`);
}
Expand All @@ -162,16 +163,16 @@ async function parseResponse<T = undefined>(response: Response): Promise<T> {
* @throws An error if the backend responses with an error.
*/
export async function setClaimed(signer: string, signature: string, recentBlockHeight: bigint, accountAddress: string) {
const body = JSON.stringify({
const body = {
signingData: {
signer,
message: {
accountAddresses: [accountAddress],
},
signature,
blockHeight: Number(recentBlockHeight),
blockHeight: recentBlockHeight,
},
});
};

return await sendBackendRequest('setClaimed', 'POST', body);
}
Expand All @@ -194,17 +195,17 @@ export async function getPendingApprovals(
limit: number,
offset: number,
): Promise<AccountData[] | undefined> {
const body = JSON.stringify({
const body = {
signingData: {
signer,
message: {
limit,
offset,
},
signature,
blockHeight: Number(recentBlockHeight),
blockHeight: recentBlockHeight,
},
});
};

const response = await sendBackendRequest('getPendingApprovals', 'POST', body);
return await parseResponse<AccountData[]>(response);
Expand All @@ -225,16 +226,16 @@ export async function getAccountData(
signature: string,
recentBlockHeight: bigint,
): Promise<AccountData> {
const body = JSON.stringify({
const body = {
signingData: {
signer,
message: {
accountAddress,
},
signature,
blockHeight: Number(recentBlockHeight),
blockHeight: recentBlockHeight,
},
});
};

const response = await sendBackendRequest('getAccountData', 'POST', body);
return await parseResponse<AccountData>(response);
Expand Down Expand Up @@ -274,16 +275,16 @@ export async function getStatement(): Promise<CredentialStatement> {
* @throws An error if the backend responses with an error.
*/
export async function submitTweet(signer: string, signature: string, recentBlockHeight: bigint, tweet: string) {
const body = JSON.stringify({
const body = {
signingData: {
signer,
message: {
tweet,
},
signature,
blockHeight: Number(recentBlockHeight),
blockHeight: recentBlockHeight,
},
});
};

return await sendBackendRequest('postTweet', 'POST', body);
}
Expand All @@ -297,10 +298,10 @@ export async function submitTweet(signer: string, signature: string, recentBlock
* @throws An error if the backend responses with an error.
*/
export async function submitZkProof(presentation: VerifiablePresentation, recentBlockHeight: bigint) {
const body = JSON.stringify({
blockHeight: Number(recentBlockHeight),
const body = {
blockHeight: recentBlockHeight,
presentation: presentation,
});
};

return await sendBackendRequest('postZKProof', 'POST', body);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ConcordiumGRPCClient } from '@concordium/web-sdk';
import { getRecentBlock, requestSignature, validateAccountAddress } from '../../utils';
import { WalletProvider } from '../../wallet-connection';
import { SCHEMA_GET_ACCOUNT_DATA_MESSAGE } from '../../constants';
import { getAccountData } from '../../apiReqeuests';
import { AccountData, getAccountData } from '../../apiReqeuests';

interface Props {
signer: string | undefined;
Expand All @@ -20,7 +20,7 @@ export function AdminGetAccountData(props: Props) {
const { signer, provider, grpcClient } = props;

const [error, setError] = useState<string | undefined>(undefined);
const [accountData, setAccountData] = useState<string | undefined>(undefined);
const [accountData, setAccountData] = useState<AccountData | undefined>(undefined);

interface FormType {
address: string;
Expand Down Expand Up @@ -50,7 +50,7 @@ export function AdminGetAccountData(props: Props) {
);

const data = await getAccountData(signer, address, signature, recentBlockHeight);
setAccountData(JSONbig.stringify(data));
setAccountData(data);
} catch (error) {
setError((error as Error).message);
}
Expand Down Expand Up @@ -78,7 +78,7 @@ export function AdminGetAccountData(props: Props) {
</Button>

<br />
{accountData && <pre className="pre">{JSON.stringify(JSON.parse(accountData), undefined, 2)}</pre>}
{accountData && <pre className="pre">{JSONbig.stringify(accountData, undefined, 2)}</pre>}

{error && <Alert variant="danger">{error}</Alert>}
</Form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ConcordiumGRPCClient } from '@concordium/web-sdk';
import { getRecentBlock, requestSignature } from '../../utils';
import { WalletProvider } from '../../wallet-connection';
import { LIMIT, OFFSET, SCHEMA_GET_PENDING_APPROVALS_MESSAGE } from '../../constants';
import { getPendingApprovals } from '../../apiReqeuests';
import { AccountData, getPendingApprovals } from '../../apiReqeuests';

interface Props {
signer: string | undefined;
Expand All @@ -20,7 +20,7 @@ export function AdminGetPendingApprovals(props: Props) {
const { signer, grpcClient, provider } = props;

const [error, setError] = useState<string | undefined>(undefined);
const [pendingApprovals, setPendingApprovals] = useState<string | undefined>(undefined);
const [pendingApprovals, setPendingApprovals] = useState<AccountData[] | undefined>(undefined);

const { handleSubmit } = useForm<[]>({ mode: 'all' });

Expand All @@ -44,7 +44,7 @@ export function AdminGetPendingApprovals(props: Props) {
);

const data = await getPendingApprovals(signer, signature, recentBlockHeight, LIMIT, OFFSET);
setPendingApprovals(JSONbig.stringify(data));
setPendingApprovals(data);
} catch (error) {
setError((error as Error).message);
}
Expand All @@ -62,9 +62,7 @@ export function AdminGetPendingApprovals(props: Props) {

{error && <Alert variant="danger">{error}</Alert>}

{pendingApprovals && (
<pre className="pre">{JSON.stringify(JSON.parse(pendingApprovals), undefined, 2)}</pre>
)}
{pendingApprovals && <pre className="pre">{JSONbig.stringify(pendingApprovals, undefined, 2)}</pre>}
</Form>
</div>
</div>
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 @@ -31,7 +31,7 @@ export async function getRecentBlock(grpcClient: ConcordiumGRPCClient | undefine

const recentBlockHeight = bestBlockHeight.value - RECENT_BLOCK_DURATION;

const recentBlockHash = ((await grpcClient.getBlocksAtHeight(recentBlockHeight)) as BlockHash.Type[])[0];
const recentBlockHash = (await grpcClient.getBlocksAtHeight(recentBlockHeight))[0];

if (!recentBlockHash) {
throw Error(`Couldn't get 'recentBlockHash' from chain`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class BrowserWalletProvider extends WalletProvider {
const payload = Buffer.from(
serializeTypeValue(
{
block_hash: Buffer.from(recentBlockHash.buffer).toString('hex'),
block_hash: BlockHash.toHexString(recentBlockHash),
context_string: CONTEXT_STRING,
message,
},
Expand Down Expand Up @@ -207,7 +207,7 @@ export class WalletConnectProvider extends WalletProvider {
const payload = Buffer.from(
serializeTypeValue(
{
block_hash: Buffer.from(recentBlockHash.buffer).toString('hex'),
block_hash: BlockHash.toHexString(recentBlockHash),
context_string: CONTEXT_STRING,
message,
},
Expand Down Expand Up @@ -270,7 +270,7 @@ export class WalletConnectProvider extends WalletProvider {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
if (isWalletConnectError(e)) {
throw new Error('Proof request rejected in wallet: ' + JSON.stringify(e));
throw new Error('Generating proof request rejected in wallet: ' + JSON.stringify(e));
}
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub enum ServerError {
UnderFlow,
#[error(
"The account was not captured by the indexer and is not in the database. Only accounts \
earlier than block height {0} are captured."
later than block height {0} are captured."
)]
AccountNotExist(AbsoluteBlockHeight),
#[error("Claim already expired. Your account creation has to be not older than {0}.")]
Expand Down

0 comments on commit 0d4361b

Please sign in to comment.