Skip to content

Commit

Permalink
refactor: generic network name validator
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Nov 18, 2024
1 parent e017e56 commit 668c520
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
11 changes: 4 additions & 7 deletions src/shared/rpc/methods/send-transfer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { SendTransferRequestParams } from '@btckit/types';
import { z } from 'zod';

import { type BitcoinNetworkModes, WalletDefaultNetworkConfigurationIds } from '@leather.io/models';
import { type BitcoinNetworkModes } from '@leather.io/models';
import { uniqueArray } from '@leather.io/utils';

import { FormErrorMessages } from '@shared/error-messages';
Expand All @@ -11,6 +11,7 @@ import {
} from '@shared/forms/address-validators';
import { checkIfDigitsOnly } from '@shared/forms/amount-validators';

import { defaultNetworksSchema } from '../rpc-schemas';
import {
accountSchema,
formatValidationErrors,
Expand All @@ -24,17 +25,13 @@ export const rpcSendTransferParamsSchemaLegacy = z.object({
account: accountSchema.optional(),
address: z.string(),
amount: z.string(),
network: z
.enum(Object.values(WalletDefaultNetworkConfigurationIds) as [string, ...string[]])
.optional(),
network: defaultNetworksSchema.optional(),
});

export const rpcSendTransferParamsSchema = z
.object({
account: accountSchema.optional(),
network: z
.enum(Object.values(WalletDefaultNetworkConfigurationIds) as [string, ...string[]])
.optional(),
network: defaultNetworksSchema.optional(),
recipients: z
.array(
z.object({
Expand Down
6 changes: 2 additions & 4 deletions src/shared/rpc/methods/sign-message.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { z } from 'zod';

import { WalletDefaultNetworkConfigurationIds } from '@leather.io/models';
import type { PaymentTypes } from '@leather.io/rpc';

import { defaultNetworksSchema } from '../rpc-schemas';
import {
accountSchema,
formatValidationErrors,
Expand All @@ -14,9 +14,7 @@ const rpcSignMessageParamsSchema = z.object({
type: z.enum(['bip322']).optional(),
account: accountSchema.optional(),
message: z.string(),
network: z
.enum(Object.values(WalletDefaultNetworkConfigurationIds) as [string, ...string[]])
.optional(),
network: defaultNetworksSchema.optional(),
paymentType: z.enum(['p2tr', 'p2wpkh'] as [PaymentTypes, PaymentTypes]).optional(),
});

Expand Down
5 changes: 2 additions & 3 deletions src/shared/rpc/methods/sign-psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RpcResponse,
} from '@leather.io/rpc';

import { defaultNetworksSchema } from '../rpc-schemas';
import {
accountSchema,
formatValidationErrors,
Expand Down Expand Up @@ -36,9 +37,7 @@ const rpcSignPsbtParamsSchema = z.object({
allowedSighash: z.array(z.any()).optional(),
broadcast: z.boolean().optional(),
hex: z.string(),
network: z
.enum(Object.values(WalletDefaultNetworkConfigurationIds) as [string, ...string[]])
.optional(),
network: defaultNetworksSchema.optional(),
signAtIndex: z
.union([z.number(), z.array(z.number())])
.optional()
Expand Down
12 changes: 12 additions & 0 deletions src/shared/rpc/rpc-schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { z } from 'zod';

import {
type DefaultNetworkConfigurations,
WalletDefaultNetworkConfigurationIds,
} from '@leather.io/models';

type NonEmptyNetworkList = [DefaultNetworkConfigurations, ...DefaultNetworkConfigurations[]];

export const defaultNetworksSchema = z.enum(
Object.values(WalletDefaultNetworkConfigurationIds) as NonEmptyNetworkList
);

0 comments on commit 668c520

Please sign in to comment.