diff --git a/README.md b/README.md index 157ff88e..82e90bfe 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,27 @@ Key pair with ed25519:33Vn9VtNEtWQPPd1f4jf5HzJ5weLcvGHU8oz7o5UnPqy public key fo --- +#### 1b) `near generate-key --curveType=[secp256k1 \ ed25519 (default)]` + +> Creates a key pair locally in `.near-credentials` with an [implicit account](http://docs.near.org/docs/roles/integrator/implicit-accounts) as the accountId with certain key curve type. _(hash representation of the public key)_ + +```bash +near generate-key --curveType=secp256k1 +``` + +
+Example Response +

+ +```bash +Key pair with ed25519:33Vn9VtNEtWQPPd1f4jf5HzJ5weLcvGHU8oz7o5UnPqy public key for an account "1e5b1346bdb4fc5ccd465f6757a9082a84bcacfd396e7d80b0c726252fe8b3e8" +``` + +

+
+ +--- + #### 2) `near generate-key accountId` > Creates a key pair locally in `.near-credentials` with an `accountId` that you specify. diff --git a/commands/generate-key.js b/commands/generate-key.js index 2bae5499..b807f202 100644 --- a/commands/generate-key.js +++ b/commands/generate-key.js @@ -1,3 +1,4 @@ +const keyCurveTypes = require('near-api-js').keyCurveTypes; const KeyPair = require('near-api-js').KeyPair; const exitOnError = require('../utils/exit-on-error'); const implicitAccountId = require('../utils/implicit-accountid'); @@ -9,6 +10,10 @@ module.exports = { .option('yolo', { description: 'Do not ask for extra confirmation when using Ledger', type: 'boolean', + }) + .option('curveType', { + description: 'Use curve type ed25519 or secp256k1 for key pair generate', + type: 'string', }), handler: exitOnError(async (argv) => { let near = await require('../utils/connect')(argv); @@ -44,7 +49,10 @@ module.exports = { // 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'); + const curveType = (!argv.curveType) ? 'ed25519' : keyCurveTypes.validate(argv.curveType); + console.log('Generate key pair from random on curve type: ' + curveType); + + const keyPair = KeyPair.fromRandom(curveType); argv.publicKey = keyPair.publicKey.toString(); argv.accountId = argv.accountId || implicitAccountId(argv.publicKey); await keyStore.setKey(argv.networkId, argv.accountId, keyPair);