From dc36ba46384e13bc77e9ecf50ac20c2863dd7ca1 Mon Sep 17 00:00:00 2001 From: dragosp1011 <109967337+dragosp1011@users.noreply.github.com> Date: Wed, 6 Mar 2024 12:58:54 +0200 Subject: [PATCH] feat: make key nickname required (#1198) --- .../20240306113102_default_keys_nickname.js | 23 +++++++++++++++++++ packages/wallet/backend/src/user/service.ts | 1 + .../src/walletAddressKeys/controller.ts | 8 +------ .../backend/src/walletAddressKeys/model.ts | 2 +- .../backend/src/walletAddressKeys/service.ts | 4 ++-- .../src/walletAddressKeys/validation.ts | 20 +++++++--------- 6 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 packages/wallet/backend/migrations/20240306113102_default_keys_nickname.js diff --git a/packages/wallet/backend/migrations/20240306113102_default_keys_nickname.js b/packages/wallet/backend/migrations/20240306113102_default_keys_nickname.js new file mode 100644 index 000000000..531e4560c --- /dev/null +++ b/packages/wallet/backend/migrations/20240306113102_default_keys_nickname.js @@ -0,0 +1,23 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = async function (knex) { + return knex.schema.alterTable('walletAddressKeys', async (table) => { + await knex('walletAddressKeys') + .update({ nickname: 'Testnet managed' }) + .whereNull('nickname') + + table.string('nickname').notNullable().alter() + }) +} + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = async function (knex) { + return knex.schema.alterTable('walletAddressKeys', async (table) => { + table.string('nickname').nullable().alter() + }) +} diff --git a/packages/wallet/backend/src/user/service.ts b/packages/wallet/backend/src/user/service.ts index 71d2c8eb4..2ada46053 100644 --- a/packages/wallet/backend/src/user/service.ts +++ b/packages/wallet/backend/src/user/service.ts @@ -165,6 +165,7 @@ export class UserService implements IUserService { userId: boutiqueInfo.createdUser.id, accountId: boutiqueInfo.defaultAccount.id, walletAddressId: boutiqueWallet.id, + nickname: 'Testnet managed', keyPair: { publicKeyPEM: this.env.DEFAULT_BOUTIQUE_KEYS.public_key, privateKeyPEM: this.env.DEFAULT_BOUTIQUE_KEYS.private_key, diff --git a/packages/wallet/backend/src/walletAddressKeys/controller.ts b/packages/wallet/backend/src/walletAddressKeys/controller.ts index b3bc4269d..12bb93ec9 100644 --- a/packages/wallet/backend/src/walletAddressKeys/controller.ts +++ b/packages/wallet/backend/src/walletAddressKeys/controller.ts @@ -16,12 +16,6 @@ interface IWalletAddressKeyController { list: ControllerFunction } -interface KeyPair { - publicKey: string - privateKey: string - keyId: string -} - export class WalletAddressKeyController implements IWalletAddressKeyController { constructor(private walletAddressKeyService: WalletAddressKeyService) {} @@ -75,7 +69,7 @@ export class WalletAddressKeyController implements IWalletAddressKeyController { registerKey = async ( req: Request, - res: CustomResponse, + res: CustomResponse, next: NextFunction ) => { try { diff --git a/packages/wallet/backend/src/walletAddressKeys/model.ts b/packages/wallet/backend/src/walletAddressKeys/model.ts index e4a33766f..0424da592 100644 --- a/packages/wallet/backend/src/walletAddressKeys/model.ts +++ b/packages/wallet/backend/src/walletAddressKeys/model.ts @@ -5,7 +5,7 @@ import { WalletAddress } from '@/walletAddress/model' export class WalletAddressKeys extends BaseModel { static tableName = 'walletAddressKeys' - nickname?: string + nickname!: string readonly walletAddressId!: string readonly rafikiId!: string readonly publicKey!: string diff --git a/packages/wallet/backend/src/walletAddressKeys/service.ts b/packages/wallet/backend/src/walletAddressKeys/service.ts index 8995d6a41..6ff1a49bd 100644 --- a/packages/wallet/backend/src/walletAddressKeys/service.ts +++ b/packages/wallet/backend/src/walletAddressKeys/service.ts @@ -11,7 +11,7 @@ export type KeyResponse = { privateKey: string publicKey: string keyId: string - nickname?: string + nickname: string } interface WalletAddressKeyArgs { @@ -21,7 +21,7 @@ interface WalletAddressKeyArgs { } interface RegisterKeyArgs extends WalletAddressKeyArgs { - nickname?: string + nickname: string keyPair?: { publicKeyPEM: string privateKeyPEM: string diff --git a/packages/wallet/backend/src/walletAddressKeys/validation.ts b/packages/wallet/backend/src/walletAddressKeys/validation.ts index e4fc61d8e..19b85e5df 100644 --- a/packages/wallet/backend/src/walletAddressKeys/validation.ts +++ b/packages/wallet/backend/src/walletAddressKeys/validation.ts @@ -2,12 +2,10 @@ import { z } from 'zod' export const generateWalletAddressKey = z.object({ body: z.object({ - nickname: z.optional( - z - .string() - .trim() - .min(3, { message: 'Name must be at least 3 characters long' }) - ) + nickname: z + .string() + .trim() + .min(3, { message: 'Name must be at least 3 characters long' }) }) }) export const patchWalletAddressKey = z.object({ @@ -21,12 +19,10 @@ export const patchWalletAddressKey = z.object({ export const uploadWalletAddressKey = z.object({ body: z.object({ - nickname: z.optional( - z - .string() - .trim() - .min(3, { message: 'Name must be at least 3 characters long' }) - ), + nickname: z + .string() + .trim() + .min(3, { message: 'Name must be at least 3 characters long' }), base64Key: z.string() }) })