Skip to content

Commit

Permalink
fix checkCredentials
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisjoshford committed Mar 25, 2021
1 parent 0ff1279 commit 915ffc3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion commands/create-account.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const createAccountCommandDeprecated = {
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 subaccounts
checkCredentials(options);
await checkCredentials(options.masterAccount, options.networkId, options.keyStore);
const splitAccount = options.accountId.split('.');

const splitMaster = options.masterAccount.split('.');
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ exports.keys = async function (options) {
};

exports.sendMoney = async function (options) {
checkCredentials(options);
await checkCredentials(options.sender, options.networkId, options.keyStore);
console.log(`Sending ${options.amount} NEAR to ${options.receiver} from ${options.sender}`);
const near = await connect(options);
const account = await near.account(options.sender);
Expand Down
20 changes: 12 additions & 8 deletions utils/check-credentials.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
const homedir = require('os').homedir();
const credentials_dir = homedir + '/.near-credentials';
const fs = require('fs').promises;
const { askYesNoQuestion } = require('./readline');

module.exports = async function checkCredentials(options) {
if(!(await options.keyStore.getKey(options.networkId, options.accountId))) {
console.log(`Unable to find ${options.networkId} credentials for ${options.accountId}`);
const hasInDefault = await options.keyStore.getKey('default', options.accountId);
module.exports = async function checkCredentials(accountId, networkId, keyStore) {
console.log('networkId', networkId);
console.log('accountId', accountId);
if(!(await keyStore.getKey(networkId, accountId))) {
console.log(`Unable to find ${networkId} credentials for ${accountId}`);
const hasInDefault = await keyStore.getKey('default', accountId);
if(hasInDefault) {
const answer = await askYesNoQuestion('Key found in deprecated \'default\' folder. Would you like to move key to \'testnet\' folder? (y/n): ', false);
if(answer) {
console.log('Moving files...');
await fs.copyFile(`${credentials_dir}/default/${option.accountId}`, `${credentials_dir}/testnet/${options.accountId}`)
await fs.copyFile(`${credentials_dir}/default/${accountId}.json`, `${credentials_dir}/testnet/${accountId}.json`)
throw ('Please run command again.')
} else {
console.log('Please relocate credentials found in \'default\' directory to \'testnet\'.');
return;
throw ('Please relocate credentials found in \'default\' directory to \'testnet\'.');
}
}
} else throw ('error')
}
}

0 comments on commit 915ffc3

Please sign in to comment.