import { type Container } from "inversify"; import { Left } from "purify-ts"; import { calldataTypes } from "@/calldata/di/calldataTypes"; import { dynamicNetworkTypes } from "@/dynamic-network/di/dynamicNetworkTypes"; import { type BlindSigningReportParams } from "@/reporter/data/BlindSigningReporterDatasource"; import { reporterTypes } from "@/reporter/di/reporterTypes"; import { type BlindSigningReporter } from "@/reporter/domain/BlindSigningReporter"; import type { TypedDataClearSignContext } from "@/shared/model/TypedDataClearSignContext"; import type { TypedDataContext } from "@/shared/model/TypedDataContext"; import { trustedNameTypes } from "@/trusted-name/di/trustedNameTypes"; import { type ContextModuleLoaderConfig, type ContextModuleServiceConfig, } from "./config/model/ContextModuleConfig"; import { externalPluginTypes } from "./external-plugin/di/externalPluginTypes"; import { gatedSigningTypes } from "./gated-signing/di/gatedSigningTypes"; import { nftTypes } from "./nft/di/nftTypes"; import { proxyTypes } from "./proxy/di/proxyTypes"; import { safeTypes } from "./safe/di/safeTypes"; import { type ContextFieldLoader } from "./shared/domain/ContextFieldLoader"; import { type ContextLoader } from "./shared/domain/ContextLoader"; import { type ClearSignContext, ClearSignContextType, } from "./shared/model/ClearSignContext"; import { type SolanaTransactionContext } from "./shared/model/SolanaTransactionContext"; import { solanaContextTypes } from "./solana/di/solanaContextTypes"; import { type SolanaContextLoader } from "./solana/domain/SolanaContextLoader"; import { type SolanaTransactionContextResult } from "./solana/domain/solanaContextTypes"; import { tokenTypes } from "./token/di/tokenTypes"; import { transactionCheckTypes } from "./transaction-check/di/transactionCheckTypes"; import { typedDataTypes } from "./typed-data/di/typedDataTypes"; import type { TypedDataContextLoader } from "./typed-data/domain/TypedDataContextLoader"; import { type ContextModule } from "./ContextModule"; import { makeContainer } from "./di"; export class DefaultContextModule implements ContextModule { private _container: Container; private _loaders: ContextLoader[]; private _typedDataLoader: TypedDataContextLoader; private _solanaLoader: SolanaContextLoader; private _fieldLoaders: ContextFieldLoader[]; private _blindSigningReporter: BlindSigningReporter; constructor(args: ContextModuleServiceConfig & ContextModuleLoaderConfig) { this._container = makeContainer({ config: args }); this._loaders = args.defaultLoaders ? this._getDefaultLoaders() : []; this._loaders.push(...args.customLoaders); this._fieldLoaders = args.defaultFieldLoaders ? this._getDefaultFieldLoaders() : []; this._fieldLoaders.push(...args.customFieldLoaders); this._typedDataLoader = args.customTypedDataLoader ?? this._getDefaultTypedDataLoader(); this._solanaLoader = args.customSolanaLoader ?? this._getSolanaLoader(); this._blindSigningReporter = args.customBlindSigningReporter ?? this._getBlindSigningReporter(); } private _getDefaultFieldLoaders(): ContextFieldLoader[] { return [ this._container.get(nftTypes.NftContextFieldLoader), this._container.get( tokenTypes.TokenContextFieldLoader, ), this._container.get( trustedNameTypes.TrustedNameContextFieldLoader, ), this._container.get( proxyTypes.ProxyContextFieldLoader, ), ]; } private _getDefaultLoaders(): ContextLoader[] { return [ this._container.get( externalPluginTypes.ExternalPluginContextLoader, ), this._container.get( trustedNameTypes.TrustedNameContextLoader, ), this._container.get(nftTypes.NftContextLoader), this._container.get(tokenTypes.TokenContextLoader), this._container.get(calldataTypes.CalldataContextLoader), this._container.get( dynamicNetworkTypes.DynamicNetworkContextLoader, ), this._container.get(safeTypes.SafeAddressLoader), this._container.get( gatedSigningTypes.GatedSigningContextLoader, ), this._container.get( gatedSigningTypes.GatedSigningTypedDataContextLoader, ), this._container.get( transactionCheckTypes.TransactionCheckContextLoader, ), this._container.get( transactionCheckTypes.TypedDataCheckContextLoader, ), ]; } private _getDefaultTypedDataLoader(): TypedDataContextLoader { return this._container.get( typedDataTypes.TypedDataContextLoader, ); } private _getBlindSigningReporter(): BlindSigningReporter { return this._container.get( reporterTypes.BlindSigningReporter, ); } private _getSolanaLoader(): SolanaContextLoader { try { return this._container.get( solanaContextTypes.SolanaContextLoader, ); } catch { return { load: async (_ctx) => Left( new Error( "[ContextModule] - DefaultContextModule: no SolanaContextLoader bound", ), ), }; } } public async getContexts( input: unknown, expectedTypes?: ClearSignContextType[], ): Promise { const allContextTypes = Object.values(ClearSignContextType); const loaders = this._loaders.filter((l) => l.canHandle(input, expectedTypes ?? allContextTypes), ); const promises = loaders.map((fetcher) => fetcher.load(input)); const responses = await Promise.all(promises); return responses.flat(); } public async getFieldContext( field: TInput, expectedType: ClearSignContextType, ): Promise { const loaders = this._fieldLoaders.filter((l) => l.canHandle(field, expectedType), ); if (loaders.length === 0) { return Promise.resolve({ type: ClearSignContextType.ERROR, error: new Error( `Loader not found for field: ${field} and expected type: ${expectedType}`, ), }); } for (const loader of loaders) { const context = await loader.loadField(field); if (context.type !== ClearSignContextType.ERROR) { return context; } } return { type: ClearSignContextType.ERROR, error: new Error( `Loader not found for field: ${field} and expected type: ${expectedType}`, ), }; } public async getTypedDataFilters( typedData: TypedDataContext, ): Promise { return this._typedDataLoader.load(typedData); } public async getSolanaContext( transactionContext: SolanaTransactionContext, ): Promise { return await this._solanaLoader.load(transactionContext); } public async report(params: BlindSigningReportParams): Promise { await this._blindSigningReporter.report(params); } }