skip to Main Content

Im trying to get the NEAR WALLET AccountID.

I have the next vode to get only the public key but i want also to get the account ID from privatekey or from public key.

const nearApi = require('near-api-js');                     

let keypair = nearApi.KeyPair.fromString('3by8kdJoJHu7uUkKfoaLJ2Dp1q1TigeWMGpHu9UGXsWdREqPcshCM223kWadmrMKpV9AsWG5wL9F9hZzjHSRFXud');                                               

console.log(keypair.getPublicKey().toString());

I need something like this website does: https://near.github.io/account-lookup/

This website has the option to gwt the accountId from the public key like this (ed25519:6gaTj2kWoCAYGNJs1CR1bACsy4DRXwvd5B9cqUmx2CJw) and I have already a code to get this public key from privatekey.

So my question is how to get the accountId with javascript?

Can someone extract the correct code from the website above to generate the accountId from the public key?

Thanks!

2

Answers


  1. Just import https://cdn.jsdelivr.net/npm/near-api-js/dist/near-api-js.js to use it in Vanilla JavaScript.

    let keypair = nearApi.KeyPair.fromString('3by8kdJoJHu7uUkKfoaLJ2Dp1q1TigeWMGpHu9UGXsWdREqPcshCM223kWadmrMKpV9AsWG5wL9F9hZzjHSRFXud');
    console.log(keypair.getPublicKey().toString());
    <script src="https://cdn.jsdelivr.net/npm/near-api-js/dist/near-api-js.js"></script>
    Login or Signup to reply.
  2. In the NEAR Protocol, the relationship between keys and accounts is many-to-many, which means one public key could be associated with multiple account IDs and one account could have multiple associated keys. NEAR Protocol doesn’t provide a built-in API method to fetch an account ID from a public key directly due to this complexity.

    However, you can set up a custom Indexer to track the association between public keys and account IDs. An Indexer is a service that listens to the NEAR blockchain and records events into a queryable database. You could build an Indexer to track AddKey events, which include the account ID and the new public key being added. This would allow you to look up the account ID associated with a given public key.

    Here’s a high-level overview of the steps you would need to take:

    1. Set up an Indexer using the NEAR Lake Framework or NEAR Indexer Framework (the latter requires the same amount of resources as an RPC NEAR node).
    2. Customize the Indexer to record AddKey events in a database. These events contain the account ID and public key.
    3. Query the database to find the account ID(s) associated with a given public key.

    Remember that this is a non-trivial task that involves running a full NEAR node and setting up a database to record events, and it might not be suitable for all use cases. It’s also possible that multiple account IDs could be associated with a single public key.

    This is a complex task and should only be undertaken if absolutely necessary and you have the necessary technical skills and resources. For routine tasks, it’s best to keep a secure record of your account IDs and keys when you create them.

    Wallets have their own indexers for this task. The account lookup website only works for lockup contracts that are named after the public key of the owner account id, so it is not going to work in general cases.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search