All files / src/services/KeyAgent restoreKeyAgent.ts

95.23% Statements 20/21
83.33% Branches 10/12
100% Functions 4/4
95% Lines 19/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103    42x 42x                       42x   42x                 42x         1x 1x           1x 1x                                                         42x           6x 6x   3x         3x 1x       2x     1x     1x     1x            
/* eslint-disable func-style */
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { Cardano } from '@cardano-sdk/core';
import {
  GetPassphrase,
  InMemoryKeyAgent,
  KeyAgent,
  KeyAgentDependencies,
  KeyAgentType,
  SerializableInMemoryKeyAgentData,
  SerializableKeyAgentData,
  SerializableLedgerKeyAgentData,
  SerializableTrezorKeyAgentData,
  errors
} from '@cardano-sdk/key-management';
import { LedgerKeyAgent } from '@cardano-sdk/hardware-ledger';
import { Logger } from 'ts-log';
import { TrezorKeyAgent } from '@cardano-sdk/hardware-trezor';
 
// TODO: use this type as 2nd parameter of restoreKeyAgent
export interface RestoreInMemoryKeyAgentProps {
  /** Required for InMemoryKeyAgent */
  getPassphrase?: GetPassphrase;
}
 
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const migrateSerializableData = <T extends SerializableKeyAgentData>(data: any, logger: Logger): T => ({
  ...data,
  chainId:
    data.chainId ||
    (() => {
      logger.info('Migrating serializable data due to missing "chainId"');
      return {
        ...data,
        chainId:
          data.networkId === Cardano.NetworkId.Mainnet
            ? Cardano.ChainIds.Mainnet
            : (() => {
                logger.warn('Assuming "Preprod" network is used');
                return Cardano.ChainIds.Preprod;
              })()
      };
    })()
});
 
export function restoreKeyAgent(
  data: SerializableInMemoryKeyAgentData,
  dependencies: KeyAgentDependencies,
  getPassphrase: GetPassphrase
): Promise<KeyAgent>;
export function restoreKeyAgent(
  data: SerializableKeyAgentData,
  dependencies: KeyAgentDependencies,
  getPassphrase?: GetPassphrase
): Promise<KeyAgent>;
export function restoreKeyAgent(
  data: SerializableLedgerKeyAgentData,
  dependencies: KeyAgentDependencies
): Promise<KeyAgent>;
export function restoreKeyAgent(
  data: SerializableTrezorKeyAgentData,
  dependencies: KeyAgentDependencies
): Promise<KeyAgent>;
/**
 * Restore key agent from serializable data
 *
 * @throws InvalidSerializableDataError, AuthenticationError
 */
export async function restoreKeyAgent<T extends SerializableKeyAgentData>(
  dataArg: T,
  dependencies: KeyAgentDependencies,
  getPassphrase?: GetPassphrase
): Promise<KeyAgent> {
  // migrateSerializableData
  const data = migrateSerializableData(dataArg, dependencies.logger);
  switch (data.__typename) {
    case KeyAgentType.InMemory: {
      Iif (!data.encryptedRootPrivateKeyBytes || data.encryptedRootPrivateKeyBytes.length !== 156) {
        throw new errors.InvalidSerializableDataError(
          'Expected encrypted root private key in "agentData" for InMemoryKeyAgent"'
        );
      }
      if (!getPassphrase) {
        throw new errors.InvalidSerializableDataError(
          'Expected "getPassphrase" in RestoreKeyAgentProps for InMemoryKeyAgent"'
        );
      }
      return new InMemoryKeyAgent({ ...data, getPassphrase }, dependencies);
    }
    case KeyAgentType.Ledger: {
      return new LedgerKeyAgent(data, dependencies);
    }
    case KeyAgentType.Trezor: {
      return new TrezorKeyAgent(data, dependencies);
    }
    default:
      throw new errors.InvalidSerializableDataError(
        // @ts-ignore
        `Restoring key agent of __typename '${data.__typename}' is not implemented`
      );
  }
}