2

When I compare two public keys with:

expect(lotteryState.authority).to.equal(user.publicKey);

I get the following error:

AssertionError: expected PublicKey(DCpJReAfonSrgohiQbTmKKbjbqVofspFRHz9yQikzooP){ _bn: <BN: b5531d6246f2015e72621c71ebf9304871dd8bbcf6f6c714948be6ddbd1e6ff2> } to equal PublicKey(DCpJReAfonSrgohiQbTmKKbjbqVofspFRHz9yQikzooP){ _bn: <BN: b5531d6246f2015e72621c71ebf9304871dd8bbcf6f6c714948be6ddbd1e6ff2> }

As you can see, both keys are the same but the test suite says that they are different. I can fix this by changing to a deep equality check:

expect(lotteryState.authority).to.deep.equal(user.publicKey);

But I am wondering if there exists a native method in anchor to compare to keys such as:

expect(lotteryState.authority).to.publicKeyEqual(user.publicKey);

1 Answer 1

4

The PublicKey class contains an equals method that can compare two pubkeys:

  equals(publicKey: PublicKey): boolean {
    return this._bn.eq(publicKey._bn);
  }

IIRC there can also be compare issues here as well, so it is pretty common to just compare the base58 strings of both pubkeys (pubkey1.toBase58() and pubkey2.toBase58()).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.