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