Skip to content

Commit

Permalink
chore: Add custom inspect for base types (#4890)
Browse files Browse the repository at this point in the history
Improves display of custom types when debugging to make them less
verbose.

Before:
```
AztecAddress {
        asBuffer: Buffer(32) [Uint8Array] [
           31,  83, 136, 221,  86,  24, 222, 199,
          209,  84, 126,  88, 242,  65, 241, 120,
           45, 175, 195, 124, 100,  32, 173,  65,
          113, 136, 197, 249,  19, 163, 103,  97
        ],
        asBigInt: 14169291217176329001256622499166672439531577110004770510807332793442883561313n
      },
```

After:
```
AztecAddress<0x2ea8404477a7858b3978fedd1cd98c216212fba0e44ff40e12f4cf3bfc2546e4>
```
  • Loading branch information
spalladino committed Mar 1, 2024
1 parent bd3f614 commit a1b3c01
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion yarn-project/foundation/src/abi/selector.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { inspect } from 'util';

import { toBufferBE } from '../bigint-buffer/index.js';
import { Fr } from '../fields/index.js';

Expand Down Expand Up @@ -34,7 +36,11 @@ export abstract class Selector {
* @returns The string.
*/
toString(): string {
return this.toBuffer().toString('hex');
return '0x' + this.toBuffer().toString('hex');
}

[inspect.custom]() {
return `Selector<${this.toString()}>`;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions yarn-project/foundation/src/aztec-address/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { inspect } from 'util';

import { Fr, fromBuffer } from '../fields/index.js';
import { BufferReader, FieldReader } from '../serialize/index.js';

Expand All @@ -16,6 +18,10 @@ export class AztecAddress extends Fr {
super(buffer);
}

[inspect.custom]() {
return `AztecAddress<${this.toString()}>`;
}

static ZERO = new AztecAddress(Buffer.alloc(32));

static zero(): AztecAddress {
Expand Down
6 changes: 6 additions & 0 deletions yarn-project/foundation/src/eth-address/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { inspect } from 'util';

import { keccak256String } from '../crypto/keccak/index.js';
import { randomBytes } from '../crypto/random/index.js';
import { Fr } from '../fields/index.js';
Expand Down Expand Up @@ -158,6 +160,10 @@ export class EthAddress {
return `0x${this.buffer.toString('hex')}` as `0x${string}`;
}

[inspect.custom]() {
return `EthAddress<${this.toString()}>`;
}

/**
* Returns the Ethereum address as a checksummed string.
* The output string will have characters in the correct upper or lowercase form, according to EIP-55.
Expand Down
10 changes: 10 additions & 0 deletions yarn-project/foundation/src/fields/fields.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { inspect } from 'util';

import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
import { randomBytes } from '../crypto/random/index.js';
import { BufferReader } from '../serialize/buffer_reader.js';
Expand Down Expand Up @@ -185,6 +187,10 @@ export class Fr extends BaseField {
super(value);
}

[inspect.custom]() {
return `Fr<${this.toString()}>`;
}

protected modulus() {
return Fr.MODULUS;
}
Expand Down Expand Up @@ -251,6 +257,10 @@ export class Fq extends BaseField {
private static HIGH_SHIFT = BigInt((BaseField.SIZE_IN_BYTES / 2) * 8);
private static LOW_MASK = (1n << Fq.HIGH_SHIFT) - 1n;

[inspect.custom]() {
return `Fq<${this.toString()}>`;
}

get low(): Fr {
return new Fr(this.toBigInt() & Fq.LOW_MASK);
}
Expand Down

0 comments on commit a1b3c01

Please sign in to comment.