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

Add ability to generate secp256k1 keys #863

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

<details>
<summary><strong>Example Response</strong></summary>
<p>

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

</p>
</details>

---

#### 2) `near generate-key accountId`

> Creates a key pair locally in `.near-credentials` with an `accountId` that you specify.
Expand Down
6 changes: 5 additions & 1 deletion commands/generate-key.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const keyCurveTypes = require('near-api-js').keyCurveTypes;
Copy link
Collaborator

@volovyks volovyks Nov 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TarasenkoFirelabs this line gives me undefined.
And as a result: TypeError: Cannot read property 'validate' of undefined

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still working onto this task in near-api-js repo

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, then it's better to keep this one in a draft. Ping me when it will be ready for review, thanks!

const KeyPair = require('near-api-js').KeyPair;
const exitOnError = require('../utils/exit-on-error');
const implicitAccountId = require('../utils/implicit-accountid');
Expand Down Expand Up @@ -44,7 +45,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);
Expand Down