diff --git a/commands/add-key.js b/commands/add-key.js index 38966b52..3fb96dba 100644 --- a/commands/add-key.js +++ b/commands/add-key.js @@ -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 ', @@ -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); diff --git a/commands/delete-key.js b/commands/delete-key.js index 0a408cc0..380c771d 100644 --- a/commands/delete-key.js +++ b/commands/delete-key.js @@ -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 ', @@ -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); diff --git a/commands/generate-key.js b/commands/generate-key.js index adf4b897..fecb72cf 100644 --- a/commands/generate-key.js +++ b/commands/generate-key.js @@ -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}"`); + }) +}; diff --git a/index.js b/index.js index 3b012a1f..ce9b6963 100644 --- a/index.js +++ b/index.js @@ -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}`); @@ -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); @@ -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(); @@ -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); @@ -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(); @@ -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); @@ -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); diff --git a/test/test_account_operations.sh b/test/test_account_operations.sh index a40dc851..73bebd4d 100755 --- a/test/test_account_operations.sh +++ b/test/test_account_operations.sh @@ -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 diff --git a/test/test_generate_key.sh b/test/test_generate_key.sh index f4897ef1..20288316 100755 --- a/test/test_generate_key.sh +++ b/test/test_generate_key.sh @@ -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 @@ -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.*" diff --git a/utils/eventtracking.js b/utils/eventtracking.js index 8903e8d0..a233e947 100644 --- a/utils/eventtracking.js +++ b/utils/eventtracking.js @@ -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; @@ -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)) { @@ -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, })]); @@ -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') { @@ -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. @@ -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',