Skip to content

Commit

Permalink
TW-1240: Magic Square campaign. + Msg nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-tsx committed Jan 13, 2024
1 parent de6e712 commit e34362c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { coinGeckoTokens } from './utils/gecko-tokens';
import { getExternalApiErrorPayload, isDefined, isNonEmptyString } from './utils/helpers';
import logger from './utils/logger';
import { getSignedMoonPayUrl } from './utils/moonpay/get-signed-moonpay-url';
import { getSigningNonce, SIGNING_NONCE_TTL } from './utils/signing-nonce';
import { getSigningNonce } from './utils/signing-nonce';
import SingleQueryDataProvider from './utils/SingleQueryDataProvider';
import { tezExchangeRateProvider } from './utils/tezos';
import { getExchangeRatesFromDB } from './utils/tokens';
Expand Down Expand Up @@ -360,14 +360,12 @@ app.get('/api/magic-square-quest/participants', basicAuth, async (req, res) => {
}
});

app.get('/api/auth-nonce', async (req, res) => {
app.get('/api/signing-nonce', async (req, res) => {
try {
const pkh = req.query.pkh;
if (!pkh || typeof pkh !== 'string') throw new Error('PKH is not a string');

Check warning on line 366 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected value in conditional. A boolean expression is required

const nonce = getSigningNonce(pkh);

res.status(200).send({ nonce, ttl: SIGNING_NONCE_TTL });
res.status(200).send(getSigningNonce(pkh));
} catch (error: any) {

Check warning on line 369 in src/index.ts

View workflow job for this annotation

GitHub Actions / Checks if ts and lint works

Unexpected any. Specify a different type
console.error(error);

Expand Down
17 changes: 13 additions & 4 deletions src/magic-square.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { validateAddress, ValidationResult, verifySignature, getPkhfromPk } from
import { redisClient } from './redis';
import { CodedError } from './utils/errors';
import { safeCheck } from './utils/helpers';
import { getSigningNonce } from './utils/signing-nonce';
import { getSigningNonce, removeSigningNonce } from './utils/signing-nonce';

const REDIS_DB_KEY = 'magic_square_quest';

Expand Down Expand Up @@ -43,6 +43,16 @@ export async function startMagicSquareQuest({ pkh, publicKey, messageBytes, sign
throw new CodedError(STATUS_CODE.BAD_REQUEST, 'Invalid EVM public key hash');
}

// Nonce
const { value: nonce } = getSigningNonce(pkh);
const nonceBytes = Buffer.from(nonce, 'utf-8').toString('hex');

if (!messageBytes.includes(nonceBytes))
throw new CodedError(STATUS_CODE.UNAUTHORIZED, 'Invalid Tezos message nonce', 'INVALID_NONCE_TEZ');

if (!evm.messageBytes.includes(nonceBytes))
throw new CodedError(STATUS_CODE.UNAUTHORIZED, 'Invalid EVM message nonce', 'INVALID_NONCE_EVM');

// Signatures

if (!safeCheck(() => verifySignature(messageBytes, publicKey, signature)))
Expand All @@ -64,11 +74,10 @@ export async function startMagicSquareQuest({ pkh, publicKey, messageBytes, sign
.lrange(REDIS_DB_KEY, 0, -1)
.then(items => items.some(item => item.includes(pkh) && item.includes(evmPkh)));

if (exists)
throw new CodedError(STATUS_CODE.CONFLICT, 'Quest already started for the given credentials', 'QUEST_STARTED');
if (exists) throw new CodedError(STATUS_CODE.CONFLICT, 'Your quest was already started before', 'QUEST_IS_STARTED');

// Auth nonce
getSigningNonce.delete(pkh);
removeSigningNonce(pkh);

// Registering

Expand Down
31 changes: 20 additions & 11 deletions src/utils/signing-nonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ import memoizee from 'memoizee';

import { CodedError } from './errors';

export const SIGNING_NONCE_TTL = 5 * 60_000;
const SIGNING_NONCE_TTL = 5 * 60_000;

const MEMOIZE_OPTIONS = {
max: 500,
maxAge: SIGNING_NONCE_TTL
};
export const getSigningNonce = memoizee(
(pkh: string) => {
if (validateAddress(pkh) !== ValidationResult.VALID) throw new CodedError(400, 'Invalid address');

export const getSigningNonce = memoizee((pkh: string) => {
if (validateAddress(pkh) !== ValidationResult.VALID) throw new CodedError(400, 'Invalid address');
return buildNonce();
},
{
max: 500,
maxAge: SIGNING_NONCE_TTL
}
);

return buildNonce();
}, MEMOIZE_OPTIONS);
export function removeSigningNonce(pkh: string) {
getSigningNonce.delete(pkh);
}

function buildNonce() {
// The way it is done in SIWE.generateNonce()
return randomStringForEntropy(96);
// Same as in in SIWE.generateNonce()
const value = randomStringForEntropy(96);

const expiresAt = new Date(Date.now() + SIGNING_NONCE_TTL).toISOString();

return { value, expiresAt };
}

0 comments on commit e34362c

Please sign in to comment.