Search code examples
javascriptsolanasolana-web3js

Solana method cannot convert string to Base58 format


I'm using @solana/web3.js and have this code:

const web3 = require("@solana/web3.js");
const clusterApi = process.env.SOLANA_CLUSTER;
module.exports = {
    getConfirmedSignaturesForAddress: async address => {
    try {
      const connection = new web3.Connection(web3.clusterApiUrl(clusterApi), "confirmed");

      const result = await connection.getSignaturesForAddress(address, {
        limit: 25
      });

      return {
        tx: result,
        status: true
      };
    } catch (e) {
      return {
        status: false,
        error: e.message
      };
    }
  }
}

And every time I call this function I get this error:

{ status: false, error: 'address.toBase58 is not a function' }

I was trying to send it already converted to Base58, but it just doesn't work. What's wrong?


Solution

  • This is how I solved this problem. Generally speaking, you need to convert it not just by converting to pure Base58, but like this:

    const web3 = require("@solana/web3.js");
    const bip39 = require("bip39");
    
    const getKeyFromMemonic = async mnemonic => {
      return new Promise((resolve, reject) => {
        bip39
          .mnemonicToSeed(mnemonic)
          .then(buffer => {
            const a = new Uint8Array(buffer.toJSON().data.slice(0, 32));
            const key = web3.Keypair.fromSeed(a);
            resolve(key);
          })
          .catch(err => reject(err));
      });
    };
    
    getSignaturesForAddress: async address => {
        try {
          const key = await getKeyFromMemonic(address);
    
          const connection = new web3.Connection(web3.clusterApiUrl(clusterApi), "confirmed");
    
          const result = await connection.getSignaturesForAddress(key.publicKey);
    
          return {
            tx: result,
            status: true
          };
        } catch (e) {
          return {
            status: false,
            error: e.message
          };
        }
      }