检查 access_key 是否与 NEAR 帐户匹配的最佳方法

Best way to check if access_key matches NEAR account

我正在构建一个用户身份验证解决方案,将附近的帐户与我自己 生成的访问令牌相匹配。我想在我的 node.js 后端(使用 Nearlib?)验证 near access key(存储在浏览器的本地存储中)是否与提供的 near 帐户匹配。这是为了证明该请求确实是由账户所有者发送的。

所以如果我有:

accountID: "myAccount",
near_access_token: "ed25519:{...}"

我还假设此处使用的正确 near access tokennearlib:keystore:klopt:default 下。

I want to verify in my node.js backend (using Nearlib?) if a near access key (stored in local storage in browser) matches the near account provided.

您应该能够使用 account.getAccessKeys() 调用 https://github.com/nearprotocol/nearlib/blob/master/src.ts/account.ts#L202

获取给定帐户的访问密钥列表

然后您可以检查密钥库中的密钥对是否具有相同的 public 密钥。

This is to prove that the request is actually sent by the account owner.

如果您想证明请求是由帐户所有者发送的——您需要验证签名。

参见例如帐户助手微服务中的此代码: https://github.com/nearprotocol/near-contract-helper/blob/19ac6ce05a0d44f0e389c85b30bc2b6a9190caac/app.js#L97

在那种情况下 securityCode 必须由帐户签名(在设置帐户恢复时)。在您的情况下,您想要签署请求而不是签署 securityCode

您应该可以使用 Signer.signMessage API https://github.com/nearprotocol/nearlib/blob/master/src.ts/signer.ts#L38

签署请求