|
| 1 | +import { |
| 2 | + DeviceModelId, |
| 3 | + LoggerPublisherService, |
| 4 | +} from "@ledgerhq/device-management-kit"; |
| 5 | +import { inject, injectable } from "inversify"; |
| 6 | +import { Codec, exactly, number, oneOf, string } from "purify-ts"; |
| 7 | + |
| 8 | +import { configTypes } from "@/config/di/configTypes"; |
| 9 | +import { pkiTypes } from "@/modules/multichain/pki/di/pkiTypes"; |
| 10 | +import { type PkiCertificateLoader } from "@/modules/multichain/pki/domain/PkiCertificateLoader"; |
| 11 | +import { KeyUsage } from "@/modules/multichain/pki/model/KeyUsage"; |
| 12 | +import { type TransactionCheckDataSource } from "@/modules/multichain/transaction-check/data/TransactionCheckDataSource"; |
| 13 | +import { transactionCheckTypes } from "@/modules/multichain/transaction-check/di/transactionCheckTypes"; |
| 14 | +import { type TransactionCheckLoader } from "@/modules/multichain/transaction-check/loaders/TransactionCheckLoader"; |
| 15 | +import { TransactionCheckPaths } from "@/modules/multichain/transaction-check/utils/constants"; |
| 16 | +import { |
| 17 | + ClearSignContext, |
| 18 | + ClearSignContextType, |
| 19 | +} from "@/shared/model/ClearSignContext"; |
| 20 | + |
| 21 | +export type SolanaTransactionCheckRequest = { |
| 22 | + from: string; |
| 23 | + rawTx: string; |
| 24 | + chain: number; |
| 25 | +}; |
| 26 | + |
| 27 | +export type SolanaTransactionCheckContextInput = { |
| 28 | + deviceModelId: DeviceModelId; |
| 29 | + transactionCheck: SolanaTransactionCheckRequest; |
| 30 | +}; |
| 31 | + |
| 32 | +const SUPPORTED_TYPES: ClearSignContextType[] = [ |
| 33 | + ClearSignContextType.SOLANA_TRANSACTION_CHECK, |
| 34 | +]; |
| 35 | + |
| 36 | +const solanaTransactionCheckInputCodec = Codec.interface({ |
| 37 | + deviceModelId: oneOf([ |
| 38 | + exactly(DeviceModelId.NANO_X), |
| 39 | + exactly(DeviceModelId.NANO_SP), |
| 40 | + exactly(DeviceModelId.STAX), |
| 41 | + exactly(DeviceModelId.FLEX), |
| 42 | + ]), |
| 43 | + transactionCheck: Codec.interface({ |
| 44 | + from: string, |
| 45 | + rawTx: string, |
| 46 | + chain: number, |
| 47 | + }), |
| 48 | +}); |
| 49 | + |
| 50 | +@injectable() |
| 51 | +export class SolanaTransactionCheckLoader |
| 52 | + implements TransactionCheckLoader<SolanaTransactionCheckContextInput> |
| 53 | +{ |
| 54 | + private readonly logger: LoggerPublisherService; |
| 55 | + |
| 56 | + constructor( |
| 57 | + @inject(transactionCheckTypes.TransactionCheckDataSource) |
| 58 | + private readonly transactionCheckDataSource: TransactionCheckDataSource, |
| 59 | + @inject(pkiTypes.PkiCertificateLoader) |
| 60 | + private readonly certificateLoader: PkiCertificateLoader, |
| 61 | + @inject(configTypes.ContextModuleLoggerFactory) |
| 62 | + loggerFactory: (tag: string) => LoggerPublisherService, |
| 63 | + ) { |
| 64 | + this.logger = loggerFactory("SolanaTransactionCheckLoader"); |
| 65 | + } |
| 66 | + |
| 67 | + canHandle( |
| 68 | + input: unknown, |
| 69 | + expectedType: ClearSignContextType[], |
| 70 | + ): input is SolanaTransactionCheckContextInput { |
| 71 | + if (!SUPPORTED_TYPES.every((type) => expectedType.includes(type))) |
| 72 | + return false; |
| 73 | + return solanaTransactionCheckInputCodec.decode(input).caseOf({ |
| 74 | + Left: () => false, |
| 75 | + Right: ({ transactionCheck: { from, rawTx } }) => |
| 76 | + from.length > 0 && rawTx.length > 0, |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + async load( |
| 81 | + ctx: SolanaTransactionCheckContextInput, |
| 82 | + ): Promise<ClearSignContext[]> { |
| 83 | + const { from, rawTx, chain } = ctx.transactionCheck; |
| 84 | + |
| 85 | + const txCheck = await this.transactionCheckDataSource.check({ |
| 86 | + path: TransactionCheckPaths.SOLANA_TRANSACTION, |
| 87 | + body: { tx: { from, raw: rawTx }, chain }, |
| 88 | + }); |
| 89 | + |
| 90 | + const context = await txCheck.caseOf<Promise<ClearSignContext>>({ |
| 91 | + Left: (error) => |
| 92 | + Promise.resolve({ |
| 93 | + type: ClearSignContextType.ERROR, |
| 94 | + error, |
| 95 | + }), |
| 96 | + Right: async (data) => { |
| 97 | + const certificate = await this.certificateLoader.loadCertificate({ |
| 98 | + keyId: data.publicKeyId, |
| 99 | + keyUsage: KeyUsage.TxSimulationSigner, |
| 100 | + targetDevice: ctx.deviceModelId, |
| 101 | + }); |
| 102 | + |
| 103 | + return { |
| 104 | + type: ClearSignContextType.SOLANA_TRANSACTION_CHECK, |
| 105 | + payload: { descriptor: data.descriptor }, |
| 106 | + certificate, |
| 107 | + }; |
| 108 | + }, |
| 109 | + }); |
| 110 | + |
| 111 | + const result = [context]; |
| 112 | + this.logger.debug("load result", { data: { result } }); |
| 113 | + return result; |
| 114 | + } |
| 115 | +} |
0 commit comments