All files / test util.ts

95.65% Statements 44/46
50% Branches 2/4
81.81% Functions 9/11
97.22% Lines 35/36

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 111 11212x 12x 12x 12x 12x   12x 12x 12x   12x 12x 12x 12x 12x   12x         108x             12x 108x 216x         21x           12x               12x     12x         21x 21x 21x               21x     12x 52x       52x                   12x                 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 { HexBlob } from '@cardano-sdk/util';
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 address for a DRepKey using an appropriate Network Tag and a hash of a public DRep Key. */
export const buildDRepIDFromDRepKey = (
  dRepKey: Crypto.Ed25519PublicKeyHex,
  networkId: Cardano.NetworkId = Cardano.NetworkId.Testnet,
  addressType: Cardano.AddressType = Cardano.AddressType.EnterpriseKey
) => {
  const dRepKeyBytes = Buffer.from(dRepKey, 'hex');
  const dRepIdHex = Crypto.blake2b(28).update(dRepKeyBytes).digest('hex');
  const paymentAddress = Cardano.EnterpriseAddress.packParts({
    networkId,
    paymentPart: {
      hash: Crypto.Hash28ByteBase16(dRepIdHex),
      type: Cardano.CredentialType.KeyHash
    },
    type: addressType
  });
  return HexBlob.toTypedBech32<Cardano.DRepID>('drep', HexBlob.fromBytes(paymentAddress));
};
 
export const createAsyncKeyAgent = async () =>
  util.createAsyncKeyAgent(
    await InMemoryKeyAgent.fromBip39MnemonicWords(
      {
        chainId: Cardano.ChainIds.Preview,
        getPassphrase: async () => Buffer.from([]),
        mnemonicWords: util.generateMnemonicWords()
      },
      {
        bip32Ed25519: new SodiumBip32Ed25519(),
        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;
};