-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate function for asking id (#581)
* generate function for asking id * asking with more commands * change message * fix issue for asking repeatly * update test * fix identity setting * change config * change back Co-authored-by: Mike Purvis <mikedotexe@gmail.com>
- Loading branch information
1 parent
496ed57
commit 6a71513
Showing
7 changed files
with
126 additions
and
43 deletions.
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,45 +1,50 @@ | ||
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 doesn'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); | ||
} 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}"`); | ||
}) | ||
}; | ||
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); | ||
} | ||
|
||
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