Skip to content

Commit

Permalink
TW-1240: Magic Square campaign. -- 'pkh' from /start payload
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-tsx committed Jan 16, 2024
1 parent 226e2c0 commit 245f445
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/magic-square.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as ethersAddressUtils from '@ethersproject/address';
import * as ethersHashUtils from '@ethersproject/hash';
import * as ethersStringsUtils from '@ethersproject/strings';
import * as ethersTxUtils from '@ethersproject/transactions';
import { validateAddress, ValidationResult, verifySignature, getPkhfromPk } from '@taquito/utils';
import { verifySignature, getPkhfromPk } from '@taquito/utils';
import { StatusCodes } from 'http-status-codes';

import { objectStorageMethodsFactory } from './redis';
Expand All @@ -25,7 +25,6 @@ export function getMagicSquareQuestParticipants() {
}

interface StartQuestPayload {
pkh: string;
publicKey: string;
messageBytes: string;
signature: string;
Expand All @@ -36,11 +35,16 @@ interface StartQuestPayload {
};
}

export async function startMagicSquareQuest({ pkh, publicKey, messageBytes, signature, evm }: StartQuestPayload) {
export async function startMagicSquareQuest({ publicKey, messageBytes, signature, evm }: StartQuestPayload) {
// Public Key Hashes

if (!safeCheck(() => validateAddress(pkh) === ValidationResult.VALID && getPkhfromPk(publicKey) === pkh))
throw new CodedError(StatusCodes.BAD_REQUEST, 'Invalid Tezos public key (hash)');
let pkh: string;
try {
pkh = getPkhfromPk(publicKey);
} catch (err) {
console.error(err);
throw new CodedError(StatusCodes.BAD_REQUEST, 'Invalid Tezos public key');
}

let evmPkh: string;
try {
Expand All @@ -51,8 +55,6 @@ export async function startMagicSquareQuest({ pkh, publicKey, messageBytes, sign
throw new CodedError(StatusCodes.BAD_REQUEST, 'Invalid EVM public key hash');
}

const storageKey = `${pkh}+${evmPkh}`;

// Nonce
const { value: nonce } = getSigningNonce(pkh);
const nonceBytes = Buffer.from(nonce, 'utf-8').toString('hex');
Expand Down Expand Up @@ -80,12 +82,14 @@ export async function startMagicSquareQuest({ pkh, publicKey, messageBytes, sign

// Presence check

const storageKey = `${pkh}+${evmPkh}`;

const existingValue = await redisStorage.getByKey(storageKey);

if (existingValue)
throw new CodedError(StatusCodes.CONFLICT, 'Your quest was already started before', 'QUEST_IS_STARTED');

// Auth nonce
// Invalidating nonce
removeSigningNonce(pkh);

// Registering
Expand Down

0 comments on commit 245f445

Please sign in to comment.