Skip to content

Commit

Permalink
[near cli] promisify fs operations
Browse files Browse the repository at this point in the history
  • Loading branch information
janedegtiareva committed Feb 27, 2019
1 parent 652aa84 commit 60d06c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "near-shell",
"version": "0.0.1",
"version": "0.0.3",
"description": "Command line utilities to interact with near blockchain",
"main": "index.js",
"scripts": {
Expand All @@ -18,13 +18,16 @@
"url": "https://github.com/nearprotocol/near-shell/issues"
},
"homepage": "https://github.com/nearprotocol/near-shell#readme",
"devDependencies": {
"bin": {
"near": "near"
},
"devDependencies": {},
"dependencies": {
"assemblyscript": "github:nearprotocol/assemblyscript",
"fs": "0.0.1-security",
"gulp": "^4.0.0",
"nearlib": "file:../nearcore/nearlib",
"run-sequence": "^2.2.1",
"yargs": "^13.2.1"
},
"dependencies": {}
}
}
19 changes: 12 additions & 7 deletions unencrypted_file_system_keystore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const keyDir = './neardev';
const keyFile = 'devkey.json';
const KeyPair = require('nearlib/signing/key_pair')
const {promisify} = require('util');

/**
* Unencrypted file system key store.
Expand All @@ -10,23 +11,23 @@ class UnencryptedFileSystemKeyStore {
constructor() {}

async setKey(accountId, keypair) {
if (!fs.existsSync(keyDir)){
fs.mkdirSync(keyDir);
if (!await promisify(fs.exists)(keyDir)){
await promisify(fs.mkdir)(keyDir);
}
const keyFileContent = {
public_key: keypair.getPublicKey(),
secret_key: keypair.getSecretKey(),
account_id: accountId
};
const writeResult = await fs.writeFileSync(this.getKeyFilePath(), JSON.stringify(keyFileContent));
await promisify(fs.writeFile)(this.getKeyFilePath(), JSON.stringify(keyFileContent));
}

async getKey(accountId) {
// Find keys/account id
if (!fs.existsSync(this.getKeyFilePath())) {
if (!await promisify(fs.exists)(this.getKeyFilePath())) {
throw 'Key lookup failed. Please make sure you set up an account.';
}
const rawKey = JSON.parse(fs.readFileSync(this.getKeyFilePath()));
const rawKey = await this.getRawKey();
if (!rawKey.public_key || !rawKey.secret_key || !rawKey.account_id) {
throw 'Deployment failed. neardev/devkey.json format problem. Please make sure file contains public_key, secret_key, and account_id".';
}
Expand All @@ -41,10 +42,10 @@ class UnencryptedFileSystemKeyStore {
* Returns all account ids.
*/
async getAccountIds() {
if (!fs.existsSync(this.getKeyFilePath())) {
if (!await promisify(fs.exists)(this.getKeyFilePath())) {
return [];
}
const rawKey = JSON.parse(fs.readFileSync(this.getKeyFilePath()));
const rawKey = await this.getRawKey();
if (!rawKey.public_key || !rawKey.secret_key || !rawKey.account_id) {
return [];
}
Expand All @@ -59,6 +60,10 @@ class UnencryptedFileSystemKeyStore {
getKeyFilePath() {
return keyDir + "/" + keyFile;
}

async getRawKey() {
return JSON.parse(await promisify(fs.readFile)(this.getKeyFilePath()));
}
}

module.exports = UnencryptedFileSystemKeyStore;

0 comments on commit 60d06c7

Please sign in to comment.