|
| 1 | +/* eslint-disable brace-style */ |
| 2 | +import { Cardano, Serialization } from '@cardano-sdk/core'; |
| 3 | +import { InMemoryWallet, WalletType } from '../types'; |
| 4 | +import { KeyAgent, SignBlobResult, TrezorConfig, errors } from '@cardano-sdk/key-management'; |
| 5 | +import { KeyAgentFactory } from './KeyAgentFactory'; |
| 6 | +import { |
| 7 | + RequestBase, |
| 8 | + RequestContext, |
| 9 | + SignDataProps, |
| 10 | + SignDataRequest, |
| 11 | + SignRequest, |
| 12 | + SignTransactionProps, |
| 13 | + SignerManagerConfirmationApi, |
| 14 | + SignerManagerSignApi, |
| 15 | + TransactionWitnessRequest |
| 16 | +} from './types'; |
| 17 | +import { Subject } from 'rxjs'; |
| 18 | + |
| 19 | +export type HardwareKeyAgentOptions = TrezorConfig; |
| 20 | + |
| 21 | +export type SignerManagerProps = { |
| 22 | + hwOptions: HardwareKeyAgentOptions; |
| 23 | +}; |
| 24 | + |
| 25 | +export type SignerManagerDependencies = { |
| 26 | + keyAgentFactory: KeyAgentFactory; |
| 27 | +}; |
| 28 | + |
| 29 | +export class SignerManager<WalletMetadata extends {}> |
| 30 | + implements SignerManagerConfirmationApi<WalletMetadata>, SignerManagerSignApi<WalletMetadata> |
| 31 | +{ |
| 32 | + readonly transactionWitnessRequest$ = new Subject<TransactionWitnessRequest<WalletMetadata>>(); |
| 33 | + readonly signDataRequest$ = new Subject<SignDataRequest<WalletMetadata>>(); |
| 34 | + readonly #hwOptions: HardwareKeyAgentOptions; |
| 35 | + readonly #keyAgentFactory: KeyAgentFactory; |
| 36 | + |
| 37 | + constructor(props: SignerManagerProps, { keyAgentFactory }: SignerManagerDependencies) { |
| 38 | + this.#hwOptions = props.hwOptions; |
| 39 | + this.#keyAgentFactory = keyAgentFactory; |
| 40 | + } |
| 41 | + |
| 42 | + async signTransaction( |
| 43 | + { tx, signContext, options }: SignTransactionProps, |
| 44 | + requestContext: RequestContext<WalletMetadata> |
| 45 | + ): Promise<Cardano.Signatures> { |
| 46 | + const transaction = Serialization.Transaction.fromCbor(tx); |
| 47 | + return this.#signRequest( |
| 48 | + this.transactionWitnessRequest$, |
| 49 | + { |
| 50 | + requestContext, |
| 51 | + signContext, |
| 52 | + transaction, |
| 53 | + walletType: requestContext.wallet.type |
| 54 | + }, |
| 55 | + (keyAgent) => |
| 56 | + keyAgent.signTransaction( |
| 57 | + { |
| 58 | + body: transaction.body().toCore(), |
| 59 | + hash: transaction.getId() |
| 60 | + }, |
| 61 | + signContext, |
| 62 | + options |
| 63 | + ) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + async signData(props: SignDataProps, requestContext: RequestContext<WalletMetadata>): Promise<SignBlobResult> { |
| 68 | + return this.#signRequest( |
| 69 | + this.signDataRequest$, |
| 70 | + { |
| 71 | + ...props, |
| 72 | + requestContext, |
| 73 | + walletType: requestContext.wallet.type |
| 74 | + }, |
| 75 | + (keyAgent) => keyAgent.signBlob(props.derivationPath, props.blob) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + #signRequest<R, Req extends RequestBase<WalletMetadata> & SignRequest<R>>( |
| 80 | + emitter$: Subject<Req>, |
| 81 | + request: Omit<Req, 'reject' | 'sign'>, |
| 82 | + sign: (keyAgent: KeyAgent) => Promise<R> |
| 83 | + ) { |
| 84 | + return new Promise<R>((resolve, reject) => { |
| 85 | + if (!emitter$.observed) { |
| 86 | + return reject(new errors.AuthenticationError('Internal error: signDataRequest$ not observed')); |
| 87 | + } |
| 88 | + const account = request.requestContext.wallet.accounts.find( |
| 89 | + ({ accountIndex }) => accountIndex === request.requestContext.accountIndex |
| 90 | + ); |
| 91 | + if (!account) { |
| 92 | + return reject(new errors.ProofGenerationError(`Account not found: ${request.requestContext.accountIndex}`)); |
| 93 | + } |
| 94 | + const bubbleResolveReject = async (action: () => Promise<R>): Promise<R> => { |
| 95 | + try { |
| 96 | + const result = action(); |
| 97 | + resolve(result); |
| 98 | + return result; |
| 99 | + } catch (error) { |
| 100 | + reject(error); |
| 101 | + throw error; |
| 102 | + } |
| 103 | + }; |
| 104 | + const commonRequestProps = { |
| 105 | + ...request, |
| 106 | + reject: async (reason: string) => reject(new errors.AuthenticationError(reason)) |
| 107 | + }; |
| 108 | + emitter$.next( |
| 109 | + request.walletType === WalletType.InMemory |
| 110 | + ? ({ |
| 111 | + ...commonRequestProps, |
| 112 | + sign: async (passphrase: Uint8Array) => |
| 113 | + bubbleResolveReject(() => { |
| 114 | + const wallet = request.requestContext.wallet as InMemoryWallet<WalletMetadata>; |
| 115 | + return sign( |
| 116 | + this.#keyAgentFactory.InMemory({ |
| 117 | + accountIndex: account.accountIndex, |
| 118 | + chainId: request.requestContext.chainId, |
| 119 | + encryptedRootPrivateKeyBytes: [ |
| 120 | + ...Buffer.from(wallet.encryptedSecrets.rootPrivateKeyBytes, 'hex') |
| 121 | + ], |
| 122 | + extendedAccountPublicKey: wallet.extendedAccountPublicKey, |
| 123 | + // TODO: this might be in memory for longer than needed |
| 124 | + getPassphrase: async () => passphrase |
| 125 | + }) |
| 126 | + ); |
| 127 | + }), |
| 128 | + walletType: request.walletType |
| 129 | + } as Req) |
| 130 | + : ({ |
| 131 | + ...commonRequestProps, |
| 132 | + sign: async (): Promise<R> => |
| 133 | + bubbleResolveReject(async () => |
| 134 | + sign( |
| 135 | + request.walletType === WalletType.Ledger |
| 136 | + ? this.#keyAgentFactory.Ledger({ |
| 137 | + accountIndex: request.requestContext.accountIndex, |
| 138 | + chainId: request.requestContext.chainId, |
| 139 | + communicationType: this.#hwOptions.communicationType, |
| 140 | + extendedAccountPublicKey: request.requestContext.wallet.extendedAccountPublicKey |
| 141 | + }) |
| 142 | + : this.#keyAgentFactory.Trezor({ |
| 143 | + accountIndex: request.requestContext.accountIndex, |
| 144 | + chainId: request.requestContext.chainId, |
| 145 | + extendedAccountPublicKey: request.requestContext.wallet.extendedAccountPublicKey, |
| 146 | + trezorConfig: this.#hwOptions |
| 147 | + }) |
| 148 | + ) |
| 149 | + ), |
| 150 | + walletType: request.walletType |
| 151 | + } as Req) |
| 152 | + ); |
| 153 | + }); |
| 154 | + } |
| 155 | +} |
0 commit comments