All files / src/utils bignumber.ts

93.94% Statements 31/33
65.38% Branches 17/26
100% Functions 5/5
93.55% Lines 29/31
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 821x                                   3080x   1540x   1540x   1540x         1540x 1540x 12320x 12320x 12320x         12320x 12320x       1540x             2400x 2400x   2400x 2400x       2400x   2400x 2400x     2400x 189x     2400x   7200x   2400x 2400x 19200x 19200x       2400x    
import BigNumber from 'bignumber.js';
 
export interface IToFromBufferOpts {
  /**
   * Size of the buffer
   */
  size?: 'auto' | number;
  /**
   * Encoding type.
   */
  endian?: 'big' | 'little';
}
 
/**
 * Calculates BigNumber from buffer representation
 * @param {Buffer} buf
 * @param {ToFromBufferOpts} opts
 */
export const bigNumberFromBuffer = (buf: Buffer, Eopts: IToFromBufferOpts = {}) => {
 
  const endian = opts.endian || 'big';
 
  const size = opts.size === 'auto' ? Math.ceil(buf.length) : (opts.size || 1);
 
  Iif (buf.length % size !== 0) {
    throw new RangeError('Buffer length (' + buf.length + ')'
      + ' must be a multiple of size (' + size + ')');
  }
 
  const hex = [];
  for (let i = 0; i < buf.length; i += size) {
    const chunk = [];
    for (let j = 0; j < size; j++) {
      chunk.push(buf[
      i + (endian === 'big' ? j : (size - j - 1))
        ]);
    }
 
    hex.push(chunk
      .map((c) => `${(c < 16 ? '0' : '')}${c.toString(16)}`)
      .join(''));
  }
 
  return new BigNumber(hex.join(''), 16);
};
 
/**
 * Exports bignumber to buffer.
 * @returns {Buffer}
 */
export const bigNumberToBuffer = (bignum: BigNumber, Iopts: IToFromBufferOpts = {}) => {
  const endian = opts.endian || 'big';
 
  let hex = bignum.toString(16);
  Iif (hex.charAt(0) === '-') {
    throw new Error('Converting negative numbers to Buffers not supported yet');
  }
 
  const size = opts.size === 'auto' ? Math.ceil(hex.length / 2) : (opts.size || 1);
 
  const len = Math.ceil(hex.length / (2 * size)) * size;
  const buf = new Buffer(len);
 
  // Zero-pad the hex string so the chunks are all `size` long
  while (hex.length < 2 * len) {
    hex = '0' + hex;
  }
 
  const hx = hex
    .split(new RegExp('(.{' + (2 * size) + '})'))
    .filter((s) => s.length > 0);
 
  hx.forEach((chunk, i) => {
    for (let j = 0; j < size; j++) {
      const ix  = i * size + (endian === 'big' ? j : size - j - 1);
      buf[ix] = parseInt(chunk.slice(j * 2, j * 2 + 2), 16);
    }
  });
 
  return buf;
};