如何在 NEAR 协议上创建账户?

How to create an account on NEAR protocol?

我想学习如何创建 account using RPC or REST calls on NEAR protocol

如果您想创建子账户(a.frol.near 当您拥有 frol.near):提交包含 CREATE_ACCOUNT、TRANSFER、ADD_KEY 操作的交易。这是一个 example of such a transaction.

如果你想创建*.near账户,你需要在near合约上使用create_account函数调用提交交易。这是一个 example of such a transaction, and here is a code snippet from the tutorial in the docs using near-api-js JS 库:

const HELP = `Please run this script in the following format:
    node create-testnet-account.js CREATOR_ACCOUNT.testnet NEW_ACCOUNT.testnet AMOUNT
`;

const { connect, KeyPair, keyStores, utils } = require("near-api-js");
const path = require("path");
const homedir = require("os").homedir();

const CREDENTIALS_DIR = ".near-credentials";
const credentialsPath = path.join(homedir, CREDENTIALS_DIR);
const keyStore = new keyStores.UnencryptedFileSystemKeyStore(credentialsPath);

const config = {
  keyStore,
  networkId: "testnet",
  nodeUrl: "https://rpc.testnet.near.org",
};

if (process.argv.length !== 5) {
  console.info(HELP);
  process.exit(1);
}

createAccount(process.argv[2], process.argv[3], process.argv[4]);

async function createAccount(creatorAccountId, newAccountId, amount) {
  const near = await connect({ ...config, keyStore });
  const creatorAccount = await near.account(creatorAccountId);
  const keyPair = KeyPair.fromRandom("ed25519");
  const publicKey = keyPair.publicKey.toString();
  await keyStore.setKey(config.networkId, newAccountId, keyPair);

  return await creatorAccount.functionCall({
    contractId: "testnet",
    methodName: "create_account",
    args: {
      new_account_id: newAccountId,
      new_public_key: publicKey,
    },
    gas: "300000000000000",
    attachedDeposit: utils.format.parseNearAmount(amount),
  });
}

如果您不需要命名帐户,您可以只生成一个新的 ed25519 key-pair,public 密钥的十六进制表示将是您的帐户 ID(它不会被记录在链上,直到 you/someone 向它转移一些 NEAR 代币,因此它被称为 "implicit" account). Example for such an account.

这里有how to construct a transaction. Ultimately, you will submit your transaction via JSON RPC broadcast_tx* endpoints的详细教程。