Socket
Socket
Sign inDemoInstall

@near-js/keystores-node

Package Overview
Dependencies
Maintainers
0
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@near-js/keystores-node - npm Package Compare versions

Comparing version 0.0.12 to 0.1.0-next.0

7

lib/index.js

@@ -1,6 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnencryptedFileSystemKeyStore = exports.readKeyFile = void 0;
var unencrypted_file_system_keystore_1 = require("./unencrypted_file_system_keystore");
Object.defineProperty(exports, "readKeyFile", { enumerable: true, get: function () { return unencrypted_file_system_keystore_1.readKeyFile; } });
Object.defineProperty(exports, "UnencryptedFileSystemKeyStore", { enumerable: true, get: function () { return unencrypted_file_system_keystore_1.UnencryptedFileSystemKeyStore; } });
export { readKeyFile, UnencryptedFileSystemKeyStore } from './unencrypted_file_system_keystore';

171

lib/unencrypted_file_system_keystore.js

@@ -1,21 +0,6 @@

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnencryptedFileSystemKeyStore = exports.readKeyFile = void 0;
const crypto_1 = require("@near-js/crypto");
const keystores_1 = require("@near-js/keystores");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const util_1 = require("util");
import { KeyPair } from '@near-js/crypto';
import { KeyStore } from '@near-js/keystores';
import fs from 'fs';
import path from 'path';
import { promisify as _promisify } from 'util';
/* remove for versions not referenced by near-api-js */

