All files / src/Metadata mappers.ts

100% Statements 14/14
100% Branches 5/5
100% Functions 2/2
100% Lines 12/12

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 2238x 38x     38x 30x 30x   30x 28x 27x   26x 26x   26x       28x    
import { Cardano, Serialization } from '@cardano-sdk/core';
import { HexBlob } from '@cardano-sdk/util';
import { TxMetadataModel } from './types';
 
export const mapTxMetadata = (metadataModel: Pick<TxMetadataModel, 'bytes' | 'key'>[]): Cardano.TxMetadata =>
  metadataModel.reduce((map, metadatum) => {
    const { bytes, key } = metadatum;
 
    if (bytes && key) {
      const biKey = BigInt(key);
      const metadata = Serialization.GeneralTransactionMetadata.fromCbor(HexBlob.fromBytes(bytes)).toCore();
 
      if (metadata) {
        const datum = metadata.get(biKey);
 
        if (datum) map.set(biKey, datum);
      }
    }
 
    return map;
  }, new Map<bigint, Cardano.Metadatum>());