1

I'm working with Solana addresses in a TypeScript project and need to ensure consistency in their representation, similar to Ethereum's toChecksumAddress (https://docs.web3js.org/api/web3-utils/function/toChecksumAddress/).

An example is:

      import { PublicKey } from '@solana/web3.js';

      const key = new PublicKey("2wicZ7QPvUn1RNUHVUsdf8xAH22RBkiXEkGS43RDboJ4");
      const key2 = new PublicKey("2wicz7qpvun1rnuhvusdf8xah22rbkixekgs43rdboj4"); // lowercase

      const keyInfo = await this.connection.getAccountInfo(key); // returns data
      const keyInfo2 = await this.connection.getAccountInfo(key2); // returns null

Is there a recommended approach or library for achieving this in JavaScript/TypeScript? Any insights or code examples would be appreciated. Thanks!

1 Answer 1

1

Simply using the PublicKey class is the best way to go. You can use simple base58 string representation of your public key in most places, however wrapping it in PublicKey class is the best way to go, which you have done in the example code snippet you have provided. I wouldn't worry about anything else really, you're good to go.

4
  • Unfortunately the lower case one returns null Commented Jun 20 at 6:15
  • The base58 encoding for public keys is case-sensitive, so those two addresses are not the same.
    – Jon C
    Commented Jun 21 at 10:50
  • I think the OP is rather looking for a Solana version of web3.py's .toChecksumAddress() method, which takes in a string and tries to normalize(standardize) the case of characters in it. Commented Sep 9 at 16:44
  • Ah okay, makes sense, I misunderstood the question. No such thing in Solana unfortunately as base58 encoding is case-senstive, unlike hex representation of Ethereum addresses which are case-insensitive. Commented Oct 3 at 10:44

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.