@@ -28,42 +13,35 @@ const promisify = (fn) => {

}
return (0, util_1.promisify)(fn);
return _promisify(fn);
};
const exists = promisify(fs_1.default.exists);
const readFile = promisify(fs_1.default.readFile);
const writeFile = promisify(fs_1.default.writeFile);
const unlink = promisify(fs_1.default.unlink);
const readdir = promisify(fs_1.default.readdir);
const mkdir = promisify(fs_1.default.mkdir);
const exists = promisify(fs.exists);
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
const unlink = promisify(fs.unlink);
const readdir = promisify(fs.readdir);
const mkdir = promisify(fs.mkdir);
/** @hidden */
function loadJsonFile(filename) {
return __awaiter(this, void 0, void 0, function* () {
const content = yield readFile(filename);
return JSON.parse(content.toString());
});
async function loadJsonFile(filename) {
const content = await readFile(filename);
return JSON.parse(content.toString());
}
function ensureDir(dir) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield mkdir(dir, { recursive: true });
async function ensureDir(dir) {
try {
await mkdir(dir, { recursive: true });
}
catch (err) {
if (err.code !== 'EEXIST') {
throw err;
}
catch (err) {
if (err.code !== 'EEXIST') {
throw err;
}
}
});
}
}
/** @hidden */
function readKeyFile(filename) {
return __awaiter(this, void 0, void 0, function* () {
const accountInfo = yield loadJsonFile(filename);
// The private key might be in private_key or secret_key field.
let privateKey = accountInfo.private_key;
if (!privateKey && accountInfo.secret_key) {
privateKey = accountInfo.secret_key;
}
return [accountInfo.account_id, crypto_1.KeyPair.fromString(privateKey)];
});
export async function readKeyFile(filename) {
const accountInfo = await loadJsonFile(filename);
// The private key might be in private_key or secret_key field.
let privateKey = accountInfo.private_key;
if (!privateKey && accountInfo.secret_key) {
privateKey = accountInfo.secret_key;
}
return [accountInfo.account_id, KeyPair.fromString(privateKey)];
}
exports.readKeyFile = readKeyFile;
/**

@@ -92,3 +70,5 @@ * This class is used to store keys on the file system.

*/
class UnencryptedFileSystemKeyStore extends keystores_1.KeyStore {
export class UnencryptedFileSystemKeyStore extends KeyStore {
/** @hidden */
keyDir;
/**

@@ -99,3 +79,3 @@ * @param keyDir base directory for key storage. Keys will be stored in `keyDir/networkId/accountId.json`

super();
this.keyDir = path_1.default.resolve(keyDir);
this.keyDir = path.resolve(keyDir);
}

@@ -108,8 +88,6 @@ /**

*/
setKey(networkId, accountId, keyPair) {
return __awaiter(this, void 0, void 0, function* () {
yield ensureDir(`${this.keyDir}/${networkId}`);
const content = { account_id: accountId, public_key: keyPair.getPublicKey().toString(), private_key: keyPair.toString() };
yield writeFile(this.getKeyFilePath(networkId, accountId), JSON.stringify(content), { mode: 0o600 });
});
async setKey(networkId, accountId, keyPair) {
await ensureDir(`${this.keyDir}/${networkId}`);
const content = { account_id: accountId, public_key: keyPair.getPublicKey().toString(), private_key: keyPair.toString() };
await writeFile(this.getKeyFilePath(networkId, accountId), JSON.stringify(content), { mode: 0o600 });
}

@@ -122,11 +100,9 @@ /**

*/
getKey(networkId, accountId) {
return __awaiter(this, void 0, void 0, function* () {
// Find key / account id.
if (!(yield exists(this.getKeyFilePath(networkId, accountId)))) {
return null;
}
const accountKeyPair = yield readKeyFile(this.getKeyFilePath(networkId, accountId));
return accountKeyPair[1];
});
async getKey(networkId, accountId) {
// Find key / account id.
if (!await exists(this.getKeyFilePath(networkId, accountId))) {
return null;
}
const accountKeyPair = await readKeyFile(this.getKeyFilePath(networkId, accountId));
return accountKeyPair[1];
}

@@ -138,8 +114,6 @@ /**

*/
removeKey(networkId, accountId) {
return __awaiter(this, void 0, void 0, function* () {
if (yield exists(this.getKeyFilePath(networkId, accountId))) {
yield unlink(this.getKeyFilePath(networkId, accountId));
}
});
async removeKey(networkId, accountId) {
if (await exists(this.getKeyFilePath(networkId, accountId))) {
await unlink(this.getKeyFilePath(networkId, accountId));
}
}

@@ -149,10 +123,8 @@ /**

*/
clear() {
return __awaiter(this, void 0, void 0, function* () {
for (const network of yield this.getNetworks()) {
for (const account of yield this.getAccounts(network)) {
yield this.removeKey(network, account);
}
async clear() {
for (const network of await this.getNetworks()) {
for (const account of await this.getAccounts(network)) {
await this.removeKey(network, account);
}
});
}
}

@@ -167,11 +139,9 @@ /** @hidden */

*/
getNetworks() {
return __awaiter(this, void 0, void 0, function* () {
const files = yield readdir(this.keyDir);
const result = new Array();
files.forEach((item) => {
result.push(item);
});
return result;
async getNetworks() {
const files = await readdir(this.keyDir);
const result = new Array();
files.forEach((item) => {
result.push(item);
});
return result;
}

@@ -182,12 +152,10 @@ /**

*/
getAccounts(networkId) {
return __awaiter(this, void 0, void 0, function* () {
if (!(yield exists(`${this.keyDir}/${networkId}`))) {
return [];
}
const files = yield readdir(`${this.keyDir}/${networkId}`);
return files
.filter(file => file.endsWith('.json'))
.map(file => file.replace(/.json$/, ''));
});
async getAccounts(networkId) {
if (!await exists(`${this.keyDir}/${networkId}`)) {
return [];
}
const files = await readdir(`${this.keyDir}/${networkId}`);
return files
.filter(file => file.endsWith('.json'))
.map(file => file.replace(/.json$/, ''));
}

@@ -199,2 +167,1 @@ /** @hidden */

}
exports.UnencryptedFileSystemKeyStore = UnencryptedFileSystemKeyStore;
{
"name": "@near-js/keystores-node",
"version": "0.0.12",
"version": "0.1.0-next.0",
"description": "KeyStore implementation for working with keys in the local filesystem",
"main": "lib/index.js",
"type": "module",
"keywords": [],

@@ -10,10 +11,12 @@ "author": "",

"dependencies": {
"@near-js/crypto": "1.2.4",
"@near-js/keystores": "0.0.12"
"@near-js/crypto": "1.3.0-next.0",
"@near-js/keystores": "0.1.0-next.0"
},
"devDependencies": {
"@types/node": "18.11.18",
"jest": "26.0.1",
"ts-jest": "26.5.6",
"typescript": "4.9.4"
"@jest/globals": "^29.7.0",
"@types/node": "20.0.0",
"jest": "29.7.0",
"ts-jest": "29.1.5",
"typescript": "5.4.5",
"tsconfig": "0.0.0"
},

@@ -26,8 +29,6 @@ "files": [

"compile": "tsc -p tsconfig.json",
"lint:js": "eslint -c ../../.eslintrc.js.yml test/**/*.js --no-eslintrc",
"lint:js:fix": "eslint -c ../../.eslintrc.js.yml test/**/*.js --no-eslintrc --fix",
"lint:ts": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts --no-eslintrc",
"lint:ts:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts --no-eslintrc --fix",
"test": "jest test"
"lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc",
"lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix",
"test": "jest"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc