Skip to content

Commit

Permalink
Merge pull request #41 from GeneralMagicio/staging
Browse files Browse the repository at this point in the history
Combine store badge & identity insertion methods
  • Loading branch information
mmahdigh authored Jul 1, 2024
2 parents 671c661 + ade7a25 commit de2060d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 20 deletions.
2 changes: 2 additions & 0 deletions prisma/migrations/20240630094629_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- DropIndex
DROP INDEX "User_identity_key";
3 changes: 2 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ model User {
isBadgeHolder Int @map("is_badgeholder")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
identity Json? @unique()
identity Json? // Ideally should be unique except {} and null values but Prisma doesn't support partial
// unique constraints
badges Json?
projectVotes Vote[]
nonce Nonce?
Expand Down
20 changes: 10 additions & 10 deletions src/flow/flow.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,15 +682,15 @@ export class FlowController {
// for (let i = 2; i < 9; i++) {
const user = await this.prismaService.user.findFirst({
select: { id: true },
where: { address: '0xcb73a971e3643f756E0Ce8c81cccA3D7B6AB2b9d' },
where: { address: '0x9cb5129b4c710dB85F1dcd4071CB5f777e5B7612' },
});

// console.log(user?.id);

if (user && !userId) {
userId = user?.id;
console.log(user.id);
} else return;
} else if (!user && !userId) return;

// userId = 448;

Expand Down Expand Up @@ -726,17 +726,17 @@ export class FlowController {
where: { userId: userId },
});

await this.prismaService.user.update({
where: { id: userId },
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
data: { identity: null, badges: null },
});

// await this.prismaService.user.deleteMany({
// await this.prismaService.user.update({
// where: { id: userId },
// // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// // @ts-ignore
// data: { identity: null, badges: null },
// });

await this.prismaService.user.deleteMany({
where: { id: userId },
});

// }
}
}
12 changes: 12 additions & 0 deletions src/user/dto/ConnectFlowDTOs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { IsDefined, IsEthereumAddress } from 'class-validator';

export class StoreBadgesAndIdentityDTO {
@IsEthereumAddress()
@IsDefined()
mainAddress: string;

@IsDefined()
signature: string;

@IsDefined()
identity: string;
}

export class StoreBadgesDTO {
@IsEthereumAddress()
@IsDefined()
Expand Down
81 changes: 72 additions & 9 deletions src/user/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { AuthedReq } from 'src/utils/types/AuthedReq.type';
import {
GetBadgesDTO,
StoreBadgesAndIdentityDTO,
StoreBadgesDTO,
StoreIdentityDTO,
} from './dto/ConnectFlowDTOs';
Expand All @@ -33,6 +34,59 @@ export class UsersController {
private readonly logger = new Logger(UsersController.name);
constructor(private readonly prismaService: PrismaService) {}

@UseGuards(AuthGuard)
@Post('/store-badges-identity')
async storeBadgesAndIdentity(
@Req() { userId }: AuthedReq,
@Body() { mainAddress, signature, identity }: StoreBadgesAndIdentityDTO,
) {
if (
!(await verifySignature(
'Sign this message to generate your Semaphore identity.',
signature,
mainAddress,
))
)
throw new UnauthorizedException('Signature invalid');

// if (!verifyIdentity(identity))
// throw new UnauthorizedException('Invalid identity format');

const user = await this.prismaService.user.findUnique({
select: { badges: true, identity: true, opAddress: true },
where: { id: userId },
});

if (!user) throw new InternalServerErrorException("User doesn't exist");

if (user.identity?.valueOf() || user.badges?.valueOf() || user.opAddress)
throw new ForbiddenException('User has already connected');

const badges = await getBadges(snapshotPoints, mainAddress);

try {
await this.prismaService.user.update({
where: {
id: userId,
},
data: {
identity,
badges: badges || {},
opAddress: mainAddress,
},
});
} catch (e: unknown) {
if (
e instanceof Prisma.PrismaClientKnownRequestError &&
e.code === 'P2002'
) {
throw new ConflictException('This eth address is already connected');
}
}

return 'success';
}

@UseGuards(AuthGuard)
@Post('/store-badges')
async storeBadges(
Expand Down Expand Up @@ -63,15 +117,24 @@ export class UsersController {
if (!user.identity?.valueOf)
throw new BadRequestException('You need to insert your identity first');

await this.prismaService.user.update({
where: {
id: userId,
},
data: {
badges: badges || {},
opAddress: mainAddress,
},
});
try {
await this.prismaService.user.update({
where: {
id: userId,
},
data: {
badges: badges || {},
opAddress: mainAddress,
},
});
} catch (e: unknown) {
if (
e instanceof Prisma.PrismaClientKnownRequestError &&
e.code === 'P2002'
) {
throw new ConflictException('This eth address is already connected');
}
}

return badges;
}
Expand Down

0 comments on commit de2060d

Please sign in to comment.