fes
fes

Reputation: 2515

Converting _bn back into PublicKey with Solana

When creating a Solana Transaction I set the feePayer with a public key. When I send this transaction between various endpoints, the feePayer gets converted to something like below:

"feePayer": {
        "_bn": {
          "negative": 0,
          "words": [
            37883239,
            7439402,
            52491380,
            11153292,
            7903486,
            65863299,
            41062795,
            11403443,
            13257012,
            320410,
            0
          ],
          "length": 10,
          "red": null
        }
      }

My question is, how can I convert this feePayer JSON object back as a PublicKey?

I've tried

new solanaWeb3.PublicKey(feePayer) or new solanaWeb3.PublicKey(feePayer._bn)

However both don't seem to work, any ideas how to get this json form back into PublicKey: BN<....>?

Upvotes: 1

Views: 1903

Answers (1)

KRist
KRist

Reputation: 1412

A BN is a BigNumber. I had a similar case:

"feePayer": {
        "_bn": "xcdcasaldkjalsd...."
}

Solution to it:

import BN from "bn.js";

const publicKeyFromBn = (feePayer) => {
    const bigNumber = new BN(feePayer._bn, 16)
    const decoded = { _bn: bigNumber };
    return new PublicKey(decoded);
};

You should play with new BN(feePayer._bn, 16) params to make it work specifically for your case.

Upvotes: 2

Related Questions