Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add custom inspect for base types #4890

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading