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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from nearprotocol/generate-key
Add `near generate-key` command
- Loading branch information
Showing
4 changed files
with
73 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const KeyPair = require('nearlib').KeyPair; | ||
const exitOnError = require('../utils/exit-on-error'); | ||
|
||
module.exports = { | ||
command: 'generate-key <account-id>', | ||
desc: 'generate key ', | ||
builder: (yargs) => yargs, | ||
handler: exitOnError(async (argv) => { | ||
let near = await require('../utils/connect')(argv); | ||
if (argv.accountId) { | ||
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`); | ||
} else { | ||
const keyPair = KeyPair.fromRandom('ed25519'); | ||
await keyStore.setKey(argv.networkId, argv.accountId, keyPair); | ||
console.log(`Generated key pair with ${keyPair.publicKey} public key`); | ||
} | ||
} | ||
}) | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
#!/bin/bash | ||
set -ex | ||
KEY_FILE=neardev/default/generate-key-test.json | ||
rm -f "$KEY_FILE" | ||
|
||
RESULT=$(./bin/near generate-key generate-key-test --networkId default) | ||
echo $RESULT | ||
|
||
if [[ ! -f "${KEY_FILE}" ]]; then | ||
echo "FAILURE Key file doesn't exist" | ||
exit 1 | ||
fi | ||
|
||
EXPECTED=".*Generated key pair with ed25519:.+ public key.*" | ||
if [[ ! "$RESULT" =~ $EXPECTED ]]; then | ||
echo FAILURE Unexpected output from near generate-key | ||
exit 1 | ||
fi | ||
|
||
RESULT2=$(./bin/near generate-key generate-key-test --networkId default) | ||
echo $RESULT2 | ||
|
||
EXPECTED2=".*Account has existing key pair with ed25519:.+ public key.*" | ||
if [[ ! "$RESULT2" =~ $EXPECTED2 ]]; then | ||
echo FAILURE Unexpected output from near generate-key when key already exists | ||
exit 1 | ||
fi |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = (promiseFn) => async (...args) => { | ||
const promise = promiseFn.apply(null, args); | ||
try { | ||
await promise; | ||
} catch (e) { | ||
console.log('Error: ', e); | ||
process.exit(1); | ||
} | ||
}; |