All files / src/util formatters.js

100% Statements 20/20
100% Branches 8/8
100% Functions 3/3
100% Lines 20/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                    14x   40x   39x   39x     14x 40x 40x 1x   40x 40x 10x 10x   40x 2x   37x   3x   37x             14x 9x 9x    
/**
 * @file formatters.js
 * @author Marek Kotewicz <marek@ethdev.com>
 * @author Fabian Vogelsteller <fabian@ethdev.com>
 * @date 2015
 */
import descriptor from '@aelfqueen/protobufjs/ext/descriptor';
import bs58 from 'bs58';
import { base58 } from './utils';
 
const getByteCountByAddress = base58Str => {
  // convert a Base58 string to a binary array and get its byte count
  const buffer = bs58.decode(base58Str);
  // get byte
  const byteCount = buffer.length;
  // last four digits are the checksum
  return byteCount;
};
 
export const inputAddressFormatter = address => {
  let realAddress = address;
  if (address && address.value) {
    realAddress = address.value;
  }
  try {
    if (realAddress.indexOf('_') > 0) {
      const parts = realAddress.split('_');
      realAddress = parts?.[1];
    }
    if (getByteCountByAddress(realAddress) !== 36) {
      throw new Error('Invalid address');
    }
    base58.decode(realAddress, 'hex');
  } catch (e) {
    throw new Error('Invalid address');
  }
  return realAddress;
};
 
/**
 * @param {String} result base64 representation of serialized FileDescriptorSet
 * @returns {FileDescriptorSet} decoded FileDescriptorSet message
 */
export const outputFileDescriptorSetFormatter = result => {
  const buffer = Buffer.from(result, 'base64');
  return descriptor.FileDescriptorSet.decode(buffer);
};