3

Is it possible to get an authorized account's nonce accounts by using only authorized account's public key?

I could not find such a solution on github, discord, and in docs. They always use the public key of the nonce account.

I use web3js on my project.

1 Answer 1

7

Using knowledge of the following facts:

  • All nonce accounts are owned by the system program
  • Nonce accounts are exactly 80 bytes long
  • The nonce authority's public key is written into bytes 8–40

You can find all of the nonce accounts for which X is the authority using getProgramAccounts:

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

const AUTHORITY_PUBLIC_KEY = new PublicKey(
  'A1FGsvF7c87UgjYxDyC22rAKAqchBAcFLSMcAGhECLKV'
);
const SYSTEM_PROGRAM_ADDRESS = new PublicKey(
  '11111111111111111111111111111111'
);

const connection = new Connection(clusterApiUrl('testnet'));
const nonceAccounts = await connection.getProgramAccounts(
  // The system program owns all nonce accounts.
  SYSTEM_PROGRAM_ADDRESS,
  {
    filters: [
      {
        // Nonce accounts are exactly 80 bytes long
        dataSize: 80,
      },
      {
        // The authority's 32-byte public key is written
        // into bytes 8-40 of the nonce's account data.
        memcmp: {
          bytes: AUTHORITY_PUBLIC_KEY.toBase58(),
          offset: 8,
        },
      },
    ],
  }
);
console.log(nonceAccounts);

Playground link.

2
  • 3
    Be aware that getProgramAccounts is a monster of a query method, and your RPC provider may limit your usage of it, or at least be annoyed by you. Commented Jan 23 at 22:31
  • 2
    Thank you so much. It worked, and you are right about how heavy query this is. It took more than 1 minute. Commented Jan 24 at 9:24

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.