프로젝트/졸업과제

[졸과][NEAR] 테스트넷 계정 생성

KimCookieYa 2024. 3. 1. 02:55

배경

Near Protocol의 Testnet에서 Named Account를 생성하기 위해 고군분투했다. 그 결과, 로컬에서 Testnet 계정을 생성하기 위해서는 near를 보유한 기존 계정의 FAK(Full Access Key)가 반드시 필요한 줄 알았는데, 그렇지 않다는 걸 깨달았다!

 

계정 생성 흐름

메인넷

  1. Implicit Account 생성은 제한없이 가능
  2. near를 보유한 기존 계정에서 막 생성된 Implicit Account로 near를 송금한다.
  3. Account ID를 지정하여 Named Account로 전환한다.

 

테스트넷

  • 그냥 바로 일정 near를 보유한 Named Account를 생성하면 된다.
  • 메인넷의 생성 흐름을 따를 수도 있다.

near-cli

Near 공식 문서

 

NEAR CLI | NEAR Documentation

The NEAR Command Line Interface (CLI) is a tool that enables to interact with the NEAR network directly from the shell. Among other things,

docs.near.org

 

# near를 보유한 기존 계정의 near로 새 계정을 생성한다.
near create-account new-acc.testnet --useAccount example-acct.testnet

# Testnet의 faucet의 near를 사용해서 새 계정을 생성한다.
near create-account new-acc.testnet --useFaucet

 

 

near-api-js

import { utils, keyStores, KeyPair, connect, providers } from 'near-api-js';

const myKeyStore = new keyStores.BrowserLocalStorageKeyStore();

const config = {
  networkId: 'testnet',
  keyStore: myKeyStore,
  nodeUrl: 'https://rpc.testnet.near.org',
  walletUrl: 'https://testnet.mynearwallet.com/',
  helperUrl: 'https://helper.testnet.near.org',
};

export async function createNearAccount(creatorAccountId: string, newAccountId: string, amount: string) {
  const near = await connect(config);
  const creatorAccount = await near.account(creatorAccountId);
  const keyPair = KeyPair.fromRandom('ed25519');
  const publicKey = keyPair.getPublicKey().toString();

  await myKeyStore.setKey(config.networkId, newAccountId, keyPair);
  try {
  	// 계정 생성 함수
    await creatorAccount.createAccount(newAccountId, publicKey, utils.format.parseNearAmount(amount));
    console.log('Account creation successful');
    return true;
  } catch (error) {
    console.error('Account creation failed:', error);
    return false;
  }
}

 

- near-api-js에서 생성자 계정을 Account 객체로 불러와서 createAccount 메서드를 호출한다.

- 확실하게 새로운 Named Account를 생성할 수 있지만, 로컬에서 생성자 계정의 FAK(Full Access Key)를 보유하고 있어야 해서 키노출의 위험이 너무 큰 방식이다.

 

솔루션

다른 방식을 찾고자 Near 코리아 디스코드에도 물어보고, near-api-js Github에도 Issue를 남겼는데... 정답을 얻었다!

 

near-api-js Issue

 

[Error] createAccount don't work on testnet. · Issue #1290 · near/near-api-js

Prerequisites I'm using the latest version of near-api-js. I have tried to start with a fresh project and reproduce the defect with minimal code changes. I have read the console error messages care...

github.com

 

레포지토리의 Collaborator가 친절히 방법을 알려주었다. Testnet과 연결된 Near 객체에서 createAccount를 호출하면 끝이다! NearBlock(Testnet)에서 확인해본 결과 실제로, 잘 생성된 것을 볼 수 있었다.

 

NearBlock

 

 

2주일 동안 고민한 [계정 생성] 프로세스가 너무 간단하게 끝나서 조금 허탈하기도 하다. 그래도 아직 구현해야 할 게 많으니 힘내야겠다.