英文:
How to get NEAR WALLET accountId from private or public key?
问题
我试图获取 NEAR 钱包的 AccountID。
我有下面的代码可以获取公钥,但我也想从私钥或公钥中获取 AccountID。
const nearApi = require('near-api-js');
let keypair = nearApi.KeyPair.fromString('3by8kdJoJHu7uUkKfoaLJ2Dp1q1TigeWMGpHu9UGXsWdREqPcshCM223kWadmrMKpV9AsWG5wL9F9hZzjHSRFXud');
console.log(keypair.getPublicKey().toString());
我需要类似这个网站的功能:https://near.github.io/account-lookup/
这个网站可以从公钥获取 accountId,像这样 (ed25519:6gaTj2kWoCAYGNJs1CR1bACsy4DRXwvd5B9cqUmx2CJw)。我已经有一个从私钥获取公钥的代码。
所以我的问题是如何用 JavaScript 获取 accountId?
有人能从上述网站中提取正确的代码以从公钥生成 accountId 吗?
谢谢!
英文:
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!
答案1
得分: 2
在 NEAR 协议中,密钥与账户之间的关系是多对多的,这意味着一个公钥可以与多个账户ID关联,一个账户也可以有多个关联的密钥。由于这种复杂性,NEAR 协议并未提供直接从公钥获取账户ID的内置API方法。
然而,你可以设置一个自定义的 索引器 来跟踪公钥与账户ID之间的关联。索引器是一个服务,它监听 NEAR 区块链并将事件记录到可查询的数据库中。你可以构建一个索引器来跟踪 AddKey
事件,其中包括了账户ID和新添加的公钥。这将允许你查找与给定公钥关联的账户ID。
以下是你需要采取的步骤的高级概述:
- 使用 NEAR Lake Framework 或 NEAR Indexer Framework 来设置一个索引器(后者需要与一个 RPC NEAR 节点相同数量的资源)。
- 自定义索引器以将
AddKey
事件记录在数据库中。这些事件包含了账户ID和公钥。 - 查询数据库以查找与给定公钥关联的账户ID(s)。
请记住,这是一个需要运行完整 NEAR 节点并设置数据库以记录事件的非常规任务,可能并不适用于所有的用例。同时也可能存在多个账户ID与单个公钥关联的情况。
这是一个复杂的任务,只有在绝对必要且具备必要的技术技能和资源时才应着手进行。对于常规任务,最好在创建时保留你的账户ID和密钥的安全记录。
钱包会为此任务提供它们自己的索引器。账户查询网站只适用于以所有者账户ID的公钥命名的锁定合约,因此在一般情况下可能无法正常工作。
英文:
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:
- 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).
- Customize the Indexer to record
AddKey
events in a database. These events contain the account ID and public key. - 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.
答案2
得分: 1
在从 https://near.github.io/account-lookup/ 提取了 script.js 后,我找到了生成 accountId
的方式如下:
- 点击搜索按钮后,将执行
lookup()
函数。
- 查找函数将从公钥解码到
accountId
,使用了 bs58 解码。
let from_b58 = function(S,A){var d=[],b=[],i,j,c,n;for(i in S){j=0,c=A.indexOf(S[i]);if(c<0)return undefined;c||b.length^i?i:b.push(0);while(j in d||c){n=d[j];n=n?n*58+c:c;c=n>>8;d[j]=n%256;j++}}while(j--)b.push(d[j]);return new Uint8Array(b)};
const MAP = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const options = {
nodeUrl: "https://rpc.mainnet.near.org",
networkId: "mainnet",
deps: {},
};
function prepareAccountId(data) {
if (data.toLowerCase().endsWith(".near")) {
return data
.replace("@", "")
.replace("https://wallet.near.org/send-money/", "")
.toLowerCase();
}
if (data.length === 64 && !data.startsWith("ed25519:")) {
return data;
}
let publicKey;
if (data.startsWith("NEAR")) {
publicKey = Buffer.from(from_b58(data.slice(4), MAP).slice(0, -4));
} else {
publicKey = Buffer.from(from_b58(data.replace("ed25519:", ""), MAP));
}
return publicKey.toString("hex");
}
async function lookup() {
let keypair = nearApi.KeyPair.fromString("3by8kdJoJHu7uUkKfoaLJ2Dp1q1TigeWMGpHu9UGXsWdREqPcshCM223kWadmrMKpV9AsWG5wL9F9hZzjHSRFXud");
let publicKey = keypair.getPublicKey().toString();
console.log('public key:',publicKey);
const near = await nearApi.connect(options);
let accountId = prepareAccountId(publicKey);
console.log('account id:',accountId);
}
lookup();
英文:
After extracting script.js from https://near.github.io/account-lookup/, I find how the accountId
is generated like this:
- After clicking the search button, the
lookup()
function will be executed.
- The lookup function will do bs58 decoding from the public key to
accountId
.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let from_b58 = function(S,A){var d=[],b=[],i,j,c,n;for(i in S){j=0,c=A.indexOf(S[i]);if(c<0)return undefined;c||b.length^i?i:b.push(0);while(j in d||c){n=d[j];n=n?n*58+c:c;c=n>>8;d[j]=n%256;j++}}while(j--)b.push(d[j]);return new Uint8Array(b)};
const MAP = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const options = {
nodeUrl: "https://rpc.mainnet.near.org",
networkId: "mainnet",
deps: {},
};
function prepareAccountId(data) {
if (data.toLowerCase().endsWith(".near")) {
return data
.replace("@", "")
.replace("https://wallet.near.org/send-money/", "")
.toLowerCase();
}
if (data.length === 64 && !data.startsWith("ed25519:")) {
return data;
}
let publicKey;
if (data.startsWith("NEAR")) {
publicKey = Buffer.from(from_b58(data.slice(4), MAP).slice(0, -4));
} else {
publicKey = Buffer.from(from_b58(data.replace("ed25519:", ""), MAP));
}
return publicKey.toString("hex");
}
async function lookup() {
let keypair = nearApi.KeyPair.fromString("3by8kdJoJHu7uUkKfoaLJ2Dp1q1TigeWMGpHu9UGXsWdREqPcshCM223kWadmrMKpV9AsWG5wL9F9hZzjHSRFXud");
let publicKey = keypair.getPublicKey().toString();
console.log('public key:',publicKey);
const near = await nearApi.connect(options);
let accountId = prepareAccountId(publicKey);
console.log('account id:',accountId);
}
lookup();
<!-- language: lang-html -->
<script src="https://cdn.jsdelivr.net/npm/near-api-js/dist/near-api-js.js"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论