All files / test util.ts

95.45% Statements 42/44
33.33% Branches 1/3
81.81% Functions 9/11
97.05% Lines 33/34

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 104 105 106 107 108 109 110 11111x 11x 11x 11x   11x 11x 11x   11x 11x 11x 11x 11x   11x         109x             11x 109x 218x         21x           11x               11x                 11x       3x 3x       3x     11x 52x       52x                   11x                 59x 59x 59x   59x                     59x    
import * as Crypto from '@cardano-sdk/crypto';
import { Cardano, Serialization } from '@cardano-sdk/core';
import { GroupedAddress, InMemoryKeyAgent, WitnessedTx, util } from '@cardano-sdk/key-management';
import { Observable, catchError, filter, firstValueFrom, throwError, timeout } from 'rxjs';
import { ObservableWallet, OutgoingTx, WalletUtil } from '../src';
import { SodiumBip32Ed25519 } from '@cardano-sdk/crypto';
import { logger } from '@cardano-sdk/util-dev';
import { testAsyncKeyAgent } from '../../key-management/test/mocks';
 
const SECOND = 1000;
const MINUTE = 60 * SECOND;
export const TX_TIMEOUT = 7 * MINUTE;
const SYNC_TIMEOUT = 3 * MINUTE;
export const FAST_OPERATION_TIMEOUT = 15 * SECOND;
 
export const firstValueFromTimed = <T>(
  observable$: Observable<T>,
  timeoutMessage = 'Timed out',
  timeoutAfter = FAST_OPERATION_TIMEOUT
) =>
  firstValueFrom(
    observable$.pipe(
      timeout(timeoutAfter),
      catchError(() => throwError(() => new Error(timeoutMessage)))
    )
  );
 
export const waitForWalletStateSettle = (wallet: ObservableWallet) =>
  firstValueFromTimed(
    wallet.syncStatus.isSettled$.pipe(filter((isSettled) => isSettled)),
    'Took too long to load',
    SYNC_TIMEOUT
  );
 
export const toOutgoingTx = (tx: Cardano.Tx): OutgoingTx => ({
  body: { ...tx.body },
  cbor: Serialization.TxCBOR.serialize(tx),
  id: tx.id
});
 
export const toSignedTx = (tx: Cardano.Tx): WitnessedTx => ({
  cbor: Serialization.TxCBOR.serialize(tx),
  context: {
    handleResolutions: []
  },
  tx
});
 
export const dummyCbor = Serialization.TxCBOR('123');
 
/**
 * Construct a type 6 or 7 address for a DRepKey using a hash of a public DRep Key.
 *
 * @param dRepKey The public DRep key to hash and use in the address
 * @param type The type of credential to use in the address.
 * @returns A a type 6 address for keyHash credential type, or a type 7 address for script credential type.
 */
export const buildDRepAddressFromDRepKey = async (
  dRepKey: Crypto.Ed25519PublicKeyHex,
  type: Cardano.CredentialType = Cardano.CredentialType.KeyHash
) => {
  const drepKeyHash = Crypto.Ed25519PublicKey.fromHex(dRepKey).hash().hex();
  const drepId = Cardano.DRepID.cip129FromCredential({
    hash: Crypto.Hash28ByteBase16.fromEd25519KeyHashHex(drepKeyHash),
    type
  });
  return Cardano.DRepID.toAddress(drepId);
};
 
export const createAsyncKeyAgent = async () =>
  util.createAsyncKeyAgent(
    await InMemoryKeyAgent.fromBip39MnemonicWords(
      {
        chainId: Cardano.ChainIds.Preview,
        getPassphrase: async () => Buffer.from([]),
        mnemonicWords: util.generateMnemonicWords()
      },
      {
        bip32Ed25519: await SodiumBip32Ed25519.create(),
        logger
      }
    )
  );
 
export const signTx = async ({
  tx,
  addresses$,
  walletUtil
}: {
  tx: Cardano.TxBodyWithHash;
  addresses$: Observable<GroupedAddress[]>;
  walletUtil: WalletUtil;
}): Promise<Cardano.Tx> => {
  const keyAgent = await testAsyncKeyAgent();
  const knownAddresses = await firstValueFrom(addresses$);
  const witnesser = util.createBip32Ed25519Witnesser(keyAgent);
 
  const signed = await witnesser.witness(
    new Serialization.Transaction(
      Serialization.TransactionBody.fromCore(tx.body),
      Serialization.TransactionWitnessSet.fromCore({ signatures: new Map() })
    ),
    {
      knownAddresses,
      txInKeyPathMap: await util.createTxInKeyPathMap(tx.body, knownAddresses, walletUtil)
    }
  );
 
  return signed.tx;
};