Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
address growing account names, warn against network inconsistency, tl…
Browse files Browse the repository at this point in the history
…a's >= 32
  • Loading branch information
mikedotexe committed Apr 24, 2020
1 parent 2c82feb commit 8d39834
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
56 changes: 35 additions & 21 deletions commands/create-account.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const { KeyPair } = require('near-api-js');
const NEAR_ENV_SUFFIXES = [
'near',
'test',
'beta',
'dev'
];
const TLA_MIN_LENGTH = 11;
const NEAR_ENV_SUFFIXES = {
production: 'near',
default: 'test',
development: 'test',
devnet: 'dev',
betanet: 'beta'
};
const TLA_MIN_LENGTH = 32;

module.exports = {
command: 'create_account <accountId>',
desc: 'create a new developer account (top-level account 11+ chars, or a subdomain of the masterAccount)',
desc: 'create a new developer account (subaccount of the masterAccount, ex: app.alice.test)',
builder: (yargs) => yargs
.option('accountId', {
desc: 'Unique identifier for the newly created account',
Expand All @@ -38,25 +39,38 @@ module.exports = {

async function createAccount(options) {
// NOTE: initialBalance is passed as part of config here, parsed in middleware/initial-balance
// periods are disallowed in top-level accounts and can only be used for subdomains
// periods are disallowed in top-level accounts and can only be used for subaccounts
const splitAccount = options.accountId.split('.');
if (splitAccount.length === 2) {
// TLA (bob-at-least-maximum-chars.test)

const splitMaster = options.masterAccount.split('.');
const masterRootTLA = splitMaster[splitMaster.length - 1];
if (splitAccount.length === 1) {
// TLA (bob-with-at-least-maximum-characters)
if (splitAccount[0].length < TLA_MIN_LENGTH) {
console.log(`Top-level accounts (not ending in .near, .test, etc) must be greater than ${TLA_MIN_LENGTH} characters`);
console.log(`Top-level accounts must be greater than ${TLA_MIN_LENGTH} characters.\n` +
'Note: this is for advanced usage only. Typical account names are of the form:\n' +
'app.alice.test, where the masterAccount shares the top-level account (.test).'
);
return;
}
} else if (splitAccount.length === 3) {
// Subdomain (short.alice.near)
if (!NEAR_ENV_SUFFIXES.includes(splitAccount[2])) {
console.log(`Expected a subdomain account name ending in ${NEAR_ENV_SUFFIXES.join(', ')}. (Example: counter.alice.test)`);
} else if (splitAccount.length > 1) {
// Subaccounts (short.alice.near, even.more.bob.test, and eventually peter.potato)
// Check that master account TLA matches
const accountRootTLA = splitAccount[splitAccount.length - 1];
const accountTLA = splitAccount.filter((n, i) => i !== 0).join('.');
if (accountTLA !== options.masterAccount) {
console.log(`New account doesn't share the same top-level account. Expecting account name to end in ".${options.masterAccount}"`);
return;
}
} else {
console.log('Unexpected account name format. Please use one of these formats:\n' +
`1. Top-level name with ${TLA_MIN_LENGTH}+ characters (ex: near-friend.test)\n` +
`2. A subdomain account name ending with .${NEAR_ENV_SUFFIXES.join(', .')}. (Example: counter.alice.test)`);
return;

// Rules apply for environments except local, test, ci, ci-staging
if (Object.keys(NEAR_ENV_SUFFIXES).includes(options.networkId)) {
// Recommend that the TLA matches the expected network in most cases
const networkTLA = NEAR_ENV_SUFFIXES[options.networkId];
if (networkTLA !== masterRootTLA || networkTLA !== accountRootTLA) {
console.log(`NOTE: In most cases, when connected to "${options.networkId}" account and masterAccount will end in ".${networkTLA}"`);
}
}
}
let near = await connect(options);
let keyPair;
Expand Down
4 changes: 2 additions & 2 deletions commands/dev-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function devDeploy(options) {
console.log(`Done deploying to ${accountId}`);
}

async function createDevAccountIfNeeded({ near, keyStore, networkId, init }) {
async function createDevAccountIfNeeded({ near, keyStore, networkId, init, masterAccount }) {
// TODO: once examples and create-near-app use the dev-account.env file, we can remove the creation of dev-account
// https://github.com/nearprotocol/near-shell/issues/287
const accountFilePath = `${keyStore.keyDir}/dev-account`;
Expand All @@ -67,7 +67,7 @@ async function createDevAccountIfNeeded({ near, keyStore, networkId, init }) {
}
}

const accountId = `dev-${Date.now()}.test.near`;
const accountId = `dev-${Date.now()}.${masterAccount}`;
const keyPair = await KeyPair.fromRandom('ed25519');
await near.accountCreator.createAccount(accountId, keyPair.publicKey);
await keyStore.setKey(networkId, accountId, keyPair);
Expand Down

0 comments on commit 8d39834

Please sign in to comment.