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

Commit

Permalink
Don't overwrite existing key in near generate-key
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Nov 5, 2019
1 parent 69e520b commit f9a0b19
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 8 additions & 3 deletions commands/generate-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ module.exports = {
let near = await require('../utils/connect')(argv);
if (argv.accountId) {
const { deps: { keyStore }} = near.config;
const keyPair = KeyPair.fromRandom('ed25519');
await keyStore.setKey(argv.networkId, argv.accountId, keyPair);
console.log(`Generated key pair with ${keyPair.publicKey} public key`);
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`);
}
}
})
};
11 changes: 10 additions & 1 deletion test/test_generate_key.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#!/bin/bash
set -ex
KEY_FILE=neardev/default/generate-key-test.json
rm -f KEY_FILE
rm -f "$KEY_FILE"

RESULT=$(./bin/near generate-key generate-key-test --networkId default)
echo $RESULT
Expand All @@ -16,4 +16,13 @@ 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

0 comments on commit f9a0b19

Please sign in to comment.