This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
generate function for asking id #581
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fe5a618
generate function for asking id
icerove 9c0d408
asking with more commands
icerove cae81d2
change message
icerove 5e89d23
fix issue for asking repeatly
icerove 853776d
update test
icerove 25e6675
fix identity setting
icerove a6db13b
change config
icerove 4701a3e
change back
icerove 76b9e21
Merge branch 'master' into mixpanel/identity-management
mikedotexe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,48 @@ | ||
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(async (argv) => { | ||
let near = await require('../utils/connect')(argv); | ||
handler: exitOnError(generateKey) | ||
}; | ||
|
||
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; | ||
} | ||
async function generateKey(options) { | ||
await eventtracking.askForId(options); | ||
const near = await connect(options); | ||
|
||
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; | ||
if (options.usingLedger) { | ||
if (options.accountId) { | ||
console.log('WARN: Account id is provided but ignored in case of using Ledger.'); | ||
} | ||
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 doen't match already. | ||
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 (!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); | ||
} | ||
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); | ||
} | ||
|
||
console.log(`Key pair with ${argv.publicKey} public key for an account "${argv.accountId}"`); | ||
}) | ||
}; | ||
console.log(`Key pair with ${options.publicKey} public key for an account "${options.accountId}"`); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ 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; | ||
|
@@ -40,6 +41,20 @@ 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)) { | ||
|
@@ -60,10 +75,8 @@ const track = async (eventType, eventProperties, options) => { | |
}; | ||
Object.assign(mixPanelProperties, eventProperties); | ||
await Promise.all([mixpanel.track(eventType, mixPanelProperties), | ||
mixpanel.people.set_once({ | ||
distinct_id: isGitPod() | ||
? getGitPodUserHash() | ||
: shellSettings[TRACKING_SESSION_ID_KEY], | ||
mixpanel.people.set(mixPanelProperties.distinct_id, { | ||
deployed_contracts: 0, | ||
network_id: options.networkId, | ||
node_url: options.nodeUrl, | ||
})]); | ||
|
@@ -85,7 +98,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 via an anonymous identifier.` + | ||
chalk` We will never send private information. We only collect which commands are run with attributes.` + | ||
chalk`{bold.yellow Would you like to opt in (y/n)? }`, | ||
async (consentToEventTracking) => { | ||
if (consentToEventTracking.toLowerCase() == 'y') { | ||
|
@@ -109,6 +122,57 @@ 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)){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is when answer is 'N' or 'n' |
||
return; | ||
} | ||
else{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is first time to ask for that |
||
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. | ||
|
@@ -128,13 +192,16 @@ const askForConsentIfNeeded = async (options) => { | |
}; | ||
|
||
const trackDeployedContract = async () => { | ||
await mixpanel.people.increment({deployed_contracts: 1}); | ||
const shellSettings = settings.getShellSettings(); | ||
const id = isGitPod() ? getGitPodUserHash() : shellSettings[TRACKING_SESSION_ID_KEY]; | ||
await mixpanel.people.increment(id, 'deployed_contracts'); | ||
}; | ||
|
||
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', | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is when the answer is 'Y' or 'y'