Custom Inspect for Node.js UInt Arrays

Custom Inspect for Node.js UInt Arrays

By Nathan

The Node.js util.inspect function is what console.log uses internally to format objects. By default, when inspecting binary data within Uint arrays, we get verbose raw integer values that are harder to identify at a glance.

const data = {
  bytes: Uint8Array.of(123, 45, 67),
};

console.log(data);

The default format for the above inspected Uint8Array will look like the following:

{ bytes: Uint8Array(3) [ 123, 45, 67 ] }

By defining a util.inspect.custom method on any object, we can customize the display format used when the object is inspected. The following example shows how the UInt8Array prototype can be modified to have custom inspect formatting:

import { inspect } from 'node:util';

Uint8Array.prototype[inspect.custom] = function () {
  return `UInt8[0x${Buffer.from(this).toString('hex')}]`;
}

const data = {
  bytes: Uint8Array.of(123, 45, 67),
};

console.log(data);

The console output from the above example will look like the following:

{ bytes: UInt8[0x7b2d43] }

Inspecting Algorand Transactions

Algorand addresses are usually presented as base32 encoding. By using base32 as the default format when inspecting Uint8 arrays we get more compact output and can spot Algorand sender and receiver addresses.

import { inspect } from 'node:util';
import algosdk from 'algosdk';
import base32 from 'hi-base32';

Uint8Array.prototype[inspect.custom] = function () {
  return `UInt8[${base32.encode(this).replaceAll('=','')}]`;
}

// setup algod client, submit transaction
// see https://developer.algorand.org/docs/sdks/javascript/

const txInfo = await algosdk.waitForConfirmation(algodClient, txId, 4);
console.log(txInfo);

Console output for the inspected Algorand transaction will look like the following:

{
  sig: UInt8[3FMU46ANM2ZLSGJBUMXQRE6LUFMRYEU6ICYF4GFWKMJ2LJ5AF4HUXC7FWXNQGQZXI2EVCLXKG5VE3ATQCVSJ6CYWOMYRVCBPQLKY4AI],
  txn: {
    amt: 125,
    fee: 1000,
    fv: 28,
    gen: 'dockernet-v1',
    gh: UInt8[GPV7SJUAYE7VMR6WNT2DQYKBAZE4I4EOCQUICSKJP5HNGZ3562NQ],
    lv: 1028,
    note: UInt8[NBSWY3DPEB3W64TMMQ],
    rcv: UInt8[HNVCPPGOW2SC2YQ5BLGEWBG7BCG3TNRDK3DFPLSP4LINK62Z3IUQ],
    snd: UInt8[XJOVAFHGJ5J5QXVDVDICU3YNONSTEFLXDXREHJR2YBEKDCVYWYYA],
    type: 'pay'
  }
}