Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug/tnc: fix braavos sig verification #158

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions src/app/api/tnc/signUser/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import { LATEST_TNC_DOC_VERSION, SIGNING_DATA } from '@/constants';
import { db } from '@/db';
import { standariseAddress } from '@/utils';
import { Account, RpcProvider } from 'starknet';
import { Account, CallData, RpcProvider, stark } from 'starknet';
import { toBigInt } from 'ethers';

export async function POST(req: Request) {
const { address, signature } = await req.json();
Expand Down Expand Up @@ -45,7 +46,8 @@

console.log(`Verifying signature for address: ${parsedAddress}`);
try {
isValid = await myAccount.verifyMessage(SIGNING_DATA, parsedSignature);
const hash = await myAccount.hashMessage(SIGNING_DATA);
isValid = await verifyMessageHash(myAccount, hash, parsedSignature);
console.log('isValid', isValid);
} catch (error) {
console.log('verification failed:', error);
Expand Down Expand Up @@ -98,3 +100,44 @@
user: updatedUser,
});
}

async function verifyMessageHash(
account: Account,
hash: string,
signature: string[],
entrypoint = 'isValidSignature',
) {
try {
const resp = await account.callContract({
contractAddress: account.address,
entrypoint,
calldata: CallData.compile({
hash: toBigInt(hash).toString(),
signature: stark.formatSignature(signature),
}),
});
if (Number(resp[0]) == 0) {

Check warning on line 119 in src/app/api/tnc/signUser/route.ts

View workflow job for this annotation

GitHub Actions / Performs linting, formatting on the application

Expected '===' and instead saw '=='
return false;
}
return true;
} catch (err: any) {
if (entrypoint === 'isValidSignature') {
console.warn(
'could be Invalid message selector, trying with is_valid_signature',
);
return verifyMessageHash(account, hash, signature, 'is_valid_signature');
}

if (
[
'argent/invalid-signature',
'is invalid, with respect to the public key',
].some((errMessage) => err.message.includes(errMessage))
) {
return false;
}
throw Error(
`Signature verification request is rejected by the network: ${err}`,
);
}
}
28 changes: 16 additions & 12 deletions src/components/TncModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { addressAtom } from '@/store/claims.atoms';
import {
Button,
Center,
Link,
Modal,
ModalBody,
ModalContent,
Expand Down Expand Up @@ -101,9 +100,12 @@ const TncModal: React.FC<TncModalProps> = (props) => {
setIsSigningPending(true);

try {
const signature = (await account.signMessage(SIGNING_DATA)) as string[];
const _signature = (await account.signMessage(SIGNING_DATA)) as string[];

console.log('signature', signature);
console.log('signature', _signature);
const sig_len = _signature.length;
const signature =
sig_len > 2 ? _signature.slice(sig_len - 2, sig_len) : _signature;
if (signature && signature.length > 0) {
const res2 = await axios.post('/api/tnc/signUser', {
address,
Expand Down Expand Up @@ -154,15 +156,17 @@ const TncModal: React.FC<TncModalProps> = (props) => {
signing. You are required to sign this to continue using the App.
</Text>

<Text textAlign="left" width={'100%'} fontWeight={'bold'}>
<Link
href={SIGNING_DATA.message.document}
color="white"
target="_blank"
_hover={{ textDecor: 'underline' }}
>
T&C Document link <ExternalLinkIcon />
</Link>
<Text
textAlign="left"
as={'a'}
width={'100%'}
fontWeight={'bold'}
href={SIGNING_DATA.message.document}
color="white"
target="_blank"
_hover={{ textDecor: 'underline' }}
>
T&C Document link <ExternalLinkIcon />
</Text>

<Text textAlign="left" width={'100%'}>
Expand Down
Loading