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

Revert "generate function for asking id" #594

Merged
merged 1 commit into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions commands/add-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const inspectResponse = require('../utils/inspect-response');
const { utils } = require('near-api-js');
const eventtracking = require('../utils/eventtracking');


module.exports = {
command: 'add-key <account-id> <access-key>',
Expand Down Expand Up @@ -33,7 +31,6 @@ module.exports = {
};

async function addAccessKey(options) {
await eventtracking.askForId(options);
console.log(`Adding ${options.contractId ? 'function call access' : 'full access'} key = ${options.accessKey} to ${options.accountId}.`);
const near = await connect(options);
const account = await near.account(options.accountId);
Expand Down
2 changes: 0 additions & 2 deletions commands/delete-key.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const inspectResponse = require('../utils/inspect-response');
const eventtracking = require('../utils/eventtracking');

module.exports = {
command: 'delete-key <account-id> <access-key>',
Expand All @@ -16,7 +15,6 @@ module.exports = {
};

async function deleteAccessKey(options) {
await eventtracking.askForId(options);
console.log(`Deleting key = ${options.accessKey} on ${options.accountId}.`);
const near = await connect(options);
const account = await near.account(options.accountId);
Expand Down
71 changes: 33 additions & 38 deletions commands/generate-key.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,45 @@
const KeyPair = require('near-api-js').KeyPair;
const exitOnError = require('../utils/exit-on-error');
const implicitAccountId = require('../utils/implicit-accountid');
const connect = require('../utils/connect');
const eventtracking = require('../utils/eventtracking');

module.exports = {
command: 'generate-key [account-id]',
desc: 'generate key or show key from Ledger',
builder: (yargs) => yargs,
handler: exitOnError(generateKey)
};

async function generateKey(options) {
await eventtracking.askForId(options);
const near = await connect(options);
handler: exitOnError(async (argv) => {
let near = await require('../utils/connect')(argv);

if (options.usingLedger) {
if (options.accountId) {
console.log('WARN: Account id is provided but ignored in case of using Ledger.');
if (argv.usingLedger) {
if (argv.accountId) {
console.log('WARN: Account id is provided but ignored in case of using Ledger.');
}
const publicKey = await argv.signer.getPublicKey();
// NOTE: Command above already prints public key.
console.log(`Implicit account: ${implicitAccountId(publicKey.toString())}`);
// TODO: query all accounts with this public key here.
// TODO: check if implicit account exist, and if the key doen't match already.
return;
}
const publicKey = await options.signer.getPublicKey();
// NOTE: Command above already prints public key.
console.log(`Implicit account: ${implicitAccountId(publicKey.toString())}`);
// TODO: query all accounts with this public key here.
// TODO: check if implicit account exist, and if the key doesn't match already.
return;
}

const { deps: { keyStore } } = near.config;
const existingKey = await keyStore.getKey(options.networkId, options.accountId);
if (existingKey) {
console.log(`Account has existing key pair with ${existingKey.publicKey} public key`);
return;
}

// If key doesn't exist, create one and store in the keyStore.
// Otherwise, it's expected that both key and accountId are already provided in arguments.
if (!options.publicKey) {
const keyPair = KeyPair.fromRandom('ed25519');
options.publicKey = keyPair.publicKey.toString();
options.accountId = options.accountId || implicitAccountId(options.publicKey);
await keyStore.setKey(options.networkId, options.accountId, keyPair);
} else if (options.seedPhrase) {
const seededKeyPair = await options.signer.keyStore.getKey(options.networkId, options.accountId);
await keyStore.setKey(options.networkId, options.accountId, seededKeyPair);
}
const { deps: { keyStore } } = near.config;
const existingKey = await keyStore.getKey(argv.networkId, argv.accountId);
if (existingKey) {
console.log(`Account has existing key pair with ${existingKey.publicKey} public key`);
return;
}

console.log(`Key pair with ${options.publicKey} public key for an account "${options.accountId}"`);
}
// If key doesn't exist, create one and store in the keyStore.
// Otherwise, it's expected that both key and accountId are already provided in arguments.
if (!argv.publicKey) {
const keyPair = KeyPair.fromRandom('ed25519');
argv.publicKey = keyPair.publicKey.toString();
argv.accountId = argv.accountId || implicitAccountId(argv.publicKey);
await keyStore.setKey(argv.networkId, argv.accountId, keyPair);
} else if (argv.seedPhrase) {
const seededKeyPair = await argv.signer.keyStore.getKey(argv.networkId, argv.accountId);
await keyStore.setKey(argv.networkId, argv.accountId, seededKeyPair);
}

console.log(`Key pair with ${argv.publicKey} public key for an account "${argv.accountId}"`);
})
};
8 changes: 1 addition & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ exports.clean = async function () {
};

exports.deploy = async function (options) {
await eventtracking.askForId(options);
console.log(
`Starting deployment. Account id: ${options.accountId}, node: ${options.nodeUrl}, helper: ${options.helperUrl}, file: ${options.wasmFile}`);

Expand Down Expand Up @@ -83,7 +82,6 @@ const openUrl = async function(url) {

exports.login = async function (options) {
await eventtracking.askForConsentIfNeeded(options);
await eventtracking.askForId(options);
if (!options.walletUrl) {
console.log('Log in is not needed on this environment. Please use appropriate master account for shell operations.');
await eventtracking.track(eventtracking.EVENT_ID_LOGIN_END, { success: true, login_is_not_needed: true }, options);
Expand Down Expand Up @@ -178,7 +176,6 @@ exports.login = async function (options) {
};

exports.viewAccount = async function (options) {
await eventtracking.askForId(options);
let near = await connect(options);
let account = await near.account(options.accountId);
let state = await account.state();
Expand All @@ -190,7 +187,7 @@ exports.viewAccount = async function (options) {
};

exports.deleteAccount = async function (options) {
await eventtracking.askForId(options);

console.log(
`Deleting account. Account id: ${options.accountId}, node: ${options.nodeUrl}, helper: ${options.helperUrl}, beneficiary: ${options.beneficiaryId}`);
const near = await connect(options);
Expand All @@ -201,7 +198,6 @@ exports.deleteAccount = async function (options) {
};

exports.keys = async function (options) {
await eventtracking.askForId(options);
let near = await connect(options);
let account = await near.account(options.accountId);
let accessKeys = await account.getAccessKeys();
Expand All @@ -210,7 +206,6 @@ exports.keys = async function (options) {
};

exports.sendMoney = async function (options) {
await eventtracking.askForId(options);
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 All @@ -219,7 +214,6 @@ exports.sendMoney = async function (options) {
};

exports.stake = async function (options) {
await eventtracking.askForId(options);
console.log(`Staking ${options.amount} (${utils.format.parseNearAmount(options.amount)}) on ${options.accountId} with public key = ${qs.unescape(options.stakingKey)}.`);
const near = await connect(options);
const account = await near.account(options.accountId);
Expand Down
2 changes: 1 addition & 1 deletion test/test_account_operations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ echo Create account
../bin/near create-account $testaccount

echo Get account state
RESULT=$(yes | ../bin/near state $testaccount -v | ../node_modules/.bin/strip-ansi)
RESULT=$(../bin/near state $testaccount -v | ../node_modules/.bin/strip-ansi)
echo $RESULT
EXPECTED=".+Account $testaccount.+amount:.+'100000000000000000000000000'.+ "
if [[ ! "$RESULT" =~ $EXPECTED ]]; then
Expand Down
4 changes: 2 additions & 2 deletions test/test_generate_key.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ KEY_FILE=~/.near-credentials/$NODE_ENV/generate-key-test.json
rm -f "$KEY_FILE"
echo "Testing generating-key: new key"

RESULT=$(yes |./bin/near generate-key generate-key-test --networkId $NODE_ENV -v)
RESULT=$(./bin/near generate-key generate-key-test --networkId $NODE_ENV -v)
echo $RESULT

if [[ ! -f "${KEY_FILE}" ]]; then
Expand All @@ -21,7 +21,7 @@ fi

echo "Testing generating-key: key for account already exists"

RESULT2=$(yes |./bin/near generate-key generate-key-test --networkId $NODE_ENV -v)
RESULT2=$(./bin/near generate-key generate-key-test --networkId $NODE_ENV -v)
echo $RESULT2

EXPECTED2=".*Account has existing key pair with ed25519:.+ public key.*"
Expand Down
79 changes: 6 additions & 73 deletions utils/eventtracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const uuid = require('uuid');

const TRACKING_ENABLED_KEY = 'trackingEnabled';
const TRACKING_SESSION_ID_KEY = 'trackingSessionId';
const TRACKING_ID_KEY = 'trackingID';

const isGitPod = () => {
return !!process.env.GITPOD_WORKSPACE_URL;
Expand Down Expand Up @@ -41,20 +40,6 @@ const shouldTrack = (shellSettings) => {
);
};

const shouldTrackID = (shellSettings) => {
return (
TRACKING_ID_KEY in shellSettings &&
shellSettings[TRACKING_ID_KEY]
);
};

const shouldNOTTrackID = (shellSettings) => {
return (
TRACKING_ID_KEY in shellSettings &&
!shellSettings[TRACKING_ID_KEY]
);
};

const track = async (eventType, eventProperties, options) => {
const shellSettings = settings.getShellSettings();
if (!shouldTrack(shellSettings)) {
Expand All @@ -75,8 +60,10 @@ const track = async (eventType, eventProperties, options) => {
};
Object.assign(mixPanelProperties, eventProperties);
await Promise.all([mixpanel.track(eventType, mixPanelProperties),
mixpanel.people.set(mixPanelProperties.distinct_id, {
deployed_contracts: 0,
mixpanel.people.set_once({
distinct_id: isGitPod()
? getGitPodUserHash()
: shellSettings[TRACKING_SESSION_ID_KEY],
network_id: options.networkId,
node_url: options.nodeUrl,
})]);
Expand All @@ -98,7 +85,7 @@ const getEventTrackingConsent = async () => {
const answer = await new Promise((resolve) => {
rl.question(
chalk`We would like to collect data on near-cli usage to improve developer experience.` +
chalk` We will never send private information. We only collect which commands are run with attributes.` +
chalk` We will never send private information. We only collect which commands are run via an anonymous identifier.` +
chalk`{bold.yellow Would you like to opt in (y/n)? }`,
async (consentToEventTracking) => {
if (consentToEventTracking.toLowerCase() == 'y') {
Expand All @@ -122,57 +109,6 @@ const getEventTrackingConsent = async () => {
}
};

const getIdTrackingConsent = async () => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
try {
for (let attempts = 0; attempts < 3; attempts++) {
const answer = await new Promise((resolve) => {
rl.question(
chalk`We would like to help with your development journey with NEAR.` +
chalk` We will ask you to share your account Id while using command. ` +
chalk`{bold.yellow Would you like to share the account Id (y/n)? }`,
async (consentToEventTracking) => {
if (consentToEventTracking.toLowerCase() == 'y') {
resolve(true);
} else if (
consentToEventTracking.toLowerCase() == 'n'
) {
resolve(false);
}
resolve(undefined);
}
);
});
if (answer !== undefined) {
return answer;
}
}
return false; // If they can't figure it out in this many attempts, just opt out
} finally {
rl.close();
}
};

const askForId = async (options) => {
const shellSettings = settings.getShellSettings();
if(shouldTrackID(shellSettings)){
const id = isGitPod() ? getGitPodUserHash() : shellSettings[TRACKING_SESSION_ID_KEY];
await Promise.all([
mixpanel.alias(options.accountId, id),
mixpanel.people.set(id, {account_id: options.accountId})
]);
}else if(shouldNOTTrackID(shellSettings)){
return;
}
else{
shellSettings[TRACKING_ID_KEY] = (await getIdTrackingConsent());
settings.saveShellSettings(shellSettings);
}
};

const askForConsentIfNeeded = async (options) => {
const shellSettings = settings.getShellSettings();
// if the appropriate option is not in settings, ask now and save settings.
Expand All @@ -192,16 +128,13 @@ const askForConsentIfNeeded = async (options) => {
};

const trackDeployedContract = async () => {
const shellSettings = settings.getShellSettings();
const id = isGitPod() ? getGitPodUserHash() : shellSettings[TRACKING_SESSION_ID_KEY];
await mixpanel.people.increment(id, 'deployed_contracts');
await mixpanel.people.increment({deployed_contracts: 1});
};

module.exports = {
track,
askForConsentIfNeeded,
trackDeployedContract,
askForId,
// Some of the event ids are auto-generated runtime with the naming convention event_id_shell_{command}_start

EVENT_ID_CREATE_ACCOUNT_END: 'event_id_shell_create-account_end',
Expand Down