|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
1 | 2 | import { privateKeyToAccount } from 'viem/accounts'; |
2 | | -import { SupportedNetworks, type EthAddress, type LivelyDiamondSDKOptions } from './types.js'; |
| 3 | +import { SupportedNetworks } from './types.js'; |
3 | 4 |
|
4 | | -// NOTE: Need to get these working as actual decorators. Mainly having trouble with the constructor decorator for the class. |
5 | | - |
6 | | -export function isValidNetwork(network: SupportedNetworks): boolean { |
7 | | - if (!Object.values(SupportedNetworks).includes(network)) return false; |
8 | | - return true; |
| 5 | +export function isValidNetwork(network?: unknown): boolean { |
| 6 | + return Boolean(typeof network === 'string' && Object.values(SupportedNetworks).includes(network)); |
9 | 7 | } |
10 | 8 |
|
11 | | -export function isValidPrivateKey(privateKey: EthAddress): boolean { |
| 9 | +export function isValidPrivateKey(privateKey?: unknown): boolean { |
| 10 | + if (typeof privateKey !== 'string' || !privateKey.startsWith('0x')) return false; |
12 | 11 | try { |
13 | | - privateKeyToAccount(privateKey); |
| 12 | + privateKeyToAccount(privateKey as `0x${string}`); |
14 | 13 | return true; |
15 | 14 | } catch (error) { |
16 | | - return false; |
17 | 15 | // throw new Error(`Invalid private key: ${(error as Error).message}`); |
| 16 | + return false; |
18 | 17 | } |
19 | 18 | } |
20 | 19 |
|
21 | | -// eslint-disable-next-line @typescript-eslint/ban-types |
22 | | -export function Validate<T extends { new (...args: any[]): {} }>(constructor: T, ...args: any[]) { |
23 | | - console.log({ constructor, args }); |
24 | | - return class extends constructor { |
25 | | - isValidNetwork(network: SupportedNetworks): boolean { |
26 | | - return isValidNetwork(network); |
| 20 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 21 | +export function validate<T extends abstract new (...args: any) => any>( |
| 22 | + field: string, |
| 23 | + validator: (v: unknown) => boolean, |
| 24 | + validateOptions: { optional?: boolean } = { optional: false } |
| 25 | +) { |
| 26 | + return function ( |
| 27 | + target: any, |
| 28 | + context: ClassDecoratorContext<T> | ClassMethodDecoratorContext<T> |
| 29 | + ): T | void { |
| 30 | + if (!context || context.kind === 'class') { |
| 31 | + // Maybe do this instead of wrapping? |
| 32 | + // context.addInitializer(function () { |
| 33 | + // if (!validator(this[field as keyof T])) { |
| 34 | + // throw new Error(`Invalid ${String(field)}`); |
| 35 | + // } |
| 36 | + // }); |
| 37 | + const wrapper = function (...args: any) { |
| 38 | + const opts = args?.[0]; |
| 39 | + if ((validateOptions.optional && !opts?.[field]) || validator(opts?.[field])) { |
| 40 | + return new target(...args); |
| 41 | + } |
| 42 | + throw new Error(`Invalid ${String(field)}`); |
| 43 | + }; |
| 44 | + wrapper.prototype = target.prototype; |
| 45 | + return wrapper as unknown as T; |
| 46 | + } |
| 47 | + if (context.kind === 'function') { |
| 48 | + const wrapper = function (...args: any) { |
| 49 | + const opts = args?.[0]; |
| 50 | + if ((validateOptions.optional && !opts?.[field]) || validator(opts?.[field])) { |
| 51 | + return target(...args); |
| 52 | + } |
| 53 | + throw new Error(`Invalid ${String(field)}`); |
| 54 | + }; |
| 55 | + wrapper.prototype = target.prototype; |
| 56 | + return wrapper as unknown as T; |
27 | 57 | } |
28 | 58 | }; |
29 | 59 | } |
30 | | - |
31 | | -// /** |
32 | | -// * Check if the options passed are valid |
33 | | -// * @param opts All valid configuration options |
34 | | -// * @returns void |
35 | | -// * @throws Error if any of the options are invalid |
36 | | -// */ |
37 | | -// export function isValidOptions(opts?: Partial<LivelyDiamondSDKOptions>): boolean { |
38 | | -// console.log(`Inside isValidOptions: ${opts}`); |
39 | | - |
40 | | -// if (!opts) return true; |
41 | | - |
42 | | -// let isValid: boolean | undefined = undefined; |
43 | | - |
44 | | -// if (opts?.privateKey) isValid ??= isValidPrivateKey(opts.privateKey); |
45 | | -// if (opts?.network) isValid ??= isValidNetwork(opts.network); |
46 | | - |
47 | | -// if (!isValid) isValid ??= false; |
48 | | - |
49 | | -// return isValid; |
50 | | -// } |
|
0 commit comments