Skip to content

Commit

Permalink
feat: added a user/signature query for internal use
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNilesh committed Jun 18, 2024
1 parent f130205 commit 91b2c12
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';

export class UserSignatureQueryDto {
@IsString()
@IsNotEmpty()
@ApiProperty({
description: 'The private key of the user',
})
privateKey: string;

@IsString()
@IsNotEmpty()
@ApiProperty({
description: 'Typed data',
})
typedData: string;
}
22 changes: 21 additions & 1 deletion packages/agents/nova/src/core/resources/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ import { UserBalanceQueryDto } from './dto/user-balance-query.dto';
import { UserBalanceResponseDto } from './dto/user-balance-response.dto';
import { UserDataQueryDto } from './dto/user-data-query.dto';
import { UserInitBodyDto } from './dto/user-init-body.dto';
import { UserInitResponseDto, UserResponseDto } from './dto/user-init-response.dto';
import {
UserInitResponseDto,
UserResponseDto,
} from './dto/user-init-response.dto';
import { UserSendTxBodyDto } from './dto/user-send-tx-body.dto';
import { UserService } from './user.service';
import { UserSignatureQueryDto } from './dto/user-signature-query.dto';

@ApiTags('user')
@Controller('user')
Expand Down Expand Up @@ -88,4 +92,20 @@ export class UserController {
async sendTx(@Body() body: UserSendTxBodyDto): Promise<void> {
return await this.userService.sendTx(body);
}

/**
* Retrieves user sign tx
* @param {UserSignatureQueryDto} query - query params containing user wallet address and the way returned data should be arranged
* @returns userTokenBalance array
*/
@HttpCode(HttpStatus.OK)
@ApiResponse({
status: HttpStatus.OK,
description: 'Get Signature',
type: UserSignatureQueryDto,
})
@Get('signature')
getSignature(@Query() query: UserSignatureQueryDto): Promise<string> {
return this.userService.getSignature(query);
}
}
22 changes: 20 additions & 2 deletions packages/agents/nova/src/core/resources/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ import {
signSafeTransaction,
isSCWDeployed,
} from '@qw/utils';
import {JsonRpcProvider, parseUnits} from 'ethers';
import { JsonRpcProvider, parseUnits } from 'ethers';
import { ConfigService } from 'src/config/config.service';
import { NovaConfig } from 'src/config/schema';
import { UserBalanceQueryDto } from './dto/user-balance-query.dto';
import { UserDataQueryDto } from './dto/user-data-query.dto';
import { UserInitBodyDto } from './dto/user-init-body.dto';
import { UserResponseDto } from './dto/user-init-response.dto';
import { UserSendTxBodyDto } from './dto/user-send-tx-body.dto';
import { ethers } from 'ethers';
import { UserSignatureQueryDto } from './dto/user-signature-query.dto';

@Injectable()
export class UserService {
Expand Down Expand Up @@ -65,12 +67,28 @@ export class UserService {
return resp.data;
}

async getSignature(query: UserSignatureQueryDto): Promise<string> {
const wallet = new ethers.Wallet(query.privateKey);

const typedData = JSON.parse(query.typedData);

const signature = await wallet.signTypedData(
JSON.parse(typedData).domain,
{ SafeTx: JSON.parse(typedData).types.SafeTx },
JSON.parse(typedData).message,
);
return signature;
}

/**
* This service is called by /user/init endpoint
* It deploys the smart contract and initializes the user
* @returns transaction
*/
async userInit({ signerAddress, provider }: UserInitBodyDto): Promise<UserResponseDto> {
async userInit({
signerAddress,
provider,
}: UserInitBodyDto): Promise<UserResponseDto> {
const rpcUrl = Object.values(this.config.chains)[0].providers[0]; // RPC URL

const gelatoApiKey = this.config.gelatoApiKey;
Expand Down

0 comments on commit 91b2c12

Please sign in to comment.