-
Notifications
You must be signed in to change notification settings - Fork 249
/
create-testnet-account.ts
49 lines (42 loc) · 1.91 KB
/
create-testnet-account.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {
createTopLevelAccount,
generateRandomKeyPair,
getSignerFromKeystore,
getTestnetRpcProvider,
} from '@near-js/client';
import { UnencryptedFileSystemKeyStore } from '@near-js/keystores-node';
import chalk from 'chalk';
import { join } from 'node:path';
import { homedir } from 'node:os';
export default async function createTestnetAccountCookbook(accountId: string, newAccountId?: string) {
if (!accountId) {
console.log(chalk`{red pnpm createTestnetAccount -- CREATOR_ACCOUNT_ID [NEW_ACCOUNT_ID]}`);
return;
}
// initialize testnet RPC provider
const rpcProvider = getTestnetRpcProvider();
// initialize the transaction signer using a pre-existing key for `accountId`
const keystore = new UnencryptedFileSystemKeyStore(join(homedir(), '.near-credentials'));
const signer = getSignerFromKeystore(accountId, 'testnet', keystore);
// create a new key from random data
const keyPair = generateRandomKeyPair('ed25519');
const newAccount = newAccountId || `${new Date().valueOf()}-${Math.ceil(Math.random() * 10e12)}.testnet`;
await createTopLevelAccount({
account: accountId,
contract: 'testnet',
initialBalance: 100n,
newAccount,
newPublicKey: keyPair.getPublicKey().toString(),
deps: {
rpcProvider,
signer,
},
});
// save new account in plaintext filesystem keystore
await keystore.setKey('testnet', accountId, keyPair);
console.log(chalk`{white ------------------------------------------------------------------------ }`);
console.log(chalk`{bold.green RESULTS} {white Created testnet account}`);
console.log(chalk`{white ------------------------------------------------------------------------ }`);
console.log(chalk`{bold.white New Account} {white |} {bold.yellow ${accountId}} {white |} {bold.yellow ${keyPair.getPublicKey().toString()}}`);
console.log(chalk`{white ------------------------------------------------------------------------ }`);
}