|
| 1 | +import { AnyBip32Wallet, InMemoryWallet, WalletType } from '../types'; |
1 | 2 | import { Cardano, Serialization } from '@cardano-sdk/core'; |
2 | 3 | import { Cip30DataSignature } from '@cardano-sdk/dapp-connector'; |
3 | 4 | import { CustomError } from 'ts-custom-error'; |
4 | | -import { InMemoryWallet, WalletType } from '../types'; |
5 | 5 | import { KeyAgent, KeyPurpose, SignDataContext, TrezorConfig, errors } from '@cardano-sdk/key-management'; |
6 | 6 | import { KeyAgentFactory } from './KeyAgentFactory'; |
7 | 7 | import { Logger } from 'ts-log'; |
@@ -83,6 +83,24 @@ export class SigningCoordinator<WalletMetadata extends {}, AccountMetadata exten |
83 | 83 | this.#logger = contextLogger(logger, 'SigningCoordinator'); |
84 | 84 | } |
85 | 85 |
|
| 86 | + /** |
| 87 | + * Gets the appropriate TrezorConfig for the given wallet. |
| 88 | + * |
| 89 | + * This allows wallets to specify only the properties they want to override |
| 90 | + * (e.g., derivationType) while inheriting global settings (e.g., communicationType, manifest) |
| 91 | + */ |
| 92 | + #getTrezorConfig(wallet: AnyBip32Wallet<WalletMetadata, AccountMetadata>): TrezorConfig { |
| 93 | + const trezorConfig = |
| 94 | + wallet.type === WalletType.Trezor && 'trezorConfig' in wallet.metadata |
| 95 | + ? (wallet.metadata as { trezorConfig?: Partial<TrezorConfig> }).trezorConfig |
| 96 | + : undefined; |
| 97 | + |
| 98 | + return { |
| 99 | + ...this.#hwOptions, // Global defaults (communicationType, manifest, etc.) |
| 100 | + ...(trezorConfig || {}) // Wallet-specific overrides (derivationType, etc.) |
| 101 | + }; |
| 102 | + } |
| 103 | + |
86 | 104 | async signTransaction( |
87 | 105 | { tx, signContext, options }: SignTransactionProps, |
88 | 106 | requestContext: RequestContext<WalletMetadata, AccountMetadata> |
@@ -123,6 +141,7 @@ export class SigningCoordinator<WalletMetadata extends {}, AccountMetadata exten |
123 | 141 | request: Omit<Req, 'reject' | 'sign'>, |
124 | 142 | sign: (keyAgent: KeyAgent) => Promise<R> |
125 | 143 | ) { |
| 144 | + /* eslint-disable sonarjs/cognitive-complexity */ |
126 | 145 | return new Promise<R>((resolve, reject) => { |
127 | 146 | if (!emitter$.observed) { |
128 | 147 | return reject(new WrongTargetError('Not expecting sign requests at this time')); |
@@ -181,30 +200,36 @@ export class SigningCoordinator<WalletMetadata extends {}, AccountMetadata exten |
181 | 200 | ...commonRequestProps, |
182 | 201 | sign: async (): Promise<R> => |
183 | 202 | bubbleResolveReject( |
184 | | - async (options?: SignOptions) => |
185 | | - sign( |
186 | | - request.walletType === WalletType.Ledger |
187 | | - ? await this.#keyAgentFactory.Ledger({ |
188 | | - accountIndex: request.requestContext.accountIndex, |
189 | | - chainId: request.requestContext.chainId, |
190 | | - communicationType: this.#hwOptions.communicationType, |
191 | | - extendedAccountPublicKey: account.extendedAccountPublicKey, |
192 | | - purpose: account.purpose || KeyPurpose.STANDARD |
193 | | - }) |
194 | | - : await this.#keyAgentFactory.Trezor({ |
195 | | - accountIndex: request.requestContext.accountIndex, |
196 | | - chainId: request.requestContext.chainId, |
197 | | - extendedAccountPublicKey: account.extendedAccountPublicKey, |
198 | | - purpose: account.purpose || KeyPurpose.STANDARD, |
199 | | - trezorConfig: this.#hwOptions |
200 | | - }) |
201 | | - ).catch((error) => throwMaybeWrappedWithNoRejectError(error, options)), |
| 203 | + async (options?: SignOptions) => { |
| 204 | + try { |
| 205 | + const keyAgent = |
| 206 | + request.walletType === WalletType.Ledger |
| 207 | + ? await this.#keyAgentFactory.Ledger({ |
| 208 | + accountIndex: request.requestContext.accountIndex, |
| 209 | + chainId: request.requestContext.chainId, |
| 210 | + communicationType: this.#hwOptions.communicationType, |
| 211 | + extendedAccountPublicKey: account.extendedAccountPublicKey, |
| 212 | + purpose: account.purpose || KeyPurpose.STANDARD |
| 213 | + }) |
| 214 | + : await this.#keyAgentFactory.Trezor({ |
| 215 | + accountIndex: request.requestContext.accountIndex, |
| 216 | + chainId: request.requestContext.chainId, |
| 217 | + extendedAccountPublicKey: account.extendedAccountPublicKey, |
| 218 | + purpose: account.purpose || KeyPurpose.STANDARD, |
| 219 | + trezorConfig: this.#getTrezorConfig(request.requestContext.wallet) |
| 220 | + }); |
| 221 | + return await sign(keyAgent); |
| 222 | + } catch (error) { |
| 223 | + return throwMaybeWrappedWithNoRejectError(error, options); |
| 224 | + } |
| 225 | + }, |
202 | 226 | resolve, |
203 | 227 | reject |
204 | 228 | ), |
205 | 229 | walletType: request.walletType |
206 | 230 | } as Req) |
207 | 231 | ); |
208 | 232 | }); |
| 233 | + /* eslint-enable sonarjs/cognitive-complexity */ |
209 | 234 | } |
210 | 235 | } |
0 commit comments