Skip to content

Commit

Permalink
Adds custom inspect function to PublicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Stella Wang authored and stellaw1 committed Feb 10, 2022
1 parent d5dec98 commit a5ab59b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions web3.js/src/publickey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import bs58 from 'bs58';
import {Buffer} from 'buffer';
import nacl from 'tweetnacl';
import {sha256} from '@ethersproject/sha2';
import util from 'util';

import {Struct, SOLANA_SCHEMA} from './util/borsh-schema';
import {toBuffer} from './util/to-buffer';
Expand Down Expand Up @@ -119,6 +120,13 @@ export class PublicKey extends Struct {
return this.toBase58();
}

/**
* Returns public key in string format when console.log is called
*/
[util.inspect.custom]() {
return this.toString();
}

/**
* Derive a public key from another key, a seed, and a program ID.
* The program ID will also serve as the owner of the public key, giving
Expand Down
7 changes: 7 additions & 0 deletions web3.js/test/publickey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import BN from 'bn.js';
import {Buffer} from 'buffer';
import {expect, use} from 'chai';
import chaiAsPromised from 'chai-as-promised';
import util from 'util';

import {Keypair} from '../src/keypair';
import {PublicKey, MAX_SEED_LENGTH} from '../src/publickey';
Expand Down Expand Up @@ -56,19 +57,25 @@ describe('PublicKey', function () {
const key = new PublicKey('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
expect(key.toBase58()).to.eq('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
expect(key.toString()).to.eq('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
expect(util.inspect(key)).to.eq(
'CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3',
);

const key2 = new PublicKey('1111111111111111111111111111BukQL');
expect(key2.toBase58()).to.eq('1111111111111111111111111111BukQL');
expect(key2.toString()).to.eq('1111111111111111111111111111BukQL');
expect(util.inspect(key2)).to.eq('1111111111111111111111111111BukQL');

const key3 = new PublicKey('11111111111111111111111111111111');
expect(key3.toBase58()).to.eq('11111111111111111111111111111111');
expect(util.inspect(key3)).to.eq('11111111111111111111111111111111');

const key4 = new PublicKey([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
]);
expect(key4.toBase58()).to.eq('11111111111111111111111111111111');
expect(util.inspect(key4)).to.eq('11111111111111111111111111111111');
});

it('toJSON', () => {
Expand Down

0 comments on commit a5ab59b

Please sign in to comment.